Developer Documentation
PropertyNameListModel.cc
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 #include "PropertyNameListModel.hh"
45 
46 #include <OpenMesh/Core/Utils/Property.hh>
47 #include <ACG/Math/VectorT.hh>
48 #include <ACG/Math/Matrix3x3T.hh>
49 
50 #include <iostream>
51 
52 const PropertyNameListModel::TypeInfoWrapper PropertyNameListModel::proptype_bool =
54 const PropertyNameListModel::TypeInfoWrapper PropertyNameListModel::proptype_int =
56 const PropertyNameListModel::TypeInfoWrapper PropertyNameListModel::proptype_uint =
58 const PropertyNameListModel::TypeInfoWrapper PropertyNameListModel::proptype_double =
60 const PropertyNameListModel::TypeInfoWrapper PropertyNameListModel::proptype_Vec3d =
62 const PropertyNameListModel::TypeInfoWrapper PropertyNameListModel::proptype_Vec3f =
64 const PropertyNameListModel::TypeInfoWrapper PropertyNameListModel::proptype_Vec2d =
66 const PropertyNameListModel::TypeInfoWrapper PropertyNameListModel::proptype_Vec2f =
68 const PropertyNameListModel::TypeInfoWrapper PropertyNameListModel::proptype_Matrix3x3d =
70 
71 #ifdef ENABLE_SKELETON_SUPPORT
72  #include <ObjectTypes/Skeleton/BaseSkin.hh>
73  const PropertyNameListModel::TypeInfoWrapper PropertyNameListModel::proptype_SkinWeights =
75 #endif
76 
77 /*
78  * I'd love to do this with boost::assign but I'm not allowed to. :-(
79  */
80 
81 const PropertyNameListModel::TypeInfoWrapper PropertyNameListModel::prop_types[] = {
82  proptype_bool,
83  proptype_int,
84  proptype_uint,
85  proptype_double,
86  proptype_Vec3d,
87  proptype_Vec3f,
88  proptype_Vec2d,
89  proptype_Vec2f,
90 #ifdef ENABLE_SKELETON_SUPPORT
91  proptype_SkinWeights,
92 #endif
93 };
94 
95 #ifdef ENABLE_SKELETON_SUPPORT
96 const PropertyNameListModel::TYPE_INFO_SET PropertyNameListModel::sane_prop_types(prop_types, prop_types + 9);
97 #else
98 const PropertyNameListModel::TYPE_INFO_SET PropertyNameListModel::sane_prop_types(prop_types, prop_types + 8);
99 #endif
100 
101 const char *PropertyNameListModel::entity2str(ENTITY_FILTER entity) {
102  switch (entity) {
103  case EF_EDGE:
104  return "→";
105  case EF_FACE:
106  return "△";
107  case EF_HALFEDGE:
108  return "⇀";
109  case EF_VERTEX:
110  return "•";
111  default:
112  return "error";
113  }
114 }
115 
116 
117 PropertyNameListModel::PropertyNameListModel(QObject *parent) :
118  QAbstractListModel(parent) {
119 }
120 
121 PropertyNameListModel::~PropertyNameListModel() {
122 }
123 
124 int PropertyNameListModel::rowCount(const QModelIndex & parent) const {
125  return propList_.size();
126 }
127 
128 QVariant PropertyNameListModel::data(const QModelIndex & index, int role) const {
129  switch (role) {
130  case Qt::DisplayRole:
131  return propList_[index.row()].toString();
132  default:
133  return QVariant::Invalid;
134  }
135 }
136 
137 QVariant PropertyNameListModel::headerData(int section, Qt::Orientation orientation, int role) const {
138  switch (role) {
139  case Qt::DisplayRole:
140  return tr("Some header. %1 %2").arg(section).arg(orientation);
141  break;
142  default:
143  return QAbstractListModel::headerData(section, orientation, role);
144  }
145 }
146 
147 namespace {
148 class InsDel {
149  public:
150  enum OP { INS, DEL };
151  InsDel(OP op, int first, int count, int before) : op(op), first(first), count(count), before(before) {}
152  OP op;
153  int first, count, before;
154 };
155 }
156 
157 bool PropertyNameListModel::tryInsertionsDeletions(std::vector<PROP_INFO> &propList) {
158 
159  std::vector<InsDel> result;
160  typedef std::vector<PROP_INFO>::iterator IT;
161  int correction = 0;
162  IT oldIt, newIt;
163  for (oldIt = propList_.begin(), newIt = propList.begin(); oldIt < propList_.end() && newIt < propList.end(); ++oldIt, ++newIt) {
164  if (*oldIt == *newIt) continue;
165  const IT nextNew = std::find(newIt+1, propList.end(), *oldIt);
166  if (nextNew != propList.end()) {
167  const int count = std::distance(newIt, nextNew);
168  result.push_back(InsDel(InsDel::INS, std::distance(propList.begin(), newIt), count, std::distance(propList_.begin(), oldIt) + correction));
169  correction += count;
170  newIt += count;
171  continue;
172  }
173  const IT nextOld = std::find(oldIt+1, propList_.end(), *newIt);
174  if (nextOld != propList_.end()) {
175  const int count = std::distance(oldIt, nextOld);
176  result.push_back(InsDel(InsDel::DEL, std::distance(propList_.begin(), oldIt) + correction, count, 0));
177  correction -= count;
178  oldIt += count;
179  continue;
180  }
181  return false;
182  }
183  if (oldIt < propList_.end() && newIt < propList.end()) {
184  return false;
185  }
186 
187  if (oldIt < propList_.end())
188  result.push_back(InsDel(InsDel::DEL, std::distance(propList_.begin(), oldIt) + correction, std::distance(oldIt, propList_.end()), 0));
189  if (newIt < propList.end())
190  result.push_back(InsDel(InsDel::INS, std::distance(propList.begin(), newIt), std::distance(newIt, propList.end()), propList_.size() + correction));
191 
192  for (std::vector<InsDel>::iterator it = result.begin(); it != result.end(); ++it) {
193  if (it->op == InsDel::INS) {
194  beginInsertRows(QModelIndex(), it->before, it->before + it->count - 1);
195  propList_.insert(propList_.begin() + it->before, propList.begin() + it->first, propList.begin() + it->first + it->count);
196  endInsertRows();
197  } else {
198  beginRemoveRows(QModelIndex(), it->first, it->first + it->count - 1);
199  propList_.erase(propList_.begin() + it->first, propList_.begin() + it->first + it->count);
200  endRemoveRows();
201  }
202  }
203 
204  if (propList_ != propList) {
205  std::cerr << "Apparently, the function PropertyNameListModel::tryInsertionsDeletions() has an implementation error." << std::endl;
206  throw std::logic_error("Apparently, the function PropertyNameListModel::tryInsertionsDeletions() has an implementation error.");
207  }
208 
209  return true;
210 }
bool tryInsertionsDeletions(std::vector< PROP_INFO > &propList)
Default property class for any type T.
Definition: Property.hh:89