aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/quicktemplates2/qquickmenu.cpp48
-rw-r--r--tests/auto/qquickmenu/data/instantiator.qml76
-rw-r--r--tests/auto/qquickmenu/data/instantiatorWithItemsBeforeAndAfter.qml88
-rw-r--r--tests/auto/qquickmenu/tst_qquickmenu.cpp71
4 files changed, 279 insertions, 4 deletions
diff --git a/src/quicktemplates2/qquickmenu.cpp b/src/quicktemplates2/qquickmenu.cpp
index 7086db91..42059e7f 100644
--- a/src/quicktemplates2/qquickmenu.cpp
+++ b/src/quicktemplates2/qquickmenu.cpp
@@ -43,6 +43,7 @@
#include "qquickpopuppositioner_p_p.h"
#include "qquickaction_p.h"
+#include <QtCore/qloggingcategory.h>
#include <QtGui/qevent.h>
#include <QtGui/qcursor.h>
#include <QtGui/qpa/qplatformintegration.h>
@@ -54,6 +55,8 @@
#include <QtQml/private/qv4variantobject_p.h>
#include <QtQml/private/qv4qobjectwrapper_p.h>
#include <QtQml/private/qqmlobjectmodel_p.h>
+#include <QtQml/private/qqmlinstantiator_p.h>
+#include <QtQml/private/qqmlinstantiator_p_p.h>
#include <QtQuick/private/qquickitem_p.h>
#include <QtQuick/private/qquickitemchangelistener_p.h>
#include <QtQuick/private/qquickitemview_p.h>
@@ -65,6 +68,8 @@ QT_BEGIN_NAMESPACE
// copied from qfusionstyle.cpp
static const int SUBMENU_DELAY = 225;
+Q_LOGGING_CATEGORY(qlcQQuickMenu, "qt.quick.controls.menu")
+
/*!
\qmltype Menu
\inherits Popup
@@ -279,11 +284,37 @@ void QQuickMenuPrivate::recreateItems()
// removeItem() will remove stuff from contentData, so we have to make a copy of it.
const auto originalContentData = contentData;
- while (contentModel->count() > 0)
- removeItem(0, itemAt(0));
+ qCDebug(qlcQQuickMenu) << "removing items so that we can recreate them:";
+ QSet<QObject*> instantiatedObjects;
+ for (int i = 0; i < contentModel->count(); ) {
+ QQuickItem *item = itemAt(i);
+ QQmlInstantiator *instantiator = qobject_cast<QQmlInstantiator*>(item->parent());
+ if (instantiator) {
+ // Don't try to recreate items owned by an instantiator.
+ qCDebug(qlcQQuickMenu) << "- item" << item << "at index" << i << "with parent"
+ << item->parent() << "was instantiated by Instantiator; ignoring";
+ instantiatedObjects.insert(item);
+ ++i;
+ } else {
+ qCDebug(qlcQQuickMenu) << "- removing item" << item << "at index" << i << "with parent" << item->parent();
+ removeItem(0, item);
+ }
+ }
- for (QObject *object : originalContentData)
- createAndAppendItem(object);
+ qCDebug(qlcQQuickMenu) << "recreating items:";
+ for (QObject *object : originalContentData) {
+ QQmlInstantiator *instantiator = qobject_cast<QQmlInstantiator*>(object);
+ // Instantiators are part of our contentData. If this particular object is an Instantiator,
+ // let it recreate its own items, otherwise strange things happen (the items are culled).
+ if (instantiator) {
+ qCDebug(qlcQQuickMenu) << "- contentData object" << object << "is an Instantiator;"
+ << "asking them to regenerate their own items";
+ QQmlInstantiatorPrivate::get(instantiator)->regenerate();
+ } else if (!instantiatedObjects.contains(object)) {
+ qCDebug(qlcQQuickMenu) << "- creating MenuItem for contentData object" << object;
+ createAndAppendItem(object);
+ }
+ }
}
QQuickItem *QQuickMenuPrivate::beginCreateItem()
@@ -358,6 +389,7 @@ void QQuickMenuPrivate::resizeItems()
void QQuickMenuPrivate::itemChildAdded(QQuickItem *, QQuickItem *child)
{
// add dynamically reparented items (eg. by a Repeater)
+ qCDebug(qlcQQuickMenu) << "item child added" << child;
if (!QQuickItemPrivate::get(child)->isTransparentForPositioner() && !contentData.contains(child))
insertItem(contentModel->count(), child);
}
@@ -365,6 +397,7 @@ void QQuickMenuPrivate::itemChildAdded(QQuickItem *, QQuickItem *child)
void QQuickMenuPrivate::itemParentChanged(QQuickItem *item, QQuickItem *parent)
{
// remove dynamically unparented items (eg. by a Repeater)
+ qCDebug(qlcQQuickMenu) << "parent of item" << item << "changed to" << parent;
if (!parent)
removeItem(contentModel->indexOf(item, nullptr), item);
}
@@ -375,6 +408,8 @@ void QQuickMenuPrivate::itemSiblingOrderChanged(QQuickItem *)
Q_Q(QQuickMenu);
QList<QQuickItem *> siblings = contentItem->childItems();
+ qCDebug(qlcQQuickMenu) << "item sibling order changed";
+
int to = 0;
for (int i = 0; i < siblings.count(); ++i) {
QQuickItem* sibling = siblings.at(i);
@@ -387,6 +422,7 @@ void QQuickMenuPrivate::itemSiblingOrderChanged(QQuickItem *)
void QQuickMenuPrivate::itemDestroyed(QQuickItem *item)
{
+ qCDebug(qlcQQuickMenu) << "item child destroyed" << item;
QQuickPopupPrivate::itemDestroyed(item);
int index = contentModel->indexOf(item, nullptr);
if (index != -1)
@@ -662,12 +698,16 @@ void QQuickMenuPrivate::contentData_append(QQmlListProperty<QObject> *prop, QObj
QQuickMenuPrivate *p = QQuickMenuPrivate::get(q);
if (!p->complete) {
+ qCDebug(qlcQQuickMenu) << "appending object" << obj
+ << "to contentData, but delaying MenuItem creation (if necessary) until we're completed";
// Don't add items until we're complete, as the delegate could change in the meantime.
// We'll add it to contentData and create it when we're complete.
p->contentData.append(obj);
return;
}
+ qCDebug(qlcQQuickMenu) << "appending object" << obj
+ << "to contentData and creating MenuItem for it if necessary";
p->createAndAppendItem(obj);
}
diff --git a/tests/auto/qquickmenu/data/instantiator.qml b/tests/auto/qquickmenu/data/instantiator.qml
new file mode 100644
index 00000000..270266c3
--- /dev/null
+++ b/tests/auto/qquickmenu/data/instantiator.qml
@@ -0,0 +1,76 @@
+/****************************************************************************
+**
+** Copyright (C) 2018 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** 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 https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** BSD License Usage
+** Alternatively, 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 QtQml 2.11
+import QtQuick 2.11
+import QtQuick.Controls 2.4
+
+ApplicationWindow {
+ width: 200
+ height: 200
+
+ property alias menu: menu
+
+ Menu {
+ id: menu
+
+ Instantiator {
+ model: ["A", "B"]
+
+ MenuItem {
+ objectName: text
+ text: modelData
+ }
+
+ onObjectAdded: menu.insertItem(index, object)
+ onObjectRemoved: menu.removeItem(object)
+ }
+ }
+}
diff --git a/tests/auto/qquickmenu/data/instantiatorWithItemsBeforeAndAfter.qml b/tests/auto/qquickmenu/data/instantiatorWithItemsBeforeAndAfter.qml
new file mode 100644
index 00000000..9f3ef430
--- /dev/null
+++ b/tests/auto/qquickmenu/data/instantiatorWithItemsBeforeAndAfter.qml
@@ -0,0 +1,88 @@
+/****************************************************************************
+**
+** Copyright (C) 2018 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** 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 https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** BSD License Usage
+** Alternatively, 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 QtQml 2.11
+import QtQuick 2.11
+import QtQuick.Controls 2.4
+
+ApplicationWindow {
+ width: 200
+ height: 200
+
+ property alias menu: menu
+
+ Menu {
+ id: menu
+
+ MenuItem {
+ objectName: text
+ text: "Before"
+ }
+
+ Instantiator {
+ model: ["Instantiated #1", "Instantiated #2"]
+
+ MenuItem {
+ objectName: text
+ text: modelData
+ }
+
+ // We want our items to be added after the "Before" MenuItem,
+ // so we have to insert items at the index after it.
+ onObjectAdded: menu.insertItem(1 + index, object)
+ onObjectRemoved: menu.removeItem(object)
+ }
+
+ MenuItem {
+ objectName: text
+ text: "After"
+ }
+ }
+}
diff --git a/tests/auto/qquickmenu/tst_qquickmenu.cpp b/tests/auto/qquickmenu/tst_qquickmenu.cpp
index 27cd8aaa..2ba9363b 100644
--- a/tests/auto/qquickmenu/tst_qquickmenu.cpp
+++ b/tests/auto/qquickmenu/tst_qquickmenu.cpp
@@ -89,6 +89,8 @@ private slots:
void scrollable_data();
void scrollable();
void delegateFromSeparateComponent();
+ void instantiator();
+ void instantiatorWithItemsBeforeAndAfter();
};
void tst_QQuickMenu::defaults()
@@ -1434,6 +1436,75 @@ void tst_QQuickMenu::delegateFromSeparateComponent()
QCOMPARE(actionItem2Bg->property("color").value<QColor>(), green);
}
+void tst_QQuickMenu::instantiator()
+{
+ if (QGuiApplication::styleHints()->tabFocusBehavior() != Qt::TabFocusAllControls)
+ QSKIP("This platform only allows tab focus for text controls");
+
+ QQuickApplicationHelper helper(this, QLatin1String("instantiator.qml"));
+
+ QQuickApplicationWindow *window = helper.appWindow;
+ window->show();
+ window->requestActivate();
+ QVERIFY(QTest::qWaitForWindowActive(window));
+ QVERIFY(QGuiApplication::focusWindow() == window);
+ centerOnScreen(window);
+ moveMouseAway(window);
+
+ QQuickMenu *menu = window->property("menu").value<QQuickMenu*>();
+ menu->open();
+ QVERIFY(menu->isVisible());
+ waitForMenuListViewPolish(menu);
+ menu->setFocus(true);
+
+ // Highlight the first item.
+ QQuickMenuItem *firstItem = qobject_cast<QQuickMenuItem *>(menu->itemAt(0));
+ QVERIFY(firstItem);
+ QTest::keyClick(window, Qt::Key_Tab);
+ QVERIFY(firstItem->hasActiveFocus());
+ QVERIFY(firstItem->hasVisualFocus());
+ QVERIFY(firstItem->isHighlighted());
+ QTRY_VERIFY(!QQuickItemPrivate::get(firstItem)->culled);
+
+ // Highlight the second item.
+ QQuickMenuItem *secondItem = qobject_cast<QQuickMenuItem *>(menu->itemAt(1));
+ QVERIFY(secondItem);
+ QTest::keyClick(window, Qt::Key_Down);
+ QVERIFY(secondItem->hasActiveFocus());
+ QVERIFY(secondItem->hasVisualFocus());
+ QVERIFY(secondItem->isHighlighted());
+ QVERIFY(!QQuickItemPrivate::get(secondItem)->culled);
+}
+
+void tst_QQuickMenu::instantiatorWithItemsBeforeAndAfter()
+{
+ QQuickApplicationHelper helper(this, QLatin1String("instantiatorWithItemsBeforeAndAfter.qml"));
+
+ QQuickApplicationWindow *window = helper.appWindow;
+ window->show();
+ window->requestActivate();
+ QVERIFY(QTest::qWaitForWindowActive(window));
+ QVERIFY(QGuiApplication::focusWindow() == window);
+ centerOnScreen(window);
+ moveMouseAway(window);
+
+ QQuickMenu *menu = window->property("menu").value<QQuickMenu*>();
+ menu->open();
+ QVERIFY(menu->isVisible());
+ waitForMenuListViewPolish(menu);
+
+ QStringList expectedItemTexts;
+ expectedItemTexts << QLatin1String("Before") << QLatin1String("Instantiated #1")
+ << QLatin1String("Instantiated #2") << QLatin1String("After");
+
+ for (int i = 0; i < expectedItemTexts.size(); ++i) {
+ const QString expectedText = expectedItemTexts.at(i);
+ QQuickMenuItem *menuItem = qobject_cast<QQuickMenuItem*>(menu->itemAt(i));
+ QVERIFY(menuItem);
+ QCOMPARE(menuItem->text(), expectedText);
+ }
+}
+
QTEST_MAIN(tst_QQuickMenu)
#include "tst_qquickmenu.moc"