mirror of
https://github.com/crystalidea/qt6windows7.git
synced 2025-07-04 00:05:25 +08:00
qt 6.5.1 original
This commit is contained in:
5
tests/manual/gestures/CMakeLists.txt
Normal file
5
tests/manual/gestures/CMakeLists.txt
Normal file
@ -0,0 +1,5 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
add_subdirectory(graphicsview)
|
||||
add_subdirectory(scrollarea)
|
4
tests/manual/gestures/gestures.pro
Normal file
4
tests/manual/gestures/gestures.pro
Normal file
@ -0,0 +1,4 @@
|
||||
TEMPLATE=subdirs
|
||||
|
||||
SUBDIRS = graphicsview \
|
||||
scrollarea
|
18
tests/manual/gestures/graphicsview/CMakeLists.txt
Normal file
18
tests/manual/gestures/graphicsview/CMakeLists.txt
Normal file
@ -0,0 +1,18 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
#####################################################################
|
||||
## tst_manual_graphicsview Binary:
|
||||
#####################################################################
|
||||
|
||||
qt_internal_add_manual_test(tst_manual_graphicsview
|
||||
GUI
|
||||
SOURCES
|
||||
gestures.cpp gestures.h
|
||||
imageitem.cpp imageitem.h
|
||||
main.cpp
|
||||
mousepangesturerecognizer.cpp mousepangesturerecognizer.h
|
||||
LIBRARIES
|
||||
Qt::Gui
|
||||
Qt::Widgets
|
||||
)
|
93
tests/manual/gestures/graphicsview/gestures.cpp
Normal file
93
tests/manual/gestures/graphicsview/gestures.cpp
Normal file
@ -0,0 +1,93 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
||||
|
||||
#include "gestures.h"
|
||||
|
||||
#include <QTouchEvent>
|
||||
|
||||
Qt::GestureType ThreeFingerSlideGesture::Type = Qt::CustomGesture;
|
||||
|
||||
QGesture *ThreeFingerSlideGestureRecognizer::create(QObject *)
|
||||
{
|
||||
return new ThreeFingerSlideGesture;
|
||||
}
|
||||
|
||||
QGestureRecognizer::Result ThreeFingerSlideGestureRecognizer::recognize(QGesture *state, QObject *, QEvent *event)
|
||||
{
|
||||
ThreeFingerSlideGesture *d = static_cast<ThreeFingerSlideGesture *>(state);
|
||||
QGestureRecognizer::Result result;
|
||||
switch (event->type()) {
|
||||
case QEvent::TouchBegin:
|
||||
result = QGestureRecognizer::MayBeGesture;
|
||||
break;
|
||||
case QEvent::TouchEnd:
|
||||
if (d->gestureFired)
|
||||
result = QGestureRecognizer::FinishGesture;
|
||||
else
|
||||
result = QGestureRecognizer::CancelGesture;
|
||||
break;
|
||||
case QEvent::TouchUpdate:
|
||||
if (d->state() != Qt::NoGesture) {
|
||||
QTouchEvent *ev = static_cast<QTouchEvent*>(event);
|
||||
if (ev->points().size() == 3) {
|
||||
d->gestureFired = true;
|
||||
result = QGestureRecognizer::TriggerGesture;
|
||||
} else {
|
||||
result = QGestureRecognizer::MayBeGesture;
|
||||
for (const QEventPoint &pt : ev->points()) {
|
||||
const int distance = (pt.globalPosition().toPoint() - pt.globalPressPosition().toPoint()).manhattanLength();
|
||||
if (distance > 20)
|
||||
result = QGestureRecognizer::CancelGesture;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
result = QGestureRecognizer::CancelGesture;
|
||||
}
|
||||
|
||||
break;
|
||||
case QEvent::MouseButtonPress:
|
||||
case QEvent::MouseButtonRelease:
|
||||
case QEvent::MouseMove:
|
||||
if (d->state() != Qt::NoGesture)
|
||||
result = QGestureRecognizer::Ignore;
|
||||
else
|
||||
result = QGestureRecognizer::CancelGesture;
|
||||
break;
|
||||
default:
|
||||
result = QGestureRecognizer::Ignore;
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void ThreeFingerSlideGestureRecognizer::reset(QGesture *state)
|
||||
{
|
||||
static_cast<ThreeFingerSlideGesture *>(state)->gestureFired = false;
|
||||
QGestureRecognizer::reset(state);
|
||||
}
|
||||
|
||||
|
||||
QGesture *RotateGestureRecognizer::create(QObject *)
|
||||
{
|
||||
return new QGesture;
|
||||
}
|
||||
|
||||
QGestureRecognizer::Result RotateGestureRecognizer::recognize(QGesture *, QObject *, QEvent *event)
|
||||
{
|
||||
switch (event->type()) {
|
||||
case QEvent::TouchBegin:
|
||||
case QEvent::TouchEnd:
|
||||
case QEvent::TouchUpdate:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return QGestureRecognizer::Ignore;
|
||||
}
|
||||
|
||||
void RotateGestureRecognizer::reset(QGesture *state)
|
||||
{
|
||||
QGestureRecognizer::reset(state);
|
||||
}
|
||||
|
||||
#include "moc_gestures.cpp"
|
40
tests/manual/gestures/graphicsview/gestures.h
Normal file
40
tests/manual/gestures/graphicsview/gestures.h
Normal file
@ -0,0 +1,40 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
||||
|
||||
#ifndef GESTURE_H
|
||||
#define GESTURE_H
|
||||
|
||||
#include <QGestureRecognizer>
|
||||
#include <QGesture>
|
||||
|
||||
class ThreeFingerSlideGesture : public QGesture
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
static Qt::GestureType Type;
|
||||
|
||||
ThreeFingerSlideGesture(QObject *parent = nullptr) : QGesture(parent) { }
|
||||
|
||||
bool gestureFired;
|
||||
};
|
||||
|
||||
class ThreeFingerSlideGestureRecognizer : public QGestureRecognizer
|
||||
{
|
||||
private:
|
||||
QGesture *create(QObject *target);
|
||||
QGestureRecognizer::Result recognize(QGesture *state, QObject *watched, QEvent *event);
|
||||
void reset(QGesture *state);
|
||||
};
|
||||
|
||||
class RotateGestureRecognizer : public QGestureRecognizer
|
||||
{
|
||||
public:
|
||||
RotateGestureRecognizer();
|
||||
|
||||
private:
|
||||
QGesture *create(QObject *target);
|
||||
QGestureRecognizer::Result recognize(QGesture *state, QObject *watched, QEvent *event);
|
||||
void reset(QGesture *state);
|
||||
};
|
||||
|
||||
#endif // GESTURE_H
|
12
tests/manual/gestures/graphicsview/graphicsview.pro
Normal file
12
tests/manual/gestures/graphicsview/graphicsview.pro
Normal file
@ -0,0 +1,12 @@
|
||||
QT += widgets
|
||||
|
||||
TARGET = tst_manual_graphicsview
|
||||
|
||||
SOURCES += main.cpp \
|
||||
imageitem.cpp \
|
||||
gestures.cpp \
|
||||
mousepangesturerecognizer.cpp
|
||||
|
||||
HEADERS += imageitem.h \
|
||||
gestures.h \
|
||||
mousepangesturerecognizer.h
|
55
tests/manual/gestures/graphicsview/imageitem.cpp
Normal file
55
tests/manual/gestures/graphicsview/imageitem.cpp
Normal file
@ -0,0 +1,55 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
||||
|
||||
#include "imageitem.h"
|
||||
#include "gestures.h"
|
||||
|
||||
#include <QPainter>
|
||||
#include <QEvent>
|
||||
|
||||
ImageItem::ImageItem(const QImage &image)
|
||||
{
|
||||
setImage(image);
|
||||
}
|
||||
|
||||
void ImageItem::setImage(const QImage &image)
|
||||
{
|
||||
image_ = image;
|
||||
pixmap_ = QPixmap::fromImage(image.scaled(400, 400, Qt::KeepAspectRatio));
|
||||
update();
|
||||
}
|
||||
|
||||
QImage ImageItem::image() const
|
||||
{
|
||||
return image_;
|
||||
}
|
||||
|
||||
QRectF ImageItem::boundingRect() const
|
||||
{
|
||||
const QSize size = pixmap_.size();
|
||||
return QRectF(0, 0, size.width(), size.height());
|
||||
}
|
||||
|
||||
void ImageItem::paint(QPainter *painter, const QStyleOptionGraphicsItem*, QWidget*)
|
||||
{
|
||||
painter->drawPixmap(0, 0, pixmap_);
|
||||
}
|
||||
|
||||
|
||||
GestureImageItem::GestureImageItem(const QImage &image)
|
||||
: ImageItem(image)
|
||||
{
|
||||
grabGesture(Qt::PanGesture);
|
||||
grabGesture(ThreeFingerSlideGesture::Type);
|
||||
}
|
||||
|
||||
bool GestureImageItem::event(QEvent *event)
|
||||
{
|
||||
if (event->type() == QEvent::Gesture) {
|
||||
qDebug("gestureimageitem: gesture triggered");
|
||||
return true;
|
||||
}
|
||||
return ImageItem::event(event);
|
||||
}
|
||||
|
||||
#include "moc_imageitem.cpp"
|
39
tests/manual/gestures/graphicsview/imageitem.h
Normal file
39
tests/manual/gestures/graphicsview/imageitem.h
Normal file
@ -0,0 +1,39 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
||||
|
||||
#ifndef IMAGEITEM_H
|
||||
#define IMAGEITEM_H
|
||||
|
||||
#include <QGraphicsItem>
|
||||
#include <QImage>
|
||||
#include <QPixmap>
|
||||
#include <QTransform>
|
||||
|
||||
class ImageItem : public QGraphicsObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
ImageItem(const QImage &image);
|
||||
void setImage(const QImage &image);
|
||||
QImage image() const;
|
||||
QRectF boundingRect() const;
|
||||
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
|
||||
|
||||
private:
|
||||
QImage image_;
|
||||
QPixmap pixmap_;
|
||||
QTransform transform;
|
||||
};
|
||||
|
||||
class GestureImageItem : public ImageItem
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
GestureImageItem(const QImage &image);
|
||||
|
||||
protected:
|
||||
bool event(QEvent *event);
|
||||
};
|
||||
|
||||
#endif // IMAGEITEM_H
|
201
tests/manual/gestures/graphicsview/main.cpp
Normal file
201
tests/manual/gestures/graphicsview/main.cpp
Normal file
@ -0,0 +1,201 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
||||
|
||||
#include <QApplication>
|
||||
#include <QMainWindow>
|
||||
#include <QGraphicsScene>
|
||||
#include <QGraphicsView>
|
||||
#include <QScrollBar>
|
||||
#include <QImageReader>
|
||||
#include <QVBoxLayout>
|
||||
#include <QGestureEvent>
|
||||
#include <QDir>
|
||||
#include <QFileDialog>
|
||||
|
||||
#include "imageitem.h"
|
||||
#include "gestures.h"
|
||||
#include "mousepangesturerecognizer.h"
|
||||
|
||||
class GraphicsView : public QGraphicsView
|
||||
{
|
||||
public:
|
||||
GraphicsView(QGraphicsScene *scene, QWidget *parent = nullptr)
|
||||
: QGraphicsView(scene, parent)
|
||||
{
|
||||
}
|
||||
protected:
|
||||
bool viewportEvent(QEvent *event)
|
||||
{
|
||||
if (event->type() == QEvent::Gesture) {
|
||||
QGestureEvent *ge = static_cast<QGestureEvent *>(event);
|
||||
if (QPanGesture *pan = static_cast<QPanGesture *>(ge->gesture(Qt::PanGesture))) {
|
||||
switch (pan->state()) {
|
||||
case Qt::GestureStarted: qDebug("view: Pan: started"); break;
|
||||
case Qt::GestureFinished: qDebug("view: Pan: finished"); break;
|
||||
case Qt::GestureCanceled: qDebug("view: Pan: canceled"); break;
|
||||
case Qt::GestureUpdated: break;
|
||||
default: qDebug("view: Pan: <unknown state>"); break;
|
||||
}
|
||||
|
||||
const QPointF delta = pan->delta();
|
||||
QScrollBar *vbar = verticalScrollBar();
|
||||
QScrollBar *hbar = horizontalScrollBar();
|
||||
vbar->setValue(vbar->value() - delta.y());
|
||||
hbar->setValue(hbar->value() - delta.x());
|
||||
ge->accept(pan);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return QGraphicsView::viewportEvent(event);
|
||||
}
|
||||
};
|
||||
|
||||
class StandardGestures : public QWidget
|
||||
{
|
||||
public:
|
||||
StandardGestures(QWidget *parent = nullptr)
|
||||
: QWidget(parent)
|
||||
{
|
||||
scene = new QGraphicsScene(this);
|
||||
scene->setSceneRect(-2000, -2000, 4000, 4000);
|
||||
view = new QGraphicsView(scene, 0);
|
||||
QVBoxLayout *l = new QVBoxLayout(this);
|
||||
l->addWidget(view);
|
||||
}
|
||||
|
||||
QGraphicsScene *scene;
|
||||
QGraphicsView *view;
|
||||
};
|
||||
|
||||
class GlobalViewGestures : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
GlobalViewGestures(QWidget *parent = nullptr)
|
||||
: QWidget(parent)
|
||||
{
|
||||
scene = new QGraphicsScene(this);
|
||||
scene->setSceneRect(-2000, -2000, 4000, 4000);
|
||||
view = new GraphicsView(scene, 0);
|
||||
view->viewport()->grabGesture(Qt::PanGesture);
|
||||
view->viewport()->grabGesture(ThreeFingerSlideGesture::Type);
|
||||
QVBoxLayout *l = new QVBoxLayout(this);
|
||||
l->addWidget(view);
|
||||
}
|
||||
|
||||
QGraphicsScene *scene;
|
||||
QGraphicsView *view;
|
||||
};
|
||||
|
||||
class GraphicsItemGestures : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
GraphicsItemGestures(QWidget *parent = nullptr)
|
||||
: QWidget(parent)
|
||||
{
|
||||
scene = new QGraphicsScene(this);
|
||||
scene->setSceneRect(-2000, -2000, 4000, 4000);
|
||||
view = new QGraphicsView(scene, 0);
|
||||
view->viewport()->grabGesture(Qt::PanGesture);
|
||||
view->viewport()->grabGesture(ThreeFingerSlideGesture::Type);
|
||||
QVBoxLayout *l = new QVBoxLayout(this);
|
||||
l->addWidget(view);
|
||||
}
|
||||
|
||||
QGraphicsScene *scene;
|
||||
QGraphicsView *view;
|
||||
};
|
||||
|
||||
class MainWindow : public QMainWindow
|
||||
{
|
||||
public:
|
||||
MainWindow();
|
||||
|
||||
void setDirectory(const QString &path);
|
||||
|
||||
private:
|
||||
QTabWidget *tabWidget;
|
||||
StandardGestures *standardGestures;
|
||||
GlobalViewGestures *globalViewGestures;
|
||||
GraphicsItemGestures *graphicsItemGestures;
|
||||
};
|
||||
|
||||
MainWindow::MainWindow()
|
||||
{
|
||||
(void)QGestureRecognizer::registerRecognizer(new MousePanGestureRecognizer);
|
||||
ThreeFingerSlideGesture::Type = QGestureRecognizer::registerRecognizer(new ThreeFingerSlideGestureRecognizer);
|
||||
|
||||
tabWidget = new QTabWidget;
|
||||
|
||||
standardGestures = new StandardGestures;
|
||||
tabWidget->addTab(standardGestures, "Standard gestures");
|
||||
|
||||
globalViewGestures = new GlobalViewGestures;
|
||||
tabWidget->addTab(globalViewGestures , "Global gestures");
|
||||
|
||||
graphicsItemGestures = new GraphicsItemGestures;
|
||||
tabWidget->addTab(graphicsItemGestures, "Graphics item gestures");
|
||||
|
||||
setCentralWidget(tabWidget);
|
||||
}
|
||||
|
||||
void MainWindow::setDirectory(const QString &path)
|
||||
{
|
||||
QDir dir(path);
|
||||
QStringList files = dir.entryList(QDir::Files | QDir::Readable | QDir::NoDotAndDotDot);
|
||||
foreach(const QString &file, files) {
|
||||
QImageReader img(path + QLatin1Char('/') +file);
|
||||
QImage image = img.read();
|
||||
if (!image.isNull()) {
|
||||
{
|
||||
ImageItem *item = new ImageItem(image);
|
||||
item->setPos(0, 0);
|
||||
item->setFlags(QGraphicsItem::ItemIsMovable);
|
||||
standardGestures->scene->addItem(item);
|
||||
}
|
||||
{
|
||||
ImageItem *item = new ImageItem(image);
|
||||
item->setPos(0, 0);
|
||||
item->setFlags(QGraphicsItem::ItemIsMovable);
|
||||
globalViewGestures->scene->addItem(item);
|
||||
}
|
||||
{
|
||||
GestureImageItem *item = new GestureImageItem(image);
|
||||
item->setPos(0, 0);
|
||||
item->setFlags(QGraphicsItem::ItemIsMovable);
|
||||
graphicsItemGestures->scene->addItem(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
QList<QGraphicsItem*> items = standardGestures->scene->items();
|
||||
if (!items.isEmpty())
|
||||
standardGestures->view->ensureVisible(items.at(0));
|
||||
}
|
||||
{
|
||||
QList<QGraphicsItem*> items = globalViewGestures->scene->items();
|
||||
if (!items.isEmpty())
|
||||
globalViewGestures->view->ensureVisible(items.at(0));
|
||||
}
|
||||
{
|
||||
QList<QGraphicsItem*> items = graphicsItemGestures->scene->items();
|
||||
if (!items.isEmpty())
|
||||
graphicsItemGestures->view->ensureVisible(items.at(0));
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
MainWindow window;
|
||||
if (QApplication::arguments().size() > 1)
|
||||
window.setDirectory(QApplication::arguments().at(1));
|
||||
else
|
||||
window.setDirectory(QFileDialog::getExistingDirectory(0, "Select image folder"));
|
||||
window.show();
|
||||
return app.exec();
|
||||
}
|
||||
|
||||
#include "main.moc"
|
@ -0,0 +1,69 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
||||
|
||||
#include "mousepangesturerecognizer.h"
|
||||
|
||||
#include <QEvent>
|
||||
#include <QVariant>
|
||||
#include <QGraphicsSceneMouseEvent>
|
||||
#include <QMouseEvent>
|
||||
#include <QGesture>
|
||||
|
||||
MousePanGestureRecognizer::MousePanGestureRecognizer()
|
||||
{
|
||||
}
|
||||
|
||||
QGesture* MousePanGestureRecognizer::create(QObject *)
|
||||
{
|
||||
return new QPanGesture;
|
||||
}
|
||||
|
||||
QGestureRecognizer::Result MousePanGestureRecognizer::recognize(QGesture *state, QObject *, QEvent *event)
|
||||
{
|
||||
QPanGesture *g = static_cast<QPanGesture *>(state);
|
||||
QPoint globalPos;
|
||||
switch (event->type()) {
|
||||
case QEvent::GraphicsSceneMousePress:
|
||||
case QEvent::GraphicsSceneMouseDoubleClick:
|
||||
case QEvent::GraphicsSceneMouseMove:
|
||||
case QEvent::GraphicsSceneMouseRelease:
|
||||
globalPos = static_cast<QGraphicsSceneMouseEvent *>(event)->screenPos();
|
||||
break;
|
||||
case QEvent::MouseButtonPress:
|
||||
case QEvent::MouseMove:
|
||||
case QEvent::MouseButtonRelease:
|
||||
globalPos = static_cast<QMouseEvent *>(event)->globalPosition().toPoint();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonDblClick
|
||||
|| event->type() == QEvent::GraphicsSceneMousePress || event->type() == QEvent::GraphicsSceneMouseDoubleClick) {
|
||||
g->setHotSpot(globalPos);
|
||||
g->setProperty("startPos", globalPos);
|
||||
g->setProperty("pressed", QVariant::fromValue<bool>(true));
|
||||
return QGestureRecognizer::TriggerGesture | QGestureRecognizer::ConsumeEventHint;
|
||||
} else if (event->type() == QEvent::MouseMove || event->type() == QEvent::GraphicsSceneMouseMove) {
|
||||
if (g->property("pressed").toBool()) {
|
||||
QPoint offset = globalPos - g->property("startPos").toPoint();
|
||||
g->setLastOffset(g->offset());
|
||||
g->setOffset(QPointF(offset.x(), offset.y()));
|
||||
return QGestureRecognizer::TriggerGesture | QGestureRecognizer::ConsumeEventHint;
|
||||
}
|
||||
return QGestureRecognizer::CancelGesture;
|
||||
} else if (event->type() == QEvent::MouseButtonRelease || event->type() == QEvent::GraphicsSceneMouseRelease) {
|
||||
return QGestureRecognizer::FinishGesture | QGestureRecognizer::ConsumeEventHint;
|
||||
}
|
||||
return QGestureRecognizer::Ignore;
|
||||
}
|
||||
|
||||
void MousePanGestureRecognizer::reset(QGesture *state)
|
||||
{
|
||||
QPanGesture *g = static_cast<QPanGesture *>(state);
|
||||
g->setLastOffset(QPointF());
|
||||
g->setOffset(QPointF());
|
||||
g->setAcceleration(0);
|
||||
g->setProperty("startPos", QVariant());
|
||||
g->setProperty("pressed", QVariant::fromValue<bool>(false));
|
||||
QGestureRecognizer::reset(state);
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
||||
|
||||
#ifndef MOUSEPANGESTURERECOGNIZER_H
|
||||
#define MOUSEPANGESTURERECOGNIZER_H
|
||||
|
||||
#include <QGestureRecognizer>
|
||||
|
||||
class MousePanGestureRecognizer : public QGestureRecognizer
|
||||
{
|
||||
public:
|
||||
MousePanGestureRecognizer();
|
||||
|
||||
QGesture* create(QObject *target);
|
||||
QGestureRecognizer::Result recognize(QGesture *state, QObject *watched, QEvent *event);
|
||||
void reset(QGesture *state);
|
||||
};
|
||||
|
||||
#endif // MOUSEPANGESTURERECOGNIZER_H
|
16
tests/manual/gestures/scrollarea/CMakeLists.txt
Normal file
16
tests/manual/gestures/scrollarea/CMakeLists.txt
Normal file
@ -0,0 +1,16 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
#####################################################################
|
||||
## scrollarea Binary:
|
||||
#####################################################################
|
||||
|
||||
qt_internal_add_manual_test(scrollarea
|
||||
GUI
|
||||
SOURCES
|
||||
main.cpp
|
||||
mousepangesturerecognizer.cpp mousepangesturerecognizer.h
|
||||
LIBRARIES
|
||||
Qt::Gui
|
||||
Qt::Widgets
|
||||
)
|
213
tests/manual/gestures/scrollarea/main.cpp
Normal file
213
tests/manual/gestures/scrollarea/main.cpp
Normal file
@ -0,0 +1,213 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
||||
|
||||
#include <QApplication>
|
||||
#include <QSlider>
|
||||
#include <QScrollArea>
|
||||
#include <QScrollBar>
|
||||
#include <QMainWindow>
|
||||
#include <QLabel>
|
||||
#include <QPushButton>
|
||||
#include <QVBoxLayout>
|
||||
#include <QGestureEvent>
|
||||
#include <QPanGesture>
|
||||
#include <QDebug>
|
||||
|
||||
#include "mousepangesturerecognizer.h"
|
||||
|
||||
class ScrollArea : public QScrollArea
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
ScrollArea(QWidget *parent = nullptr)
|
||||
: QScrollArea(parent), outside(false)
|
||||
{
|
||||
viewport()->grabGesture(Qt::PanGesture, Qt::ReceivePartialGestures);
|
||||
}
|
||||
|
||||
protected:
|
||||
bool viewportEvent(QEvent *event)
|
||||
{
|
||||
if (event->type() == QEvent::Gesture) {
|
||||
gestureEvent(static_cast<QGestureEvent *>(event));
|
||||
return true;
|
||||
} else if (event->type() == QEvent::GestureOverride) {
|
||||
QGestureEvent *ge = static_cast<QGestureEvent *>(event);
|
||||
if (QPanGesture *pan = static_cast<QPanGesture *>(ge->gesture(Qt::PanGesture)))
|
||||
if (pan->state() == Qt::GestureStarted) {
|
||||
outside = false;
|
||||
}
|
||||
}
|
||||
return QScrollArea::viewportEvent(event);
|
||||
}
|
||||
void gestureEvent(QGestureEvent *event)
|
||||
{
|
||||
QPanGesture *pan = static_cast<QPanGesture *>(event->gesture(Qt::PanGesture));
|
||||
if (pan) {
|
||||
switch(pan->state()) {
|
||||
case Qt::GestureStarted: qDebug() << this << "Pan: started"; break;
|
||||
case Qt::GestureFinished: qDebug() << this << "Pan: finished"; break;
|
||||
case Qt::GestureCanceled: qDebug() << this << "Pan: canceled"; break;
|
||||
case Qt::GestureUpdated: break;
|
||||
default: qDebug() << this << "Pan: <unknown state>"; break;
|
||||
}
|
||||
|
||||
if (pan->state() == Qt::GestureStarted)
|
||||
outside = false;
|
||||
event->ignore();
|
||||
event->ignore(pan);
|
||||
if (outside)
|
||||
return;
|
||||
|
||||
const QPointF delta = pan->delta();
|
||||
const QPointF totalOffset = pan->offset();
|
||||
QScrollBar *vbar = verticalScrollBar();
|
||||
QScrollBar *hbar = horizontalScrollBar();
|
||||
|
||||
if ((vbar->value() == vbar->minimum() && totalOffset.y() > 10) ||
|
||||
(vbar->value() == vbar->maximum() && totalOffset.y() < -10)) {
|
||||
outside = true;
|
||||
return;
|
||||
}
|
||||
if ((hbar->value() == hbar->minimum() && totalOffset.x() > 10) ||
|
||||
(hbar->value() == hbar->maximum() && totalOffset.x() < -10)) {
|
||||
outside = true;
|
||||
return;
|
||||
}
|
||||
vbar->setValue(vbar->value() - delta.y());
|
||||
hbar->setValue(hbar->value() - delta.x());
|
||||
event->accept(pan);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
bool outside;
|
||||
};
|
||||
|
||||
class Slider : public QSlider
|
||||
{
|
||||
public:
|
||||
Slider(Qt::Orientation orientation, QWidget *parent = nullptr)
|
||||
: QSlider(orientation, parent)
|
||||
{
|
||||
grabGesture(Qt::PanGesture);
|
||||
}
|
||||
protected:
|
||||
bool event(QEvent *event)
|
||||
{
|
||||
if (event->type() == QEvent::Gesture) {
|
||||
gestureEvent(static_cast<QGestureEvent *>(event));
|
||||
return true;
|
||||
}
|
||||
return QSlider::event(event);
|
||||
}
|
||||
void gestureEvent(QGestureEvent *event)
|
||||
{
|
||||
QPanGesture *pan = static_cast<QPanGesture *>(event->gesture(Qt::PanGesture));
|
||||
if (pan) {
|
||||
switch (pan->state()) {
|
||||
case Qt::GestureStarted: qDebug() << this << "Pan: started"; break;
|
||||
case Qt::GestureFinished: qDebug() << this << "Pan: finished"; break;
|
||||
case Qt::GestureCanceled: qDebug() << this << "Pan: canceled"; break;
|
||||
case Qt::GestureUpdated: break;
|
||||
default: qDebug() << this << "Pan: <unknown state>"; break;
|
||||
}
|
||||
|
||||
if (pan->state() == Qt::GestureStarted)
|
||||
outside = false;
|
||||
event->ignore();
|
||||
event->ignore(pan);
|
||||
if (outside)
|
||||
return;
|
||||
const QPointF delta = pan->delta();
|
||||
const QPointF totalOffset = pan->offset();
|
||||
if (orientation() == Qt::Horizontal) {
|
||||
if ((value() == minimum() && totalOffset.x() < -10) ||
|
||||
(value() == maximum() && totalOffset.x() > 10)) {
|
||||
outside = true;
|
||||
return;
|
||||
}
|
||||
if (totalOffset.y() < 40 && totalOffset.y() > -40) {
|
||||
setValue(value() + delta.x());
|
||||
event->accept(pan);
|
||||
} else {
|
||||
outside = true;
|
||||
}
|
||||
} else if (orientation() == Qt::Vertical) {
|
||||
if ((value() == maximum() && totalOffset.y() < -10) ||
|
||||
(value() == minimum() && totalOffset.y() > 10)) {
|
||||
outside = true;
|
||||
return;
|
||||
}
|
||||
if (totalOffset.x() < 40 && totalOffset.x() > -40) {
|
||||
setValue(value() - delta.y());
|
||||
event->accept(pan);
|
||||
} else {
|
||||
outside = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
private:
|
||||
bool outside;
|
||||
};
|
||||
|
||||
class MainWindow : public QMainWindow
|
||||
{
|
||||
public:
|
||||
MainWindow()
|
||||
{
|
||||
rootScrollArea = new ScrollArea;
|
||||
rootScrollArea->setObjectName(QLatin1String("rootScrollArea"));
|
||||
setCentralWidget(rootScrollArea);
|
||||
|
||||
QWidget *root = new QWidget;
|
||||
root->setFixedSize(3000, 3000);
|
||||
rootScrollArea->setWidget(root);
|
||||
|
||||
Slider *verticalSlider = new Slider(Qt::Vertical, root);
|
||||
verticalSlider->setObjectName(QLatin1String("verticalSlider"));
|
||||
verticalSlider ->move(650, 1100);
|
||||
Slider *horizontalSlider = new Slider(Qt::Horizontal, root);
|
||||
horizontalSlider->setObjectName(QLatin1String("horizontalSlider"));
|
||||
horizontalSlider ->move(600, 1000);
|
||||
|
||||
childScrollArea = new ScrollArea(root);
|
||||
childScrollArea->setObjectName(QLatin1String("childScrollArea"));
|
||||
childScrollArea->move(500, 500);
|
||||
QWidget *w = new QWidget;
|
||||
w->setMinimumWidth(700);
|
||||
QVBoxLayout *l = new QVBoxLayout(w);
|
||||
l->setContentsMargins(20, 20, 20, 20);
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
QWidget *w = new QWidget;
|
||||
QHBoxLayout *ll = new QHBoxLayout(w);
|
||||
ll->addWidget(new QLabel(QString("Label %1").arg(i)));
|
||||
ll->addWidget(new QPushButton(QString("Button %1").arg(i)));
|
||||
l->addWidget(w);
|
||||
}
|
||||
childScrollArea->setWidget(w);
|
||||
#if defined(Q_OS_WIN)
|
||||
// Windows can force Qt to create a native window handle for an
|
||||
// intermediate widget and that will block gesture to get touch events.
|
||||
// So this hack to make sure gestures get all touch events they need.
|
||||
foreach (QObject *w, children())
|
||||
if (w->isWidgetType())
|
||||
static_cast<QWidget *>(w)->setAttribute(Qt::WA_AcceptTouchEvents);
|
||||
#endif
|
||||
}
|
||||
private:
|
||||
ScrollArea *rootScrollArea;
|
||||
ScrollArea *childScrollArea;
|
||||
};
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
QGestureRecognizer::registerRecognizer(new MousePanGestureRecognizer);
|
||||
MainWindow w;
|
||||
w.show();
|
||||
return app.exec();
|
||||
}
|
||||
|
||||
#include "main.moc"
|
@ -0,0 +1,61 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
||||
|
||||
#include "mousepangesturerecognizer.h"
|
||||
|
||||
#include <QEvent>
|
||||
#include <QMouseEvent>
|
||||
#include <QGesture>
|
||||
|
||||
MousePanGestureRecognizer::MousePanGestureRecognizer()
|
||||
{
|
||||
}
|
||||
|
||||
QGesture *MousePanGestureRecognizer::create(QObject *)
|
||||
{
|
||||
return new QPanGesture;
|
||||
}
|
||||
|
||||
QGestureRecognizer::Result MousePanGestureRecognizer::recognize(QGesture *state, QObject *, QEvent *event)
|
||||
{
|
||||
QPanGesture *g = static_cast<QPanGesture *>(state);
|
||||
if (event->type() == QEvent::TouchBegin) {
|
||||
// ignore the following mousepress event
|
||||
g->setProperty("ignoreMousePress", QVariant::fromValue<bool>(true));
|
||||
} else if (event->type() == QEvent::TouchEnd) {
|
||||
g->setProperty("ignoreMousePress", QVariant::fromValue<bool>(false));
|
||||
}
|
||||
QMouseEvent *me = static_cast<QMouseEvent *>(event);
|
||||
if (event->type() == QEvent::MouseButtonPress) {
|
||||
if (g->property("ignoreMousePress").toBool())
|
||||
return QGestureRecognizer::Ignore;
|
||||
g->setHotSpot(me->globalPos());
|
||||
g->setProperty("startPos", me->globalPos());
|
||||
g->setProperty("pressed", QVariant::fromValue<bool>(true));
|
||||
return QGestureRecognizer::TriggerGesture | QGestureRecognizer::ConsumeEventHint;
|
||||
} else if (event->type() == QEvent::MouseMove) {
|
||||
if (g->property("pressed").toBool()) {
|
||||
QPoint offset = me->globalPos() - g->property("startPos").toPoint();
|
||||
g->setLastOffset(g->offset());
|
||||
g->setOffset(QPointF(offset.x(), offset.y()));
|
||||
return QGestureRecognizer::TriggerGesture | QGestureRecognizer::ConsumeEventHint;
|
||||
}
|
||||
return QGestureRecognizer::CancelGesture;
|
||||
} else if (event->type() == QEvent::MouseButtonRelease) {
|
||||
if (g->property("pressed").toBool())
|
||||
return QGestureRecognizer::FinishGesture | QGestureRecognizer::ConsumeEventHint;
|
||||
}
|
||||
return QGestureRecognizer::Ignore;
|
||||
}
|
||||
|
||||
void MousePanGestureRecognizer::reset(QGesture *state)
|
||||
{
|
||||
QPanGesture *g = static_cast<QPanGesture *>(state);
|
||||
g->setLastOffset(QPointF());
|
||||
g->setOffset(QPointF());
|
||||
g->setAcceleration(0);
|
||||
g->setProperty("startPos", QVariant());
|
||||
g->setProperty("pressed", QVariant::fromValue<bool>(false));
|
||||
g->setProperty("ignoreMousePress", QVariant::fromValue<bool>(false));
|
||||
QGestureRecognizer::reset(state);
|
||||
}
|
19
tests/manual/gestures/scrollarea/mousepangesturerecognizer.h
Normal file
19
tests/manual/gestures/scrollarea/mousepangesturerecognizer.h
Normal file
@ -0,0 +1,19 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
||||
|
||||
#ifndef MOUSEPANGESTURERECOGNIZER_H
|
||||
#define MOUSEPANGESTURERECOGNIZER_H
|
||||
|
||||
#include <QGestureRecognizer>
|
||||
|
||||
class MousePanGestureRecognizer : public QGestureRecognizer
|
||||
{
|
||||
public:
|
||||
MousePanGestureRecognizer();
|
||||
|
||||
QGesture* create(QObject *target);
|
||||
QGestureRecognizer::Result recognize(QGesture *state, QObject *watched, QEvent *event);
|
||||
void reset(QGesture *state);
|
||||
};
|
||||
|
||||
#endif // MOUSEPANGESTURERECOGNIZER_H
|
5
tests/manual/gestures/scrollarea/scrollarea.pro
Normal file
5
tests/manual/gestures/scrollarea/scrollarea.pro
Normal file
@ -0,0 +1,5 @@
|
||||
QT += widgets
|
||||
|
||||
SOURCES = main.cpp \
|
||||
mousepangesturerecognizer.cpp
|
||||
HEADERS += mousepangesturerecognizer.h
|
Reference in New Issue
Block a user