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