Tulip  4.1.0
Better Visualization Through Research
 All Classes Files Functions Variables Enumerations Enumerator Properties Groups Pages
GraphPropertiesModel.cxx
1 /*
2  *
3  * This file is part of Tulip (www.tulip-software.org)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux 1 and Inria Bordeaux - Sud Ouest
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 
20 #include <tulip/Graph.h>
21 
22 namespace tlp {
23 
24 template<typename PROPTYPE>
25 void tlp::GraphPropertiesModel<PROPTYPE>::rebuildCache() {
26  _properties.clear();
27 
28  if (_graph == NULL)
29  return;
30 
31  std::string propName;
32  forEach(propName,_graph->getInheritedProperties()) {
33 #ifdef NDEBUG
34  if (propName == "viewMetaGraph")
35  continue;
36 #endif
37  PROPTYPE* prop = dynamic_cast<PROPTYPE*>(_graph->getProperty(propName));
38 
39  if (prop != NULL) {
40  _properties+=prop;
41  }
42  }
43  forEach(propName,_graph->getLocalProperties()) {
44 #ifdef NDEBUG
45 
46  if (propName == "viewMetaGraph")
47  continue;
48 
49 #endif
50  PROPTYPE* prop = dynamic_cast<PROPTYPE*>(_graph->getProperty(propName));
51 
52  if (prop != NULL) {
53  _properties+=prop;
54  }
55  }
56 }
57 
58 template<typename PROPTYPE>
59 GraphPropertiesModel<PROPTYPE>::GraphPropertiesModel(tlp::Graph* graph, bool checkable, QObject *parent): tlp::TulipModel(parent), _graph(graph), _placeholder(QString::null), _checkable(checkable), _removingRows(false) {
60  if (_graph != NULL) {
61  _graph->addListener(this);
62  rebuildCache();
63  }
64 }
65 
66 template<typename PROPTYPE>
67 GraphPropertiesModel<PROPTYPE>::GraphPropertiesModel(QString placeholder, tlp::Graph* graph, bool checkable, QObject *parent): tlp::TulipModel(parent), _graph(graph), _placeholder(placeholder), _checkable(checkable), _removingRows(false) {
68  if (_graph != NULL) {
69  _graph->addListener(this);
70  rebuildCache();
71  }
72 }
73 
74 template<typename PROPTYPE>
75 QModelIndex GraphPropertiesModel<PROPTYPE>::index(int row, int column,const QModelIndex &parent) const {
76  if (_graph == NULL || !hasIndex(row,column,parent))
77  return QModelIndex();
78 
79  int vectorIndex = row;
80 
81  if (!_placeholder.isNull()) {
82  if (row == 0)
83  return createIndex(row,column);
84 
85  vectorIndex--;
86  }
87 
88  return createIndex(row,column,_properties[vectorIndex]);
89 }
90 
91 template<typename PROPTYPE>
92 QModelIndex GraphPropertiesModel<PROPTYPE>::parent(const QModelIndex &) const {
93  return QModelIndex();
94 }
95 
96 template<typename PROPTYPE>
97 int GraphPropertiesModel<PROPTYPE>::rowCount(const QModelIndex &parent) const {
98  if (parent.isValid() || _graph == NULL)
99  return 0;
100 
101  int result = _properties.size();
102 
103  if (!_placeholder.isNull())
104  result++;
105 
106  return result;
107 }
108 
109 template<typename PROPTYPE>
110 int GraphPropertiesModel<PROPTYPE>::columnCount(const QModelIndex &) const {
111  return 3;
112 }
113 
114 template<typename PROPTYPE>
115 QVariant GraphPropertiesModel<PROPTYPE>::data(const QModelIndex &index, int role) const {
116  if (_graph == NULL || (index.internalPointer() == NULL && index.row() != 0))
117  return QVariant();
118 
119  PROPTYPE* pi = (PROPTYPE*)(index.internalPointer());
120 
121  if (role == Qt::DisplayRole || role == Qt::ToolTipRole) {
122  if (!_placeholder.isNull() && index.row() == 0)
123  return _placeholder;
124 
125  if (pi == NULL)
126  return QString();
127 
128  if (index.column() == 0)
129  return QString::fromUtf8(pi->getName().c_str());
130  else if (index.column() == 1)
131  return pi->getTypename().c_str();
132  else if (index.column() == 2)
133  return (_graph->existLocalProperty(pi->getName()) ? trUtf8("Local") : trUtf8("Inherited"));
134  }
135 
136  else if (role == Qt::DecorationRole && index.column() == 0 && pi != NULL && !_graph->existLocalProperty(pi->getName()))
137  return QIcon(":/tulip/gui/ui/inherited_properties.png");
138 
139  else if (role == Qt::FontRole) {
140  QFont f;
141 
142  if (!_placeholder.isNull() && index.row() == 0)
143  f.setItalic(true);
144 
145  return f;
146  }
147  else if (role == PropertyRole) {
148  return QVariant::fromValue<PropertyInterface*>(pi);
149  }
150  else if (_checkable && role == Qt::CheckStateRole && index.column() == 0) {
151  return (_checkedProperties.contains(pi) ? Qt::Checked : Qt::Unchecked);
152  }
153 
154  return QVariant();
155 }
156 
157 template<typename PROPTYPE>
158 int GraphPropertiesModel<PROPTYPE>::rowOf(PROPTYPE* pi) const {
159  return _properties.indexOf(pi);
160 }
161 
162 template<typename PROPTYPE>
163 QVariant tlp::GraphPropertiesModel<PROPTYPE>::headerData(int section, Qt::Orientation orientation, int role) const {
164  if (orientation == Qt::Horizontal) {
165  if (role == Qt::DisplayRole) {
166  if (section == 0)
167  return trUtf8("Name");
168  else if (section == 1)
169  return trUtf8("Type");
170  else if (section == 2)
171  return trUtf8("Scope");
172  }
173  }
174 
175  return TulipModel::headerData(section,orientation,role);
176 }
177 
178 template<typename PROPTYPE>
179 bool tlp::GraphPropertiesModel<PROPTYPE>::setData(const QModelIndex &index, const QVariant &value, int role) {
180  if (_graph == NULL)
181  return false;
182 
183  if (_checkable && role == Qt::CheckStateRole && index.column() == 0) {
184  if (value.value<int>() == (int)Qt::Checked)
185  _checkedProperties.insert((PROPTYPE*)index.internalPointer());
186  else
187  _checkedProperties.remove((PROPTYPE*)index.internalPointer());
188 
189  emit checkStateChanged(index,(Qt::CheckState)(value.value<int>()));
190  return true;
191  }
192 
193  return false;
194 }
195 
196 }