src/gui/kernel/qgesture.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     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 QtGui module 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 "qgesture.h"
       
    43 #include "private/qgesture_p.h"
       
    44 
       
    45 QT_BEGIN_NAMESPACE
       
    46 
       
    47  /*!
       
    48     \class QGesture
       
    49     \since 4.6
       
    50     \ingroup gestures
       
    51 
       
    52     \brief The QGesture class represents a gesture, containing properties that
       
    53     describe the corresponding user input.
       
    54 
       
    55     Gesture objects are not constructed directly by developers. They are created by
       
    56     the QGestureRecognizer object that is registered with the application; see
       
    57     QApplication::registerGestureRecognizer().
       
    58 
       
    59     \section1 Gesture Properties
       
    60 
       
    61     The class has a list of properties that can be queried by the user to get
       
    62     some gesture-specific arguments. For example, the pinch gesture has a scale
       
    63     factor that is exposed as a property.
       
    64 
       
    65     Developers of custom gesture recognizers can add additional properties in
       
    66     order to provide additional information about a gesture. This can be done
       
    67     by adding new dynamic properties to a QGesture object, or by subclassing
       
    68     the QGesture class (or one of its subclasses).
       
    69 
       
    70     \section1 Lifecycle of a Gesture Object
       
    71 
       
    72     A QGesture instance is created when the application calls QWidget::grabGesture()
       
    73     or QGraphicsObject::grabGesture() to configure a widget or graphics object (the
       
    74     target object) for gesture input. One gesture object is created for each target
       
    75     object.
       
    76 
       
    77     The registered gesture recognizer monitors the input events for the target
       
    78     object via its \l{QGestureRecognizer::}{filterEvent()} function, updating the
       
    79     properties of the gesture object as required.
       
    80 
       
    81     The gesture object may be delivered to the target object in a QGestureEvent if
       
    82     the corresponding gesture is active or has just been canceled. Each event that
       
    83     is delivered contains a list of gesture objects, since support for more than
       
    84     one gesture may be enabled for the target object. Due to the way events are
       
    85     handled in Qt, gesture events may be filtered by other objects.
       
    86 
       
    87     \sa QGestureEvent, QGestureRecognizer
       
    88 */
       
    89 
       
    90 /*!
       
    91     Constructs a new gesture object with the given \a parent.
       
    92 
       
    93     QGesture objects are created by gesture recognizers in the
       
    94     QGestureRecognizer::createGesture() function.
       
    95 */
       
    96 QGesture::QGesture(QObject *parent)
       
    97     : QObject(*new QGesturePrivate, parent)
       
    98 {
       
    99     d_func()->gestureType = Qt::CustomGesture;
       
   100 }
       
   101 
       
   102 /*!
       
   103     \internal
       
   104 */
       
   105 QGesture::QGesture(QGesturePrivate &dd, QObject *parent)
       
   106     : QObject(dd, parent)
       
   107 {
       
   108 }
       
   109 
       
   110 /*!
       
   111     Destroys the gesture object.
       
   112 */
       
   113 QGesture::~QGesture()
       
   114 {
       
   115 }
       
   116 
       
   117 /*!
       
   118     \property QGesture::state
       
   119     \brief the current state of the gesture
       
   120 */
       
   121 
       
   122 /*!
       
   123     \property QGesture::gestureType
       
   124     \brief the type of the gesture
       
   125 */
       
   126 
       
   127 /*!
       
   128     \property QGesture::hotSpot
       
   129 
       
   130     \brief The point that is used to find the receiver for the gesture event.
       
   131 
       
   132     The hot-spot is a point in the global coordinate system, use
       
   133     QWidget::mapFromGlobal() or QGestureEvent::mapToScene() to get a
       
   134     local hot-spot.
       
   135 
       
   136     If the hot-spot is not set, the targetObject is used as the receiver of the
       
   137     gesture event.
       
   138 */
       
   139 
       
   140 /*!
       
   141     \property QGesture::hasHotSpot
       
   142     \brief whether the gesture has a hot-spot
       
   143 */
       
   144 
       
   145 /*!
       
   146     \property QGesture::targetObject
       
   147     \brief the target object which will receive the gesture event if the hotSpot is
       
   148     not set
       
   149 */
       
   150 
       
   151 Qt::GestureType QGesture::gestureType() const
       
   152 {
       
   153     return d_func()->gestureType;
       
   154 }
       
   155 
       
   156 Qt::GestureState QGesture::state() const
       
   157 {
       
   158     return d_func()->state;
       
   159 }
       
   160 
       
   161 QPointF QGesture::hotSpot() const
       
   162 {
       
   163     return d_func()->hotSpot;
       
   164 }
       
   165 
       
   166 void QGesture::setHotSpot(const QPointF &value)
       
   167 {
       
   168     Q_D(QGesture);
       
   169     d->hotSpot = value;
       
   170     d->isHotSpotSet = true;
       
   171 }
       
   172 
       
   173 bool QGesture::hasHotSpot() const
       
   174 {
       
   175     return d_func()->isHotSpotSet;
       
   176 }
       
   177 
       
   178 void QGesture::unsetHotSpot()
       
   179 {
       
   180     d_func()->isHotSpotSet = false;
       
   181 }
       
   182 
       
   183 /*!
       
   184     \class QPanGesture
       
   185     \since 4.6
       
   186     \brief The QPanGesture class describes a panning gesture made by the user.
       
   187     \ingroup gestures
       
   188 
       
   189     \image pangesture.png
       
   190 
       
   191     \sa {Gestures Programming}, QPinchGesture, QSwipeGesture
       
   192 */
       
   193 
       
   194 /*!
       
   195     \property QPanGesture::totalOffset
       
   196     \brief the total offset from the first input position to the current input
       
   197     position
       
   198 
       
   199     The total offset measures the total change in position of the user's input
       
   200     covered by the gesture on the input device.
       
   201 */
       
   202 
       
   203 /*!
       
   204     \property QPanGesture::lastOffset
       
   205     \brief the last offset recorded for this gesture
       
   206 
       
   207     The last offset contains the change in position of the user's input as
       
   208     reported in the \l offset property when a previous gesture event was
       
   209     delivered for this gesture.
       
   210 
       
   211     If no previous event was delivered with information about this gesture
       
   212     (i.e., this gesture object contains information about the first movement
       
   213     in the gesture) then this property contains a zero size.
       
   214 */
       
   215 
       
   216 /*!
       
   217     \property QPanGesture::offset
       
   218     \brief the offset from the previous input position to the current input
       
   219     position
       
   220 
       
   221     The offset measures the change in position of the user's input on the
       
   222     input device.
       
   223 */
       
   224 
       
   225 /*!
       
   226     \property QPanGesture::acceleration
       
   227 */
       
   228 
       
   229 /*!
       
   230     \internal
       
   231 */
       
   232 QPanGesture::QPanGesture(QObject *parent)
       
   233     : QGesture(*new QPanGesturePrivate, parent)
       
   234 {
       
   235     d_func()->gestureType = Qt::PanGesture;
       
   236 }
       
   237 
       
   238 QPointF QPanGesture::totalOffset() const
       
   239 {
       
   240     return d_func()->totalOffset;
       
   241 }
       
   242 
       
   243 QPointF QPanGesture::lastOffset() const
       
   244 {
       
   245     return d_func()->lastOffset;
       
   246 }
       
   247 
       
   248 QPointF QPanGesture::offset() const
       
   249 {
       
   250     return d_func()->offset;
       
   251 }
       
   252 
       
   253 qreal QPanGesture::acceleration() const
       
   254 {
       
   255     return d_func()->acceleration;
       
   256 }
       
   257 
       
   258 
       
   259 void QPanGesture::setTotalOffset(const QPointF &value)
       
   260 {
       
   261     d_func()->totalOffset = value;
       
   262 }
       
   263 
       
   264 void QPanGesture::setLastOffset(const QPointF &value)
       
   265 {
       
   266     d_func()->lastOffset = value;
       
   267 }
       
   268 
       
   269 void QPanGesture::setOffset(const QPointF &value)
       
   270 {
       
   271     d_func()->offset = value;
       
   272 }
       
   273 
       
   274 void QPanGesture::setAcceleration(qreal value)
       
   275 {
       
   276     d_func()->acceleration = value;
       
   277 }
       
   278 
       
   279 /*!
       
   280     \class QPinchGesture
       
   281     \since 4.6
       
   282     \brief The QPinchGesture class describes a pinch gesture made my the user.
       
   283     \ingroup multitouch
       
   284     \ingroup gestures
       
   285 
       
   286     A pinch gesture is a form of multitouch user input in which the user typically
       
   287     touches two points on the input device with a thumb and finger, before moving
       
   288     them closer together or further apart to change the scale factor, zoom, or level
       
   289     of detail of the user interface.
       
   290 
       
   291     \image pinchgesture.png
       
   292 
       
   293     Instead of repeatedly applying the same pinching gesture, the user may
       
   294     continue to touch the input device in one place, and apply a second touch
       
   295     to a new point, continuing the gesture. When this occurs, gesture events
       
   296     will continue to be delivered to the target object, containing an instance
       
   297     of QPinchGesture in the Qt::GestureUpdated state.
       
   298 
       
   299     \sa {Gestures Programming}, QPanGesture, QSwipeGesture
       
   300 */
       
   301 
       
   302 /*!
       
   303     \enum QPinchGesture::WhatChange
       
   304     
       
   305     This enum describes the changes that can occur to the properties of
       
   306     the gesture object.
       
   307 
       
   308     \value ScaleFactorChanged The scale factor held by scaleFactor changed.
       
   309     \value RotationAngleChanged The rotation angle held by rotationAngle changed.
       
   310     \value CenterPointChanged The center point held by centerPoint changed.
       
   311 
       
   312     \sa whatChanged
       
   313 */
       
   314 
       
   315 /*!
       
   316     \property QPinchGesture::whatChanged
       
   317     \brief the property of the gesture that has changed
       
   318 
       
   319     This property indicates which of the other properties has changed since
       
   320     the previous gesture event included information about this gesture. You
       
   321     can use this information to determine which aspect of your user interface
       
   322     needs to be updated.
       
   323 
       
   324     \sa scaleFactor, rotationAngle, centerPoint
       
   325 */
       
   326 
       
   327 /*!
       
   328     \property QPinchGesture::totalScaleFactor
       
   329     \brief the total scale factor
       
   330 
       
   331     The total scale factor measures the total change in scale factor from the
       
   332     original value to the current scale factor.
       
   333 
       
   334     \sa scaleFactor, lastScaleFactor
       
   335 */
       
   336 /*!
       
   337     \property QPinchGesture::lastScaleFactor
       
   338     \brief the last scale factor recorded for this gesture
       
   339 
       
   340     The last scale factor contains the scale factor reported in the
       
   341     \l scaleFactor property when a previous gesture event included
       
   342     information about this gesture.
       
   343 
       
   344     If no previous event was delivered with information about this gesture
       
   345     (i.e., this gesture object contains information about the first movement
       
   346     in the gesture) then this property contains zero.
       
   347 
       
   348     \sa scaleFactor, totalScaleFactor
       
   349 */
       
   350 /*!
       
   351     \property QPinchGesture::scaleFactor
       
   352     \brief the current scale factor
       
   353 
       
   354     The scale factor measures the scale factor associated with the distance
       
   355     between two of the user's inputs on a multitouch device.
       
   356 
       
   357     \sa totalScaleFactor, lastScaleFactor
       
   358 */
       
   359 
       
   360 /*!
       
   361     \property QPinchGesture::totalRotationAngle
       
   362     \brief the total angle covered by the gesture
       
   363 
       
   364     This total angle measures the complete angle covered by the gesture. Usually, this
       
   365     is equal to the value held by the \l rotationAngle property, except in the case where
       
   366     the user performs multiple rotations by removing and repositioning one of the touch
       
   367     points, as described above. In this case, the total angle will be the sum of the
       
   368     rotation angles for the multiple stages of the gesture.
       
   369 
       
   370     \sa rotationAngle, lastRotationAngle
       
   371 */
       
   372 /*!
       
   373     \property QPinchGesture::lastRotationAngle
       
   374     \brief the last reported angle covered by the gesture motion
       
   375 
       
   376     The last rotation angle is the angle as reported in the \l rotationAngle property
       
   377     when a previous gesture event was delivered for this gesture.
       
   378 
       
   379     \sa rotationAngle, totalRotationAngle
       
   380 */
       
   381 /*!
       
   382     \property QPinchGesture::rotationAngle
       
   383     \brief the angle covered by the gesture motion
       
   384 
       
   385     \sa totalRotationAngle, lastRotationAngle
       
   386 */
       
   387 
       
   388 /*!
       
   389     \property QPinchGesture::startCenterPoint
       
   390     \brief the starting position of the center point
       
   391 
       
   392     \sa centerPoint, lastCenterPoint
       
   393 */
       
   394 /*!
       
   395     \property QPinchGesture::lastCenterPoint
       
   396     \brief the last position of the center point recorded for this gesture
       
   397 
       
   398     \sa centerPoint, startCenterPoint
       
   399 */
       
   400 /*!
       
   401     \property QPinchGesture::centerPoint
       
   402     \brief the current center point
       
   403 
       
   404     The center point is the midpoint between the two input points in the gesture.
       
   405 
       
   406     \sa startCenterPoint, lastCenterPoint
       
   407 */
       
   408 
       
   409 /*!
       
   410     \internal
       
   411 */
       
   412 QPinchGesture::QPinchGesture(QObject *parent)
       
   413     : QGesture(*new QPinchGesturePrivate, parent)
       
   414 {
       
   415     d_func()->gestureType = Qt::PinchGesture;
       
   416 }
       
   417 
       
   418 QPinchGesture::WhatChanged QPinchGesture::whatChanged() const
       
   419 {
       
   420     return d_func()->whatChanged;
       
   421 }
       
   422 
       
   423 void QPinchGesture::setWhatChanged(QPinchGesture::WhatChanged value)
       
   424 {
       
   425     d_func()->whatChanged = value;
       
   426 }
       
   427 
       
   428 
       
   429 QPointF QPinchGesture::startCenterPoint() const
       
   430 {
       
   431     return d_func()->startCenterPoint;
       
   432 }
       
   433 
       
   434 QPointF QPinchGesture::lastCenterPoint() const
       
   435 {
       
   436     return d_func()->lastCenterPoint;
       
   437 }
       
   438 
       
   439 QPointF QPinchGesture::centerPoint() const
       
   440 {
       
   441     return d_func()->centerPoint;
       
   442 }
       
   443 
       
   444 void QPinchGesture::setStartCenterPoint(const QPointF &value)
       
   445 {
       
   446     d_func()->startCenterPoint = value;
       
   447 }
       
   448 
       
   449 void QPinchGesture::setLastCenterPoint(const QPointF &value)
       
   450 {
       
   451     d_func()->lastCenterPoint = value;
       
   452 }
       
   453 
       
   454 void QPinchGesture::setCenterPoint(const QPointF &value)
       
   455 {
       
   456     d_func()->centerPoint = value;
       
   457 }
       
   458 
       
   459 
       
   460 qreal QPinchGesture::totalScaleFactor() const
       
   461 {
       
   462     return d_func()->totalScaleFactor;
       
   463 }
       
   464 
       
   465 qreal QPinchGesture::lastScaleFactor() const
       
   466 {
       
   467     return d_func()->lastScaleFactor;
       
   468 }
       
   469 
       
   470 qreal QPinchGesture::scaleFactor() const
       
   471 {
       
   472     return d_func()->scaleFactor;
       
   473 }
       
   474 
       
   475 void QPinchGesture::setTotalScaleFactor(qreal value)
       
   476 {
       
   477     d_func()->totalScaleFactor = value;
       
   478 }
       
   479 
       
   480 void QPinchGesture::setLastScaleFactor(qreal value)
       
   481 {
       
   482     d_func()->lastScaleFactor = value;
       
   483 }
       
   484 
       
   485 void QPinchGesture::setScaleFactor(qreal value)
       
   486 {
       
   487     d_func()->scaleFactor = value;
       
   488 }
       
   489 
       
   490 
       
   491 qreal QPinchGesture::totalRotationAngle() const
       
   492 {
       
   493     return d_func()->totalRotationAngle;
       
   494 }
       
   495 
       
   496 qreal QPinchGesture::lastRotationAngle() const
       
   497 {
       
   498     return d_func()->lastRotationAngle;
       
   499 }
       
   500 
       
   501 qreal QPinchGesture::rotationAngle() const
       
   502 {
       
   503     return d_func()->rotationAngle;
       
   504 }
       
   505 
       
   506 void QPinchGesture::setTotalRotationAngle(qreal value)
       
   507 {
       
   508     d_func()->totalRotationAngle = value;
       
   509 }
       
   510 
       
   511 void QPinchGesture::setLastRotationAngle(qreal value)
       
   512 {
       
   513     d_func()->lastRotationAngle = value;
       
   514 }
       
   515 
       
   516 void QPinchGesture::setRotationAngle(qreal value)
       
   517 {
       
   518     d_func()->rotationAngle = value;
       
   519 }
       
   520 
       
   521 /*!
       
   522     \class QSwipeGesture
       
   523     \since 4.6
       
   524     \brief The QSwipeGesture class describes a swipe gesture made by the user.
       
   525     \ingroup gestures
       
   526 
       
   527     \image swipegesture.png
       
   528 
       
   529     \sa {Gestures Programming}, QPanGesture, QPinchGesture
       
   530 */
       
   531 
       
   532 /*!
       
   533     \enum QSwipeGesture::SwipeDirection
       
   534 
       
   535     This enum describes the possible directions for the gesture's motion
       
   536     along the horizontal and vertical axes.
       
   537 
       
   538     \value NoDirection The gesture had no motion associated with it on a particular axis.
       
   539     \value Left     The gesture involved a horizontal motion to the left.
       
   540     \value Right    The gesture involved a horizontal motion to the right.
       
   541     \value Up       The gesture involved an upward vertical motion.
       
   542     \value Down     The gesture involved a downward vertical motion.
       
   543 */
       
   544 
       
   545 /*!
       
   546     \property QSwipeGesture::horizontalDirection
       
   547     \brief the horizontal direction of the gesture
       
   548 
       
   549     If the gesture has a horizontal component, the horizontal direction
       
   550     is either Left or Right; otherwise, it is NoDirection.
       
   551 
       
   552     \sa verticalDirection, swipeAngle
       
   553 */
       
   554 
       
   555 /*!
       
   556     \property QSwipeGesture::verticalDirection
       
   557     \brief the vertical direction of the gesture
       
   558 
       
   559     If the gesture has a vertical component, the vertical direction
       
   560     is either Up or Down; otherwise, it is NoDirection.
       
   561 
       
   562     \sa horizontalDirection, swipeAngle
       
   563 */
       
   564 
       
   565 /*!
       
   566     \property QSwipeGesture::swipeAngle
       
   567     \brief the angle of the motion associated with the gesture
       
   568 
       
   569     If the gesture has either a horizontal or vertical component, the
       
   570     swipe angle describes the angle between the direction of motion and the
       
   571     x-axis as defined using the standard widget
       
   572     \l{The Coordinate System}{coordinate system}.
       
   573 
       
   574     \sa horizontalDirection, verticalDirection
       
   575 */
       
   576 
       
   577 /*!
       
   578     \internal
       
   579 */
       
   580 QSwipeGesture::QSwipeGesture(QObject *parent)
       
   581     : QGesture(*new QSwipeGesturePrivate, parent)
       
   582 {
       
   583     d_func()->gestureType = Qt::SwipeGesture;
       
   584 }
       
   585 
       
   586 QSwipeGesture::SwipeDirection QSwipeGesture::horizontalDirection() const
       
   587 {
       
   588     Q_D(const QSwipeGesture);
       
   589     if (d->swipeAngle < 0 || d->swipeAngle == 90 || d->swipeAngle == 270)
       
   590         return QSwipeGesture::NoDirection;
       
   591     else if (d->swipeAngle < 90 || d->swipeAngle > 270)
       
   592         return QSwipeGesture::Right;
       
   593     else
       
   594         return QSwipeGesture::Left;
       
   595 }
       
   596 
       
   597 QSwipeGesture::SwipeDirection QSwipeGesture::verticalDirection() const
       
   598 {
       
   599     Q_D(const QSwipeGesture);
       
   600     if (d->swipeAngle <= 0 || d->swipeAngle == 180)
       
   601         return QSwipeGesture::NoDirection;
       
   602     else if (d->swipeAngle < 180)
       
   603         return QSwipeGesture::Up;
       
   604     else
       
   605         return QSwipeGesture::Down;
       
   606 }
       
   607 
       
   608 qreal QSwipeGesture::swipeAngle() const
       
   609 {
       
   610     return d_func()->swipeAngle;
       
   611 }
       
   612 
       
   613 void QSwipeGesture::setSwipeAngle(qreal value)
       
   614 {
       
   615     d_func()->swipeAngle = value;
       
   616 }
       
   617 
       
   618 QT_END_NAMESPACE