BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
InstrumentListModel.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/Instrument/InstrumentListModel.cpp
6 //! @brief Implements class InstrumentListModel
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 
19 namespace {
20 
21 /// Get the default name for an instrument (i.e. the name a newly created instrument should get)
22 template <typename T>
23 QString defaultInstrumentName()
24 {
25  return "Untitled";
26 }
27 
28 template <>
29 QString defaultInstrumentName<SpecularInstrumentItem>()
30 {
31  return "Specular";
32 }
33 
34 template <>
35 QString defaultInstrumentName<DepthProbeInstrumentItem>()
36 {
37  return "DepthProbe";
38 }
39 
40 template <>
41 QString defaultInstrumentName<GISASInstrumentItem>()
42 {
43  return "GISAS";
44 }
45 
46 template <>
47 QString defaultInstrumentName<OffspecInstrumentItem>()
48 {
49  return "Offspec";
50 }
51 
52 } // namespace
53 
54 
56  : QAbstractListModel(parent)
57  , m_ec(ec)
58 {
59  m_gisasIcon.addPixmap(QPixmap(":/images/gisas_instrument.svg"), QIcon::Selected);
60  m_gisasIcon.addPixmap(QPixmap(":/images/gisas_instrument_shaded.svg"), QIcon::Normal);
61  m_offspecIcon.addPixmap(QPixmap(":/images/offspec_instrument.svg"), QIcon::Selected);
62  m_offspecIcon.addPixmap(QPixmap(":/images/offspec_instrument_shaded.svg"), QIcon::Normal);
63  m_specularIcon.addPixmap(QPixmap(":/images/specular_instrument.svg"), QIcon::Selected);
64  m_specularIcon.addPixmap(QPixmap(":/images/specular_instrument_shaded.svg"), QIcon::Normal);
65  m_depthProbeIcon.addPixmap(QPixmap(":/images/depth_instrument.svg"), QIcon::Selected);
66  m_depthProbeIcon.addPixmap(QPixmap(":/images/depth_instrument_shaded.svg"), QIcon::Normal);
67 
70 }
71 
72 int InstrumentListModel::rowCount(const QModelIndex&) const
73 {
74  return m_ec->collectedItems()->collectedItems().size();
75 }
76 
77 QVariant InstrumentListModel::data(const QModelIndex& index, int role) const
78 {
79  QVector<InstrumentItem*> instruments = m_ec->collectedItems()->collectedItems();
80  if (!index.isValid() || index.row() >= instruments.size() || index.row() < 0)
81  return {};
82 
83  const InstrumentItem* item = instruments[index.row()];
84  if (role == Qt::DecorationRole) {
85  if (dynamic_cast<const GISASInstrumentItem*>(item))
86  return m_gisasIcon;
87  if (dynamic_cast<const OffspecInstrumentItem*>(item))
88  return m_offspecIcon;
89  if (dynamic_cast<const SpecularInstrumentItem*>(item))
90  return m_specularIcon;
91  if (dynamic_cast<const DepthProbeInstrumentItem*>(item))
92  return m_depthProbeIcon;
93  return {};
94  }
95 
96  if (role == Qt::DisplayRole)
97  return item->instrumentName();
98 
99  return {};
100 }
101 
103 {
104  if (!index.isValid())
105  return nullptr;
106 
107  QVector<InstrumentItem*> instruments = m_ec->collectedItems()->collectedItems();
108  if (index.row() >= 0 && index.row() < instruments.size())
109  return instruments[index.row()];
110  return nullptr;
111 }
112 
114 {
115  return addNewInstrument<GISASInstrumentItem>();
116 }
117 
119 {
120  return addNewInstrument<OffspecInstrumentItem>();
121 }
122 
124 {
125  return addNewInstrument<SpecularInstrumentItem>();
126 }
127 
129 {
130  return addNewInstrument<DepthProbeInstrumentItem>();
131 }
132 
133 void InstrumentListModel::removeInstrument(const QModelIndex& index)
134 {
135  beginRemoveRows(QModelIndex(), index.row(), index.row());
136  InstrumentItem* instrument = instrumentForIndex(index);
137  m_ec->removeInstrument(instrument);
138  endRemoveRows();
139 }
140 
141 QModelIndex InstrumentListModel::copyInstrument(const QModelIndex& source)
142 {
143  const InstrumentItem* srcInstr = instrumentForIndex(source);
144  ASSERT(srcInstr);
145 
146  return copyInstrument(srcInstr);
147 }
148 
150 {
151  const QString copyName =
153  const int row = m_ec->collectedItems()->collectedItems().size();
154 
155  beginInsertRows(QModelIndex(), row, row);
156  m_ec->addCopy(source, copyName);
157  endInsertRows();
158 
159  return createIndex(row, 0);
160 }
161 
162 template <class Instrument>
164 {
165  const QString name =
166  m_ec->collectedItems()->suggestInstrumentName(defaultInstrumentName<Instrument>());
167  const int row = m_ec->collectedItems()->collectedItems().size();
168 
169  beginInsertRows(QModelIndex(), row, row);
170  auto* instrument = m_ec->addInstrument<Instrument>();
171  m_ec->setInstrumentName(instrument, name);
172  endInsertRows();
173 
174  return createIndex(row, 0);
175 }
176 
178 {
179  const auto instruments = m_ec->collectedItems()->collectedItems();
180  if (const auto row = instruments.indexOf(const_cast<InstrumentItem*>(instrument)); row != -1)
181  emit dataChanged(index(row, 0), index(row, 0));
182 }
Defines class InstrumentItem and all its children.
Defines class InstrumentListModel.
Defines class InstrumentsEditController.
QVector< InstrumentItem * > collectedItems() const
QString suggestInstrumentName(const QString &baseName) const
Abstract base class for instrument-specific item classes.
QString instrumentName() const
QModelIndex copyInstrument(const QModelIndex &source)
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
InstrumentListModel(QObject *parent, InstrumentsEditController *ec)
QModelIndex addNewSpecularInstrument()
int rowCount(const QModelIndex &parent=QModelIndex()) const override
InstrumentsEditController * m_ec
QModelIndex addNewOffspecInstrument()
void removeInstrument(const QModelIndex &index)
void onInstrumentNameChanged(const InstrumentItem *instrument)
QModelIndex addNewGISASInstrument()
InstrumentItem * instrumentForIndex(const QModelIndex &index) const
QModelIndex addNewDepthProbeInstrument()
Assembles beam, detector and their relative positions with respect to the sample.
Definition: Instrument.h:26
Class to modify the instruments list or a single instrument and provide the necessary signaling withi...
InstrumentItem * addCopy(const InstrumentItem *instrument, const QString &name)
Copy an instrument, set its name and emit the respective signal.
void instrumentNameChanged(const InstrumentItem *instrument)
Signals name change of the given instrument.
void setInstrumentName(InstrumentItem *instrument, const QString &name)
Set an instrument's name and emit the respective signal.
void removeInstrument(InstrumentItem *instrument)
Add an instrument and emit the respective signal.
InstrumentCollection * collectedItems()
The list of existing instruments.
T * addInstrument()
Add an instrument and emit the respective signal.
QString const & name(EShape k)
Definition: particles.cpp:20