0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the test suite of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
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 |
**
|
|
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.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
#include <QtGui>
|
|
43 |
|
|
44 |
#include "mousepangesturerecognizer.h"
|
|
45 |
|
|
46 |
class ScrollArea : public QScrollArea
|
|
47 |
{
|
|
48 |
Q_OBJECT
|
|
49 |
public:
|
|
50 |
ScrollArea(QWidget *parent = 0)
|
|
51 |
: QScrollArea(parent), outside(false)
|
|
52 |
{
|
|
53 |
viewport()->grabGesture(Qt::PanGesture);
|
|
54 |
}
|
|
55 |
|
|
56 |
protected:
|
|
57 |
bool viewportEvent(QEvent *event)
|
|
58 |
{
|
|
59 |
if (event->type() == QEvent::Gesture) {
|
|
60 |
gestureEvent(static_cast<QGestureEvent *>(event));
|
|
61 |
return true;
|
|
62 |
} else if (event->type() == QEvent::GestureOverride) {
|
|
63 |
QGestureEvent *ge = static_cast<QGestureEvent *>(event);
|
|
64 |
if (QPanGesture *pan = static_cast<QPanGesture *>(ge->gesture(Qt::PanGesture)))
|
|
65 |
if (pan->state() == Qt::GestureStarted) {
|
|
66 |
outside = false;
|
|
67 |
}
|
|
68 |
}
|
|
69 |
return QScrollArea::viewportEvent(event);
|
|
70 |
}
|
|
71 |
void gestureEvent(QGestureEvent *event)
|
|
72 |
{
|
|
73 |
QPanGesture *pan = static_cast<QPanGesture *>(event->gesture(Qt::PanGesture));
|
|
74 |
if (pan) {
|
|
75 |
switch(pan->state()) {
|
|
76 |
case Qt::GestureStarted: qDebug("area: Pan: started"); break;
|
|
77 |
case Qt::GestureFinished: qDebug("area: Pan: finished"); break;
|
|
78 |
case Qt::GestureCanceled: qDebug("area: Pan: canceled"); break;
|
|
79 |
case Qt::GestureUpdated: break;
|
|
80 |
default: qDebug("area: Pan: <unknown state>"); break;
|
|
81 |
}
|
|
82 |
|
|
83 |
if (pan->state() == Qt::GestureStarted)
|
|
84 |
outside = false;
|
|
85 |
event->ignore();
|
|
86 |
event->ignore(pan);
|
|
87 |
if (outside)
|
|
88 |
return;
|
|
89 |
|
|
90 |
const QPointF offset = pan->offset();
|
|
91 |
const QPointF totalOffset = pan->totalOffset();
|
|
92 |
QScrollBar *vbar = verticalScrollBar();
|
|
93 |
QScrollBar *hbar = horizontalScrollBar();
|
|
94 |
|
|
95 |
if ((vbar->value() == vbar->minimum() && totalOffset.y() > 10) ||
|
|
96 |
(vbar->value() == vbar->maximum() && totalOffset.y() < -10)) {
|
|
97 |
outside = true;
|
|
98 |
return;
|
|
99 |
}
|
|
100 |
if ((hbar->value() == hbar->minimum() && totalOffset.x() > 10) ||
|
|
101 |
(hbar->value() == hbar->maximum() && totalOffset.x() < -10)) {
|
|
102 |
outside = true;
|
|
103 |
return;
|
|
104 |
}
|
|
105 |
vbar->setValue(vbar->value() - offset.y());
|
|
106 |
hbar->setValue(hbar->value() - offset.x());
|
|
107 |
event->accept(pan);
|
|
108 |
}
|
|
109 |
}
|
|
110 |
|
|
111 |
private:
|
|
112 |
bool outside;
|
|
113 |
};
|
|
114 |
|
|
115 |
class Slider : public QSlider
|
|
116 |
{
|
|
117 |
public:
|
|
118 |
Slider(Qt::Orientation orientation, QWidget *parent = 0)
|
|
119 |
: QSlider(orientation, parent)
|
|
120 |
{
|
|
121 |
grabGesture(Qt::PanGesture);
|
|
122 |
}
|
|
123 |
protected:
|
|
124 |
bool event(QEvent *event)
|
|
125 |
{
|
|
126 |
if (event->type() == QEvent::Gesture) {
|
|
127 |
gestureEvent(static_cast<QGestureEvent *>(event));
|
|
128 |
return true;
|
|
129 |
}
|
|
130 |
return QSlider::event(event);
|
|
131 |
}
|
|
132 |
void gestureEvent(QGestureEvent *event)
|
|
133 |
{
|
|
134 |
QPanGesture *pan = static_cast<QPanGesture *>(event->gesture(Qt::PanGesture));
|
|
135 |
if (pan) {
|
|
136 |
switch (pan->state()) {
|
|
137 |
case Qt::GestureStarted: qDebug("slider: Pan: started"); break;
|
|
138 |
case Qt::GestureFinished: qDebug("slider: Pan: finished"); break;
|
|
139 |
case Qt::GestureCanceled: qDebug("slider: Pan: canceled"); break;
|
|
140 |
case Qt::GestureUpdated: break;
|
|
141 |
default: qDebug("slider: Pan: <unknown state>"); break;
|
|
142 |
}
|
|
143 |
|
|
144 |
if (pan->state() == Qt::GestureStarted)
|
|
145 |
outside = false;
|
|
146 |
event->ignore();
|
|
147 |
event->ignore(pan);
|
|
148 |
if (outside)
|
|
149 |
return;
|
|
150 |
const QPointF offset = pan->offset();
|
|
151 |
const QPointF totalOffset = pan->totalOffset();
|
|
152 |
if (orientation() == Qt::Horizontal) {
|
|
153 |
if ((value() == minimum() && totalOffset.x() < -10) ||
|
|
154 |
(value() == maximum() && totalOffset.x() > 10)) {
|
|
155 |
outside = true;
|
|
156 |
return;
|
|
157 |
}
|
|
158 |
if (totalOffset.y() < 40 && totalOffset.y() > -40) {
|
|
159 |
setValue(value() + offset.x());
|
|
160 |
event->accept(pan);
|
|
161 |
} else {
|
|
162 |
outside = true;
|
|
163 |
}
|
|
164 |
} else if (orientation() == Qt::Vertical) {
|
|
165 |
if ((value() == maximum() && totalOffset.y() < -10) ||
|
|
166 |
(value() == minimum() && totalOffset.y() > 10)) {
|
|
167 |
outside = true;
|
|
168 |
return;
|
|
169 |
}
|
|
170 |
if (totalOffset.x() < 40 && totalOffset.x() > -40) {
|
|
171 |
setValue(value() - offset.y());
|
|
172 |
event->accept(pan);
|
|
173 |
} else {
|
|
174 |
outside = true;
|
|
175 |
}
|
|
176 |
}
|
|
177 |
}
|
|
178 |
}
|
|
179 |
private:
|
|
180 |
bool outside;
|
|
181 |
};
|
|
182 |
|
|
183 |
class MainWindow : public QMainWindow
|
|
184 |
{
|
|
185 |
public:
|
|
186 |
MainWindow()
|
|
187 |
{
|
|
188 |
rootScrollArea = new ScrollArea;
|
|
189 |
setCentralWidget(rootScrollArea);
|
|
190 |
|
|
191 |
QWidget *root = new QWidget;
|
|
192 |
root->setFixedSize(3000, 3000);
|
|
193 |
rootScrollArea->setWidget(root);
|
|
194 |
|
|
195 |
Slider *verticalSlider = new Slider(Qt::Vertical, root);
|
|
196 |
verticalSlider ->move(650, 1100);
|
|
197 |
Slider *horizontalSlider = new Slider(Qt::Horizontal, root);
|
|
198 |
horizontalSlider ->move(600, 1000);
|
|
199 |
|
|
200 |
childScrollArea = new ScrollArea(root);
|
|
201 |
childScrollArea->move(500, 500);
|
|
202 |
QWidget *w = new QWidget;
|
|
203 |
w->setMinimumWidth(400);
|
|
204 |
QVBoxLayout *l = new QVBoxLayout(w);
|
|
205 |
l->setMargin(20);
|
|
206 |
for (int i = 0; i < 100; ++i) {
|
|
207 |
QWidget *w = new QWidget;
|
|
208 |
QHBoxLayout *ll = new QHBoxLayout(w);
|
|
209 |
ll->addWidget(new QLabel(QString("Label %1").arg(i)));
|
|
210 |
ll->addWidget(new QPushButton(QString("Button %1").arg(i)));
|
|
211 |
l->addWidget(w);
|
|
212 |
}
|
|
213 |
childScrollArea->setWidget(w);
|
|
214 |
}
|
|
215 |
private:
|
|
216 |
ScrollArea *rootScrollArea;
|
|
217 |
ScrollArea *childScrollArea;
|
|
218 |
};
|
|
219 |
|
|
220 |
int main(int argc, char **argv)
|
|
221 |
{
|
|
222 |
QApplication app(argc, argv);
|
|
223 |
app.registerGestureRecognizer(new MousePanGestureRecognizer);
|
|
224 |
MainWindow w;
|
|
225 |
w.show();
|
|
226 |
return app.exec();
|
|
227 |
}
|
|
228 |
|
|
229 |
#include "main.moc"
|