BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
InstrumentViewActions Class Reference

Collection of actions to add/remove/clone instrument. More...

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

Public Slots

void onCloneInstrument ()
 Clones currently selected instrument. More...
 
void onContextMenuRequest (const QPoint &point, const QModelIndex &indexAtPoint)
 
void onRemoveInstrument ()
 Removes currently selected instrument. More...
 

Public Member Functions

 InstrumentViewActions (QWidget *parent=nullptr)
 
 ~InstrumentViewActions ()
 
QMenu * instrumentMenu ()
 Returns menu to create one of available instrument types. More...
 
void setModel (SessionModel *model)
 
void setSelectionModel (QItemSelectionModel *selectionModel)
 

Private Member Functions

void initAddInstrumentMenu ()
 Constructs menu to add instruments of various types. More...
 
QMap< QString, int > mapOfNames ()
 
void setAllActionsEnabled (bool value)
 
QString suggestInstrumentName (const QString &currentName)
 
void updateSelection ()
 

Private Attributes

QMenu * m_addInstrumentMenu
 
QAction * m_cloneInstrumentAction
 
SessionModelm_model
 
QAction * m_removeInstrumentAction
 
QItemSelectionModel * m_selectionModel
 

Detailed Description

Collection of actions to add/remove/clone instrument.

Definition at line 29 of file InstrumentViewActions.h.

Constructor & Destructor Documentation

◆ InstrumentViewActions()

InstrumentViewActions::InstrumentViewActions ( QWidget *  parent = nullptr)
explicit

Definition at line 29 of file InstrumentViewActions.cpp.

