BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
sessionmodel.cpp
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.cpp
6 //! @brief Implements 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 
20 #include "mvvm/model/itemfactory.h"
21 #include "mvvm/model/itemmanager.h"
22 #include "mvvm/model/itempool.h"
23 #include "mvvm/model/itemutils.h"
24 #include "mvvm/model/sessionitem.h"
25 #include "mvvm/model/taginfo.h"
26 #include "mvvm/model/tagrow.h"
28 
29 using namespace ModelView;
30 
31 //! Pimpl class for SessionModel.
32 
34  SessionModel* m_self{nullptr};
35  std::string m_modelType;
36  std::unique_ptr<ItemManager> m_itemManager;
37  std::unique_ptr<CommandService> m_commands;
38  std::unique_ptr<ModelMapper> m_mapper;
39  std::unique_ptr<SessionItem> m_root_item;
40  SessionModelImpl(SessionModel* self, std::string modelType, std::shared_ptr<ItemPool> pool)
41  : m_self(self)
42  , m_modelType(std::move(modelType))
43  , m_itemManager(std::make_unique<ItemManager>())
44  , m_commands(std::make_unique<CommandService>(self))
45  , m_mapper(std::make_unique<ModelMapper>(self))
46  {
47  setItemPool(pool);
48  }
49 
50  void setItemPool(std::shared_ptr<ItemPool> pool)
51  {
52  m_itemManager->setItemPool(pool ? std::move(pool) : std::make_shared<ItemPool>());
53  }
54 
55  //! Creates root item.
57  {
58  m_root_item = m_itemManager->createRootItem();
59  m_root_item->setModel(m_self);
60  m_root_item->registerTag(TagInfo::universalTag("rootTag"), /*set_as_default*/ true);
61  }
62 };
63 
64 //! Main c-tor.
65 
66 SessionModel::SessionModel(std::string model_type, std::shared_ptr<ItemPool> pool)
67  : p_impl(std::make_unique<SessionModelImpl>(this, std::move(model_type), std::move(pool)))
68 
69 {
70  p_impl->createRootItem();
71 }
72 
74 {
75  // Explicitely call root item's destructor. It uses p_impl pointer during own descruction
76  // and we have to keep pimpl pointer intact. Without line below will crash on MacOS because
77  // of pecularities of MacOS libc++. See explanations here:
78  // http://ibob.github.io/blog/2019/11/07/dont-use-unique_ptr-for-pimpl/
79  p_impl->m_root_item.reset();
80 
81  p_impl->m_mapper->callOnModelDestroyed();
82 }
83 
84 //! Insert new item using item's modelType.
85 
87  const TagRow& tagrow)
88 {
89  // intentionally passing by value inside lambda
90  auto create_func = [this, modelType]() { return factory()->createItem(modelType); };
91  return intern_insert(create_func, parent, tagrow);
92 }
93 
94 //! Removes given row from parent.
95 
96 void SessionModel::removeItem(SessionItem* parent, const TagRow& tagrow)
97 {
98  p_impl->m_commands->removeItem(parent, tagrow);
99 }
100 
101 //! Move item from it's current parent to a new parent under given tag and row.
102 //! Old and new parents should belong to this model.
103 
104 void SessionModel::moveItem(SessionItem* item, SessionItem* new_parent, const TagRow& tagrow)
105 {
106  p_impl->m_commands->moveItem(item, new_parent, tagrow);
107 }
108 
109 //! Copy item and insert it in parent's tag and row. Item could belong to any model/parent.
110 
112  const TagRow& tagrow)
113 {
114  return p_impl->m_commands->copyItem(item, parent, tagrow);
115 }
116 
117 //! Returns the data for given item and role.
118 
119 Variant SessionModel::data(SessionItem* item, int role) const
120 {
121  return item->data<Variant>(role);
122 }
123 
124 //! Sets the data for given item.
125 
126 bool SessionModel::setData(SessionItem* item, const Variant& value, int role)
127 {
128  return p_impl->m_commands->setData(item, value, role);
129 }
130 
131 //! Returns model type.
132 
133 std::string SessionModel::modelType() const
134 {
135  return p_impl->m_modelType;
136 }
137 
138 //! Returns root item of the model.
139 
141 {
142  return p_impl->m_root_item.get();
143 }
144 
145 //! Returns model mapper. Can be used to subscribe to various model's signal.
146 
148 {
149  return p_impl->m_mapper.get();
150 }
151 
152 //! Returns command stack to perform undo/redo.
153 
155 {
156  return p_impl->m_commands->undoStack();
157 }
158 
159 //! Returns item factory which can generate all items supported by this model.
160 
162 {
163  return p_impl->m_itemManager->factory();
164 }
165 
166 //! Returns SessionItem for given identifier.
167 
169 {
170  return p_impl->m_itemManager->findItem(id);
171 }
172 
173 //! Sets brand new catalog of user-defined items. They become available for undo/redo and
174 //! serialization. Internally user catalog will be merged with the catalog of standard items.
175 
176 void SessionModel::setItemCatalogue(std::unique_ptr<ItemCatalogue> catalogue)
177 {
178  // adding standard items to the user catalogue
179  std::unique_ptr<ItemCatalogue> full_catalogue = std::move(catalogue);
180  full_catalogue->merge(*CreateStandardItemCatalogue());
181  p_impl->m_itemManager->setItemFactory(std::make_unique<ItemFactory>(std::move(full_catalogue)));
182 }
183 
184 //! Sets undo/redo either enabled or disabled. By default undo/redo is disabled.
185 
187 {
188  p_impl->m_commands->setUndoRedoEnabled(value);
189 }
190 
191 //! Removes all items from the model. If callback is provided, use it to rebuild content of root
192 //! item (used while restoring the model from serialized content).
193 
194 void SessionModel::clear(std::function<void(SessionItem*)> callback)
195 {
196  if (undoStack())
197  undoStack()->clear();
199  p_impl->createRootItem();
200  if (callback)
201  callback(rootItem());
203 }
204 
205 //! Registers item in pool. This will allow to find item pointer using its unique identifier.
206 
208 {
209  p_impl->m_itemManager->registerInPool(item);
210  item->activate(); // activates buisiness logic
211 }
212 
213 //! Unregister item from pool.
214 
216 {
217  p_impl->m_itemManager->unregisterFromPool(item);
218 }
219 
220 //! Insert new item into given parent using factory function provided.
221 
223  const TagRow& tagrow)
224 {
225  return p_impl->m_commands->insertNewItem(func, parent, tagrow);
226 }
227 
229  const std::string& label)
230 {
231  p_impl->m_itemManager->factory()->registerItem(modelType, func, label);
232 }
Provides undo/redo for all commands of SessionModel.
Interface class for all factories capable of producing SessionItem's.
virtual std::unique_ptr< SessionItem > createItem(const model_type &modelType) const =0
Manages item creation/registration for SessionModel.
Definition: itemmanager.h:30
Provides notifications on various SessionModel changes.
Definition: modelmapper.h:29
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
virtual void activate()
Definition: sessionitem.h:123
T data(int role=ItemDataRole::DATA) const
Returns data of given type T for given role.
Definition: sessionitem.h:148
Main class to hold hierarchy of SessionItem objects.
Definition: sessionmodel.h:37
bool setData(SessionItem *item, const Variant &value, int role)
Sets the data for given item.
void unregisterFromPool(SessionItem *item)
Unregister item from pool.
ModelMapper * mapper()
Returns model mapper. Can be used to subscribe to various model's signal.
std::string modelType() const
Returns model type.
SessionItem * insertNewItem(const model_type &modelType, SessionItem *parent=nullptr, const TagRow &tagrow={})
Insert new item using item's modelType.
void setUndoRedoEnabled(bool value)
Sets undo/redo either enabled or disabled. By default undo/redo is disabled.
void moveItem(SessionItem *item, SessionItem *new_parent, const TagRow &tagrow)
Move item from it's current parent to a new parent under given tag and row.
SessionItem * findItem(const identifier_type &id)
Returns SessionItem for given identifier.
SessionModel(std::string model_type={}, std::shared_ptr< ItemPool > pool={})
Main c-tor.
const ItemFactoryInterface * factory() const
Returns item factory which can generate all items supported by this model.
void registerInPool(SessionItem *item)
Registers item in pool. This will allow to find item pointer using its unique identifier.
SessionItem * copyItem(const SessionItem *item, SessionItem *parent, const TagRow &tagrow={})
Copy item and insert it in parent's tag and row. Item could belong to any model/parent.
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
void setItemCatalogue(std::unique_ptr< ItemCatalogue > catalogue)
Sets brand new catalog of user-defined items.
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.
UndoStackInterface * undoStack() const
Returns command stack to perform undo/redo.
Variant data(SessionItem *item, int role) const
Returns the data for given item and role.
void clear(std::function< void(SessionItem *)> callback={})
Removes all items from the model.
void removeItem(SessionItem *parent, const TagRow &tagrow)
Removes given row from parent.
static TagInfo universalTag(std::string name, std::vector< std::string > modelTypes={})
Constructs universal tag intended for unlimited amount of various items.
Definition: taginfo.cpp:34
Aggregate to hold (tag, row) information for SessionModel.
Definition: tagrow.h:25
Interface class for undo/redo stack.
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
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
MVVM_MODEL_EXPORT std::unique_ptr< ItemCatalogue > CreateStandardItemCatalogue()
Creates a catalog of items supported by SessionModel out-of-the-box.
std::string model_type
Definition: types.h:23
Definition: filesystem.h:81
Defines class CLASS?
Defines class CLASS?
Pimpl class for SessionModel.
void createRootItem()
Creates root item.
std::unique_ptr< ItemManager > m_itemManager
std::unique_ptr< SessionItem > m_root_item
std::unique_ptr< ModelMapper > m_mapper
std::unique_ptr< CommandService > m_commands
void setItemPool(std::shared_ptr< ItemPool > pool)
SessionModelImpl(SessionModel *self, std::string modelType, std::shared_ptr< ItemPool > pool)
Defines class CLASS?
Defines class CLASS?
QVariant Variant
Definition: variant.h:23