MeVisLab Toolbox Reference
mlStandardItemModel.h
Go to the documentation of this file.
1 /*************************************************************************************
2 **
3 ** Copyright 2011, MeVis Medical Solutions AG
4 **
5 ** The user may use this file in accordance with the license agreement provided with
6 ** the Software or, alternatively, in accordance with the terms contained in a
7 ** written agreement between the user and MeVis Medical Solutions AG.
8 **
9 ** For further information use the contact form at https://www.mevislab.de/contact
10 **
11 **************************************************************************************/
12 
13 #ifndef ML_STANDARD_ITEM_MODEL_H
14 #define ML_STANDARD_ITEM_MODEL_H
15 
16 
18 
19 #include <mlAbstractItemModel.h>
20 
21 #include <vector>
22 #include <boost/unordered_map.hpp>
23 
24 ML_START_NAMESPACE
25 
28 {
29 public:
30 
33  struct Attribute
34  {
35  Attribute(const std::string& nameArg, const Variant& defaultValueArg = Variant()) {
36  this->name = nameArg;
37  this->defaultValue = defaultValueArg;
38  }
39 
41  std::string name;
44  };
45 
47  typedef std::vector<Attribute> AttributeVector;
48 
50  typedef boost::unordered_map<int, Variant> ItemData;
52  typedef std::vector<ItemData> ItemDataVector;
53 
54 
64  int hasChildrenAttribute = -1,
65  bool flat = false);
66 
69 
70  ~StandardItemModel() override;
71 
73  bool isFlat() const override { return _isFlat; }
74  bool hasChildren(const ModelIndex& parent) override;
75  unsigned int getChildCount(const ModelIndex& parent) override;
76  ModelIndex getChild(const ModelIndex& parent, unsigned int index) override;
77  ModelIndex getParent(const ModelIndex& child) override;
78  int getAttributeCount() const override;
79  std::string getAttributeName(int index) const override;
80  Variant getAttributeDefault(int index) const override;
81  Variant getData(const ModelIndex& item, int attributeIndex) override;
82  bool rawSetData(const ModelIndex& item, int attributeIndex, const Variant& data) override;
84 
88  virtual int getChildPosition(const ModelIndex& child);
89 
91  virtual void clear();
92 
96  virtual void insertItems(const ModelIndex& parent, unsigned int position,
97  const ItemDataVector& items);
98 
99  virtual ModelIndex addItem(const ModelIndex& parent, const ItemData&);
100 
102  virtual void removeItems(const ModelIndex& parent, unsigned int position, unsigned int numItems);
103 
106  bool implementsPersistence(PersistenceInterface iface) const override { return iface == Base::PersistenceByStream; }
107  void writeTo(AbstractPersistenceOutputStream* stream) const override;
108  void readFrom(AbstractPersistenceInputStream* stream, int version) override;
110 
112 
113 protected:
114  // send event notifying listeners that the children of an item are requested for the first time
116 
117 private:
119  class ItemEntry
120  {
121  public:
122  ItemEntry(ItemEntry* aParent = nullptr);
123  ~ItemEntry();
124 
127  void writeTo(AbstractPersistenceOutputStream* stream, const std::vector<std::string>& attributeNames) const;
128 
131  void readFrom(AbstractPersistenceInputStream* stream, int version, const boost::unordered_map<std::string, int>& attributeIndices);
132 
133  ItemEntry* parent;
134  ItemData data;
135  std::vector<ItemEntry*> children;
136  bool incomplete;
137  };
138 
139  ModelIndex itemToIndex(ItemEntry* entry);
140  ItemEntry* indexToItem(const ModelIndex& index);
141 
143  AttributeVector _attributes;
144 
146  ItemEntry* _rootItem;
147 
152  ItemEntry* _incompleteParent;
153 
156  int _hasChildrenAttribute;
157 
159  bool _isFlat;
160 };
161 
162 ML_REFCOUNTED_PTR(StandardItemModel)
163 
164 //===========================================================================
165 // event classes
166 
167 class MLITEMMODEL_EXPORT ItemChildrenRequestEvent : public ItemModelEvent
172 {
173 public:
174  ItemChildrenRequestEvent(const ModelIndex& parentIndexArg) : _parentIndex(parentIndexArg) {}
175 
176  const ModelIndex& parentIndex() const { return _parentIndex; }
177 
178  ML_CLASS_HEADER(ItemChildrenRequestedEvent)
179 
180 private:
182 
183  ModelIndex _parentIndex;
184 };
185 
186 
187 ML_END_NAMESPACE
188 
189 #endif // __mlStandardItemModel_H
#define MLITEMMODEL_EXPORT
defined Header file mlItemModelSystem.h
This class represents an abstract hierarchical item model where the items have named attributes which...
Class for reading object data from a stream.
Class for writing object data to a stream.
PersistenceInterface
This enum describes the different persistence interfaces available.
Definition: mlBase.h:89
@ PersistenceByStream
Definition: mlBase.h:92
This BaseEvent informs that the children of an item are requested for the first time.
ItemChildrenRequestEvent(const ModelIndex &parentIndexArg)
const ModelIndex & parentIndex() const
This class serves as an index into an AbstractItemModel.
This could be the interface for a standard item model.
std::vector< ItemData > ItemDataVector
Vector of item data.
virtual void removeItems(const ModelIndex &parent, unsigned int position, unsigned int numItems)
remove items
unsigned int getChildCount(const ModelIndex &parent) override
Get number of children of the given item, an invalid index means number of top-level items.
ModelIndex getChild(const ModelIndex &parent, unsigned int index) override
Get n-th child of an item (or n-th top-level item if parent index is invalid).
~StandardItemModel() override
virtual ModelIndex addItem(const ModelIndex &parent, const ItemData &)
bool rawSetData(const ModelIndex &item, int attributeIndex, const Variant &data) override
This will be called by setData and bulkSetData, which will do the necessary notifications afterwards;...
virtual int getChildPosition(const ModelIndex &child)
get index position of child relative to its parent
void readFrom(AbstractPersistenceInputStream *stream, int version) override
Read the objects state from the data stream object.
bool isFlat() const override
overridden methods
virtual void clear()
clear the complete model
int getAttributeCount() const override
attribute discovery
virtual void insertItems(const ModelIndex &parent, unsigned int position, const ItemDataVector &items)
insert new items; this doesn't generate a insert notification during handling of a ItemChildrenReques...
ModelIndex getParent(const ModelIndex &child) override
Get parent of an item.
StandardItemModel(const AttributeVector &attributes, int hasChildrenAttribute=-1, bool flat=false)
Create new model with given attributes (and defaults); The second argument gives an attribute that sh...
std::vector< Attribute > AttributeVector
Vector of attributes.
StandardItemModel()
Create model without attributes.
std::string getAttributeName(int index) const override
Get name of n-th attribute. Must be overridden.
bool implementsPersistence(PersistenceInterface iface) const override
Persistence interface.
Variant getAttributeDefault(int index) const override
Return default value of n-th attribute.
void notifyItemChildrenRequest(const ModelIndex &parent)
boost::unordered_map< int, Variant > ItemData
Item data, each attribute is specified via its int identifier and its variant value.
void writeTo(AbstractPersistenceOutputStream *stream) const override
Write the objects state to the data stream object.
bool hasChildren(const ModelIndex &parent) override
Or rather mightHaveChildren.
Variant getData(const ModelIndex &item, int attributeIndex) override
data access
The Variant class stores different data types.
Definition: mlVariant.h:36
#define ML_REFCOUNTED_PTR(CLASSNAME)
Macro that defines convenience Ptr/ConstPtr typedefs to be used instead of intrusive_ptr templates.
#define ML_CLASS_HEADER(className)
Same like ML_CLASS_HEADER_EXPORTED with a non existing export symbol.
Defines the name of an attribute and its default value.
Attribute(const std::string &nameArg, const Variant &defaultValueArg=Variant())
std::string name
Name of the attribute.
Variant defaultValue
Default value of the attribute.