BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
RealDataTreeModel Class Reference

Description

Tree model for real data item selection. Used for the tree in the import view.

Definition at line 25 of file RealDataTreeModel.h.

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

Public Member Functions

 RealDataTreeModel (QObject *parent, RealDataModel *model)
 
int columnCount (const QModelIndex &parent=QModelIndex()) const override
 
QVariant data (const QModelIndex &index, int role=Qt::DisplayRole) const override
 
Qt::ItemFlags flags (const QModelIndex &index) const override
 
QModelIndex index (int row, int column, const QModelIndex &parent=QModelIndex()) const override
 
QModelIndex indexForItem (RealDataItem *item) const
 
QModelIndex indexOfHeadline (int rank) const
 
RealDataIteminsertDataItem (int rank)
 
RealDataIteminsertIntensityDataItem ()
 
RealDataIteminsertSpecularDataItem ()
 
bool isHeadline (const QModelIndex &index) const
 
RealDataItemitemForIndex (const QModelIndex &index) const
 
QModelIndex parent (const QModelIndex &index) const override
 
void refreshAfterModelChange ()
 
void removeItem (RealDataItem *item)
 
int rowCount (const QModelIndex &parent=QModelIndex()) const override
 
bool setData (const QModelIndex &index, const QVariant &value, int role) override
 
void setVisibleRanks (QSet< int > visibleRanks)
 
RealDataItemtopMostItem () const
 The topmost visible item. Can be null of course. More...
 

Private Member Functions

void clear ()
 
void onContentsProcessed (RealDataItem *item)
 
void updateSubscriptions ()
 

Private Attributes

QVector< RealDataItem * > m_items [2]
 
RealDataModelm_model = nullptr
 
QSet< int > m_visibleRanks
 

Constructor & Destructor Documentation

◆ RealDataTreeModel()

RealDataTreeModel::RealDataTreeModel ( QObject *  parent,
RealDataModel model 
)
explicit

Definition at line 23 of file RealDataTreeModel.cpp.

24  : QAbstractItemModel(parent)
25  , m_model(model)
26  , m_visibleRanks({1, 2})
27 {
28  for (auto rank : m_visibleRanks)
29  m_items[rank - 1] = model->realDataItems(rank);
30 
32 
33  connect(m_model, &RealDataModel::modelAboutToBeReset, this, &RealDataTreeModel::clear,
34  Qt::UniqueConnection);
35 }
QVector< RealDataItem * > realDataItems() const
QModelIndex parent(const QModelIndex &index) const override
QSet< int > m_visibleRanks
RealDataModel * m_model
QVector< RealDataItem * > m_items[2]

Member Function Documentation

◆ clear()

void RealDataTreeModel::clear ( )
private

Definition at line 65 of file RealDataTreeModel.cpp.

66 {
67  beginResetModel();
68  m_items[0].clear();
69  m_items[1].clear();
70  endResetModel();
72 }

References m_items, and updateSubscriptions().

Here is the call graph for this function:

◆ columnCount()

int RealDataTreeModel::columnCount ( const QModelIndex &  parent = QModelIndex()) const
override

Definition at line 171 of file RealDataTreeModel.cpp.

172 {
173  return 1;
174 }

◆ data()

QVariant RealDataTreeModel::data ( const QModelIndex &  index,
int  role = Qt::DisplayRole 
) const
override

Definition at line 189 of file RealDataTreeModel.cpp.

