diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | examples/controls/gallery/gallery.qml | 6 | ||||
-rw-r--r-- | src/imports/controls/plugins.qmltypes | 10 | ||||
-rw-r--r-- | src/imports/templates/plugins.qmltypes | 10 | ||||
-rw-r--r-- | src/templates/qquickcombobox.cpp | 2 | ||||
-rw-r--r-- | src/templates/qquickmenu.cpp | 7 | ||||
-rw-r--r-- | src/templates/qquickoverlay.cpp | 18 | ||||
-rw-r--r-- | src/templates/qquickpopup.cpp | 38 | ||||
-rw-r--r-- | src/templates/qquickpopup_p.h | 16 | ||||
-rw-r--r-- | src/templates/qquickpopup_p_p.h | 1 | ||||
-rw-r--r-- | tests/auto/auto.pro | 1 | ||||
-rw-r--r-- | tests/auto/popup/data/applicationwindow.qml | 65 | ||||
-rw-r--r-- | tests/auto/popup/popup.pro | 14 | ||||
-rw-r--r-- | tests/auto/popup/tst_popup.cpp | 184 |
14 files changed, 360 insertions, 13 deletions
@@ -14,6 +14,7 @@ /tests/auto/controls/tst_controls /tests/auto/material/tst_material /tests/auto/menu/tst_menu +/tests/auto/popup/tst_popup /tests/auto/pressandhold/tst_pressandhold /tests/auto/sanity/tst_sanity /tests/auto/snippets/tst_snippets diff --git a/examples/controls/gallery/gallery.qml b/examples/controls/gallery/gallery.qml index 7ccadd06..5f80a047 100644 --- a/examples/controls/gallery/gallery.qml +++ b/examples/controls/gallery/gallery.qml @@ -214,12 +214,11 @@ ApplicationWindow { height: settingsColumn.implicitHeight + topPadding + bottomPadding modal: true focus: true - onPressedOutside: close() + closePolicy: Popup.OnEscape | Popup.OnPressOutside contentItem: ColumnLayout { id: settingsColumn spacing: 20 - Keys.onEscapePressed: settingsPopup.close() // TODO: Popup::closePolicy Label { text: "Settings" @@ -291,12 +290,11 @@ ApplicationWindow { y: window.height / 6 width: Math.min(window.width, window.height) / 3 * 2 contentHeight: aboutColumn.height - onPressedOutside: close() + closePolicy: Popup.OnEscape | Popup.OnPressOutside Column { id: aboutColumn spacing: 20 - Keys.onEscapePressed: aboutDialog.close() // TODO: Popup::closePolicy Label { text: "About" diff --git a/src/imports/controls/plugins.qmltypes b/src/imports/controls/plugins.qmltypes index 750ca818..ba9811a1 100644 --- a/src/imports/controls/plugins.qmltypes +++ b/src/imports/controls/plugins.qmltypes @@ -372,6 +372,15 @@ Module { exports: ["Qt.labs.templates/Popup 1.0"] exportMetaObjectRevisions: [0] Enum { + name: "ClosePolicy" + values: { + "NoAutoClose": 0, + "CloseOnPressOutside": 1, + "CloseOnReleaseOutside": 2, + "CloseOnEscape": 4 + } + } + Enum { name: "TransformOrigin" values: { "TopLeft": 0, @@ -408,6 +417,7 @@ Module { Property { name: "focus"; type: "bool" } Property { name: "modal"; type: "bool" } Property { name: "visible"; type: "bool" } + Property { name: "closePolicy"; type: "ClosePolicy" } Property { name: "transformOrigin"; type: "TransformOrigin" } Property { name: "enter"; type: "QQuickTransition"; isPointer: true } Property { name: "exit"; type: "QQuickTransition"; isPointer: true } diff --git a/src/imports/templates/plugins.qmltypes b/src/imports/templates/plugins.qmltypes index 9e512e5c..807b15ba 100644 --- a/src/imports/templates/plugins.qmltypes +++ b/src/imports/templates/plugins.qmltypes @@ -520,6 +520,15 @@ Module { exports: ["Qt.labs.templates/Popup 1.0"] exportMetaObjectRevisions: [0] Enum { + name: "ClosePolicy" + values: { + "NoAutoClose": 0, + "CloseOnPressOutside": 1, + "CloseOnReleaseOutside": 2, + "CloseOnEscape": 4 + } + } + Enum { name: "TransformOrigin" values: { "TopLeft": 0, @@ -556,6 +565,7 @@ Module { Property { name: "focus"; type: "bool" } Property { name: "modal"; type: "bool" } Property { name: "visible"; type: "bool" } + Property { name: "closePolicy"; type: "ClosePolicy" } Property { name: "transformOrigin"; type: "TransformOrigin" } Property { name: "enter"; type: "QQuickTransition"; isPointer: true } Property { name: "exit"; type: "QQuickTransition"; isPointer: true } diff --git a/src/templates/qquickcombobox.cpp b/src/templates/qquickcombobox.cpp index cee384e3..8a0028c4 100644 --- a/src/templates/qquickcombobox.cpp +++ b/src/templates/qquickcombobox.cpp @@ -739,8 +739,6 @@ void QQuickComboBox::keyPressEvent(QKeyEvent *event) d->increase(); event->accept(); break; - case Qt::Key_Escape: - event->accept(); default: break; } diff --git a/src/templates/qquickmenu.cpp b/src/templates/qquickmenu.cpp index c4e98536..692395ff 100644 --- a/src/templates/qquickmenu.cpp +++ b/src/templates/qquickmenu.cpp @@ -302,8 +302,7 @@ QQuickMenu::QQuickMenu(QObject *parent) : QQuickPopup(*(new QQuickMenuPrivate), parent) { Q_D(QQuickMenu); - connect(this, &QQuickMenu::pressedOutside, this, &QQuickMenu::close); - connect(this, &QQuickMenu::releasedOutside, this, &QQuickMenu::close); + setClosePolicy(OnEscape | OnPressOutside | OnReleaseOutside); QObjectPrivate::connect(this, &QQuickMenu::visibleChanged, d, &QQuickMenuPrivate::onMenuVisibleChanged); } @@ -514,10 +513,6 @@ bool QQuickMenu::eventFilter(QObject *object, QEvent *event) QMetaObject::invokeMethod(d->contentItem, "incrementCurrentIndex"); return true; - case Qt::Key_Escape: - close(); - return true; - default: break; } diff --git a/src/templates/qquickoverlay.cpp b/src/templates/qquickoverlay.cpp index bef6193d..ab32d118 100644 --- a/src/templates/qquickoverlay.cpp +++ b/src/templates/qquickoverlay.cpp @@ -35,7 +35,7 @@ ****************************************************************************/ #include "qquickoverlay_p.h" -#include "qquickpopup_p.h" +#include "qquickpopup_p_p.h" #include "qquickdrawer_p.h" #include <QtQml/qqmlinfo.h> #include <QtQml/qqmlproperty.h> @@ -225,6 +225,14 @@ void QQuickOverlay::mousePressEvent(QMouseEvent *event) Q_D(QQuickOverlay); event->setAccepted(d->modalPopups > 0); emit pressed(); + + foreach (QQuickPopup *popup, d->popups) { + if (popup->closePolicy().testFlag(QQuickPopup::OnPressOutside)) { + QQuickItem *popupItem = QQuickPopupPrivate::get(popup)->popupItem; + if (!popupItem->contains(mapToItem(popupItem, event->pos()))) + popup->close(); + } + } } void QQuickOverlay::mouseMoveEvent(QMouseEvent *event) @@ -238,6 +246,14 @@ void QQuickOverlay::mouseReleaseEvent(QMouseEvent *event) Q_D(QQuickOverlay); event->setAccepted(d->modalPopups > 0); emit released(); + + foreach (QQuickPopup *popup, d->popups) { + if (popup->closePolicy().testFlag(QQuickPopup::OnReleaseOutside)) { + QQuickItem *popupItem = QQuickPopupPrivate::get(popup)->popupItem; + if (!popupItem->contains(mapToItem(popupItem, event->pos()))) + popup->close(); + } + } } void QQuickOverlay::wheelEvent(QWheelEvent *event) diff --git a/src/templates/qquickpopup.cpp b/src/templates/qquickpopup.cpp index 6b226ae8..d32ba602 100644 --- a/src/templates/qquickpopup.cpp +++ b/src/templates/qquickpopup.cpp @@ -82,6 +82,7 @@ QQuickPopupPrivate::QQuickPopupPrivate() , bottomPadding(0) , contentWidth(0) , contentHeight(0) + , closePolicy(QQuickPopup::OnEscape) , parentItem(Q_NULLPTR) , background(Q_NULLPTR) , contentItem(Q_NULLPTR) @@ -1091,6 +1092,36 @@ void QQuickPopup::setVisible(bool visible) } /*! + \qmlproperty enumeration Qt.labs.controls::Popup::closePolicy + + This property determines the circumstances under which the popup closes. + The flags can be combined to allow several ways of closing the popup. + + The available values are: + \value Popup.NoAutoClose The popup will only close when manually instructed to do so. + \value Popup.OnPressOutside The popup will close when the mouse is pressed outside of it. + \value Popup.OnReleaseOutside The popup will close when the mouse is released outside of it. + \value Popup.OnEscape The popup will close when the escape key is pressed while the popup + has active focus. + + The default value is \c Popup.OnEscape. +*/ +QQuickPopup::ClosePolicy QQuickPopup::closePolicy() const +{ + Q_D(const QQuickPopup); + return d->closePolicy; +} + +void QQuickPopup::setClosePolicy(ClosePolicy policy) +{ + Q_D(QQuickPopup); + if (d->closePolicy == policy) + return; + d->closePolicy = policy; + emit closePolicyChanged(); +} + +/*! \qmlproperty enumeration Qt.labs.controls::Popup::transformOrigin This property holds the origin point for transformations in enter and exit transitions. @@ -1200,7 +1231,14 @@ void QQuickPopup::focusOutEvent(QFocusEvent *event) void QQuickPopup::keyPressEvent(QKeyEvent *event) { + Q_D(QQuickPopup); event->accept(); + + if (event->key() != Qt::Key_Escape) + return; + + if (d->closePolicy.testFlag(OnEscape)) + close(); } void QQuickPopup::keyReleaseEvent(QKeyEvent *event) diff --git a/src/templates/qquickpopup_p.h b/src/templates/qquickpopup_p.h index 66eae777..1780d1b0 100644 --- a/src/templates/qquickpopup_p.h +++ b/src/templates/qquickpopup_p.h @@ -90,6 +90,7 @@ class Q_LABSTEMPLATES_EXPORT QQuickPopup : public QObject, public QQmlParserStat Q_PROPERTY(bool focus READ hasFocus WRITE setFocus NOTIFY focusChanged) Q_PROPERTY(bool modal READ isModal WRITE setModal NOTIFY modalChanged) Q_PROPERTY(bool visible READ isVisible WRITE setVisible NOTIFY visibleChanged) + Q_PROPERTY(ClosePolicy closePolicy READ closePolicy WRITE setClosePolicy NOTIFY closePolicyChanged FINAL) Q_PROPERTY(TransformOrigin transformOrigin READ transformOrigin WRITE setTransformOrigin) Q_PROPERTY(QQuickTransition *enter READ enter WRITE setEnter NOTIFY enterChanged FINAL) Q_PROPERTY(QQuickTransition *exit READ exit WRITE setExit NOTIFY exitChanged FINAL) @@ -169,6 +170,18 @@ public: bool isVisible() const; void setVisible(bool visible); + enum ClosePolicyFlag { + NoAutoClose = 0x0, + OnPressOutside = 0x1, + OnReleaseOutside = 0x2, + OnEscape = 0x4 + }; + Q_DECLARE_FLAGS(ClosePolicy, ClosePolicyFlag) + Q_FLAG(ClosePolicy) + + ClosePolicy closePolicy() const; + void setClosePolicy(ClosePolicy policy); + // keep in sync with Item.TransformOrigin enum TransformOrigin { TopLeft, Top, TopRight, @@ -215,6 +228,7 @@ Q_SIGNALS: void focusChanged(); void modalChanged(); void visibleChanged(); + void closePolicyChanged(); void enterChanged(); void exitChanged(); @@ -253,6 +267,8 @@ private: friend class QQuickPopupItem; }; +Q_DECLARE_OPERATORS_FOR_FLAGS(QQuickPopup::ClosePolicy) + QT_END_NAMESPACE QML_DECLARE_TYPE(QQuickPopup) diff --git a/src/templates/qquickpopup_p_p.h b/src/templates/qquickpopup_p_p.h index 14f80880..f5aac221 100644 --- a/src/templates/qquickpopup_p_p.h +++ b/src/templates/qquickpopup_p_p.h @@ -183,6 +183,7 @@ public: qreal bottomPadding; qreal contentWidth; qreal contentHeight; + QQuickPopup::ClosePolicy closePolicy; QQuickItem *parentItem; QQuickItem *background; QQuickItem *contentItem; diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro index 3b3ee04a..ec9d4111 100644 --- a/tests/auto/auto.pro +++ b/tests/auto/auto.pro @@ -7,6 +7,7 @@ SUBDIRS += \ controls \ material \ menu \ + popup \ pressandhold \ sanity \ snippets \ diff --git a/tests/auto/popup/data/applicationwindow.qml b/tests/auto/popup/data/applicationwindow.qml new file mode 100644 index 00000000..69cbf47f --- /dev/null +++ b/tests/auto/popup/data/applicationwindow.qml @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2015 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 Qt.labs.controls 1.0 + +ApplicationWindow { + width: 400 + height: 400 + + property alias popup: popup + + Popup { + id: popup + x: (parent.width - width) / 2 + y: (parent.height - height) / 2 + + Text { + color: "white" + text: "Hello, world!" + + MouseArea { + anchors.fill: parent + onClicked: popup.close() + } + } + } +} diff --git a/tests/auto/popup/popup.pro b/tests/auto/popup/popup.pro new file mode 100644 index 00000000..87aca3b3 --- /dev/null +++ b/tests/auto/popup/popup.pro @@ -0,0 +1,14 @@ +CONFIG += testcase +TARGET = tst_popup +SOURCES += tst_popup.cpp + +osx:CONFIG -= app_bundle + +QT += core-private gui-private qml-private quick-private testlib labstemplates-private + +include (../shared/util.pri) + +TESTDATA = data/* + +OTHER_FILES += \ + data/* diff --git a/tests/auto/popup/tst_popup.cpp b/tests/auto/popup/tst_popup.cpp new file mode 100644 index 00000000..df73c320 --- /dev/null +++ b/tests/auto/popup/tst_popup.cpp @@ -0,0 +1,184 @@ +/**************************************************************************** +** +** Copyright (C) 2015 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:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later 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 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtTest/qtest.h> +#include <QtTest/qsignalspy.h> +#include "../shared/util.h" +#include "../shared/visualtestutil.h" + +#include <QtLabsTemplates/private/qquickapplicationwindow_p.h> +#include <QtLabsTemplates/private/qquickoverlay_p.h> +#include <QtLabsTemplates/private/qquickpopup_p_p.h> + +using namespace QQuickVisualTestUtil; + +class tst_popup : public QQmlDataTest +{ + Q_OBJECT + +private slots: + void visible(); + void overlay(); + void closePolicy_data(); + void closePolicy(); +}; + +void tst_popup::visible() +{ + QQuickApplicationHelper helper(this, QStringLiteral("applicationwindow.qml")); + + QQuickApplicationWindow *window = helper.window; + window->show(); + QVERIFY(QTest::qWaitForWindowActive(window)); + + QQuickPopup *popup = helper.window->property("popup").value<QQuickPopup*>(); + QVERIFY(popup); + QQuickItem *popupItem = QQuickPopupPrivate::get(popup)->popupItem; + + popup->open(); + QVERIFY(popup->isVisible()); + QVERIFY(window->overlay()->childItems().contains(popupItem)); + + popup->close(); + QVERIFY(!popup->isVisible()); + QVERIFY(!window->overlay()->childItems().contains(popupItem)); + + popup->setVisible(true); + QVERIFY(popup->isVisible()); + QVERIFY(window->overlay()->childItems().contains(popupItem)); + + popup->setVisible(false); + QVERIFY(!popup->isVisible()); + QVERIFY(!window->overlay()->childItems().contains(popupItem)); +} + +void tst_popup::overlay() +{ + QQuickApplicationHelper helper(this, QStringLiteral("applicationwindow.qml")); + + QQuickApplicationWindow *window = helper.window; + window->show(); + QVERIFY(QTest::qWaitForWindowActive(window)); + + QQuickItem *overlay = window->overlay(); + QSignalSpy overlayPressedSignal(overlay, SIGNAL(pressed())); + QSignalSpy overlayReleasedSignal(overlay, SIGNAL(released())); + QVERIFY(overlayPressedSignal.isValid()); + QVERIFY(overlayReleasedSignal.isValid()); + + QTest::mousePress(window, Qt::LeftButton); + QCOMPARE(overlayPressedSignal.count(), 1); + QCOMPARE(overlayReleasedSignal.count(), 0); + + QTest::mouseRelease(window, Qt::LeftButton); + QCOMPARE(overlayPressedSignal.count(), 1); + QCOMPARE(overlayReleasedSignal.count(), 0); // no modal popups open + + QQuickPopup *popup = helper.window->property("popup").value<QQuickPopup*>(); + QVERIFY(popup); + + popup->setModal(true); + + popup->open(); + QVERIFY(popup->isVisible()); + + QTest::mousePress(window, Qt::LeftButton, Qt::NoModifier, QPoint(popup->x() - 1, popup->y() - 1)); + QCOMPARE(overlayPressedSignal.count(), 2); + QCOMPARE(overlayReleasedSignal.count(), 0); + + QTest::mouseRelease(window, Qt::LeftButton, Qt::NoModifier, QPoint(popup->x() - 1, popup->y() - 1)); + QCOMPARE(overlayPressedSignal.count(), 2); + QCOMPARE(overlayReleasedSignal.count(), 1); + + QTest::mouseClick(window, Qt::LeftButton, Qt::NoModifier, QPoint(popup->x() + popup->width() / 2, popup->y() + popup->height() / 2)); + QCOMPARE(overlayPressedSignal.count(), 2); + QCOMPARE(overlayReleasedSignal.count(), 1); + QVERIFY(!popup->isVisible()); +} + +Q_DECLARE_METATYPE(QQuickPopup::ClosePolicy) + +void tst_popup::closePolicy_data() +{ + qRegisterMetaType<QQuickPopup::ClosePolicy>(); + + QTest::addColumn<QQuickPopup::ClosePolicy>("closePolicy"); + + QTest::newRow("NoAutoClose") << static_cast<QQuickPopup::ClosePolicy>(QQuickPopup::NoAutoClose); + QTest::newRow("OnPressOutside") << static_cast<QQuickPopup::ClosePolicy>(QQuickPopup::OnPressOutside); + QTest::newRow("OnReleaseOutside") << static_cast<QQuickPopup::ClosePolicy>(QQuickPopup::OnReleaseOutside); + QTest::newRow("OnEscape") << static_cast<QQuickPopup::ClosePolicy>(QQuickPopup::OnEscape); +} + +void tst_popup::closePolicy() +{ + QFETCH(QQuickPopup::ClosePolicy, closePolicy); + + QQuickApplicationHelper helper(this, QStringLiteral("applicationwindow.qml")); + + QQuickApplicationWindow *window = helper.window; + window->show(); + QVERIFY(QTest::qWaitForWindowActive(window)); + + QQuickPopup *popup = helper.window->property("popup").value<QQuickPopup*>(); + QVERIFY(popup); + + popup->setModal(true); + popup->setFocus(true); + popup->setClosePolicy(closePolicy); + + popup->open(); + QVERIFY(popup->isVisible()); + + QTest::mousePress(window, Qt::LeftButton, Qt::NoModifier, QPoint(popup->x() - 1, popup->y() - 1)); + QCOMPARE(popup->isVisible(), !closePolicy.testFlag(QQuickPopup::OnPressOutside)); + + popup->open(); + QVERIFY(popup->isVisible()); + + QTest::mouseRelease(window, Qt::LeftButton, Qt::NoModifier, QPoint(popup->x() - 1, popup->y() - 1)); + QCOMPARE(popup->isVisible(), !closePolicy.testFlag(QQuickPopup::OnReleaseOutside)); + + popup->open(); + QVERIFY(popup->isVisible()); + + QTest::keyClick(window, Qt::Key_Escape); + QCOMPARE(popup->isVisible(), !closePolicy.testFlag(QQuickPopup::OnEscape)); +} + +QTEST_MAIN(tst_popup) + +#include "tst_popup.moc" |