diff options
Diffstat (limited to 'examples/widgets/gestures/imagegestures')
6 files changed, 84 insertions, 56 deletions
diff --git a/examples/widgets/gestures/imagegestures/doc/src/imagegestures.qdoc b/examples/widgets/gestures/imagegestures/doc/src/imagegestures.qdoc index 6a1405b0b1..2751c64e76 100644 --- a/examples/widgets/gestures/imagegestures/doc/src/imagegestures.qdoc +++ b/examples/widgets/gestures/imagegestures/doc/src/imagegestures.qdoc @@ -1,7 +1,7 @@ /**************************************************************************** ** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ ** ** This file is part of the documentation of the Qt Toolkit. ** @@ -10,15 +10,15 @@ ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and Digia. For licensing terms and -** conditions see http://qt.digia.com/licensing. For further information -** use the contact form at http://qt.digia.com/contact-us. +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. ** ** GNU Free Documentation License Usage ** Alternatively, this file may be used under the terms of the GNU Free ** Documentation License version 1.3 as published by the Free Software ** Foundation and appearing in the file included in the packaging of -** this file. Please review the following information to ensure +** this file. Please review the following information to ensure ** the GNU Free Documentation License version 1.3 requirements ** will be met: http://www.gnu.org/copyleft/fdl.html. ** $QT_END_LICENSE$ @@ -86,18 +86,50 @@ QGesture subclass. To illustrate how a standard gesture can be interpreted by an application, we - show the implementation of the \c swipeTriggered() function, which handles the - gesture associated with a brushing or swiping motion on the user's display or + show the implementation of the \c pinchTriggered() function, which handles the + pinch gesture when the user moves two fingers around on the display or input device: - \snippet gestures/imagegestures/imagewidget.cpp swipe function - - The QSwipeGesture class provides specialized functions and defines a enum - to make it more convenient for developers to discover which direction, if - any, the user swiped the display. Here, we simply navigate to the previous - image in the collection if the user swiped upwards or to the left; otherwise - we navigate to the next image in the collection. - - The other gestures are also handled by special purpose functions, but use - the values of properties held by the QGesture object passed to them. + \snippet gestures/imagegestures/imagewidget.cpp pinch function + + The QPinchGesture class provides properties to interpret the changing + distance between the two touch points as a zoom factor, and the angle delta + as a rotation to be applied to the image. The center point between the + touch points could be used to drag the image, but in this example we use + the pan gesture for that purpose. + + The \c scaleFactor() is a relative value representing how much the zoom + should change from one event to the next, whereas \c totalScaleFactor() + provides the amount of zoom that has been expressed since the gesture + began. When the touch points are released and another gesture begins, + \c totalScaleFactor() will begin again at 1.0. In this case we store + \c totalScaleFactor() into the \c currentStepScaleFactor variable so that + it can be used in \c paintEvent() to scale the image. Alternatively it would + be possible to simply multiply the stored total scale factor by + \c scaleFactor() here in the pinch handler. + + In contrast, \c rotationAngle() represents the amount of rotation since the + pinch gesture began, while \c lastRotationAngle() provides the previous + value. So it is necessary to subtract in order to get an incremental delta. + When the user begins a new pinch gesture, \c rotationAngle() will start + from zero, and we want the image to begin to rotate from its current angle. + This is achieved by adding the delta to the stored \c rotationAngle (which + will be applied in \c paintEvent()). If we simply assigned + \c totalRotationAngle() to the stored \c rotationAngle, a new gesture would + cause the image to reset to a right-side-up orientation before beginning to + rotate again. But it would be possible to store the rotation angle since the + gesture began and add it to \c rotationAngle in \c paintEvent(), just as + we store the amount of zoom since the gesture began. + + The pan and swipe gestures in this example are also handled in separate + functions, and use the values of properties from the QGesture objects + passed to them. + + \snippet gestures/imagegestures/imagewidget.cpp paint method + + In \c paintEvent(), scaleFactor represents the zoom level before the pinch + gesture began, while currentStepScaleFactor represents the additional zoom + factor while a pinch gesture is in progress. But for rotation, only the + current rotationAngle is stored. The horizontal and vertical offsets + represent the distance that the image has been dragged by the pan gesture. */ diff --git a/examples/widgets/gestures/imagegestures/imagewidget.cpp b/examples/widgets/gestures/imagegestures/imagewidget.cpp index 57c2af4502..3d0d7e7a93 100644 --- a/examples/widgets/gestures/imagegestures/imagewidget.cpp +++ b/examples/widgets/gestures/imagegestures/imagewidget.cpp @@ -1,8 +1,8 @@ /**************************************************************************** ** -** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ ** ** This file is part of the examples of the Qt Toolkit. ** @@ -18,8 +18,8 @@ ** notice, this list of conditions and the following disclaimer in ** the documentation and/or other materials provided with the ** distribution. -** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names -** of its contributors may be used to endorse or promote products derived +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived ** from this software without specific prior written permission. ** ** @@ -77,6 +77,7 @@ bool ImageWidget::event(QEvent *event) } //! [event handler] +//! [paint method] void ImageWidget::paintEvent(QPaintEvent*) { QPainter p(this); @@ -93,6 +94,7 @@ void ImageWidget::paintEvent(QPaintEvent*) p.translate(-iw/2, -ih/2); p.drawImage(0, 0, currentImage); } +//! [paint method] void ImageWidget::mouseDoubleClickEvent(QMouseEvent *) { @@ -108,7 +110,7 @@ void ImageWidget::mouseDoubleClickEvent(QMouseEvent *) //! [gesture event handler] bool ImageWidget::gestureEvent(QGestureEvent *event) { - qCDebug(lcExample) << "gestureEvent():" << event->gestures().size(); + qCDebug(lcExample) << "gestureEvent():" << event; if (QGesture *swipe = event->gesture(Qt::SwipeGesture)) swipeTriggered(static_cast<QSwipeGesture *>(swipe)); else if (QGesture *pan = event->gesture(Qt::PanGesture)) @@ -132,26 +134,26 @@ void ImageWidget::panTriggered(QPanGesture *gesture) } #endif QPointF delta = gesture->delta(); - qCDebug(lcExample) << "panTriggered():" << delta; + qCDebug(lcExample) << "panTriggered():" << gesture; horizontalOffset += delta.x(); verticalOffset += delta.y(); update(); } +//! [pinch function] void ImageWidget::pinchTriggered(QPinchGesture *gesture) { QPinchGesture::ChangeFlags changeFlags = gesture->changeFlags(); if (changeFlags & QPinchGesture::RotationAngleChanged) { - const qreal value = gesture->property("rotationAngle").toReal(); - const qreal lastValue = gesture->property("lastRotationAngle").toReal(); - const qreal rotationAngleDelta = value - lastValue; - rotationAngle += rotationAngleDelta; - qCDebug(lcExample) << "pinchTriggered(): rotation by" << rotationAngleDelta << rotationAngle; + qreal rotationDelta = gesture->rotationAngle() - gesture->lastRotationAngle(); + rotationAngle += rotationDelta; + qCDebug(lcExample) << "pinchTriggered(): rotate by" << + rotationDelta << "->" << rotationAngle; } if (changeFlags & QPinchGesture::ScaleFactorChanged) { - qreal value = gesture->property("scaleFactor").toReal(); - currentStepScaleFactor = value; - qCDebug(lcExample) << "pinchTriggered(): " << currentStepScaleFactor; + currentStepScaleFactor = gesture->totalScaleFactor(); + qCDebug(lcExample) << "pinchTriggered(): zoom by" << + gesture->scaleFactor() << "->" << currentStepScaleFactor; } if (gesture->state() == Qt::GestureFinished) { scaleFactor *= currentStepScaleFactor; @@ -159,6 +161,7 @@ void ImageWidget::pinchTriggered(QPinchGesture *gesture) } update(); } +//! [pinch function] //! [swipe function] void ImageWidget::swipeTriggered(QSwipeGesture *gesture) diff --git a/examples/widgets/gestures/imagegestures/imagewidget.h b/examples/widgets/gestures/imagegestures/imagewidget.h index fc1faf9379..1629516c0a 100644 --- a/examples/widgets/gestures/imagegestures/imagewidget.h +++ b/examples/widgets/gestures/imagegestures/imagewidget.h @@ -1,7 +1,7 @@ /**************************************************************************** ** -** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ ** ** This file is part of the examples of the Qt Toolkit. ** @@ -17,8 +17,8 @@ ** notice, this list of conditions and the following disclaimer in ** the documentation and/or other materials provided with the ** distribution. -** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names -** of its contributors may be used to endorse or promote products derived +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived ** from this software without specific prior written permission. ** ** diff --git a/examples/widgets/gestures/imagegestures/main.cpp b/examples/widgets/gestures/imagegestures/main.cpp index 80f275bd5f..b65c0cf949 100644 --- a/examples/widgets/gestures/imagegestures/main.cpp +++ b/examples/widgets/gestures/imagegestures/main.cpp @@ -1,7 +1,7 @@ /**************************************************************************** ** -** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ ** ** This file is part of the examples of the Qt Toolkit. ** @@ -17,8 +17,8 @@ ** notice, this list of conditions and the following disclaimer in ** the documentation and/or other materials provided with the ** distribution. -** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names -** of its contributors may be used to endorse or promote products derived +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived ** from this software without specific prior written permission. ** ** @@ -82,14 +82,7 @@ int main(int argc, char *argv[]) "qt.examples.imagegestures.debug=true\n"); commandLineParser.setApplicationDescription(description); - if (!commandLineParser.parse(QCoreApplication::arguments())) { - showHelp(commandLineParser, commandLineParser.errorText()); - return -1; - } - if (commandLineParser.isSet(helpOption)) { - showHelp(commandLineParser); - return 0; - } + commandLineParser.process(QCoreApplication::arguments()); QStringList arguments = commandLineParser.positionalArguments(); if (!arguments.isEmpty() && !QFileInfo(arguments.front()).isDir()) { diff --git a/examples/widgets/gestures/imagegestures/mainwidget.cpp b/examples/widgets/gestures/imagegestures/mainwidget.cpp index 74c2cc5bb1..a59dcebcff 100644 --- a/examples/widgets/gestures/imagegestures/mainwidget.cpp +++ b/examples/widgets/gestures/imagegestures/mainwidget.cpp @@ -1,7 +1,7 @@ /**************************************************************************** ** -** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ ** ** This file is part of the examples of the Qt Toolkit. ** @@ -17,8 +17,8 @@ ** notice, this list of conditions and the following disclaimer in ** the documentation and/or other materials provided with the ** distribution. -** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names -** of its contributors may be used to endorse or promote products derived +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived ** from this software without specific prior written permission. ** ** diff --git a/examples/widgets/gestures/imagegestures/mainwidget.h b/examples/widgets/gestures/imagegestures/mainwidget.h index 79ed3a087a..20e32d1afb 100644 --- a/examples/widgets/gestures/imagegestures/mainwidget.h +++ b/examples/widgets/gestures/imagegestures/mainwidget.h @@ -1,7 +1,7 @@ /**************************************************************************** ** -** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/legal +** Copyright (C) 2015 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ ** ** This file is part of the examples of the Qt Toolkit. ** @@ -17,8 +17,8 @@ ** notice, this list of conditions and the following disclaimer in ** the documentation and/or other materials provided with the ** distribution. -** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names -** of its contributors may be used to endorse or promote products derived +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived ** from this software without specific prior written permission. ** ** |