BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
modelutils.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/modelutils.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_MODELUTILS_H
16 #define BORNAGAIN_MVVM_MODEL_MVVM_MODEL_MODELUTILS_H
17 
19 #include "mvvm/model/itemutils.h"
20 #include "mvvm/model/sessionitem.h"
22 #include "mvvm/model_export.h"
23 #include <memory>
24 #include <vector>
25 
26 namespace ModelView {
27 
28 class Path;
29 
30 namespace Utils {
31 
32 //! Returns all top level items of given type.
33 
34 template <typename T = SessionItem> std::vector<T*> TopItems(const SessionModel* model)
35 {
36  std::vector<T*> result;
37  for (auto child : model->rootItem()->children()) {
38  if (auto item = dynamic_cast<T*>(child); item)
39  result.push_back(item);
40  }
41 
42  return result;
43 }
44 
45 //! Returns top level item of given type.
46 
47 template <typename T = SessionItem> T* TopItem(const SessionModel* model)
48 {
49  auto items = TopItems<T>(model);
50  return items.empty() ? nullptr : items.front();
51 }
52 
53 //! Returns all items in a tree of given type.
54 
55 template <typename T = SessionItem> std::vector<T*> FindItems(const SessionModel* model)
56 {
57  std::vector<T*> result;
58 
59  auto func = [&result](SessionItem* item) {
60  if (auto concrete = dynamic_cast<T*>(item); concrete)
61  result.push_back(concrete);
62  };
63 
64  iterate(model->rootItem(), func);
65 
66  return result;
67 }
68 
69 //! Constructs path to find given item. Item must belong to a model.
70 MVVM_MODEL_EXPORT Path PathFromItem(const SessionItem* item);
71 
72 //! Returns item found in the model following given Path.
73 MVVM_MODEL_EXPORT SessionItem* ItemFromPath(const SessionModel& moodel, const Path& path);
74 
75 //! Populate empty model with content of target model using provided converter.
76 //! Serves as auxiliary function for model copying and cloning.
77 MVVM_MODEL_EXPORT void PopulateEmptyModel(const JsonModelConverterInterface* converter,
78  const SessionModel& source, SessionModel& target);
79 
80 //! Creates full deep copy of given model. All item's ID will be generated.
81 template <typename T = SessionModel> std::unique_ptr<T> CreateCopy(const T& model)
82 {
83  auto result = std::make_unique<T>();
84  auto converter = CreateModelCopyConverter();
85  PopulateEmptyModel(converter.get(), model, *result.get());
86  return result;
87 }
88 
89 //! Creates exact clone of given model. All item's ID will be preserved.
90 template <typename T = SessionModel> std::unique_ptr<T> CreateClone(const T& model)
91 {
92  auto result = std::make_unique<T>();
93  auto converter = CreateModelCloneConverter();
94  PopulateEmptyModel(converter.get(), model, *result.get());
95  return result;
96 }
97 
98 //! Removes and deletes item from its model.
99 MVVM_MODEL_EXPORT void DeleteItemFromModel(SessionItem* item);
100 
101 //! Moves item up (decrements row of the item). Works on children belonging to single tag.
102 MVVM_MODEL_EXPORT void MoveUp(SessionItem* item);
103 
104 //! Moves item down (increments row of the item). Works on children belonging to single tag.
105 MVVM_MODEL_EXPORT void MoveDown(SessionItem* item);
106 
107 //! Undo last model operation. If not undo/redo enabled, will do nothing.
108 MVVM_MODEL_EXPORT void Undo(SessionModel& model);
109 
110 //! Redo model operation which was undone just before. If not undo/redo enabled, will do nothing.
111 MVVM_MODEL_EXPORT void Redo(SessionModel& model);
112 
113 //! Begin undo/redo macros with given name. Works only if item belongs to the model, and model has
114 //! undo/redo enabled. Otherwise, do nothing.
115 MVVM_MODEL_EXPORT void BeginMacros(const SessionItem* item, const std::string& macro_name);
116 
117 //! Finishes undo/redo macros. Works only if item belongs to the model, and model has undo/redo
118 //! enabled. Otherwise, do nothing.
119 MVVM_MODEL_EXPORT void EndMacros(const SessionItem* item);
120 
121 } // namespace Utils
122 } // namespace ModelView
123 
124 #endif // BORNAGAIN_MVVM_MODEL_MVVM_MODEL_MODELUTILS_H
Base class for all converters of SessionModel to/from json object.
Supports navigation through SessionModel.
Definition: path.h:35
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
std::vector< SessionItem * > children() const
Returns vector of children formed from all chidlren from all tags.
Main class to hold hierarchy of SessionItem objects.
Definition: sessionmodel.h:37
SessionItem * rootItem() const
Returns root item of the model.
Defines class CLASS?
Defines class CLASS?
std::unique_ptr< T > CreateCopy(const T &model)
Creates full deep copy of given model. All item's ID will be generated.
Definition: modelutils.h:81
MVVM_MODEL_EXPORT void EndMacros(const SessionItem *item)
Finishes undo/redo macros.
Definition: modelutils.cpp:100
MVVM_MODEL_EXPORT void MoveDown(SessionItem *item)
Moves item down (increments row of the item). Works on children belonging to single tag.
Definition: modelutils.cpp:71
MVVM_MODEL_EXPORT void BeginMacros(const SessionItem *item, const std::string &macro_name)
Begin undo/redo macros with given name.
Definition: modelutils.cpp:91
MVVM_MODEL_EXPORT void iterate(SessionItem *item, const std::function< void(SessionItem *)> &fun)
Iterates through item and all its children.
Definition: itemutils.cpp:24
MVVM_MODEL_EXPORT void MoveUp(SessionItem *item)
Moves item up (decrements row of the item). Works on children belonging to single tag.
Definition: modelutils.cpp:63
MVVM_MODEL_EXPORT SessionItem * ItemFromPath(const SessionModel &moodel, const Path &path)
Returns item found in the model following given Path.
Definition: modelutils.cpp:36
T * TopItem(const SessionModel *model)
Returns top level item of given type.
Definition: modelutils.h:47
std::unique_ptr< T > CreateClone(const T &model)
Creates exact clone of given model. All item's ID will be preserved.
Definition: modelutils.h:90
MVVM_MODEL_EXPORT void Undo(SessionModel &model)
Undo last model operation. If not undo/redo enabled, will do nothing.
Definition: modelutils.cpp:79
MVVM_MODEL_EXPORT Path PathFromItem(const SessionItem *item)
Constructs path to find given item. Item must belong to a model.
Definition: modelutils.cpp:22
MVVM_MODEL_EXPORT void Redo(SessionModel &model)
Redo model operation which was undone just before. If not undo/redo enabled, will do nothing.
Definition: modelutils.cpp:85
std::vector< T * > FindItems(const SessionModel *model)
Returns all items in a tree of given type.
Definition: modelutils.h:55
std::vector< T * > TopItems(const SessionModel *model)
Returns all top level items of given type.
Definition: modelutils.h:34
MVVM_MODEL_EXPORT void PopulateEmptyModel(const JsonModelConverterInterface *converter, const SessionModel &source, SessionModel &target)
Populate empty model with content of target model using provided converter.
Definition: modelutils.cpp:47
MVVM_MODEL_EXPORT void DeleteItemFromModel(SessionItem *item)
Removes and deletes item from its model.
Definition: modelutils.cpp:54
materialitems.h Collection of materials to populate MaterialModel.
MVVM_MODEL_EXPORT std::unique_ptr< JsonModelConverterInterface > CreateModelCopyConverter()
Creates a JSON model converter intended for model copying.
MVVM_MODEL_EXPORT std::unique_ptr< JsonModelConverterInterface > CreateModelCloneConverter()
Creates a JSON model converter intended for model cloning.
Defines class CLASS?
Defines class CLASS?