Tulip  5.7.4
Large graphs analysis and drawing
GraphHierarchiesModel.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 GRAPHHIERARCHIESMODEL_H
22 #define GRAPHHIERARCHIESMODEL_H
23 
24 #include <tulip/tulipconf.h>
25 #include <tulip/TulipModel.h>
26 #include <tulip/Observable.h>
27 #include <tulip/Graph.h>
28 
29 #include <QList>
30 #include <QSet>
31 
32 namespace tlp {
33 class Graph;
34 class GraphNeedsSavingObserver;
35 class TulipProject;
36 class PluginProgress;
37 
38 class TLP_QT_SCOPE GraphHierarchiesModel : public tlp::TulipModel, public tlp::Observable {
39  Q_OBJECT
40 
41  QList<tlp::Graph *> _graphs;
42  QString generateName(tlp::Graph *) const;
43 
44  tlp::Graph *_currentGraph;
45  QMap<const tlp::Graph *, QModelIndex> _indexCache;
46  QMap<const tlp::Graph *, GraphNeedsSavingObserver *> _saveNeeded;
47  void initIndexCache(tlp::Graph *root);
48 
49 public:
50  bool needsSaving();
51 
52  explicit GraphHierarchiesModel(QObject *parent = nullptr);
53  GraphHierarchiesModel(const GraphHierarchiesModel &);
54  ~GraphHierarchiesModel() override;
55 
56  // Allows the model to behave like a list and to be iterable
57  typedef QList<tlp::Graph *>::iterator iterator;
58  typedef QList<tlp::Graph *>::const_iterator const_iterator;
59  tlp::Graph *operator[](int i) const {
60  return _graphs[i];
61  }
62  tlp::Graph *operator[](int i) {
63  return _graphs[i];
64  }
65  int size() const {
66  return _graphs.size();
67  }
68 
69  iterator begin() {
70  return _graphs.begin();
71  }
72  iterator end() {
73  return _graphs.end();
74  }
75  const_iterator begin() const {
76  return _graphs.begin();
77  }
78  const_iterator end() const {
79  return _graphs.end();
80  }
81 
82  QList<tlp::Graph *> graphs() const {
83  return _graphs;
84  }
85  bool empty() const {
86  return _graphs.isEmpty();
87  }
88 
89  // Methods re-implemented from QAbstractItemModel
90  QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
91  QModelIndex parent(const QModelIndex &child) const override;
92  int rowCount(const QModelIndex &parent = QModelIndex()) const override;
93  int columnCount(const QModelIndex &parent = QModelIndex()) const override;
94  QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
95  bool setData(const QModelIndex &index, const QVariant &value, int role) override;
96  Qt::ItemFlags flags(const QModelIndex &index) const override;
97  QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
98  QMimeData *mimeData(const QModelIndexList &indexes) const override;
99 
100  QModelIndex indexOf(const Graph *);
101  QModelIndex forceGraphIndex(Graph *);
102 
103  // Methods inherited from the observable system
104  void treatEvent(const tlp::Event &) override;
105 
106  void treatEvents(const std::vector<tlp::Event> &) override;
107 
108  // active graph handling
109  void setCurrentGraph(tlp::Graph *);
110  tlp::Graph *currentGraph() const;
111 
112 signals:
113  void currentGraphChanged(tlp::Graph *);
114 
115 public slots:
116  void addGraph(tlp::Graph *);
117  void removeGraph(tlp::Graph *);
118 
119  QMap<QString, tlp::Graph *> readProject(tlp::TulipProject *, tlp::PluginProgress *);
120  QMap<tlp::Graph *, QString> writeProject(tlp::TulipProject *, tlp::PluginProgress *);
121 
122 private:
123  QSet<const Graph *> _graphsChanged;
124 };
125 } // namespace tlp
126 
127 #endif // GRAPHHIERARCHIESMODEL_H
128 ///@endcond
Event is the base class for all events used in the Observation mechanism.
Definition: Observable.h:52
The Observable class is the base of Tulip's observation system.
Definition: Observable.h:127
PluginProcess subclasses are meant to notify about the progress state of some process (typically a pl...