Prepare 1.0 alpha3 release.
[tagua/yd.git] / src / option.h
blobfbd427c323a38ac99bb15e34bd49e1e144199206
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
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(const 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 friend class OptionWidget;
88 friend bool options_list_load_from_settings(OptList& options, const Settings& s);
90 bool m_value;
91 QList<OptPtr> m_sub_options;
92 public:
93 typedef bool ValueType;
94 BoolOpt(const QString& name, const QString& label, bool def = false,
95 const QList<OptPtr>& suboptions = QList<OptPtr>())
96 : BaseOpt(name, label)
97 , m_value(def)
98 , m_sub_options(suboptions) {}
99 bool value() const { return m_value; }
100 void setValue(bool v) { m_value = v; }
101 OptList subOptions() { return m_sub_options; }
102 virtual OptPtr clone() const {
103 BoolOpt *o = new BoolOpt(name(), label(), m_value, options_list_duplicate(m_sub_options) );
104 return OptPtr(o);
106 virtual bool equals(const BaseOpt& _o) const {
107 const BoolOpt* o = dynamic_cast<const BoolOpt*>(&_o);
108 return o
109 && m_value != o->m_value
110 && options_list_equals(m_sub_options, o->m_sub_options);
114 class IntOpt : public BaseOpt {
115 public:
116 enum Visualization {
117 SpinBox,
118 Slider
120 private:
121 int m_min;
122 int m_max;
123 int m_value;
124 Visualization m_visualization;
125 public:
126 typedef int ValueType;
127 IntOpt(const QString& name, const QString& label, int def, int min, int max, Visualization visualization)
128 : BaseOpt(name, label)
129 , m_min(min)
130 , m_max(max)
131 , m_value(def)
132 , m_visualization(visualization) {}
133 int min() const { return m_min; }
134 int max() const { return m_max; }
135 int value() const { return m_value; }
136 Visualization visualization() const { return m_visualization; }
137 void setValue(int v) { m_value = v; }
138 virtual OptPtr clone() const {
139 IntOpt *o = new IntOpt(name(), label(), m_value, m_min, m_max, m_visualization);
140 return OptPtr(o);
142 virtual bool equals(const BaseOpt& _o) const {
143 const IntOpt* o = dynamic_cast<const IntOpt*>(&_o);
144 return o
145 && m_min != o->m_min
146 && m_max != o->m_max
147 && m_value != o->m_value
148 && m_visualization != o->m_visualization;
152 class StringOpt : public BaseOpt {
153 private:
154 QString m_value;
155 public:
156 typedef QString ValueType;
157 StringOpt(const QString& name, const QString& label, QString def = QString())
158 : BaseOpt(name, label)
159 , m_value(def) {}
160 QString value() const { return m_value; }
161 void setValue(QString v) { m_value = v; }
162 virtual OptPtr clone() const {
163 StringOpt *o = new StringOpt(name(), label(), m_value);
164 return OptPtr(o);
166 virtual bool equals(const BaseOpt& _o) const {
167 const StringOpt* o = dynamic_cast<const StringOpt*>(&_o);
168 return o
169 && m_value != o->m_value;
173 class UrlOpt : public BaseOpt {
174 private:
175 QString m_value;
176 public:
177 typedef QString ValueType;
178 UrlOpt(const QString& name, const QString& label, QString def = QString() )
179 : BaseOpt(name, label)
180 , m_value(def) {}
181 QString value() const { return m_value; }
182 void setValue(QString v) { m_value = v; }
183 virtual OptPtr clone() const {
184 UrlOpt *o = new UrlOpt(name(), label(), m_value);
185 return OptPtr(o);
187 virtual bool equals(const BaseOpt& _o) const {
188 const UrlOpt* o = dynamic_cast<const UrlOpt*>(&_o);
189 return o
190 && m_value != o->m_value;
194 class ColorOpt : public BaseOpt {
195 private:
196 QColor m_value;
197 public:
198 typedef QColor ValueType;
199 ColorOpt(const QString& name, const QString& label, QColor def = Qt::black)
200 : BaseOpt(name, label)
201 , m_value(def) {}
202 QColor value() const { return m_value; }
203 void setValue(QColor v) { m_value = v; }
204 virtual OptPtr clone() const {
205 ColorOpt *o = new ColorOpt(name(), label(), m_value);
206 return OptPtr(o);
208 virtual bool equals(const BaseOpt& _o) const {
209 const ColorOpt* o = dynamic_cast<const ColorOpt*>(&_o);
210 return o
211 && m_value != o->m_value;
215 class FontOpt : public BaseOpt {
216 private:
217 QFont m_value;
218 public:
219 typedef QFont ValueType;
220 FontOpt(const QString& name, const QString& label, QFont def = QApplication::font())
221 : BaseOpt(name, label)
222 , m_value(def) {}
223 QFont value() const { return m_value; }
224 void setValue(QFont v) { m_value = v; }
225 virtual OptPtr clone() const {
226 FontOpt *o = new FontOpt(name(), label(), m_value);
227 return OptPtr(o);
229 virtual bool equals(const BaseOpt& _o) const {
230 const FontOpt* o = dynamic_cast<const FontOpt*>(&_o);
231 return o
232 && m_value != o->m_value;
236 class ComboOpt : public BaseOpt {
237 private:
238 QStringList m_values;
239 int m_selected;
240 public:
241 typedef int ValueType;
242 ComboOpt(const QString& name, const QString& label, const QStringList& values, int selected = 0)
243 : BaseOpt(name, label)
244 , m_values(values)
245 , m_selected(selected) {}
246 int value() const { return m_selected; }
247 int selected() const { return m_selected; }
248 void setSelected(int v) { m_selected = v; }
249 QStringList values() const { return m_values; }
250 virtual OptPtr clone() const {
251 ComboOpt *o = new ComboOpt(name(), label(), m_values, m_selected);
252 return OptPtr(o);
254 virtual bool equals(const BaseOpt& _o) const {
255 const ComboOpt* o = dynamic_cast<const ComboOpt*>(&_o);
256 return o
257 && m_values == o->m_values
258 && m_selected != o->m_selected;
262 class SelectOpt : public BaseOpt {
263 private:
264 friend class OptionWidget;
265 friend bool options_list_load_from_settings(OptList& options, const Settings& s);
267 QList<BoolOptPtr> m_options;
268 int m_selected;
270 public:
271 typedef int ValueType;
272 SelectOpt(const QString& name, const QString& label, QList<BoolOptPtr> options,
273 int selected = 0)
274 : BaseOpt(name, label)
275 , m_options(options) {
276 setSelected(selected);
278 int value() const { return m_selected; }
279 int selected() const { return m_selected; }
280 void setSelected(int s) {
281 m_selected = std::min(std::max(s, 0), m_options.size()-1);
282 for(int i=0;i<m_options.size();i++)
283 m_options[i]->setValue(i==m_selected);
285 BoolOptList options(){ return m_options; }
286 virtual OptPtr clone() const {
287 return OptPtr(new SelectOpt(name(), label(), options_list_duplicate(m_options), m_selected));
289 virtual bool equals(const BaseOpt& _o) const {
290 const SelectOpt* o = dynamic_cast<const SelectOpt*>(&_o);
291 return o
292 && m_selected != o->m_selected
293 && options_list_equals(m_options, o->m_options);
299 template<typename O>
300 boost::shared_ptr<O> options_list_find(const OptList& o, const QString& name) {
301 for(int i=0;i<o.size();i++)
302 if(o[i]->name() == name)
303 if(boost::shared_ptr<O> retv = boost::dynamic_pointer_cast<O, BaseOpt>(o[i]))
304 return retv;
305 return boost::shared_ptr<O>();
308 template<typename O>
309 typename O::ValueType options_list_find(const OptList& o, const QString& name,
310 const typename O::ValueType& def) {
311 for(int i=0;i<o.size();i++)
312 if(o[i]->name() == name)
313 if(boost::shared_ptr<O> retv = boost::dynamic_pointer_cast<O, BaseOpt>(o[i]))
314 return retv->value();
315 return def;
318 bool options_list_load_from_settings(OptList&, const Settings& s);
319 void options_list_save_to_settings(const OptList&, Settings s);
322 class OptionWidget : public QWidget {
323 Q_OBJECT
324 bool m_changed;
325 bool m_dont_fire;
326 QList<OptPtr> m_options;
327 void setupOptionWidget(QWidget*, OptList&, bool indent = false);
328 void setOptionWidgetValues(QWidget*, OptList&);
329 void notifyChange();
331 friend class OptCheckBox;
332 friend class OptRadioButton;
333 friend class OptSpinBox;
334 friend class OptSlider;
335 friend class OptLineEdit;
336 friend class OptColorButton;
337 friend class OptFontRequester;
338 friend class OptUrlRequester;
339 friend class OptComboBox;
341 public:
342 OptionWidget(const OptList& options, QWidget* parent = NULL);
343 void setValues(OptList& newopts);
345 Q_SIGNALS:
346 void changed(const OptList& options);
349 #endif //OPTION_H