Added real animations to the Shogi animator.
[tagua.git] / src / option.h
blob5b012a37f2da5f7a68594d90c18e077b6eaac1e9
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;
47 template<typename O>
48 QList<boost::shared_ptr<O> > options_list_duplicate(const QList<boost::shared_ptr<O> >& o) {
49 QList<boost::shared_ptr<O> > retv;
50 for(int i=0;i<o.size();i++)
51 retv << boost::static_pointer_cast<O,BaseOpt>(o[i]->clone());
52 return retv;
55 template<typename O1, typename O2>
56 bool options_list_equals(const QList<boost::shared_ptr<O1> >& o1,
57 const QList<boost::shared_ptr<O2> >& o2) {
58 if(o1.size() != o2.size())
59 return false;
60 for(int i=0;i<o1.size();i++)
61 if(!o1[i]->equals(*o2[i]))
62 return false;
63 return true;
66 void dump_options_list(OptList& options, int indent = 0);
68 class BaseOpt {
69 private:
70 QString m_name;
71 QString m_label;
72 public:
73 BaseOpt(const QString& name, const QString& label)
74 : m_name(name)
75 , m_label(label) {}
76 virtual ~BaseOpt() {};
77 QString name() const { return m_name; }
78 QString label() const { return m_label; }
79 virtual OptPtr clone() const = 0;
80 virtual bool equals(const BaseOpt&) const = 0;
83 class BoolOpt : public BaseOpt {
84 //private:
85 public:
86 bool m_value;
87 QList<OptPtr> m_sub_options;
88 public:
89 typedef bool ValueType;
90 BoolOpt(const QString& name, const QString& label, bool def = false,
91 const QList<OptPtr>& suboptions = QList<OptPtr>())
92 : BaseOpt(name, label)
93 , m_value(def)
94 , m_sub_options(suboptions) {}
95 bool value() const { return m_value; }
96 void setValue(bool v) { m_value = v; }
97 OptList subOptions() { return m_sub_options; }
98 virtual OptPtr clone() const {
99 BoolOpt *o = new BoolOpt(name(), label(), m_value, options_list_duplicate(m_sub_options) );
100 return OptPtr(o);
102 virtual bool equals(const BaseOpt& _o) const {
103 const BoolOpt* o = dynamic_cast<const BoolOpt*>(&_o);
104 return o
105 && m_value != o->m_value
106 && options_list_equals(m_sub_options, o->m_sub_options);
110 class IntOpt : public BaseOpt {
111 private:
112 int m_min;
113 int m_max;
114 int m_value;
115 public:
116 typedef int ValueType;
117 IntOpt(const QString& name, const QString& label, int def, int min, int max)
118 : BaseOpt(name, label)
119 , m_min(min)
120 , m_max(max)
121 , m_value(def) {}
122 int min() const { return m_min; }
123 int max() const { return m_max; }
124 int value() const { return m_value; }
125 void setValue(int v) { m_value = v; }
126 virtual OptPtr clone() const {
127 IntOpt *o = new IntOpt(name(), label(), m_value, m_min, m_max);
128 return OptPtr(o);
130 virtual bool equals(const BaseOpt& _o) const {
131 const IntOpt* o = dynamic_cast<const IntOpt*>(&_o);
132 return o
133 && m_min != o->m_min
134 && m_max != o->m_max
135 && m_value != o->m_value;
139 class StringOpt : public BaseOpt {
140 private:
141 QString m_value;
142 public:
143 typedef QString ValueType;
144 StringOpt(const QString& name, const QString& label, QString def = QString())
145 : BaseOpt(name, label)
146 , m_value(def) {}
147 QString value() const { return m_value; }
148 void setValue(QString v) { m_value = v; }
149 virtual OptPtr clone() const {
150 StringOpt *o = new StringOpt(name(), label(), m_value);
151 return OptPtr(o);
153 virtual bool equals(const BaseOpt& _o) const {
154 const StringOpt* o = dynamic_cast<const StringOpt*>(&_o);
155 return o
156 && m_value != o->m_value;
160 class UrlOpt : public BaseOpt {
161 private:
162 QString m_value;
163 public:
164 typedef QString ValueType;
165 UrlOpt(const QString& name, const QString& label, QString def = QString())
166 : BaseOpt(name, label)
167 , m_value(def) {}
168 QString value() const { return m_value; }
169 void setValue(QString v) { m_value = v; }
170 virtual OptPtr clone() const {
171 UrlOpt *o = new UrlOpt(name(), label(), m_value);
172 return OptPtr(o);
174 virtual bool equals(const BaseOpt& _o) const {
175 const UrlOpt* o = dynamic_cast<const UrlOpt*>(&_o);
176 return o
177 && m_value != o->m_value;
181 class ColorOpt : public BaseOpt {
182 private:
183 QColor m_value;
184 public:
185 typedef QColor ValueType;
186 ColorOpt(const QString& name, const QString& label, QColor def = Qt::black)
187 : BaseOpt(name, label)
188 , m_value(def) {}
189 QColor value() const { return m_value; }
190 void setValue(QColor v) { m_value = v; }
191 virtual OptPtr clone() const {
192 ColorOpt *o = new ColorOpt(name(), label(), m_value);
193 return OptPtr(o);
195 virtual bool equals(const BaseOpt& _o) const {
196 const ColorOpt* o = dynamic_cast<const ColorOpt*>(&_o);
197 return o
198 && m_value != o->m_value;
202 class FontOpt : public BaseOpt {
203 private:
204 QFont m_value;
205 public:
206 typedef QFont ValueType;
207 FontOpt(const QString& name, const QString& label, QFont def = QApplication::font())
208 : BaseOpt(name, label)
209 , m_value(def) {}
210 QFont value() const { return m_value; }
211 void setValue(QFont v) { m_value = v; }
212 virtual OptPtr clone() const {
213 FontOpt *o = new FontOpt(name(), label(), m_value);
214 return OptPtr(o);
216 virtual bool equals(const BaseOpt& _o) const {
217 const FontOpt* o = dynamic_cast<const FontOpt*>(&_o);
218 return o
219 && m_value != o->m_value;
223 class ComboOpt : public BaseOpt {
224 //private:
225 public:
226 QStringList m_values;
227 int m_selected;
228 public:
229 typedef int ValueType;
230 ComboOpt(const QString& name, const QString& label, const QStringList& values, int selected = 0)
231 : BaseOpt(name, label)
232 , m_values(values)
233 , m_selected(selected) {}
234 int value() const { return m_selected; }
235 int selected() const { return m_selected; }
236 void setSelected(int v) { m_selected = v; }
237 virtual OptPtr clone() const {
238 ComboOpt *o = new ComboOpt(name(), label(), m_values, m_selected);
239 return OptPtr(o);
241 virtual bool equals(const BaseOpt& _o) const {
242 const ComboOpt* o = dynamic_cast<const ComboOpt*>(&_o);
243 return o
244 && m_values == o->m_values
245 && m_selected != o->m_selected;
249 class SelectOpt : public BaseOpt {
250 //private:
251 public:
252 QList<BoolOptPtr> m_options;
253 int m_selected;
255 public:
256 typedef int ValueType;
257 SelectOpt(const QString& name, const QString& label, QList<BoolOptPtr> options,
258 int selected = 0)
259 : BaseOpt(name, label)
260 , m_options(options) {
261 setSelected(selected);
263 int value() const { return m_selected; }
264 int selected() const { return m_selected; }
265 void setSelected(int s) {
266 m_selected = std::min(std::max(s, 0), m_options.size()-1);
267 for(int i=0;i<m_options.size();i++)
268 m_options[i]->setValue(i==m_selected);
270 BoolOptList options(){ return m_options; }
271 virtual OptPtr clone() const {
272 return OptPtr(new SelectOpt(name(), label(), options_list_duplicate(m_options), m_selected));
274 virtual bool equals(const BaseOpt& _o) const {
275 const SelectOpt* o = dynamic_cast<const SelectOpt*>(&_o);
276 return o
277 && m_selected != o->m_selected
278 && options_list_equals(m_options, o->m_options);
284 template<typename O>
285 boost::shared_ptr<O> options_list_find(const OptList& o, const QString& name) {
286 for(int i=0;i<o.size();i++)
287 if(o[i]->name() == name)
288 if(boost::shared_ptr<O> retv = boost::dynamic_pointer_cast<O, BaseOpt>(o[i]))
289 return retv;
290 return boost::shared_ptr<O>();
293 template<typename O>
294 typename O::ValueType options_list_find(const OptList& o, const QString& name,
295 const typename O::ValueType& def) {
296 for(int i=0;i<o.size();i++)
297 if(o[i]->name() == name)
298 if(boost::shared_ptr<O> retv = boost::dynamic_pointer_cast<O, BaseOpt>(o[i]))
299 return retv->value();
300 return def;
303 bool options_list_load_from_settings(OptList&, const Settings& s);
304 void options_list_save_to_settings(const OptList&, Settings s);
307 class OptionWidget : public QWidget {
308 Q_OBJECT
309 QList<OptPtr> m_options;
310 void setupOptionWidget(QWidget*, OptList&, bool indent = false);
311 void notifyChange();
313 friend class OptCheckBox;
314 friend class OptRadioButton;
315 friend class OptSpinBox;
316 friend class OptLineEdit;
317 friend class OptColorButton;
318 friend class OptFontRequester;
319 friend class OptUrlRequester;
320 friend class OptComboBox;
322 public:
323 OptionWidget(const OptList& options, QWidget* parent = NULL);
325 signals:
326 void changed(const OptList& options);
329 #endif //OPTION_H