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