BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
JobListViewDelegate.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/Job/JobListViewDelegate.cpp
6 //! @brief Implements class JobListViewDelegate
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
16 #include "GUI/Model/Job/JobItem.h"
18 #include <QApplication>
19 #include <QMouseEvent>
20 #include <QPaintDevice>
21 #include <QPainter>
22 #include <QStyleOptionProgressBarV2>
23 #include <QWidget>
24 
26  : QItemDelegate(parent)
27 {
28  m_buttonState = QStyle::State_Enabled;
29  m_status_to_color[JobStatus::Idle] = QColor(255, 286, 12);
30  m_status_to_color[JobStatus::Running] = QColor(5, 150, 230);
31  m_status_to_color[JobStatus::Completed] = QColor(5, 150, 230);
32  m_status_to_color[JobStatus::Canceled] = QColor(186, 0, 0);
33  m_status_to_color[JobStatus::Failed] = QColor(255, 186, 12);
34 }
35 
36 void JobListViewDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,
37  const QModelIndex& index) const
38 {
39  if (option.state & QStyle::State_Selected)
40  painter->fillRect(option.rect, option.palette.highlight());
41 
42  const auto* model = dynamic_cast<const JobListModel*>(index.model());
43  ASSERT(model);
44 
45  const JobItem* item = model->jobForIndex(index);
46  ASSERT(item);
47 
48  painter->save();
49 
50  painter->setRenderHint(QPainter::Antialiasing, true);
51 
52  QRect textRect = getTextRect(option.rect);
53  painter->drawText(textRect, item->jobName());
54 
55  drawCustomProjectBar(item, painter, option);
56 
57  if (item->isRunning()) {
58  QStyleOptionButton button;
59  button.rect = getButtonRect(option.rect);
60  button.state = m_buttonState | QStyle::State_Enabled;
61  button.icon = QIcon(":/images/dark-close.svg");
62  button.iconSize = QSize(12, 12);
63 
64  QApplication::style()->drawControl(QStyle::CE_PushButton, &button, painter);
65  }
66 
67  painter->restore();
68 }
69 
70 bool JobListViewDelegate::editorEvent(QEvent* event, QAbstractItemModel* model,
71  const QStyleOptionViewItem& option, const QModelIndex& index)
72 {
73  if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonRelease) {
74  } else {
75  m_buttonState = QStyle::State_Raised;
76  return QItemDelegate::editorEvent(event, model, option, index);
77  }
78 
79  const auto* jqmodel = dynamic_cast<const JobListModel*>(index.model());
80  ASSERT(model);
81 
82  const JobItem* item = jqmodel->jobForIndex(index);
83  ASSERT(item);
84 
85  if (!item->isRunning())
86  return false;
87 
88  QRect buttonRect = getButtonRect(option.rect);
89 
90  auto* mouseEvent = dynamic_cast<QMouseEvent*>(event);
91  if (!buttonRect.contains(mouseEvent->pos())) {
92  m_buttonState = QStyle::State_Raised;
93  return false; // so that selection can change
94  }
95 
96  if (event->type() == QEvent::MouseButtonPress)
97  m_buttonState = QStyle::State_Sunken;
98  else if (event->type() == QEvent::MouseButtonRelease) {
99  m_buttonState = QStyle::State_Raised;
100  emit cancelButtonClicked(index);
101  }
102  return true;
103 }
104 
105 void JobListViewDelegate::drawCustomProjectBar(const JobItem* item, QPainter* painter,
106  const QStyleOptionViewItem& option) const
107 {
108  int progress = item->getProgress();
109  QRect rect = getProgressBarRect(option.rect);
110 
111  painter->save();
112  painter->setRenderHint(QPainter::Antialiasing);
113  painter->setBrush(QColor(204, 223, 230));
114  painter->setPen(QColor("transparent"));
115  QRect rect2(rect.x(), rect.y(), rect.width(), rect.height());
116  painter->drawRoundedRect(rect2, 2, 2);
117  painter->restore();
118 
119  int progBarWidth = (rect.width() * progress) / 100;
120  painter->save();
121  painter->setRenderHint(QPainter::Antialiasing);
122  painter->setPen(QColor("transparent"));
123  painter->setBrush(m_status_to_color[item->getStatus()]);
124  QRect rect5(rect.x(), rect.y(), progBarWidth, rect.height());
125  painter->drawRoundedRect(rect5, 2, 2);
126  painter->restore();
127 }
128 
129 //! Returns rectangle for text
130 QRect JobListViewDelegate::getTextRect(QRect optionRect) const
131 {
132  int width = optionRect.width() * 0.4;
133  int height = optionRect.height();
134  int x = optionRect.x() + 3;
135  int y = optionRect.y();
136  QRect result(x, y, width, height);
137  return result;
138 }
139 
140 //! Returns rectangle for progress bar
141 QRect JobListViewDelegate::getProgressBarRect(QRect optionRect) const
142 {
143  int width = optionRect.width() * 0.4;
144  int height = optionRect.height() * 0.6;
145  int x = optionRect.x() + optionRect.width() * 0.5;
146  int y = optionRect.y() + (optionRect.height() - height) / 2.;
147  QRect result(x, y, width, height);
148  return result;
149 }
150 
151 //! Returns rectangle for button
152 QRect JobListViewDelegate::getButtonRect(QRect optionRect) const
153 {
154  int height = 10;
155  int width = 10;
156  int x = optionRect.x() + optionRect.width() * 0.92;
157  int y = optionRect.y() + (optionRect.height() - height) / 2.;
158  QRect result(x, y, width, height);
159  return result;
160 }
Defines class JobItem.
Defines class JobListModel.
Defines class JobListViewDelegate.
@ Completed
the job was successfully completed
@ Canceled
the job was stopped by the user
@ Running
the job is busy calculating
@ Failed
the job aborted because it hit an error
@ Idle
the job has not been started yet
int getProgress() const
Definition: JobItem.cpp:201
QString jobName() const
Definition: JobItem.cpp:84
bool isRunning() const
Definition: JobItem.cpp:129
JobStatus getStatus() const
Definition: JobItem.cpp:106
JobListViewDelegate(QWidget *parent)
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override
void cancelButtonClicked(const QModelIndex &index)
QRect getProgressBarRect(QRect optionRect) const
Returns rectangle for progress bar.
QStyle::State m_buttonState
QRect getTextRect(QRect optionRect) const
Returns rectangle for text.
QRect getButtonRect(QRect optionRect) const
Returns rectangle for button.
QMap< JobStatus, QColor > m_status_to_color
void drawCustomProjectBar(const JobItem *item, QPainter *painter, const QStyleOptionViewItem &option) const