Tulip  4.4.0
Better Visualization Through Research
 All Classes Namespaces 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 
35  if (propName == "viewMetaGraph")
36  continue;
37 
38 #endif
39  PROPTYPE* prop = dynamic_cast<PROPTYPE*>(_graph->getProperty(propName));
40 
41  if (prop != NULL) {
42  _properties+=prop;
43  }
44  }
45  forEach(propName,_graph->getLocalProperties()) {
46 #ifdef NDEBUG
47 
48  if (propName == "viewMetaGraph")
49  continue;
50 
51 #endif
52  PROPTYPE* prop = dynamic_cast<PROPTYPE*>(_graph->getProperty(propName));
53 
54  if (prop != NULL) {
55  _properties+=prop;
56  }
57  }
58 }
59 
60 template<typename PROPTYPE>
61 GraphPropertiesModel<PROPTYPE>::GraphPropertiesModel(tlp::Graph* graph, bool checkable, QObject *parent): tlp::TulipModel(parent), _graph(graph), _placeholder(QString::null), _checkable(checkable), _removingRows(false), forcingRedraw(false) {
62  if (_graph != NULL) {
63  _graph->addListener(this);
64  rebuildCache();
65  }
66 }
67 
68 template<typename PROPTYPE>
69 GraphPropertiesModel<PROPTYPE>::GraphPropertiesModel(QString placeholder, tlp::Graph* graph, bool checkable, QObject *parent): tlp::TulipModel(parent), _graph(graph), _placeholder(placeholder), _checkable(checkable), _removingRows(false), forcingRedraw(false) {
70  if (_graph != NULL) {
71  _graph->addListener(this);
72  rebuildCache();
73  }
74 }
75 
76 template<typename PROPTYPE>
77 QModelIndex GraphPropertiesModel<PROPTYPE>::index(int row, int column,const QModelIndex &parent) const {
78  if (_graph == NULL || !hasIndex(row,column,parent))
79  return QModelIndex();
80 
81  int vectorIndex = row;
82 
83  if (!_placeholder.isNull()) {
84  if (row == 0)
85  return createIndex(row,column);
86 
87  vectorIndex--;
88  }
89 
90  return createIndex(row,column,_properties[vectorIndex]);
91 }
92 
93 template<typename PROPTYPE>
94 QModelIndex GraphPropertiesModel<PROPTYPE>::parent(const QModelIndex &) const {
95  return QModelIndex();
96 }
97 
98 template<typename PROPTYPE>
99 int GraphPropertiesModel<PROPTYPE>::rowCount(const QModelIndex &parent) const {
100  if (parent.isValid() || _graph == NULL || forcingRedraw)
101  return 0;
102 
103  int result = _properties.size();
104 
105  if (!_placeholder.isNull())
106  result++;
107 
108  return result;
109 }
110 
111 template<typename PROPTYPE>
112 int GraphPropertiesModel<PROPTYPE>::columnCount(const QModelIndex &) const {
113  return 3;
114 }
115 
116 template<typename PROPTYPE>
117 QVariant GraphPropertiesModel<PROPTYPE>::data(const QModelIndex &index, int role) const {
118  if (_graph == NULL || (index.internalPointer() == NULL && index.row() != 0))
119  return QVariant();
120 
121  PropertyInterface* pi = (PropertyInterface*)(index.internalPointer());
122 
123  if (role == Qt::DisplayRole || role == Qt::ToolTipRole) {
124  if (!_placeholder.isNull() && index.row() == 0)
125  return _placeholder;
126 
127  if (pi == NULL)
128  return QString();
129 
130  if (index.column() == 0)
131  return QString::fromUtf8(pi->getName().c_str());
132  else if (index.column() == 1)
133  return pi->getTypename().c_str();
134  else if (index.column() == 2)
135  return (_graph->existLocalProperty(pi->getName()) ? trUtf8("Local") : tr("Inherited from graph ") + QString::number(pi->getGraph()->getId()) + " (" + QString::fromUtf8(pi->getGraph()->getName().c_str()) + ')');
136  }
137 
138  else if (role == Qt::DecorationRole && index.column() == 0 && pi != NULL && !_graph->existLocalProperty(pi->getName()))
139  return QIcon(":/tulip/gui/ui/inherited_properties.png");
140 
141  else if (role == Qt::FontRole) {
142  QFont f;
143 
144  if (!_placeholder.isNull() && index.row() == 0)
145  f.setItalic(true);
146 
147  return f;
148  }
149  else if (role == PropertyRole) {
150  return QVariant::fromValue<PropertyInterface*>(pi);
151  }
152  else if (_checkable && role == Qt::CheckStateRole && index.column() == 0) {
153  return (_checkedProperties.contains((PROPTYPE*) pi) ? Qt::Checked : Qt::Unchecked);
154  }
155 
156  return QVariant();
157 }
158 
159 template<typename PROPTYPE>
160 int GraphPropertiesModel<PROPTYPE>::rowOf(PROPTYPE* pi) const {
161  int result = _properties.indexOf(pi);
162 
163  if (!_placeholder.isNull())
164  ++result;
165 
166  return result;
167 }
168 
169 template<typename PROPTYPE>
170 QVariant tlp::GraphPropertiesModel<PROPTYPE>::headerData(int section, Qt::Orientation orientation, int role) const {
171  if (orientation == Qt::Horizontal) {
172  if (role == Qt::DisplayRole) {
173  if (section == 0)
174  return trUtf8("Name");
175  else if (section == 1)
176  return trUtf8("Type");
177  else if (section == 2)
178  return trUtf8("Scope");
179  }
180  }
181 
182  return TulipModel::headerData(section,orientation,role);
183 }
184 
185 template<typename PROPTYPE>
186 bool tlp::GraphPropertiesModel<PROPTYPE>::setData(const QModelIndex &index, const QVariant &value, int role) {
187  if (_graph == NULL)
188  return false;
189 
190  if (_checkable && role == Qt::CheckStateRole && index.column() == 0) {
191  if (value.value<int>() == (int)Qt::Checked)
192  _checkedProperties.insert((PROPTYPE*)index.internalPointer());
193  else
194  _checkedProperties.remove((PROPTYPE*)index.internalPointer());
195 
196  emit checkStateChanged(index,(Qt::CheckState)(value.value<int>()));
197  return true;
198  }
199 
200  return false;
201 }
202 
203 }