From 3a345056c734f0d475b3ecd9959b9ae83ae70acc Mon Sep 17 00:00:00 2001 From: Frederik Gladhorn Date: Sat, 23 Jun 2012 11:29:59 +0200 Subject: [PATCH] Add mouse cursor shapes to MouseArea This adds a new property that uses the enum from the Qt namespace to allow setting of custom cursor shapes for mouse areas. Change-Id: I4d1ac6339b69dc9ea7855a8b8b6aa9276c71a90d Reviewed-by: Jeremy Katz --- src/quick/items/qquickmousearea.cpp | 78 +++++++++++++++++++++++++++++++++++ src/quick/items/qquickmousearea_p.h | 11 +++++ src/quick/items/qquickmousearea_p_p.h | 3 ++ 3 files changed, 92 insertions(+) diff --git a/src/quick/items/qquickmousearea.cpp b/src/quick/items/qquickmousearea.cpp index bf949e8..17458ab 100644 --- a/src/quick/items/qquickmousearea.cpp +++ b/src/quick/items/qquickmousearea.cpp @@ -197,6 +197,9 @@ QQuickMouseAreaPrivate::QQuickMouseAreaPrivate() #ifndef QT_NO_DRAGANDDROP , drag(0) #endif +#ifndef QT_NO_CURSOR + , cursor(0) +#endif { } @@ -205,6 +208,9 @@ QQuickMouseAreaPrivate::~QQuickMouseAreaPrivate() #ifndef QT_NO_DRAGANDDROP delete drag; #endif +#ifndef QT_NO_CURSOR + delete cursor; +#endif } void QQuickMouseAreaPrivate::init() @@ -1119,8 +1125,18 @@ void QQuickMouseArea::setHovered(bool h) d->hovered = h; emit hoveredChanged(); d->hovered ? emit entered() : emit exited(); +#ifndef QT_NO_CURSOR + if (d->cursor) { + if (d->hovered) { + canvas()->setCursor(QCursor(*d->cursor)); + } else { + canvas()->unsetCursor(); + } + } +#endif } } + /*! \qmlproperty QtQuick2::Qt::MouseButtons MouseArea::acceptedButtons This property holds the mouse buttons that the mouse area reacts to. @@ -1192,6 +1208,68 @@ bool QQuickMouseArea::setPressed(bool p) return false; } + +/*! + \qmlproperty QtQuick2::Qt::CursorShape MouseArea::cursorShape + This property holds the cursor shape for this mouse area. + Note that on platforms that do not display a mouse cursor this may have + no effect. + + The available cursor shapes are: + \list + \li Qt.ArrowCursor + \li Qt.UpArrowCursor + \li Qt.CrossCursor + \li Qt.WaitCursor + \li Qt.IBeamCursor + \li Qt.SizeVerCursor + \li Qt.SizeHorCursor + \li Qt.SizeBDiagCursor + \li Qt.SizeFDiagCursor + \li Qt.SizeAllCursor + \li Qt.BlankCursor + \li Qt.SplitVCursor + \li Qt.SplitHCursor + \li Qt.PointingHandCursor + \li Qt.ForbiddenCursor + \li Qt.WhatsThisCursor + \li Qt.BusyCursor + \li Qt.OpenHandCursor + \li Qt.ClosedHandCursor + \li Qt.DragCopyCursor + \li Qt.DragMoveCursor + \li Qt.DragLinkCursor + \endlist + + In order to only set a mouse cursor shape for a region without reacting + to mouse events set the acceptedButtons to none: + + \code + MouseArea { cursorShape: Qt.IBeamCursor; acceptedButtons: Qt.NoButton } + \endcode + + The default value is \c Qt.ArrowCursor. + \sa Qt::CursorShape +*/ + +#ifndef QT_NO_CURSOR +Qt::CursorShape QQuickMouseArea::cursorShape() const +{ + Q_D(const QQuickMouseArea); + if (d->cursor) + return d->cursor->shape(); + return Qt::ArrowCursor; +} + +void QQuickMouseArea::setCursorShape(Qt::CursorShape shape) +{ + Q_D(QQuickMouseArea); + setHoverEnabled(true); + delete d->cursor; + d->cursor = new QCursor(shape); +} +#endif + /*! \qmlproperty Item QtQuick2::MouseArea::drag.target \qmlproperty bool QtQuick2::MouseArea::drag.active diff --git a/src/quick/items/qquickmousearea_p.h b/src/quick/items/qquickmousearea_p.h index aee7809..14b74f4 100644 --- a/src/quick/items/qquickmousearea_p.h +++ b/src/quick/items/qquickmousearea_p.h @@ -143,6 +143,9 @@ class Q_QUICK_PRIVATE_EXPORT QQuickMouseArea : public QQuickItem #endif Q_PROPERTY(bool preventStealing READ preventStealing WRITE setPreventStealing NOTIFY preventStealingChanged) Q_PROPERTY(bool propagateComposedEvents READ propagateComposedEvents WRITE setPropagateComposedEvents NOTIFY propagateComposedEventsChanged) +#ifndef QT_NO_CURSOR + Q_PROPERTY(Qt::CursorShape cursorShape READ cursorShape WRITE setCursorShape NOTIFY cursorShapeChanged) +#endif public: QQuickMouseArea(QQuickItem *parent=0); @@ -175,12 +178,20 @@ public: bool propagateComposedEvents() const; void setPropagateComposedEvents(bool propagate); +#ifndef QT_NO_CURSOR + Qt::CursorShape cursorShape() const; + void setCursorShape(Qt::CursorShape shape); +#endif + Q_SIGNALS: void hoveredChanged(); void pressedChanged(); void enabledChanged(); void acceptedButtonsChanged(); void hoverEnabledChanged(); +#ifndef QT_NO_CURSOR + void cursorShapeChanged(); +#endif void positionChanged(QQuickMouseEvent *mouse); void mouseXChanged(QQuickMouseEvent *mouse); void mouseYChanged(QQuickMouseEvent *mouse); diff --git a/src/quick/items/qquickmousearea_p_p.h b/src/quick/items/qquickmousearea_p_p.h index 4e4b9a8..39a0616 100644 --- a/src/quick/items/qquickmousearea_p_p.h +++ b/src/quick/items/qquickmousearea_p_p.h @@ -107,6 +107,9 @@ public: Qt::MouseButtons lastButtons; Qt::KeyboardModifiers lastModifiers; QBasicTimer pressAndHoldTimer; +#ifndef QT_NO_CURSOR + QCursor *cursor; +#endif }; QT_END_NAMESPACE -- 2.7.4