Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ObjectInfoListModel.hh
1 #pragma once
2 
3 #include <algorithm>
4 #include <utility>
5 
11 
12 #include <QAbstractItemModel>
13 
15  bool operator()(BaseObjectData *object) const
16  {
17  if (object->source()) {
18  if (object->dataType(DATA_HEXAHEDRAL_MESH) ||
19  object->dataType(DATA_POLYHEDRAL_MESH) ||
20  object->dataType(DATA_TRIANGLE_MESH)) {
21  return true;
22  }
23  }
24  return false;
25  }
26 };
27 
29  bool operator()(BaseObjectData *object) const
30  {
31  if (object->target()) {
32  if (object->dataType(DATA_HEXAHEDRAL_MESH) ||
33  object->dataType(DATA_POLYHEDRAL_MESH) ||
34  object->dataType(DATA_TRIANGLE_MESH)) {
35  return true;
36  }
37  }
38  return false;
39  }
40 };
41 
42 template< typename TPredicate >
43 class ObjectInfoListModel : public QAbstractListModel
44 {
45  typedef std::vector<std::pair<QString, int> > Object_container;
46 
47 public:
48  int rowCount(QModelIndex const&) const
49  {
50  return objects_.size() + 1;
51  }
52 
53  QVariant data(QModelIndex const& index, int role = Qt::DisplayRole) const
54  {
55  switch (role) {
56  case Qt::DisplayRole:
57  if (index.row() <= 0) {
58  return tr("<none>");
59  }
60  return objects_[index.row() - 1].first;
61  case Qt::UserRole:
62  if (index.row() <= 0) {
63  return QVariant::fromValue<int>(-1);
64  }
65  return QVariant::fromValue(objects_[index.row() - 1].second);
66  default:
67  return QVariant::Invalid;
68  }
69  }
70 
71  void removeObject(int object_id)
72  {
73  bool found = false;
74  for (unsigned int i = 0; i < objects_.size(); ++i) {
75  if (objects_[i].second == object_id) {
76  found = true;
77  break;
78  }
79  }
80 
81  if (!found) {
82  return;
83  }
84 
85  beginResetModel();
86  for (int i = objects_.size() - 1; i >= 0; --i) {
87  if (objects_[i].second == object_id) {
88  objects_.erase(objects_.begin() + i);
89  }
90  }
91  endResetModel();
92  }
93 
94  void refresh()
95  {
96  Object_container objects;
97  for (PluginFunctions::ObjectIterator o_it; o_it != PluginFunctions::objectsEnd(); ++o_it) {
98  if (predicate_(*o_it)) {
99  objects.push_back(std::make_pair(o_it->name(), o_it->id()));
100  }
101  }
102  std::sort(objects.begin(), objects.end());
103  if (objects != objects_) {
104  beginResetModel();
105  objects.swap(objects_);
106  endResetModel();
107  }
108  }
109 
110 private:
111  Object_container objects_;
112  TPredicate predicate_;
113 };
bool dataType(DataType _type) const
Definition: BaseObject.cc:232
bool source()
Definition: BaseObject.cc:302
#define DATA_POLYHEDRAL_MESH
bool target()
Definition: BaseObject.cc:284
#define DATA_TRIANGLE_MESH
Definition: TriangleMesh.hh:66
DLLEXPORT ObjectIterator objectsEnd()
Return Iterator to Object End.
#define DATA_HEXAHEDRAL_MESH