BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
JobSelectorActions.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/JobSelectorActions.cpp
6 //! @brief Implements class JobSelectorActions
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 
20 #include <QAction>
21 #include <QItemSelectionModel>
22 #include <QMenu>
23 #include <QPersistentModelIndex>
24 #include <memory>
25 
27  : QObject(parent)
28  , m_runJobAction(nullptr)
29  , m_removeJobAction(nullptr)
30  , m_selectionModel(nullptr)
31  , m_jobModel(jobModel)
32 {
33  m_runJobAction = new QAction("Run", this);
34  m_runJobAction->setIcon(QIcon(":/images/play.svg"));
35  m_runJobAction->setToolTip("Run currently selected job");
36  connect(m_runJobAction, &QAction::triggered, this, &JobSelectorActions::onRunJob);
37 
38  m_removeJobAction = new QAction("Remove", this);
39  m_removeJobAction->setIcon(QIcon(":/images/delete.svg"));
40  m_removeJobAction->setToolTip("Remove currently selected job.");
41  connect(m_removeJobAction, &QAction::triggered, this, &JobSelectorActions::onRemoveJob);
42 }
43 
44 void JobSelectorActions::setSelectionModel(QItemSelectionModel* selectionModel)
45 {
46  m_selectionModel = selectionModel;
47 }
48 
50 {
51  QModelIndexList indexList = m_selectionModel->selectedIndexes();
52  for (auto index : indexList) {
53  if (canRunJob(index))
54  m_jobModel->runJob(index);
55  }
56 }
57 
59 {
60  QList<QPersistentModelIndex> toRemove;
61  for (auto index : m_selectionModel->selectedIndexes())
62  if (canRemoveJob(index))
63  toRemove.append(QPersistentModelIndex(index));
64 
65  for (auto index : toRemove)
66  m_jobModel->removeJob(index);
67 }
68 
69 //! Generates context menu at given point. If indexAtPoint is provided, the actions will be done
70 //! for corresponding JobItem
71 
72 void JobSelectorActions::onContextMenuRequest(const QPoint& point, const QModelIndex& indexAtPoint)
73 {
74  QMenu menu;
75  initItemContextMenu(menu, indexAtPoint);
76  menu.exec(point);
78 }
79 
80 //! Puts all IntensityDataItem axes range to the selected job
81 
83 {
84  QModelIndexList selectedList = m_selectionModel->selectedIndexes();
85 
86  if (selected_id >= selectedList.size())
87  return;
88 
89  JobItem* referenceItem = m_jobModel->getJobItemForIndex(selectedList.at(selected_id));
90  ASSERT(referenceItem);
91 
92  IntensityDataItem* referenceDataItem = referenceItem->intensityDataItem();
93  if (!referenceDataItem)
94  return;
95 
96  for (auto index : selectedList) {
97  JobItem* jobItem = m_jobModel->getJobItemForIndex(index);
98  if (jobItem == referenceItem)
99  continue;
100  if (IntensityDataItem* dataItem = jobItem->intensityDataItem()) {
101  dataItem->setLowerX(referenceDataItem->getLowerX());
102  dataItem->setUpperX(referenceDataItem->getUpperX());
103  dataItem->setLowerY(referenceDataItem->getLowerY());
104  dataItem->setUpperY(referenceDataItem->getUpperY());
105  dataItem->setLowerZ(referenceDataItem->getLowerZ());
106  dataItem->setUpperZ(referenceDataItem->getUpperZ());
107  }
108  }
109 }
110 
111 void JobSelectorActions::initItemContextMenu(QMenu& menu, const QModelIndex& indexAtPoint)
112 {
113  menu.setToolTipsVisible(true);
114 
115  menu.addAction(m_runJobAction);
116  menu.addAction(m_removeJobAction);
117 
118  QModelIndex targetIndex = indexAtPoint;
119  if (!targetIndex.isValid()) {
120  QModelIndexList indexList = m_selectionModel->selectedIndexes();
121  if (!indexList.empty())
122  targetIndex = indexList.first();
123  }
124  m_runJobAction->setEnabled(canRunJob(targetIndex));
125  m_removeJobAction->setEnabled(canRemoveJob(targetIndex));
126 
127  setupEqualizeMenu(menu);
128 }
129 
131 {
132  menu.addSeparator();
133 
134  QMenu* equalize_menu = menu.addMenu("Equalize selected plots");
135  equalize_menu->setToolTipsVisible(true);
136  equalize_menu->setToolTip(
137  "All plots from the list of selected jobs will be equalized to the one.");
138  QModelIndexList selected = m_selectionModel->selectedIndexes();
139 
140  if (selected.size() <= 1) {
141  equalize_menu->setDisabled(true);
142  return;
143  }
144  std::sort(selected.begin(), selected.end(),
145  [](const QModelIndex& a, const QModelIndex& b) { return a.row() < b.row(); });
146 
147  for (int i = 0; i < selected.count(); ++i) {
148  JobItem* jobItem = m_jobModel->getJobItemForIndex(selected.at(i));
149  QAction* action = equalize_menu->addAction(QString("to ").append(jobItem->itemName()));
150  connect(action, &QAction::triggered, [=] { equalizeSelectedToJob(i); });
151  }
152 }
153 
155 {
156  m_runJobAction->setEnabled(value);
157  m_removeJobAction->setEnabled(value);
158 }
159 
160 bool JobSelectorActions::canRunJob(const QModelIndex& index) const
161 {
162  if (!index.isValid())
163  return false;
164 
165  const JobItem* jobItem = m_jobModel->getJobItemForIndex(index);
166 
167  if (jobItem->isRunning() || jobItem->getStatus() == "Fitting")
168  return false;
169 
170  return true;
171 }
172 
173 bool JobSelectorActions::canRemoveJob(const QModelIndex& index) const
174 {
175  if (!index.isValid())
176  return false;
177 
178  const JobItem* jobItem = m_jobModel->getJobItemForIndex(index);
179  if (jobItem->isRunning() || jobItem->getStatus() == "Fitting")
180  return false;
181 
182  return true;
183 }
#define ASSERT(condition)
Definition: Assert.h:31
Defines class IntensityDataItem.
Defines class JobItem.
Defines class JobModel.
Defines class JobSelectorActions.
Defines class StyledToolBar.
double getLowerZ() const
returns lower and upper zoom ranges of z-axis
double getLowerY() const
returns lower and upper zoom ranges of y-axis
double getUpperY() const
double getUpperX() const
double getLowerX() const
returns lower and upper zoom ranges of x-axis
double getUpperZ() const
QString getStatus() const
Definition: JobItem.cpp:123
bool isRunning() const
Definition: JobItem.cpp:145
IntensityDataItem * intensityDataItem()
Definition: JobItem.cpp:113
void runJob(const QModelIndex &index)
Definition: JobModel.cpp:179
void removeJob(const QModelIndex &index)
Definition: JobModel.cpp:189
const JobItem * getJobItemForIndex(const QModelIndex &index) const
Definition: JobModel.cpp:46
void onContextMenuRequest(const QPoint &point, const QModelIndex &indexAtPoint={})
Generates context menu at given point.
void setupEqualizeMenu(QMenu &menu)
void initItemContextMenu(QMenu &menu, const QModelIndex &indexAtPoint)
JobSelectorActions(JobModel *jobModel, QObject *parent=0)
void equalizeSelectedToJob(int selected_id)
Puts all IntensityDataItem axes range to the selected job.
void setAllActionsEnabled(bool value)
QItemSelectionModel * m_selectionModel
void setSelectionModel(QItemSelectionModel *selectionModel)
bool canRemoveJob(const QModelIndex &index) const
bool canRunJob(const QModelIndex &index) const
QString itemName() const
Get item name, return display name if no name is set.