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
24ML_START_NAMESPACE
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;
75 ModelIndex getParent(const ModelIndex& child) override;
76 int getAttributeCount() const override;
77 std::string getAttributeName(int index) const override;
78 Variant getAttributeDefault(int index) const override;
79 Variant getData(const ModelIndex& item, int attributeIndex) 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
104 bool implementsPersistence(PersistenceInterface iface) const override { return iface == Base::PersistenceByStream; }
105 void writeTo(AbstractPersistenceOutputStream* stream) const override;
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:
177 ItemChildrenRequestEvent(const ModelIndex& parentIndexArg) : _parentIndex(parentIndexArg) {}
178
179 const ModelIndex& parentIndex() const { return _parentIndex; }
180
181 ML_CLASS_HEADER(ItemChildrenRequestedEvent)
182
183private:
185
186 ModelIndex _parentIndex;
187};
188
189
190ML_END_NAMESPACE
191
192#endif // __mlStandardItemModel_H
#define MLITEMMODEL_EXPORT
defined Header file mlItemModelSystem.h
PersistenceInterface
This enum describes the different persistence interfaces available.
Definition mlBase.h:73
@ PersistenceByStream
Definition mlBase.h:76
ItemChildrenRequestEvent(const ModelIndex &parentIndexArg)
const ModelIndex & parentIndex() const
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
ModelIndex getChild(const ModelIndex &parent, unsigned int index) override
~StandardItemModel() override
virtual ModelIndex addItem(const ModelIndex &parent, const ItemData &)
bool rawSetData(const ModelIndex &item, int attributeIndex, const Variant &data) override
virtual int getChildPosition(const ModelIndex &child)
void readFrom(AbstractPersistenceInputStream *stream, int version) override
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)
ModelIndex getParent(const ModelIndex &child) override
StandardItemModel(const AttributeVector &attributes, int hasChildrenAttribute=-1, bool flat=false)
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
Variant getAttributeDefault(int index) const override
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
bool hasChildren(const ModelIndex &parent) override
Variant getData(const ModelIndex &item, int attributeIndex) override
Data access.
#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)
Attribute(const std::string &nameArg, const Variant &defaultValueArg=Variant())
std::string name
The name of the attribute.
Variant defaultValue
Default value of the attribute.