BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
InstrumentLibraryEditor::TreeModel Class Reference

Description

A model extension for InstrumentsTreeModel which.

  • shows a "NEW" sign in the icon of a newly added instrument.
  • creates a HTML text for the Display role

Definition at line 61 of file InstrumentLibraryEditor.h.

Inheritance diagram for InstrumentLibraryEditor::TreeModel:
[legend]
Collaboration diagram for InstrumentLibraryEditor::TreeModel:
[legend]

Public Types

enum  InstrumentType {
  None = 0x0 , Gisas = 0x1 , Offspec = 0x2 , Specular = 0x4 ,
  DepthProbe = 0x8 , All = Gisas | Offspec | Specular | DepthProbe
}
 

Public Member Functions

 TreeModel (QObject *parent, InstrumentCollection *model)
 
int columnCount (const QModelIndex &parent=QModelIndex()) const override
 
QVariant data (const QModelIndex &index, int role) const override
 
void enableEmptyHeadlines (bool b)
 
Qt::ItemFlags flags (const QModelIndex &index) const override
 
QModelIndex index (int row, int column, const QModelIndex &parent=QModelIndex()) const override
 
QModelIndex indexForItem (InstrumentItem *item) const
 
QModelIndex indexOfHeadline (InstrumentType type) const
 
bool isHeadline (const QModelIndex &index) const
 
InstrumentItemitemForIndex (const QModelIndex &index) const
 
QModelIndex parent (const QModelIndex &index) const override
 
void refreshAfterModelChange ()
 
void removeItem (InstrumentItem *item)
 
int rowCount (const QModelIndex &parent=QModelIndex()) const override
 
bool setData (const QModelIndex &index, const QVariant &value, int role) override
 
void setNewInstrument (InstrumentItem *addedInstrument)
 Set the instrument which shall have a "NEW" sign in its icon. More...
 
void setTypeEnabled (InstrumentType type, bool b)
 
InstrumentItemtopMostItem () const
 The topmost visible item. Can be null of course. More...
 

Static Public Member Functions

static InstrumentType instrumentType (InstrumentItem *item)
 

Private Member Functions

void clear ()
 
QVector< InstrumentItem * > instruments (InstrumentType type) const
 
QList< InstrumentTypevisibleTypes () const
 

Private Attributes

bool m_enableEmptyHeadlines
 
InstrumentCollectionm_model = nullptr
 
bool m_namesAreEditable
 
InstrumentItemm_newInstrument = nullptr
 
VisibleInstrumentTypes m_visibleTypes
 

Member Enumeration Documentation

◆ InstrumentType

Enumerator
None 
Gisas 
Offspec 
Specular 
DepthProbe 
All 

Definition at line 29 of file InstrumentsTreeModel.h.

Constructor & Destructor Documentation

◆ TreeModel()

InstrumentLibraryEditor::TreeModel::TreeModel ( QObject *  parent,
InstrumentCollection model 
)

Definition at line 269 of file InstrumentLibraryEditor.cpp.

270  : InstrumentsTreeModel(parent, model)
271  , m_newInstrument(nullptr)
272 {
273 }
QModelIndex parent(const QModelIndex &index) const override
InstrumentsTreeModel(QObject *parent, InstrumentCollection *model)

Member Function Documentation

◆ clear()

void InstrumentsTreeModel::clear ( )
privateinherited

Definition at line 63 of file InstrumentsTreeModel.cpp.

64 {
65  beginResetModel();
66  endResetModel();
67 }

◆ columnCount()

int InstrumentsTreeModel::columnCount ( const QModelIndex &  parent = QModelIndex()) const
overrideinherited

Definition at line 163 of file InstrumentsTreeModel.cpp.

164 {
165  return 1;
166 }

◆ data()

QVariant InstrumentLibraryEditor::TreeModel::data ( const QModelIndex &  index,
int  role 
) const
override

Definition at line 280 of file InstrumentLibraryEditor.cpp.

