source: trunk/src/qt3support/widgets/q3spinwidget.cpp@ 646

Last change on this file since 646 was 561, checked in by Dmitry A. Kuminov, 16 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 10.5 KB
RevLine 
[2]1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
[561]4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
[2]6**
7** This file is part of the Qt3Support module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
[561]24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
[2]27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
[561]36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
[2]38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "q3rangecontrol.h"
43
44#ifndef QT_NO_SPINWIDGET
45
46#include "qabstractspinbox.h"
47#include "qevent.h"
48#include "qpainter.h"
49#include "qrect.h"
50#include "qstyle.h"
51#include "qstyleoption.h"
52#include "qtimer.h"
53
54QT_BEGIN_NAMESPACE
55
56class Q3SpinWidgetPrivate
57{
58public:
59 Q3SpinWidgetPrivate()
60 : upEnabled(true),
61 downEnabled(true),
62 theButton(0),
63 buttonDown(0),
64 timerUp(0),
65 bsyms(Q3SpinWidget::UpDownArrows),
66 ed (0) {}
67 uint upEnabled :1;
68 uint downEnabled :1;
69 uint theButton :2;
70 uint buttonDown :2;
71 uint timerUp : 1;
72 QRect up;
73 QRect down;
74 QTimer auRepTimer;
75 Q3SpinWidget::ButtonSymbols bsyms;
76 QWidget *ed;
77 void startTimer(int msec) { auRepTimer.start(msec, true); }
78 void startTimer(bool up, int msec) { timerUp = up; startTimer(msec); }
79 void stopTimer() { auRepTimer.stop(); }
80};
81
82/*!
83 \class Q3SpinWidget
84 \brief The Q3SpinWidget class is an internal range control related class.
85
86 \internal
87
88 Constructs an empty range control widget with parent \a parent
89 called \a name.
90
91*/
92
93Q3SpinWidget::Q3SpinWidget(QWidget* parent, const char* name)
94 : QWidget(parent, name)
95{
96 d = new Q3SpinWidgetPrivate();
97 connect(&d->auRepTimer, SIGNAL(timeout()), this, SLOT(timerDone()));
98 setFocusPolicy(Qt::StrongFocus);
99
100 arrange();
101 updateDisplay();
102}
103
104
105/*! Destroys the object and frees any allocated resources.
106
107*/
108
109Q3SpinWidget::~Q3SpinWidget()
110{
111 delete d;
112}
113
114/*! */
115QWidget * Q3SpinWidget::editWidget()
116{
117 return d->ed;
118}
119
120/*!
121 Sets the editing widget to \a w.
122*/
123void Q3SpinWidget::setEditWidget(QWidget * w)
124{
125 if (w) {
126 if (w->parentWidget() != this)
127 w->setParent(this);
128 setFocusProxy(w);
129 }
130 d->ed = w;
131 arrange();
132 updateDisplay();
133}
134
135/*! \reimp
136
137*/
138
139void Q3SpinWidget::mousePressEvent(QMouseEvent *e)
140{
141 if (e->button() != Qt::LeftButton) {
142 d->stopTimer();
143 d->buttonDown = 0;
144 d->theButton = 0;
145 repaint(d->down.united(d->up));
146 return;
147 }
148
149 uint oldButtonDown = d->buttonDown;
150
151 if (d->down.contains(e->pos()) && d->downEnabled)
152 d->buttonDown = 1;
153 else if (d->up.contains(e->pos()) && d->upEnabled)
154 d->buttonDown = 2;
155 else
156 d->buttonDown = 0;
157
158 d->theButton = d->buttonDown;
159 if (oldButtonDown != d->buttonDown) {
160 if (!d->buttonDown) {
161 repaint(d->down.united(d->up));
162 } else if (d->buttonDown & 1) {
163 repaint(d->down);
164 stepDown();
165 d->startTimer(false, 300);
166 } else if (d->buttonDown & 2) {
167 repaint(d->up);
168 stepUp();
169 d->startTimer(true, 300);
170 }
171 }
172
173 if (!oldButtonDown && !d->buttonDown)
174 e->ignore();
175
176}
177
178static QStyleOptionSpinBox getStyleOption(const Q3SpinWidget *spin)
179{
180 QStyleOptionSpinBox opt;
181 opt.init(spin);
182 opt.frame = true;
183 opt.subControls = 0;
184 opt.buttonSymbols = (QAbstractSpinBox::ButtonSymbols)spin->buttonSymbols();
185 opt.stepEnabled = 0;
186 if (spin->isUpEnabled())
187 opt.stepEnabled |= QAbstractSpinBox::StepUpEnabled;
188 if (spin->isDownEnabled())
189 opt.stepEnabled |= QAbstractSpinBox::StepDownEnabled;
190 opt.activeSubControls = 0;
191 return opt;
192}
193
194/*!
195
196*/
197
198void Q3SpinWidget::arrange()
199{
200 QStyleOptionSpinBox opt = getStyleOption(this);
201 d->up = style()->subControlRect(QStyle::CC_SpinBox, &opt, QStyle::SC_SpinBoxUp, this);
202 d->down = style()->subControlRect(QStyle::CC_SpinBox, &opt, QStyle::SC_SpinBoxDown, this);
203 if (d->ed) {
204 QRect r = style()->subControlRect(QStyle::CC_SpinBox, &opt,
205 QStyle::SC_SpinBoxEditField, this);
206 d->ed->setGeometry(r);
207 }
208}
209
210/*!
211
212*/
213
214void Q3SpinWidget::stepUp()
215{
216 emit stepUpPressed();
217}
218
219void Q3SpinWidget::resizeEvent(QResizeEvent*)
220{
221 arrange();
222}
223
224/*!
225
226*/
227
228void Q3SpinWidget::stepDown()
229{
230 emit stepDownPressed();
231}