Tulip  5.1.0
Large graphs analysis and drawing
GraphPropertiesModel.cxx
1 /*
2  *
3  * This file is part of Tulip (http://tulip.labri.fr)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux
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 #include <tulip/TlpQtTools.h>
22 
23 namespace tlp {
24 
25 template<typename PROPTYPE>
26 void tlp::GraphPropertiesModel<PROPTYPE>::rebuildCache() {
27  _properties.clear();
28 
29  if (_graph == NULL)
30  return;
31 
32  std::string propName;
33  forEach(propName,_graph->getInheritedProperties()) {
34 #ifdef NDEBUG
35 
36  if (propName == "viewMetaGraph")
37  continue;
38 
39 #endif
40  PROPTYPE* prop = dynamic_cast<PROPTYPE*>(_graph->getProperty(propName));
41 
42  if (prop != NULL) {
43  _properties+=prop;
44  }
45  }
46  forEach(propName,_graph->getLocalProperties()) {
47 #ifdef NDEBUG
48 
49  if (propName == "viewMetaGraph")
50  continue;
51 
52 #endif
53  PROPTYPE* prop = dynamic_cast<PROPTYPE*>(_graph->getProperty(propName));
54 
55  if (prop != NULL) {
56  _properties+=prop;
57  }
58  }
59 }
60 
61 template<typename PROPTYPE>
62 GraphPropertiesModel<PROPTYPE>::GraphPropertiesModel(tlp::Graph* graph, bool checkable, QObject *parent): tlp::TulipModel(parent), _graph(graph), _checkable(checkable), _removingRows(false), forcingRedraw(false) {
63  if (_graph != NULL) {
64  _graph->addListener(this);
65  rebuildCache();
66  }
67 }
68 
69 template<typename PROPTYPE>
70 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) {
71  if (_graph != NULL) {
72  _graph->addListener(this);
73  rebuildCache();
74  }
75 }
76 
77 template<typename PROPTYPE>
78 QModelIndex GraphPropertiesModel<PROPTYPE>::index(int row, int column,const QModelIndex &parent) const {
79  if (_graph == NULL || !hasIndex(row,column,parent))
80  return QModelIndex();
81 
82  int vectorIndex = row;
83 
84  if (!_placeholder.isEmpty()) {
85  if (row == 0)
86  return createIndex(row,column);
87 
88  vectorIndex--;
89  }
90 
91  return createIndex(row,column,_properties[vectorIndex]);
92 }
93 
94 template<typename PROPTYPE>
95 QModelIndex GraphPropertiesModel<PROPTYPE>::parent(const QModelIndex &) const {
96  return QModelIndex();
97 }
98 
99 template<typename PROPTYPE>
100 int GraphPropertiesModel<PROPTYPE>::rowCount(const QModelIndex &parent) const {
101  if (parent.isValid() || _graph == NULL || forcingRedraw)
102  return 0;
103 
104  int result = _properties.size();
105 
106  if (!_placeholder.isEmpty())
107  result++;
108 
109  return result;
110 }
111 
112 template<typename PROPTYPE>
113 int GraphPropertiesModel<PROPTYPE>::columnCount(const QModelIndex &) const {
114  return 3;
115 }
116 
117 template<typename PROPTYPE>
118 QVariant GraphPropertiesModel<PROPTYPE>::data(const QModelIndex &index, int role) const {
119  if (_graph == NULL || (index.internalPointer() == NULL && index.row() != 0))
120  return QVariant();
121 
122  PropertyInterface* pi = (PropertyInterface*)(index.internalPointer());
123 
124  if (role == Qt::DisplayRole || role == Qt::ToolTipRole) {
125  if (!_placeholder.isEmpty() && index.row() == 0)
126  return _placeholder;
127 
128  if (pi == NULL)
129  return QString();
130 
131  if (index.column() == 0)
132  return tlpStringToQString(pi->getName());
133  else if (index.column() == 1)
134  return pi->getTypename().c_str();
135  else if (index.column() == 2)
136  return (_graph->existLocalProperty(pi->getName()) ? trUtf8("Local") : tr("Inherited from graph ") + QString::number(pi->getGraph()->getId()) + " (" + tlpStringToQString(pi->getGraph()->getName()) + ')');
137  }
138 
139  else if (role == Qt::DecorationRole && index.column() == 0 && pi != NULL && !_graph->existLocalProperty(pi->getName()))
140  return QIcon(":/tulip/gui/ui/inherited_properties.png");
141 
142  else if (role == Qt::FontRole) {
143  QFont f;
144 
145  if (!_placeholder.isEmpty() && index.row() == 0)
146  f.setItalic(true);
147 
148  return f;
149  }
150  else if (role == PropertyRole) {
151  return QVariant::fromValue<PropertyInterface*>(pi);
152  }
153  else if (_checkable && role == Qt::CheckStateRole && index.column() == 0) {
154  return (_checkedProperties.contains((PROPTYPE*) pi) ? Qt::Checked : Qt::Unchecked);
155  }
156 
157  return QVariant();
158 }
159 
160 template<typename PROPTYPE>
161 int GraphPropertiesModel<PROPTYPE>::rowOf(PROPTYPE* pi) const {
162  int result = _properties.indexOf(pi);
163 
164  if (!_placeholder.isEmpty())
165  ++result;
166 
167  return result;
168 }
169 
170 template<typename PROPTYPE>
171 int GraphPropertiesModel<PROPTYPE>::rowOf(const QString& pName) const {
172  for(int i = 0; i < _properties.size(); ++i) {
173  if (pName == tlpStringToQString(_properties[i]->getName()))
174  return i;
175  }
176 
177  return -1;
178 }
179 
180 template<typename PROPTYPE>
181 QVariant tlp::GraphPropertiesModel<PROPTYPE>::headerData(int section, Qt::Orientation orientation, int role) const {
182  if (orientation == Qt::Horizontal) {
183  if (role == Qt::DisplayRole) {
184  if (section == 0)
185  return trUtf8("Name");
186  else if (section == 1)
187  return trUtf8("Type");
188  else if (section == 2)
189  return trUtf8("Scope");
190  }
191  }
192 
193  return TulipModel::headerData(section,orientation,role);
194 }
195 
196 template<typename PROPTYPE>
197 bool tlp::GraphPropertiesModel<PROPTYPE>::setData(const QModelIndex &index, const QVariant &value, int role) {
198  if (_graph == NULL)
199  return false;
200 
201  if (_checkable && role == Qt::CheckStateRole && index.column() == 0) {
202  if (value.value<int>() == (int)Qt::Checked)
203  _checkedProperties.insert((PROPTYPE*)index.internalPointer());
204  else
205  _checkedProperties.remove((PROPTYPE*)index.internalPointer());
206 
207  emit checkStateChanged(index,(Qt::CheckState)(value.value<int>()));
208  return true;
209  }
210 
211  return false;
212 }
213 
214 }
QString tlpStringToQString(const std::string &toConvert)
Convert a Tulip UTF-8 encoded std::string to a QString.
Definition: TlpQtTools.h:54