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