281 {
282  if (isHeadline(index))
283  return InstrumentsTreeModel::data(index, role);
284 
285  auto* const item = itemForIndex(index);
286 
287  if (role == Qt::DisplayRole) {
288  auto descr = item->description();
289  if (!descr.isEmpty()) {
290  descr.prepend("<br><br>");
291  // max 4 lines
292  while (descr.count("\n") > 3) {
293  descr.truncate(descr.lastIndexOf("\n"));
294  descr += " [...]";
295  }
296  descr.replace("\n", "<br>");
297  }
298  return "<b>" + item->instrumentName() + "</b>" + descr;
299  }
300 
301  if (role == Qt::DecorationRole && (item == m_newInstrument)) {
302  if (role == Qt::DecorationRole)
303  switch (instrumentType(item)) {
304  case Gisas:
305  return QIcon(":/images/gisas_instrument_new.svg");
306  case Offspec:
307  return QIcon(":/images/offspec_instrument_new.svg");
308  case Specular:
309  return QIcon(":/images/specular_instrument_new.svg");
310  case DepthProbe:
311  return QIcon(":/images/depth_instrument_new.svg");
312  default:
313  break;
314  }
315  }
316  return InstrumentsTreeModel::data(index, role);
317 }
static InstrumentType instrumentType(InstrumentItem *item)
InstrumentItem * itemForIndex(const QModelIndex &index) const
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
bool isHeadline(const QModelIndex &index) const
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override

References InstrumentsTreeModel::data().

Here is the call graph for this function:

◆ enableEmptyHeadlines()

void InstrumentsTreeModel::enableEmptyHeadlines ( bool  b)
inherited

Definition at line 32 of file InstrumentsTreeModel.cpp.

33 {
34  if (b != m_enableEmptyHeadlines) {
35  beginResetModel();
37  endResetModel();
38  }
39 }

References InstrumentsTreeModel::m_enableEmptyHeadlines.

Referenced by InstrumentLibraryEditor::InstrumentLibraryEditor().

◆ flags()

Qt::ItemFlags InstrumentsTreeModel::flags ( const QModelIndex &  index) const
overrideinherited

Definition at line 258 of file InstrumentsTreeModel.cpp.

259 {
260  if (isHeadline(index) || !index.isValid())
261  return Qt::NoItemFlags;
262 
263  auto f = QAbstractItemModel::flags(index);
264  f |= Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled;
265 
266  if (index.column() == 0 && m_namesAreEditable) // col 0 contains name of the data entry
267  f |= Qt::ItemIsEditable;
268 
269  return f;
270 }

References InstrumentsTreeModel::index(), InstrumentsTreeModel::isHeadline(), and InstrumentsTreeModel::m_namesAreEditable.

Here is the call graph for this function:

◆ index()

QModelIndex InstrumentsTreeModel::index ( int  row,
int  column,
const QModelIndex &  parent = QModelIndex() 
) const
overrideinherited

Definition at line 132 of file InstrumentsTreeModel.cpp.

133 {
134  if (!hasIndex(row, column, parent))
135  return QModelIndex();
136 
137  if (!parent.isValid())
138  return createIndex(row, column, nullptr);
139 
140  for (auto type : visibleTypes())
141  if (parent == indexOfHeadline(type))
142  return createIndex(row, column, instruments(type)[row]);
143 
144  return QModelIndex();
145 }
QList< InstrumentType > visibleTypes() const
QModelIndex indexOfHeadline(InstrumentType type) const
QVector< InstrumentItem * > instruments(InstrumentType type) const

References InstrumentsTreeModel::indexOfHeadline(), InstrumentsTreeModel::instruments(), InstrumentsTreeModel::parent(), and InstrumentsTreeModel::visibleTypes().

Referenced by InstrumentsTreeModel::data(), InstrumentsTreeModel::flags(), InstrumentsTreeModel::isHeadline(), InstrumentsTreeModel::itemForIndex(), InstrumentsTreeModel::parent(), InstrumentsTreeModel::removeItem(), and InstrumentsTreeModel::setData().

Here is the call graph for this function:

◆ indexForItem()

QModelIndex InstrumentsTreeModel::indexForItem ( InstrumentItem item) const
inherited

Definition at line 300 of file InstrumentsTreeModel.cpp.

301 {
302  if (item == nullptr)
303  return QModelIndex();
304 
305  for (auto type : visibleTypes())
306  if (auto row = instruments(type).indexOf(item); row >= 0)
307  return createIndex(row, 0, item);
308 
309  return QModelIndex();
310 }

References InstrumentsTreeModel::instruments(), and InstrumentsTreeModel::visibleTypes().

Referenced by InstrumentLibraryEditor::execAdd(), and InstrumentsTreeModel::removeItem().

Here is the call graph for this function:

