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.
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(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
{
89 QList
<OptPtr
> m_sub_options
;
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
)
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
) );
104 virtual bool equals(const BaseOpt
& _o
) const {
105 const BoolOpt
* o
= dynamic_cast<const BoolOpt
*>(&_o
);
107 && m_value
!= o
->m_value
108 && options_list_equals(m_sub_options
, o
->m_sub_options
);
112 class IntOpt
: public BaseOpt
{
118 typedef int ValueType
;
119 IntOpt(const QString
& name
, const QString
& label
, int def
, int min
, int max
)
120 : BaseOpt(name
, label
)
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
);
132 virtual bool equals(const BaseOpt
& _o
) const {
133 const IntOpt
* o
= dynamic_cast<const IntOpt
*>(&_o
);
137 && m_value
!= o
->m_value
;
141 class StringOpt
: public BaseOpt
{
145 typedef QString ValueType
;
146 StringOpt(const QString
& name
, const QString
& label
, QString def
= QString())
147 : BaseOpt(name
, label
)
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
);
155 virtual bool equals(const BaseOpt
& _o
) const {
156 const StringOpt
* o
= dynamic_cast<const StringOpt
*>(&_o
);
158 && m_value
!= o
->m_value
;
162 class UrlOpt
: public BaseOpt
{
166 typedef QString ValueType
;
167 UrlOpt(const QString
& name
, const QString
& label
, QString def
= QString() )
168 : BaseOpt(name
, label
)
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
);
176 virtual bool equals(const BaseOpt
& _o
) const {
177 const UrlOpt
* o
= dynamic_cast<const UrlOpt
*>(&_o
);
179 && m_value
!= o
->m_value
;
183 class ColorOpt
: public BaseOpt
{
187 typedef QColor ValueType
;
188 ColorOpt(const QString
& name
, const QString
& label
, QColor def
= Qt::black
)
189 : BaseOpt(name
, label
)
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
);
197 virtual bool equals(const BaseOpt
& _o
) const {
198 const ColorOpt
* o
= dynamic_cast<const ColorOpt
*>(&_o
);
200 && m_value
!= o
->m_value
;
204 class FontOpt
: public BaseOpt
{
208 typedef QFont ValueType
;
209 FontOpt(const QString
& name
, const QString
& label
, QFont def
= QApplication::font())
210 : BaseOpt(name
, label
)
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
);
218 virtual bool equals(const BaseOpt
& _o
) const {
219 const FontOpt
* o
= dynamic_cast<const FontOpt
*>(&_o
);
221 && m_value
!= o
->m_value
;
225 class ComboOpt
: public BaseOpt
{
228 QStringList m_values
;
231 typedef int ValueType
;
232 ComboOpt(const QString
& name
, const QString
& label
, const QStringList
& values
, int selected
= 0)
233 : BaseOpt(name
, label
)
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
);
243 virtual bool equals(const BaseOpt
& _o
) const {
244 const ComboOpt
* o
= dynamic_cast<const ComboOpt
*>(&_o
);
246 && m_values
== o
->m_values
247 && m_selected
!= o
->m_selected
;
251 class SelectOpt
: public BaseOpt
{
254 QList
<BoolOptPtr
> m_options
;
258 typedef int ValueType
;
259 SelectOpt(const QString
& name
, const QString
& label
, QList
<BoolOptPtr
> options
,
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
);
279 && m_selected
!= o
->m_selected
280 && options_list_equals(m_options
, o
->m_options
);
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
]))
292 return boost::shared_ptr
<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();
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
{
313 QList
<OptPtr
> m_options
;
314 void setupOptionWidget(QWidget
*, OptList
&, bool indent
= false);
315 void setOptionWidgetValues(QWidget
*, OptList
&);
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
;
328 OptionWidget(const OptList
& options
, QWidget
* parent
= NULL
);
329 void setValues(OptList
& newopts
);
332 void changed(const OptList
& options
);