190 {
191  if (isHeadline(index)) {
192  QString title = (index == indexOfHeadline(1)) ? "1D Data" : "2D Data";
193  if (m_visibleRanks.size() < 2)
194  title = "Data";
195 
196  switch (role) {
197  case Qt::DisplayRole:
198  return title;
199 
200  case Qt::FontRole: {
201  QFont f(QApplication::font());
202  f.setPointSize(f.pointSize() * 1.5);
203  f.setBold(true);
204  return f;
205  }
206 
207  case Qt::SizeHintRole: {
208  QFont f(QApplication::font());
209  f.setPointSize(f.pointSize() * 1.5);
210  f.setBold(true);
211  QSize s = QFontMetrics(f).boundingRect(title).size();
212  return QSize(s.width() * 2, s.height() * 2);
213  }
214 
215  case Qt::TextAlignmentRole:
216  return QVariant(Qt::AlignLeft | Qt::AlignVCenter);
217 
218  case Qt::BackgroundRole:
220 
221  case Qt::ForegroundRole:
223 
224  default:
225  return QVariant();
226  }
227  }
228 
229  auto* const item = itemForIndex(index);
230 
231  if (role == Qt::ToolTipRole)
232  return QString();
233 
234  if (role == Qt::DecorationRole) {
235  if (item->isSpecularData())
236  return item->hasImportErrors() ? QIcon(":/images/warning_16x16.png")
237  : QIcon(":/images/1D_OK.png");
238  return QIcon(":/images/2D_OK.png");
239  }
240 
241  if (role == Qt::DisplayRole) {
242  const auto numErrors =
243  (item->dataLoader() != nullptr) ? item->dataLoader()->numErrors() : 0;
244 
245  if (numErrors == 0)
246  return item->dataName();
247  if (numErrors == 1)
248  return item->dataName() + " (1 parser warning)";
249  return item->dataName() + QString(" (%1 parser warnings)").arg(numErrors);
250  }
251 
252  if (role == Qt::EditRole)
253  return item->dataName();
254 
255  return QVariant();
256 }
ApplicationSettings * appSettings
global pointer to the instance
const Palette & styleSheetPalette() const
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
bool isHeadline(const QModelIndex &index) const
RealDataItem * itemForIndex(const QModelIndex &index) const
QModelIndex indexOfHeadline(int rank) const

References appSettings, ApplicationSettings::Palette::headlineBackground, ApplicationSettings::Palette::headlineText, index(), indexOfHeadline(), isHeadline(), itemForIndex(), m_visibleRanks, and ApplicationSettings::styleSheetPalette().

Here is the call graph for this function:

◆ flags()

Qt::ItemFlags RealDataTreeModel::flags ( const QModelIndex &  index) const
override

Definition at line 258 of file RealDataTreeModel.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) // col 0 contains name of the data entry
267  f |= Qt::ItemIsEditable;
268 
269  return f;
270 }

References index(), and isHeadline().

Here is the call graph for this function:

◆ index()

QModelIndex RealDataTreeModel::index ( int  row,
int  column,
const QModelIndex &  parent = QModelIndex() 
) const
override

Definition at line 142 of file RealDataTreeModel.cpp.

143 {
144  if (!hasIndex(row, column, parent))
145  return QModelIndex();
146 
147  if (!parent.isValid())
148  return createIndex(row, column, nullptr);
149 
150  for (auto rank : m_visibleRanks)
151  if (parent == indexOfHeadline(rank))
152  return createIndex(row, column, m_items[rank - 1][row]);
153 
154  return QModelIndex();
155 }

References indexOfHeadline(), m_items, m_visibleRanks, and parent().

Referenced by data(), flags(), indexForItem(), isHeadline(), itemForIndex(), onContentsProcessed(), parent(), removeItem(), and setData().

Here is the call graph for this function:

◆ indexForItem()

QModelIndex RealDataTreeModel::indexForItem ( RealDataItem item) const

Definition at line 294 of file RealDataTreeModel.cpp.

295 {
296  if (item == nullptr)
297  return QModelIndex();
298 
299  const int rank = item->isSpecularData() ? 1 : 2;
300  if (m_visibleRanks.contains(rank))
301  if (auto index = m_items[rank - 1].indexOf(item); index >= 0)
302  return createIndex(index, 0, item);
303 
304  return QModelIndex();
305 }
bool isSpecularData() const

References index(), RealDataItem::isSpecularData(), m_items, and m_visibleRanks.

Referenced by onContentsProcessed(), removeItem(), and RealDataSelectorWidget::setCurrentItem().

Here is the call graph for this function:

◆ indexOfHeadline()

QModelIndex RealDataTreeModel::indexOfHeadline ( int  rank) const

Definition at line 126 of file RealDataTreeModel.cpp.

127 {
128  if (!m_visibleRanks.contains(rank))
129  return QModelIndex();
130 
131  if (rank == 1)
132  return createIndex(0, 0, nullptr);
133 
134  if (rank == 2) {
135  const bool has1D = m_visibleRanks.contains(1);
136  return createIndex(has1D ? 1 : 0, 0, nullptr);
137  }
138 
139  return QModelIndex();
140 }

References m_visibleRanks.

Referenced by data(), RealDataSelectorWidget::getOverlayActions(), index(), insertDataItem(), parent(), refreshAfterModelChange(), removeItem(), and rowCount().

◆ insertDataItem()

RealDataItem * RealDataTreeModel::insertDataItem ( int  rank)

Definition at line 103 of file RealDataTreeModel.cpp.

