MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
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:
32 struct Attribute
33 {
34 Attribute(const std::string& nameArg, const Variant& defaultValueArg = Variant())
35 {
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
67
69 bool isFlat() const override
70 {
71 return _isFlat;
72 }
73 bool hasChildren(const ModelIndex& parent) override;
74 unsigned int getChildCount(const ModelIndex& parent) override;
75 ModelIndex getChild(const ModelIndex& parent, unsigned int index) override;
77 int getAttributeCount() const override;
78 std::string getAttributeName(int index) const override;
79 Variant getAttributeDefault(int index) const override;
81 bool rawSetData(const ModelIndex& item, int attributeIndex, const Variant& data) override;
83
87 virtual int getChildPosition(const ModelIndex& child);
88
90 virtual void clear();
91
95 virtual void insertItems(const ModelIndex& parent, unsigned int position, 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
105 {
107 }
109 void readFrom(AbstractPersistenceInputStream* stream, int version) override;
111
113
114protected:
115 // StandardItemModel should only be deleted by the shared pointer -
116 // embedding a StandardItemModel as a member of another class/struct, which is automatically
117 // deleted, conflicts with the reference counting of the RefCountedBase class.
119
120 // Sends event notifying listeners that the children of an item are requested for the first time.
122
123private:
125 class ItemEntry
126 {
127 public:
128 ItemEntry(ItemEntry* aParent = nullptr);
129 ~ItemEntry();
130
133 void writeTo(AbstractPersistenceOutputStream* stream, const std::vector<std::string>& attributeNames) const;
134
137 void readFrom(AbstractPersistenceInputStream* stream, int version,
138 const boost::unordered_map<std::string, int>& attributeIndices);
139
140 ItemEntry* parent;
141 ItemData data;
142 std::vector<ItemEntry*> children;
143 bool incomplete;
144 };
145
146 ModelIndex itemToIndex(ItemEntry* entry);
147 ItemEntry* indexToItem(const ModelIndex& index);
148
150 AttributeVector _attributes;
151
153 ItemEntry* _rootItem;
154
159 ItemEntry* _incompleteParent;
160
163 int _hasChildrenAttribute;
164
166 bool _isFlat;
167};
168
169ML_REFCOUNTED_PTR(StandardItemModel)
170
171//===========================================================================
172// event classes
173
174
179{
180public:
184
185 const ModelIndex& parentIndex() const
186 {
187 return _parentIndex;
188 }
189
191
192private:
194 {}
195
196 ModelIndex _parentIndex;
197};
198
199
201
202#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:76
@ PersistenceByStream
Definition mlBase.h:79
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.