From d782ceee4de31bc4a11cd6cdeacc881bd1c118d8 Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Mon, 27 Apr 2009 17:39:47 +1000 Subject: Make MouseRegion signals pass a mouse event as their parameter. --- examples/declarative/connections/connections.qml | 6 +- examples/declarative/mouseregion/mouse.qml | 12 +-- src/declarative/fx/fx.pri | 2 + src/declarative/fx/qfxevents.cpp | 72 +++++++++++++ src/declarative/fx/qfxevents_p.h | 127 +++++++++++++++++++++++ src/declarative/fx/qfxitem.cpp | 27 +---- src/declarative/fx/qfxitem.h | 5 +- src/declarative/fx/qfxmouseregion.cpp | 59 +++++++---- src/declarative/fx/qfxmouseregion.h | 11 +- src/declarative/fx/qfxmouseregion_p.h | 11 ++ 10 files changed, 269 insertions(+), 63 deletions(-) create mode 100644 src/declarative/fx/qfxevents.cpp create mode 100644 src/declarative/fx/qfxevents_p.h diff --git a/examples/declarative/connections/connections.qml b/examples/declarative/connections/connections.qml index 3146338..45c0e99 100644 --- a/examples/declarative/connections/connections.qml +++ b/examples/declarative/connections/connections.qml @@ -1,9 +1,9 @@ - + color="green"; - dot.x = x-1; - dot.y = y-1; + dot.x = mouse.x-1; + dot.y = mouse.y-1; diff --git a/examples/declarative/mouseregion/mouse.qml b/examples/declarative/mouseregion/mouse.qml index f0f22ac..9581da2 100644 --- a/examples/declarative/mouseregion/mouse.qml +++ b/examples/declarative/mouseregion/mouse.qml @@ -1,10 +1,10 @@ - @@ -14,8 +14,8 @@ +#include +#include +#include + +QT_BEGIN_NAMESPACE + +class QFxKeyEvent : public QObject +{ + Q_OBJECT + Q_PROPERTY(int key READ key); + Q_PROPERTY(QString text READ text); + Q_PROPERTY(Qt::KeyboardModifiers modifiers READ modifiers); + Q_PROPERTY(bool isAutoRepeat READ isAutoRepeat); + Q_PROPERTY(int count READ count); + Q_PROPERTY(bool accepted READ isAccepted WRITE setAccepted); + +public: + QFxKeyEvent(QEvent::Type type, int key, Qt::KeyboardModifiers modifiers, const QString &text=QString(), bool autorep=false, ushort count=1) + : event(type, key, modifiers, text, autorep, count) { event.setAccepted(false); } + QFxKeyEvent(const QKeyEvent &ke) + : event(ke) { event.setAccepted(false); } + + int key() const { return event.key(); } + QString text() const { return event.text(); } + Qt::KeyboardModifiers modifiers() const { return event.modifiers(); } + bool isAutoRepeat() const { return event.isAutoRepeat(); } + int count() const { return event.count(); } + + bool isAccepted() { return event.isAccepted(); } + void setAccepted(bool accepted) { event.setAccepted(accepted); } + +private: + QKeyEvent event; +}; + +QML_DECLARE_TYPE(QFxKeyEvent); + +class QFxMouseEvent : public QObject +{ + Q_OBJECT + Q_PROPERTY(int x READ x); + Q_PROPERTY(int y READ y); + Q_PROPERTY(Qt::MouseButton button READ button); + Q_PROPERTY(Qt::MouseButtons buttons READ buttons); + Q_PROPERTY(Qt::KeyboardModifiers modifiers READ modifiers); + Q_PROPERTY(bool wasHeld READ wasHeld); + Q_PROPERTY(bool isClick READ isClick); + Q_PROPERTY(bool accepted READ isAccepted WRITE setAccepted); + +public: + QFxMouseEvent(int x, int y, Qt::MouseButton button, Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers + , bool isClick=false, bool wasHeld=false) + : _x(x), _y(y), _button(button), _buttons(buttons), _modifiers(modifiers) + , _wasHeld(wasHeld), _isClick(isClick), _accepted(false) {} + + int x() const { return _x; } + int y() const { return _y; } + Qt::MouseButton button() const { return _button; } + Qt::MouseButtons buttons() const { return _buttons; } + Qt::KeyboardModifiers modifiers() const { return _modifiers; } + bool wasHeld() const { return _wasHeld; } + bool isClick() const { return _isClick; } + + bool isAccepted() { return _accepted; } + void setAccepted(bool accepted) { _accepted = accepted; } + +private: + int _x; + int _y; + Qt::MouseButton _button; + Qt::MouseButtons _buttons; + Qt::KeyboardModifiers _modifiers; + bool _wasHeld; + bool _isClick; + bool _accepted; +}; + +QML_DECLARE_TYPE(QFxMouseEvent); + +QT_END_NAMESPACE + +#endif // QFXEVENTS_P_H diff --git a/src/declarative/fx/qfxitem.cpp b/src/declarative/fx/qfxitem.cpp index 240aef6..c2e35b9 100644 --- a/src/declarative/fx/qfxitem.cpp +++ b/src/declarative/fx/qfxitem.cpp @@ -60,6 +60,7 @@ #include "qfxitem_p.h" #include "qfxitem.h" +#include "qfxevents_p.h" #include #include @@ -1024,34 +1025,13 @@ void QFxItem::setFlipHorizontally(bool v) setFlip((QSimpleCanvasItem::Flip)(flip() & ~HorizontalFlip)); } -class QFxKeyEvent : public QObject -{ - Q_OBJECT - Q_PROPERTY(int key READ key); - Q_PROPERTY(QString text READ text); - Q_PROPERTY(bool accepted READ isAccepted WRITE setAccepted); -public: - QFxKeyEvent(int key, const QString &text=QString()) : _accepted(false), _key(key), _text(text) {} - - bool isAccepted() { return _accepted; } - void setAccepted(bool accepted) { _accepted = accepted; } - - int key() const { return _key; } - - QString text() const { return _text; } - -private: - bool _accepted; - int _key; - QString _text; -}; /*! \reimp */ void QFxItem::keyPressEvent(QKeyEvent *event) { - QFxKeyEvent ke(event->key(), event->text()); + QFxKeyEvent ke(*event); emit keyPress(&ke); event->setAccepted(ke.isAccepted()); if (itemParent() && !ke.isAccepted()) @@ -1063,7 +1043,7 @@ void QFxItem::keyPressEvent(QKeyEvent *event) */ void QFxItem::keyReleaseEvent(QKeyEvent *event) { - QFxKeyEvent ke(event->key(), event->text()); + QFxKeyEvent ke(*event); emit keyRelease(&ke); event->setAccepted(ke.isAccepted()); if (itemParent() && !ke.isAccepted()) @@ -1777,5 +1757,4 @@ QFxItemPrivate::AnchorLines::AnchorLines(QFxItem *q) vCenter.anchorLine = QFxAnchorLine::VCenter; } -#include "qfxitem.moc" QT_END_NAMESPACE diff --git a/src/declarative/fx/qfxitem.h b/src/declarative/fx/qfxitem.h index 7de0aae..3f7784b 100644 --- a/src/declarative/fx/qfxitem.h +++ b/src/declarative/fx/qfxitem.h @@ -94,6 +94,7 @@ class QmlContext; class QmlState; class QmlTransition; class QFxTransform; +class QFxKeyEvent; class QFxItemPrivate; class Q_DECLARATIVE_EXPORT QFxItem : public QSimpleCanvasItem, public QmlParserStatus { @@ -228,8 +229,8 @@ Q_SIGNALS: void focusChanged(); void activeFocusChanged(); void parentChanged(); - void keyPress(QObject *event); - void keyRelease(QObject *event); + void keyPress(QFxKeyEvent *event); + void keyRelease(QFxKeyEvent *event); void rotationChanged(); void scaleChanged(); void opacityChanged(); diff --git a/src/declarative/fx/qfxmouseregion.cpp b/src/declarative/fx/qfxmouseregion.cpp index c6a7f96..bfd03f7 100644 --- a/src/declarative/fx/qfxmouseregion.cpp +++ b/src/declarative/fx/qfxmouseregion.cpp @@ -41,6 +41,7 @@ #include "qfxmouseregion.h" #include "qfxmouseregion_p.h" +#include "qfxevents_p.h" #include @@ -154,12 +155,15 @@ void QFxDrag::setYmax(int m) \endcode - For the mouse handlers the variable mouseButton is set to be one of 'Left', 'Right', 'Middle', - or 'None'. This allows you to distinguish left and right clicking. Below we have the previous + Many MouseRegion signals pass a \l {qml-mouseevent}{mouse} parameter that contains + additional information about the mouse event, such as the position, button, + and any key modifiers. + + Below we have the previous example extended so as to give a different color when you right click. \code - + \endcode @@ -197,46 +201,50 @@ void QFxDrag::setYmax(int m) */ /*! - \qmlsignal MouseRegion::onClicked + \qmlsignal MouseRegion::onClicked(mouse) This handler is called when there is a click. A click is defined as a press followed by a release, both inside the MouseRegion (pressing, moving outside the MouseRegion, and then moving back inside and releasing is also considered a click). - The x and y parameters tell you the position of the release of the click. The followsPressAndHold parameter tells - you whether or not the release portion of the click followed a long press. + + The \l {MouseEvent}{mouse} parameter provides information about the click, including the x and y + position of the release of the click, and whether the click wasHeld. */ /*! - \qmlsignal MouseRegion::onPressed + \qmlsignal MouseRegion::onPressed(mouse) This handler is called when there is a press. - The x and y parameters tell you the position of the press. + The \l {MouseEvent}{mouse} parameter provides information about the click, including the x and y + position of the release of the click, and whether the click wasHeld. */ /*! - \qmlsignal MouseRegion::onReleased + \qmlsignal MouseRegion::onReleased(mouse) This handler is called when there is a release. - The x and y parameters tell you the position of the release. The isClick parameter tells you whether - or not the release is part of a click. The followsPressAndHold parameter tells you whether or not the - release followed a long press. + The \l {MouseEvent}{mouse} parameter provides information about the click, including the x and y + position of the release of the click, and whether the click wasHeld. */ /*! - \qmlsignal MouseRegion::onPressAndHold + \qmlsignal MouseRegion::onPressAndHold(mouse) This handler is called when there is a long press (currently 800ms). - The x and y parameters tell you the position of the long press. + The \l {MouseEvent}{mouse} parameter provides information about the click, including the x and y + position of the release of the click, and whether the click wasHeld. */ /*! - \qmlsignal MouseRegion::onDoubleClicked + \qmlsignal MouseRegion::onDoubleClicked(mouse) This handler is called when there is a double-click (a press followed by a release followed by a press). - The x and y parameters tell you the position of the double-click. + The \l {MouseEvent}{mouse} parameter provides information about the click, including the x and y + position of the release of the click, and whether the click wasHeld. */ QML_DEFINE_TYPE(QFxMouseRegion,MouseRegion); + /*! \internal \class QFxMouseRegion @@ -330,7 +338,7 @@ void QFxMouseRegion::mousePressEvent(QGraphicsSceneMouseEvent *event) emit hoveredChanged(); } d->longPress = false; - d->lastPos = event->pos(); + d->saveEvent(event); d->dragX = drag()->axis().contains(QLatin1String("x")); d->dragY = drag()->axis().contains(QLatin1String("y")); d->dragged = false; @@ -354,7 +362,7 @@ void QFxMouseRegion::mouseMoveEvent(QGraphicsSceneMouseEvent *event) return; } - d->lastPos = event->pos(); + d->saveEvent(event); // ### we should skip this if these signals aren't used const QRect &bounds = itemBoundingRect(); @@ -426,6 +434,7 @@ void QFxMouseRegion::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) if(!d->absorb) QFxItem::mouseReleaseEvent(event); else { + d->saveEvent(event); setPressed(false); //d->inside = false; //emit hoveredChanged(); @@ -441,8 +450,10 @@ void QFxMouseRegion::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) else { //d->inside = true; //emit hoveredChanged(); + d->saveEvent(event); setPressed(true); - emit this->doubleClicked(d->lastPos.x(), d->lastPos.y()); + QFxMouseEvent me(d->lastPos.x(), d->lastPos.y(), d->lastButton, d->lastButtons, d->lastModifiers, true, false); + emit this->doubleClicked(&me); event->accept(); } } @@ -490,7 +501,8 @@ void QFxMouseRegion::timerEvent(QTimerEvent *event) d->pressAndHoldTimer.stop(); if (d->pressed && d->dragged == false && d->inside == true) { d->longPress = true; - emit pressAndHold(d->lastPos.x(), d->lastPos.y()); + QFxMouseEvent me(d->lastPos.x(), d->lastPos.y(), d->lastButton, d->lastButtons, d->lastModifiers, false, d->longPress); + emit pressAndHold(&me); } } } @@ -533,12 +545,13 @@ void QFxMouseRegion::setPressed(bool p) if(d->pressed != p) { d->pressed = p; + QFxMouseEvent me(d->lastPos.x(), d->lastPos.y(), d->lastButton, d->lastButtons, d->lastModifiers, isclick, d->longPress); if(d->pressed) - emit pressed(d->lastPos.x(), d->lastPos.y()); + emit pressed(&me); else { - emit released(d->lastPos.x(), d->lastPos.y(), isclick, d->longPress); + emit released(&me); if (isclick) - emit clicked(d->lastPos.x(), d->lastPos.y(), d->longPress); + emit clicked(&me); } emit pressedChanged(); diff --git a/src/declarative/fx/qfxmouseregion.h b/src/declarative/fx/qfxmouseregion.h index ee8b577..2e2a1d4 100644 --- a/src/declarative/fx/qfxmouseregion.h +++ b/src/declarative/fx/qfxmouseregion.h @@ -88,6 +88,7 @@ private: }; QML_DECLARE_TYPE(QFxDrag); +class QFxMouseEvent; class QFxMouseRegionPrivate; class Q_DECLARATIVE_EXPORT QFxMouseRegion : public QFxItem { @@ -122,11 +123,11 @@ Q_SIGNALS: void pressedChanged(); void positionChanged(); - void pressed(int x, int y); - void pressAndHold(int x, int y); - void released(int x, int y, bool isClick, bool followsPressAndHold); - void clicked(int x, int y, bool followsPressAndHold); - void doubleClicked(int x, int y); + void pressed(QFxMouseEvent *mouse); + void pressAndHold(QFxMouseEvent *mouse); + void released(QFxMouseEvent *mouse); + void clicked(QFxMouseEvent *mouse); + void doubleClicked(QFxMouseEvent *mouse); void entered(); void exited(); void exitedWhilePressed(); diff --git a/src/declarative/fx/qfxmouseregion_p.h b/src/declarative/fx/qfxmouseregion_p.h index 09e1b98..e9d1986 100644 --- a/src/declarative/fx/qfxmouseregion_p.h +++ b/src/declarative/fx/qfxmouseregion_p.h @@ -55,6 +55,7 @@ #include "qdatetime.h" #include "qbasictimer.h" +#include "qgraphicssceneevent.h" #include "qfxitem_p.h" QT_BEGIN_NAMESPACE @@ -78,6 +79,13 @@ public: void bindButtonValue(Qt::MouseButton); + void saveEvent(QGraphicsSceneMouseEvent *event) { + lastPos = event->pos(); + lastButton = event->button(); + lastButtons = event->buttons(); + lastModifiers = event->modifiers(); + } + bool absorb; bool hovered; bool inside; @@ -93,6 +101,9 @@ public: int startX; int startY; QPointF lastPos; + Qt::MouseButton lastButton; + Qt::MouseButtons lastButtons; + Qt::KeyboardModifiers lastModifiers; QBasicTimer pressAndHoldTimer; }; -- cgit v0.12