diff options
-rw-r--r-- | src/quicktemplates2/qquickdrawer.cpp | 40 | ||||
-rw-r--r-- | src/quicktemplates2/qquickdrawer_p.h | 2 | ||||
-rw-r--r-- | src/quicktemplates2/qquickoverlay.cpp | 144 | ||||
-rw-r--r-- | src/quicktemplates2/qquickoverlay_p.h | 4 | ||||
-rw-r--r-- | src/quicktemplates2/qquickoverlay_p_p.h | 2 | ||||
-rw-r--r-- | src/quicktemplates2/qquickpopup.cpp | 6 | ||||
-rw-r--r-- | tests/auto/controls/data/tst_popup.qml | 72 | ||||
-rw-r--r-- | tests/auto/drawer/data/multiple.qml | 85 | ||||
-rw-r--r-- | tests/auto/drawer/data/window.qml | 56 | ||||
-rw-r--r-- | tests/auto/drawer/tst_drawer.cpp | 181 |
10 files changed, 449 insertions, 143 deletions
diff --git a/src/quicktemplates2/qquickdrawer.cpp b/src/quicktemplates2/qquickdrawer.cpp index 551352de..44fc8f50 100644 --- a/src/quicktemplates2/qquickdrawer.cpp +++ b/src/quicktemplates2/qquickdrawer.cpp @@ -191,6 +191,12 @@ bool QQuickDrawerPrivate::startDrag(QQuickWindow *window, QMouseEvent *event) break; } + if (drag) { + prepareEnterTransition(); + reposition(); + handleMousePressEvent(window->contentItem(), event); + } + return drag; } @@ -289,21 +295,12 @@ bool QQuickDrawerPrivate::ungrabMouse(QMouseEvent *event) bool QQuickDrawerPrivate::handleMousePressEvent(QQuickItem *item, QMouseEvent *event) { - pressPoint = event->windowPos(); offset = 0; - - if (qFuzzyIsNull(position)) { - // only accept pressing at drag margins when fully closed - event->setAccepted(startDrag(item->window(), event)); - } else { - if (modal) - event->setAccepted(item->isAncestorOf(popupItem)); - else - event->setAccepted(false); - } - + pressPoint = event->windowPos(); velocityCalculator.startMeasuring(pressPoint, event->timestamp()); + // don't block press events a) outside a non-modal drawer, or b) to drawer children + event->setAccepted(modal && !popupItem->isAncestorOf(item)); return event->isAccepted(); } @@ -384,7 +381,7 @@ void QQuickDrawerPrivate::finalizeEnterTransition() void QQuickDrawerPrivate::finalizeExitTransition(bool hide) { - QQuickPopupPrivate::finalizeExitTransition(hide = false); + QQuickPopupPrivate::finalizeExitTransition(hide); } QQuickDrawer::QQuickDrawer(QObject *parent) : @@ -448,12 +445,8 @@ void QQuickDrawer::setPosition(qreal position) d->position = position; if (isComponentComplete()) d->reposition(); - if (d->dimmer) { + if (d->dimmer) d->dimmer->setOpacity(position); - // TODO: check QStyleHints::useHoverEffects in Qt 5.8 - d->dimmer->setAcceptHoverEvents(d->modal && position > 0.0); - // d->dimmer->setAcceptHoverEvents(d->modal && position > 0.0 && QGuiApplication::styleHints()->useHoverEffects()); - } emit positionChanged(); } @@ -548,15 +541,4 @@ bool QQuickDrawer::overlayEvent(QQuickItem *item, QEvent *event) } } -void QQuickDrawer::componentComplete() -{ - Q_D(QQuickDrawer); - QQuickPopup::componentComplete(); - if (d->window) { - bool notify = false; - d->prepareEnterTransition(notify); - d->reposition(); - } -} - QT_END_NAMESPACE diff --git a/src/quicktemplates2/qquickdrawer_p.h b/src/quicktemplates2/qquickdrawer_p.h index 23c5c2ab..e694e27b 100644 --- a/src/quicktemplates2/qquickdrawer_p.h +++ b/src/quicktemplates2/qquickdrawer_p.h @@ -87,8 +87,6 @@ protected: void mouseUngrabEvent() override; bool overlayEvent(QQuickItem *item, QEvent *event) override; - void componentComplete() override; - private: Q_DISABLE_COPY(QQuickDrawer) Q_DECLARE_PRIVATE(QQuickDrawer) diff --git a/src/quicktemplates2/qquickoverlay.cpp b/src/quicktemplates2/qquickoverlay.cpp index d34fd7fc..19677cb7 100644 --- a/src/quicktemplates2/qquickoverlay.cpp +++ b/src/quicktemplates2/qquickoverlay.cpp @@ -37,11 +37,12 @@ #include "qquickoverlay_p.h" #include "qquickoverlay_p_p.h" #include "qquickpopup_p_p.h" -#include "qquickdrawer_p.h" +#include "qquickdrawer_p_p.h" #include "qquickapplicationwindow_p.h" #include <QtQml/qqmlinfo.h> #include <QtQml/qqmlproperty.h> #include <QtQml/qqmlcomponent.h> +#include <algorithm> QT_BEGIN_NAMESPACE @@ -93,7 +94,8 @@ static QQuickItem *createDimmer(QQmlComponent *component, QQuickPopup *popup, QQ item->setParentItem(parent); item->stackBefore(popup->popupItem()); item->setZ(popup->z()); - if (popup->isModal() && !qobject_cast<QQuickDrawer *>(popup)) { + if (popup->isModal()) { + item->setAcceptedMouseButtons(Qt::AllButtons); // TODO: switch to QStyleHints::useHoverEffects in Qt 5.8 item->setAcceptHoverEvents(true); // item->setAcceptHoverEvents(QGuiApplication::styleHints()->useHoverEffects()); @@ -162,6 +164,15 @@ QVector<QQuickPopup *> QQuickOverlayPrivate::stackingOrderPopups() const return popups; } +QVector<QQuickDrawer *> QQuickOverlayPrivate::stackingOrderDrawers() const +{ + QVector<QQuickDrawer *> sorted(allDrawers); + std::sort(sorted.begin(), sorted.end(), [](const QQuickDrawer *one, const QQuickDrawer *another) { + return one->z() > another->z(); + }); + return sorted; +} + void QQuickOverlayPrivate::itemGeometryChanged(QQuickItem *, const QRectF &newGeometry, const QRectF &) { Q_Q(QQuickOverlay); @@ -170,22 +181,26 @@ void QQuickOverlayPrivate::itemGeometryChanged(QQuickItem *, const QRectF &newGe QQuickOverlayPrivate::QQuickOverlayPrivate() : modal(nullptr), - modeless(nullptr), - modalPopups(0) + modeless(nullptr) { } void QQuickOverlayPrivate::addPopup(QQuickPopup *popup) { + Q_Q(QQuickOverlay); allPopups += popup; - if (QQuickDrawer *drawer = qobject_cast<QQuickDrawer *>(popup)) + if (QQuickDrawer *drawer = qobject_cast<QQuickDrawer *>(popup)) { allDrawers += drawer; + q->setVisible(!allDrawers.isEmpty() || !q->childItems().isEmpty()); + } } void QQuickOverlayPrivate::removePopup(QQuickPopup *popup) { + Q_Q(QQuickOverlay); allPopups.removeOne(popup); - allDrawers.removeOne(static_cast<QQuickDrawer *>(popup)); + if (allDrawers.removeOne(static_cast<QQuickDrawer *>(popup))) + q->setVisible(!allDrawers.isEmpty() || !q->childItems().isEmpty()); } QQuickOverlay::QQuickOverlay(QQuickItem *parent) @@ -275,7 +290,7 @@ void QQuickOverlay::itemChange(ItemChange change, const ItemChangeData &data) QQuickPopup *popup = nullptr; if (change == ItemChildAddedChange || change == ItemChildRemovedChange) { popup = qobject_cast<QQuickPopup *>(data.item->parent()); - setVisible(!childItems().isEmpty()); + setVisible(!d->allDrawers.isEmpty() || !childItems().isEmpty()); } if (!popup) return; @@ -286,8 +301,6 @@ void QQuickOverlay::itemChange(ItemChange change, const ItemChangeData &data) QObjectPrivate::connect(popup, &QQuickPopup::dimChanged, d, &QQuickOverlayPrivate::toggleOverlay); QObjectPrivate::connect(popup, &QQuickPopup::modalChanged, d, &QQuickOverlayPrivate::toggleOverlay); if (!qobject_cast<QQuickDrawer *>(popup)) { - if (popup->isModal()) - ++d->modalPopups; QObjectPrivate::connect(popup, &QQuickPopup::aboutToShow, d, &QQuickOverlayPrivate::popupAboutToShow); QObjectPrivate::connect(popup, &QQuickPopup::aboutToHide, d, &QQuickOverlayPrivate::popupAboutToHide); } @@ -296,8 +309,6 @@ void QQuickOverlay::itemChange(ItemChange change, const ItemChangeData &data) QObjectPrivate::disconnect(popup, &QQuickPopup::dimChanged, d, &QQuickOverlayPrivate::toggleOverlay); QObjectPrivate::disconnect(popup, &QQuickPopup::modalChanged, d, &QQuickOverlayPrivate::toggleOverlay); if (!qobject_cast<QQuickDrawer *>(popup)) { - if (popup->isModal()) - --d->modalPopups; QObjectPrivate::disconnect(popup, &QQuickPopup::aboutToShow, d, &QQuickOverlayPrivate::popupAboutToShow); QObjectPrivate::disconnect(popup, &QQuickPopup::aboutToHide, d, &QQuickOverlayPrivate::popupAboutToHide); } @@ -312,75 +323,82 @@ void QQuickOverlay::geometryChanged(const QRectF &newGeometry, const QRectF &old d->resizeOverlay(popup); } -bool QQuickOverlay::event(QEvent *event) +void QQuickOverlay::mousePressEvent(QMouseEvent *event) { Q_D(QQuickOverlay); - switch (event->type()) { - case QEvent::MouseButtonPress: { - emit pressed(); + emit pressed(); + + if (!d->allDrawers.isEmpty()) { + // the overlay background was pressed, so there are no modal popups open. + // test if the press point lands on any drawer's drag margin + + const QVector<QQuickDrawer *> drawers = d->stackingOrderDrawers(); + for (QQuickDrawer *drawer : drawers) { + QQuickDrawerPrivate *p = QQuickDrawerPrivate::get(drawer); + if (p->startDrag(window(), event)) { + d->mouseGrabberPopup = drawer; + return; + } + } + } + + if (!d->mouseGrabberPopup) { const auto popups = d->stackingOrderPopups(); for (QQuickPopup *popup : popups) { if (popup->overlayEvent(this, event)) { d->mouseGrabberPopup = popup; - return true; + return; } } - break; } - case QEvent::MouseMove: - if (d->mouseGrabberPopup) { - if (d->mouseGrabberPopup->overlayEvent(this, event)) - return true; - } else { - const auto popups = d->stackingOrderPopups(); - for (QQuickPopup *popup : popups) { - if (popup->overlayEvent(this, event)) - return true; - } - } - break; - case QEvent::MouseButtonRelease: - emit released(); - if (d->mouseGrabberPopup) { - QQuickPopup *grabber = d->mouseGrabberPopup; - d->mouseGrabberPopup = nullptr; - if (grabber->overlayEvent(this, event)) - return true; - } else { - const auto popups = d->stackingOrderPopups(); - for (QQuickPopup *popup : popups) { - if (popup->overlayEvent(this, event)) - return true; - } + + event->ignore(); +} + +void QQuickOverlay::mouseMoveEvent(QMouseEvent *event) +{ + Q_D(QQuickOverlay); + if (d->mouseGrabberPopup) + d->mouseGrabberPopup->overlayEvent(this, event); +} + +void QQuickOverlay::mouseReleaseEvent(QMouseEvent *event) +{ + Q_D(QQuickOverlay); + emit released(); + + if (d->mouseGrabberPopup) { + d->mouseGrabberPopup->overlayEvent(this, event); + d->mouseGrabberPopup = nullptr; + } else { + const auto popups = d->stackingOrderPopups(); + for (QQuickPopup *popup : popups) { + if (popup->overlayEvent(this, event)) + break; } - break; - default: - break; } - - return QQuickItem::event(event); } bool QQuickOverlay::childMouseEventFilter(QQuickItem *item, QEvent *event) { Q_D(QQuickOverlay); - if (d->modalPopups == 0) - return false; - // TODO Filter touch events - if (event->type() != QEvent::MouseButtonPress) - return false; - while (item->parentItem() != this) - item = item->parentItem(); - - const auto popups = d->stackingOrderPopups(); - for (QQuickPopup *popup : popups) { - if (popup->popupItem() == item) - break; - - if (popup->overlayEvent(item, event)) - return true; + for (QQuickPopup *popup : qAsConst(d->allPopups)) { + QQuickItem *dimmer = QQuickPopupPrivate::get(popup)->dimmer; + if (item == dimmer) { + switch (event->type()) { + case QEvent::MouseButtonPress: + emit pressed(); + return popup->overlayEvent(item, event); + case QEvent::MouseMove: + return popup->overlayEvent(item, event); + case QEvent::MouseButtonRelease: + emit released(); + return popup->overlayEvent(item, event); + default: + break; + } + } } - return false; } diff --git a/src/quicktemplates2/qquickoverlay_p.h b/src/quicktemplates2/qquickoverlay_p.h index ecb0e20a..07f7daec 100644 --- a/src/quicktemplates2/qquickoverlay_p.h +++ b/src/quicktemplates2/qquickoverlay_p.h @@ -84,7 +84,9 @@ protected: void itemChange(ItemChange change, const ItemChangeData &data) override; void geometryChanged(const QRectF &oldGeometry, const QRectF &newGeometry) override; - bool event(QEvent *event) override; + void mousePressEvent(QMouseEvent *event) override; + void mouseMoveEvent(QMouseEvent *event) override; + void mouseReleaseEvent(QMouseEvent *event) override; bool childMouseEventFilter(QQuickItem *item, QEvent *event) override; private: diff --git a/src/quicktemplates2/qquickoverlay_p_p.h b/src/quicktemplates2/qquickoverlay_p_p.h index f9e962ee..6dc2853c 100644 --- a/src/quicktemplates2/qquickoverlay_p_p.h +++ b/src/quicktemplates2/qquickoverlay_p_p.h @@ -82,6 +82,7 @@ public: void toggleOverlay(); QVector<QQuickPopup *> stackingOrderPopups() const; + QVector<QQuickDrawer *> stackingOrderDrawers() const; void itemGeometryChanged(QQuickItem *item, const QRectF &newGeometry, const QRectF &oldGeometry) override; @@ -90,7 +91,6 @@ public: QVector<QQuickPopup *> allPopups; QVector<QQuickDrawer *> allDrawers; QPointer<QQuickPopup> mouseGrabberPopup; - int modalPopups; }; QT_END_NAMESPACE diff --git a/src/quicktemplates2/qquickpopup.cpp b/src/quicktemplates2/qquickpopup.cpp index 6c184243..6d06708b 100644 --- a/src/quicktemplates2/qquickpopup.cpp +++ b/src/quicktemplates2/qquickpopup.cpp @@ -1898,6 +1898,12 @@ void QQuickPopup::mouseDoubleClickEvent(QMouseEvent *event) void QQuickPopup::mouseUngrabEvent() { + QQuickOverlay *overlay = QQuickOverlay::overlay(window()); + if (overlay) { + QQuickOverlayPrivate *p = QQuickOverlayPrivate::get(overlay); + if (p->mouseGrabberPopup == this) + p->mouseGrabberPopup = nullptr; + } } bool QQuickPopup::overlayEvent(QQuickItem *item, QEvent *event) diff --git a/tests/auto/controls/data/tst_popup.qml b/tests/auto/controls/data/tst_popup.qml index 2c6d60da..e714a7d0 100644 --- a/tests/auto/controls/data/tst_popup.qml +++ b/tests/auto/controls/data/tst_popup.qml @@ -971,18 +971,13 @@ TestCase { ApplicationWindow { property alias firstDrawer: firstDrawer property alias secondDrawer: secondDrawer - property alias upperDrawer: upperDrawer property alias modalPopup: modalPopup property alias modelessPopup: modelessPopup property alias plainPopup: plainPopup property alias modalPopupWithoutDim: modalPopupWithoutDim visible: true Drawer { - z: 5 - id: upperDrawer - } - Drawer { - z: 1 + z: 0 id: firstDrawer } Drawer { @@ -1038,52 +1033,41 @@ TestCase { window.requestActivate() tryCompare(window, "active", true) - var countBefore = window.overlay.children.length - compare(countBefore, 6) // 3 drawers + 3 overlays + compare(window.overlay.children.length, 0) var firstOverlay = findOverlay(window, window.firstDrawer) + verify(!firstOverlay) + window.firstDrawer.open() + compare(window.overlay.children.length, 2) // 1 drawer + 1 overlay + firstOverlay = findOverlay(window, window.firstDrawer) verify(firstOverlay) - compare(firstOverlay.opacity, 0.0) compare(firstOverlay.z, window.firstDrawer.z) compare(indexOf(window.overlay.children, firstOverlay), indexOf(window.overlay.children, window.firstDrawer.contentItem.parent) - 1) + tryCompare(firstOverlay, "opacity", 1.0) var secondOverlay = findOverlay(window, window.secondDrawer) + verify(!secondOverlay) + window.secondDrawer.open() + compare(window.overlay.children.length, 4) // 2 drawers + 2 overlays + secondOverlay = findOverlay(window, window.secondDrawer) verify(secondOverlay) - compare(secondOverlay.opacity, 0.0) compare(secondOverlay.z, window.secondDrawer.z) compare(indexOf(window.overlay.children, secondOverlay), indexOf(window.overlay.children, window.secondDrawer.contentItem.parent) - 1) + tryCompare(secondOverlay, "opacity", 1.0) - var upperOverlay = findOverlay(window, window.upperDrawer) - verify(upperOverlay) - compare(upperOverlay.opacity, 0.0) - compare(upperOverlay.z, window.upperDrawer.z) - compare(indexOf(window.overlay.children, upperOverlay), - indexOf(window.overlay.children, window.upperDrawer.contentItem.parent) - 1) - - window.firstDrawer.open() - compare(firstOverlay.z, 1.0) - tryCompare(firstOverlay, "opacity", 1.0) window.firstDrawer.close() - tryCompare(firstOverlay, "opacity", 0.0) tryCompare(window.firstDrawer, "visible", false) + firstOverlay = findOverlay(window, window.firstDrawer) + verify(!firstOverlay) + compare(window.overlay.children.length, 2) // 1 drawer + 1 overlay - window.secondDrawer.open() - compare(secondOverlay.z, 1.0) - tryCompare(secondOverlay, "opacity", 1.0) window.secondDrawer.close() - tryCompare(secondOverlay, "opacity", 0.0) tryCompare(window.secondDrawer, "visible", false) - - window.firstDrawer.open() - window.secondDrawer.open() - tryCompare(firstOverlay, "opacity", 1.0) - tryCompare(secondOverlay, "opacity", 1.0) - window.firstDrawer.close() - window.secondDrawer.close() - tryCompare(firstOverlay, "opacity", 0.0) - tryCompare(secondOverlay, "opacity", 0.0) + secondOverlay = findOverlay(window, window.secondDrawer) + verify(!secondOverlay) + compare(window.overlay.children.length, 0) var modalOverlay = findOverlay(window, window.modalPopup) verify(!modalOverlay) @@ -1093,7 +1077,7 @@ TestCase { compare(modalOverlay.z, window.modalPopup.z) compare(window.modalPopup.visible, true) tryCompare(modalOverlay, "opacity", 1.0) - compare(window.overlay.children.length, countBefore + 2) // 1 popup + 1 overlay + compare(window.overlay.children.length, 2) // 1 popup + 1 overlay var modelessOverlay = findOverlay(window, window.modelessPopup) verify(!modelessOverlay) @@ -1103,13 +1087,13 @@ TestCase { compare(modelessOverlay.z, window.modelessPopup.z) compare(window.modelessPopup.visible, true) tryCompare(modelessOverlay, "opacity", 1.0) - compare(window.overlay.children.length, countBefore + 4) // 2 popups + 2 overlays + compare(window.overlay.children.length, 4) // 2 popups + 2 overlays window.modelessPopup.close() tryCompare(window.modelessPopup, "visible", false) modelessOverlay = findOverlay(window, window.modelessPopup) verify(!modelessOverlay) - compare(window.overlay.children.length, countBefore + 2) // 1 popup + 1 overlay + compare(window.overlay.children.length, 2) // 1 popup + 1 overlay compare(window.modalPopup.visible, true) compare(modalOverlay.opacity, 1.0) @@ -1118,29 +1102,29 @@ TestCase { tryCompare(window.modalPopup, "visible", false) modalOverlay = findOverlay(window, window.modalPopup) verify(!modalOverlay) - compare(window.overlay.children.length, countBefore) + compare(window.overlay.children.length, 0) window.plainPopup.open() tryCompare(window.plainPopup, "visible", true) - compare(window.overlay.children.length, countBefore + 1) // only popup added, no overlays involved + compare(window.overlay.children.length, 1) // only popup added, no overlays involved window.plainPopup.modal = true - compare(window.overlay.children.length, countBefore + 2) // overlay added + compare(window.overlay.children.length, 2) // overlay added window.plainPopup.close() tryCompare(window.plainPopup, "visible", false) - compare(window.overlay.children.length, countBefore) // popup + overlay removed + compare(window.overlay.children.length, 0) // popup + overlay removed window.modalPopupWithoutDim.open() tryCompare(window.modalPopupWithoutDim, "visible", true) - compare(window.overlay.children.length, countBefore + 1) // only popup added, no overlays involved + compare(window.overlay.children.length, 1) // only popup added, no overlays involved window.modalPopupWithoutDim.dim = true - compare(window.overlay.children.length, countBefore + 2) // overlay added + compare(window.overlay.children.length, 2) // overlay added window.modalPopupWithoutDim.close() tryCompare(window.modalPopupWithoutDim, "visible", false) - compare(window.overlay.children.length, countBefore) // popup + overlay removed + compare(window.overlay.children.length, 0) // popup + overlay removed window.destroy() } diff --git a/tests/auto/drawer/data/multiple.qml b/tests/auto/drawer/data/multiple.qml new file mode 100644 index 00000000..b2ef9781 --- /dev/null +++ b/tests/auto/drawer/data/multiple.qml @@ -0,0 +1,85 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.6 +import QtQuick.Controls 2.0 + +ApplicationWindow { + width: 400 + height: 400 + + property alias leftDrawer: leftDrawer + property alias leftButton: leftButton + + property alias rightDrawer: rightDrawer + property alias rightButton: rightButton + + property alias contentButton: contentButton + + Drawer { + id: leftDrawer + width: 300 + height: 400 + z: 1 + + contentItem: Button { + id: leftButton + text: "Left" + } + } + + Button { + id: contentButton + text: "Content" + anchors.fill: parent + } + + Drawer { + id: rightDrawer + width: 300 + height: 400 + edge: Qt.RightEdge + + contentItem: Button { + id: rightButton + text: "Right" + } + } +} diff --git a/tests/auto/drawer/data/window.qml b/tests/auto/drawer/data/window.qml new file mode 100644 index 00000000..47ef5cb0 --- /dev/null +++ b/tests/auto/drawer/data/window.qml @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** You may use this file under the terms of the BSD license as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.6 +import QtQuick.Window 2.2 +import QtQuick.Controls 2.0 + +Window { + width: 400 + height: 400 + + property alias drawer: drawer + + Drawer { + id: drawer + width: 200 + height: 200 + } +} diff --git a/tests/auto/drawer/tst_drawer.cpp b/tests/auto/drawer/tst_drawer.cpp index 43236817..02a4f91f 100644 --- a/tests/auto/drawer/tst_drawer.cpp +++ b/tests/auto/drawer/tst_drawer.cpp @@ -42,6 +42,7 @@ #include <QtGui/qstylehints.h> #include <QtGui/qguiapplication.h> #include <QtQuickTemplates2/private/qquickapplicationwindow_p.h> +#include <QtQuickTemplates2/private/qquickoverlay_p.h> #include <QtQuickTemplates2/private/qquickdrawer_p.h> #include <QtQuickTemplates2/private/qquickbutton_p.h> @@ -52,6 +53,9 @@ class tst_Drawer : public QQmlDataTest Q_OBJECT private slots: + void visible_data(); + void visible(); + void position_data(); void position(); @@ -62,8 +66,58 @@ private slots: void hover_data(); void hover(); + + void multiple(); }; +void tst_Drawer::visible_data() +{ + QTest::addColumn<QString>("source"); + QTest::newRow("Window") << "window.qml"; + QTest::newRow("ApplicationWindow") << "applicationwindow.qml"; +} + +void tst_Drawer::visible() +{ + QFETCH(QString, source); + QQuickApplicationHelper helper(this, source); + + QQuickWindow *window = helper.window; + window->show(); + QVERIFY(QTest::qWaitForWindowExposed(window)); + + QQuickDrawer *drawer = window->property("drawer").value<QQuickDrawer*>(); + QVERIFY(drawer); + QQuickItem *popupItem = drawer->popupItem(); + + QCOMPARE(drawer->isVisible(), false); + QCOMPARE(drawer->position(), qreal(0.0)); + + QQuickOverlay *overlay = QQuickOverlay::overlay(window); + QVERIFY(overlay); + QVERIFY(!overlay->childItems().contains(popupItem)); + + drawer->open(); + QVERIFY(drawer->isVisible()); + QVERIFY(overlay->childItems().contains(popupItem)); + QTRY_COMPARE(drawer->position(), qreal(1.0)); + + drawer->close(); + QTRY_VERIFY(!drawer->isVisible()); + QTRY_COMPARE(drawer->position(), qreal(0.0)); + QVERIFY(!overlay->childItems().contains(popupItem)); + + drawer->setVisible(true); + QVERIFY(drawer->isVisible()); + QVERIFY(overlay->childItems().contains(popupItem)); + QTRY_COMPARE(drawer->position(), qreal(1.0)); + + drawer->setVisible(false); + QTRY_VERIFY(!drawer->isVisible()); + QTRY_COMPARE(drawer->position(), qreal(0.0)); + QTRY_VERIFY(!overlay->childItems().contains(popupItem)); +} + void tst_Drawer::position_data() { QTest::addColumn<Qt::Edge>("edge"); @@ -179,10 +233,10 @@ void tst_Drawer::reposition() drawer->open(); QTRY_COMPARE(drawer->popupItem()->x(), window->width() - drawer->width()); - drawer->close(); - QTRY_COMPARE(drawer->popupItem()->x(), static_cast<qreal>(window->width())); - window->setWidth(window->width() + 100); + QTRY_COMPARE(drawer->popupItem()->x(), window->width() - drawer->width()); + + drawer->close(); QTRY_COMPARE(drawer->popupItem()->x(), static_cast<qreal>(window->width())); } @@ -250,6 +304,127 @@ void tst_Drawer::hover() QVERIFY(backgroundButton->isHovered()); } +void tst_Drawer::multiple() +{ + QQuickApplicationHelper helper(this, QStringLiteral("multiple.qml")); + QQuickWindow *window = helper.window; + window->show(); + QVERIFY(QTest::qWaitForWindowExposed(window)); + + QQuickDrawer *leftDrawer = window->property("leftDrawer").value<QQuickDrawer*>(); + QVERIFY(leftDrawer); + QQuickButton *leftButton = window->property("leftButton").value<QQuickButton*>(); + QVERIFY(leftButton); + QSignalSpy leftClickSpy(leftButton, SIGNAL(clicked())); + QVERIFY(leftClickSpy.isValid()); + + QQuickDrawer *rightDrawer = window->property("rightDrawer").value<QQuickDrawer*>(); + QVERIFY(rightDrawer); + QQuickButton *rightButton = window->property("rightButton").value<QQuickButton*>(); + QVERIFY(rightButton); + QSignalSpy rightClickSpy(rightButton, SIGNAL(clicked())); + QVERIFY(rightClickSpy.isValid()); + + QQuickButton *contentButton = window->property("contentButton").value<QQuickButton*>(); + QVERIFY(contentButton); + QSignalSpy contentClickSpy(contentButton, SIGNAL(clicked())); + QVERIFY(contentClickSpy.isValid()); + + // no drawers open, click the content + QTest::mouseClick(window, Qt::LeftButton); + QCOMPARE(contentClickSpy.count(), 1); + QCOMPARE(leftClickSpy.count(), 0); + QCOMPARE(rightClickSpy.count(), 0); + + // drag the left drawer open + QTest::mousePress(window, Qt::LeftButton, Qt::NoModifier, QPoint(0, window->height() / 2)); + QTest::mouseMove(window, QPoint(leftDrawer->width() / 2, window->height() / 2)); + QCOMPARE(leftDrawer->position(), 0.5); + QCOMPARE(rightDrawer->position(), 0.0); + QTest::mouseRelease(window, Qt::LeftButton, Qt::NoModifier, QPoint(leftDrawer->width() / 2, window->height() / 2)); + QTRY_COMPARE(leftDrawer->position(), 1.0); + QCOMPARE(rightDrawer->position(), 0.0); + + // cannot drag the right drawer while the left drawer is open + QTest::mousePress(window, Qt::LeftButton, Qt::NoModifier, QPoint(window->width() - 1, window->height() / 2)); + QTest::mouseMove(window, QPoint(window->width() - leftDrawer->width() / 2, window->height() / 2)); + QCOMPARE(leftDrawer->position(), 1.0); + QCOMPARE(rightDrawer->position(), 0.0); + QTest::mouseRelease(window, Qt::LeftButton, Qt::NoModifier, QPoint(window->width() - leftDrawer->width() / 2, window->height() / 2)); + QCOMPARE(rightDrawer->position(), 0.0); + QCOMPARE(leftDrawer->position(), 1.0); + + // open the right drawer below the left drawer + rightDrawer->open(); + QTRY_COMPARE(rightDrawer->position(), 1.0); + + // click the left drawer's button + QTest::mouseClick(window, Qt::LeftButton); + QCOMPARE(contentClickSpy.count(), 1); + QCOMPARE(leftClickSpy.count(), 1); + QCOMPARE(rightClickSpy.count(), 0); + + // click the left drawer's background (button disabled, don't leak through to the right drawer below) + leftButton->setEnabled(false); + QTest::mouseClick(window, Qt::LeftButton); + QCOMPARE(contentClickSpy.count(), 1); + QCOMPARE(leftClickSpy.count(), 1); + QCOMPARE(rightClickSpy.count(), 0); + leftButton->setEnabled(true); + + // click the overlay of the left drawer (don't leak through to right drawer below) + QTest::mouseClick(window, Qt::LeftButton, Qt::NoModifier, QPoint(window->width() - (window->width() - leftDrawer->width()) / 2, window->height() / 2)); + QCOMPARE(contentClickSpy.count(), 1); + QCOMPARE(leftClickSpy.count(), 1); + QCOMPARE(rightClickSpy.count(), 0); + QTRY_VERIFY(!leftDrawer->isVisible()); + + // click the right drawer's button + QTest::mouseClick(window, Qt::LeftButton); + QCOMPARE(contentClickSpy.count(), 1); + QCOMPARE(leftClickSpy.count(), 1); + QCOMPARE(rightClickSpy.count(), 1); + + // cannot drag the left drawer while the right drawer is open + QTest::mousePress(window, Qt::LeftButton, Qt::NoModifier, QPoint(0, window->height() / 2)); + QTest::mouseMove(window, QPoint(leftDrawer->width() / 2, window->height() / 2)); + QCOMPARE(leftDrawer->position(), 0.0); + QCOMPARE(rightDrawer->position(), 1.0); + QTest::mouseRelease(window, Qt::LeftButton, Qt::NoModifier, QPoint(leftDrawer->width() / 2, window->height() / 2)); + QCOMPARE(leftDrawer->position(), 0.0); + QCOMPARE(rightDrawer->position(), 1.0); + + // click the right drawer's background (button disabled, don't leak through to the content below) + rightButton->setEnabled(false); + QTest::mouseClick(window, Qt::LeftButton); + QCOMPARE(contentClickSpy.count(), 1); + QCOMPARE(leftClickSpy.count(), 1); + QCOMPARE(rightClickSpy.count(), 1); + rightButton->setEnabled(true); + + // click the overlay of the right drawer (don't leak through to the content below) + QTest::mouseClick(window, Qt::LeftButton, Qt::NoModifier, QPoint((window->width() - rightDrawer->width()) / 2, window->height() / 2)); + QCOMPARE(contentClickSpy.count(), 1); + QCOMPARE(leftClickSpy.count(), 1); + QCOMPARE(rightClickSpy.count(), 1); + QTRY_VERIFY(!rightDrawer->isVisible()); + + // no drawers open, click the content + QTest::mouseClick(window, Qt::LeftButton); + QCOMPARE(contentClickSpy.count(), 2); + QCOMPARE(leftClickSpy.count(), 1); + QCOMPARE(rightClickSpy.count(), 1); + + // drag the right drawer open + QTest::mousePress(window, Qt::LeftButton, Qt::NoModifier, QPoint(window->width() - 1, window->height() / 2)); + QTest::mouseMove(window, QPoint(window->width() - rightDrawer->width() / 2, window->height() / 2)); + QCOMPARE(rightDrawer->position(), 0.5); + QCOMPARE(leftDrawer->position(), 0.0); + QTest::mouseRelease(window, Qt::LeftButton, Qt::NoModifier, QPoint(window->width() - rightDrawer->width() / 2, window->height() / 2)); + QTRY_COMPARE(rightDrawer->position(), 1.0); + QCOMPARE(leftDrawer->position(), 0.0); +} + QTEST_MAIN(tst_Drawer) #include "tst_drawer.moc" |