/
qtscrollwheel.cpp
217 lines (174 loc) · 5.05 KB
/
qtscrollwheel.cpp
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
/*
Embedded Widgets Demo
Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).*
Contact: Qt Software Information (qt-info@nokia.com)**
This file may be used under the terms of the Embedded Widgets Demo License
Agreement.
*/
#include "qtscrollwheel.h"
#include <QtGui/QDesktopWidget>
#include <QtCore/QDebug>
#include <QtGui/QPainter>
#include <QSvgRenderer>
#include <QtGui/QMouseEvent>
#include <QApplication>
/*!
\class QtScrollWheel qtscrollwheel.h
The description of the QtScrollWheel.
\brief The QtScrollWheel class provides a scroll wheel to set values.
The QtScrollWheel is an example to show the capabilities of the Qt Framework
related to customized controls.
Here is a possible implementation shown
\code
// Create a QtScrollWheel
QWidget * widget = new QWidget(this)
QtScrollWheel * wheel = new QtScrollWheel(widget);
wheel->setSkin("Beryl");
widget->addWidget(wheel);
widget->show();
\endcode
It is important to call the function \a setSkin() to load a skin. If \a setSkin() is not called
the QtScrollWheel will not have any visible content.
The parameter \p skin has the skin name. The skins are in the resource file defined in the project file.
The name scheme in the resource file is "Skin Name"/"unique picture name.svg".
The \a setSkin() function loads the diffrent skins over the "Skin name".
The needed realised graphics for this control are "normal.svg", "hovered.svg" and "pressed.svg".
If you want to add new skins, they have to follow the name scheme to work.
The actual skin can be read over the \a skin() function.
\sa skin(), setSkin()
No Signals defined. \n
No Slots defined.
*/
/*!
Constructor of the QAbstractSlider with \p parent as
Parent.
*/
QtScrollWheel::QtScrollWheel(QWidget* parent)
: QAbstractSlider(parent)
{
init();
}
/*!
Destructor
*/
QtScrollWheel::~QtScrollWheel()
{
}
/*!
\internal
Internal initialisation function.
*/
void QtScrollWheel::init()
{
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
m_currentIndex = 0;
}
/*!
With this function you can set the skin that will be displayed in the widget.
\code
QtScrollWheel * scroll = new QtScrollWheel(this);
scroll->setSkin("Beryl");
\endcode
This function has to be called before using the QtScrollWheel.
\sa skin()
*/
void QtScrollWheel::setSkin(const QString& skin)
{
m_skin = skin;
const QString base = ":/scrollwheel/" + skin + '/';
for (int i = 0; i < 3; ++i) {
m_wheel[i].load(base + QString("wheel_%1.svg").arg(i+1));
}
// update geometry for new sizeHint and repaint
updateGeometry();
update();
}
/*!
This function returns the actual skin name. If no skin has been set the return value is "".
\code
QtScrollWheel * scroll = new QtScrollWheel(this);
scroll->setSkin("Beryl");
qDebug() << scroll->skin();
\endcode
\sa setSkin()
*/
QString QtScrollWheel::skin() const
{
return m_skin;
}
/*!
\overload
\internal
Overloaded mouse move event. Calculate new position of graphic.
*/
void QtScrollWheel::mouseMoveEvent(QMouseEvent* event)
{
int diff = event->pos().y() - m_lastMousePosition.y();
if (qAbs(diff) > QApplication::startDragDistance()) {
changeValue((diff > 0) ? -1 : 1);
m_lastMousePosition = event->pos();
}
}
/*!
\overload
\internal
Overloaded mouse press event. Save last mouse position.
*/
void QtScrollWheel::mousePressEvent(QMouseEvent* event)
{
// remember mouse position for mouse move event
m_lastMousePosition = event->pos();
QAbstractSlider::mousePressEvent(event);
}
/*!
\overload
\internal
Overloaded wheel event. Change position of wheel over mouse wheel.
*/
void QtScrollWheel::wheelEvent(QWheelEvent* event)
{
const int diff = QApplication::wheelScrollLines();
changeValue(event->delta() > 0 ? diff : -diff);
}
/*!
\internal
Change value to actual value added with \p delta.
*/
void QtScrollWheel::changeValue(int delta)
{
if (delta < 0 && minimum() < value()) {
m_currentIndex = (m_currentIndex + 2) % 3; // -1 (decrease)
setValue(value() + delta);
update();
} else if (delta > 0 && maximum() > value()) {
m_currentIndex = (m_currentIndex + 1) % 3; // +1 (increase)
setValue(value() + delta);
update();
}
}
/*!
\internal
\overload
Overloaded paint event. Draw wheel graphic.
*/
void QtScrollWheel::paintEvent(QPaintEvent* event)
{
Q_UNUSED(event);
QPainter painter(this);
m_wheel[m_currentIndex].render(&painter, QRectF(0,0, width(), height()));
}
/*!
\internal
\overload
Set size hint. Can be changed at runtime.
*/
QSize QtScrollWheel::sizeHint() const
{
return QSize(80,200);
if (!m_defaultSize.isNull()) {
double factor = static_cast<double>(m_defaultSize.height()) / m_defaultSize.width();
return QSize(70, factor * 70);
} else {
return QAbstractSlider::sizeHint();
}
}