OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
widget_state_saver.hh
Go to the documentation of this file.
1 //------------------------------------------------------------------------------
2 // This file is part of the OpenStructure project <www.openstructure.org>
3 //
4 // Copyright (C) 2008-2011 by the OpenStructure authors
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 3.0 of the License, or (at your option)
9 // any later version.
10 // This library is distributed in the hope that it will be useful, but WITHOUT
11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 //------------------------------------------------------------------------------
19 #ifndef WIDGET_STATE_SAVER_HH_
20 #define WIDGET_STATE_SAVER_HH_
21 
22 /*
23  Author: Andreas Schenk
24  */
25 
26 #include <ost/gui/module_config.hh>
27 
28 #include <QSettings>
29 #include <QPoint>
30 #include <QSize>
31 #include <QMainWindow>
32 #include <QDebug>
33 
34 namespace ost{namespace gui{
35 
37 {
38 public:
39  static void IgnoreSettings(bool flag=true);
40  static bool SettingsIgnored();
41 protected:
42  static bool flag_;
43 };
44 
45 struct unused;
46 template<class BASE, typename T1=unused, typename T2=unused, typename T3=unused>
47 class WidgetStateSaver: public BASE
48 {
49 public:
50  WidgetStateSaver(const QString& name, QWidget * parent ,T1 t1, T2 t2,T3 t3):
51  BASE(parent,t1,t2,t3),
52  name_(name),
53  pos_(),
54  size_(),
55  visible_(true),
56  default_(false)
57  {
58  set_state_();
59  }
60 
61  WidgetStateSaver(const QString& name,QPoint pos, QSize size, QWidget * parent ,T1 t1, T2 t2,T3 t3):
62  BASE(parent,t1,t2,t3),
63  name_(name),
64  pos_(pos),
65  size_(size),
66  visible_(true),
67  default_(true)
68  {
69  set_state_();
70  }
71 
72  void ResetStateSaver(const QString& name)
73  {
74  name_=name;
75  set_state_();
76  }
77 
78 protected:
79 
80  void set_state_()
81  {
82  QSettings settings;
83  settings.beginGroup(name_);
84  if(settings.contains("pos") && ! WidgetStateSaverConfig::SettingsIgnored()){
85  BASE::move(settings.value("pos").toPoint());
86  BASE::resize(settings.value("size").toSize());
87  //BASE::setVisible(settings.value("visible").toBool());
88  if(dynamic_cast<QMainWindow* >(this)){
89  dynamic_cast<QMainWindow* >(this)->restoreState(settings.value("state").toByteArray());
90  }
91  }else if(default_){
92  BASE::move(pos_);
93  BASE::resize(size_);
94  //BASE::setVisible(visible_);
95  }
96  settings.endGroup();
97  }
98 
100  {
101  QSettings settings;
102  settings.beginGroup(name_);
103  settings.setValue("pos", BASE::pos());
104  settings.setValue("size", BASE::size());
105  //settings.setValue("visible", /*BASE::isVisible()*/visible_);
106  if(dynamic_cast<QMainWindow* >(this)){
107  settings.setValue("state", dynamic_cast<QMainWindow* >(this)->saveState());
108  }
109  settings.endGroup();
110  }
111  QString name_;
112  QPoint pos_;
113  QSize size_;
114  bool visible_;
115  bool default_;
116 };
117 
118 template<class BASE, typename T1, typename T2>
119 class WidgetStateSaver<BASE,T1,T2>: public BASE
120 {
121 public:
122  WidgetStateSaver(const QString& name, QWidget * parent ,T1 t1, T2 t2):
123  BASE(parent,t1,t2),
124  name_(name),
125  pos_(),
126  size_(),
127  visible_(true),
128  default_(false)
129  {
130  set_state_();
131  }
132 
133  WidgetStateSaver(const QString& name,QPoint pos, QSize size, bool visible,
134  QWidget * parent ,T1 t1, T2 t2):
135  BASE(parent,t1,t2),
136  name_(name),
137  pos_(pos),
138  size_(size),
139  visible_(visible),
140  default_(true)
141  {
142  set_state_();
143  }
144 
145  void ResetStateSaver(const QString& name)
146  {
147  name_=name;
148  set_state_();
149  }
150 
151 protected:
152 
153  void set_state_()
154  {
155  QSettings settings;
156  settings.beginGroup(name_);
157  if(settings.contains("pos") && ! WidgetStateSaverConfig::SettingsIgnored()){
158  BASE::move(settings.value("pos").toPoint());
159  BASE::resize(settings.value("size").toSize());
160  //BASE::setVisible(settings.value("visible").toBool());
161  if(dynamic_cast<QMainWindow* >(this)){
162  dynamic_cast<QMainWindow* >(this)->restoreState(settings.value("state").toByteArray());
163  }
164  }else if(default_){
165  BASE::move(pos_);
166  BASE::resize(size_);
167  //BASE::setVisible(visible_);
168  }
169  settings.endGroup();
170  }
171 
173  {
174  QSettings settings;
175  settings.beginGroup(name_);
176  settings.setValue("pos", BASE::pos());
177  settings.setValue("size", BASE::size());
178  //settings.setValue("visible", /*BASE::isVisible()*/visible_);
179  if(dynamic_cast<QMainWindow* >(this)){
180  settings.setValue("state", dynamic_cast<QMainWindow* >(this)->saveState());
181  }
182  settings.endGroup();
183  }
184  QString name_;
185  QPoint pos_;
186  QSize size_;
187  bool visible_;
188  bool default_;
189 };
190 template<class BASE, typename T1>
191 class WidgetStateSaver<BASE,T1>: public BASE
192 {
193 public:
194  WidgetStateSaver(const QString& name, QWidget * parent ,T1 t1):
195  BASE(parent,t1),
196  name_(name),
197  pos_(),
198  size_(),
199  visible_(true),
200  default_(false)
201  {
202  set_state_();
203  }
204 
205  WidgetStateSaver(const QString& name,QPoint pos, QSize size, bool visible,
206  QWidget * parent ,T1 t1):
207  BASE(parent,t1),
208  name_(name),
209  pos_(pos),
210  size_(size),
211  visible_(visible),
212  default_(true)
213  {
214  set_state_();
215  }
216 
217  void ResetStateSaver(const QString& name)
218  {
219  name_=name;
220  set_state_();
221  }
222 
223 protected:
224 
225  void set_state_()
226  {
227  QSettings settings;
228  settings.beginGroup(name_);
229  if(settings.contains("pos") && ! WidgetStateSaverConfig::SettingsIgnored()){
230  BASE::move(settings.value("pos").toPoint());
231  BASE::resize(settings.value("size").toSize());
232  //BASE::setVisible(settings.value("visible").toBool());
233  if(dynamic_cast<QMainWindow* >(this)){
234  dynamic_cast<QMainWindow* >(this)->restoreState(settings.value("state").toByteArray());
235  }
236  }else if(default_){
237  BASE::move(pos_);
238  BASE::resize(size_);
239  //BASE::setVisible(visible_);
240  }
241  settings.endGroup();
242  }
243 
245  {
246  QSettings settings;
247  settings.beginGroup(name_);
248  settings.setValue("pos", BASE::pos());
249  settings.setValue("size", BASE::size());
250  //settings.setValue("visible", /*BASE::isVisible()*/visible_);
251  if(dynamic_cast<QMainWindow* >(this)){
252  settings.setValue("state", dynamic_cast<QMainWindow* >(this)->saveState());
253  }
254  settings.endGroup();
255  }
256  QString name_;
257  QPoint pos_;
258  QSize size_;
259  bool visible_;
260  bool default_;
261 };
262 template<class BASE>
263 class WidgetStateSaver<BASE>: public BASE
264 {
265 public:
266  WidgetStateSaver(const QString& name, QWidget * parent ):
267  BASE(parent),
268  name_(name),
269  pos_(),
270  size_(),
271  visible_(true),
272  default_(false)
273  {
274  set_state_();
275  }
276 
277  WidgetStateSaver(const QString& name,QPoint pos, QSize size,
278  QWidget* parent):
279  BASE(parent),
280  name_(name),
281  pos_(pos),
282  size_(size),
283  visible_(true),
284  default_(true)
285  {
286  set_state_();
287  }
288 
289  void ResetStateSaver(const QString& name)
290  {
291  name_=name;
292  set_state_();
293  }
294 
295 protected:
296 
297  void set_state_()
298  {
299  QSettings settings;
300  settings.beginGroup(name_);
301  if(settings.contains("pos") && ! WidgetStateSaverConfig::SettingsIgnored()){
302  BASE::move(settings.value("pos").toPoint());
303  BASE::resize(settings.value("size").toSize());
304  //BASE::setVisible(settings.value("visible").toBool());
305  if(dynamic_cast<QMainWindow* >(this)){
306  dynamic_cast<QMainWindow* >(this)->restoreState(settings.value("state").toByteArray());
307  }
308  }else if(default_){
309  BASE::move(pos_);
310  BASE::resize(size_);
311  //BASE::setVisible(visible_);
312  }
313  settings.endGroup();
314  }
315 
317  {
318  QSettings settings;
319  settings.beginGroup(name_);
320  settings.setValue("pos", BASE::pos());
321  settings.setValue("size", BASE::size());
322  //settings.setValue("visible", /*BASE::isVisible()*/visible_);
323  if(dynamic_cast<QMainWindow* >(this)){
324  settings.setValue("state", dynamic_cast<QMainWindow* >(this)->saveState());
325  }
326  settings.endGroup();
327  }
328  QString name_;
329  QPoint pos_;
330  QSize size_;
331  bool visible_;
332  bool default_;
333 };
334 
335 }}//ns
336 
337 #endif /*WIDGET_STATE_SAVER_HH_*/