Tulip  4.7.0
Better Visualization Through Research
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Groups Pages
GraphPropertiesModel.h
1 /*
2  *
3  * This file is part of Tulip (www.tulip-software.org)
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 
52  tlp::Graph* graph() const {
53  return _graph;
54  }
55 
56  void setGraph(tlp::Graph* graph) {
57  if (_graph == graph)
58  return;
59 
60  beginResetModel();
61 
62  if (_graph != NULL)
63  _graph->removeListener(this);
64 
65  _graph = graph;
66 
67  if (_graph != NULL)
68  _graph->addListener(this);
69 
70  rebuildCache();
71  endResetModel();
72  }
73 
74  QSet<PROPTYPE*> checkedProperties() const {
75  return _checkedProperties;
76  }
77 
78  // Methods re-implemented from QAbstractItemModel
79  QModelIndex index(int row, int column,const QModelIndex &parent = QModelIndex()) const;
80  QModelIndex parent(const QModelIndex &child) const;
81  int rowCount(const QModelIndex &parent = QModelIndex()) const;
82  int columnCount(const QModelIndex &parent = QModelIndex()) const;
83  QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
84  QVariant headerData(int section, Qt::Orientation orientation, int role) const;
85  bool setData(const QModelIndex &quiindex, const QVariant &value, int role);
86 
87  // Methods inherited from the observable system
88  void treatEvent(const tlp::Event& evt) {
89  if (evt.type() == Event::TLP_DELETE) {
90  beginResetModel();
91  _graph = NULL;
92  _properties.clear();
93  endResetModel();
94  return;
95  }
96 
97  const GraphEvent* graphEvent = dynamic_cast<const GraphEvent*>(&evt);
98 
99  if (graphEvent == NULL)
100  return;
101 
102  if (graphEvent->getType() == GraphEvent::TLP_BEFORE_DEL_LOCAL_PROPERTY || graphEvent->getType() == GraphEvent::TLP_BEFORE_DEL_INHERITED_PROPERTY) {
103 
104  PROPTYPE* prop = dynamic_cast<PROPTYPE*>(_graph->getProperty(graphEvent->getPropertyName()));
105 
106  if (prop != NULL) {
107  int row = rowOf(prop);
108  beginRemoveRows(QModelIndex(),row,row);
109  _properties.remove(_properties.indexOf(prop));
110  _removingRows = true;
111  _checkedProperties.remove(prop);
112  }
113  }
114  else if (graphEvent->getType() == GraphEvent::TLP_AFTER_DEL_LOCAL_PROPERTY || graphEvent->getType() == GraphEvent::TLP_AFTER_DEL_INHERITED_PROPERTY) {
115  if (_removingRows) {
116  endRemoveRows();
117  _removingRows = false;
118  }
119  }
120  else if (graphEvent->getType() == GraphEvent::TLP_ADD_LOCAL_PROPERTY || graphEvent->getType() == GraphEvent::TLP_ADD_INHERITED_PROPERTY) {
121  PROPTYPE* prop = dynamic_cast<PROPTYPE*>(_graph->getProperty(graphEvent->getPropertyName()));
122 
123  if (prop != NULL) {
124  rebuildCache();
125  int row = rowOf(prop);
126 
127  if (row >= 0) {
128  beginInsertRows(QModelIndex(),row,row);
129  endInsertRows();
130  }
131  }
132  }
133  else if (graphEvent->getType() == GraphEvent::TLP_AFTER_RENAME_LOCAL_PROPERTY) {
134  // force any needed sorting
135  emit layoutAboutToBeChanged();
136  changePersistentIndex(createIndex(0, 0),
137  createIndex(_properties.size() - 1, 0));
138  emit layoutChanged();
139  }
140  }
141 
142  int rowOf(PROPTYPE*) const;
143 
144  int rowOf(const QString& pName) const;
145 
146  Qt::ItemFlags flags(const QModelIndex &index) const {
147  Qt::ItemFlags result = QAbstractItemModel::flags(index);
148 
149  if (index.column() == 0 && _checkable)
150  result |= Qt::ItemIsUserCheckable;
151 
152  return result;
153  }
154 };
155 
156 }
157 
158 #include "cxx/GraphPropertiesModel.cxx"
159 
160 
161 #endif // GRAPHPROPERTIESMODEL_H
162 ///@endcond
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
The Observable class is the base of Tulip's observation system.
Definition: Observable.h:123