BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
InstrumentListView.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/Instrument/InstrumentListView.cpp
6 //! @brief Implements class InstrumentListView
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 
22 #include <QAction>
23 #include <QListView>
24 #include <QMessageBox>
25 #include <QVBoxLayout>
26 
28  Qt::WindowFlags f)
29  : QWidget(parent, f)
30  , m_document(document)
31 {
32  setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
33 
34  auto* layout = new QVBoxLayout(this);
35  layout->setMargin(10);
36 
37  m_listView = new QListView(this);
38  m_listView->setViewMode(QListView::IconMode);
39  m_listView->setIconSize(QSize(96, 84));
40  m_listView->setMovement(QListView::Static);
41  m_listView->setMaximumWidth(200);
42  m_listView->setSpacing(12);
43  m_listView->setSelectionMode(QAbstractItemView::SingleSelection);
44 
45  m_listView->setObjectName("listView");
46  m_listView->setStyleSheet(
47  QString::fromUtf8("QListView#listView\n"
48  "{\n"
49  " selection-background-color : rgb(98,100,105); \n"
50  " selection-color: rgb(255,255,255);\n"
51  " border: 1px solid rgb(98,100,105);\n"
52  "}\n"));
53  layout->addWidget(m_listView);
54 
56  m_listView->setModel(m_model);
57 
58  m_newGisasAction = new QAction("New GISAS", this);
59  m_newGisasAction->setIcon(QIcon(":/images/shape-square-plus.svg"));
60  m_newGisasAction->setToolTip("Add new GISAS instrument with default settings");
61  connect(m_newGisasAction, &QAction::triggered, this, &InstrumentListView::onNewGisas);
62  addAction(m_newGisasAction);
63 
64  m_newOffspecAction = new QAction("New off-specular", this);
65  m_newOffspecAction->setIcon(QIcon(":/images/shape-square-plus.svg"));
66  m_newOffspecAction->setToolTip("Add new off-specular instrument with default settings");
67  connect(m_newOffspecAction, &QAction::triggered, this, &InstrumentListView::onNewOffspec);
68  addAction(m_newOffspecAction);
69 
70  m_newSpecularAction = new QAction("New specular", this);
71  m_newSpecularAction->setIcon(QIcon(":/images/shape-square-plus.svg"));
72  m_newSpecularAction->setToolTip("Add new specular instrument with default settings");
73  connect(m_newSpecularAction, &QAction::triggered, this, &InstrumentListView::onNewSpecular);
74  addAction(m_newSpecularAction);
75 
76  m_newDepthProbeAction = new QAction("New depth probe", this);
77  m_newDepthProbeAction->setIcon(QIcon(":/images/shape-square-plus.svg"));
78  m_newDepthProbeAction->setToolTip("Add new depth probe instrument with default settings");
79  connect(m_newDepthProbeAction, &QAction::triggered, this, &InstrumentListView::onNewDepthProbe);
80  addAction(m_newDepthProbeAction);
81 
82  m_separatorAction1 = new QAction(this);
83  m_separatorAction1->setSeparator(true);
84  addAction(m_separatorAction1);
85 
86  m_removeAction = new QAction("Remove", this);
87  m_removeAction->setIcon(QIcon(":/images/delete.svg"));
88  m_removeAction->setToolTip("Remove selected instrument");
89  connect(m_removeAction, &QAction::triggered, this, &InstrumentListView::onRemove);
90  addAction(m_removeAction);
91 
92  m_copyAction = new QAction("Copy", this);
93  m_copyAction->setIcon(QIcon(":/images/content-copy.svg"));
94  m_copyAction->setToolTip("Make a copy of the selected instrument");
95  connect(m_copyAction, &QAction::triggered, this, &InstrumentListView::onCopy);
96  addAction(m_copyAction);
97 
98  m_separatorAction2 = new QAction(this);
99  m_separatorAction2->setSeparator(true);
100  addAction(m_separatorAction2);
101 
102  m_storeInLibraryAction = new QAction("Store in library", this);
103  m_storeInLibraryAction->setIcon(QIcon(":/images/library.svg"));
104  m_storeInLibraryAction->setToolTip("Store instrument in library");
105  connect(m_storeInLibraryAction, &QAction::triggered, this,
107  addAction(m_storeInLibraryAction);
108 
109  m_loadFromLibraryAction = new QAction("Choose from library", this);
110  m_loadFromLibraryAction->setIcon(QIcon(":/images/library.svg"));
111  m_loadFromLibraryAction->setToolTip("Load an instrument from the instrument library");
112  connect(m_loadFromLibraryAction, &QAction::triggered, this,
114  addAction(m_loadFromLibraryAction);
115 
116  setContextMenuPolicy(Qt::ActionsContextMenu);
117 
118  connect(m_listView->selectionModel(), &QItemSelectionModel::selectionChanged, this,
120 
121  connect(m_document, &ProjectDocument::modified, this,
123 
125 
126  updateActions();
128 }
129 
131 {
132  return QSize(170, 400);
133 }
134 
136 {
137  return QSize(96, 200);
138 }
139 
140 QList<QAction*> InstrumentListView::toolbarActions() const
141 {
146 }
147 
149 {
150  const QModelIndexList indexes = m_listView->selectionModel()->selectedIndexes();
151  if (!indexes.empty())
152  return m_model->instrumentForIndex(indexes.front());
153  return nullptr;
154 }
155 
157 {
158  updateActions();
159 
160  QModelIndexList indexes = m_listView->selectionModel()->selectedIndexes();
161  if (!indexes.empty())
162  emit instrumentSelected(m_model->instrumentForIndex(indexes.front()));
163  else
164  emit instrumentSelected(nullptr);
165 }
166 
168 {
169  QModelIndex idx = m_model->addNewGISASInstrument();
170  m_listView->selectionModel()->select(idx, QItemSelectionModel::ClearAndSelect);
171 }
172 
174 {
175  QModelIndex idx = m_model->addNewOffspecInstrument();
176  m_listView->selectionModel()->select(idx, QItemSelectionModel::ClearAndSelect);
177 }
178 
180 {
181  QModelIndex idx = m_model->addNewSpecularInstrument();
182  m_listView->selectionModel()->select(idx, QItemSelectionModel::ClearAndSelect);
183 }
184 
186 {
187  QModelIndex idx = m_model->addNewDepthProbeInstrument();
188  m_listView->selectionModel()->select(idx, QItemSelectionModel::ClearAndSelect);
189 }
190 
191 //! Removes currently selected instrument.
193 {
194  QModelIndexList indexes = m_listView->selectionModel()->selectedIndexes();
195  if (!indexes.empty()) {
196  m_model->removeInstrument(indexes.front());
197 
199  }
200 }
201 
202 //! Makes a copy of the currently selected instrument.
204 {
205  QModelIndexList indexes = m_listView->selectionModel()->selectedIndexes();
206  if (!indexes.empty()) {
207  QModelIndex idx = m_model->copyInstrument(indexes.front());
208  m_listView->selectionModel()->select(idx, QItemSelectionModel::ClearAndSelect);
209  }
210 }
211 
213 {
214  if (!m_listView->selectionModel()->hasSelection())
215  return;
216 
217  QModelIndex idx = m_listView->selectionModel()->selectedIndexes().front();
218  InstrumentItem* instrument = m_model->instrumentForIndex(idx);
219 
225  dlg.execAdd(*instrument);
226 }
227 
229 {
231  QMessageBox::information(GUI::Global::mainWindow, "Select from library",
232  "The library does not contain instruments so far.");
233  return;
234  }
235 
241 
242  auto* instrumentToCopy = dlg.execChoose();
243  if (instrumentToCopy == nullptr)
244  return;
245 
246  QModelIndex idx = m_model->copyInstrument(instrumentToCopy);
247  m_listView->selectionModel()->select(idx, QItemSelectionModel::ClearAndSelect);
248 }
249 
251 {
252  const auto f = m_document->functionalities();
253 
254  m_newGisasAction->setVisible(f.testFlag(ProjectDocument::Gisas));
255  m_newOffspecAction->setVisible(f.testFlag(ProjectDocument::Offspec));
256  m_newSpecularAction->setVisible(f.testFlag(ProjectDocument::Specular));
257  m_newDepthProbeAction->setVisible(f.testFlag(ProjectDocument::DepthProbe));
259 }
260 
262 {
263  bool enabled = m_listView->selectionModel()->hasSelection();
264  m_removeAction->setEnabled(enabled);
265  m_copyAction->setEnabled(enabled);
266  m_storeInLibraryAction->setEnabled(enabled);
267 }
268 
270 {
271  if (!m_listView->selectionModel()->hasSelection() && m_model->rowCount()) {
272  QModelIndex last = m_model->index(m_model->rowCount() - 1, 0, QModelIndex());
273  m_listView->selectionModel()->select(last, QItemSelectionModel::ClearAndSelect);
274  }
275 }
Defines global pointers.
Defines class InstrumentItem and all its children.
Defines class InstrumentLibraryEditor.
Defines class InstrumentListModel.
Defines class InstrumentListView.
Defines class ProjectDocument.
SessionData * gSessionData
global pointer to the single instance
Definition: SessionData.cpp:17
Defines struct SessionData.
Abstract base class for instrument-specific item classes.
InstrumentItem * execChoose()
Execute the dialog for choosing an instrument from the library. Returns nullptr if canceled.
void execAdd(const InstrumentItem &instrumentToAdd)
Execute the dialog to add an instrument to the library.
List model for instruments.
QModelIndex copyInstrument(const QModelIndex &source)
QModelIndex addNewSpecularInstrument()
int rowCount(const QModelIndex &parent=QModelIndex()) const override
QModelIndex addNewOffspecInstrument()
void removeInstrument(const QModelIndex &index)
QModelIndex addNewGISASInstrument()
InstrumentItem * instrumentForIndex(const QModelIndex &index) const
QModelIndex addNewDepthProbeInstrument()
QSize minimumSizeHint() const override
ProjectDocument * m_document
InstrumentListModel * m_model
void onCopy()
Makes a copy of the currently selected instrument.
InstrumentItem * currentInstrument() const
void instrumentSelected(InstrumentItem *instrument)
QAction * m_newDepthProbeAction
QSize sizeHint() const override
QList< QAction * > toolbarActions() const
InstrumentListView(ProjectDocument *document, QWidget *parent=nullptr, Qt::WindowFlags f=Qt::WindowFlags())
void updateFunctionalityNarrowing()
Show/hide UI elements according to settings in current project.
void onRemove()
Removes currently selected instrument.
QAction * m_loadFromLibraryAction
QAction * m_storeInLibraryAction
Project document class handles all data related to the opened project (sample, job,...
Functionalities functionalities() const
bool singleInstrumentMode() const
void modified()
Emitted for any modifications in the document.
InstrumentsEditController * instrumentsEditController()
The edit controller for the instruments in this project document.
static QMainWindow * mainWindow
Definition: Globals.h:22
void information(QWidget *parent, const QString &title, const QString &text, const QString &detailedText)
Definition: MessageBox.cpp:22
InstrumentLibrary instrumentLibrary
Definition: SessionData.h:26