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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
** the names of its contributors may be used to endorse or promote
** products derived from this software without specific prior written
** permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtGui>
#include "wheelwidget.h"
#define WHEEL_SCROLL_OFFSET 50000.0
AbstractWheelWidget::AbstractWheelWidget(bool touch, QWidget *parent)
: QWidget(parent)
, m_currentItem(0)
, m_itemOffset(0)
{
// ![0]
QScroller::grabGesture(this, touch ? QScroller::TouchGesture : QScroller::LeftMouseButtonGesture);
// ![0]
}
AbstractWheelWidget::~AbstractWheelWidget()
{ }
int AbstractWheelWidget::currentIndex() const
{
return m_currentItem;
}
void AbstractWheelWidget::setCurrentIndex(int index)
{
if (index >= 0 && index < itemCount()) {
m_currentItem = index;
m_itemOffset = 0;
update();
}
}
bool AbstractWheelWidget::event(QEvent *e)
{
switch (e->type()) {
// ![1]
case QEvent::ScrollPrepare:
{
// We set the snap positions as late as possible so that we are sure
// we get the correct itemHeight
QScroller *scroller = QScroller::scroller(this);
scroller->setSnapPositionsY( WHEEL_SCROLL_OFFSET, itemHeight() );
QScrollPrepareEvent *se = static_cast<QScrollPrepareEvent *>(e);
se->setViewportSize(QSizeF(size()));
// we claim a huge scrolling area and a huge content position and
// hope that the user doesn't notice that the scroll area is restricted
se->setContentPosRange(QRectF(0.0, 0.0, 0.0, WHEEL_SCROLL_OFFSET * 2));
se->setContentPos(QPointF(0.0, WHEEL_SCROLL_OFFSET + m_currentItem * itemHeight() + m_itemOffset));
se->accept();
return true;
}
// ![1]
// ![2]
case QEvent::Scroll:
{
QScrollEvent *se = static_cast<QScrollEvent *>(e);
qreal y = se->contentPos().y();
int iy = y - WHEEL_SCROLL_OFFSET;
int ih = itemHeight();
// ![2]
// -- calculate the current item position and offset and redraw the widget
int ic = itemCount();
if (ic>0) {
m_currentItem = iy / ih % ic;
m_itemOffset = iy % ih;
// take care when scrolling backwards. Modulo returns negative numbers
if (m_itemOffset < 0) {
m_itemOffset += ih;
m_currentItem--;
}
if (m_currentItem < 0)
m_currentItem += ic;
}
// -- repaint
update();
se->accept();
return true;
}
default:
return QWidget::event(e);
}
return true;
}
void AbstractWheelWidget::paintEvent(QPaintEvent* event)
{
Q_UNUSED( event );
// -- first calculate size and position.
int w = width();
int h = height();
QPainter painter(this);
QPalette palette = QApplication::palette();
QPalette::ColorGroup colorGroup = isEnabled() ? QPalette::Active : QPalette::Disabled;
// linear gradient brush
QLinearGradient grad(0.5, 0, 0.5, 1.0);
grad.setColorAt(0, palette.color(colorGroup, QPalette::ButtonText));
grad.setColorAt(0.2, palette.color(colorGroup, QPalette::Button));
grad.setColorAt(0.8, palette.color(colorGroup, QPalette::Button));
grad.setColorAt(1.0, palette.color(colorGroup, QPalette::ButtonText));
grad.setCoordinateMode( QGradient::ObjectBoundingMode );
QBrush gBrush( grad );
// paint a border and background
painter.setPen(palette.color(colorGroup, QPalette::ButtonText));
painter.setBrush(gBrush);
// painter.setBrushOrigin( QPointF( 0.0, 0.0 ) );
painter.drawRect( 0, 0, w-1, h-1 );
// paint inner border
painter.setPen(palette.color(colorGroup, QPalette::Button));
painter.setBrush(Qt::NoBrush);
painter.drawRect( 1, 1, w-3, h-3 );
// paint the items
painter.setClipRect( QRect( 3, 3, w-6, h-6 ) );
painter.setPen(palette.color(colorGroup, QPalette::ButtonText));
int iH = itemHeight();
int iC = itemCount();
if (iC > 0) {
m_itemOffset = m_itemOffset % iH;
for (int i=-h/2/iH; i<=h/2/iH+1; i++) {
int itemNum = m_currentItem + i;
while (itemNum < 0)
itemNum += iC;
while (itemNum >= iC)
itemNum -= iC;
paintItem(&painter, itemNum, QRect(6, h/2 +i*iH - m_itemOffset - iH/2, w-6, iH ));
}
}
// draw a transparent bar over the center
QColor highlight = palette.color(colorGroup, QPalette::Highlight);
highlight.setAlpha(150);
QLinearGradient grad2(0.5, 0, 0.5, 1.0);
grad2.setColorAt(0, highlight);
grad2.setColorAt(1.0, highlight.lighter());
grad2.setCoordinateMode( QGradient::ObjectBoundingMode );
QBrush gBrush2( grad2 );
QLinearGradient grad3(0.5, 0, 0.5, 1.0);
grad3.setColorAt(0, highlight);
grad3.setColorAt(1.0, highlight.darker());
grad3.setCoordinateMode( QGradient::ObjectBoundingMode );
QBrush gBrush3( grad3 );
painter.fillRect( QRect( 0, h/2 - iH/2, w, iH/2 ), gBrush2 );
painter.fillRect( QRect( 0, h/2, w, iH/2 ), gBrush3 );
}
/*!
Rotates the wheel widget to a given index.
You can also give an index greater than itemCount or less than zero in which
case the wheel widget will scroll in the given direction and end up with
(index % itemCount)
*/
void AbstractWheelWidget::scrollTo(int index)
{
QScroller *scroller = QScroller::scroller(this);
scroller->scrollTo(QPointF(0, WHEEL_SCROLL_OFFSET + index * itemHeight()), 5000);
}
/*!
\class StringWheelWidget
\brief The StringWheelWidget class is an implementation of the AbstractWheelWidget class that draws QStrings as items.
\sa AbstractWheelWidget
*/
StringWheelWidget::StringWheelWidget(bool touch)
: AbstractWheelWidget(touch)
{ }
QStringList StringWheelWidget::items() const
{
return m_items;
}
void StringWheelWidget::setItems( const QStringList &items )
{
m_items = items;
if (m_currentItem >= items.count())
m_currentItem = items.count()-1;
update();
}
QSize StringWheelWidget::sizeHint() const
{
// determine font size
QFontMetrics fm(font());
return QSize( fm.width("m") * 10 + 6, fm.height() * 7 + 6 );
}
QSize StringWheelWidget::minimumSizeHint() const
{
QFontMetrics fm(font());
return QSize( fm.width("m") * 5 + 6, fm.height() * 3 + 6 );
}
void StringWheelWidget::paintItem(QPainter* painter, int index, const QRect &rect)
{
painter->drawText(rect, Qt::AlignCenter, m_items.at(index));
}
int StringWheelWidget::itemHeight() const
{
QFontMetrics fm(font());
return fm.height();
}
int StringWheelWidget::itemCount() const
{
return m_items.count();
}
|