00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef WIDGET_STATE_SAVER_HH_
00020 #define WIDGET_STATE_SAVER_HH_
00021
00022
00023
00024
00025
00026 #include <ost/gui/module_config.hh>
00027
00028 #include <QSettings>
00029 #include <QPoint>
00030 #include <QSize>
00031 #include <QMainWindow>
00032 #include <QDebug>
00033
00034 namespace ost{namespace gui{
00035
00036 class DLLEXPORT_OST_GUI WidgetStateSaverConfig
00037 {
00038 public:
00039 static void IgnoreSettings(bool flag=true);
00040 static bool SettingsIgnored();
00041 protected:
00042 static bool flag_;
00043 };
00044
00045 struct unused;
00046 template<class BASE, typename T1=unused, typename T2=unused, typename T3=unused>
00047 class WidgetStateSaver: public BASE
00048 {
00049 public:
00050 WidgetStateSaver(const QString& name, QWidget * parent ,T1 t1, T2 t2,T3 t3):
00051 BASE(parent,t1,t2,t3),
00052 name_(name),
00053 pos_(),
00054 size_(),
00055 visible_(true),
00056 default_(false)
00057 {
00058 set_state_();
00059 }
00060
00061 WidgetStateSaver(const QString& name,QPoint pos, QSize size, QWidget * parent ,T1 t1, T2 t2,T3 t3):
00062 BASE(parent,t1,t2,t3),
00063 name_(name),
00064 pos_(pos),
00065 size_(size),
00066 visible_(true),
00067 default_(true)
00068 {
00069 set_state_();
00070 }
00071
00072 void ResetStateSaver(const QString& name)
00073 {
00074 name_=name;
00075 set_state_();
00076 }
00077
00078 protected:
00079
00080 void set_state_()
00081 {
00082 QSettings settings;
00083 settings.beginGroup(name_);
00084 if(settings.contains("pos") && ! WidgetStateSaverConfig::SettingsIgnored()){
00085 BASE::move(settings.value("pos").toPoint());
00086 BASE::resize(settings.value("size").toSize());
00087
00088 if(dynamic_cast<QMainWindow* >(this)){
00089 dynamic_cast<QMainWindow* >(this)->restoreState(settings.value("state").toByteArray());
00090 }
00091 }else if(default_){
00092 BASE::move(pos_);
00093 BASE::resize(size_);
00094
00095 }
00096 settings.endGroup();
00097 }
00098
00099 virtual ~WidgetStateSaver()
00100 {
00101 QSettings settings;
00102 settings.beginGroup(name_);
00103 settings.setValue("pos", BASE::pos());
00104 settings.setValue("size", BASE::size());
00105
00106 if(dynamic_cast<QMainWindow* >(this)){
00107 settings.setValue("state", dynamic_cast<QMainWindow* >(this)->saveState());
00108 }
00109 settings.endGroup();
00110 }
00111 QString name_;
00112 QPoint pos_;
00113 QSize size_;
00114 bool visible_;
00115 bool default_;
00116 };
00117
00118 template<class BASE, typename T1, typename T2>
00119 class WidgetStateSaver<BASE,T1,T2>: public BASE
00120 {
00121 public:
00122 WidgetStateSaver(const QString& name, QWidget * parent ,T1 t1, T2 t2):
00123 BASE(parent,t1,t2),
00124 name_(name),
00125 pos_(),
00126 size_(),
00127 visible_(true),
00128 default_(false)
00129 {
00130 set_state_();
00131 }
00132
00133 WidgetStateSaver(const QString& name,QPoint pos, QSize size, bool visible,
00134 QWidget * parent ,T1 t1, T2 t2):
00135 BASE(parent,t1,t2),
00136 name_(name),
00137 pos_(pos),
00138 size_(size),
00139 visible_(visible),
00140 default_(true)
00141 {
00142 set_state_();
00143 }
00144
00145 void ResetStateSaver(const QString& name)
00146 {
00147 name_=name;
00148 set_state_();
00149 }
00150
00151 protected:
00152
00153 void set_state_()
00154 {
00155 QSettings settings;
00156 settings.beginGroup(name_);
00157 if(settings.contains("pos") && ! WidgetStateSaverConfig::SettingsIgnored()){
00158 BASE::move(settings.value("pos").toPoint());
00159 BASE::resize(settings.value("size").toSize());
00160
00161 if(dynamic_cast<QMainWindow* >(this)){
00162 dynamic_cast<QMainWindow* >(this)->restoreState(settings.value("state").toByteArray());
00163 }
00164 }else if(default_){
00165 BASE::move(pos_);
00166 BASE::resize(size_);
00167
00168 }
00169 settings.endGroup();
00170 }
00171
00172 virtual ~WidgetStateSaver()
00173 {
00174 QSettings settings;
00175 settings.beginGroup(name_);
00176 settings.setValue("pos", BASE::pos());
00177 settings.setValue("size", BASE::size());
00178
00179 if(dynamic_cast<QMainWindow* >(this)){
00180 settings.setValue("state", dynamic_cast<QMainWindow* >(this)->saveState());
00181 }
00182 settings.endGroup();
00183 }
00184 QString name_;
00185 QPoint pos_;
00186 QSize size_;
00187 bool visible_;
00188 bool default_;
00189 };
00190 template<class BASE, typename T1>
00191 class WidgetStateSaver<BASE,T1>: public BASE
00192 {
00193 public:
00194 WidgetStateSaver(const QString& name, QWidget * parent ,T1 t1):
00195 BASE(parent,t1),
00196 name_(name),
00197 pos_(),
00198 size_(),
00199 visible_(true),
00200 default_(false)
00201 {
00202 set_state_();
00203 }
00204
00205 WidgetStateSaver(const QString& name,QPoint pos, QSize size, bool visible,
00206 QWidget * parent ,T1 t1):
00207 BASE(parent,t1),
00208 name_(name),
00209 pos_(pos),
00210 size_(size),
00211 visible_(visible),
00212 default_(true)
00213 {
00214 set_state_();
00215 }
00216
00217 void ResetStateSaver(const QString& name)
00218 {
00219 name_=name;
00220 set_state_();
00221 }
00222
00223 protected:
00224
00225 void set_state_()
00226 {
00227 QSettings settings;
00228 settings.beginGroup(name_);
00229 if(settings.contains("pos") && ! WidgetStateSaverConfig::SettingsIgnored()){
00230 BASE::move(settings.value("pos").toPoint());
00231 BASE::resize(settings.value("size").toSize());
00232
00233 if(dynamic_cast<QMainWindow* >(this)){
00234 dynamic_cast<QMainWindow* >(this)->restoreState(settings.value("state").toByteArray());
00235 }
00236 }else if(default_){
00237 BASE::move(pos_);
00238 BASE::resize(size_);
00239
00240 }
00241 settings.endGroup();
00242 }
00243
00244 virtual ~WidgetStateSaver()
00245 {
00246 QSettings settings;
00247 settings.beginGroup(name_);
00248 settings.setValue("pos", BASE::pos());
00249 settings.setValue("size", BASE::size());
00250
00251 if(dynamic_cast<QMainWindow* >(this)){
00252 settings.setValue("state", dynamic_cast<QMainWindow* >(this)->saveState());
00253 }
00254 settings.endGroup();
00255 }
00256 QString name_;
00257 QPoint pos_;
00258 QSize size_;
00259 bool visible_;
00260 bool default_;
00261 };
00262 template<class BASE>
00263 class WidgetStateSaver<BASE>: public BASE
00264 {
00265 public:
00266 WidgetStateSaver(const QString& name, QWidget * parent ):
00267 BASE(parent),
00268 name_(name),
00269 pos_(),
00270 size_(),
00271 visible_(true),
00272 default_(false)
00273 {
00274 set_state_();
00275 }
00276
00277 WidgetStateSaver(const QString& name,QPoint pos, QSize size,
00278 QWidget* parent):
00279 BASE(parent),
00280 name_(name),
00281 pos_(pos),
00282 size_(size),
00283 visible_(true),
00284 default_(true)
00285 {
00286 set_state_();
00287 }
00288
00289 void ResetStateSaver(const QString& name)
00290 {
00291 name_=name;
00292 set_state_();
00293 }
00294
00295 protected:
00296
00297 void set_state_()
00298 {
00299 QSettings settings;
00300 settings.beginGroup(name_);
00301 if(settings.contains("pos") && ! WidgetStateSaverConfig::SettingsIgnored()){
00302 BASE::move(settings.value("pos").toPoint());
00303 BASE::resize(settings.value("size").toSize());
00304
00305 if(dynamic_cast<QMainWindow* >(this)){
00306 dynamic_cast<QMainWindow* >(this)->restoreState(settings.value("state").toByteArray());
00307 }
00308 }else if(default_){
00309 BASE::move(pos_);
00310 BASE::resize(size_);
00311
00312 }
00313 settings.endGroup();
00314 }
00315
00316 virtual ~WidgetStateSaver()
00317 {
00318 QSettings settings;
00319 settings.beginGroup(name_);
00320 settings.setValue("pos", BASE::pos());
00321 settings.setValue("size", BASE::size());
00322
00323 if(dynamic_cast<QMainWindow* >(this)){
00324 settings.setValue("state", dynamic_cast<QMainWindow* >(this)->saveState());
00325 }
00326 settings.endGroup();
00327 }
00328 QString name_;
00329 QPoint pos_;
00330 QSize size_;
00331 bool visible_;
00332 bool default_;
00333 };
00334
00335 }}
00336
00337 #endif