Tulip  5.0.0
Large graphs analysis and drawing
GraphModel.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 #ifndef GRAPHMODEL_H
22 #define GRAPHMODEL_H
23 
24 #include <QVector>
25 #include <QSet>
26 #include <QSortFilterProxyModel>
27 
28 
29 #include <tulip/Observable.h>
30 #include <tulip/TulipModel.h>
31 
32 namespace tlp {
33 class Graph;
34 class PropertyInterface;
35 class BooleanProperty;
36 
37 class TLP_QT_SCOPE GraphModel : public tlp::TulipModel, public Observable {
38 
39 public:
40  static QVariant nodeValue(unsigned int, tlp::PropertyInterface*);
41  static QVariant nodeDefaultValue(tlp::PropertyInterface*);
42  static bool setNodeValue(unsigned int,tlp::PropertyInterface*, QVariant);
43  static bool setNodeDefaultValue(tlp::PropertyInterface*, QVariant);
44  static bool setAllNodeValue(tlp::PropertyInterface*, QVariant, Graph *graph = NULL);
45  static QVariant edgeValue(unsigned int, tlp::PropertyInterface*);
46  static QVariant edgeDefaultValue(tlp::PropertyInterface*);
47  static bool setEdgeValue(unsigned int,tlp::PropertyInterface*, QVariant);
48  static bool setEdgeDefaultValue(tlp::PropertyInterface*, QVariant);
49  static bool setAllEdgeValue(tlp::PropertyInterface*, QVariant, Graph *graph = NULL);
50 
51  explicit GraphModel(QObject *parent = NULL);
52 
53  virtual void setGraph(tlp::Graph*);
54  tlp::Graph* graph() const;
55 
56  // Methods re-implemented from QAbstractItemModel
57  int rowCount(const QModelIndex &parent = QModelIndex()) const;
58  int columnCount(const QModelIndex &parent = QModelIndex()) const;
59  QModelIndex parent(const QModelIndex &child) const;
60  Qt::ItemFlags flags(const QModelIndex &index) const;
61 
62  QVariant headerData(int section, Qt::Orientation orientation, int role) const;
63  QModelIndex index(int row, int column,const QModelIndex &parent = QModelIndex()) const;
64  QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
65  bool setData(const QModelIndex &index, const QVariant &value, int role);
66 
67  void treatEvent(const tlp::Event&);
68  void treatEvents(const std::vector<tlp::Event> &);
69 
70  unsigned int elementAt(int) const;
71  virtual bool lessThan(unsigned int,unsigned int,tlp::PropertyInterface*) const = 0;
72  virtual QString stringValue(unsigned int,tlp::PropertyInterface*) const = 0;
73  virtual QVariant value(unsigned int,tlp::PropertyInterface*) const = 0;
74  virtual bool isNode() const = 0;
75 
76 protected:
77  tlp::Graph* _graph;
78  QVector<unsigned int> _elements;
79  QVector<QPair<unsigned int,bool> > _elementsToModify;
80  QVector<PropertyInterface*> _properties;
81  QSet<PropertyInterface*> _propertiesModified;
82 
83  virtual bool setValue(unsigned int,tlp::PropertyInterface*,QVariant) const = 0;
84  void addRemoveRowsSequence(const QVector<unsigned int> &rowsSequence, bool add);
85 };
86 
87 class TLP_QT_SCOPE GraphSortFilterProxyModel: public QSortFilterProxyModel, public Observable {
88  QVector<PropertyInterface*> _properties;
89  BooleanProperty* _filterProperty;
90 
91 public:
92  GraphSortFilterProxyModel(QObject* parent = NULL);
93 
94  void setFilterProperty(tlp::BooleanProperty*);
95  void setSelectedOnly(bool);
96  void setProperties(QVector<PropertyInterface*>);
97 
98  bool lessThan(const QModelIndex &left, const QModelIndex &right) const;
99  bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
100 
101  void treatEvent(const tlp::Event&);
102 
103  tlp::BooleanProperty* filterProperty() const;
104 };
105 
106 class TLP_QT_SCOPE NodesGraphModel: public GraphModel {
107 public:
108  NodesGraphModel(QObject* parent = NULL);
109  void setGraph(Graph *);
110 
111  void treatEvent(const tlp::Event &);
112  void treatEvents(const std::vector<tlp::Event> &);
113  virtual bool lessThan(unsigned int,unsigned int,tlp::PropertyInterface*) const;
114  virtual QString stringValue(unsigned int,tlp::PropertyInterface*) const;
115  virtual QVariant value(unsigned int,tlp::PropertyInterface*) const;
116  QVariant headerData(int section, Qt::Orientation orientation, int role) const;
117 
118  bool isNode() const {
119  return true;
120  }
121 
122  static QString getNodeTooltip(Graph *graph, node n);
123 
124 protected:
125  virtual bool setValue(unsigned int,tlp::PropertyInterface*,QVariant) const;
126 private:
127  bool _nodesAdded;
128  bool _nodesRemoved;
129 };
130 
131 class TLP_QT_SCOPE EdgesGraphModel: public GraphModel {
132 public:
133  EdgesGraphModel(QObject* parent = NULL);
134  void setGraph(Graph *);
135  void treatEvent(const tlp::Event &);
136  void treatEvents(const std::vector<tlp::Event> &);
137  virtual bool lessThan(unsigned int,unsigned int,tlp::PropertyInterface*) const;
138  virtual QString stringValue(unsigned int,tlp::PropertyInterface*) const;
139  virtual QVariant value(unsigned int,tlp::PropertyInterface*) const;
140  QVariant headerData(int section, Qt::Orientation orientation, int role) const;
141 
142  bool isNode() const {
143  return false;
144  }
145 
146  static QString getEdgeTooltip(Graph *graph, edge e);
147 
148 protected:
149  virtual bool setValue(unsigned int,tlp::PropertyInterface*,QVariant) const;
150 private:
151  bool _edgesAdded;
152  bool _edgesRemoved;
153 };
154 
155 }
156 
157 #endif // GRAPHMODEL_H
158 ///@endcond
A graph property that maps a boolean value to graph elements.
PropertyInterface describes the interface of a graph property.
Event is the base class for all events used in the Observation mechanism.
Definition: Observable.h:47