104 {
105  auto* newItem = m_model->insertDataItem(rank);
106  if (!m_visibleRanks.contains(rank))
107  return newItem;
108 
109  const int rowOfItem = m_model->realDataItems(rank).indexOf(newItem);
110  beginInsertRows(indexOfHeadline(rank), rowOfItem, rowOfItem);
111  m_items[rank - 1] = m_model->realDataItems(rank);
112  endInsertRows();
114  return newItem;
115 }
RealDataItem * insertDataItem(int rank)

References indexOfHeadline(), RealDataModel::insertDataItem(), m_items, m_model, m_visibleRanks, RealDataModel::realDataItems(), and updateSubscriptions().

Referenced by insertIntensityDataItem(), and insertSpecularDataItem().

Here is the call graph for this function:

◆ insertIntensityDataItem()

RealDataItem * RealDataTreeModel::insertIntensityDataItem ( )

Definition at line 98 of file RealDataTreeModel.cpp.

99 {
100  return insertDataItem(2);
101 }
RealDataItem * insertDataItem(int rank)

References insertDataItem().

Referenced by RealDataSelectorWidget::importData2D().

Here is the call graph for this function:

◆ insertSpecularDataItem()

RealDataItem * RealDataTreeModel::insertSpecularDataItem ( )

Definition at line 93 of file RealDataTreeModel.cpp.

94 {
95  return insertDataItem(1);
96 }

References insertDataItem().

Referenced by RealDataSelectorWidget::importData1D().

Here is the call graph for this function:

◆ isHeadline()

bool RealDataTreeModel::isHeadline ( const QModelIndex &  index) const

Definition at line 307 of file RealDataTreeModel.cpp.

308 {
309  if (!index.isValid())
310  return false;
311 
312  return index.internalPointer() == nullptr;
313 }

References index().

Referenced by data(), flags(), and RealDataSelectorWidget::getOverlayActions().

Here is the call graph for this function:

◆ itemForIndex()

RealDataItem * RealDataTreeModel::itemForIndex ( const QModelIndex &  index) const

Definition at line 286 of file RealDataTreeModel.cpp.

287 {
288  if (!index.isValid())
289  return nullptr;
290 
291  return reinterpret_cast<RealDataItem*>(index.internalPointer());
292 }
Provides access to experimental data, for display and fitting. Owns an AbstractDataLoader.
Definition: RealDataItem.h:33

References index().

Referenced by RealDataSelectorWidget::currentItem(), data(), RealDataSelectorWidget::getOverlayActions(), RealDataSelectorWidget::onContextMenuRequest(), parent(), and setData().

Here is the call graph for this function:

◆ onContentsProcessed()

void RealDataTreeModel::onContentsProcessed ( RealDataItem item)
private

Definition at line 326 of file RealDataTreeModel.cpp.

327 {
328  QModelIndex index = indexForItem(item);
329  emit dataChanged(index, index);
330 }
QModelIndex indexForItem(RealDataItem *item) const

References index(), and indexForItem().

Referenced by updateSubscriptions().

Here is the call graph for this function:

◆ parent()

QModelIndex RealDataTreeModel::parent ( const QModelIndex &  index) const
override

Definition at line 157 of file RealDataTreeModel.cpp.

158 {
159  if (!index.isValid())
160  return QModelIndex();
161 
162  if (index.internalPointer() == nullptr) // index is headline => no parent
163  return QModelIndex();
164 
165  if (itemForIndex(index)->isSpecularData())
166  return indexOfHeadline(1);
167 
168  return indexOfHeadline(2);
169 }

References index(), indexOfHeadline(), and itemForIndex().

Referenced by index(), and rowCount().

Here is the call graph for this function:

◆ refreshAfterModelChange()

void RealDataTreeModel::refreshAfterModelChange ( )

Definition at line 52 of file RealDataTreeModel.cpp.

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

References indexOfHeadline(), m_items, m_model, m_visibleRanks, RealDataModel::realDataItems(), and updateSubscriptions().

Here is the call graph for this function:

◆ removeItem()

void RealDataTreeModel::removeItem ( RealDataItem item)

Definition at line 74 of file RealDataTreeModel.cpp.

