Tulip 5.7.1
Large graphs analysis and drawing
Loading...
Searching...
No Matches
GraphPropertiesModel.cxx
1/*
2 *
3 * This file is part of Tulip (https://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
23namespace tlp {
24
25template <typename PROPTYPE>
26void 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
60template <typename PROPTYPE>
61GraphPropertiesModel<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
71template <typename PROPTYPE>
72GraphPropertiesModel<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
82template <typename PROPTYPE>
83QModelIndex 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
100template <typename PROPTYPE>
101QModelIndex GraphPropertiesModel<PROPTYPE>::parent(const QModelIndex &) const {
102 return QModelIndex();
103}
104
105template <typename PROPTYPE>
106int 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
118template <typename PROPTYPE>
119int GraphPropertiesModel<PROPTYPE>::columnCount(const QModelIndex &) const {
120 return 3;
121}
122
123template <typename PROPTYPE>
124QVariant 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 ? tr("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 QWidget *p = dynamic_cast<QWidget *>(QAbstractItemModel::parent());
155 if (p)
156 f = p->font();
157
158 if (!_placeholder.isEmpty() && index.row() == 0)
159 f.setItalic(true);
160
161 return f;
162 } else if (role == PropertyRole) {
163 return QVariant::fromValue<PropertyInterface *>(pi);
164 } else if (_checkable && role == Qt::CheckStateRole && index.column() == 0) {
165 return (_checkedProperties.contains(static_cast<PROPTYPE *>(pi)) ? Qt::Checked : Qt::Unchecked);
166 }
167
168 return QVariant();
169}
170
171template <typename PROPTYPE>
172int GraphPropertiesModel<PROPTYPE>::rowOf(PROPTYPE *pi) const {
173 int result = _properties.indexOf(pi);
174
175 if (result > -1 && !_placeholder.isEmpty())
176 ++result;
177
178 return result;
179}
180
181template <typename PROPTYPE>
182int GraphPropertiesModel<PROPTYPE>::rowOf(const QString &pName) const {
183 for (int i = 0; i < _properties.size(); ++i) {
184 if (pName == tlpStringToQString(_properties[i]->getName()))
185 return i;
186 }
187
188 return -1;
189}
190
191template <typename PROPTYPE>
192QVariant tlp::GraphPropertiesModel<PROPTYPE>::headerData(int section, Qt::Orientation orientation,
193 int role) const {
194 if (orientation == Qt::Horizontal) {
195 if (role == Qt::DisplayRole) {
196 if (section == 0)
197 return tr("Name");
198 else if (section == 1)
199 return tr("Type");
200 else if (section == 2)
201 return tr("Scope");
202 }
203 }
204
205 return TulipModel::headerData(section, orientation, role);
206}
207
208template <typename PROPTYPE>
209bool tlp::GraphPropertiesModel<PROPTYPE>::setData(const QModelIndex &index, const QVariant &value,
210 int role) {
211 if (_graph == nullptr)
212 return false;
213
214 if (_checkable && role == Qt::CheckStateRole && index.column() == 0) {
215 if (value.value<int>() == int(Qt::Checked))
216 _checkedProperties.insert(static_cast<PROPTYPE *>(index.internalPointer()));
217 else
218 _checkedProperties.remove(static_cast<PROPTYPE *>(index.internalPointer()));
219
220 emit checkStateChanged(index, static_cast<Qt::CheckState>(value.value<int>()));
221 return true;
222 }
223
224 return false;
225}
226} // namespace tlp
QString tlpStringToQString(const std::string &toConvert)
Convert a Tulip UTF-8 encoded std::string to a QString.
Definition: TlpQtTools.h:55