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.
15 #include <boost/shared_ptr.hpp>
19 #include <QApplication>
21 #include <QStringList>
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
;
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());
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())
62 for(int i
=0;i
<o1
.size();i
++)
63 if(!o1
[i
]->equals(*o2
[i
]))
68 void dump_options_list(const OptList
& options
, int indent
= 0);
75 BaseOpt(const QString
& name
, const QString
& 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
{
87 friend class OptionWidget
;
88 friend bool options_list_load_from_settings(OptList
& options
, const Settings
& s
);
91 QList
<OptPtr
> m_sub_options
;
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
)
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
) );
106 virtual bool equals(const BaseOpt
& _o
) const {
107 const BoolOpt
* o
= dynamic_cast<const BoolOpt
*>(&_o
);
109 && m_value
!= o
->m_value
110 && options_list_equals(m_sub_options
, o
->m_sub_options
);
114 class IntOpt
: public BaseOpt
{
124 Visualization m_visualization
;
126 typedef int ValueType
;
127 IntOpt(const QString
& name
, const QString
& label
, int def
, int min
, int max
, Visualization visualization
)
128 : BaseOpt(name
, label
)
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
);
142 virtual bool equals(const BaseOpt
& _o
) const {
143 const IntOpt
* o
= dynamic_cast<const IntOpt
*>(&_o
);
147 && m_value
!= o
->m_value
148 && m_visualization
!= o
->m_visualization
;
152 class StringOpt
: public BaseOpt
{
156 typedef QString ValueType
;
157 StringOpt(const QString
& name
, const QString
& label
, QString def
= QString())
158 : BaseOpt(name
, label
)
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
);
166 virtual bool equals(const BaseOpt
& _o
) const {
167 const StringOpt
* o
= dynamic_cast<const StringOpt
*>(&_o
);
169 && m_value
!= o
->m_value
;
173 class UrlOpt
: public BaseOpt
{
177 typedef QString ValueType
;
178 UrlOpt(const QString
& name
, const QString
& label
, QString def
= QString() )
179 : BaseOpt(name
, label
)
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
);
187 virtual bool equals(const BaseOpt
& _o
) const {
188 const UrlOpt
* o
= dynamic_cast<const UrlOpt
*>(&_o
);
190 && m_value
!= o
->m_value
;
194 class ColorOpt
: public BaseOpt
{
198 typedef QColor ValueType
;
199 ColorOpt(const QString
& name
, const QString
& label
, QColor def
= Qt::black
)
200 : BaseOpt(name
, label
)
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
);
208 virtual bool equals(const BaseOpt
& _o
) const {
209 const ColorOpt
* o
= dynamic_cast<const ColorOpt
*>(&_o
);
211 && m_value
!= o
->m_value
;
215 class FontOpt
: public BaseOpt
{
219 typedef QFont ValueType
;
220 FontOpt(const QString
& name
, const QString
& label
, QFont def
= QApplication::font())
221 : BaseOpt(name
, label
)
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
);
229 virtual bool equals(const BaseOpt
& _o
) const {
230 const FontOpt
* o
= dynamic_cast<const FontOpt
*>(&_o
);
232 && m_value
!= o
->m_value
;
236 class ComboOpt
: public BaseOpt
{
238 QStringList m_values
;
241 typedef int ValueType
;
242 ComboOpt(const QString
& name
, const QString
& label
, const QStringList
& values
, int selected
= 0)
243 : BaseOpt(name
, label
)
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
);
254 virtual bool equals(const BaseOpt
& _o
) const {
255 const ComboOpt
* o
= dynamic_cast<const ComboOpt
*>(&_o
);
257 && m_values
== o
->m_values
258 && m_selected
!= o
->m_selected
;
262 class SelectOpt
: public BaseOpt
{
264 friend class OptionWidget
;
265 friend bool options_list_load_from_settings(OptList
& options
, const Settings
& s
);
267 QList
<BoolOptPtr
> m_options
;
271 typedef int ValueType
;
272 SelectOpt(const QString
& name
, const QString
& label
, QList
<BoolOptPtr
> options
,
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
);
292 && m_selected
!= o
->m_selected
293 && options_list_equals(m_options
, o
->m_options
);
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
]))
305 return boost::shared_ptr
<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();
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
{
326 QList
<OptPtr
> m_options
;
327 void setupOptionWidget(QWidget
*, OptList
&, bool indent
= false);
328 void setOptionWidgetValues(QWidget
*, OptList
&);
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
;
342 OptionWidget(const OptList
& options
, QWidget
* parent
= NULL
);
343 void setValues(OptList
& newopts
);
346 void changed(const OptList
& options
);