BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
MaskEditorActions.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/Views/MaskWidgets/MaskEditorActions.cpp
6 //! @brief Implement class MaskEditorActions
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 
18 #include <QAction>
19 #include <QItemSelectionModel>
20 #include <QMenu>
21 
23  : QObject(parent)
24  , m_toggleMaskValueAction(new QAction("Toggle mask value", parent))
25  , m_bringToFrontAction(new QAction("Rise mask up", parent))
26  , m_sendToBackAction(new QAction("Lower mask down", parent))
27  , m_deleteMaskAction(new QAction("Remove mask", parent))
28  , m_resetViewAction(new QAction(this))
29  , m_savePlotAction(new QAction(this))
30  , m_togglePanelAction(new QAction(this))
31  , m_maskModel(nullptr)
32  , m_selectionModel(nullptr)
33 
34 {
35  connect(m_toggleMaskValueAction, &QAction::triggered, this,
37 
38  m_bringToFrontAction->setIcon(QIcon(":/MaskWidgets/images/maskeditor_bringtofront.svg"));
39  m_bringToFrontAction->setToolTip("Rise selected mask one level up (PgUp)");
40  m_bringToFrontAction->setShortcuts(QKeySequence::MoveToPreviousPage);
41  connect(m_bringToFrontAction, &QAction::triggered, this,
43 
44  m_sendToBackAction->setIcon(QIcon(":/MaskWidgets/images/maskeditor_sendtoback.svg"));
45  m_sendToBackAction->setToolTip("Lower selected mask one level down (PgDown)");
46  m_sendToBackAction->setShortcuts(QKeySequence::MoveToNextPage);
47  connect(m_sendToBackAction, &QAction::triggered, this, &MaskEditorActions::onSendToBackAction);
48 
49  m_deleteMaskAction->setToolTip("Remove selected mask (Del)");
50  m_deleteMaskAction->setShortcuts(QKeySequence::Delete);
51  parent->addAction(m_deleteMaskAction);
52  connect(m_deleteMaskAction, &QAction::triggered, this, &MaskEditorActions::onDeleteMaskAction);
53 
54  // Actions for top toolbar
55  m_resetViewAction->setText("Center view");
56  m_resetViewAction->setIcon(QIcon(":/images/camera-metering-center.svg"));
57  m_resetViewAction->setToolTip("Center View");
58  connect(m_resetViewAction, &QAction::triggered, this, &MaskEditorActions::resetViewRequest);
59 
60  m_savePlotAction->setText("Save");
61  m_savePlotAction->setIcon(QIcon(":/images/content-save-outline.svg"));
62  m_savePlotAction->setToolTip("Save Plot");
63  connect(m_savePlotAction, &QAction::triggered, this, &MaskEditorActions::savePlotRequest);
64 
65  m_togglePanelAction->setText("Properties");
66  m_togglePanelAction->setIcon(QIcon(":/images/dock-right.svg"));
67  m_togglePanelAction->setToolTip("Toggle Property Panel");
68  connect(m_togglePanelAction, &QAction::triggered, this,
70 }
71 
72 void MaskEditorActions::setModel(SessionModel* maskModel, const QModelIndex& rootIndex)
73 {
74  m_maskModel = maskModel;
75  m_rootIndex = rootIndex;
76 }
77 
78 void MaskEditorActions::setSelectionModel(QItemSelectionModel* selectionModel)
79 {
80  m_selectionModel = selectionModel;
81 }
82 
84 {
85  return m_sendToBackAction;
86 }
87 
89 {
90  return m_bringToFrontAction;
91 }
92 
94 {
95  return QList<QAction*>() << m_resetViewAction << m_togglePanelAction;
96 }
97 
98 //! Constructs MaskItem context menu following the request from MaskGraphicsScene
99 //! or MaskEditorInfoPanel
101 {
102  QMenu menu;
103  initItemContextMenu(menu);
104  menu.exec(point);
105  setAllActionsEnabled(true);
106 }
107 
109 {
112 
113  QModelIndexList indexes = m_selectionModel->selectedIndexes();
114  while (indexes.size()) {
115  m_maskModel->removeRows(indexes.back().row(), 1, indexes.back().parent());
116  indexes = m_selectionModel->selectedIndexes();
117  }
118 }
119 
120 //! Performs switch of mask value for all selected items (true -> false, false -> true)
122 {
125  for (auto itemIndex : m_selectionModel->selectedIndexes()) {
126  if (SessionItem* item = m_maskModel->itemForIndex(itemIndex)) {
127  bool old_value = item->getItemValue(MaskItem::P_MASK_VALUE).toBool();
128  item->setItemValue(MaskItem::P_MASK_VALUE, !old_value);
129  }
130  }
131 }
132 
134 {
136 }
137 
139 {
141 }
142 
143 //! Lower mask one level down or rise one level up in the masks stack
144 void MaskEditorActions::changeMaskStackingOrder(MaskEditorFlags::Stacking value)
145 {
146  if (!m_maskModel || !m_selectionModel)
147  return;
148 
149  int change_in_row(0);
150  if (value == MaskEditorFlags::BRING_TO_FRONT)
151  change_in_row = -1;
152  if (value == MaskEditorFlags::SEND_TO_BACK)
153  change_in_row = 2;
154 
155  QModelIndexList indexes = m_selectionModel->selectedIndexes();
156 
157  for (auto itemIndex : indexes) {
158  if (SessionItem* item = m_maskModel->itemForIndex(itemIndex)) {
159  int new_row = itemIndex.row() + change_in_row;
160  if (new_row >= 0 && new_row <= m_maskModel->rowCount(m_rootIndex)) {
161  SessionItem* newItem =
163  m_selectionModel->select(m_maskModel->indexOfItem(newItem),
164  QItemSelectionModel::Select);
165  }
166  }
167  }
168 }
169 
170 //! Returns true if at least one of MaskItems in the selection can be moved one level up
171 //! (Naturally, it is always true, if selection contains more than one item. If selection contains
172 //! only one item, the result will depend on position of item on the stack.
173 //! Top item can't be moved up. Used to disable corresponding context menu line.)
175 {
176  bool result(false);
177  QModelIndexList indexes = m_selectionModel->selectedIndexes();
178  if (indexes.size() == 1 && indexes.front().row() != 0)
179  result = true;
180  return result;
181 }
182 
183 //! Returns true if at least one of MaskItems in the selection can be moved one level down.
185 {
186  bool result(false);
187  QModelIndexList indexes = m_selectionModel->selectedIndexes();
188  if (indexes.size() == 1) {
189  SessionItem* item = m_maskModel->itemForIndex(indexes.front());
190  if (indexes.front().row() != item->parent()->numberOfChildren() - 1)
191  result = true;
192  }
193  return result;
194 }
195 
197 {
198  m_sendToBackAction->setEnabled(value);
199  m_bringToFrontAction->setEnabled(value);
200  m_toggleMaskValueAction->setEnabled(value);
201  m_deleteMaskAction->setEnabled(value);
202 }
203 
204 //! Init external context menu with currently defined actions.
205 //! Triggered from MaskGraphicsScene of MaskEditorInfoPanel (QListView)
207 {
208  if (!m_rootIndex.isValid())
209  return;
210 
213 
214  if (m_selectionModel->selectedIndexes().isEmpty())
215  setAllActionsEnabled(false);
216 
219 
220  menu.addAction(m_toggleMaskValueAction);
221  menu.addAction(m_bringToFrontAction);
222  menu.addAction(m_sendToBackAction);
223  menu.addSeparator();
224  menu.addAction(m_deleteMaskAction);
225 }
#define ASSERT(condition)
Definition: Assert.h:31
Defines class MaskEditorActions.
Defines MaskItems classes.
Defines class SessionModel.
QItemSelectionModel * m_selectionModel
Index in the model corresponding to IntensityDataItem.
bool isSendToBackPossible() const
Returns true if at least one of MaskItems in the selection can be moved one level down.
QAction * m_toggleMaskValueAction
void propertyPanelRequest()
QAction * m_togglePanelAction
void onToggleMaskValueAction()
Performs switch of mask value for all selected items (true -> false, false -> true)
QList< QAction * > topToolBarActions()
MaskEditorActions(QWidget *parent)
void onItemContextMenuRequest(const QPoint &point)
Constructs MaskItem context menu following the request from MaskGraphicsScene or MaskEditorInfoPanel.
void setAllActionsEnabled(bool value)
QAction * m_bringToFrontAction
QAction * m_sendToBackAction
QAction * m_deleteMaskAction
void setModel(SessionModel *maskModel, const QModelIndex &rootIndex)
void changeMaskStackingOrder(MaskEditorFlags::Stacking value)
Lower mask one level down or rise one level up in the masks stack.
QAction * bringToFrontAction()
QAction * m_savePlotAction
void initItemContextMenu(QMenu &menu)
Init external context menu with currently defined actions.
QAction * sendToBackAction()
bool isBringToFrontPossible() const
Returns true if at least one of MaskItems in the selection can be moved one level up (Naturally,...
SessionModel * m_maskModel
QAction * m_resetViewAction
void setSelectionModel(QItemSelectionModel *selectionModel)
QModelIndex m_rootIndex
static const QString P_MASK_VALUE
Definition: MaskItems.h:33
int numberOfChildren() const
Returns total number of children.
Definition: SessionItem.cpp:94
SessionItem * parent() const
Returns parent of this item.
Definition: SessionItem.cpp:73
SessionItem * itemForIndex(const QModelIndex &index) const
virtual bool removeRows(int row, int count, const QModelIndex &parent)
SessionItem * moveItem(SessionItem *item, SessionItem *new_parent=0, int row=-1, const QString &tag="")
Move given parameterized item to the new_parent at given row.
QModelIndex indexOfItem(SessionItem *item) const