diff options
author | Leonardo Sobral Cunha <leo.cunha@nokia.com> | 2010-04-20 00:56:04 (GMT) |
---|---|---|
committer | Leonardo Sobral Cunha <leo.cunha@nokia.com> | 2010-04-20 01:11:13 (GMT) |
commit | fca7ddd5522f1462192b6c2b4b9e9e5a0f8449d9 (patch) | |
tree | 61c30b5562b53fa5997c05abfb66c248e5082482 | |
parent | 8efa7be3fd66119753730be643d0882afe329348 (diff) | |
download | Qt-fca7ddd5522f1462192b6c2b4b9e9e5a0f8449d9.zip Qt-fca7ddd5522f1462192b6c2b4b9e9e5a0f8449d9.tar.gz Qt-fca7ddd5522f1462192b6c2b4b9e9e5a0f8449d9.tar.bz2 |
Add drag.active property to MouseArea in qml
The drag.active property specifies if the target item is being dragged.
Task-number: QTBUG-9833
Reviewed-by: Martin Jones
5 files changed, 120 insertions, 9 deletions
diff --git a/src/declarative/graphicsitems/qdeclarativemousearea.cpp b/src/declarative/graphicsitems/qdeclarativemousearea.cpp index bdb4868..969c60e 100644 --- a/src/declarative/graphicsitems/qdeclarativemousearea.cpp +++ b/src/declarative/graphicsitems/qdeclarativemousearea.cpp @@ -50,7 +50,8 @@ QT_BEGIN_NAMESPACE static const int PressAndHoldDelay = 800; QDeclarativeDrag::QDeclarativeDrag(QObject *parent) -: QObject(parent), _target(0), _axis(XandYAxis), _xmin(0), _xmax(0), _ymin(0), _ymax(0) +: QObject(parent), _target(0), _axis(XandYAxis), _xmin(0), _xmax(0), _ymin(0), _ymax(0), +_active(false) { } @@ -144,6 +145,19 @@ void QDeclarativeDrag::setYmax(qreal m) emit maximumYChanged(); } +bool QDeclarativeDrag::active() const +{ + return _active; +} + +void QDeclarativeDrag::setActive(bool drag) +{ + if (_active == drag) + return; + _active = drag; + emit activeChanged(); +} + QDeclarativeMouseAreaPrivate::~QDeclarativeMouseAreaPrivate() { delete drag; @@ -389,7 +403,8 @@ void QDeclarativeMouseArea::mousePressEvent(QGraphicsSceneMouseEvent *event) d->dragX = drag()->axis() & QDeclarativeDrag::XAxis; d->dragY = drag()->axis() & QDeclarativeDrag::YAxis; } - d->dragged = false; + if (d->drag) + d->drag->setActive(false); setHovered(true); d->startScene = event->scenePos(); // we should only start timer if pressAndHold is connected to. @@ -438,7 +453,7 @@ void QDeclarativeMouseArea::mouseMoveEvent(QGraphicsSceneMouseEvent *event) qreal dx = qAbs(curLocalPos.x() - startLocalPos.x()); qreal dy = qAbs(curLocalPos.y() - startLocalPos.y()); if ((d->dragX && !(dx < dragThreshold)) || (d->dragY && !(dy < dragThreshold))) - d->dragged = true; + d->drag->setActive(true); if (!keepMouseGrab()) { if ((!d->dragY && dy < dragThreshold && d->dragX && dx > dragThreshold) || (!d->dragX && dx < dragThreshold && d->dragY && dy > dragThreshold) @@ -447,7 +462,7 @@ void QDeclarativeMouseArea::mouseMoveEvent(QGraphicsSceneMouseEvent *event) } } - if (d->dragX && d->dragged) { + if (d->dragX && d->drag->active()) { qreal x = (curLocalPos.x() - startLocalPos.x()) + d->startX; if (x < drag()->xmin()) x = drag()->xmin(); @@ -455,7 +470,7 @@ void QDeclarativeMouseArea::mouseMoveEvent(QGraphicsSceneMouseEvent *event) x = drag()->xmax(); drag()->target()->setX(x); } - if (d->dragY && d->dragged) { + if (d->dragY && d->drag->active()) { qreal y = (curLocalPos.y() - startLocalPos.y()) + d->startY; if (y < drag()->ymin()) y = drag()->ymin(); @@ -481,6 +496,8 @@ void QDeclarativeMouseArea::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) } else { d->saveEvent(event); setPressed(false); + if (d->drag) + d->drag->setActive(false); // If we don't accept hover, we need to reset containsMouse. if (!acceptHoverEvents()) setHovered(false); @@ -559,7 +576,8 @@ void QDeclarativeMouseArea::timerEvent(QTimerEvent *event) Q_D(QDeclarativeMouseArea); if (event->timerId() == d->pressAndHoldTimer.timerId()) { d->pressAndHoldTimer.stop(); - if (d->pressed && d->dragged == false && d->hovered == true) { + bool dragged = d->drag && d->drag->active(); + if (d->pressed && dragged == false && d->hovered == true) { d->longPress = true; QDeclarativeMouseEvent me(d->lastPos.x(), d->lastPos.y(), d->lastButton, d->lastButtons, d->lastModifiers, false, d->longPress); emit pressAndHold(&me); @@ -659,7 +677,8 @@ void QDeclarativeMouseArea::setAcceptedButtons(Qt::MouseButtons buttons) bool QDeclarativeMouseArea::setPressed(bool p) { Q_D(QDeclarativeMouseArea); - bool isclick = d->pressed == true && p == false && d->dragged == false && d->hovered == true; + bool dragged = d->drag && d->drag->active(); + bool isclick = d->pressed == true && p == false && dragged == false && d->hovered == true; if (d->pressed != p) { d->pressed = p; @@ -693,6 +712,7 @@ QDeclarativeDrag *QDeclarativeMouseArea::drag() /*! \qmlproperty Item MouseArea::drag.target + \qmlproperty bool MouseArea::drag.active \qmlproperty Axis MouseArea::drag.axis \qmlproperty real MouseArea::drag.minimumX \qmlproperty real MouseArea::drag.maximumX @@ -703,6 +723,7 @@ QDeclarativeDrag *QDeclarativeMouseArea::drag() \list \i \c target specifies the item to drag. + \i \c active specifies if the target item is being currently dragged. \i \c axis specifies whether dragging can be done horizontally (XAxis), vertically (YAxis), or both (XandYAxis) \i the minimum and maximum properties limit how far the target can be dragged along the corresponding axes. \endlist diff --git a/src/declarative/graphicsitems/qdeclarativemousearea_p.h b/src/declarative/graphicsitems/qdeclarativemousearea_p.h index 630840f..4f7df62 100644 --- a/src/declarative/graphicsitems/qdeclarativemousearea_p.h +++ b/src/declarative/graphicsitems/qdeclarativemousearea_p.h @@ -61,6 +61,7 @@ class Q_DECLARATIVE_EXPORT QDeclarativeDrag : public QObject Q_PROPERTY(qreal maximumX READ xmax WRITE setXmax NOTIFY maximumXChanged) Q_PROPERTY(qreal minimumY READ ymin WRITE setYmin NOTIFY minimumYChanged) Q_PROPERTY(qreal maximumY READ ymax WRITE setYmax NOTIFY maximumYChanged) + Q_PROPERTY(bool active READ active NOTIFY activeChanged) //### consider drag and drop public: @@ -84,6 +85,9 @@ public: qreal ymax() const; void setYmax(qreal); + bool active() const; + void setActive(bool); + Q_SIGNALS: void targetChanged(); void axisChanged(); @@ -91,6 +95,7 @@ Q_SIGNALS: void maximumXChanged(); void minimumYChanged(); void maximumYChanged(); + void activeChanged(); private: QGraphicsObject *_target; @@ -99,6 +104,7 @@ private: qreal _xmax; qreal _ymin; qreal _ymax; + bool _active; Q_DISABLE_COPY(QDeclarativeDrag) }; diff --git a/src/declarative/graphicsitems/qdeclarativemousearea_p_p.h b/src/declarative/graphicsitems/qdeclarativemousearea_p_p.h index 4973957..4e909ff 100644 --- a/src/declarative/graphicsitems/qdeclarativemousearea_p_p.h +++ b/src/declarative/graphicsitems/qdeclarativemousearea_p_p.h @@ -67,7 +67,8 @@ class QDeclarativeMouseAreaPrivate : public QDeclarativeItemPrivate public: QDeclarativeMouseAreaPrivate() - : absorb(true), hovered(false), pressed(false), longPress(false), drag(0) + : absorb(true), hovered(false), pressed(false), longPress(false), + moved(false), drag(0) { } @@ -100,7 +101,6 @@ public: bool moved : 1; bool dragX : 1; bool dragY : 1; - bool dragged : 1; QDeclarativeDrag *drag; QPointF startScene; qreal startX; diff --git a/tests/auto/declarative/qdeclarativemousearea/data/dragging.qml b/tests/auto/declarative/qdeclarativemousearea/data/dragging.qml new file mode 100644 index 0000000..a28f049 --- /dev/null +++ b/tests/auto/declarative/qdeclarativemousearea/data/dragging.qml @@ -0,0 +1,28 @@ +import Qt 4.7 +Rectangle { + id: whiteRect + width: 200 + height: 200 + color: "white" + Rectangle { + id: blackRect + objectName: "blackrect" + color: "black" + y: 50 + x: 50 + width: 100 + height: 100 + opacity: (whiteRect.width-blackRect.x+whiteRect.height-blackRect.y-199)/200 + Text { text: blackRect.opacity} + MouseArea { + objectName: "mouseregion" + anchors.fill: parent + drag.target: blackRect + drag.axis: Drag.XandYAxis + drag.minimumX: 0 + drag.maximumX: whiteRect.width-blackRect.width + drag.minimumY: 0 + drag.maximumY: whiteRect.height-blackRect.height + } + } + } diff --git a/tests/auto/declarative/qdeclarativemousearea/tst_qdeclarativemousearea.cpp b/tests/auto/declarative/qdeclarativemousearea/tst_qdeclarativemousearea.cpp index 4a58049..eb4aa12 100644 --- a/tests/auto/declarative/qdeclarativemousearea/tst_qdeclarativemousearea.cpp +++ b/tests/auto/declarative/qdeclarativemousearea/tst_qdeclarativemousearea.cpp @@ -52,6 +52,7 @@ class tst_QDeclarativeMouseArea: public QObject private slots: void dragProperties(); void resetDrag(); + void dragging(); void updateMouseAreaPosOnClick(); void updateMouseAreaPosOnResize(); void noOnClickedWithPressAndHold(); @@ -163,6 +164,61 @@ void tst_QDeclarativeMouseArea::resetDrag() } +void tst_QDeclarativeMouseArea::dragging() +{ + QDeclarativeView *canvas = createView(); + + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/dragging.qml")); + canvas->show(); + canvas->setFocus(); + QVERIFY(canvas->rootObject() != 0); + + QDeclarativeMouseArea *mouseRegion = canvas->rootObject()->findChild<QDeclarativeMouseArea*>("mouseregion"); + QDeclarativeDrag *drag = mouseRegion->drag(); + QVERIFY(mouseRegion != 0); + QVERIFY(drag != 0); + + // target + QDeclarativeItem *blackRect = canvas->rootObject()->findChild<QDeclarativeItem*>("blackrect"); + QVERIFY(blackRect != 0); + QVERIFY(blackRect == drag->target()); + + QVERIFY(!drag->active()); + + QGraphicsScene *scene = canvas->scene(); + QGraphicsSceneMouseEvent pressEvent(QEvent::GraphicsSceneMousePress); + pressEvent.setScenePos(QPointF(100, 100)); + pressEvent.setButton(Qt::LeftButton); + pressEvent.setButtons(Qt::LeftButton); + QApplication::sendEvent(scene, &pressEvent); + + QVERIFY(!drag->active()); + QCOMPARE(blackRect->x(), 50.0); + QCOMPARE(blackRect->y(), 50.0); + + QGraphicsSceneMouseEvent moveEvent(QEvent::GraphicsSceneMouseMove); + moveEvent.setScenePos(QPointF(110, 110)); + moveEvent.setButton(Qt::LeftButton); + moveEvent.setButtons(Qt::LeftButton); + QApplication::sendEvent(scene, &moveEvent); + + QVERIFY(drag->active()); + QCOMPARE(blackRect->x(), 60.0); + QCOMPARE(blackRect->y(), 60.0); + + QGraphicsSceneMouseEvent releaseEvent(QEvent::GraphicsSceneMouseRelease); + releaseEvent.setScenePos(QPointF(110, 110)); + releaseEvent.setButton(Qt::LeftButton); + releaseEvent.setButtons(Qt::LeftButton); + QApplication::sendEvent(scene, &releaseEvent); + + QVERIFY(!drag->active()); + QCOMPARE(blackRect->x(), 60.0); + QCOMPARE(blackRect->y(), 60.0); + + delete canvas; +} + QDeclarativeView *tst_QDeclarativeMouseArea::createView() { QDeclarativeView *canvas = new QDeclarativeView(0); |