Tulip  5.2.1
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  beginRemoveRows(QModelIndex(), row, row);
117  _properties.remove(_properties.indexOf(prop));
118  _removingRows = true;
119  _checkedProperties.remove(prop);
120  }
121  } else if (graphEvent->getType() == GraphEvent::TLP_AFTER_DEL_LOCAL_PROPERTY ||
122  graphEvent->getType() == GraphEvent::TLP_AFTER_DEL_INHERITED_PROPERTY) {
123  if (_removingRows) {
124  endRemoveRows();
125  _removingRows = false;
126  }
127  } else if (graphEvent->getType() == GraphEvent::TLP_ADD_LOCAL_PROPERTY ||
128  graphEvent->getType() == GraphEvent::TLP_ADD_INHERITED_PROPERTY) {
129  PROPTYPE *prop = dynamic_cast<PROPTYPE *>(_graph->getProperty(graphEvent->getPropertyName()));
130 
131  if (prop != nullptr) {
132  rebuildCache();
133  int row = rowOf(prop);
134 
135  if (row >= 0) {
136  beginInsertRows(QModelIndex(), row, row);
137  endInsertRows();
138  }
139  }
140  } else if (graphEvent->getType() == GraphEvent::TLP_AFTER_RENAME_LOCAL_PROPERTY) {
141  // force any needed sorting
142  emit layoutAboutToBeChanged();
143  changePersistentIndex(createIndex(0, 0), createIndex(_properties.size() - 1, 0));
144  emit layoutChanged();
145  }
146  }
147 
148  int rowOf(PROPTYPE *) const;
149 
150  int rowOf(const QString &pName) const;
151 
152  Qt::ItemFlags flags(const QModelIndex &index) const override {
153  Qt::ItemFlags result = QAbstractItemModel::flags(index);
154 
155  if (index.column() == 0 && _checkable)
156  result |= Qt::ItemIsUserCheckable;
157 
158  return result;
159  }
160 };
161 } // namespace tlp
162 
163 #include "cxx/GraphPropertiesModel.cxx"
164 
165 #endif // GRAPHPROPERTIESMODEL_H
166 ///@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.