BornAgain  1.19.79
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/View/Mask/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 "GUI/Util/ActionFactory.h"
19 #include <QAction>
20 #include <QItemSelectionModel>
21 #include <QMenu>
22 
24  : QObject(parent)
25  , m_toggleMaskValueAction(new QAction("Toggle mask value", parent))
26  , m_bringToFrontAction(new QAction("Rise mask up", parent))
27  , m_sendToBackAction(new QAction("Lower mask down", parent))
28  , m_deleteMaskAction(new QAction("Remove mask", parent))
29  , m_resetViewAction(new QAction(this))
30  , m_savePlotAction(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(":/Mask/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(":/Mask/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 
66  connect(m_togglePanelAction, &QAction::triggered, this,
68 }
69 
70 void MaskEditorActions::setModel(SessionModel* maskModel, const QModelIndex& rootIndex)
71 {
72  m_maskModel = maskModel;
73  m_rootIndex = rootIndex;
74 }
75 
76 void MaskEditorActions::setSelectionModel(QItemSelectionModel* selectionModel)
77 {
78  m_selectionModel = selectionModel;
79 }
80 
82 {
83  return m_sendToBackAction;
84 }
85 
87 {
88  return m_bringToFrontAction;
89 }
90 
92 {
93  return QList<QAction*>() << m_resetViewAction << m_togglePanelAction;
94 }
95 
96 //! Constructs MaskItem context menu following the request from MaskGraphicsScene
97 //! or MaskEditorInfoPanel
99 {
100  QMenu menu;
101  initItemContextMenu(menu);
102  menu.exec(point);
103  setAllActionsEnabled(true);
104 }
105 
107 {
108  ASSERT(m_maskModel);
109  ASSERT(m_selectionModel);
110 
111  QModelIndexList indexes = m_selectionModel->selectedIndexes();
112  while (!indexes.empty()) {
113  m_maskModel->removeRows(indexes.back().row(), 1, indexes.back().parent());
114  indexes = m_selectionModel->selectedIndexes();
115  }
116 }
117 
118 //! Performs switch of mask value for all selected items (true -> false, false -> true)
120 {
121  ASSERT(m_maskModel);
122  ASSERT(m_selectionModel);
123  for (auto itemIndex : m_selectionModel->selectedIndexes()) {
124  if (auto* item = dynamic_cast<MaskItem*>(m_maskModel->itemForIndex(itemIndex)))
125  item->setMaskValue(!item->maskValue());
126  }
127 }
128 
130 {
132 }
133 
135 {
137 }
138 
139 //! Lower mask one level down or rise one level up in the masks stack
140 void MaskEditorActions::changeMaskStackingOrder(MaskEditorFlags::Stacking value)
141 {
142  if (!m_maskModel || !m_selectionModel)
143  return;
144 
145  int change_in_row(0);
146  if (value == MaskEditorFlags::BRING_TO_FRONT)
147  change_in_row = -1;
148  if (value == MaskEditorFlags::SEND_TO_BACK)
149  change_in_row = 2;
150 
151  QModelIndexList indexes = m_selectionModel->selectedIndexes();
152 
153  for (auto itemIndex : indexes) {
154  if (SessionItem* item = m_maskModel->itemForIndex(itemIndex)) {
155  int new_row = itemIndex.row() + change_in_row;
156  if (new_row >= 0 && new_row <= m_maskModel->rowCount(m_rootIndex)) {
157  SessionItem* newItem =
159  m_selectionModel->select(m_maskModel->indexOfItem(newItem),
160  QItemSelectionModel::Select);
161  }
162  }
163  }
164 }
165 
166 //! Returns true if at least one of MaskItems in the selection can be moved one level up
167 //! (Naturally, it is always true, if selection contains more than one item. If selection contains
168 //! only one item, the result will depend on position of item on the stack.
169 //! Top item can't be moved up. Used to disable corresponding context menu line.)
171 {
172  bool result(false);
173  QModelIndexList indexes = m_selectionModel->selectedIndexes();
174  if (indexes.size() == 1 && indexes.front().row() != 0)
175  result = true;
176  return result;
177 }
178 
179 //! Returns true if at least one of MaskItems in the selection can be moved one level down.
181 {
182  bool result(false);
183  QModelIndexList indexes = m_selectionModel->selectedIndexes();
184  if (indexes.size() == 1) {
185  SessionItem* item = m_maskModel->itemForIndex(indexes.front());
186  if (indexes.front().row() != item->parentItem()->numberOfChildren() - 1)
187  result = true;
188  }
189  return result;
190 }
191 
193 {
194  m_sendToBackAction->setEnabled(value);
195  m_bringToFrontAction->setEnabled(value);
196  m_toggleMaskValueAction->setEnabled(value);
197  m_deleteMaskAction->setEnabled(value);
198 }
199 
200 //! Init external context menu with currently defined actions.
201 //! Triggered from MaskGraphicsScene of MaskEditorInfoPanel (QListView)
203 {
204  if (!m_rootIndex.isValid())
205  return;
206 
207  ASSERT(m_maskModel);
208  ASSERT(m_selectionModel);
209 
210  if (m_selectionModel->selectedIndexes().isEmpty())
211  setAllActionsEnabled(false);
212 
215 
216  menu.addAction(m_toggleMaskValueAction);
217  menu.addAction(m_bringToFrontAction);
218  menu.addAction(m_sendToBackAction);
219  menu.addSeparator();
220  menu.addAction(m_deleteMaskAction);
221 }
Defines class ActionFactory.
Defines class MaskEditorActions.
Defines MaskItems classes.
Defines class SessionModel.
static QAction * createTogglePropertiesPanelAction(QObject *parent, QWidget *toggledWidget=nullptr)
Create "toggle properties panel" action.
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
QList< QAction * > topToolbarActions()
void propertyPanelRequest()
QAction * m_togglePanelAction
void onToggleMaskValueAction()
Performs switch of mask value for all selected items (true -> false, false -> true)
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. Triggered from MaskGraphicsScene of MaskEd...
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
A base class for all mask items.
Definition: MaskItems.h:27
Base class for a GUI data item.
Definition: SessionItem.h:204
int numberOfChildren() const
Returns total number of children.
Definition: SessionItem.cpp:88
SessionItem * parentItem() const
Returns parent of this item.
Definition: SessionItem.cpp:67
Base class for a GUI data collection. A collection is e.g. all real data (RealDataModel)....
Definition: SessionModel.h:42
SessionItem * moveItem(SessionItem *item, SessionItem *new_parent=nullptr, int row=-1, const QString &tag="")
Move given parameterized item to the new_parent at given row. If new_parent is not defined,...
SessionItem * itemForIndex(const QModelIndex &index) const
bool removeRows(int row, int count, const QModelIndex &parent) override
QModelIndex indexOfItem(SessionItem *item) const