BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
sessionmodel.h
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // qt-mvvm: Model-view-view-model framework for large GUI applications
4 //
5 //! @file mvvm/model/mvvm/model/sessionmodel.h
6 //! @brief Defines class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Gennady Pospelov et al, Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #ifndef BORNAGAIN_MVVM_MODEL_MVVM_MODEL_SESSIONMODEL_H
16 #define BORNAGAIN_MVVM_MODEL_MVVM_MODEL_SESSIONMODEL_H
17 
18 #include "mvvm/core/types.h"
19 #include "mvvm/core/variant.h"
21 #include "mvvm/model/sessionitem.h"
22 #include "mvvm/model/tagrow.h"
23 #include "mvvm/model_export.h"
24 #include <memory>
25 
26 namespace ModelView {
27 
28 class SessionItem;
29 class ItemCatalogue;
30 class ItemPool;
31 class ModelMapper;
32 class ItemFactoryInterface;
33 class UndoStackInterface;
34 
35 //! Main class to hold hierarchy of SessionItem objects.
36 
37 class MVVM_MODEL_EXPORT SessionModel {
38 public:
39  explicit SessionModel(std::string model_type = {}, std::shared_ptr<ItemPool> pool = {});
40  virtual ~SessionModel();
41  SessionModel(const SessionModel& other) = delete;
42  SessionModel& operator=(const SessionModel& other) = delete;
43 
44  // Methods to manipulate data and items.
45 
46  SessionItem* insertNewItem(const model_type& modelType, SessionItem* parent = nullptr,
47  const TagRow& tagrow = {});
48 
49  template <typename T> T* insertItem(SessionItem* parent = nullptr, const TagRow& tagrow = {});
50 
51  void removeItem(SessionItem* parent, const TagRow& tagrow);
52 
53  void moveItem(SessionItem* item, SessionItem* new_parent, const TagRow& tagrow);
54 
55  SessionItem* copyItem(const SessionItem* item, SessionItem* parent, const TagRow& tagrow = {});
56 
57  Variant data(SessionItem* item, int role) const;
58 
59  bool setData(SessionItem* item, const Variant& value, int role);
60 
61  // Various getters.
62 
63  std::string modelType() const;
64 
65  SessionItem* rootItem() const;
66 
67  ModelMapper* mapper();
68 
69  UndoStackInterface* undoStack() const;
70 
71  const ItemFactoryInterface* factory() const;
72 
73  SessionItem* findItem(const identifier_type& id);
74 
75  template <typename T = SessionItem> std::vector<T*> topItems() const;
76 
77  template <typename T = SessionItem> T* topItem() const;
78 
79  // Methods to steer global behaviour.
80 
81  void setItemCatalogue(std::unique_ptr<ItemCatalogue> catalogue);
82 
83  void setUndoRedoEnabled(bool value);
84 
85  void clear(std::function<void(SessionItem*)> callback = {});
86 
87  template <typename T> void registerItem(const std::string& label = {});
88 
89 private:
90  friend class SessionItem;
91  void registerInPool(SessionItem* item);
92  void unregisterFromPool(SessionItem* item);
93  SessionItem* intern_insert(const item_factory_func_t& func, SessionItem* parent,
94  const TagRow& tagrow);
95  void intern_register(const model_type& modelType, const item_factory_func_t& func,
96  const std::string& label);
97 
98  struct SessionModelImpl;
99  std::unique_ptr<SessionModelImpl> p_impl;
100 };
101 
102 //! Inserts item into given parent under given tagrow.
103 
104 template <typename T> T* SessionModel::insertItem(SessionItem* parent, const TagRow& tagrow)
105 {
106  return static_cast<T*>(intern_insert(ItemFactoryFunction<T>(), parent, tagrow));
107 }
108 
109 //! Returns top items of the given type.
110 //! The top item is an item that is a child of an invisible root item.
111 
112 template <typename T> std::vector<T*> SessionModel::topItems() const
113 {
114  std::vector<T*> result;
115  for (auto child : rootItem()->children()) {
116  if (auto item = dynamic_cast<T*>(child))
117  result.push_back(item);
118  }
119 
120  return result;
121 }
122 
123 //! Returns top item of the given type. If more than one item exists, return the first one.
124 //! The top item is an item that is a child of an invisible root item.
125 
126 template <typename T> T* SessionModel::topItem() const
127 {
128  auto items = topItems<T>();
129  return items.empty() ? nullptr : items.front();
130 }
131 
132 //! Register used defined item to use with the model. It will become possible to undo/redo
133 //! operations with this item, as well as serialize it to/from JSON.
134 
135 template <typename T> void SessionModel::registerItem(const std::string& label)
136 {
137  intern_register(T().modelType(), ItemFactoryFunction<T>(), label);
138 }
139 
140 } // namespace ModelView
141 
142 #endif // BORNAGAIN_MVVM_MODEL_MVVM_MODEL_SESSIONMODEL_H
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
Main class to hold hierarchy of SessionItem objects.
Definition: sessionmodel.h:37
SessionModel(const SessionModel &other)=delete
T * topItem() const
Returns top item of the given type.
Definition: sessionmodel.h:126
std::vector< T * > topItems() const
Returns top items of the given type.
Definition: sessionmodel.h:112
std::string modelType() const
Returns model type.
SessionModel & operator=(const SessionModel &other)=delete
void intern_register(const model_type &modelType, const item_factory_func_t &func, const std::string &label)
std::unique_ptr< SessionModelImpl > p_impl
Definition: sessionmodel.h:98
SessionItem * intern_insert(const item_factory_func_t &func, SessionItem *parent, const TagRow &tagrow)
Insert new item into given parent using factory function provided.
SessionItem * rootItem() const
Returns root item of the model.
void registerItem(const std::string &label={})
Register used defined item to use with the model.
Definition: sessionmodel.h:135
T * insertItem(SessionItem *parent=nullptr, const TagRow &tagrow={})
Inserts item into given parent under given tagrow.
Definition: sessionmodel.h:104
Aggregate to hold (tag, row) information for SessionModel.
Definition: tagrow.h:25
SessionItem * parent() const
Returns parent of this item.
Definition: SessionItem.cpp:73
T * item(const QString &tag) const
Definition: SessionItem.h:151
QString modelType() const
Get model type.
Defines class CLASS?
materialitems.h Collection of materials to populate MaterialModel.
std::function< std::unique_ptr< SessionItem >()> item_factory_func_t
Definition for item factory funciton.
std::string identifier_type
Definition: types.h:22
std::string model_type
Definition: types.h:23
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
QVariant Variant
Definition: variant.h:23