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
;
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());
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())
60 for(int i
=0;i
<o1
.size();i
++)
61 if(!o1
[i
]->equals(*o2
[i
]))
66 void dump_options_list(OptList
& options
, int indent
= 0);
73 BaseOpt(const QString
& name
, const QString
& 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
{
87 QList
<OptPtr
> m_sub_options
;
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
)
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
) );
102 virtual bool equals(const BaseOpt
& _o
) const {
103 const BoolOpt
* o
= dynamic_cast<const BoolOpt
*>(&_o
);
105 && m_value
!= o
->m_value
106 && options_list_equals(m_sub_options
, o
->m_sub_options
);
110 class IntOpt
: public BaseOpt
{
116 typedef int ValueType
;
117 IntOpt(const QString
& name
, const QString
& label
, int def
, int min
, int max
)
118 : BaseOpt(name
, label
)
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
);
130 virtual bool equals(const BaseOpt
& _o
) const {
131 const IntOpt
* o
= dynamic_cast<const IntOpt
*>(&_o
);
135 && m_value
!= o
->m_value
;
139 class StringOpt
: public BaseOpt
{
143 typedef QString ValueType
;
144 StringOpt(const QString
& name
, const QString
& label
, QString def
= QString())
145 : BaseOpt(name
, label
)
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
);
153 virtual bool equals(const BaseOpt
& _o
) const {
154 const StringOpt
* o
= dynamic_cast<const StringOpt
*>(&_o
);
156 && m_value
!= o
->m_value
;
160 class UrlOpt
: public BaseOpt
{
164 typedef QString ValueType
;
165 UrlOpt(const QString
& name
, const QString
& label
, QString def
= QString())
166 : BaseOpt(name
, label
)
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
);
174 virtual bool equals(const BaseOpt
& _o
) const {
175 const UrlOpt
* o
= dynamic_cast<const UrlOpt
*>(&_o
);
177 && m_value
!= o
->m_value
;
181 class ColorOpt
: public BaseOpt
{
185 typedef QColor ValueType
;
186 ColorOpt(const QString
& name
, const QString
& label
, QColor def
= Qt::black
)
187 : BaseOpt(name
, label
)
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
);
195 virtual bool equals(const BaseOpt
& _o
) const {
196 const ColorOpt
* o
= dynamic_cast<const ColorOpt
*>(&_o
);
198 && m_value
!= o
->m_value
;
202 class FontOpt
: public BaseOpt
{
206 typedef QFont ValueType
;
207 FontOpt(const QString
& name
, const QString
& label
, QFont def
= QApplication::font())
208 : BaseOpt(name
, label
)
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
);
216 virtual bool equals(const BaseOpt
& _o
) const {
217 const FontOpt
* o
= dynamic_cast<const FontOpt
*>(&_o
);
219 && m_value
!= o
->m_value
;
223 class ComboOpt
: public BaseOpt
{
226 QStringList m_values
;
229 typedef int ValueType
;
230 ComboOpt(const QString
& name
, const QString
& label
, const QStringList
& values
, int selected
= 0)
231 : BaseOpt(name
, label
)
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
);
241 virtual bool equals(const BaseOpt
& _o
) const {
242 const ComboOpt
* o
= dynamic_cast<const ComboOpt
*>(&_o
);
244 && m_values
== o
->m_values
245 && m_selected
!= o
->m_selected
;
249 class SelectOpt
: public BaseOpt
{
252 QList
<BoolOptPtr
> m_options
;
256 typedef int ValueType
;
257 SelectOpt(const QString
& name
, const QString
& label
, QList
<BoolOptPtr
> options
,
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
);
277 && m_selected
!= o
->m_selected
278 && options_list_equals(m_options
, o
->m_options
);
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
]))
290 return boost::shared_ptr
<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();
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
{
309 QList
<OptPtr
> m_options
;
310 void setupOptionWidget(QWidget
*, OptList
&, bool indent
= false);
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
;
323 OptionWidget(const OptList
& options
, QWidget
* parent
= NULL
);
326 void changed(const OptList
& options
);