summaryrefslogtreecommitdiffstats
path: root/chalk/ui/kis_int_spinbox.cc
blob: cb1b6938436d4c96ee982d3afe235a9474a93703 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 *  Copyright (c) 2006 Boudewijn Rempt <[email protected]>
 *  Copyright (c) 2006 Casper Boemann <[email protected]>
 *
 *  Requires the TQt widget libraries, available at no cost at
 *  http://www.troll.no/
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif
#include <assert.h>
#include <math.h>
#include <algorithm>

#include <tqtimer.h>
#include <tqapplication.h>
#include <tqsize.h>
#include <tqslider.h>
#include <tqstyle.h>
#include <tqlabel.h>
#include <tqpopupmenu.h>
#include <tqlineedit.h>
#include <tqlayout.h>
#include <tqvalidator.h>

#include <knuminput.h>
#include <tdeglobal.h>
#include <tdelocale.h>
#include <kdebug.h>
#include <karrowbutton.h>

#include "kdialog.h"
#include "knumvalidator.h"
#include "kis_int_spinbox.h"

class KisIntSpinbox::KisIntSpinboxPrivate {
public:

    KIntSpinBox * m_numinput;
    KisPopupSlider *m_slider;
    KArrowButton *m_arrow;
    int m_prevValue;
    TQValidator *m_validator;
    TQTimer m_timer;
};


KisIntSpinbox::KisIntSpinbox(TQWidget *parent, const char *name)
    : TQWidget(parent, name)
{
    init(0);
}

KisIntSpinbox::KisIntSpinbox(const TQString & /*label*/, int val, TQWidget *parent, const char *name)
    : TQWidget(parent, name)
{
    init(val);
}

void KisIntSpinbox::init(int val)
{
    d = new KisIntSpinboxPrivate( );
    TQBoxLayout * l = new TQHBoxLayout( this );

    l->insertStretch(0, 1);
    d->m_numinput = new KIntSpinBox(0, 100, 1, val, 10, this, "KisIntSpinbox::KIntSpinBox");

    d->m_numinput->setSizePolicy(TQSizePolicy::MinimumExpanding, TQSizePolicy::Fixed);
    d->m_numinput->setSuffix("%");
    l->addWidget( d->m_numinput );

    d->m_slider = new KisPopupSlider(0, 100, 10, val, Qt::Horizontal, this);
    d->m_slider->setFrameStyle(TQFrame::Panel|TQFrame::Raised);

    d->m_arrow = new KArrowButton(this, Qt::DownArrow);
    d->m_arrow->setPopup(d->m_slider);
    d->m_arrow->setMaximumHeight( fontMetrics().height() + 4);
    d->m_arrow->setEnabled(true); // Why is the arrow still gray?

    l->addWidget( d->m_arrow );

    d->m_prevValue = val;
    setValue(val);
    setFocusProxy(d->m_numinput);
    layout();

    connect(d->m_numinput, TQT_SIGNAL(valueChanged(int)), TQT_SLOT(spinboxValueChanged(int)));
    connect(d->m_slider, TQT_SIGNAL(valueChanged(int)), TQT_SLOT(sliderValueChanged(int)));
    connect(d->m_slider, TQT_SIGNAL(aboutToShow()), TQT_SLOT(slotAboutToShow()));
    connect(d->m_slider, TQT_SIGNAL(aboutToHide()), TQT_SLOT(slotAboutToHide()));

    connect(&(d->m_timer), TQT_SIGNAL(timeout()), this, TQT_SLOT(slotTimeout()));
}

void KisIntSpinbox::spinboxValueChanged(int val)
{
    setValue(val);
    d->m_timer.start(300, true);

}

void KisIntSpinbox::sliderValueChanged(int val)
{
    setValue(val);
    emit valueChanged(val);
    emit valueChanged(val, true);
}

void KisIntSpinbox::setRange(int lower, int upper, int /*step*/)
{
    upper = kMax(upper, lower);
    lower = kMin(upper, lower);
    d->m_slider->setRange(lower, upper);

    layout();
}

void KisIntSpinbox::setMinValue(int min)
{
    setRange(min, maxValue(), d->m_slider->lineStep());
}

int KisIntSpinbox::minValue() const
{
    return d->m_slider->minValue();
}

void KisIntSpinbox::setMaxValue(int max)
{
    setRange(minValue(), max, d->m_slider->lineStep());
}

int KisIntSpinbox::maxValue() const
{
    return d->m_slider->maxValue();
}

KisIntSpinbox::~KisIntSpinbox()
{
    delete d;
}

void KisIntSpinbox::setValue(int val)
{
   d->m_slider->blockSignals(true);
   d->m_slider->setValue(val);
   d->m_slider->blockSignals(false);

   d->m_numinput->blockSignals(true);
   d->m_numinput->setValue(val);
   d->m_numinput->blockSignals(false);
}

int  KisIntSpinbox::value() const
{
    return d->m_numinput->value(); // From the numinput: that one isn't in steps of ten
}

void KisIntSpinbox::setLabel(const TQString & /*label*/)
{
}

void KisIntSpinbox::slotAboutToShow()
{
    d->m_prevValue = value();
}

void KisIntSpinbox::slotAboutToHide()
{
    if( d->m_prevValue != value() )
    {
        emit finishedChanging( d->m_prevValue, value() );
        d->m_prevValue = value();
    }
}

void KisIntSpinbox::slotTimeout()
{
    emit valueChanged(value());
    emit valueChanged(value(), true);
}
#include "kis_int_spinbox.moc"