BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
JobListViewDelegate Class Reference

Description

ViewDelegate to show progress bar JobQueuListView.

Definition at line 26 of file JobListViewDelegate.h.

Inheritance diagram for JobListViewDelegate:
[legend]
Collaboration diagram for JobListViewDelegate:
[legend]

Signals

void cancelButtonClicked (const QModelIndex &index)
 

Public Member Functions

 JobListViewDelegate (QWidget *parent)
 
bool editorEvent (QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override
 
void paint (QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
 

Private Member Functions

void drawCustomProjectBar (const JobItem *item, QPainter *painter, const QStyleOptionViewItem &option) const
 
QRect getButtonRect (QRect optionRect) const
 Returns rectangle for button. More...
 
QRect getProgressBarRect (QRect optionRect) const
 Returns rectangle for progress bar. More...
 
QRect getTextRect (QRect optionRect) const
 Returns rectangle for text. More...
 

Private Attributes

QStyle::State m_buttonState
 
QMap< JobStatus, QColor > m_status_to_color
 

Constructor & Destructor Documentation

◆ JobListViewDelegate()

JobListViewDelegate::JobListViewDelegate ( QWidget *  parent)

Definition at line 25 of file JobListViewDelegate.cpp.

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 }
@ 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
QStyle::State m_buttonState
QMap< JobStatus, QColor > m_status_to_color

References Canceled, Completed, Failed, Idle, m_buttonState, m_status_to_color, and Running.

Member Function Documentation

◆ cancelButtonClicked

void JobListViewDelegate::cancelButtonClicked ( const QModelIndex &  index)
signal

Referenced by editorEvent().

◆ drawCustomProjectBar()

void JobListViewDelegate::drawCustomProjectBar ( const JobItem item,
QPainter *  painter,
const QStyleOptionViewItem &  option 
) const
private

Definition at line 105 of file JobListViewDelegate.cpp.

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 }
int getProgress() const
Definition: JobItem.cpp:201
JobStatus getStatus() const
Definition: JobItem.cpp:106
QRect getProgressBarRect(QRect optionRect) const
Returns rectangle for progress bar.

References JobItem::getProgress(), getProgressBarRect(), JobItem::getStatus(), and m_status_to_color.

Referenced by paint().

Here is the call graph for this function:

◆ editorEvent()

bool JobListViewDelegate::editorEvent ( QEvent *  event,
QAbstractItemModel *  model,
const QStyleOptionViewItem &  option,
const QModelIndex &  index 
)
override

Definition at line 70 of file JobListViewDelegate.cpp.

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 }
bool isRunning() const
Definition: JobItem.cpp:129
void cancelButtonClicked(const QModelIndex &index)
QRect getButtonRect(QRect optionRect) const
Returns rectangle for button.

References cancelButtonClicked(), getButtonRect(), JobItem::isRunning(), and m_buttonState.

Here is the call graph for this function:

◆ getButtonRect()

QRect JobListViewDelegate::getButtonRect ( QRect  optionRect) const
private

Returns rectangle for button.

Definition at line 152 of file JobListViewDelegate.cpp.

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 }

Referenced by editorEvent(), and paint().

◆ getProgressBarRect()

QRect JobListViewDelegate::getProgressBarRect ( QRect  optionRect) const
private

Returns rectangle for progress bar.

Definition at line 141 of file JobListViewDelegate.cpp.

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 }

Referenced by drawCustomProjectBar().

◆ getTextRect()

QRect JobListViewDelegate::getTextRect ( QRect  optionRect) const
private

Returns rectangle for text.

Definition at line 130 of file JobListViewDelegate.cpp.

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 }

Referenced by paint().

◆ paint()

void JobListViewDelegate::paint ( QPainter *  painter,
const QStyleOptionViewItem &  option,
const QModelIndex &  index 
) const
override

Definition at line 36 of file JobListViewDelegate.cpp.

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 }
QString jobName() const
Definition: JobItem.cpp:84
QRect getTextRect(QRect optionRect) const
Returns rectangle for text.
void drawCustomProjectBar(const JobItem *item, QPainter *painter, const QStyleOptionViewItem &option) const

References drawCustomProjectBar(), getButtonRect(), getTextRect(), JobItem::isRunning(), JobItem::jobName(), and m_buttonState.

Here is the call graph for this function:

Member Data Documentation

◆ m_buttonState

QStyle::State JobListViewDelegate::m_buttonState
private

Definition at line 41 of file JobListViewDelegate.h.

Referenced by JobListViewDelegate(), editorEvent(), and paint().

◆ m_status_to_color

QMap<JobStatus, QColor> JobListViewDelegate::m_status_to_color
private

Definition at line 49 of file JobListViewDelegate.h.

Referenced by JobListViewDelegate(), and drawCustomProjectBar().


The documentation for this class was generated from the following files: