BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
sessionitem.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/sessionitem.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_SESSIONITEM_H
16 #define BORNAGAIN_MVVM_MODEL_MVVM_MODEL_SESSIONITEM_H
17 
18 #include "mvvm/core/variant.h"
20 #include "mvvm/model/mvvm_types.h"
21 #include "mvvm/model/tagrow.h"
22 #include "mvvm/model_export.h"
23 #include <memory>
24 #include <stdexcept>
25 #include <vector>
26 
27 namespace ModelView {
28 
29 class SessionModel;
30 class TagInfo;
31 class ItemMapper;
32 class SessionItemData;
33 class SessionItemTags;
34 
35 //! The main object representing an editable/displayable/serializable entity. Serves as a
36 //! construction element (node) of SessionModel to represent all the data of GUI application.
37 
38 class MVVM_MODEL_EXPORT SessionItem {
39 public:
40  explicit SessionItem(model_type modelType = Constants::BaseType);
41  virtual ~SessionItem();
42  SessionItem(const SessionItem&) = delete;
43  SessionItem& operator=(const SessionItem&) = delete;
44 
45  // basic item properties
46 
47  model_type modelType() const;
48 
49  std::string identifier() const;
50 
51  virtual SessionItem* setDisplayName(const std::string& name);
52  virtual std::string displayName() const;
53 
54  SessionModel* model() const;
55 
56  SessionItem* parent() const;
57 
58  TagRow tagRow() const;
59 
60  // methods to deal with item data
61 
62  bool hasData(int role = ItemDataRole::DATA) const;
63 
64  template <typename T> T data(int role = ItemDataRole::DATA) const;
65 
66  template <typename T>
67  bool setData(const T& value, int role = ItemDataRole::DATA, bool direct = false);
68 
69  SessionItemData* itemData();
70  const SessionItemData* itemData() const;
71 
72  // children access
73 
74  int childrenCount() const;
75 
76  std::vector<SessionItem*> children() const;
77 
78  int itemCount(const std::string& tag) const;
79 
80  SessionItem* getItem(const std::string& tag, int row = 0) const;
81 
82  std::vector<SessionItem*> getItems(const std::string& tag) const;
83 
84  template <typename T> T* item(const std::string& tag) const;
85  template <typename T = SessionItem> std::vector<T*> items(const std::string& tag) const;
86 
87  TagRow tagRowOfItem(const SessionItem* item) const;
88 
89  void registerTag(const TagInfo& tagInfo, bool set_as_default = false);
90 
91  SessionItemTags* itemTags();
92  const SessionItemTags* itemTags() const;
93 
94  // item manipulation
95 
96  bool insertItem(SessionItem* item, const TagRow& tagrow);
97 
98  SessionItem* takeItem(const TagRow& tagrow);
99 
100  // more convenience methods
101 
102  bool isEditable() const;
103  SessionItem* setEditable(bool value);
104 
105  bool isEnabled() const;
106  SessionItem* setEnabled(bool value);
107 
108  std::string toolTip() const;
109  SessionItem* setToolTip(const std::string& tooltip);
110 
111  std::string editorType() const;
112  SessionItem* setEditorType(const std::string& editor_type);
113 
114  template <typename T> T property(const std::string& tag) const;
115  template <typename T> void setProperty(const std::string& tag, const T& value);
116  void setProperty(const std::string& tag, const char* value);
117 
118  ItemMapper* mapper();
119 
120 private:
121  friend class SessionModel;
122  friend class JsonItemConverter;
123  virtual void activate() {}
124  bool set_data_internal(const Variant& value, int role, bool direct);
125  Variant data_internal(int role) const;
126  void setParent(SessionItem* parent);
127  void setModel(SessionModel* model);
128  void setAppearanceFlag(int flag, bool value);
129 
130  void setDataAndTags(std::unique_ptr<SessionItemData> data,
131  std::unique_ptr<SessionItemTags> tags);
132 
133  struct SessionItemImpl;
134  std::unique_ptr<SessionItemImpl> p_impl;
135 };
136 
137 //! Sets data for a given role. When extra parameter `direct` is false (default case), will act
138 //! through the model to register command in undo/redo framework (if enabled) and so allow later
139 //! undo.
140 
141 template <typename T> inline bool SessionItem::setData(const T& value, int role, bool direct)
142 {
143  return set_data_internal(Variant::fromValue(value), role, direct);
144 }
145 
146 //! Returns data of given type T for given role.
147 
148 template <typename T> inline T SessionItem::data(int role) const
149 {
150  return data_internal(role).value<T>();
151 }
152 
153 //! Returns first item under given tag casted to a specified type.
154 //! Returns nullptr, if item doesn't exist. If item exists but can't be casted will throw.
155 
156 template <typename T> inline T* SessionItem::item(const std::string& tag) const
157 {
158  if (auto item = getItem(tag); item) {
159  T* tag_item = dynamic_cast<T*>(item);
160  if (!tag_item)
161  throw std::runtime_error("Can't cast an item to given type");
162  return tag_item;
163  }
164  return nullptr;
165 }
166 
167 //! Returns all items under given tag casted to specific type.
168 
169 template <typename T> std::vector<T*> SessionItem::items(const std::string& tag) const
170 {
171  std::vector<T*> result;
172  for (auto item : getItems(tag))
173  if (auto casted = dynamic_cast<T*>(item); casted)
174  result.push_back(casted);
175  return result;
176 }
177 
178 //! Returns data stored in property item.
179 //! Property is single item registered under certain tag via CompoundItem::addProperty method.
180 
181 template <typename T> inline T SessionItem::property(const std::string& tag) const
182 {
183  return getItem(tag)->data<T>();
184 }
185 
186 //! Sets value to property item.
187 //! Property is single item registered under certain tag via CompoundItem::addProperty method, the
188 //! value will be assigned to it's data role.
189 
190 template <typename T> inline void SessionItem::setProperty(const std::string& tag, const T& value)
191 {
192  getItem(tag)->setData(value);
193 }
194 
195 //! Sets value to property item (specialized for special "const char *" case).
196 //! Property is single item registered under certain tag via CompoundItem::addProperty method, the
197 //! value will be assigned to it's data role.
198 
199 inline void SessionItem::setProperty(const std::string& tag, const char* value)
200 {
201  setProperty(tag, std::string(value));
202 }
203 
204 } // namespace ModelView
205 
206 #endif // BORNAGAIN_MVVM_MODEL_MVVM_MODEL_SESSIONITEM_H
Provides notifications on various changes for a specific item.
Definition: itemmapper.h:32
Converter between SessionItem and JSON object.
Handles data roles for SessionItem.
Collection of SessionItem's containers according to their tags.
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
std::unique_ptr< SessionItemImpl > p_impl
Definition: sessionitem.h:133
std::vector< SessionItem * > getItems(const std::string &tag) const
Returns all children stored at given tag.
SessionItem & operator=(const SessionItem &)=delete
SessionItem * getItem(const std::string &tag, int row=0) const
Returns item at given row of given tag.
std::vector< T * > items(const std::string &tag) const
Returns all items under given tag casted to specific type.
Definition: sessionitem.h:169
bool setData(const T &value, int role=ItemDataRole::DATA, bool direct=false)
Sets data for a given role.
Definition: sessionitem.h:141
virtual void activate()
Definition: sessionitem.h:123
bool set_data_internal(const Variant &value, int role, bool direct)
Sets the data for given role. Method invented to hide implementaiton details.
T data(int role=ItemDataRole::DATA) const
Returns data of given type T for given role.
Definition: sessionitem.h:148
SessionItem(const SessionItem &)=delete
T property(const std::string &tag) const
Returns data stored in property item.
Definition: sessionitem.h:181
T * item(const std::string &tag) const
Returns first item under given tag casted to a specified type.
Definition: sessionitem.h:156
Variant data_internal(int role) const
Returns data for given role.
void setProperty(const std::string &tag, const T &value)
Sets value to property item.
Definition: sessionitem.h:190
Main class to hold hierarchy of SessionItem objects.
Definition: sessionmodel.h:37
Holds info about single tag for SessionItem.
Definition: taginfo.h:28
Aggregate to hold (tag, row) information for SessionModel.
Definition: tagrow.h:25
Handles all data roles for SessionItem.
Holds all tag info for SessionItem.
Defines class CLASS?
Defines class CLASS?
const model_type BaseType
Definition: mvvm_types.h:45
const int DATA
main data role
Definition: mvvm_types.h:30
materialitems.h Collection of materials to populate MaterialModel.
std::string model_type
Definition: types.h:23
QString const & name(EShape k)
Definition: particles.cpp:21
Defines class CLASS?
Defines class CLASS?
QVariant Variant
Definition: variant.h:23