Tulip  5.3.0
Large graphs analysis and drawing
GraphPropertiesModel.h
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 ///@cond DOXYGEN_HIDDEN
20 
21 #ifndef GRAPHPROPERTIESMODEL_H
22 #define GRAPHPROPERTIESMODEL_H
23 
24 #include <tulip/TulipModel.h>
25 #include <tulip/Observable.h>
26 #include <tulip/Graph.h>
27 
28 #include <QFont>
29 #include <QIcon>
30 #include <QSet>
31 
32 namespace tlp {
33 
34 template <typename PROPTYPE>
35 class GraphPropertiesModel : public tlp::TulipModel, public tlp::Observable {
36  tlp::Graph *_graph;
37  QString _placeholder;
38  bool _checkable;
39  QSet<PROPTYPE *> _checkedProperties;
40  QVector<PROPTYPE *> _properties;
41  bool _removingRows;
42  bool forcingRedraw;
43 
44  void rebuildCache();
45 
46 public:
47  explicit GraphPropertiesModel(tlp::Graph *graph, bool checkable = false,
48  QObject *parent = nullptr);
49  explicit GraphPropertiesModel(QString placeholder, tlp::Graph *graph, bool checkable = false,
50  QObject *parent = nullptr);
51  ~GraphPropertiesModel() override {
52  if (_graph != nullptr)
53  _graph->removeListener(this);
54  }
55 
56  tlp::Graph *graph() const {
57  return _graph;
58  }
59 
60  void setGraph(tlp::Graph *graph) {
61  if (_graph == graph)
62  return;
63 
64  beginResetModel();
65 
66  if (_graph != nullptr)
67  _graph->removeListener(this);
68 
69  _graph = graph;
70 
71  if (_graph != nullptr)
72  _graph->addListener(this);
73 
74  rebuildCache();
75  endResetModel();
76  }
77 
78  QSet<PROPTYPE *> checkedProperties() const {
79  return _checkedProperties;
80  }
81 
82  // Methods re-implemented from QAbstractItemModel
83  QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
84  QModelIndex parent(const QModelIndex &child) const override;
85  int rowCount(const QModelIndex &parent = QModelIndex()) const override;
86  int columnCount(const QModelIndex &parent = QModelIndex()) const override;
87  QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
88  QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
89  bool setData(const QModelIndex &quiindex, const QVariant &value, int role) override;
90 
91  // Methods inherited from the observable system
92  void treatEvent(const tlp::Event &evt) override {
93  if (evt.type() == Event::TLP_DELETE) {
94  // calls to *ResetModel() functions below
95  // are not needed because they may cause a Free Memory Read.
96  // However the current model will be soon deleted
97  // beginResetModel();
98  _graph = nullptr;
99  _properties.clear();
100  // endResetModel();
101  return;
102  }
103 
104  const GraphEvent *graphEvent = dynamic_cast<const GraphEvent *>(&evt);
105 
106  if (graphEvent == nullptr)
107  return;
108 
109  if (graphEvent->getType() == GraphEvent::TLP_BEFORE_DEL_LOCAL_PROPERTY ||
110  graphEvent->getType() == GraphEvent::TLP_BEFORE_DEL_INHERITED_PROPERTY) {
111 
112  PROPTYPE *prop = dynamic_cast<PROPTYPE *>(_graph->getProperty(graphEvent->getPropertyName()));
113 
114  if (prop != nullptr) {
115  int row = rowOf(prop);
116  if (row > -1) {
117  beginRemoveRows(QModelIndex(), row, row);
118  _properties.remove(_properties.indexOf(prop));
119  _removingRows = true;
120  _checkedProperties.remove(prop);
121  }
122  }
123  } else if (graphEvent->getType() == GraphEvent::TLP_AFTER_DEL_LOCAL_PROPERTY ||
124  graphEvent->getType() == GraphEvent::TLP_AFTER_DEL_INHERITED_PROPERTY) {
125  if (_removingRows) {
126  endRemoveRows();
127  _removingRows = false;
128  }
129  } else if (graphEvent->getType() == GraphEvent::TLP_ADD_LOCAL_PROPERTY ||
130  graphEvent->getType() == GraphEvent::TLP_ADD_INHERITED_PROPERTY) {
131  PROPTYPE *prop = dynamic_cast<PROPTYPE *>(_graph->getProperty(graphEvent->getPropertyName()));
132 
133  if (prop != nullptr) {
134  rebuildCache();
135  int row = rowOf(prop);
136 
137  if (row > -1) {
138  beginInsertRows(QModelIndex(), row, row);
139  endInsertRows();
140  }
141  }
142  } else if (graphEvent->getType() == GraphEvent::TLP_AFTER_RENAME_LOCAL_PROPERTY) {
143  // force any needed sorting
144  emit layoutAboutToBeChanged();
145  changePersistentIndex(createIndex(0, 0), createIndex(_properties.size() - 1, 0));
146  emit layoutChanged();
147  }
148  }
149 
150  int rowOf(PROPTYPE *) const;
151 
152  int rowOf(const QString &pName) const;
153 
154  Qt::ItemFlags flags(const QModelIndex &index) const override {
155  Qt::ItemFlags result = QAbstractItemModel::flags(index);
156 
157  if (index.column() == 0 && _checkable)
158  result |= Qt::ItemIsUserCheckable;
159 
160  return result;
161  }
162 };
163 } // namespace tlp
164 
165 #include "cxx/GraphPropertiesModel.cxx"
166 
167 #endif // GRAPHPROPERTIESMODEL_H
168 ///@endcond
virtual PropertyInterface * getProperty(const std::string &name) const =0
Gets an existing property. In DEBUG mode an assertion checks the existence of the property...
void removeListener(Observable *const listener) const
Removes a listener from this object.
Event is the base class for all events used in the Observation mechanism.
Definition: Observable.h:51
virtual void clear()=0
Removes all nodes, edges and subgraphs from this graph.
The Observable class is the base of Tulip&#39;s observation system.
Definition: Observable.h:141
void addListener(Observable *const listener) const
Adds a Listener to this object.