BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
SessionModel.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Model/Model/SessionModel.h
6 //! @brief Defines class SessionModel
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 
15 #ifndef BORNAGAIN_GUI_MODEL_MODEL_SESSIONMODEL_H
16 #define BORNAGAIN_GUI_MODEL_MODEL_SESSIONMODEL_H
17 
19 #include <QStringList>
20 #include <QVariant>
21 #include <QXmlStreamWriter> // used in every including file ?
22 #include <heinz/Complex.h>
23 #include <heinz/Vectors3D.h>
24 
25 class MessageService;
26 
27 //! Base class for a GUI data collection.
28 //! A collection is e.g. all real data (RealDataModel).
29 //! Every model is implementing a Qt item model, therefore
30 //! the "model" in the class name.
31 //! Each model is populated by objects derived from SessionItem.
32 //!
33 //! Purpose of a model is to
34 //! * Keep the root item of the contained data (which then keeps the tree of all
35 //! items in this model)
36 //! * adding/removing items
37 //! * iteration over contained items
38 //! * load/store the items in the project file as XML
39 //! * use Qt mechanisms for GUI representation in different views
40 //! * notification of data changes to any listener
41 
42 class SessionModel : public QAbstractItemModel {
43  Q_OBJECT
44  friend class SessionItem;
45 
46 public:
47  explicit SessionModel(QString model_tag, QObject* parent = nullptr);
48  ~SessionModel() override;
49  void createRootItem();
50 
51  // Begin overridden methods from QAbstractItemModel
52  Qt::ItemFlags flags(const QModelIndex& index) const override;
53  QVariant data(const QModelIndex& index, int role) const override;
54  QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
55  int rowCount(const QModelIndex& parent) const override;
56  int columnCount(const QModelIndex& parent) const override;
57  QModelIndex index(int row, int column, const QModelIndex& parent) const override;
58  QModelIndex parent(const QModelIndex& child) const override;
59 
60  bool setData(const QModelIndex& index, const QVariant& value, int role) override;
61  bool removeRows(int row, int count, const QModelIndex& parent) override;
62 
63  Qt::DropActions supportedDragActions() const override;
64  Qt::DropActions supportedDropActions() const override;
65  QStringList mimeTypes() const override;
66  QMimeData* mimeData(const QModelIndexList& indices) const override;
67  bool canDropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column,
68  const QModelIndex& parent) const override;
69  bool dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column,
70  const QModelIndex& parent) override;
71  // End overridden methods from QAbstractItemModel
72 
73  QModelIndex indexOfItem(SessionItem* item) const;
74  SessionItem* insertNewItem(QString model_type, SessionItem* parent_item = nullptr, int row = -1,
75  QString tag = "");
76 
77  // #migration Method is deprecated, index usage is discouraged.
78  SessionItem* insertNewItem(QString model_type, const QModelIndex& parent_item, int row = -1,
79  QString tag = "");
80 
81  template <typename T>
82  T* insertItem(SessionItem* parent = nullptr, int row = -1, QString tag = "");
83 
84  // #migration Method is deprecated, index usage is discouraged.
85  template <typename T>
86  T* insertItem(const QModelIndex& parent, int row = -1, QString tag = "");
87 
89 
90  QString getModelTag() const;
91 
92  QVector<QString> acceptableDefaultItemTypes(const QModelIndex& parent) const;
93 
94  virtual void clear();
95 
96  // Sets mimedata pointer of item being dragged
97  void setDraggedItemType(const QString& type);
98 
99  // Returns root item if index is not valid
100  SessionItem* itemForIndex(const QModelIndex& index) const;
101 
102  virtual void readFrom(QXmlStreamReader* reader, MessageService* messageService = nullptr);
103  virtual void writeTo(QXmlStreamWriter* writer);
104 
105  SessionItem* moveItem(SessionItem* item, SessionItem* new_parent = nullptr, int row = -1,
106  const QString& tag = "");
107 
108  template <typename T>
109  T* copyItem(const T* item_to_copy, SessionItem* new_parent = nullptr, const QString& tag = "");
110 
111  SessionItem* copy(const SessionItem* item_to_copy, SessionItem* new_parent = nullptr,
112  const QString& tag = "");
113 
114  //! Returns first item in list of topItems
115  template <typename T = SessionItem>
116  T* topItem() const;
117  template <typename T = SessionItem>
118  QVector<T*> topItems() const;
119  template <typename T = SessionItem>
120  QVector<T*> topItems(std::function<bool(const T&)> accept) const;
121 
122  virtual void initFrom(SessionModel* model, SessionItem* parent);
123  SessionItem* rootItem() const;
124 
125  virtual QVector<SessionItem*> nonXMLItems() const;
126 
127 protected:
128  void setRootItem(SessionItem* root) { m_root_item = root; }
129 
130 private:
133  QString m_model_tag;
134 };
135 
136 template <typename T>
137 T* SessionModel::insertItem(SessionItem* parent, int row, QString tag)
138 {
139  return dynamic_cast<T*>(insertNewItem(T().modelType(), parent, row, tag));
140 }
141 
142 template <typename T>
143 T* SessionModel::insertItem(const QModelIndex& parent, int row, QString tag)
144 {
145  return insertItem<T>(itemForIndex(parent), row, tag);
146 }
147 
148 template <typename T>
149 T* SessionModel::copyItem(const T* item_to_copy, SessionItem* new_parent, const QString& tag)
150 {
151  return static_cast<T*>(copy(item_to_copy, new_parent, tag));
152 }
153 
154 template <typename T>
156 {
157  auto items = topItems<T>();
158  return items.isEmpty() ? nullptr : items.front();
159 }
160 
161 template <typename T>
162 QVector<T*> SessionModel::topItems() const
163 {
164  QVector<T*> result;
165 
166  QModelIndex parentIndex;
167  for (int i_row = 0; i_row < rowCount(parentIndex); ++i_row) {
168  QModelIndex itemIndex = index(i_row, 0, parentIndex);
169  if (auto item = dynamic_cast<T*>(itemForIndex(itemIndex)))
170  result.push_back(item);
171  }
172 
173  return result;
174 }
175 
176 template <typename T>
177 QVector<T*> SessionModel::topItems(std::function<bool(const T&)> accept) const
178 {
179  QVector<T*> result;
180 
181  QModelIndex parentIndex;
182  for (int i_row = 0; i_row < rowCount(parentIndex); ++i_row) {
183  QModelIndex itemIndex = index(i_row, 0, parentIndex);
184  if (auto item = dynamic_cast<T*>(itemForIndex(itemIndex)))
185  if (accept(*item))
186  result.push_back(item);
187  }
188 
189  return result;
190 }
191 
192 inline Qt::DropActions SessionModel::supportedDragActions() const
193 {
194  return Qt::MoveAction;
195 }
196 
197 inline Qt::DropActions SessionModel::supportedDropActions() const
198 {
199  return Qt::MoveAction;
200 }
201 
202 inline QString SessionModel::getModelTag() const
203 {
204  return m_model_tag;
205 }
206 
207 inline void SessionModel::setDraggedItemType(const QString& type)
208 {
209  m_dragged_item_type = type;
210 }
211 
212 #endif // BORNAGAIN_GUI_MODEL_MODEL_SESSIONMODEL_H
Defines class SessionItem.
The service to collect messages from different senders.
Base class for a GUI data item.
Definition: SessionItem.h:204
QVariant value() const
Get value.
SessionModel * model() const
Returns model of this item.
Definition: SessionItem.cpp:60
T * item(const QString &tag) const
Definition: SessionItem.h:353
QVector< T * > items(const QString &tag="") const
Definition: SessionItem.h:361
QString modelType() const
Get model type.
Base class for a GUI data collection. A collection is e.g. all real data (RealDataModel)....
Definition: SessionModel.h:42
QVariant data(const QModelIndex &index, int role) const override
QModelIndex parent(const QModelIndex &child) const override
T * topItem() const
Returns first item in list of topItems.
Definition: SessionModel.h:155
virtual void clear()
bool setData(const QModelIndex &index, const QVariant &value, int role) override
SessionItem * moveItem(SessionItem *item, SessionItem *new_parent=nullptr, int row=-1, const QString &tag="")
Move given parameterized item to the new_parent at given row. If new_parent is not defined,...
SessionModel(QString model_tag, QObject *parent=nullptr)
QMimeData * mimeData(const QModelIndexList &indices) const override
SessionItem * itemForIndex(const QModelIndex &index) const
void setDraggedItemType(const QString &type)
Definition: SessionModel.h:207
T * insertItem(SessionItem *parent=nullptr, int row=-1, QString tag="")
Definition: SessionModel.h:137
Qt::ItemFlags flags(const QModelIndex &index) const override
virtual QVector< SessionItem * > nonXMLItems() const
QString m_dragged_item_type
Definition: SessionModel.h:132
QModelIndex index(int row, int column, const QModelIndex &parent) const override
QVector< T * > topItems() const
Definition: SessionModel.h:162
QStringList mimeTypes() const override
int columnCount(const QModelIndex &parent) const override
QString getModelTag() const
Definition: SessionModel.h:202
virtual void readFrom(QXmlStreamReader *reader, MessageService *messageService=nullptr)
void createRootItem()
Qt::DropActions supportedDragActions() const override
Definition: SessionModel.h:192
void removeItem(SessionItem *item)
virtual void writeTo(QXmlStreamWriter *writer)
SessionItem * insertNewItem(QString model_type, SessionItem *parent_item=nullptr, int row=-1, QString tag="")
bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const override
virtual void initFrom(SessionModel *model, SessionItem *parent)
QString m_model_tag
Definition: SessionModel.h:133
SessionItem * rootItem() const
QVector< QString > acceptableDefaultItemTypes(const QModelIndex &parent) const
bool removeRows(int row, int count, const QModelIndex &parent) override
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
T * copyItem(const T *item_to_copy, SessionItem *new_parent=nullptr, const QString &tag="")
Definition: SessionModel.h:149
Qt::DropActions supportedDropActions() const override
Definition: SessionModel.h:197
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
int rowCount(const QModelIndex &parent) const override
void setRootItem(SessionItem *root)
Definition: SessionModel.h:128
SessionItem * m_root_item
Definition: SessionModel.h:131
QModelIndex indexOfItem(SessionItem *item) const
SessionItem * copy(const SessionItem *item_to_copy, SessionItem *new_parent=nullptr, const QString &tag="")
Copy given item to the new_parent at given row. Item intended for copying can belong to another model...
~SessionModel() override