diff options
Diffstat (limited to 'examples/gestures/imageviewer/imagewidget.cpp')
-rw-r--r-- | examples/gestures/imageviewer/imagewidget.cpp | 82 |
1 files changed, 47 insertions, 35 deletions
diff --git a/examples/gestures/imageviewer/imagewidget.cpp b/examples/gestures/imageviewer/imagewidget.cpp index 2be6cc5..f3fd8e4 100644 --- a/examples/gestures/imageviewer/imagewidget.cpp +++ b/examples/gestures/imageviewer/imagewidget.cpp @@ -59,24 +59,16 @@ ImageWidget::ImageWidget(QWidget *parent) setAttribute(Qt::WA_OpaquePaintEvent); setAttribute(Qt::WA_NoSystemBackground); - QGesture *panGesture = new QPanGesture(this); - connect(panGesture, SIGNAL(started()), this, SLOT(panTriggered())); - connect(panGesture, SIGNAL(finished()), this, SLOT(panTriggered())); - connect(panGesture, SIGNAL(canceled()), this, SLOT(panTriggered())); - connect(panGesture, SIGNAL(triggered()), this, SLOT(panTriggered())); - - QGesture *pinchGesture = new QPinchGesture(this); - connect(pinchGesture, SIGNAL(started()), this, SLOT(pinchTriggered())); - connect(pinchGesture, SIGNAL(finished()), this, SLOT(pinchTriggered())); - connect(pinchGesture, SIGNAL(canceled()), this, SLOT(pinchTriggered())); - connect(pinchGesture, SIGNAL(triggered()), this, SLOT(pinchTriggered())); - -//! [construct swipe gesture] - QGesture *swipeGesture = new QSwipeGesture(this); -//! [construct swipe gesture] -//! [connect swipe gesture] - connect(swipeGesture, SIGNAL(triggered()), this, SLOT(swipeTriggered())); -//! [connect swipe gesture] + grabGesture(Qt::PanGesture); + grabGesture(Qt::PinchGesture); + grabGesture(Qt::SwipeGesture); +} + +bool ImageWidget::event(QEvent *event) +{ + if (event->type() == QEvent::Gesture) + return gestureEvent(static_cast<QGestureEvent*>(event)); + return QWidget::event(event); } void ImageWidget::paintEvent(QPaintEvent*) @@ -106,11 +98,25 @@ void ImageWidget::mouseDoubleClickEvent(QMouseEvent *) update(); } -void ImageWidget::panTriggered() +bool ImageWidget::gestureEvent(QGestureEvent *event) +{ + if (QGesture *pan = event->gesture(Qt::PanGesture)) { + panTriggered(static_cast<QPanGesture*>(pan)); + return true; + } else if (QGesture *pinch = event->gesture(Qt::PinchGesture)) { + pinchTriggered(static_cast<QPinchGesture*>(pinch)); + return true; + } else if (QGesture *swipe = event->gesture(Qt::SwipeGesture)) { + swipeTriggered(static_cast<QSwipeGesture*>(swipe)); + return true; + } + return false; +} + +void ImageWidget::panTriggered(QPanGesture *gesture) { - QPanGesture *pg = qobject_cast<QPanGesture*>(sender()); #ifndef QT_NO_CURSOR - switch (pg->state()) { + switch (gesture->state()) { case Qt::GestureStarted: case Qt::GestureUpdated: setCursor(Qt::SizeAllCursor); @@ -119,33 +125,39 @@ void ImageWidget::panTriggered() setCursor(Qt::ArrowCursor); } #endif - horizontalOffset += pg->lastOffset().width(); - verticalOffset += pg->lastOffset().height(); + QSizeF lastOffset = gesture->offset(); + horizontalOffset += lastOffset.width(); + verticalOffset += lastOffset.height(); update(); } -void ImageWidget::pinchTriggered() +void ImageWidget::pinchTriggered(QPinchGesture *gesture) { - QPinchGesture *pg = qobject_cast<QPinchGesture*>(sender()); - if (pg->whatChanged() & QPinchGesture::RotationAngleChanged) - rotationAngle += pg->rotationAngle() - pg->lastRotationAngle(); - if (pg->whatChanged() & QPinchGesture::ScaleFactorChanged) - scaleFactor += pg->scaleFactor() - pg->lastScaleFactor(); + QPinchGesture::WhatChanged whatChanged = gesture->whatChanged(); + if (whatChanged & QPinchGesture::RotationAngleChanged) { + qreal value = gesture->property("rotationAngle").toReal(); + qreal lastValue = gesture->property("lastRotationAngle").toReal(); + rotationAngle += value - lastValue; + } + if (whatChanged & QPinchGesture::ScaleFactorChanged) { + qreal value = gesture->property("scaleFactor").toReal(); + qreal lastValue = gesture->property("lastScaleFactor").toReal(); + scaleFactor += value - lastValue; + } update(); } -//! [swipe slot start] -void ImageWidget::swipeTriggered() +//! [swipe slot] +void ImageWidget::swipeTriggered(QSwipeGesture *gesture) { - QSwipeGesture *pg = qobject_cast<QSwipeGesture*>(sender()); - if (pg->horizontalDirection() == QSwipeGesture::Left - || pg->verticalDirection() == QSwipeGesture::Up) + if (gesture->horizontalDirection() == QSwipeGesture::Left + || gesture->verticalDirection() == QSwipeGesture::Up) goPrevImage(); else goNextImage(); update(); } -//! [swipe slot finish] +//! [swipe slot] void ImageWidget::resizeEvent(QResizeEvent*) { |