BornAgain  1.19.0
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/coregui/Views/JobWidgets/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 
19 #include <QApplication>
20 #include <QMouseEvent>
21 #include <QPaintDevice>
22 #include <QPainter>
23 #include <QStyleOptionProgressBarV2>
24 #include <QWidget>
25 
26 JobListViewDelegate::JobListViewDelegate(QWidget* parent) : QItemDelegate(parent)
27 {
28  m_buttonState = QStyle::State_Enabled;
29  m_status_to_color["Idle"] = QColor(255, 286, 12);
30  m_status_to_color["Running"] = QColor(5, 150, 230);
31  m_status_to_color["Completed"] = QColor(5, 150, 230);
32  m_status_to_color["Canceled"] = QColor(186, 0, 0);
33  m_status_to_color["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 JobModel* model = static_cast<const JobModel*>(index.model());
43  ASSERT(model);
44 
45  const JobItem* item = model->getJobItemForIndex(index);
46  ASSERT(item);
47 
48  painter->save();
49 
50  painter->setRenderHint(QPainter::Antialiasing, true);
51 
52  QString text = item->itemName();
53  QRect textRect = getTextRect(option.rect);
54  painter->drawText(textRect, text);
55 
56  drawCustomProjectBar(item, painter, option);
57 
58  if (item->isRunning()) {
59  QStyleOptionButton button;
60  button.rect = getButtonRect(option.rect);
61  button.state = m_buttonState | QStyle::State_Enabled;
62  button.icon = QIcon(":/images/dark-close.svg");
63  button.iconSize = QSize(12, 12);
64 
65  QApplication::style()->drawControl(QStyle::CE_PushButton, &button, painter);
66  }
67 
68  painter->restore();
69 }
70 
71 bool JobListViewDelegate::editorEvent(QEvent* event, QAbstractItemModel* model,
72  const QStyleOptionViewItem& option, const QModelIndex& index)
73 {
74  if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonRelease) {
75  } else {
76  m_buttonState = QStyle::State_Raised;
77  return QItemDelegate::editorEvent(event, model, option, index);
78  }
79 
80  const JobModel* jqmodel = static_cast<const JobModel*>(index.model());
81  ASSERT(model);
82 
83  const JobItem* item = jqmodel->getJobItemForIndex(index);
84  ASSERT(item);
85 
86  if (!item->isRunning())
87  return false;
88 
89  QRect buttonRect = getButtonRect(option.rect);
90 
91  QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
92  if (!buttonRect.contains(mouseEvent->pos())) {
93  m_buttonState = QStyle::State_Raised;
94  return false; // so that selection can change
95  }
96 
97  if (event->type() == QEvent::MouseButtonPress) {
98  m_buttonState = QStyle::State_Sunken;
99  } else if (event->type() == QEvent::MouseButtonRelease) {
100  m_buttonState = QStyle::State_Raised;
101  qDebug("JobListViewDelegate::editorEvent() -> cancel clicked");
102  emit cancelButtonClicked(index);
103  }
104  return true;
105 }
106 
107 void JobListViewDelegate::drawCustomProjectBar(const JobItem* item, QPainter* painter,
108  const QStyleOptionViewItem& option) const
109 {
110  int progress = item->getProgress();
111  QRect rect = getProgressBarRect(option.rect);
112 
113  painter->save();
114  painter->setRenderHint(QPainter::Antialiasing);
115  painter->setBrush(QColor(204, 223, 230));
116  painter->setPen(QColor("transparent"));
117  QRect rect2(rect.x(), rect.y(), rect.width(), rect.height());
118  painter->drawRoundedRect(rect2, 2, 2);
119  painter->restore();
120 
121  int progBarWidth = float((rect.width() * progress) / 100);
122  painter->save();
123  painter->setRenderHint(QPainter::Antialiasing);
124  painter->setPen(QColor("transparent"));
125  painter->setBrush(m_status_to_color[item->getStatus()]);
126  QRect rect5(rect.x(), rect.y(), progBarWidth, rect.height());
127  painter->drawRoundedRect(rect5, 2, 2);
128  painter->restore();
129 }
130 
131 //! returns rectangle for text
132 QRect JobListViewDelegate::getTextRect(QRect optionRect) const
133 {
134  int width = optionRect.width() * 0.4;
135  int height = optionRect.height();
136  int x = optionRect.x() + 3;
137  int y = optionRect.y();
138  QRect result(x, y, width, height);
139  return result;
140 }
141 
142 //! returns rectangle for progress bar
143 QRect JobListViewDelegate::getProgressBarRect(QRect optionRect) const
144 {
145  int width = optionRect.width() * 0.4;
146  int height = optionRect.height() * 0.6;
147  int x = optionRect.x() + optionRect.width() * 0.5;
148  int y = optionRect.y() + (optionRect.height() - height) / 2.;
149  QRect result(x, y, width, height);
150  return result;
151 }
152 
153 //! returns rectangle for button
154 QRect JobListViewDelegate::getButtonRect(QRect optionRect) const
155 {
156  int height = 10;
157  int width = 10;
158  int x = optionRect.x() + optionRect.width() * 0.92;
159  int y = optionRect.y() + (optionRect.height() - height) / 2.;
160  QRect result(x, y, width, height);
161  return result;
162 }
#define ASSERT(condition)
Definition: Assert.h:31
Defines class JobItem.
Defines class JobListViewDelegate.
Defines class JobModel.
int getProgress() const
Definition: JobItem.cpp:199
QString getStatus() const
Definition: JobItem.cpp:123
bool isRunning() const
Definition: JobItem.cpp:145
JobListViewDelegate(QWidget *parent)
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
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< QString, QColor > m_status_to_color
void drawCustomProjectBar(const JobItem *item, QPainter *painter, const QStyleOptionViewItem &option) const
const JobItem * getJobItemForIndex(const QModelIndex &index) const
Definition: JobModel.cpp:46
QString itemName() const
Get item name, return display name if no name is set.
Defines Utils namespace.