chalk

kis_int_spinbox.cpp
1/*
2 * Copyright (c) 2006 Boudewijn Rempt <boud@valdyas.org>
3 * Copyright (c) 2006 Casper Boemann <cbr@boemann.dk>
4 *
5 * Requires the TQt widget libraries, available at no cost at
6 * http://www.troll.no/
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 */
23
24#ifdef HAVE_LIMITS_H
25#include <limits.h>
26#endif
27#include <assert.h>
28#include <math.h>
29#include <algorithm>
30
31#include <tqtimer.h>
32#include <tqapplication.h>
33#include <tqsize.h>
34#include <tqslider.h>
35#include <tqstyle.h>
36#include <tqlabel.h>
37#include <tqpopupmenu.h>
38#include <tqlineedit.h>
39#include <tqlayout.h>
40#include <tqvalidator.h>
41
42#include <knuminput.h>
43#include <tdeglobal.h>
44#include <tdelocale.h>
45#include <kdebug.h>
46#include <karrowbutton.h>
47
48#include "kdialog.h"
49#include "knumvalidator.h"
50#include "kis_int_spinbox.h"
51
52class KisIntSpinbox::KisIntSpinboxPrivate {
53public:
54
55 KIntSpinBox * m_numinput;
56 KisPopupSlider *m_slider;
57 KArrowButton *m_arrow;
58 int m_prevValue;
59 TQValidator *m_validator;
60 TQTimer m_timer;
61};
62
63
64KisIntSpinbox::KisIntSpinbox(TQWidget *parent, const char *name)
65 : TQWidget(parent, name)
66{
67 init(0);
68}
69
70KisIntSpinbox::KisIntSpinbox(const TQString & /*label*/, int val, TQWidget *parent, const char *name)
71 : TQWidget(parent, name)
72{
73 init(val);
74}
75
76void KisIntSpinbox::init(int val)
77{
78 d = new KisIntSpinboxPrivate( );
79 TQBoxLayout * l = new TQHBoxLayout( this );
80
81 l->insertStretch(0, 1);
82 d->m_numinput = new KIntSpinBox(0, 100, 1, val, 10, this, "KisIntSpinbox::KIntSpinBox");
83
84 d->m_numinput->setSizePolicy(TQSizePolicy::MinimumExpanding, TQSizePolicy::Fixed);
85 d->m_numinput->setSuffix("%");
86 l->addWidget( d->m_numinput );
87
88 d->m_slider = new KisPopupSlider(0, 100, 10, val, TQt::Horizontal, this);
89 d->m_slider->setFrameStyle(TQFrame::Panel|TQFrame::Raised);
90
91 d->m_arrow = new KArrowButton(this, TQt::DownArrow);
92 d->m_arrow->setPopup(d->m_slider);
93 d->m_arrow->setMaximumHeight( fontMetrics().height() + 4);
94 d->m_arrow->setEnabled(true); // Why is the arrow still gray?
95
96 l->addWidget( d->m_arrow );
97
98 d->m_prevValue = val;
99 setValue(val);
100 setFocusProxy(d->m_numinput);
101 layout();
102
103 connect(d->m_numinput, TQ_SIGNAL(valueChanged(int)), TQ_SLOT(spinboxValueChanged(int)));
104 connect(d->m_slider, TQ_SIGNAL(valueChanged(int)), TQ_SLOT(sliderValueChanged(int)));
105 connect(d->m_slider, TQ_SIGNAL(aboutToShow()), TQ_SLOT(slotAboutToShow()));
106 connect(d->m_slider, TQ_SIGNAL(aboutToHide()), TQ_SLOT(slotAboutToHide()));
107
108 connect(&(d->m_timer), TQ_SIGNAL(timeout()), this, TQ_SLOT(slotTimeout()));
109}
110
111void KisIntSpinbox::spinboxValueChanged(int val)
112{
113 setValue(val);
114 d->m_timer.start(300, true);
115
116}
117
118void KisIntSpinbox::sliderValueChanged(int val)
119{
120 setValue(val);
121 emit valueChanged(val);
122 emit valueChanged(val, true);
123}
124
125void KisIntSpinbox::setRange(int lower, int upper, int /*step*/)
126{
127 upper = kMax(upper, lower);
128 lower = kMin(upper, lower);
129 d->m_slider->setRange(lower, upper);
130
131 layout();
132}
133
135{
136 setRange(min, maxValue(), d->m_slider->lineStep());
137}
138
140{
141 return d->m_slider->minValue();
142}
143
145{
146 setRange(minValue(), max, d->m_slider->lineStep());
147}
148
150{
151 return d->m_slider->maxValue();
152}
153
155{
156 delete d;
157}
158
160{
161 d->m_slider->blockSignals(true);
162 d->m_slider->setValue(val);
163 d->m_slider->blockSignals(false);
164
165 d->m_numinput->blockSignals(true);
166 d->m_numinput->setValue(val);
167 d->m_numinput->blockSignals(false);
168}
169
171{
172 return d->m_numinput->value(); // From the numinput: that one isn't in steps of ten
173}
174
175void KisIntSpinbox::setLabel(const TQString & /*label*/)
176{
177}
178
179void KisIntSpinbox::slotAboutToShow()
180{
181 d->m_prevValue = value();
182}
183
184void KisIntSpinbox::slotAboutToHide()
185{
186 if( d->m_prevValue != value() )
187 {
188 emit finishedChanging( d->m_prevValue, value() );
189 d->m_prevValue = value();
190 }
191}
192
193void KisIntSpinbox::slotTimeout()
194{
195 emit valueChanged(value());
196 emit valueChanged(value(), true);
197}
198#include "kis_int_spinbox.moc"
int minValue() const
void valueChanged(int value)
Emitted every time the value changes (by calling setValue() or by user interaction).
void finishedChanging(int previous, int value)
Emitted after the slider has been hidden, if the value was changed while it was shown.
int maxValue() const
virtual ~KisIntSpinbox()
Destructor.
void setRange(int min, int max, int step=1)
void setValue(int)
Sets the value of the control.
void setMinValue(int min)
Sets the minimum value.
KisIntSpinbox(TQWidget *parent=0, const char *name=0)
Constructs an input control for integer values with base 10 and initial value 0.
void setMaxValue(int max)
Sets the maximum value.