◆ indexOfHeadline()

QModelIndex InstrumentsTreeModel::indexOfHeadline ( InstrumentType  type) const
inherited

Definition at line 120 of file InstrumentsTreeModel.cpp.

121 {
122  int row = 0;
123  for (auto t : visibleTypes()) {
124  if (t == type)
125  return createIndex(row, 0, nullptr);
126  row++;
127  }
128 
129  return QModelIndex();
130 }

References InstrumentsTreeModel::visibleTypes().

Referenced by InstrumentsTreeModel::data(), InstrumentsTreeModel::index(), InstrumentsTreeModel::parent(), and InstrumentsTreeModel::rowCount().

Here is the call graph for this function:

◆ instruments()

QVector< InstrumentItem * > InstrumentsTreeModel::instruments ( InstrumentType  type) const
privateinherited

Definition at line 87 of file InstrumentsTreeModel.cpp.

88 {
89  switch (type) {
90  case Gisas:
91  return m_model->instrumentItems([](const InstrumentItem* p) {
92  return dynamic_cast<const GISASInstrumentItem*>(p) != nullptr;
93  });
94  case Offspec:
95  return m_model->instrumentItems([](const InstrumentItem* p) {
96  return dynamic_cast<const OffspecInstrumentItem*>(p) != nullptr;
97  });
98  case Specular:
99  return m_model->instrumentItems([](const InstrumentItem* p) {
100  return dynamic_cast<const SpecularInstrumentItem*>(p) != nullptr;
101  });
102  case DepthProbe:
103  return m_model->instrumentItems([](const InstrumentItem* p) {
104  return dynamic_cast<const DepthProbeInstrumentItem*>(p) != nullptr;
105  });
106  default:
107  return {};
108  }
109 }
QVector< InstrumentItem * > instrumentItems(const std::function< bool(const InstrumentItem *)> &accept) const
Abstract base class for instrument-specific item classes.
InstrumentCollection * m_model

References InstrumentsTreeModel::DepthProbe, InstrumentsTreeModel::Gisas, InstrumentCollection::instrumentItems(), InstrumentsTreeModel::m_model, InstrumentsTreeModel::Offspec, and InstrumentsTreeModel::Specular.

Referenced by InstrumentsTreeModel::index(), InstrumentsTreeModel::indexForItem(), InstrumentsTreeModel::parent(), InstrumentsTreeModel::rowCount(), InstrumentsTreeModel::topMostItem(), and InstrumentsTreeModel::visibleTypes().

Here is the call graph for this function:

◆ instrumentType()

InstrumentsTreeModel::InstrumentType InstrumentsTreeModel::instrumentType ( InstrumentItem item)
staticinherited

Definition at line 331 of file InstrumentsTreeModel.cpp.

332 {
333  if (item->is<GISASInstrumentItem>())
334  return Gisas;
335 
336  if (item->is<OffspecInstrumentItem>())
337  return Offspec;
338 
339  if (item->is<SpecularInstrumentItem>())
340  return Specular;
341 
342  if (item->is<DepthProbeInstrumentItem>())
343  return DepthProbe;
344 
345  ASSERT(false);
346  return None;
347 }
bool is() const

References InstrumentsTreeModel::DepthProbe, InstrumentsTreeModel::Gisas, InstrumentItem::is(), InstrumentsTreeModel::None, InstrumentsTreeModel::Offspec, and InstrumentsTreeModel::Specular.

Referenced by InstrumentsTreeModel::data(), and InstrumentLibraryEditor::execAdd().

Here is the call graph for this function:

◆ isHeadline()

bool InstrumentsTreeModel::isHeadline ( const QModelIndex &  index) const
inherited

Definition at line 323 of file InstrumentsTreeModel.cpp.

324 {
325  if (!index.isValid())
326  return false;
327 
328  return index.internalPointer() == nullptr;
329 }

References InstrumentsTreeModel::index().

Referenced by InstrumentsTreeModel::data(), InstrumentsTreeModel::flags(), and InstrumentLibraryEditor::getOverlayActions().

Here is the call graph for this function:

◆ itemForIndex()

InstrumentItem * InstrumentsTreeModel::itemForIndex ( const QModelIndex &  index) const
inherited

Definition at line 292 of file InstrumentsTreeModel.cpp.

293 {
294  if (!index.isValid())
295  return nullptr;
296 
297  return reinterpret_cast<InstrumentItem*>(index.internalPointer());
298 }

