BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
SessionModel Class Reference
Inheritance diagram for SessionModel:
[legend]
Collaboration diagram for SessionModel:
[legend]

Classes

struct  SessionModelImpl
 Pimpl class for SessionModel. More...
 

Signals

void modelLoaded ()
 

Public Member Functions

 SessionModel (QString model_tag, QObject *parent=0)
 
virtual ~SessionModel ()
 
QVector< QString > acceptableDefaultItemTypes (const QModelIndex &parent) const
 
virtual bool canDropMimeData (const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const
 
virtual void clear ()
 
virtual int columnCount (const QModelIndex &parent) const
 
template<typename T >
T * copyItem (const T *item_to_copy, SessionItem *new_parent=0, const QString &tag="")
 
virtual SessionModelcreateCopy (SessionItem *parent=0)
 
void createRootItem ()
 
virtual QVariant data (const QModelIndex &index, int role) const
 
virtual bool dropMimeData (const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
 
virtual Qt::ItemFlags flags (const QModelIndex &index) const
 
QString getModelName () const
 
QString getModelTag () const
 
virtual QVariant headerData (int section, Qt::Orientation orientation, int role) const
 
virtual QModelIndex index (int row, int column, const QModelIndex &parent) const
 
QModelIndex indexOfItem (SessionItem *item) const
 
virtual void initFrom (SessionModel *model, SessionItem *parent)
 
template<typename T >
T * insertItem (const QModelIndex &parent, int row=-1, QString tag="")
 
template<typename T >
T * insertItem (SessionItem *parent=nullptr, int row=-1, QString tag="")
 
SessionIteminsertNewItem (QString model_type, const QModelIndex &parent_item, int row=-1, QString tag="")
 
SessionIteminsertNewItem (QString model_type, SessionItem *parent_item=nullptr, int row=-1, QString tag="")
 
SessionItemitemForIndex (const QModelIndex &index) const
 
void load (const QString &filename="")
 
virtual QMimeData * mimeData (const QModelIndexList &indexes) const
 
virtual QStringList mimeTypes () const
 
SessionItemmoveItem (SessionItem *item, SessionItem *new_parent=0, int row=-1, const QString &tag="")
 Move given parameterized item to the new_parent at given row. More...
 
virtual QVector< SessionItem * > nonXMLItems () const
 
virtual QModelIndex parent (const QModelIndex &child) const
 
virtual void readFrom (QXmlStreamReader *reader, MessageService *messageService=0)
 
void removeItem (SessionItem *item)
 
virtual bool removeRows (int row, int count, const QModelIndex &parent)
 
SessionItemrootItem () const
 
virtual int rowCount (const QModelIndex &parent) const
 
void save (const QString &filename="")
 
virtual bool setData (const QModelIndex &index, const QVariant &value, int role)
 
void setDraggedItemType (const QString &type)
 
virtual bool setHeaderData (int, Qt::Orientation, const QVariant &, int=Qt::EditRole)
 
virtual Qt::DropActions supportedDragActions () const
 
virtual Qt::DropActions supportedDropActions () const
 
template<typename T = SessionItem>
T * topItem () const
 Returns first item in list of topItems. More...
 
template<typename T = SessionItem>
QVector< T * > topItems () const
 
template<typename T = SessionItem>
QVector< T * > topItems (std::function< bool(const T &)> accept) const
 
virtual void writeTo (QXmlStreamWriter *writer, SessionItem *parent=0)
 

Protected Member Functions

void setRootItem (SessionItem *root)
 

Private Member Functions

SessionItemcopy (const SessionItem *item_to_copy, SessionItem *new_parent=0, const QString &tag="")
 Copy given item to the new_parent at given row. More...
 

Private Attributes

QString m_dragged_item_type
 
QString m_model_tag
 model tag (SampleModel, InstrumentModel) More...
 
QString m_name
 model name More...
 
SessionItemm_root_item
 

Friends

class SessionItem
 

Detailed Description

Definition at line 23 of file SessionModel.h.

Constructor & Destructor Documentation

◆ SessionModel()

SessionModel::SessionModel ( QString  model_tag,
QObject *  parent = 0 
)
explicit

Definition at line 30 of file SessionModel.cpp.

31  : QAbstractItemModel(parent), m_root_item(0), m_name("DefaultName"), m_model_tag(model_tag)
32 {
34 }
virtual QModelIndex parent(const QModelIndex &child) const
QString m_name
model name
Definition: SessionModel.h:121
void createRootItem()
QString m_model_tag
model tag (SampleModel, InstrumentModel)
Definition: SessionModel.h:122
SessionItem * m_root_item
Definition: SessionModel.h:119

References createRootItem().

Here is the call graph for this function:

◆ ~SessionModel()

SessionModel::~SessionModel ( )
virtual

Definition at line 44 of file SessionModel.cpp.

45 {
46  delete m_root_item;
47 }

References m_root_item.

Member Function Documentation

◆ acceptableDefaultItemTypes()

QVector< QString > SessionModel::acceptableDefaultItemTypes ( const QModelIndex &  parent) const

Definition at line 299 of file SessionModel.cpp.

300 {
301  QVector<QString> result;
302  if (SessionItem* parent_item = itemForIndex(parent))
303  result = parent_item->acceptableDefaultItemTypes();
304 
305  return result;
306 }
SessionItem * itemForIndex(const QModelIndex &index) const

References itemForIndex(), and parent().

Referenced by canDropMimeData(), flags(), and SampleTreeWidget::showContextMenu().

Here is the call graph for this function:

◆ canDropMimeData()

bool SessionModel::canDropMimeData ( const QMimeData *  data,
Qt::DropAction  action,
int  row,
int  column,
const QModelIndex &  parent 
) const
virtual

Definition at line 199 of file SessionModel.cpp.

201 {
202  Q_UNUSED(row);
203 
204  if (action == Qt::IgnoreAction)
205  return true;
206  if (action != Qt::MoveAction || column > 0 || !data
207  || !data->hasFormat(SessionXML::ItemMimeType))
208  return false;
209  if (!parent.isValid())
210  return true;
211  QVector<QString> acceptable_child_items = acceptableDefaultItemTypes(parent);
212  QByteArray xml_data = qUncompress(data->data(SessionXML::ItemMimeType));
213  QXmlStreamReader reader(xml_data);
214  while (!reader.atEnd()) {
215  reader.readNext();
216  if (reader.isStartElement()) {
217  if (reader.name() == SessionXML::ItemTag) {
218  const QString model_type =
219  reader.attributes().value(SessionXML::ModelTypeAttribute).toString();
220  return acceptable_child_items.contains(model_type);
221  }
222  }
223  }
224  return false;
225 }
virtual QVariant data(const QModelIndex &index, int role) const
QVector< QString > acceptableDefaultItemTypes(const QModelIndex &parent) const
std::string model_type
Definition: types.h:23
const QString ModelTypeAttribute("ModelType")
const QString ItemTag("Item")
const QString ItemMimeType
Definition: SessionXML.h:26

References acceptableDefaultItemTypes(), data(), SessionXML::ItemMimeType, SessionXML::ItemTag(), SessionXML::ModelTypeAttribute(), and parent().

Referenced by dropMimeData().

Here is the call graph for this function:

◆ clear()

void SessionModel::clear ( )
virtual

Reimplemented in JobModel.

Definition at line 308 of file SessionModel.cpp.

309 {
310  beginResetModel();
311  delete m_root_item;
312  createRootItem();
313  endResetModel();
314 }

References createRootItem(), and m_root_item.

Referenced by JobModel::clear(), DetectorMaskDelegate::createIntensityDataItem(), load(), MaterialEditorDialog::onOKButton(), readFrom(), and ApplicationModels::resetModels().

Here is the call graph for this function:

◆ columnCount()

int SessionModel::columnCount ( const QModelIndex &  parent) const
virtual

Definition at line 119 of file SessionModel.cpp.

120 {
121  if (parent.isValid() && parent.column() != 0)
122  return 0;
124 }

References SessionFlags::MAX_COLUMNS, and parent().

Referenced by data(), and index().

Here is the call graph for this function:

◆ copy()

SessionItem * SessionModel::copy ( const SessionItem item_to_copy,
SessionItem new_parent = 0,
const QString &  tag = "" 
)
private

Copy given item to the new_parent at given row.

Item intended for copying can belong to another model and it will remain intact. Returns pointer to the new child.

Definition at line 419 of file SessionModel.cpp.

421 {
422  if (!new_parent)
423  new_parent = m_root_item;
424 
425  const QString tagName = tag.isEmpty() ? new_parent->defaultTag() : tag;
426 
427  QByteArray xml_data;
428  QXmlStreamWriter writer(&xml_data);
429  SessionXML::writeItemAndChildItems(&writer, item_to_copy);
430 
431  QXmlStreamReader reader(xml_data);
432  SessionXML::readItems(&reader, new_parent, tagName);
433 
434  return new_parent->getItems(tagName).back();
435 }
QVector< SessionItem * > getItems(const QString &tag="") const
Returns vector of all items of given tag.
QString defaultTag() const
Get default tag.
void writeItemAndChildItems(QXmlStreamWriter *writer, const SessionItem *item)
Definition: SessionXML.cpp:49
void readItems(QXmlStreamReader *reader, SessionItem *parent, QString topTag="", MessageService *messageService=nullptr)
Definition: SessionXML.cpp:122

References SessionItem::defaultTag(), SessionItem::getItems(), m_root_item, SessionXML::readItems(), and SessionXML::writeItemAndChildItems().

Referenced by copyItem().

Here is the call graph for this function:

◆ copyItem()

template<typename T >
T * SessionModel::copyItem ( const T *  item_to_copy,
SessionItem new_parent = 0,
const QString &  tag = "" 
)

Definition at line 136 of file SessionModel.h.

137 {
138  return static_cast<T*>(copy(item_to_copy, new_parent, tag));
139 }
SessionItem * copy(const SessionItem *item_to_copy, SessionItem *new_parent=0, const QString &tag="")
Copy given item to the new_parent at given row.

References copy().

Referenced by JobModel::addJob(), MaterialModel::cloneMaterial(), JobModelFunctions::copyRealDataItem(), DetectorItem::importMasks(), MaterialItemContainer::insertCopy(), InstrumentViewActions::onCloneInstrument(), JobModelFunctions::setupJobItemInstrument(), and JobModelFunctions::setupJobItemSampleData().

Here is the call graph for this function:

◆ createCopy()

SessionModel * SessionModel::createCopy ( SessionItem parent = 0)
virtual

Reimplemented in InstrumentModel, SampleModel, and MaterialModel.

Definition at line 437 of file SessionModel.cpp.

438 {
439  Q_UNUSED(parent);
440  throw GUIHelpers::Error("SessionModel::createCopy() -> Error. Not implemented.");
441 }

References parent().

Here is the call graph for this function:

◆ createRootItem()

void SessionModel::createRootItem ( )

Definition at line 36 of file SessionModel.cpp.

37 {
39  m_root_item->setModel(this);
40  m_root_item->registerTag("rootTag");
41  m_root_item->setDefaultTag("rootTag");
42 }
bool registerTag(const QString &name, int min=0, int max=-1, QStringList modelTypes={})
Add new tag to this item with given name, min, max and types.
void setDefaultTag(const QString &tag)
Set default tag.
void setModel(SessionModel *model)
SessionItem * CreateEmptyItem()
create empty SessionItem that serves as a root item
Definition: ItemFactory.cpp:39

References ItemFactory::CreateEmptyItem(), m_root_item, SessionItem::registerTag(), SessionItem::setDefaultTag(), and SessionItem::setModel().

Referenced by SessionModel(), and clear().

Here is the call graph for this function:

◆ data()

QVariant SessionModel::data ( const QModelIndex &  index,
int  role 
) const
virtual

Definition at line 70 of file SessionModel.cpp.

71 {
72  if (!m_root_item || !index.isValid() || index.column() < 0
73  || index.column() >= columnCount(QModelIndex())) {
74  return QVariant();
75  }
76  if (SessionItem* item = itemForIndex(index)) {
77  if (role == Qt::DisplayRole || role == Qt::EditRole) {
78  if (index.column() == SessionFlags::ITEM_VALUE)
79  return item->value();
80  if (index.column() == SessionFlags::ITEM_NAME)
81  return item->itemName();
82  } else if (role == Qt::ToolTipRole) {
83  return SessionItemUtils::ToolTipRole(*item, index.column());
84 
85  } else if (role == Qt::ForegroundRole) {
87  } else if (role == Qt::DecorationRole && index.column() == SessionFlags::ITEM_VALUE) {
89  } else if (role == Qt::CheckStateRole && index.column() == SessionFlags::ITEM_VALUE) {
91  } else {
92  return item->roleProperty(role);
93  }
94  }
95  return QVariant();
96 }
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
virtual int columnCount(const QModelIndex &parent) const
QVariant DecorationRole(const SessionItem &item)
Returns tooltip for given item.
QVariant CheckStateRole(const SessionItem &item)
Returns check state for given item.
QVariant ToolTipRole(const SessionItem &item, int ncol=0)
Returns tooltip for given item.
QVariant ForegroundRole(const SessionItem &item)
Returns text color for given item.

References columnCount(), index(), SessionFlags::ITEM_NAME, SessionFlags::ITEM_VALUE, itemForIndex(), m_root_item, and SessionItemUtils::ToolTipRole().

Referenced by canDropMimeData(), RealDataTreeModel::data(), dropMimeData(), gui2::JobModel::updateSLDProfile(), and gui2::JobModel::updateSpecularData().

Here is the call graph for this function:

◆ dropMimeData()

bool SessionModel::dropMimeData ( const QMimeData *  data,
Qt::DropAction  action,
int  row,
int  column,
const QModelIndex &  parent 
)
virtual

Definition at line 227 of file SessionModel.cpp.

229 {
230  if (action == Qt::IgnoreAction)
231  return true;
232  if (action != Qt::MoveAction || column > 0 || !data
233  || !data->hasFormat(SessionXML::ItemMimeType))
234  return false;
235  if (!canDropMimeData(data, action, row, column, parent))
236  return false;
237  if (SessionItem* item = itemForIndex(parent)) {
238  QByteArray xml_data = qUncompress(data->data(SessionXML::ItemMimeType));
239  QXmlStreamReader reader(xml_data);
240  if (row == -1)
241  row = item->numberOfChildren();
242  beginInsertRows(parent, row, row);
243  // this code block is currently not in use. The row parameter of the reader is removed
244  // SessionReader::readItems(&reader, item, row);
245  endInsertRows();
246  return true;
247  }
248  return false;
249 }
virtual bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const

References canDropMimeData(), data(), itemForIndex(), SessionXML::ItemMimeType, and parent().

Here is the call graph for this function:

◆ flags()

Qt::ItemFlags SessionModel::flags ( const QModelIndex &  index) const
virtual

Definition at line 51 of file SessionModel.cpp.

52 {
53  Qt::ItemFlags result_flags = QAbstractItemModel::flags(index);
54  if (index.isValid()) {
55  result_flags |= Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled;
57  if (index.column() == SessionFlags::ITEM_VALUE && item->value().isValid()
58  && item->isEditable() && item->isEnabled())
59  result_flags |= Qt::ItemIsEditable;
60  QVector<QString> acceptable_child_items = acceptableDefaultItemTypes(index);
61  if (acceptable_child_items.contains(m_dragged_item_type)) {
62  result_flags |= Qt::ItemIsDropEnabled;
63  }
64  } else {
65  result_flags |= Qt::ItemIsDropEnabled;
66  }
67  return result_flags;
68 }
QVariant value() const
Get value.
bool isEnabled() const
bool isEditable() const
QString m_dragged_item_type
Definition: SessionModel.h:120

References acceptableDefaultItemTypes(), index(), SessionItem::isEditable(), SessionItem::isEnabled(), SessionFlags::ITEM_VALUE, itemForIndex(), m_dragged_item_type, and SessionItem::value().

Referenced by RealDataTreeModel::flags().

Here is the call graph for this function:

◆ getModelName()

QString SessionModel::getModelName ( ) const
inline

Definition at line 196 of file SessionModel.h.

197 {
198  return m_name;
199 }

References m_name.

Referenced by SessionXML::writeTo().

◆ getModelTag()

QString SessionModel::getModelTag ( ) const
inline

Definition at line 191 of file SessionModel.h.

192 {
193  return m_model_tag;
194 }

References m_model_tag.

Referenced by ModelTreeView::ModelTreeView(), SessionXML::readItems(), and SessionXML::writeTo().

◆ headerData()

QVariant SessionModel::headerData ( int  section,
Qt::Orientation  orientation,
int  role 
) const
virtual

Definition at line 98 of file SessionModel.cpp.

99 {
100  if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
101  switch (section) {
103  return "Name";
105  return "Value";
106  }
107  }
108  return QVariant();
109 }

References SessionFlags::ITEM_NAME, and SessionFlags::ITEM_VALUE.

◆ index()

QModelIndex SessionModel::index ( int  row,
int  column,
const QModelIndex &  parent 
) const
virtual

Definition at line 126 of file SessionModel.cpp.

127 {
128  if (!m_root_item || row < 0 || column < 0 || column >= columnCount(QModelIndex())
129  || (parent.isValid() && parent.column() != 0))
130  return QModelIndex();
131 
132  SessionItem* parent_item = itemForIndex(parent);
133 
134  if (SessionItem* item = parent_item->childAt(row))
135  return createIndex(row, column, item);
136  return QModelIndex();
137 }
SessionItem * childAt(int row) const
Returns the child at the given row.

References SessionItem::childAt(), columnCount(), itemForIndex(), m_root_item, and parent().

Referenced by SampleViewAligner::alignSample(), JobModel::cancelJob(), MaterialModel::cloneMaterial(), MaskResultsPresenter::createMaskPresentation(), data(), MaskGraphicsScene::deleteViews(), DesignerScene::deleteViews(), flags(), JobModel::generateJobName(), SampleTreeWidget::getIndexAtColumnZero(), JobModel::getJobItemForIdentifier(), JobModel::getJobItemForIndex(), MaterialEditor::init_views(), ModelPath::isValidItem(), itemForIndex(), MaterialModel::materialFromIndex(), ModelMapper::onBeginRemoveRows(), MaterialPropertyController::onMaterialRowsAboutToBeRemoved(), ModelMapper::onRowRemoved(), MaskGraphicsScene::onRowsAboutToBeRemoved(), RealSpaceCanvas::onRowsAboutToBeRemoved(), DesignerScene::onRowsAboutToBeRemoved(), ItemStackWidget::onRowsAboutToBeRemoved(), ModelMapper::onRowsInserted(), removeItem(), JobModel::removeJob(), JobModel::runJob(), ItemSelectorWidget::selectLast(), setData(), MaskGraphicsScene::setItemName(), MaskGraphicsScene::setZValues(), ItemSelectorWidget::showEvent(), topItems(), JobQueueData::updateGlobalProgress(), InstrumentViewActions::updateSelection(), SampleViewAligner::updateViews(), MaskGraphicsScene::updateViews(), and DesignerScene::updateViews().

Here is the call graph for this function:

◆ indexOfItem()

QModelIndex SessionModel::indexOfItem ( SessionItem item) const

Definition at line 251 of file SessionModel.cpp.

252 {
253  if (!item || item == m_root_item || !item->parent())
254  return QModelIndex();
255  SessionItem* parent_item = item->parent();
256  int row = parent_item->rowOfChild(item);
257  return createIndex(row, 0, item);
258 }
int rowOfChild(SessionItem *child) const
Returns row index of given child.
SessionItem * parent() const
Returns parent of this item.
Definition: SessionItem.cpp:73

References m_root_item, SessionItem::parent(), and SessionItem::rowOfChild().

Referenced by SampleTreeWidget::addItem(), SampleViewAligner::alignSample(), MaskGraphicsScene::cancelCurrentDrawing(), MaskEditorActions::changeMaskStackingOrder(), RealDataTreeModel::data(), SessionItem::emitDataChanged(), RealDataTreeModel::flags(), SessionItem::index(), DetectorMaskDelegate::initMaskEditorContext(), MaskGraphicsScene::onSceneSelectionChanged(), DesignerScene::onSceneSelectionChanged(), MaskGraphicsScene::onSessionSelectionChanged(), DesignerScene::onSessionSelectionChanged(), MaskGraphicsScene::processLineItem(), MaskGraphicsScene::processPolygonItem(), removeItem(), and MaterialEditor::setInitialMaterialProperty().

Here is the call graph for this function:

◆ initFrom()

void SessionModel::initFrom ( SessionModel model,
SessionItem parent 
)
virtual

Definition at line 443 of file SessionModel.cpp.

444 {
445  QByteArray byte_array;
446  QXmlStreamWriter writer(&byte_array);
447  writer.setAutoFormatting(true);
448 
449  model->writeTo(&writer);
450 
451  QXmlStreamReader reader(byte_array);
452 
453  while (!reader.atEnd()) {
454  reader.readNext();
455  if (reader.isStartElement())
456  readFrom(&reader);
457  }
458  modelLoaded();
459 }
void modelLoaded()
virtual void writeTo(QXmlStreamWriter *writer, SessionItem *parent=0)
virtual void readFrom(QXmlStreamReader *reader, MessageService *messageService=0)

References modelLoaded(), readFrom(), and writeTo().

Referenced by MaterialModel::createCopy(), SampleModel::createCopy(), InstrumentModel::createCopy(), and MaterialEditorDialog::onOKButton().

Here is the call graph for this function:

◆ insertItem() [1/2]

template<typename T >
T * SessionModel::insertItem ( const QModelIndex &  parent,
int  row = -1,
QString  tag = "" 
)

Definition at line 130 of file SessionModel.h.

131 {
132  return insertItem<T>(itemForIndex(parent), row, tag);
133 }

References itemForIndex(), and parent().

Here is the call graph for this function:

◆ insertItem() [2/2]

template<typename T >
T * SessionModel::insertItem ( SessionItem parent = nullptr,
int  row = -1,
QString  tag = "" 
)

Definition at line 125 of file SessionModel.h.

126 {
127  return static_cast<T*>(insertNewItem(T().modelType(), parent, row, tag));
128 }
SessionItem * insertNewItem(QString model_type, SessionItem *parent_item=nullptr, int row=-1, QString tag="")

References insertNewItem(), SessionItem::modelType(), and parent().

Referenced by IntensityDataFFTPresenter::IntensityDataFFTPresenter(), FitComparison1DViewController::createDiffViewItem(), FitParameterHelper::createFitParameter(), DetectorMaskDelegate::createIntensityDataItem(), DetectorItem::createMaskContainer(), ParameterTreeUtils::createParameterTree(), DesignerScene::dropEvent(), TestComponentView::init_source(), InstrumentViewActions::initAddInstrumentMenu(), RealDataItem::initDataItem(), JobModelFunctions::initDataView(), TestComponentView::onAddItemRequest(), ProjectionsEditorCanvas::onEnteringColorMap(), ParameterTreeUtils::parameterDictionary(), ParameterTreeUtils::parameterNameToLinkedItem(), GUIObjectBuilder::populateDocumentModel(), ParameterTreeUtils::populateParameterContainer(), MaskGraphicsScene::processHorizontalLineItem(), MaskGraphicsScene::processMaskAllItem(), MaskGraphicsScene::processPolygonItem(), MaskGraphicsScene::processVerticalLineItem(), ApplicationModels::resetModels(), JobModelFunctions::setupJobItemOutput(), JobModelFunctions::setupJobItemSampleData(), TestView::test_MinimizerSettings(), TestView::test_specular_data_widget(), and GUIDomainSampleVisitor::visit().

Here is the call graph for this function:

◆ insertNewItem() [1/2]

SessionItem * SessionModel::insertNewItem ( QString  model_type,
const QModelIndex &  parent_item,
int  row = -1,
QString  tag = "" 
)

Definition at line 286 of file SessionModel.cpp.

288 {
289  return insertNewItem(model_type, itemForIndex(parent_item), row, tag);
290 }

References insertNewItem(), and itemForIndex().

Here is the call graph for this function:

◆ insertNewItem() [2/2]

SessionItem * SessionModel::insertNewItem ( QString  model_type,
SessionItem parent_item = nullptr,
int  row = -1,
QString  tag = "" 
)

Definition at line 260 of file SessionModel.cpp.

262 {
263  if (!parent_item)
264  parent_item = m_root_item;
265  if (row > parent_item->numberOfChildren())
266  return nullptr;
267  if (parent_item != m_root_item) {
268  if (tag.isEmpty())
269  tag = parent_item->defaultTag();
270 
271  if (!parent_item->sessionItemTags()->isValid(tag, model_type))
272  return nullptr;
273  }
274 
276 
277  if (!new_item)
278  throw GUIHelpers::Error("SessionModel::insertNewItem() -> Wrong model type " + model_type);
279 
280  if (!parent_item->insertItem(row, new_item, tag))
281  throw GUIHelpers::Error("SessionModel::insertNewItem -> Error. Can't insert item");
282 
283  return new_item;
284 }
bool isValid(const QString &tagName, const QString &modelType="") const
Returns true if there is a registered tag with such name.
SessionItemTags * sessionItemTags()
bool insertItem(int row, SessionItem *item, const QString &tag="")
Insert item into given tag into given row.
int numberOfChildren() const
Returns total number of children.
Definition: SessionItem.cpp:94
SessionItem * CreateItem(const QString &model_name, SessionItem *parent=nullptr)
create SessionItem of specific type and parent
Definition: ItemFactory.cpp:30

References ItemFactory::CreateItem(), SessionItem::defaultTag(), SessionItem::insertItem(), SessionItemTags::isValid(), m_root_item, SessionItem::numberOfChildren(), and SessionItem::sessionItemTags().

Referenced by SampleTreeWidget::addItem(), FitParameterHelper::addToFitParameter(), DesignerScene::dropEvent(), MultiLayerView::dropEvent(), GUIDomainSampleVisitor::InsertIParticle(), insertItem(), insertNewItem(), RealDataMaskWidget::maskContainer(), InstrumentViewActions::onCloneInstrument(), MaskGraphicsScene::processRectangleShapeItem(), and IntensityDataProjectionsWidget::projectionContainer().

Here is the call graph for this function:

◆ itemForIndex()

SessionItem * SessionModel::itemForIndex ( const QModelIndex &  index) const

Definition at line 347 of file SessionModel.cpp.

348 {
349  if (index.isValid())
350  if (SessionItem* item = static_cast<SessionItem*>(index.internalPointer()))
351  return item;
352 
353  return m_root_item;
354 }

References index(), and m_root_item.

Referenced by acceptableDefaultItemTypes(), MaskEditorActions::changeMaskStackingOrder(), SessionDecorationModel::createIcon(), MaskResultsPresenter::createMaskPresentation(), data(), DataProperties::dataItem(), DesignerScene::deleteSelectedItems(), MaskGraphicsScene::deleteViews(), DesignerScene::deleteViews(), dropMimeData(), flags(), JobModel::generateJobName(), ModelPath::getItemFromPath(), JobModel::getJobItemForIndex(), ParameterTuningModel::getParameterItem(), SampleViewAligner::getViewForIndex(), index(), insertItem(), insertNewItem(), MaskEditorActions::isSendToBackPossible(), ModelPath::isValidItem(), ParameterItem::linkedItem(), MaterialModel::materialFromIndex(), mimeData(), ModelMapper::onBeginRemoveRows(), InstrumentViewActions::onCloneInstrument(), PySampleWidget::onDataChanged(), RealSpaceCanvas::onDataChanged(), ComponentFlatView::onDataChanged(), ModelMapper::onDataChanged(), MaterialPropertyController::onMaterialDataChanged(), MaterialPropertyController::onMaterialRowsAboutToBeRemoved(), ModelMapper::onRowRemoved(), ItemStackWidget::onRowsAboutToBeRemoved(), ModelMapper::onRowsInserted(), ItemSelectorWidget::onSelectionChanged(), MaskEditorPropertyPanel::onSelectionChanged(), MaterialEditor::onSelectionChanged(), TestComponentView::onSelectionChanged(), FitParameterProxyModel::onSourceDataChanged(), MaskEditorActions::onToggleMaskValueAction(), parent(), ComponentProxyStrategy::processSourceIndex(), MaterialPropertyController::relatedSampleItems(), removeRows(), rowCount(), setData(), MaskGraphicsScene::setItemName(), MaskEditor::setMaskContext(), MaskGraphicsScene::setMaskContext(), MaskEditorPropertyPanel::setPanelHidden(), MaskGraphicsScene::setZValues(), ComponentProxyModel::sourceDataChanged(), SessionDecorationModel::textColor(), topItems(), MaskGraphicsScene::updateScene(), RealSpaceCanvas::updateScene(), MaskGraphicsScene::updateViews(), and DesignerScene::updateViews().

Here is the call graph for this function:

◆ load()

void SessionModel::load ( const QString &  filename = "")

Definition at line 316 of file SessionModel.cpp.

317 {
318  beginResetModel();
319  QFile file(filename);
320  if (!file.open(QIODevice::ReadOnly))
321  throw GUIHelpers::Error(file.errorString());
322  clear();
324  QXmlStreamReader reader(&file);
326  if (reader.hasError())
327  throw GUIHelpers::Error(reader.errorString());
328  endResetModel();
329 }
virtual void clear()
std::string filename(const std::string &path)
Returns path without directory part ("Foo/Bar/Doz.int.gz" -> "Doz.int.gz")

References clear(), ItemFactory::CreateEmptyItem(), FileSystemUtils::filename(), m_root_item, and SessionXML::readItems().

Here is the call graph for this function:

◆ mimeData()

QMimeData * SessionModel::mimeData ( const QModelIndexList &  indexes) const
virtual

Definition at line 183 of file SessionModel.cpp.

184 {
185  if (indices.count() != 2)
186  return 0;
187 
188  if (SessionItem* item = itemForIndex(indices.at(0))) {
189  QMimeData* mime_data = new QMimeData;
190  QByteArray xml_data;
191  QXmlStreamWriter writer(&xml_data);
192  SessionXML::writeItemAndChildItems(&writer, item);
193  mime_data->setData(SessionXML::ItemMimeType, qCompress(xml_data, MaxCompression));
194  return mime_data;
195  }
196  return 0;
197 }

References itemForIndex(), SessionXML::ItemMimeType, and SessionXML::writeItemAndChildItems().

Here is the call graph for this function:

◆ mimeTypes()

QStringList SessionModel::mimeTypes ( ) const
virtual

Definition at line 178 of file SessionModel.cpp.

179 {
180  return QStringList() << SessionXML::ItemMimeType;
181 }

References SessionXML::ItemMimeType.

◆ modelLoaded

void SessionModel::modelLoaded ( )
signal

◆ moveItem()

SessionItem * SessionModel::moveItem ( SessionItem item,
SessionItem new_parent = 0,
int  row = -1,
const QString &  tag = "" 
)

Move given parameterized item to the new_parent at given row.

If new_parent is not defined, use root_item as a new parent.

Definition at line 384 of file SessionModel.cpp.

386 {
387  if (!new_parent)
388  new_parent = m_root_item;
389 
390  const QString tagName = tag.isEmpty() ? new_parent->defaultTag() : tag;
391 
392  if (!new_parent->sessionItemTags()->isValid(tagName, item->modelType()))
393  return 0;
394 
395  if (item->parent() == new_parent) {
396  // take care of indexes when moving item within same parent
397  int previousIndex = item->parent()->getItems(tagName).indexOf(item);
398  if (row == previousIndex)
399  return item;
400  else if (previousIndex >= 0 && row > previousIndex)
401  row--;
402  }
403 
404  if (new_parent->sessionItemTags()->maximumReached(tagName)) {
405  SessionItem* prev = new_parent->takeItem(0, tagName);
406  m_root_item->insertItem(-1, prev);
407  }
408 
409  // removing item from previous parent and inserting to the new one
410  item->parent()->takeRow(item->parent()->rowOfChild(item));
411  new_parent->insertItem(row, item, tagName);
412 
413  return item;
414 }
bool maximumReached(const QString &tagName) const
SessionItem * takeRow(int row)
Removes row from item and returns the item.
SessionItem * takeItem(int row, const QString &tag)
Remove item from given row from given tag.
QString modelType() const
Get model type.

References SessionItem::defaultTag(), SessionItem::getItems(), SessionItem::insertItem(), SessionItemTags::isValid(), m_root_item, SessionItemTags::maximumReached(), SessionItem::modelType(), SessionItem::parent(), SessionItem::rowOfChild(), SessionItem::sessionItemTags(), SessionItem::takeItem(), and SessionItem::takeRow().

Referenced by MaskEditorActions::changeMaskStackingOrder(), ILayerView::mouseReleaseEvent(), DesignerScene::onEstablishedConnection(), and DesignerScene::removeConnection().

Here is the call graph for this function:

◆ nonXMLItems()

QVector< SessionItem * > SessionModel::nonXMLItems ( ) const
virtual

Reimplemented in RealDataModel, JobModel, and InstrumentModel.

Definition at line 466 of file SessionModel.cpp.

467 {
468  return QVector<SessionItem*>();
469 }

◆ parent()

QModelIndex SessionModel::parent ( const QModelIndex &  child) const
virtual

Definition at line 139 of file SessionModel.cpp.

140 {
141  if (!child.isValid())
142  return QModelIndex();
143 
144  if (SessionItem* child_item = itemForIndex(child)) {
145  if (SessionItem* parent_item = child_item->parent()) {
146  if (parent_item == m_root_item)
147  return QModelIndex();
148 
149  return createIndex(ParentRow(*parent_item), 0, parent_item);
150  }
151  }
152  return QModelIndex();
153 }
int ParentRow(const SessionItem &item)
Returns the index of the given item within its parent. Returns -1 when no parent is set.

References itemForIndex(), m_root_item, SessionItem::parent(), and SessionItemUtils::ParentRow().

Referenced by acceptableDefaultItemTypes(), canDropMimeData(), columnCount(), createCopy(), MaterialModel::createCopy(), SampleModel::createCopy(), InstrumentModel::createCopy(), SampleTreeWidget::deleteItem(), dropMimeData(), SampleTreeWidget::getIndexAtColumnZero(), index(), insertItem(), InstrumentModel::onRowsChange(), RealDataModel::onRowsChange(), removeRows(), rowCount(), and writeTo().

Here is the call graph for this function:

◆ readFrom()

void SessionModel::readFrom ( QXmlStreamReader *  reader,
MessageService messageService = 0 
)
virtual

Reimplemented in RealDataModel, JobModel, and InstrumentModel.

Definition at line 356 of file SessionModel.cpp.

357 {
358  ASSERT(reader);
359 
360  if (reader->name() != m_model_tag)
361  throw GUIHelpers::Error("SessionModel::readFrom() -> Format error in p1");
362 
363  beginResetModel();
364  clear();
365 
366  m_name = reader->attributes().value(SessionXML::ModelNameAttribute).toString();
367 
368  SessionXML::readItems(reader, m_root_item, QString(), messageService);
369  if (reader->hasError())
370  throw GUIHelpers::Error(reader->errorString());
371  endResetModel();
372 }
#define ASSERT(condition)
Definition: Assert.h:31
const QString ModelNameAttribute("Name")

References ASSERT, clear(), m_model_tag, m_name, m_root_item, SessionXML::ModelNameAttribute(), and SessionXML::readItems().

Referenced by initFrom(), InstrumentModel::readFrom(), JobModel::readFrom(), and RealDataModel::readFrom().

Here is the call graph for this function:

◆ removeItem()

void SessionModel::removeItem ( SessionItem item)

Definition at line 292 of file SessionModel.cpp.

293 {
294  QModelIndex index = indexOfItem(item);
295  if (index.isValid())
296  removeRows(index.row(), 1, QModelIndex());
297 }
virtual bool removeRows(int row, int count, const QModelIndex &parent)
QModelIndex indexOfItem(SessionItem *item) const

References index(), indexOfItem(), and removeRows().

Referenced by RealDataTreeModel::removeItem().

Here is the call graph for this function:

◆ removeRows()

bool SessionModel::removeRows ( int  row,
int  count,
const QModelIndex &  parent 
)
virtual

Definition at line 168 of file SessionModel.cpp.

169 {
170  if (!m_root_item)
171  return false;
172  SessionItem* item = parent.isValid() ? itemForIndex(parent) : m_root_item;
173  for (int i = 0; i < count; ++i)
174  delete item->takeRow(row);
175  return true;
176 }

References itemForIndex(), m_root_item, parent(), and SessionItem::takeRow().

Referenced by MaskGraphicsScene::cancelCurrentDrawing(), SampleTreeWidget::deleteItem(), DesignerScene::deleteSelectedItems(), ProjectionsEditorActions::onDeleteAction(), MaskEditorActions::onDeleteMaskAction(), InstrumentViewActions::onRemoveInstrument(), MaterialEditorToolBar::onRemoveMaterialAction(), removeItem(), and JobModel::removeJob().

Here is the call graph for this function:

◆ rootItem()

SessionItem * SessionModel::rootItem ( ) const

Definition at line 461 of file SessionModel.cpp.

462 {
463  return m_root_item;
464 }

References m_root_item.

Referenced by ModelPath::getIndexFromPath(), and ModelMapper::nestlingDepth().

◆ rowCount()

int SessionModel::rowCount ( const QModelIndex &  parent) const
virtual

◆ save()

void SessionModel::save ( const QString &  filename = "")

Definition at line 331 of file SessionModel.cpp.

332 {
333  QFile file(filename);
334  if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
335  throw GUIHelpers::Error(file.errorString());
336 
337  QXmlStreamWriter writer(&file);
338  writer.setAutoFormatting(true);
339  writer.writeStartDocument();
340  writer.writeStartElement("BornAgain");
341  writer.writeAttribute("Version", GUIHelpers::getBornAgainVersionString());
343  writer.writeEndElement(); // BornAgain
344  writer.writeEndDocument();
345 }
QString getBornAgainVersionString()
Definition: GUIHelpers.cpp:130

References FileSystemUtils::filename(), GUIHelpers::getBornAgainVersionString(), m_root_item, and SessionXML::writeItemAndChildItems().

Here is the call graph for this function:

◆ setData()

bool SessionModel::setData ( const QModelIndex &  index,
const QVariant &  value,
int  role 
)
virtual

Definition at line 155 of file SessionModel.cpp.

156 {
157  if (!index.isValid())
158  return false;
159 
160  QModelIndex dataIndex = index;
161  if (SessionItem* item = itemForIndex(dataIndex))
162  if (item->setRoleProperty(role, value))
163  return true;
164 
165  return false;
166 }

References index(), and itemForIndex().

Here is the call graph for this function:

◆ setDraggedItemType()

void SessionModel::setDraggedItemType ( const QString &  type)
inline

Definition at line 201 of file SessionModel.h.

202 {
203  m_dragged_item_type = type;
204 }

References m_dragged_item_type.

Referenced by ItemTreeView::dragMoveEvent().

◆ setHeaderData()

bool SessionModel::setHeaderData ( int  ,
Qt::Orientation  ,
const QVariant &  ,
int  = Qt::EditRole 
)
inlinevirtual

Definition at line 176 of file SessionModel.h.

177 {
178  return false;
179 }

◆ setRootItem()

void SessionModel::setRootItem ( SessionItem root)
inlineprotected

Definition at line 112 of file SessionModel.h.

112 { m_root_item = root; }

References m_root_item.

◆ supportedDragActions()

Qt::DropActions SessionModel::supportedDragActions ( ) const
inlinevirtual

Definition at line 181 of file SessionModel.h.

182 {
183  return Qt::MoveAction;
184 }

◆ supportedDropActions()

Qt::DropActions SessionModel::supportedDropActions ( ) const
inlinevirtual

Definition at line 186 of file SessionModel.h.

187 {
188  return Qt::MoveAction;
189 }

◆ topItem()

template<typename T >
T * SessionModel::topItem

Returns first item in list of topItems.

Definition at line 141 of file SessionModel.h.

142 {
143  auto items = topItems<T>();
144  return items.isEmpty() ? nullptr : items.front();
145 }

◆ topItems() [1/2]

template<typename T >
QVector< T * > SessionModel::topItems

Definition at line 147 of file SessionModel.h.

148 {
149  QVector<T*> result;
150 
151  QModelIndex parentIndex;
152  for (int i_row = 0; i_row < rowCount(parentIndex); ++i_row) {
153  QModelIndex itemIndex = index(i_row, 0, parentIndex);
154  if (auto item = dynamic_cast<T*>(itemForIndex(itemIndex)))
155  result.push_back(item);
156  }
157 
158  return result;
159 }
virtual int rowCount(const QModelIndex &parent) const

References index(), SessionItem::item(), itemForIndex(), and rowCount().

Referenced by JobModel::clear(), MaterialItemUtils::defaultMaterialProperty(), PySampleWidget::generateCodeSnippet(), SimulationDataSelectorWidget::selectedInstrumentItem(), SimulationDataSelectorWidget::selectedMultiLayerItem(), and ModelUtils::topItemNames().

Here is the call graph for this function:

◆ topItems() [2/2]

template<typename T >
QVector< T * > SessionModel::topItems ( std::function< bool(const T &)>  accept) const

Definition at line 161 of file SessionModel.h.

162 {
163  QVector<T*> result;
164 
165  QModelIndex parentIndex;
166  for (int i_row = 0; i_row < rowCount(parentIndex); ++i_row) {
167  QModelIndex itemIndex = index(i_row, 0, parentIndex);
168  if (auto item = dynamic_cast<T*>(itemForIndex(itemIndex)))
169  if (accept(*item))
170  result.push_back(item);
171  }
172 
173  return result;
174 }

References index(), SessionItem::item(), itemForIndex(), and rowCount().

Here is the call graph for this function:

◆ writeTo()

void SessionModel::writeTo ( QXmlStreamWriter *  writer,
SessionItem parent = 0 
)
virtual

Definition at line 374 of file SessionModel.cpp.

375 {
376  if (!parent)
378  SessionXML::writeTo(writer, parent);
379 }
void writeTo(QXmlStreamWriter *writer, SessionItem *parent)
Definition: SessionXML.cpp:39

References m_root_item, parent(), and SessionXML::writeTo().

Referenced by initFrom().

Here is the call graph for this function:

Friends And Related Function Documentation

◆ SessionItem

friend class SessionItem
friend

Definition at line 25 of file SessionModel.h.

Member Data Documentation

◆ m_dragged_item_type

QString SessionModel::m_dragged_item_type
private

Definition at line 120 of file SessionModel.h.

Referenced by flags(), and setDraggedItemType().

◆ m_model_tag

QString SessionModel::m_model_tag
private

model tag (SampleModel, InstrumentModel)

Definition at line 122 of file SessionModel.h.

Referenced by getModelTag(), and readFrom().

◆ m_name

QString SessionModel::m_name
private

model name

Definition at line 121 of file SessionModel.h.

Referenced by getModelName(), and readFrom().

◆ m_root_item


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