Tulip  4.2.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 #include <tulip/PropertyInterface.h>
29 #include <tulip/TlpQtTools.h>
30 #include <QtGui/QFont>
31 #include <QtGui/QIcon>
32 #include <QtCore/QSet>
33 
34 namespace tlp {
35 
36 template<typename PROPTYPE>
37 class GraphPropertiesModel : public tlp::TulipModel, public tlp::Observable {
38  tlp::Graph* _graph;
39  QString _placeholder;
40  bool _checkable;
41  QSet<PROPTYPE*> _checkedProperties;
42  QVector<PROPTYPE*> _properties;
43  bool _removingRows;
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 #ifndef NDEBUG
52  qDebug() << "Deleting GraphPropertiesModel<" << typeid(PROPTYPE).name() << "*>";
53 #endif
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 != NULL)
67  _graph->removeListener(this);
68 
69  _graph = graph;
70 
71  if (_graph != NULL)
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;
84  QModelIndex parent(const QModelIndex &child) const;
85  int rowCount(const QModelIndex &parent = QModelIndex()) const;
86  int columnCount(const QModelIndex &parent = QModelIndex()) const;
87  QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
88  QVariant headerData(int section, Qt::Orientation orientation, int role) const;
89  bool setData(const QModelIndex &quiindex, const QVariant &value, int role);
90 
91  // Methods inherited from the observable system
92  void treatEvent(const tlp::Event& evt) {
93  if (evt.type() == Event::TLP_DELETE) {
94  beginResetModel();
95  _graph = NULL;
96  _properties.clear();
97  endResetModel();
98  return;
99  }
100 
101  const GraphEvent* graphEvent = dynamic_cast<const GraphEvent*>(&evt);
102 
103  if (graphEvent == NULL)
104  return;
105 
106  if (graphEvent->getType() == GraphEvent::TLP_BEFORE_DEL_LOCAL_PROPERTY || graphEvent->getType() == GraphEvent::TLP_BEFORE_DEL_INHERITED_PROPERTY) {
107 
108  PROPTYPE* prop = dynamic_cast<PROPTYPE*>(_graph->getProperty(graphEvent->getPropertyName()));
109 
110  if (prop != NULL) {
111  int row = rowOf(prop);
112  beginRemoveRows(QModelIndex(),row,row);
113  _properties.remove(_properties.indexOf(prop));
114  _removingRows = true;
115  _checkedProperties.remove(prop);
116  }
117  }
118  else if (graphEvent->getType() == GraphEvent::TLP_AFTER_DEL_LOCAL_PROPERTY || graphEvent->getType() == GraphEvent::TLP_AFTER_DEL_INHERITED_PROPERTY) {
119  if (_removingRows) {
120  endRemoveRows();
121  _removingRows = false;
122  }
123  }
124  else if (graphEvent->getType() == GraphEvent::TLP_ADD_LOCAL_PROPERTY || graphEvent->getType() == GraphEvent::TLP_ADD_INHERITED_PROPERTY) {
125  PROPTYPE* prop = dynamic_cast<PROPTYPE*>(_graph->getProperty(graphEvent->getPropertyName()));
126 
127  if (prop != NULL) {
128  rebuildCache();
129  int row = rowOf(prop);
130 
131  if (row >= 0) {
132  beginInsertRows(QModelIndex(),row,row);
133  endInsertRows();
134  }
135  }
136  }
137  }
138 
139  int rowOf(PROPTYPE*) const;
140 
141  Qt::ItemFlags flags(const QModelIndex &index) const {
142  Qt::ItemFlags result = QAbstractItemModel::flags(index);
143 
144  if (index.column() == 0 && _checkable)
145  result |= Qt::ItemIsUserCheckable;
146 
147  return result;
148  }
149 };
150 
151 }
152 
153 #include "cxx/GraphPropertiesModel.cxx"
154 
155 
156 #endif // GRAPHPROPERTIESMODEL_H
157 ///@endcond