References InstrumentsTreeModel::index().

Referenced by InstrumentLibraryEditor::createWidgetsForCurrentInstrument(), InstrumentsTreeModel::data(), InstrumentLibraryEditor::getOverlayActions(), InstrumentLibraryEditor::onCurrentChangedForChoose(), InstrumentLibraryEditor::onInstrumentChangedByEditor(), InstrumentLibraryEditor::onItemDoubleClickedForChoose(), InstrumentsTreeModel::parent(), and InstrumentsTreeModel::setData().

Here is the call graph for this function:

◆ parent()

QModelIndex InstrumentsTreeModel::parent ( const QModelIndex &  index) const
overrideinherited

Definition at line 147 of file InstrumentsTreeModel.cpp.

148 {
149  if (!index.isValid())
150  return QModelIndex();
151 
152  if (index.internalPointer() == nullptr) // index is headline => no parent
153  return QModelIndex();
154 
155  auto* item = itemForIndex(index);
156  for (auto type : visibleTypes())
157  if (instruments(type).contains(item))
158  return indexOfHeadline(type);
159 
160  return QModelIndex();
161 }

References InstrumentsTreeModel::index(), InstrumentsTreeModel::indexOfHeadline(), InstrumentsTreeModel::instruments(), InstrumentsTreeModel::itemForIndex(), and InstrumentsTreeModel::visibleTypes().

Referenced by InstrumentsTreeModel::index(), and InstrumentsTreeModel::rowCount().

Here is the call graph for this function:

◆ refreshAfterModelChange()

void InstrumentsTreeModel::refreshAfterModelChange ( )
inherited

Definition at line 50 of file InstrumentsTreeModel.cpp.

51 {
52  // for (auto rank : m_visibleRanks) {
53  // if (!m_items[rank - 1].isEmpty()) {
54  // beginRemoveRows(indexOfHeadline(rank), 0, m_items[rank - 1].size() - 1);
55  // m_items[rank - 1] = m_model->InstrumentCollection(rank);
56  // endRemoveRows();
57  // }
58  // }
59  //
60  // updateSubscriptions();
61 }

◆ removeItem()

void InstrumentsTreeModel::removeItem ( InstrumentItem item)
inherited

Definition at line 312 of file InstrumentsTreeModel.cpp.

313 {
314  QModelIndex index = indexForItem(item);
315  if (!index.isValid())
316  return;
317 
318  beginRemoveRows(index.parent(), index.row(), index.row());
319  m_model->removeInstrument(item);
320  endRemoveRows();
321 }
void removeInstrument(InstrumentItem *instrument)
QModelIndex indexForItem(InstrumentItem *item) const

References InstrumentsTreeModel::index(), InstrumentsTreeModel::indexForItem(), InstrumentsTreeModel::m_model, and InstrumentCollection::removeInstrument().

Referenced by InstrumentLibraryEditor::getOverlayActions().

Here is the call graph for this function:

◆ rowCount()

int InstrumentsTreeModel::rowCount ( const QModelIndex &  parent = QModelIndex()) const
overrideinherited

Definition at line 168 of file InstrumentsTreeModel.cpp.

169 {
170  if (!parent.isValid())
171  return visibleTypes().size();
172 
173  // parent is a headline
174  for (auto type : visibleTypes())
175  if (parent == indexOfHeadline(type))
176  return instruments(type).size();
177 
178  return 0;
179 }

References InstrumentsTreeModel::indexOfHeadline(), InstrumentsTreeModel::instruments(), InstrumentsTreeModel::parent(), and InstrumentsTreeModel::visibleTypes().

Here is the call graph for this function:

◆ setData()

bool InstrumentsTreeModel::setData ( const QModelIndex &  index,
const QVariant &  value,
int  role 
)
overrideinherited

Definition at line 272 of file InstrumentsTreeModel.cpp.

273 {
274  if (!index.isValid())
275  return false;
276 
277  if (role == Qt::EditRole && index.column() == 0) {
278  itemForIndex(index)->setInstrumentName(value.toString());
279  emit dataChanged(index, index);
280  return true;
281  }
282 
283  if (role == Qt::ToolTipRole && index.column() == 0) {
284  itemForIndex(index)->setDescription(value.toString());
285  emit dataChanged(index, index);
286  return true;
287  }
288 
289  return false;
290 }
void setDescription(const QString &description)
void setInstrumentName(const QString &instrumentName)

