Tulip  6.0.0
Large graphs analysis and drawing
GraphModel.h
1 /*
2  *
3  * This file is part of Tulip (https://tulip.labri.fr)
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 #include <tulip/Edge.h>
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 = nullptr);
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 = nullptr);
50 
51  explicit GraphModel(QObject *parent = nullptr);
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 override;
58  int columnCount(const QModelIndex &parent = QModelIndex()) const override;
59  QModelIndex parent(const QModelIndex &child) const override;
60  Qt::ItemFlags flags(const QModelIndex &index) const override;
61 
62  QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
63  QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
64  QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
65  bool setData(const QModelIndex &index, const QVariant &value, int role) override;
66 
67  void treatEvent(const tlp::Event &) override;
68  void treatEvents(const std::vector<tlp::Event> &) override;
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(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 = nullptr);
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 override;
99  bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
100 
101  void treatEvent(const tlp::Event &) override;
102 
103  tlp::BooleanProperty *filterProperty() const;
104 };
105 
106 class TLP_QT_SCOPE NodesGraphModel : public GraphModel {
107 public:
108  NodesGraphModel(QObject *parent = nullptr);
109  void setGraph(Graph *) override;
110 
111  void treatEvent(const tlp::Event &) override;
112  void treatEvents(const std::vector<tlp::Event> &) override;
113  bool lessThan(unsigned int, unsigned int, tlp::PropertyInterface *) const override;
114  QString stringValue(unsigned int, tlp::PropertyInterface *) const override;
115  QVariant value(unsigned int, tlp::PropertyInterface *) const override;
116  QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
117 
118  bool isNode() const override {
119  return true;
120  }
121 
122  static QString getNodeTooltip(Graph *graph, node n);
123 
124 protected:
125  bool setValue(unsigned int, tlp::PropertyInterface *, QVariant) const override;
126 
127 private:
128  bool _nodesAdded;
129  bool _nodesRemoved;
130 };
131 
132 class TLP_QT_SCOPE EdgesGraphModel : public GraphModel {
133 public:
134  EdgesGraphModel(QObject *parent = nullptr);
135  void setGraph(Graph *) override;
136  void treatEvent(const tlp::Event &) override;
137  void treatEvents(const std::vector<tlp::Event> &) override;
138  bool lessThan(unsigned int, unsigned int, tlp::PropertyInterface *) const override;
139  QString stringValue(unsigned int, tlp::PropertyInterface *) const override;
140  QVariant value(unsigned int, tlp::PropertyInterface *) const override;
141  QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
142 
143  bool isNode() const override {
144  return false;
145  }
146 
147  static QString getEdgeTooltip(Graph *graph, edge e);
148 
149 protected:
150  bool setValue(unsigned int, tlp::PropertyInterface *, QVariant) const override;
151 
152 private:
153  bool _edgesAdded;
154  bool _edgesRemoved;
155 };
156 } // namespace tlp
157 
158 #endif // GRAPHMODEL_H
159 ///@endcond
A graph property that maps a Boolean value to graph elements.
Event is the base class for all events used in the Observation mechanism.
Definition: Observable.h:52
PropertyInterface describes the interface of a graph property.