75 {
76  QModelIndex index = indexForItem(item);
77  if (!index.isValid())
78  return;
79 
80  if (item->dataLoader())
81  item->dataLoader()->disconnect(this);
82 
83  const int rank = item->isSpecularData() ? 1 : 2;
84  if (m_visibleRanks.contains(rank)) {
85  const int rowOfItem = m_items[rank - 1].indexOf(item);
86  beginRemoveRows(indexOfHeadline(rank), rowOfItem, rowOfItem);
87  m_items[rank - 1].removeAll(item);
88  m_model->remove(item);
89  endRemoveRows();
90  }
91 }
AbstractDataLoader * dataLoader() const
void remove(RealDataItem *item)

References RealDataItem::dataLoader(), index(), indexForItem(), indexOfHeadline(), RealDataItem::isSpecularData(), m_items, m_model, m_visibleRanks, and RealDataModel::remove().

Referenced by RealDataSelectorWidget::getOverlayActions(), RealDataSelectorWidget::importData1D(), RealDataSelectorWidget::importData2D(), and RealDataSelectorWidget::removeCurrentItem().

Here is the call graph for this function:

◆ rowCount()

int RealDataTreeModel::rowCount ( const QModelIndex &  parent = QModelIndex()) const
override

Definition at line 176 of file RealDataTreeModel.cpp.

177 {
178  if (!parent.isValid())
179  return m_visibleRanks.size();
180 
181  // parent is a headline
182  for (int rank : m_visibleRanks)
183  if (parent == indexOfHeadline(rank))
184  return m_items[rank - 1].size();
185 
186  return 0;
187 }

References indexOfHeadline(), m_items, m_visibleRanks, and parent().

Here is the call graph for this function:

◆ setData()

bool RealDataTreeModel::setData ( const QModelIndex &  index,
const QVariant &  value,
int  role 
)
override

Definition at line 272 of file RealDataTreeModel.cpp.

273 {
274  if (!index.isValid())
275  return false;
276 
277  if (role == Qt::EditRole && index.column() == 0) {
278  itemForIndex(index)->setDataName(value.toString());
279  emit dataChanged(index, index);
280  return true;
281  }
282 
283  return false;
284 }
void setDataName(const QString &name)

References index(), itemForIndex(), and RealDataItem::setDataName().

Here is the call graph for this function:

◆ setVisibleRanks()

void RealDataTreeModel::setVisibleRanks ( QSet< int >  visibleRanks)

Definition at line 37 of file RealDataTreeModel.cpp.

38 {
39  if (m_visibleRanks == visibleRanks)
40  return;
41 
42  m_visibleRanks = visibleRanks;
43 
44  beginResetModel();
45  for (auto rank : m_visibleRanks)
46  m_items[rank - 1] = m_model->realDataItems(rank);
47  endResetModel();
48 
50 }

References m_items, m_model, m_visibleRanks, RealDataModel::realDataItems(), and updateSubscriptions().

Referenced by RealDataSelectorWidget::updateFunctionalities().

Here is the call graph for this function:

◆ topMostItem()

RealDataItem * RealDataTreeModel::topMostItem ( ) const

The topmost visible item. Can be null of course.

Definition at line 117 of file RealDataTreeModel.cpp.

118 {
119  if (!m_items[0].isEmpty() && m_visibleRanks.contains(1))
120  return m_items[0].first();
121  if (!m_items[1].isEmpty() && m_visibleRanks.contains(2))
122  return m_items[1].first();
123  return nullptr;
124 }

References m_items, and m_visibleRanks.

Referenced by RealDataSelectorWidget::RealDataSelectorWidget().

◆ updateSubscriptions()

void RealDataTreeModel::updateSubscriptions ( )
private

Definition at line 315 of file RealDataTreeModel.cpp.

316 {
317  for (auto* item : m_items[0])
318  connect(item, &RealDataItem::importContentsProcessed, this,
319  [=]() { onContentsProcessed(item); });
320 
321  for (auto* item : m_items[1])
322  connect(item, &RealDataItem::importContentsProcessed, this,
323  [=]() { onContentsProcessed(item); });
324 }
void importContentsProcessed()
void onContentsProcessed(RealDataItem *item)

References RealDataItem::importContentsProcessed(), m_items, and onContentsProcessed().

Referenced by clear(), insertDataItem(), refreshAfterModelChange(), and setVisibleRanks().

Here is the call graph for this function:

Member Data Documentation

◆ m_items

QVector<RealDataItem*> RealDataTreeModel::m_items[2]
private

◆ m_model

RealDataModel* RealDataTreeModel::m_model = nullptr
private

◆ m_visibleRanks

QSet<int> RealDataTreeModel::m_visibleRanks
private

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