Replaced (almost) all occurrences of 'kboard' with 'tagua'.
[tagua/yd.git] / src / option.h
blobe45c1f65a259ba4996990e08802eca138c558da1
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@sns.it>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 */
11 #ifndef OPTION_H
12 #define OPTION_H
14 #include <list>
15 #include <boost/shared_ptr.hpp>
16 #include <QString>
17 #include <QFont>
18 #include <QColor>
19 #include <QApplication>
20 #include <QList>
21 #include <QStringList>
22 #include <QWidget>
24 class Settings;
26 class BaseOpt;
27 class BoolOpt;
28 class IntOpt;
29 class StringOpt;
30 class ColorOpt;
31 class FontOpt;
32 class ComboOpt;
33 class SelectOpt;
34 class UrlOpt;
35 typedef boost::shared_ptr<BaseOpt> OptPtr;
36 typedef boost::shared_ptr<BoolOpt> BoolOptPtr;
37 typedef boost::shared_ptr<IntOpt> IntOptPtr;
38 typedef boost::shared_ptr<StringOpt> StringOptPtr;
39 typedef boost::shared_ptr<ColorOpt> ColorOptPtr;
40 typedef boost::shared_ptr<FontOpt> FontOptPtr;
41 typedef boost::shared_ptr<ComboOpt> ComboOptPtr;
42 typedef boost::shared_ptr<SelectOpt> SelectOptPtr;
43 typedef boost::shared_ptr<UrlOpt> UrlOptPtr;
44 typedef QList<OptPtr> OptList;
45 typedef QList<BoolOptPtr> BoolOptList;
46 typedef boost::shared_ptr<OptList> OptListPtr;
47 typedef boost::shared_ptr<BoolOptList> BoolOptListPtr;
49 template<typename O>
50 QList<boost::shared_ptr<O> > options_list_duplicate(const QList<boost::shared_ptr<O> >& o) {
51 QList<boost::shared_ptr<O> > retv;
52 for(int i=0;i<o.size();i++)
53 retv << boost::static_pointer_cast<O,BaseOpt>(o[i]->clone());
54 return retv;
57 template<typename O1, typename O2>
58 bool options_list_equals(const QList<boost::shared_ptr<O1> >& o1,
59 const QList<boost::shared_ptr<O2> >& o2) {
60 if(o1.size() != o2.size())
61 return false;
62 for(int i=0;i<o1.size();i++)
63 if(!o1[i]->equals(*o2[i]))
64 return false;
65 return true;
68 void dump_options_list(OptList& options, int indent = 0);
70 class BaseOpt {
71 private:
72 QString m_name;
73 QString m_label;
74 public:
75 BaseOpt(const QString& name, const QString& label)
76 : m_name(name)
77 , m_label(label) {}
78 virtual ~BaseOpt() {};
79 QString name() const { return m_name; }
80 QString label() const { return m_label; }
81 virtual OptPtr clone() const = 0;
82 virtual bool equals(const BaseOpt&) const = 0;
85 class BoolOpt : public BaseOpt {
86 //private:
87 public:
88 bool m_value;
89 QList<OptPtr> m_sub_options;
90 public:
91 typedef bool ValueType;
92 BoolOpt(const QString& name, const QString& label, bool def = false,
93 const QList<OptPtr>& suboptions = QList<OptPtr>())
94 : BaseOpt(name, label)
95 , m_value(def)
96 , m_sub_options(suboptions) {}
97 bool value() const { return m_value; }
98 void setValue(bool v) { m_value = v; }
99 OptList subOptions() { return m_sub_options; }
100 virtual OptPtr clone() const {
101 BoolOpt *o = new BoolOpt(name(), label(), m_value, options_list_duplicate(m_sub_options) );
102 return OptPtr(o);
104 virtual bool equals(const BaseOpt& _o) const {
105 const BoolOpt* o = dynamic_cast<const BoolOpt*>(&_o);
106 return o
107 && m_value != o->m_value
108 && options_list_equals(m_sub_options, o->m_sub_options);
112 class IntOpt : public BaseOpt {
113 private:
114 int m_min;
115 int m_max;
116 int m_value;
117 public:
118 typedef int ValueType;
119 IntOpt(const QString& name, const QString& label, int def, int min, int max)
120 : BaseOpt(name, label)
121 , m_min(min)
122 , m_max(max)
123 , m_value(def) {}
124 int min() const { return m_min; }
125 int max() const { return m_max; }
126 int value() const { return m_value; }
127 void setValue(int v) { m_value = v; }
128 virtual OptPtr clone() const {
129 IntOpt *o = new IntOpt(name(), label(), m_value, m_min, m_max);
130 return OptPtr(o);
132 virtual bool equals(const BaseOpt& _o) const {
133 const IntOpt* o = dynamic_cast<const IntOpt*>(&_o);
134 return o
135 && m_min != o->m_min
136 && m_max != o->m_max
137 && m_value != o->m_value;
141 class StringOpt : public BaseOpt {
142 private:
143 QString m_value;
144 public:
145 typedef QString ValueType;
146 StringOpt(const QString& name, const QString& label, QString def = QString())
147 : BaseOpt(name, label)
148 , m_value(def) {}
149 QString value() const { return m_value; }
150 void setValue(QString v) { m_value = v; }
151 virtual OptPtr clone() const {
152 StringOpt *o = new StringOpt(name(), label(), m_value);
153 return OptPtr(o);
155 virtual bool equals(const BaseOpt& _o) const {
156 const StringOpt* o = dynamic_cast<const StringOpt*>(&_o);
157 return o
158 && m_value != o->m_value;
162 class UrlOpt : public BaseOpt {
163 private:
164 QString m_value;
165 public:
166 typedef QString ValueType;
167 UrlOpt(const QString& name, const QString& label, QString def = QString() )
168 : BaseOpt(name, label)
169 , m_value(def) {}
170 QString value() const { return m_value; }
171 void setValue(QString v) { m_value = v; }
172 virtual OptPtr clone() const {
173 UrlOpt *o = new UrlOpt(name(), label(), m_value);
174 return OptPtr(o);
176 virtual bool equals(const BaseOpt& _o) const {
177 const UrlOpt* o = dynamic_cast<const UrlOpt*>(&_o);
178 return o
179 && m_value != o->m_value;
183 class ColorOpt : public BaseOpt {
184 private:
185 QColor m_value;
186 public:
187 typedef QColor ValueType;
188 ColorOpt(const QString& name, const QString& label, QColor def = Qt::black)
189 : BaseOpt(name, label)
190 , m_value(def) {}
191 QColor value() const { return m_value; }
192 void setValue(QColor v) { m_value = v; }
193 virtual OptPtr clone() const {
194 ColorOpt *o = new ColorOpt(name(), label(), m_value);
195 return OptPtr(o);
197 virtual bool equals(const BaseOpt& _o) const {
198 const ColorOpt* o = dynamic_cast<const ColorOpt*>(&_o);
199 return o
200 && m_value != o->m_value;
204 class FontOpt : public BaseOpt {
205 private:
206 QFont m_value;
207 public:
208 typedef QFont ValueType;
209 FontOpt(const QString& name, const QString& label, QFont def = QApplication::font())
210 : BaseOpt(name, label)
211 , m_value(def) {}
212 QFont value() const { return m_value; }
213 void setValue(QFont v) { m_value = v; }
214 virtual OptPtr clone() const {
215 FontOpt *o = new FontOpt(name(), label(), m_value);
216 return OptPtr(o);
218 virtual bool equals(const BaseOpt& _o) const {
219 const FontOpt* o = dynamic_cast<const FontOpt*>(&_o);
220 return o
221 && m_value != o->m_value;
225 class ComboOpt : public BaseOpt {
226 //private:
227 public:
228 QStringList m_values;
229 int m_selected;
230 public:
231 typedef int ValueType;
232 ComboOpt(const QString& name, const QString& label, const QStringList& values, int selected = 0)
233 : BaseOpt(name, label)
234 , m_values(values)
235 , m_selected(selected) {}
236 int value() const { return m_selected; }
237 int selected() const { return m_selected; }
238 void setSelected(int v) { m_selected = v; }
239 virtual OptPtr clone() const {
240 ComboOpt *o = new ComboOpt(name(), label(), m_values, m_selected);
241 return OptPtr(o);
243 virtual bool equals(const BaseOpt& _o) const {
244 const ComboOpt* o = dynamic_cast<const ComboOpt*>(&_o);
245 return o
246 && m_values == o->m_values
247 && m_selected != o->m_selected;
251 class SelectOpt : public BaseOpt {
252 //private:
253 public:
254 QList<BoolOptPtr> m_options;
255 int m_selected;
257 public:
258 typedef int ValueType;
259 SelectOpt(const QString& name, const QString& label, QList<BoolOptPtr> options,
260 int selected = 0)
261 : BaseOpt(name, label)
262 , m_options(options) {
263 setSelected(selected);
265 int value() const { return m_selected; }
266 int selected() const { return m_selected; }
267 void setSelected(int s) {
268 m_selected = std::min(std::max(s, 0), m_options.size()-1);
269 for(int i=0;i<m_options.size();i++)
270 m_options[i]->setValue(i==m_selected);
272 BoolOptList options(){ return m_options; }
273 virtual OptPtr clone() const {
274 return OptPtr(new SelectOpt(name(), label(), options_list_duplicate(m_options), m_selected));
276 virtual bool equals(const BaseOpt& _o) const {
277 const SelectOpt* o = dynamic_cast<const SelectOpt*>(&_o);
278 return o
279 && m_selected != o->m_selected
280 && options_list_equals(m_options, o->m_options);
286 template<typename O>
287 boost::shared_ptr<O> options_list_find(const OptList& o, const QString& name) {
288 for(int i=0;i<o.size();i++)
289 if(o[i]->name() == name)
290 if(boost::shared_ptr<O> retv = boost::dynamic_pointer_cast<O, BaseOpt>(o[i]))
291 return retv;
292 return boost::shared_ptr<O>();
295 template<typename O>
296 typename O::ValueType options_list_find(const OptList& o, const QString& name,
297 const typename O::ValueType& def) {
298 for(int i=0;i<o.size();i++)
299 if(o[i]->name() == name)
300 if(boost::shared_ptr<O> retv = boost::dynamic_pointer_cast<O, BaseOpt>(o[i]))
301 return retv->value();
302 return def;
305 bool options_list_load_from_settings(OptList&, const Settings& s);
306 void options_list_save_to_settings(const OptList&, Settings s);
309 class OptionWidget : public QWidget {
310 Q_OBJECT
311 bool m_changed;
312 bool m_dont_fire;
313 QList<OptPtr> m_options;
314 void setupOptionWidget(QWidget*, OptList&, bool indent = false);
315 void setOptionWidgetValues(QWidget*, OptList&);
316 void notifyChange();
318 friend class OptCheckBox;
319 friend class OptRadioButton;
320 friend class OptSpinBox;
321 friend class OptLineEdit;
322 friend class OptColorButton;
323 friend class OptFontRequester;
324 friend class OptUrlRequester;
325 friend class OptComboBox;
327 public:
328 OptionWidget(const OptList& options, QWidget* parent = NULL);
329 void setValues(OptList& newopts);
331 signals:
332 void changed(const OptList& options);
335 #endif //OPTION_H