Tulip  4.1.0
Better Visualization Through Research
 All Classes 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 <QtGui/QFont>
30 #include <QtGui/QIcon>
31 #include <QtCore/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 #ifndef NDEBUG
51  qDebug() << "Deleting GraphPropertiesModel<" << typeid(PROPTYPE).name() << "*>";
52 #endif
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 &index, 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  beginResetModel();
94  _graph = NULL;
95  _properties.clear();
96  endResetModel();
97  return;
98  }
99 
100  const GraphEvent* graphEvent = dynamic_cast<const GraphEvent*>(&evt);
101 
102  if (graphEvent == NULL)
103  return;
104 
105  if (graphEvent->getType() == GraphEvent::TLP_BEFORE_DEL_LOCAL_PROPERTY || graphEvent->getType() == GraphEvent::TLP_BEFORE_DEL_INHERITED_PROPERTY) {
106 
107  PROPTYPE* prop = dynamic_cast<PROPTYPE*>(_graph->getProperty(graphEvent->getPropertyName()));
108 
109  if (prop != NULL) {
110  int row = rowOf(prop);
111  beginRemoveRows(QModelIndex(),row,row);
112  _properties.remove(row);
113  _removingRows = true;
114  }
115  }
116  else if (graphEvent->getType() == GraphEvent::TLP_AFTER_DEL_LOCAL_PROPERTY || graphEvent->getType() == GraphEvent::TLP_AFTER_DEL_INHERITED_PROPERTY) {
117  if (_removingRows) {
118  endRemoveRows();
119  _removingRows = false;
120  }
121  }
122  else if (graphEvent->getType() == GraphEvent::TLP_ADD_LOCAL_PROPERTY || graphEvent->getType() == GraphEvent::TLP_ADD_INHERITED_PROPERTY) {
123  PROPTYPE* prop = dynamic_cast<PROPTYPE*>(_graph->getProperty(graphEvent->getPropertyName()));
124 
125  if (prop != NULL) {
126  rebuildCache();
127  int row = rowOf(prop);
128 
129  if (row >= 0) {
130  beginInsertRows(QModelIndex(),row,row);
131  endInsertRows();
132  }
133  }
134  }
135  }
136 
137  int rowOf(PROPTYPE*) const;
138 
139  Qt::ItemFlags flags(const QModelIndex &index) const {
140  Qt::ItemFlags result = QAbstractItemModel::flags(index);
141 
142  if (index.column() == 0 && _checkable)
143  result |= Qt::ItemIsUserCheckable;
144 
145  return result;
146  }
147 };
148 
149 }
150 
151 #include "cxx/GraphPropertiesModel.cxx"
152 
153 
154 #endif // GRAPHPROPERTIESMODEL_H
155 ///@endcond