MeVisLab Toolbox Reference
mlItemModelItemFilter.h
Go to the documentation of this file.
1 /*************************************************************************************
2 **
3 ** Copyright 2014, 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_ITEM_MODEL_ITEM_FILTER_H
14 #define ML_ITEM_MODEL_ITEM_FILTER_H
15 
16 
20 
21 #include <mlItemModelProxy.h>
22 
23 #include <ThirdPartyWarningsDisable.h>
24 #include <boost/unordered_map.hpp>
25 #include <ThirdPartyWarningsRestore.h>
26 
27 ML_START_NAMESPACE
28 
29 //===========================================================================
30 
32 {
33 public:
34  enum FilterMode {
37  CheckRecursively
38  };
39 
40  ItemModelItemFilter(AbstractItemModelPtr sourceModel, FilterMode filterMode = DirectMatch);
42 
43 protected:
46  virtual bool isFilteredAttribute(int attributeIndex) const;
47 
49  virtual bool filterItem(const ModelIndex& sourceItem) = 0;
50 
51 public:
53 
54  bool isFlat() const override;
55 
56  bool hasChildren(const ModelIndex& parent) override;
57 
58  unsigned int getChildCount(const ModelIndex& parent) override;
59 
60  ModelIndex getChild(const ModelIndex& parent, unsigned int index) override;
61 
62  ModelIndex getParent(const ModelIndex& child) override;
64 
66 
68  int getAttributeCount() const override;
69 
71  std::string getAttributeName(int index) const override;
72 
75  Variant getAttributeDefault(int index) const override;
76 
79  int getAttributeIndex(const std::string& name) override;
81 
83 
84  Variant getData(const ModelIndex& item, int attributeIndex) override;
85  bool setData(const ModelIndex& item, int attributeIndex, const Variant& data,
86  void* skipListener = nullptr) override;
87  bool bulkSetData(const std::vector<ModelIndex>& items,
88  int attributeIndex, const Variant& data,
89  void* skipListener = nullptr) override;
90  bool bulkSetData(const std::vector<ModelIndex>& items,
91  int attributeIndex, const std::vector<Variant>& data,
92  void* skipListener = nullptr) override;
93 
94  bool rawSetData(const ModelIndex& item, int attributeIndex, const Variant& data) override;
95 
97 
99 
100 private:
102  virtual bool itemVisible(const ModelIndex& sourceItem);
103 
106  bool refilterItem(const ModelIndex& sourceItem);
107 
109  unsigned int getTargetPos(const ModelIndex& sourceParent, unsigned int sourcePos);
110 
112  void sourceItemsInserted(ml::ItemsInsertedEvent* ev) override;
113  void sourceItemsRemoved(ml::ItemsRemovedEvent* ev) override;
114  void sourceItemsDataChanged(ml::ItemsDataChangedEvent* evt) override;
115  void sourceItemChanged(ml::ItemChangedEvent* ev) override;
117 
118  typedef std::vector<unsigned int> MappingVector;
119  typedef boost::unordered_map<size_t, MappingVector*> MappingTable;
120 
123  MappingVector* getMappingVector(const ModelIndex& index);
124 
126  MappingVector* getFilledMappingVector(const ModelIndex& index);
127 
129  void dropMappingVector(const ModelIndex& sourceParent, MappingVector* v);
130 
132  FilterMode _filterMode;
133 
135  MappingVector* _topLevelMapping;
136 
138  MappingTable _items;
139 
141  inline size_t mappingKey(const ModelIndex& index)
142  {
143  return index.userID();
144  }
145 
147  inline bool hasMapping(const ModelIndex& index)
148  {
149  return _items.find(mappingKey(index)) != _items.end();
150  }
151 
153  void* _listenerToSkipForDataChange;
154 };
155 
156 ML_REFCOUNTED_PTR(ItemModelItemFilter)
157 
158 //===========================================================================
159 
160 ML_END_NAMESPACE
161 
162 #endif // __mlItemModelItemFilter_H
#define MLITEMMODEL_EXPORT
defined Header file mlItemModelSystem.h
This event tells us that the children of the item have changed completely (for an invalid ModelIndex ...
virtual bool filterItem(const ModelIndex &sourceItem)=0
Return true if the item from the source model should be in the result (directly matches)
std::string getAttributeName(int index) const override
Get name of n-th attribute. Must be overridden.
bool bulkSetData(const std::vector< ModelIndex > &items, int attributeIndex, const std::vector< Variant > &data, void *skipListener=nullptr) override
This method can be used to update whole columns with varying data.
int getAttributeCount() const override
attribute discovery, overridden to pass on requests
int getAttributeIndex(const std::string &name) override
Get index of attribute with the given name.
Variant getAttributeDefault(int index) const override
Return default value of n-th attribute.
unsigned int getChildCount(const ModelIndex &parent) override
Get number of children of the given item, an invalid index means number of top-level items.
bool hasChildren(const ModelIndex &parent) override
Or rather mightHaveChildren.
Variant getData(const ModelIndex &item, int attributeIndex) override
data access, overridden to apply filter mapping
ItemModelItemFilter(AbstractItemModelPtr sourceModel, FilterMode filterMode=DirectMatch)
ModelIndex getParent(const ModelIndex &child) override
Get parent of an item.
bool isFlat() const override
item traversal interface, overridden for filtering
virtual bool isFilteredAttribute(int attributeIndex) const
Returns if an attribute is involved in the filtering, returns true by default (this is for optimizati...
bool bulkSetData(const std::vector< ModelIndex > &items, int attributeIndex, const Variant &data, void *skipListener=nullptr) override
bulkSetData will be used by views to set selection state to an attribute, so that we only get a singl...
~ItemModelItemFilter() override
bool setData(const ModelIndex &item, int attributeIndex, const Variant &data, void *skipListener=nullptr) override
Update a single attribute on a single item.
ModelIndex getChild(const ModelIndex &parent, unsigned int index) override
Get n-th child of an item (or n-th top-level item if parent index is invalid).
bool rawSetData(const ModelIndex &item, int attributeIndex, const Variant &data) override
This will be called by setData and bulkSetData, which will do the necessary notifications afterwards;...
This event informs about attributes that have changed their values.
This event informs about inserted items.
This event informs about removed items.
This class serves as an index into an AbstractItemModel.
size_t userID() const
access the internal user value (same as user pointer)
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.