Developer Documentation
PropertyNameListModel.hh
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43 * *
44 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 #ifndef PROPERTYNAMELISTMODEL_HH_
51 #define PROPERTYNAMELISTMODEL_HH_
52 
53 #include <OpenMesh/Core/Utils/BaseProperty.hh>
54 
55 #include <QAbstractListModel>
56 
57 #include <set>
58 #include <vector>
59 #include <algorithm>
60 
61 class PropertyNameListModel: public QAbstractListModel {
62  public:
63  enum ENTITY_FILTER {
64  EF_ANY = 0x0F,
65  EF_FACE = 0x01,
66  EF_EDGE = 0x02,
67  EF_HALFEDGE = 0x04,
68  EF_VERTEX = 0x08,
69  };
70 
71  static const char *entity2str(ENTITY_FILTER entity);
72 
74  public:
75  TypeInfoWrapper(const std::type_info & ti, const char *friendlyName) : ti(&ti), friendlyName(friendlyName) {}
76  TypeInfoWrapper(const std::type_info & ti) : ti(&ti),friendlyName("") {}
77 
78  operator const std::type_info *() const { return ti; }
79  operator const std::type_info &() const { return *ti; }
80  operator const char *() const { return friendlyName; }
81 
82  const std::type_info *operator->() const { return ti; }
83  const std::type_info &get() const { return *ti; }
84  const char *getName() const { return friendlyName; }
85 
86  bool operator==(const TypeInfoWrapper & other) const {
87  /*
88  * We compare name strings, not the type ids themselves because
89  * the same type from different SOs will give different type ids.
90  */
91  return strcmp(ti->name(), other.ti->name()) == 0;
92  }
93 
94  bool operator<(const TypeInfoWrapper & other) const {
95  return strcmp(ti->name(), other.ti->name()) < 0;
96  }
97 
98  private:
99  const std::type_info *ti;
100  const char *friendlyName;
101  };
102 
103  class PROP_INFO {
104  public:
105  PROP_INFO(const std::string &propName, const TypeInfoWrapper &typeinfo, ENTITY_FILTER entity) :
106  propName_(propName), typeinfo_(typeinfo), entity(entity) {}
107 
108  QString toString() const {
109  return tr("%3 %1 : %2").arg(propName_.c_str()).arg(friendlyTypeName()).arg(QString::fromUtf8(entity2str(entity)));
110  }
111 
112  bool operator==(const PROP_INFO &rhs) const {
113  return propName_ == rhs.propName_ && typeinfo_ == rhs.typeinfo_ && entity == rhs.entity;
114  }
115 
116  bool operator<(const PROP_INFO &rhs) const {
117  if (entity != rhs.entity) return entity < rhs.entity;
118 
119  int result = propName_.compare(rhs.propName_);
120  if (result) return result < 0;
121 
122  return typeinfo_ < rhs.typeinfo_;
123  }
124 
125  inline bool isFaceProp() const { return entity == EF_FACE; }
126  inline bool isEdgeProp() const { return entity == EF_EDGE; }
127  inline bool isHalfedgeProp() const { return entity == EF_HALFEDGE; }
128  inline bool isVertexProp() const { return entity == EF_VERTEX; }
129 
130  inline const std::string &propName() const { return propName_; }
131  inline const char *friendlyTypeName() const { return typeinfo_.getName(); }
132  inline const TypeInfoWrapper &typeinfo() const { return typeinfo_; }
133  inline ENTITY_FILTER entityType() const { return entity; }
134 
135  private:
136  std::string propName_;
137  TypeInfoWrapper typeinfo_;
138  ENTITY_FILTER entity;
139  };
140 
141  public:
142  PropertyNameListModel(QObject *parent = 0);
143  virtual ~PropertyNameListModel();
144 
145  virtual int rowCount(const QModelIndex & parent = QModelIndex()) const;
146  virtual QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
147  virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
148 
154  template<typename MeshT>
155  void refresh(MeshT *mesh, ENTITY_FILTER entityFilterMask, std::type_info *typeIdFilter) {
156  std::vector<PROP_INFO> propList;
157 
158  if (mesh) {
159  if (entityFilterMask & EF_FACE)
160  gatherProperties(mesh, mesh->fprops_begin(), mesh->fprops_end(), typeIdFilter, std::back_inserter(propList), EF_FACE);
161 
162  if (entityFilterMask & EF_EDGE)
163  gatherProperties(mesh, mesh->eprops_begin(), mesh->eprops_end(), typeIdFilter, std::back_inserter(propList), EF_EDGE);
164 
165  if (entityFilterMask & EF_HALFEDGE)
166  gatherProperties(mesh, mesh->hprops_begin(), mesh->hprops_end(), typeIdFilter, std::back_inserter(propList), EF_HALFEDGE);
167 
168  if (entityFilterMask & EF_VERTEX)
169  gatherProperties(mesh, mesh->vprops_begin(), mesh->vprops_end(), typeIdFilter, std::back_inserter(propList), EF_VERTEX);
170  }
171 
172  std::sort(propList.begin(), propList.end());
173 
174  if (propList != propList_) {
175  if (!tryInsertionsDeletions(propList)) {
176  beginResetModel();
177  propList_.swap(propList);
178  endResetModel();
179  }
180  }
181  }
182 
188  bool tryInsertionsDeletions(std::vector<PROP_INFO> &propList);
189 
190  template<typename MeshT, typename OUTPUT_ITERATOR>
191  void gatherProperties(MeshT *mesh,
192  typename MeshT::prop_iterator props_first,
193  typename MeshT::prop_iterator props_last,
194  std::type_info *typeIdFilter, OUTPUT_ITERATOR oit,
195  ENTITY_FILTER entity) {
196 
197  for (typename MeshT::prop_iterator pit = props_first; pit != props_last; ++pit) {
198  OpenMesh::BaseProperty * const baseProp = *pit;
199  if (!baseProp) continue;
200 
201  TypeInfoWrapper bp_type = typeid(*baseProp);
202  TYPE_INFO_SET::const_iterator sane_prop_it = sane_prop_types.find(bp_type);
203  if ((typeIdFilter == 0 || TypeInfoWrapper(*typeIdFilter) == bp_type) && sane_prop_it != sane_prop_types.end()) {
204  *oit++ = PROP_INFO(baseProp->name(), *sane_prop_it, entity);
205  }
206  }
207  }
208 
209  const PROP_INFO &operator[] (size_t index) {
210  return propList_[index];
211  }
212 
213  protected:
214  std::vector<PROP_INFO> propList_;
215 
216  public:
217  static const TypeInfoWrapper proptype_bool;
218  static const TypeInfoWrapper proptype_int;
219  static const TypeInfoWrapper proptype_uint;
220  static const TypeInfoWrapper proptype_double;
221  static const TypeInfoWrapper proptype_Vec3d;
222  static const TypeInfoWrapper proptype_Vec3f;
223  static const TypeInfoWrapper proptype_Vec2d;
224  static const TypeInfoWrapper proptype_Vec2f;
225  static const TypeInfoWrapper proptype_SkinWeights;
226 
227  private:
228  typedef std::set<TypeInfoWrapper> TYPE_INFO_SET;
229  static const TypeInfoWrapper prop_types[];
230  static const TYPE_INFO_SET sane_prop_types;
231 };
232 
233 #endif /* PROPERTYNAMELISTMODEL_HH_ */
bool tryInsertionsDeletions(std::vector< PROP_INFO > &propList)
const std::string & name() const
Return the name of the property.
void refresh(MeshT *mesh, ENTITY_FILTER entityFilterMask, std::type_info *typeIdFilter)