MeVisLabToolboxReference
MeVisLab/Standard/Sources/Shared/MLItemModel/mlAbstractItemModel.h
Go to the documentation of this file.
00001 // **InsertLicense** code
00002 //===========================================================================
00008 
00013 //===========================================================================
00014 
00015 #ifndef __mlAbstractItemModel_H
00016 #define __mlAbstractItemModel_H
00017 
00018 #include <mlRefCountedBase.h>
00019 
00020 #include <vector>
00021 
00022 #include "MLItemModelSystem.h"
00023 #include <mlVariant.h>
00024 
00025 ML_START_NAMESPACE
00026 
00027 //===========================================================================
00028 
00029 class AbstractItemModel;
00030 
00034 class MLITEMMODEL_EXPORT ModelIndex
00035 // This is analog to QModelIndex
00036 // Note that we skip column and row attributes, they serve no special purpose for us
00037 {
00038 public:
00040   ModelIndex() : _model(NULL), _userID(0) {}
00041 
00043   inline AbstractItemModel* model() const { return _model; }
00044 
00046   inline bool isValid() const { return _model != NULL; }
00047 
00049   inline void* userPtr() const { return _userPtr; }
00050 
00052   inline size_t userID() const { return _userID; }
00053 
00055   Variant getData(int attributeIndex) const;
00056 
00057   bool operator== (const ModelIndex& index) const;
00058   bool operator!= (const ModelIndex& index) const;
00059 
00060 private:
00061   friend class AbstractItemModel;
00062   ModelIndex(AbstractItemModel* model, void* userPtr) : _model(model), _userPtr(userPtr) {}
00063   ModelIndex(AbstractItemModel* model, size_t userID) : _model(model), _userID(userID) {}
00064 
00065   AbstractItemModel* _model;
00066   union {
00067     void* _userPtr;
00068     size_t _userID;
00069   };
00070 };
00071 
00072 //===========================================================================
00073 
00078 class MLITEMMODEL_EXPORT AbstractItemModel : public RefCountedBase
00079 {
00080 public:
00082 
00085   virtual bool isFlat() const { return false; }
00086 
00093   virtual bool hasChildren(const ModelIndex& parent);
00094 
00099   virtual unsigned int getChildCount(const ModelIndex& parent) = 0;
00100 
00105   virtual ModelIndex getChild(const ModelIndex& parent, unsigned int index) = 0;
00106 
00109   virtual ModelIndex getParent(const ModelIndex& child) = 0;
00111   
00113 
00115   virtual int getAttributeCount() const = 0;
00116 
00118   virtual std::string getAttributeName(int index) const = 0;
00119 
00122   virtual Variant getAttributeDefault(int index) const;
00123 
00126   virtual int getAttributeIndex(const std::string& name);
00128 
00130 
00132   virtual Variant getData(const ModelIndex& item, int attributeIndex) = 0;
00133 
00138   virtual bool setData(const ModelIndex& item, int attributeIndex, const Variant& data,
00139                        void* skipListener = NULL);
00140   
00144   virtual bool bulkSetData(const std::vector<ModelIndex>& items,
00145                            int attributeIndex, const Variant& data,
00146                            void* skipListener = NULL);
00147 
00151   virtual bool bulkSetData(const std::vector<ModelIndex>& items,
00152                            int attributeIndex, const std::vector<Variant>& data,
00153                            void* skipListener = NULL);
00155 
00156   ML_ABSTRACT_CLASS_HEADER(AbstractItemModel)
00157   
00158 protected:  
00159   AbstractItemModel() {}
00160 
00165   virtual bool rawSetData(const ModelIndex& item, int attributeIndex, const Variant& data) = 0;
00166 
00168   ModelIndex createIndex(void* data);
00169   ModelIndex createIndex(size_t data);
00171   
00173 
00179   virtual void notifyItemChanged(const ModelIndex& item, bool after,
00180                                  void* skipListener = NULL);
00181 
00187   virtual void notifyItemsInserted(const ModelIndex& parent, unsigned int childIndex, unsigned int itemsInserted, bool after,
00188                                    void* skipListener = NULL); 
00189 
00195   virtual void notifyItemsRemoved(const ModelIndex& parent, unsigned int childIndex, unsigned int itemsRemoved, bool after,
00196                                   void* skipListener = NULL); 
00197 
00203   virtual void notifyItemsDataChanged(const std::vector<ModelIndex>& items,
00204                                       const std::vector<int>& attributeIndices,
00205                                       void* skipListener = NULL); 
00207 };
00208 
00209 ML_REFCOUNTED_PTR(AbstractItemModel)
00210 
00211 //===========================================================================
00212 // event classes
00213 
00214 
00215 
00216 class MLITEMMODEL_EXPORT ItemModelEvent : public BaseEvent
00217 {
00218 public:
00219   ItemModelEvent() {}
00220   AbstractItemModel* model() const { return static_cast<AbstractItemModel*>(source()); }
00221 
00222   ML_ABSTRACT_CLASS_HEADER(ItemModelEvent)
00223 };
00224 
00225 //---------------------------------------------------------------------------
00226 
00229 class MLITEMMODEL_EXPORT TwoPhaseItemModelEvent : public ItemModelEvent
00230 {
00231 public:
00232   TwoPhaseItemModelEvent(bool after = false) : ItemModelEvent(), _after(after) {}
00233   bool after() const { return _after; }
00234 
00235   ML_ABSTRACT_CLASS_HEADER(TwoPhaseItemModelEvent)
00236 
00237 private:
00238   bool _after;
00239 };
00240 
00241 //---------------------------------------------------------------------------
00242 
00244 class MLITEMMODEL_EXPORT ItemsInsertedEvent : public TwoPhaseItemModelEvent
00245 {
00246 public:
00247   ItemsInsertedEvent(const ModelIndex& parentIndex, unsigned int position, unsigned int itemsInserted, bool after);
00248 
00249   const ModelIndex& parentIndex() const { return _parentIndex; }
00250   unsigned int position() const { return _position; }
00251   unsigned int itemsInserted() const { return _itemsInserted; }
00252 
00253   ML_CLASS_HEADER(ItemsInsertedEvent)
00254 
00255 private:
00256   ItemsInsertedEvent() {}
00257 
00258   ModelIndex _parentIndex;
00259   unsigned int _position;
00260   unsigned int _itemsInserted;
00261 };
00262 
00263 //---------------------------------------------------------------------------
00264 
00266 class MLITEMMODEL_EXPORT ItemsRemovedEvent : public TwoPhaseItemModelEvent
00267 {
00268 public:
00269   ItemsRemovedEvent(const ModelIndex& parentIndex, unsigned int position, unsigned int itemsInserted, bool after);
00270 
00271   const ModelIndex& parentIndex() const { return _parentIndex; }
00272   unsigned int position() const { return _position; }
00273   unsigned int itemsRemoved() const { return _itemsRemoved; }
00274 
00275   ML_CLASS_HEADER(ItemsRemovedEvent)
00276 
00277 private:
00278   ItemsRemovedEvent() {}
00279 
00280   ModelIndex _parentIndex;
00281   unsigned int _position;
00282   unsigned int _itemsRemoved;
00283 };
00284 
00285 //---------------------------------------------------------------------------
00286 
00289 class MLITEMMODEL_EXPORT ItemChangedEvent : public TwoPhaseItemModelEvent
00290 {
00291 public:
00292   ItemChangedEvent(const ModelIndex& index, bool after);
00293 
00294   const ModelIndex& index() const { return _index; }
00295 
00296   ML_CLASS_HEADER(ItemChangedEvent)
00297 
00298 private:
00299   ItemChangedEvent() {}
00300 
00301   ModelIndex _index;
00302 };
00303 
00304 //---------------------------------------------------------------------------
00305 
00307 class MLITEMMODEL_EXPORT ItemsDataChangedEvent : public ItemModelEvent
00308 {
00309 public:
00310   ItemsDataChangedEvent(const std::vector<ModelIndex>& itemIndices,
00311     const std::vector<int>& attributeIndices);
00312 
00313   const std::vector<ModelIndex>& itemIndices() const { return _itemIndices; }
00314   const std::vector<int>& attributeIndices() const { return _attributeIndices; }
00315 
00316   ML_CLASS_HEADER(ItemsDataChangedEvent)
00317 
00318 private:
00319   ItemsDataChangedEvent() {}
00320 
00321   std::vector<ModelIndex> _itemIndices;
00322   std::vector<int> _attributeIndices;
00323 };
00324 
00325 //===========================================================================
00326 
00327 ML_END_NAMESPACE
00328 
00329 #endif // __mlAbstractItemModel_H