diff options
-rw-r--r-- | examples/declarative/connections/connections.qml | 6 | ||||
-rw-r--r-- | examples/declarative/mouseregion/mouse.qml | 12 | ||||
-rw-r--r-- | src/declarative/fx/fx.pri | 2 | ||||
-rw-r--r-- | src/declarative/fx/qfxevents.cpp | 72 | ||||
-rw-r--r-- | src/declarative/fx/qfxevents_p.h | 127 | ||||
-rw-r--r-- | src/declarative/fx/qfxitem.cpp | 27 | ||||
-rw-r--r-- | src/declarative/fx/qfxitem.h | 5 | ||||
-rw-r--r-- | src/declarative/fx/qfxmouseregion.cpp | 59 | ||||
-rw-r--r-- | src/declarative/fx/qfxmouseregion.h | 11 | ||||
-rw-r--r-- | src/declarative/fx/qfxmouseregion_p.h | 11 |
10 files changed, 269 insertions, 63 deletions
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 @@ <Rect id="rect" color="blue" width="40" height="30"> <Rect id="dot" color="red" width="3" height="3" x="{rect.width/2}" y="{rect.height/2}"/> <MouseRegion id="mr" anchors.fill="{rect}"/> - <Connection sender="{mr}" signal="clicked(x,y)"> + <Connection sender="{mr}" signal="clicked(mouse)"> color="green"; - dot.x = x-1; - dot.y = y-1; + dot.x = mouse.x-1; + dot.y = mouse.y-1; </Connection> </Rect> 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 @@ <Rect color="white" width="200" height="200"> <Rect width="50" height="50" color="red"> <Text text="Click" anchors.centeredIn="{parent}"/> - <MouseRegion onPressed="print('press (x: ' + x + ' y: ' + y + ')')" - onReleased="print('release (x: ' + x + ' y: ' + y + ' isClick: ' + isClick + ' followsPressAndHold: ' + followsPressAndHold + ')')" - onClicked="print('click (x: ' + x + ' y: ' + y + ' followsPressAndHold: ' + followsPressAndHold + ')')" - onDoubleClicked="print('double click (x: ' + x + ' y: ' + y + ')')" + <MouseRegion onPressed="print('press (x: ' + mouse.x + ' y: ' + mouse.y + ')')" + onReleased="print('release (x: ' + mouse.x + ' y: ' + mouse.y + ' isClick: ' + mouse.isClick + ' wasHeld: ' + mouse.wasHeld + ')')" + onClicked="print('click (x: ' + mouse.x + ' y: ' + mouse.y + ' wasHeld: ' + mouse.wasHeld + ')')" + onDoubleClicked="print('double click (x: ' + mouse.x + ' y: ' + mouse.y + ')')" onPressAndHold="print('press and hold')" onExitedWhilePressed="print('exiting while pressed')" onReenteredWhilePressed="print('reentering while pressed')" anchors.fill="{parent}"/> @@ -14,8 +14,8 @@ <MouseRegion drag.target="{parent}" drag.axis="x" drag.xmin="0" drag.xmax="150" onPressed="print('press')" - onReleased="print('release (isClick: ' + isClick + ') (followsPressAndHold: ' + followsPressAndHold + ')')" - onClicked="print('click' + '(followsPressAndHold: ' + followsPressAndHold + ')')" + onReleased="print('release (isClick: ' + mouse.isClick + ') (wasHeld: ' + mouse.wasHeld + ')')" + onClicked="print('click' + '(wasHeld: ' + mouse.wasHeld + ')')" onDoubleClicked="print('double click')" onPressAndHold="print('press and hold')" onExitedWhilePressed="print('exiting while pressed')" diff --git a/src/declarative/fx/fx.pri b/src/declarative/fx/fx.pri index b8b62b9..7fe86fe 100644 --- a/src/declarative/fx/fx.pri +++ b/src/declarative/fx/fx.pri @@ -8,6 +8,7 @@ HEADERS += \ fx/qfxcomponentinstance_p.h \ fx/qfxcontentwrapper.h \ fx/qfxcontentwrapper_p.h \ + fx/qfxevents_p.h \ fx/qfxflickable.h \ fx/qfxflickable_p.h \ fx/qfxfocuspanel.h \ @@ -57,6 +58,7 @@ SOURCES += \ fx/qfxblurfilter.cpp \ fx/qfxcomponentinstance.cpp \ fx/qfxcontentwrapper.cpp \ + fx/qfxevents.cpp \ fx/qfxflickable.cpp \ fx/qfxfocuspanel.cpp \ fx/qfxfocusrealm.cpp \ diff --git a/src/declarative/fx/qfxevents.cpp b/src/declarative/fx/qfxevents.cpp new file mode 100644 index 0000000..a3fa6af --- /dev/null +++ b/src/declarative/fx/qfxevents.cpp @@ -0,0 +1,72 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qfxevents_p.h" + +QT_BEGIN_NAMESPACE + +/*! + \qmlclass MouseEvent QFxMouseEvent + \brief The MouseEvent element provides information about a mouse event. +*/ + +/*! + \internal + \class QFxMouseEvent +*/ + +/*! + \qmlproperty int x + \qmlproperty int y + + These properties hold the position of the mouse event. +*/ + +/*! + \qmlproperty enum button + + This property holds the button that caused the event. +*/ + +QML_DEFINE_NOCREATE_TYPE(QFxKeyEvent); +QML_DEFINE_NOCREATE_TYPE(QFxMouseEvent); + +QT_END_NAMESPACE diff --git a/src/declarative/fx/qfxevents_p.h b/src/declarative/fx/qfxevents_p.h new file mode 100644 index 0000000..d096a90 --- /dev/null +++ b/src/declarative/fx/qfxevents_p.h @@ -0,0 +1,127 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QFXEVENTS_P_H +#define QFXEVENTS_P_H + +#include <qfxglobal.h> +#include <qml.h> +#include <QtCore/qobject.h> +#include <QtGui/qevent.h> + +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 <qsimplecanvasfilter.h> #include <qmlcomponent.h> @@ -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 <QGraphicsSceneMouseEvent> @@ -154,12 +155,15 @@ void QFxDrag::setYmax(int m) </Rect> \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 <Rect width="100" height="100"> - <MouseRegion anchors.fill="{parent}" onClick="if(mouseButton=='Right') { parent.color='blue';} else { parent.color = 'red';}"/> + <MouseRegion anchors.fill="{parent}" onClick="if(mouse.button=='Right') { parent.color='blue';} else { parent.color = 'red';}"/> </Rect> \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; }; |