30  : QObject(parent)
31  , m_addInstrumentMenu(nullptr)
32  , m_removeInstrumentAction(nullptr)
33  , m_cloneInstrumentAction(nullptr)
34  , m_model(nullptr)
35  , m_selectionModel(nullptr)
36 {
38 
40  new QAction(QIcon(":/images/delete.svg"), "Remove this instrument", this);
41 
43  new QAction(QIcon(":/images/content-copy.svg"), "Clone this instrument", this);
44 
45  connect(m_removeInstrumentAction, &QAction::triggered, this,
47  connect(m_cloneInstrumentAction, &QAction::triggered, this,
49 }
void onRemoveInstrument()
Removes currently selected instrument.
void onCloneInstrument()
Clones currently selected instrument.
void initAddInstrumentMenu()
Constructs menu to add instruments of various types.
QItemSelectionModel * m_selectionModel

References initAddInstrumentMenu(), m_cloneInstrumentAction, m_removeInstrumentAction, onCloneInstrument(), and onRemoveInstrument().

Here is the call graph for this function:

◆ ~InstrumentViewActions()

InstrumentViewActions::~InstrumentViewActions ( )

Definition at line 51 of file InstrumentViewActions.cpp.

52 {
53  delete m_addInstrumentMenu;
54 }

References m_addInstrumentMenu.

Member Function Documentation

◆ initAddInstrumentMenu()

void InstrumentViewActions::initAddInstrumentMenu ( )
private

Constructs menu to add instruments of various types.

The type of instrument is encoded in QAction internal data.

Definition at line 187 of file InstrumentViewActions.cpp.

188 {
189  m_addInstrumentMenu = new QMenu("Add new instrument");
190  m_addInstrumentMenu->setToolTipsVisible(true);
191 
192  const auto addAction = [this](const QString& menuEntry, const QString& tooltip,
193  std::function<InstrumentItem*()> insertInstrument) {
194  auto action = m_addInstrumentMenu->addAction(menuEntry);
195  action->setToolTip(tooltip);
196 
197  connect(action, &QAction::triggered, [=]() {
198  InstrumentItem* instrument = insertInstrument();
199  instrument->setItemName(suggestInstrumentName(instrument->defaultName()));
200  updateSelection();
201 
202  // set default action to the just triggered action
203  m_addInstrumentMenu->setDefaultAction(action);
204  });
205  };
206 
207  addAction("GISAS", "Add GISAS instrument with default settings",
208  [this]() { return m_model->insertItem<GISASInstrumentItem>(); });
209 
210  addAction("OffSpecular", "Add OffSpecular instrument with default settings",
211  [this]() { return m_model->insertItem<OffSpecularInstrumentItem>(); });
212 
213  addAction("Specular", "Add Specular instrument with default settings",
214  [this]() { return m_model->insertItem<SpecularInstrumentItem>(); });
215 
216  addAction("DepthProbe", "Add DepthProbe instrument with default settings",
217  [this]() { return m_model->insertItem<DepthProbeInstrumentItem>(); });
218 
219  m_addInstrumentMenu->setDefaultAction(m_addInstrumentMenu->actions().front());
220 }
virtual QString defaultName() const =0
The default user visible name when creating an instrument.
QString suggestInstrumentName(const QString &currentName)
void setItemName(const QString &name)
Set item name, add property if necessary.
T * insertItem(SessionItem *parent=nullptr, int row=-1, QString tag="")
Definition: SessionModel.h:125

References InstrumentItem::defaultName(), SessionModel::insertItem(), m_addInstrumentMenu, m_model, SessionItem::setItemName(), suggestInstrumentName(), and updateSelection().

Referenced by InstrumentViewActions().

Here is the call graph for this function:

◆ instrumentMenu()

QMenu * InstrumentViewActions::instrumentMenu ( )

Returns menu to create one of available instrument types.

Definition at line 68 of file InstrumentViewActions.cpp.

69 {
70  return m_addInstrumentMenu;
71 }

References m_addInstrumentMenu.

◆ mapOfNames()

QMap< QString, int > InstrumentViewActions::mapOfNames ( )
private

Definition at line 167 of file InstrumentViewActions.cpp.

168 {
169  QMap<QString, int> result;
170 
171  for (auto& name : ModelUtils::topItemNames(m_model)) {
172  int ncopy(1);
173  QRegExp regexp("\\((.*)\\)");
174  if (regexp.indexIn(name) >= 0)
175  ncopy = regexp.cap(1).toInt();
176  name.replace(regexp.cap(0), "");
177  name = name.trimmed();
178  result[name] = ncopy;
179  }
180 
181  return result;
182 }
QStringList topItemNames(SessionModel *model, const QString &modelType="")
Returns list of top iten manes.
Definition: ModelUtils.cpp:20
QString const & name(EShape k)
Definition: particles.cpp:21

References m_model, RealSpace::Particles::name(), and ModelUtils::topItemNames().

Referenced by suggestInstrumentName().

Here is the call graph for this function:

◆ onCloneInstrument

void InstrumentViewActions::onCloneInstrument ( )
slot

Clones currently selected instrument.

Definition at line 87 of file InstrumentViewActions.cpp.

88 {
89  QModelIndex currentIndex = m_selectionModel->currentIndex();
90 
91  if (currentIndex.isValid()) {
92  SessionItem* item = m_model->itemForIndex(currentIndex);
93  QString nameOfClone = suggestInstrumentName(item->itemName());
95  static_cast<InstrumentItem*>(m_model->insertNewItem(item->modelType()));
96  const QString idBackup = clone->id(); // will be overwritten by the following
97  for (auto child : item->children()) {
98  auto tag = item->tagFromItem(child);
99  m_model->copyItem(child, clone, tag);
100  }
101  clone->setId(idBackup);
102  clone->setItemName(nameOfClone);
103 
104  // copying non-uniform axis data
105  if (auto instrument = dynamic_cast<SpecularInstrumentItem*>(item)) {
106  auto axis_group = instrument->beamItem()->inclinationAxisGroup();
107 
108  auto donor_axis =
109  dynamic_cast<PointwiseAxisItem*>(axis_group->getItemOfType("PointwiseAxis"));
110  if (!donor_axis->containsNonXMLData())
111  return;
112 
113  auto acceptor_axis =
114  dynamic_cast<PointwiseAxisItem*>(dynamic_cast<SpecularInstrumentItem*>(clone)
115  ->beamItem()
116  ->inclinationAxisGroup()
117  ->getItemOfType("PointwiseAxis"));
118  acceptor_axis->init(*donor_axis->axis(), donor_axis->getUnitsLabel());
119  }
120  }
121 }
Item for non-uniform axis with specified coordinates.
void init(const IAxis &axis, const QString &units_label)
QString itemName() const
Get item name, return display name if no name is set.
QVector< SessionItem * > children() const
Returns vector of all children.
QString modelType() const
Get model type.
QString tagFromItem(const SessionItem *item) const
Returns the tag name of given item when existing.
T * copyItem(const T *item_to_copy, SessionItem *new_parent=0, const QString &tag="")
Definition: SessionModel.h:136
SessionItem * itemForIndex(const QModelIndex &index) const
SessionItem * insertNewItem(QString model_type, SessionItem *parent_item=nullptr, int row=-1, QString tag="")
@ clone
full deep copying with item identifiers preserved

References SessionItem::children(), SessionModel::copyItem(), PointwiseAxisItem::init(), SessionModel::insertNewItem(), SessionModel::itemForIndex(), SessionItem::itemName(), m_model, m_selectionModel, SessionItem::modelType(), suggestInstrumentName(), and SessionItem::tagFromItem().

Referenced by InstrumentViewActions(), and InstrumentViewToolBar::InstrumentViewToolBar().

Here is the call graph for this function:

◆ onContextMenuRequest

void InstrumentViewActions::onContextMenuRequest ( const QPoint &  point,
const QModelIndex &  indexAtPoint 
)
slot

Definition at line 123 of file InstrumentViewActions.cpp.

125 {
126  QMenu menu;
127 
128  setAllActionsEnabled(indexAtPoint.isValid());
129 
130  menu.addAction(m_cloneInstrumentAction);
131  menu.addAction(m_removeInstrumentAction);
132  menu.addSeparator();
133  menu.addMenu(m_addInstrumentMenu);
134  menu.exec(point);
135 }
void setAllActionsEnabled(bool value)

References m_addInstrumentMenu, m_cloneInstrumentAction, m_removeInstrumentAction, and setAllActionsEnabled().

Referenced by InstrumentView::InstrumentView().

Here is the call graph for this function:

◆ onRemoveInstrument

void InstrumentViewActions::onRemoveInstrument ( )
slot

Removes currently selected instrument.

Definition at line 75 of file InstrumentViewActions.cpp.

76 {
77  QModelIndex currentIndex = m_selectionModel->currentIndex();
78 
79  if (currentIndex.isValid())
80  m_model->removeRows(currentIndex.row(), 1, QModelIndex());
81 
83 }
virtual bool removeRows(int row, int count, const QModelIndex &parent)

References m_model, m_selectionModel, SessionModel::removeRows(), and updateSelection().

Referenced by InstrumentViewActions(), and InstrumentViewToolBar::InstrumentViewToolBar().

Here is the call graph for this function:

◆ setAllActionsEnabled()

void InstrumentViewActions::setAllActionsEnabled ( bool  value)
private

Definition at line 137 of file InstrumentViewActions.cpp.

138 {
139  m_removeInstrumentAction->setEnabled(value);
140  m_cloneInstrumentAction->setEnabled(value);
141 }

References m_cloneInstrumentAction, and m_removeInstrumentAction.

Referenced by onContextMenuRequest().

◆ setModel()

void InstrumentViewActions::setModel ( SessionModel model)

Definition at line 56 of file InstrumentViewActions.cpp.

57 {
58  m_model = model;
59 }

References m_model.

Referenced by InstrumentView::InstrumentView().

◆ setSelectionModel()

void InstrumentViewActions::setSelectionModel ( QItemSelectionModel *  selectionModel)

Definition at line 61 of file InstrumentViewActions.cpp.

62 {
63  m_selectionModel = selectionModel;
64 }

References m_selectionModel.

Referenced by InstrumentView::InstrumentView().

◆ suggestInstrumentName()

QString InstrumentViewActions::suggestInstrumentName ( const QString &  currentName)
private

Definition at line 153 of file InstrumentViewActions.cpp.

154 {
155  auto map_of_names = mapOfNames();
156 
157  int ncopies = map_of_names[currentName];
158  if (ncopies == 0) {
159  map_of_names[currentName] = 1;
160  return currentName;
161  } else {
162  map_of_names[currentName]++;
163  return QString("%1 (%2)").arg(currentName).arg(map_of_names[currentName]);
164  }
165 }
QMap< QString, int > mapOfNames()

References mapOfNames().

Referenced by initAddInstrumentMenu(), and onCloneInstrument().

Here is the call graph for this function:

◆ updateSelection()

void InstrumentViewActions::updateSelection ( )
private

Definition at line 143 of file InstrumentViewActions.cpp.

144 {
145  if (!m_selectionModel->hasSelection()) {
146  // select last item
147  QModelIndex itemIndex =
148  m_model->index(m_model->rowCount(QModelIndex()) - 1, 0, QModelIndex());
149  m_selectionModel->select(itemIndex, QItemSelectionModel::ClearAndSelect);
150  }
151 }
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
virtual int rowCount(const QModelIndex &parent) const

References SessionModel::index(), m_model, m_selectionModel, and SessionModel::rowCount().

Referenced by initAddInstrumentMenu(), and onRemoveInstrument().

Here is the call graph for this function:

Member Data Documentation

◆ m_addInstrumentMenu

QMenu* InstrumentViewActions::m_addInstrumentMenu
private

◆ m_cloneInstrumentAction

QAction* InstrumentViewActions::m_cloneInstrumentAction
private

◆ m_model

SessionModel* InstrumentViewActions::m_model
private

◆ m_removeInstrumentAction

QAction* InstrumentViewActions::m_removeInstrumentAction
private

◆ m_selectionModel

QItemSelectionModel* InstrumentViewActions::m_selectionModel
private

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