lib

spinbox.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>
3 Copyright (C) 2004 Alexander Dymo <cloudtemple@mskat.net>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include "spinbox.h"
22
23#include "property.h"
24
25#include <tqlayout.h>
26#include <tqobjectlist.h>
27#include <tqvariant.h>
28#include <tqpainter.h>
29#include <tqlineedit.h>
30
31#include <tdeglobal.h>
32#include <tdelocale.h>
33
34using namespace KoProperty;
35
36IntSpinBox::IntSpinBox(int lower, int upper, int step, int value, int base, IntEdit *parent, const char *name)
37: KIntSpinBox(lower, upper, step, value, base, parent, name)
38{
39 editor()->setAlignment(TQt::AlignLeft);
40 installEventFilter(editor());
41 installEventFilter(this);
42 TQObjectList *spinwidgets = queryList( "TQSpinWidget", 0, false, true );
43 TQSpinWidget* spin = static_cast<TQSpinWidget*>(spinwidgets->first());
44 if (spin)
45 spin->installEventFilter(this);
46 delete spinwidgets;
47}
48
49void IntSpinBox::setValue(const TQVariant &value)
50{
51 if (dynamic_cast<IntEdit*>(parentWidget()) && dynamic_cast<IntEdit*>(parentWidget())->isReadOnly())
52 return;
53 if (value.isNull())
54 editor()->clear();
55 else
56 KIntSpinBox::setValue(value.toInt());
57}
58
59bool
60IntSpinBox::eventFilter(TQObject *o, TQEvent *e)
61{
62 if(o == editor())
63 {
64 if(e->type() == TQEvent::KeyPress)
65 {
66 TQKeyEvent* ev = static_cast<TQKeyEvent*>(e);
67 if((ev->key()==Key_Up || ev->key()==Key_Down) && ev->state() !=ControlButton)
68 {
69 parentWidget()->eventFilter(o, e);
70 return true;
71 }
72 }
73 }
74 if ((o == editor() || o == this || o->parent() == this)
75 && e->type() == TQEvent::Wheel && static_cast<IntEdit*>(parentWidget())->isReadOnly())
76 {
77 return true; //avoid value changes for read-only widget
78 }
79
80 return KIntSpinBox::eventFilter(o, e);
81}
82
83
85
86IntEdit::IntEdit(Property *property, TQWidget *parent, const char *name)
87 : Widget(property, parent, name)
88{
89 TQVariant minVal( property ? property->option("min") : 0 );
90 TQVariant maxVal( property ? property->option("max") : TQVariant() );
91 TQVariant minValueText( property ? property->option("minValueText") : TQVariant() );
92 if (minVal.isNull())
93 minVal = 0;
94 if (maxVal.isNull())
95 maxVal = INT_MAX;
96
97 m_edit = new IntSpinBox(minVal.toInt(), maxVal.toInt(), 1, 0, 10, this);
98 if (!minValueText.isNull())
99 m_edit->setSpecialValueText(minValueText.toString());
100 m_edit->setMinimumHeight(5);
101 setEditor(m_edit);
102
103 setLeavesTheSpaceForRevertButton(true);
104 setFocusWidget(m_edit);
105 connect(m_edit, TQ_SIGNAL(valueChanged(int)), this, TQ_SLOT(slotValueChanged(int)));
106}
107
108IntEdit::~IntEdit()
109{}
110
111TQVariant
112IntEdit::value() const
113{
114 if (m_edit->cleanText().isEmpty())
115 return TQVariant();
116 return m_edit->value();
117}
118
119void
120IntEdit::setValue(const TQVariant &value, bool emitChange)
121{
122 m_edit->blockSignals(true);
123 m_edit->setValue(value);
124 updateSpinWidgets();
125 m_edit->blockSignals(false);
126 if (emitChange)
127 emit valueChanged(this);
128}
129
130void
131IntEdit::drawViewer(TQPainter *p, const TQColorGroup &cg, const TQRect &r, const TQVariant &value)
132{
133 TQString valueText = value.toString();
134 if (property() && property()->hasOptions()) {
135 //replace min value with minValueText if defined
136 TQVariant minValue( property()->option("min") );
137 TQVariant minValueText( property()->option("minValueText") );
138 if (!minValue.isNull() && !minValueText.isNull() && minValue.toInt() == value.toInt()) {
139 valueText = minValueText.toString();
140 }
141 }
142
143 Widget::drawViewer(p, cg, r, valueText);
144// p->eraseRect(r);
145// p->drawText(r, TQt::AlignLeft | TQt::AlignVCenter | TQt::SingleLine, valueText);
146}
147
148void
149IntEdit::slotValueChanged(int)
150{
151 emit valueChanged(this);
152}
153
154void
155IntEdit::updateSpinWidgets()
156{
157 TQObjectList *spinwidgets = queryList( "TQSpinWidget", 0, false, true );
158 TQSpinWidget* spin = static_cast<TQSpinWidget*>(spinwidgets->first());
159 if (spin) {
160 spin->setUpEnabled(!isReadOnly());
161 spin->setDownEnabled(!isReadOnly());
162 }
163 delete spinwidgets;
164}
165
166void
167IntEdit::setReadOnlyInternal(bool readOnly)
168{
169 //disable editor and spin widget
170 m_edit->editor()->setReadOnly(readOnly);
171 updateSpinWidgets();
172 if (readOnly)
173 setLeavesTheSpaceForRevertButton(false);
174}
175
178
179DoubleSpinBox::DoubleSpinBox (double lower, double upper, double step, double value, int precision, DoubleEdit *parent)
180: KDoubleSpinBox(lower, upper, step, value, precision, parent)
181{
182 editor()->setAlignment(TQt::AlignLeft);
183 installEventFilter(editor());
184 installEventFilter(this);
185 TQObjectList *spinwidgets = queryList( "TQSpinWidget", 0, false, true );
186 TQSpinWidget* spin = static_cast<TQSpinWidget*>(spinwidgets->first());
187 if (spin)
188 spin->installEventFilter(this);
189 delete spinwidgets;
190}
191
192bool
193DoubleSpinBox::eventFilter(TQObject *o, TQEvent *e)
194{
195 if(o == editor())
196 {
197 if(e->type() == TQEvent::KeyPress)
198 {
199 TQKeyEvent* ev = static_cast<TQKeyEvent*>(e);
200 if((ev->key()==Key_Up || ev->key()==Key_Down) && ev->state()!=ControlButton)
201 {
202 parentWidget()->eventFilter(o, e);
203 return true;
204 }
205 }
206 }
207 if ((o == editor() || o == this || o->parent() == this)
208 && e->type() == TQEvent::Wheel && static_cast<IntEdit*>(parentWidget())->isReadOnly())
209 {
210 return true; //avoid value changes for read-only widget
211 }
212
213 return KDoubleSpinBox::eventFilter(o, e);
214}
215
216
217void DoubleSpinBox::setValue( const TQVariant& value )
218{
219 if (dynamic_cast<DoubleEdit*>(parentWidget()) && dynamic_cast<DoubleEdit*>(parentWidget())->isReadOnly())
220 return;
221 if (value.isNull())
222 editor()->clear();
223 else
225}
226
228
229DoubleEdit::DoubleEdit(Property *property, TQWidget *parent, const char *name)
230 : Widget(property, parent, name)
231{
232 TQVariant minVal( property ? property->option("min") : 0 );
233 TQVariant maxVal( property ? property->option("max") : TQVariant() );
234 TQVariant step( property ? property->option("step") : TQVariant());
235 TQVariant precision( property ? property->option("precision") : TQVariant());
236 TQVariant minValueText( property ? property->option("minValueText") : TQVariant() );
237 if (minVal.isNull())
238 minVal = 0;
239 if (maxVal.isNull())
240 maxVal = (double)(INT_MAX/100);
241 if(step.isNull())
242 step = 0.1;
243 if(precision.isNull())
244 precision = 2;
245
246 m_edit = new DoubleSpinBox(minVal.toDouble(), maxVal.toDouble(), step.toDouble(),
247 0, precision.toInt(), this);
248 if (!minValueText.isNull())
249 m_edit->setSpecialValueText(minValueText.toString());
250 m_edit->setSizePolicy(TQSizePolicy::Expanding, TQSizePolicy::Expanding);
251 m_edit->setMinimumHeight(5);
252 setEditor(m_edit);
253
254 setLeavesTheSpaceForRevertButton(true);
255 setFocusWidget(m_edit);
256 connect(m_edit, TQ_SIGNAL(valueChanged(double)), this, TQ_SLOT(slotValueChanged(double)));
257}
258
259DoubleEdit::~DoubleEdit()
260{}
261
262TQVariant
263DoubleEdit::value() const
264{
265 if (m_edit->cleanText().isEmpty())
266 return TQVariant();
267 return m_edit->value();
268}
269
270void
271DoubleEdit::setValue(const TQVariant &value, bool emitChange)
272{
273 m_edit->blockSignals(true);
274 m_edit->setValue(value);
275 updateSpinWidgets();
276 m_edit->blockSignals(false);
277 if (emitChange)
278 emit valueChanged(this);
279}
280
281void
282DoubleEdit::drawViewer(TQPainter *p, const TQColorGroup &cg, const TQRect &r, const TQVariant &value)
283{
284 TQString valueText;
285 if (property() && property()->hasOptions()) {
286 //replace min value with minValueText if defined
287 TQVariant minValue( property()->option("min") );
288 TQVariant minValueText( property()->option("minValueText") );
289 if (!minValue.isNull() && !minValueText.isNull() && minValue.toString().toDouble() == value.toString().toDouble()) {
290 valueText = minValueText.toString();
291 }
292 }
293 if (valueText.isEmpty())
294 valueText = TQString(value.toString()).replace('.', TDEGlobal::locale()->decimalSymbol());
295
296 Widget::drawViewer(p, cg, r, valueText);
297// p->eraseRect(r);
298// p->drawText(r, TQt::AlignLeft | TQt::AlignVCenter | TQt::SingleLine, valueText);
299}
300
301void
302DoubleEdit::slotValueChanged(double)
303{
304 emit valueChanged(this);
305}
306
307void
308DoubleEdit::updateSpinWidgets()
309{
310 TQObjectList *spinwidgets = queryList( "TQSpinWidget", 0, false, true );
311 TQSpinWidget* spin = static_cast<TQSpinWidget*>(spinwidgets->first());
312 if (spin) {
313 spin->setUpEnabled(!isReadOnly());
314 spin->setDownEnabled(!isReadOnly());
315 }
316 delete spinwidgets;
317}
318
319void
320DoubleEdit::setReadOnlyInternal(bool readOnly)
321{
322 //disable editor and spin widget
323 m_edit->editor()->setReadOnly(readOnly);
324 updateSpinWidgets();
325 if (readOnly)
326 setLeavesTheSpaceForRevertButton(false);
327}
328
329#include "spinbox.moc"
virtual void setValue(double value)
double value() const
The base class representing a single property.
Definition property.h:177
TQVariant option(const char *name) const
Definition property.cpp:530
The base class for all item editors used in Editor.
Definition widget.h:37
virtual void drawViewer(TQPainter *p, const TQColorGroup &cg, const TQRect &r, const TQVariant &value)
Definition widget.cpp:88
virtual Property * property() const
Definition widget.cpp:72
bool isReadOnly() const
Definition widget.cpp:208
virtual TQVariant value() const =0
const char * name(StdAction id)
Namespace for a set of classes implementing generic properties framework.