MeVisLab Toolbox Reference
mlAbstractItemModel.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_ABSTRACT_ITEM_MODEL_H
14#define ML_ABSTRACT_ITEM_MODEL_H
15
16
22
23#include <mlRefCountedBase.h>
24
25#include <vector>
26
27#include "MLItemModelSystem.h"
28#include <mlVariant.h>
29
30ML_START_NAMESPACE
31
32//===========================================================================
33
34class AbstractItemModel;
35class ItemModelProxy;
36
41// This is analog to QModelIndex
42// Note that we skip column and row attributes, they serve no special purpose for us
43{
44public:
47 ModelIndex() : _model(nullptr), _userID(0) {}
48 ModelIndex(const ModelIndex& other);
50
52 inline AbstractItemModel* model() const { return _model; }
53
55 inline bool isValid() const { return _model != nullptr; }
56
58 inline void* userPtr() const { return _userPtr; }
59
61 inline size_t userID() const { return _userID; }
62
64 Variant getData(int attributeIndex) const;
65
67 const ModelIndex& operator= (const ModelIndex& index);
68
69 bool operator== (const ModelIndex& index) const;
70 bool operator!= (const ModelIndex& index) const;
71
72private:
73 friend class AbstractItemModel;
76
77 AbstractItemModel* _model;
78 union {
79 void* _userPtr;
80 size_t _userID;
81 };
82};
83
84//===========================================================================
85
91{
92 friend class ItemModelProxy;
93
94public:
96
99 virtual bool isFlat() const { return false; }
100
107 virtual bool hasChildren(const ModelIndex& parent);
108
113 virtual unsigned int getChildCount(const ModelIndex& parent) = 0;
114
119 virtual ModelIndex getChild(const ModelIndex& parent, unsigned int index) = 0;
120
123 virtual ModelIndex getParent(const ModelIndex& child) = 0;
125
127
129 virtual int getAttributeCount() const = 0;
130
132 virtual std::string getAttributeName(int index) const = 0;
133
136 virtual Variant getAttributeDefault(int index) const;
137
140 virtual int getAttributeIndex(const std::string& name);
142
144
146 virtual Variant getData(const ModelIndex& item, int attributeIndex) = 0;
147
152 virtual bool setData(const ModelIndex& item, int attributeIndex, const Variant& data,
153 void* skipListener = nullptr);
154
158 virtual bool bulkSetData(const std::vector<ModelIndex>& items,
159 int attributeIndex, const Variant& data,
160 void* skipListener = nullptr);
161
165 virtual bool bulkSetData(const std::vector<ModelIndex>& items,
166 int attributeIndex, const std::vector<Variant>& data,
167 void* skipListener = nullptr);
169
171
172protected:
173 friend class ModelIndex;
174
176
181 virtual bool rawSetData(const ModelIndex& item, int attributeIndex, const Variant& data) = 0;
182
188
190 virtual void incItemRefCount(void*) {}
191 virtual void decItemRefCount(void*) {}
193
195
201 virtual void notifyItemChanged(const ModelIndex& item, bool after,
202 void* skipListener = nullptr);
203
209 virtual void notifyItemsInserted(const ModelIndex& parent, unsigned int childIndex, unsigned int itemsInserted, bool after,
210 void* skipListener = nullptr);
211
217 virtual void notifyItemsRemoved(const ModelIndex& parent, unsigned int childIndex, unsigned int itemsRemoved, bool after,
218 void* skipListener = nullptr);
219
225 virtual void notifyItemsDataChanged(const std::vector<ModelIndex>& items,
226 const std::vector<int>& attributeIndices,
227 void* skipListener = nullptr);
229};
230
232
233//===========================================================================
234// Event classes
235
236
246
247//---------------------------------------------------------------------------
248
252{
253public:
254 TwoPhaseItemModelEvent(bool afterArg = false) : ItemModelEvent(), _after(afterArg) {}
255 bool after() const { return _after; }
256
258
259private:
260 bool _after;
261};
262
263//---------------------------------------------------------------------------
264
267{
268public:
269 ItemsInsertedEvent(const ModelIndex& parentIndex, unsigned int position, unsigned int itemsInserted, bool after);
270
271 const ModelIndex& parentIndex() const { return _parentIndex; }
272 unsigned int position() const { return _position; }
273 unsigned int itemsInserted() const { return _itemsInserted; }
274
276
277private:
279
280 ModelIndex _parentIndex;
281 unsigned int _position;
282 unsigned int _itemsInserted;
283};
284
285//---------------------------------------------------------------------------
286
289{
290public:
291 ItemsRemovedEvent(const ModelIndex& parentIndex, unsigned int position, unsigned int itemsInserted, bool after);
292
293 const ModelIndex& parentIndex() const { return _parentIndex; }
294 unsigned int position() const { return _position; }
295 unsigned int itemsRemoved() const { return _itemsRemoved; }
296
298
299private:
301
302 ModelIndex _parentIndex;
303 unsigned int _position;
304 unsigned int _itemsRemoved;
305};
306
307//---------------------------------------------------------------------------
308
312{
313public:
315
316 const ModelIndex& index() const { return _index; }
317
319
320private:
322
323 ModelIndex _index;
324};
325
326//---------------------------------------------------------------------------
327
330{
331public:
332 ItemsDataChangedEvent(const std::vector<ModelIndex>& itemIndices,
333 const std::vector<int>& attributeIndices);
334
335 const std::vector<ModelIndex>& itemIndices() const { return _itemIndices; }
336 const std::vector<int>& attributeIndices() const { return _attributeIndices; }
337
339
340private:
342
343 std::vector<ModelIndex> _itemIndices;
344 std::vector<int> _attributeIndices;
345};
346
347//===========================================================================
348
349ML_END_NAMESPACE
350
351#endif // __mlAbstractItemModel_H
#define MLITEMMODEL_EXPORT
defined Header file mlItemModelSystem.h
virtual void notifyItemsInserted(const ModelIndex &parent, unsigned int childIndex, unsigned int itemsInserted, bool after, void *skipListener=nullptr)
virtual bool setData(const ModelIndex &item, int attributeIndex, const Variant &data, void *skipListener=nullptr)
virtual bool rawSetData(const ModelIndex &item, int attributeIndex, const Variant &data)=0
virtual ModelIndex getParent(const ModelIndex &child)=0
virtual void notifyItemsRemoved(const ModelIndex &parent, unsigned int childIndex, unsigned int itemsRemoved, bool after, void *skipListener=nullptr)
virtual void notifyItemChanged(const ModelIndex &item, bool after, void *skipListener=nullptr)
change notifications
virtual std::string getAttributeName(int index) const =0
Returns the name of n-th attribute. Must be overridden.
virtual int getAttributeIndex(const std::string &name)
virtual void incItemRefCount(void *)
Implement this if the pointer in the model indices needs explicit reference counting.
virtual int getAttributeCount() const =0
Attribute discovery.
virtual Variant getAttributeDefault(int index) const
virtual bool isFlat() const
item traversal interface
virtual Variant getData(const ModelIndex &item, int attributeIndex)=0
Data access.
virtual unsigned int getChildCount(const ModelIndex &parent)=0
ModelIndex createIndex(size_t data)
ModelIndex createIndex(void *data)
virtual ModelIndex getChild(const ModelIndex &parent, unsigned int index)=0
virtual bool bulkSetData(const std::vector< ModelIndex > &items, int attributeIndex, const Variant &data, void *skipListener=nullptr)
virtual void notifyItemsDataChanged(const std::vector< ModelIndex > &items, const std::vector< int > &attributeIndices, void *skipListener=nullptr)
virtual bool hasChildren(const ModelIndex &parent)
virtual void decItemRefCount(void *)
virtual bool bulkSetData(const std::vector< ModelIndex > &items, int attributeIndex, const std::vector< Variant > &data, void *skipListener=nullptr)
EventSource * source() const
Returns the Base object emitting this event.
const ModelIndex & index() const
ItemChangedEvent(const ModelIndex &index, bool after)
AbstractItemModel * model() const
This event informs about attributes that have changed their values.
const std::vector< ModelIndex > & itemIndices() const
const std::vector< int > & attributeIndices() const
ItemsDataChangedEvent(const std::vector< ModelIndex > &itemIndices, const std::vector< int > &attributeIndices)
This event informs about inserted items.
unsigned int position() const
unsigned int itemsInserted() const
ItemsInsertedEvent(const ModelIndex &parentIndex, unsigned int position, unsigned int itemsInserted, bool after)
const ModelIndex & parentIndex() const
This event informs about removed items.
const ModelIndex & parentIndex() const
ItemsRemovedEvent(const ModelIndex &parentIndex, unsigned int position, unsigned int itemsInserted, bool after)
unsigned int itemsRemoved() const
unsigned int position() const
AbstractItemModel * model() const
get the model of this index (this will be NULL for invalid indices)
friend class AbstractItemModel
bool isValid() const
return if this index is valid i.e. it points at an item in the model
size_t userID() const
access the internal user value (same as user pointer)
Variant getData(int attributeIndex) const
convenience method, this calls model()->getData(this, attributeIndex);
void * userPtr() const
access the internal user pointer (same as user value)
ModelIndex(const ModelIndex &other)
RefCountedBase()
Constructor.
TwoPhaseItemModelEvent(bool afterArg=false)
#define ML_ABSTRACT_CLASS_HEADER(className)
#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)