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 
30 ML_START_NAMESPACE
31 
32 //===========================================================================
33 
34 class AbstractItemModel;
35 class 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 {
44 public:
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 
72 private:
73  friend class AbstractItemModel;
74  ModelIndex(AbstractItemModel* model, void* userPtr);
75  ModelIndex(AbstractItemModel* model, size_t userID);
76 
77  AbstractItemModel* _model;
78  union {
79  void* _userPtr;
80  size_t _userID;
81  };
82 };
83 
84 //===========================================================================
85 
91 {
92  friend class ItemModelProxy;
93 
94 public:
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 
172 protected:
173  friend class ModelIndex;
174 
176 
181  virtual bool rawSetData(const ModelIndex& item, int attributeIndex, const Variant& data) = 0;
182 
185  ModelIndex createIndex(void* data);
186  ModelIndex createIndex(size_t data);
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 
239 {
240 public:
242  AbstractItemModel* model() const { return static_cast<AbstractItemModel*>(source()); }
243 
245 };
246 
247 //---------------------------------------------------------------------------
248 
252 {
253 public:
254  TwoPhaseItemModelEvent(bool afterArg = false) : ItemModelEvent(), _after(afterArg) {}
255  bool after() const { return _after; }
256 
258 
259 private:
260  bool _after;
261 };
262 
263 //---------------------------------------------------------------------------
264 
267 {
268 public:
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 
277 private:
278  ItemsInsertedEvent() {}
279 
280  ModelIndex _parentIndex;
281  unsigned int _position;
282  unsigned int _itemsInserted;
283 };
284 
285 //---------------------------------------------------------------------------
286 
289 {
290 public:
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 
299 private:
300  ItemsRemovedEvent() {}
301 
302  ModelIndex _parentIndex;
303  unsigned int _position;
304  unsigned int _itemsRemoved;
305 };
306 
307 //---------------------------------------------------------------------------
308 
312 {
313 public:
314  ItemChangedEvent(const ModelIndex& index, bool after);
315 
316  const ModelIndex& index() const { return _index; }
317 
319 
320 private:
321  ItemChangedEvent() {}
322 
323  ModelIndex _index;
324 };
325 
326 //---------------------------------------------------------------------------
327 
330 {
331 public:
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 
340 private:
342 
343  std::vector<ModelIndex> _itemIndices;
344  std::vector<int> _attributeIndices;
345 };
346 
347 //===========================================================================
348 
349 ML_END_NAMESPACE
350 
351 #endif // __mlAbstractItemModel_H
#define MLITEMMODEL_EXPORT
defined Header file mlItemModelSystem.h
This class represents an abstract hierarchical item model where the items have named attributes which...
virtual void notifyItemsInserted(const ModelIndex &parent, unsigned int childIndex, unsigned int itemsInserted, bool after, void *skipListener=nullptr)
Notify listeners that an item gets/got additional children.
virtual bool setData(const ModelIndex &item, int attributeIndex, const Variant &data, void *skipListener=nullptr)
Update a single attribute on a single item.
virtual bool rawSetData(const ModelIndex &item, int attributeIndex, const Variant &data)=0
This will be called by setData and bulkSetData, which will do the necessary notifications afterwards;...
virtual ModelIndex getParent(const ModelIndex &child)=0
Get parent of an item.
virtual void notifyItemsRemoved(const ModelIndex &parent, unsigned int childIndex, unsigned int itemsRemoved, bool after, void *skipListener=nullptr)
Notify listeners that an item loses/lost additional children.
virtual void notifyItemChanged(const ModelIndex &item, bool after, void *skipListener=nullptr)
change notifications
virtual std::string getAttributeName(int index) const =0
Get name of n-th attribute. Must be overridden.
virtual int getAttributeIndex(const std::string &name)
Get index of attribute with the given 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
Return default value of n-th attribute.
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
Get number of children of the given item, an invalid index means number of top-level items.
ModelIndex createIndex(size_t data)
ModelIndex createIndex(void *data)
virtual ModelIndex getChild(const ModelIndex &parent, unsigned int index)=0
Get n-th child of an item (or n-th top-level item if parent index is invalid).
virtual bool bulkSetData(const std::vector< ModelIndex > &items, int attributeIndex, const Variant &data, void *skipListener=nullptr)
bulkSetData will be used by views to set selection state to an attribute, so that we only get a singl...
virtual void notifyItemsDataChanged(const std::vector< ModelIndex > &items, const std::vector< int > &attributeIndices, void *skipListener=nullptr)
Notify listeners that attributes have changed on the given items.
virtual bool hasChildren(const ModelIndex &parent)
Or rather mightHaveChildren.
virtual void decItemRefCount(void *)
virtual bool bulkSetData(const std::vector< ModelIndex > &items, int attributeIndex, const std::vector< Variant > &data, void *skipListener=nullptr)
This method can be used to update whole columns with varying data.
BaseEvent is the base class for all events emitted from EventSourceBase.
Definition: mlEventSource.h:86
This event tells us that the children of the item have changed completely (for an invalid ModelIndex ...
ItemChangedEvent(const ModelIndex &index, bool after)
const ModelIndex & index() const
This intermediate class only exists to conveniently get the source() as the model() from an event.
AbstractItemModel * model() const
This event informs about attributes that have changed their values.
const std::vector< int > & attributeIndices() const
const std::vector< ModelIndex > & itemIndices() 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
This class serves as an index into an AbstractItemModel.
ModelIndex()
The only public constructor creates an "invalid" model index, which represents the virtual/invisible ...
AbstractItemModel * model() const
get the model of this index (this will be NULL for invalid indices)
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 class adds intrusive reference counting support to the Base class.
This is the base class for all events that are sent before and after a change.
TwoPhaseItemModelEvent(bool afterArg=false)
The Variant class stores different data types.
Definition: mlVariant.h:36
#define ML_ABSTRACT_CLASS_HEADER(className)
Same like ML_ABSTRACT_CLASS_HEADER_EXPORTED with a non existing export symbol.
#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.
boost::graph_traits< ml_graph_ptr >::vertex_descriptor source(graph_traits< ml_graph_ptr >::edge_descriptor e, const ml_graph_ptr)
Returns the vertex descriptor for u of the edge (u,v) represented by e.
bool operator==(const Tmat2< DT > &a, const Tmat2< DT > &b)
a == b ? Return true if yes.
Definition: mlMatrix2.h:425
bool operator!=(const Tmat2< DT > &a, const Tmat2< DT > &b)
a != b ? Return true if yes.
Definition: mlMatrix2.h:433