Tulip  4.1.0
Better Visualization Through Research
 All Classes Files Functions Variables Enumerations Enumerator Properties Groups Pages
GraphNeedsSavingObserver.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 #ifndef GRAPHNEEDSSAVINGOBSERVER_H
22 #define GRAPHNEEDSSAVINGOBSERVER_H
23 
24 #include <tulip/Observable.h>
25 #include <tulip/ObservableGraph.h>
26 #include <tulip/ObservableProperty.h>
27 #include <tulip/Observable.h>
28 #include <tulip/Graph.h>
29 
30 #include <QtGui/qtabwidget.h>
31 #include <tulip/PropertyInterface.h>
32 
33 #include <deque>
34 
35 class GraphNeedsSavingObserver : public QObject, tlp::Observable {
36  Q_OBJECT
37 public :
38 
39  GraphNeedsSavingObserver(tlp::Graph* graph) :_needsSaving(false), _graph(graph) {
40  addObserver();
41  }
42 
43 protected :
44 
45  virtual void treatEvents(const std::vector<tlp::Event>&) {
46  doNeedSaving();
47  }
48 
49 public :
50 
51  void saved() {
52  _needsSaving = false;
53  removeObservers();
54  addObserver();
55  }
56 
57  bool needsSaving() const {
58  return _needsSaving;
59  }
60 
61 signals:
62  void savingNeeded();
63 
64 private:
65 
66  void doNeedSaving() {
67  if(!_needsSaving) {
68  // Stop listening graphs.
69  removeObservers();
70  _needsSaving = true;
71  emit(savingNeeded());
72  }
73  }
74 
75  /**
76  * @brief Listen all the observable objects in the graph (subgraphs, properties).
77  **/
78  void addObserver() {
79  std::deque<tlp::Graph*> toObserve;
80  toObserve.push_back(_graph);
81 
82  while(!toObserve.empty()) {
83  tlp::Graph* current = toObserve.front();
84  current->addObserver(this);
85  toObserve.pop_front();
86 
87  //Listen properties.
88  tlp::PropertyInterface* property;
89  forEach(property,current->getLocalObjectProperties()) {
90  property->addObserver(this);
91  }
92 
93  //Fetch subgraphs
94  tlp::Graph* subgraphs;
95  forEach(subgraphs,current->getSubGraphs()) {
96  toObserve.push_back(subgraphs);
97  }
98  }
99  }
100 
101  /**
102  * @brief Stop listening all the observable objects in the graph (subgraphs, properties).
103  **/
104  void removeObservers() {
105  std::deque<tlp::Graph*> toUnobserve;
106  toUnobserve.push_back(_graph);
107 
108  while(!toUnobserve.empty()) {
109  tlp::Graph* current = toUnobserve.front();
110  toUnobserve.pop_front();
111 
112  current->removeObserver(this);
113 
114  //Stop listening properties.
115  tlp::PropertyInterface* property;
116  forEach(property,current->getLocalObjectProperties()) {
117  property->removeObserver(this);
118  }
119 
120  //Fetch subgraphs
121  tlp::Graph* subgraphs;
122  forEach(subgraphs,current->getSubGraphs()) {
123  toUnobserve.push_back(subgraphs);
124  }
125  }
126  }
127 
128  bool _needsSaving;
129  tlp::Graph* _graph;
130 };
131 
132 #endif //GRAPHNEEDSSAVINGOBSERVER_H
133 ///@endcond