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
25
28{
29public:
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
71 bool isFlat() const override { return _isFlat; }
72 bool hasChildren(const ModelIndex& parent) override;
73 unsigned int getChildCount(const ModelIndex& parent) override;
74 ModelIndex getChild(const ModelIndex& parent, unsigned int index) override;
76 int getAttributeCount() const override;
77 std::string getAttributeName(int index) const override;
78 Variant getAttributeDefault(int index) const override;
80 bool rawSetData(const ModelIndex& item, int attributeIndex, const Variant& data) override;
82
86 virtual int getChildPosition(const ModelIndex& child);
87
89 virtual void clear();
90
94 virtual void insertItems(const ModelIndex& parent, unsigned int position,
95 const ItemDataVector& items);
96
97 virtual ModelIndex addItem(const ModelIndex& parent, const ItemData&);
98
100 virtual void removeItems(const ModelIndex& parent, unsigned int position, unsigned int numItems);
101
106 void readFrom(AbstractPersistenceInputStream* stream, int version) override;
108
110
111protected:
112 // StandardItemModel should only be deleted by the shared pointer -
113 // embedding a StandardItemModel as a member of another class/struct, which is automatically
114 // deleted, conflicts with the reference counting of the RefCountedBase class.
116
117 // Sends event notifying listeners that the children of an item are requested for the first time.
119
120private:
122 class ItemEntry
123 {
124 public:
125 ItemEntry(ItemEntry* aParent = nullptr);
126 ~ItemEntry();
127
130 void writeTo(AbstractPersistenceOutputStream* stream, const std::vector<std::string>& attributeNames) const;
131
134 void readFrom(AbstractPersistenceInputStream* stream, int version, const boost::unordered_map<std::string, int>& attributeIndices);
135
136 ItemEntry* parent;
137 ItemData data;
138 std::vector<ItemEntry*> children;
139 bool incomplete;
140 };
141
142 ModelIndex itemToIndex(ItemEntry* entry);
143 ItemEntry* indexToItem(const ModelIndex& index);
144
146 AttributeVector _attributes;
147
149 ItemEntry* _rootItem;
150
155 ItemEntry* _incompleteParent;
156
159 int _hasChildrenAttribute;
160
162 bool _isFlat;
163};
164
165ML_REFCOUNTED_PTR(StandardItemModel)
166
167//===========================================================================
168// event classes
169
170
175{
176public:
178
179 const ModelIndex& parentIndex() const { return _parentIndex; }
180
182
183private:
185
186 ModelIndex _parentIndex;
187};
188
189
191
192#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:73
@ PersistenceByStream
Definition mlBase.h:76
This BaseEvent informs that the children of an item are requested for the first time.
ItemChildrenRequestEvent(const ModelIndex &parentIndexArg)
const ModelIndex & parentIndex() const
This intermediate class only exists to conveniently get the source() as the model() from an event.
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)
Removes items.
unsigned int getChildCount(const ModelIndex &parent) override
Returns the 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
Returns the 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)
Returns the index position of child relative to its parent.
void readFrom(AbstractPersistenceInputStream *stream, int version) override
Reads the objects state from the data stream object.
bool isFlat() const override
Overridden methods.
virtual void clear()
Clears the entire model.
int getAttributeCount() const override
Attribute discovery.
virtual void insertItems(const ModelIndex &parent, unsigned int position, const ItemDataVector &items)
Inserts new items; this does not generate a insert notification during handling of a ItemChildrenRequ...
ModelIndex getParent(const ModelIndex &child) override
Returns the parent of an item.
StandardItemModel(const AttributeVector &attributes, int hasChildrenAttribute=-1, bool flat=false)
Creates new model with given attributes (and defaults).
std::vector< Attribute > AttributeVector
Vector of attributes.
StandardItemModel()
Creates a model without attributes.
std::string getAttributeName(int index) const override
Returns the name of n-th attribute. Must be overridden.
bool implementsPersistence(PersistenceInterface iface) const override
Persistence interface.
Variant getAttributeDefault(int index) const override
Returns the 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
Writes 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
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
#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
The name of the attribute.
Variant defaultValue
Default value of the attribute.