MeVisLabToolboxReference
MeVisLab/Standard/Sources/Shared/MLItemModel/mlStandardItemModel.h
Go to the documentation of this file.
00001 // **InsertLicense** code
00002 //===========================================================================
00004 
00009 //===========================================================================
00010 
00011 #ifndef __mlStandardItemModel_H
00012 #define __mlStandardItemModel_H
00013 
00014 #include <mlAbstractItemModel.h>
00015 
00016 #include <vector>
00017 #include <boost/unordered_map.hpp>
00018 
00019 ML_START_NAMESPACE
00020 
00022 class MLITEMMODEL_EXPORT StandardItemModel : public AbstractItemModel
00023 {
00024 public:
00025 
00028   struct Attribute
00029   {
00030     Attribute(const std::string& name, const Variant& defaultValue = Variant()) {
00031       this->name = name;
00032       this->defaultValue = defaultValue;
00033     }
00034 
00036     std::string name;
00038     Variant     defaultValue;
00039   };
00040 
00042   typedef std::vector<Attribute> AttributeVector;
00043 
00045   typedef boost::unordered_map<int, Variant> ItemData;
00047   typedef std::vector<ItemData>              ItemDataVector;
00048 
00049 
00055   StandardItemModel(const AttributeVector& attributes,
00056                     int hasChildrenAttribute = -1);
00057 
00059   StandardItemModel();
00060 
00061   virtual ~StandardItemModel();
00062 
00064   virtual bool hasChildren(const ModelIndex& parent);
00065   virtual unsigned int getChildCount(const ModelIndex& parent);
00066   virtual ModelIndex getChild(const ModelIndex& parent, unsigned int index);
00067   virtual ModelIndex getParent(const ModelIndex& child);
00068   virtual int getAttributeCount() const;
00069   virtual std::string getAttributeName(int index) const;
00070   virtual Variant getAttributeDefault(int index) const;
00071   virtual Variant getData(const ModelIndex& item, int attributeIndex);
00072   virtual bool rawSetData(const ModelIndex& item, int attributeIndex, const Variant& data);
00074 
00076   virtual void clear();
00077 
00081   virtual void insertItems(const ModelIndex& parent, unsigned int position,
00082                            const ItemDataVector& items);
00083 
00085   virtual void removeItems(const ModelIndex& parent, unsigned int position, unsigned int numItems);
00086   
00088 
00089   virtual bool implementsPersistence(PersistenceInterface iface) const { return iface == Base::PersistenceByStream; }
00090   virtual void writeTo(AbstractPersistenceOutputStream* stream) const;
00091   virtual void readFrom(AbstractPersistenceInputStream* stream, int version);
00093 
00094   ML_CLASS_HEADER(StandardItemModel)
00095 
00096 protected:
00097   // send event notifying listeners that the children of an item are requested for the first time
00098   void notifyItemChildrenRequest(const ModelIndex& parent);
00099 
00100 private:
00102   class ItemEntry
00103   {
00104   public:
00105     ItemEntry(ItemEntry* aParent = NULL);
00106     ~ItemEntry();
00107 
00110     void writeTo(AbstractPersistenceOutputStream* stream, const std::vector<std::string>& attributeNames) const;
00111 
00114     void readFrom(AbstractPersistenceInputStream* stream, int version, const boost::unordered_map<std::string, int>& attributeIndices);
00115 
00116     ItemEntry* parent;
00117     ItemData   data;
00118     std::vector<ItemEntry*> children;
00119     bool incomplete;
00120   };
00121 
00122   ModelIndex itemToIndex(ItemEntry* entry);
00123   ItemEntry* indexToItem(const ModelIndex& index);
00124 
00126   AttributeVector _attributes;
00127 
00129   ItemEntry* _rootItem;
00130 
00135   ItemEntry* _incompleteParent;
00136 
00139   int _hasChildrenAttribute;
00140 };
00141 
00142 ML_REFCOUNTED_PTR(StandardItemModel)
00143 
00144 //===========================================================================
00145 // event classes
00146 
00147 
00148 
00149 
00150 
00151 class MLITEMMODEL_EXPORT ItemChildrenRequestEvent : public ItemModelEvent
00152 {
00153 public:
00154   ItemChildrenRequestEvent(const ModelIndex& parentIndex) : _parentIndex(parentIndex) {}
00155 
00156   const ModelIndex& parentIndex() const { return _parentIndex; }
00157 
00158   ML_CLASS_HEADER(ItemChildrenRequestedEvent)
00159 
00160 private:
00161   ItemChildrenRequestEvent() {}
00162 
00163   ModelIndex _parentIndex;
00164 };
00165 
00166 
00167 ML_END_NAMESPACE
00168 
00169 #endif // __mlStandardItemModel_H