Tulip  4.3.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 1 and Inria Bordeaux - Sud Ouest
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 
44  void rebuildCache();
45 
46 public:
47  explicit GraphPropertiesModel(tlp::Graph* graph, bool checkable=false, QObject *parent = NULL);
48  explicit GraphPropertiesModel(QString placeholder, tlp::Graph* graph, bool checkable=false, QObject *parent = NULL);
49  virtual ~GraphPropertiesModel() {}
50 
51  tlp::Graph* graph() const {
52  return _graph;
53  }
54 
55  void setGraph(tlp::Graph* graph) {
56  if (_graph == graph)
57  return;
58 
59  beginResetModel();
60 
61  if (_graph != NULL)
62  _graph->removeListener(this);
63 
64  _graph = graph;
65 
66  if (_graph != NULL)
67  _graph->addListener(this);
68 
69  rebuildCache();
70  endResetModel();
71  }
72 
73  QSet<PROPTYPE*> checkedProperties() const {
74  return _checkedProperties;
75  }
76 
77  // Methods re-implemented from QAbstractItemModel
78  QModelIndex index(int row, int column,const QModelIndex &parent = QModelIndex()) const;
79  QModelIndex parent(const QModelIndex &child) const;
80  int rowCount(const QModelIndex &parent = QModelIndex()) const;
81  int columnCount(const QModelIndex &parent = QModelIndex()) const;
82  QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
83  QVariant headerData(int section, Qt::Orientation orientation, int role) const;
84  bool setData(const QModelIndex &quiindex, const QVariant &value, int role);
85 
86  // Methods inherited from the observable system
87  void treatEvent(const tlp::Event& evt) {
88  if (evt.type() == Event::TLP_DELETE) {
89  beginResetModel();
90  _graph = NULL;
91  _properties.clear();
92  endResetModel();
93  return;
94  }
95 
96  const GraphEvent* graphEvent = dynamic_cast<const GraphEvent*>(&evt);
97 
98  if (graphEvent == NULL)
99  return;
100 
101  if (graphEvent->getType() == GraphEvent::TLP_BEFORE_DEL_LOCAL_PROPERTY || graphEvent->getType() == GraphEvent::TLP_BEFORE_DEL_INHERITED_PROPERTY) {
102 
103  PROPTYPE* prop = dynamic_cast<PROPTYPE*>(_graph->getProperty(graphEvent->getPropertyName()));
104 
105  if (prop != NULL) {
106  int row = rowOf(prop);
107  beginRemoveRows(QModelIndex(),row,row);
108  _properties.remove(_properties.indexOf(prop));
109  _removingRows = true;
110  _checkedProperties.remove(prop);
111  }
112  }
113  else if (graphEvent->getType() == GraphEvent::TLP_AFTER_DEL_LOCAL_PROPERTY || graphEvent->getType() == GraphEvent::TLP_AFTER_DEL_INHERITED_PROPERTY) {
114  if (_removingRows) {
115  endRemoveRows();
116  _removingRows = false;
117  }
118  }
119  else if (graphEvent->getType() == GraphEvent::TLP_ADD_LOCAL_PROPERTY || graphEvent->getType() == GraphEvent::TLP_ADD_INHERITED_PROPERTY) {
120  PROPTYPE* prop = dynamic_cast<PROPTYPE*>(_graph->getProperty(graphEvent->getPropertyName()));
121 
122  if (prop != NULL) {
123  rebuildCache();
124  int row = rowOf(prop);
125 
126  if (row >= 0) {
127  beginInsertRows(QModelIndex(),row,row);
128  endInsertRows();
129  }
130  }
131  }
132  }
133 
134  int rowOf(PROPTYPE*) const;
135 
136  Qt::ItemFlags flags(const QModelIndex &index) const {
137  Qt::ItemFlags result = QAbstractItemModel::flags(index);
138 
139  if (index.column() == 0 && _checkable)
140  result |= Qt::ItemIsUserCheckable;
141 
142  return result;
143  }
144 };
145 
146 }
147 
148 #include "cxx/GraphPropertiesModel.cxx"
149 
150 
151 #endif // GRAPHPROPERTIESMODEL_H
152 ///@endcond