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