summaryrefslogtreecommitdiffstats
path: root/src/common/gui/config_widget.h
blob: e981aac025cfbf1bb9f3d864a60d48d637064e12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/***************************************************************************
 *   Copyright (C) 2005-2007 Nicolas Hadacek <hadacek@kde.org>             *
 *   Copyright (C) 2004 Alain Gibaud <alain.gibaud@free.fr>                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/
#ifndef CONFIG_WIDGET_H
#define CONFIG_WIDGET_H

#include <tqpixmap.h>
#include <tqcheckbox.h>
#include <tqvaluevector.h>
#include <tqvariant.h>

#include "container.h"

//-----------------------------------------------------------------------------
class ConfigWidget : public Container
{
Q_OBJECT
  
public:
  ConfigWidget(TQWidget *parent = 0) : Container(parent) {}
  virtual TQString title() const { return TQString(); }
  virtual TQString header() const { return TQString(); }
  virtual TQPixmap pixmap() const { return TQPixmap(); }

public slots:
  virtual void loadConfig() = 0;
  virtual void saveConfig() = 0;
};

//-----------------------------------------------------------------------------
template <typename Type>
class BaseConfigWidget
{
public:
  BaseConfigWidget(ConfigWidget *widget) {
    _widgets.resize(Type::Nb_Types);
    for(Type type; type<Type::Nb_Types; ++type) _widgets[type.type()] = createWidget(type, widget);
  }
  void loadConfig() {
    for(Type type; type<Type::Nb_Types; ++type) load(type, _widgets[type.type()]);
  }
  void saveConfig() {
    for(Type type; type<Type::Nb_Types; ++type) save(type, _widgets[type.type()]);
  }
  void setDefault() {
    for(Type type; type<Type::Nb_Types; ++type) setDefault(type, _widgets[type.type()]);
  }

private:
  TQValueVector<TQWidget *> _widgets;

  static TQWidget *createWidget(Type type, ConfigWidget *widget) {
    TQWidget *w = 0;
    uint row = widget->numRows();
    switch (type.data().defValue.type()) {
      case TQVariant::Bool:
        w = new TQCheckBox(type.label(), widget);
        widget->addWidget(w, row,row, 0,1);
        break;
      default: Q_ASSERT(false); break;
    }
    return w;
  }
  void load(Type type, TQWidget *widget) const {
    switch (type.data().defValue.type()) {
      case TQVariant::Bool:
        static_cast<TQCheckBox *>(widget)->setChecked(readConfigEntry(type).toBool());
        break;
      default: Q_ASSERT(false); break;
    }
  }
  void save(Type type, TQWidget *widget) {
    switch (type.data().defValue.type()) {
      case TQVariant::Bool:
        writeConfigEntry(type, TQVariant(static_cast<TQCheckBox *>(widget)->isChecked()));
        break;
      default: Q_ASSERT(false); break;
    }
  }
  void setDefault(Type type, TQWidget *widget) const {
    switch (type.data().defValue.type()) {
      case TQVariant::Bool:
        static_cast<TQCheckBox *>(widget)->setChecked(type.data().defValue.toBool());
        break;
      default: Q_ASSERT(false); break;
    }
  }
};

//-----------------------------------------------------------------------------
#define BEGIN_DECLARE_CONFIG_WIDGET(ConfigType, ConfigWidgetType) \
class ConfigWidgetType : public ::ConfigWidget, public BaseConfigWidget<ConfigType> \
{ \
public: \
  ConfigWidgetType() : BaseConfigWidget<ConfigType>(this) {} \
  virtual void loadConfig() { BaseConfigWidget<ConfigType>::loadConfig(); } \
  virtual void saveConfig() { BaseConfigWidget<ConfigType>::saveConfig(); } \

#define END_DECLARE_CONFIG_WIDGET \
};

#endif