References InstrumentsTreeModel::index(), InstrumentsTreeModel::itemForIndex(), InstrumentItem::setDescription(), and InstrumentItem::setInstrumentName().

Referenced by InstrumentLibraryEditor::onInstrumentDescriptionEdited(), and InstrumentLibraryEditor::onInstrumentNameEdited().

Here is the call graph for this function:

◆ setNewInstrument()

void InstrumentLibraryEditor::TreeModel::setNewInstrument ( InstrumentItem addedInstrument)

Set the instrument which shall have a "NEW" sign in its icon.

Definition at line 275 of file InstrumentLibraryEditor.cpp.

276 {
277  m_newInstrument = addedInstrument;
278 }

Referenced by InstrumentLibraryEditor::execAdd().

◆ setTypeEnabled()

void InstrumentsTreeModel::setTypeEnabled ( InstrumentType  type,
bool  b 
)
inherited

Definition at line 41 of file InstrumentsTreeModel.cpp.

42 {
43  if (m_visibleTypes.testFlag(type) != b) {
44  beginResetModel();
45  m_visibleTypes.setFlag(type, b);
46  endResetModel();
47  }
48 }
VisibleInstrumentTypes m_visibleTypes

References InstrumentsTreeModel::m_visibleTypes.

Referenced by InstrumentLibraryEditor::execAdd(), InstrumentLibraryEditor::setDepthProbeEnabled(), InstrumentLibraryEditor::setGisasEnabled(), InstrumentLibraryEditor::setOffspecEnabled(), and InstrumentLibraryEditor::setSpecularEnabled().

◆ topMostItem()

InstrumentItem * InstrumentsTreeModel::topMostItem ( ) const
inherited

The topmost visible item. Can be null of course.

Definition at line 111 of file InstrumentsTreeModel.cpp.

112 {
113  for (auto t : visibleTypes())
114  if (auto instr = instruments(t); !instr.isEmpty())
115  return instr.first();
116 
117  return nullptr;
118 }

References InstrumentsTreeModel::instruments(), and InstrumentsTreeModel::visibleTypes().

Here is the call graph for this function:

◆ visibleTypes()

QList< InstrumentsTreeModel::InstrumentType > InstrumentsTreeModel::visibleTypes ( ) const
privateinherited

Definition at line 69 of file InstrumentsTreeModel.cpp.

70 {
71  QList<InstrumentsTreeModel::InstrumentType> result;
72 
73  const auto forType = [&](InstrumentType type) {
74  if (m_visibleTypes.testFlag(type)
75  && (m_enableEmptyHeadlines || !instruments(type).isEmpty()))
76  result << type;
77  };
78 
79  forType(Gisas);
80  forType(Offspec);
81  forType(Specular);
82  forType(DepthProbe);
83 
84  return result;
85 }

References InstrumentsTreeModel::DepthProbe, InstrumentsTreeModel::Gisas, InstrumentsTreeModel::instruments(), InstrumentsTreeModel::m_enableEmptyHeadlines, InstrumentsTreeModel::m_visibleTypes, InstrumentsTreeModel::Offspec, and InstrumentsTreeModel::Specular.

Referenced by InstrumentsTreeModel::index(), InstrumentsTreeModel::indexForItem(), InstrumentsTreeModel::indexOfHeadline(), InstrumentsTreeModel::parent(), InstrumentsTreeModel::rowCount(), and InstrumentsTreeModel::topMostItem().

Here is the call graph for this function:

Member Data Documentation

◆ m_enableEmptyHeadlines

bool InstrumentsTreeModel::m_enableEmptyHeadlines
privateinherited

◆ m_model

InstrumentCollection* InstrumentsTreeModel::m_model = nullptr
privateinherited

◆ m_namesAreEditable

bool InstrumentsTreeModel::m_namesAreEditable
privateinherited

Definition at line 73 of file InstrumentsTreeModel.h.

Referenced by InstrumentsTreeModel::flags().

◆ m_newInstrument

InstrumentItem* InstrumentLibraryEditor::TreeModel::m_newInstrument = nullptr
private

Definition at line 71 of file InstrumentLibraryEditor.h.

◆ m_visibleTypes

VisibleInstrumentTypes InstrumentsTreeModel::m_visibleTypes
privateinherited

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