From 6b8a9673111bbf888990ce5904e176057ad4a71b Mon Sep 17 00:00:00 2001 From: Mitch Curtis Date: Tue, 4 May 2021 15:43:38 +0200 Subject: Allow creation of custom QQuickPopupItem-derived types This allows QQuickPopup-derived types to have their own QQuickPopup-derived popup item. This is useful for controlling e.g. implicit content item sizing. Task-number: QTBUG-83630 Pick-to: 6.1 5.15 Change-Id: I279d2e39df9a9cff29b3015a2f5baae7128f461f Reviewed-by: Fabian Kosmale --- src/quicktemplates2/CMakeLists.txt | 1 + src/quicktemplates2/qquickmenu.cpp | 1 + src/quicktemplates2/qquickmenu_p_p.h | 2 +- src/quicktemplates2/qquickpopup.cpp | 17 ++++- src/quicktemplates2/qquickpopup_p_p.h | 4 +- src/quicktemplates2/qquickpopupitem.cpp | 52 ++++++++----- src/quicktemplates2/qquickpopupitem_p.h | 119 ++++++++++++++++++++++++++++++ src/quicktemplates2/qquickpopupitem_p_p.h | 61 +-------------- 8 files changed, 175 insertions(+), 82 deletions(-) create mode 100644 src/quicktemplates2/qquickpopupitem_p.h (limited to 'src') diff --git a/src/quicktemplates2/CMakeLists.txt b/src/quicktemplates2/CMakeLists.txt index f0103b17..4f4bf552 100644 --- a/src/quicktemplates2/CMakeLists.txt +++ b/src/quicktemplates2/CMakeLists.txt @@ -67,6 +67,7 @@ qt_internal_add_module(QuickTemplates2 qquickpopupanchors.cpp qquickpopupanchors_p.h qquickpopupanchors_p_p.h qquickpopupitem.cpp + qquickpopupitem_p.h qquickpopupitem_p_p.h qquickpopuppositioner.cpp qquickpopuppositioner_p_p.h diff --git a/src/quicktemplates2/qquickmenu.cpp b/src/quicktemplates2/qquickmenu.cpp index 5b54b0db..d9b84714 100644 --- a/src/quicktemplates2/qquickmenu.cpp +++ b/src/quicktemplates2/qquickmenu.cpp @@ -225,6 +225,7 @@ QQuickMenuPrivate::QQuickMenuPrivate() void QQuickMenuPrivate::init() { Q_Q(QQuickMenu); + QQuickPopupPrivate::init(); contentModel = new QQmlObjectModel(q); } diff --git a/src/quicktemplates2/qquickmenu_p_p.h b/src/quicktemplates2/qquickmenu_p_p.h index 86701d9b..df51f1f3 100644 --- a/src/quicktemplates2/qquickmenu_p_p.h +++ b/src/quicktemplates2/qquickmenu_p_p.h @@ -73,7 +73,7 @@ public: return menu->d_func(); } - void init(); + void init() override; QQuickItem *itemAt(int index) const; void insertItem(int index, QQuickItem *item); diff --git a/src/quicktemplates2/qquickpopup.cpp b/src/quicktemplates2/qquickpopup.cpp index 262f2a2a..292d52a3 100644 --- a/src/quicktemplates2/qquickpopup.cpp +++ b/src/quicktemplates2/qquickpopup.cpp @@ -275,9 +275,21 @@ QQuickPopupPrivate::QQuickPopupPrivate() void QQuickPopupPrivate::init() { Q_Q(QQuickPopup); - popupItem = new QQuickPopupItem(q); + createPopupItem(); popupItem->setVisible(false); q->setParentItem(qobject_cast(parent)); + connectToPopupItem(); +} + +void QQuickPopupPrivate::createPopupItem() +{ + Q_Q(QQuickPopup); + popupItem = new QQuickPopupItem(q); +} + +void QQuickPopupPrivate::connectToPopupItem() +{ + Q_Q(QQuickPopup); QObject::connect(popupItem, &QQuickControl::paddingChanged, q, &QQuickPopup::paddingChanged); QObject::connect(popupItem, &QQuickControl::backgroundChanged, q, &QQuickPopup::backgroundChanged); QObject::connect(popupItem, &QQuickControl::contentItemChanged, q, &QQuickPopup::contentItemChanged); @@ -853,8 +865,7 @@ QQuickPopup::QQuickPopup(QObject *parent) QQuickPopup::QQuickPopup(QQuickPopupPrivate &dd, QObject *parent) : QObject(dd, parent) { - Q_D(QQuickPopup); - d->init(); + dd.init(); } QQuickPopup::~QQuickPopup() diff --git a/src/quicktemplates2/qquickpopup_p_p.h b/src/quicktemplates2/qquickpopup_p_p.h index 3e9d3d73..57183011 100644 --- a/src/quicktemplates2/qquickpopup_p_p.h +++ b/src/quicktemplates2/qquickpopup_p_p.h @@ -101,7 +101,9 @@ public: QQmlListProperty contentData(); QQmlListProperty contentChildren(); - void init(); + virtual void init(); + void createPopupItem(); + void connectToPopupItem(); void closeOrReject(); bool tryClose(const QPointF &pos, QQuickPopup::ClosePolicy flags); diff --git a/src/quicktemplates2/qquickpopupitem.cpp b/src/quicktemplates2/qquickpopupitem.cpp index a142b454..0381dac2 100644 --- a/src/quicktemplates2/qquickpopupitem.cpp +++ b/src/quicktemplates2/qquickpopupitem.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2017 The Qt Company Ltd. +** Copyright (C) 2021 The Qt Company Ltd. ** Contact: http://www.qt.io/licensing/ ** ** This file is part of the Qt Quick Templates 2 module of the Qt Toolkit. @@ -59,6 +59,30 @@ QQuickPopupItemPrivate::QQuickPopupItemPrivate(QQuickPopup *popup) isTabFence = true; } +void QQuickPopupItemPrivate::init() +{ + Q_Q(QQuickPopupItem); + q->setParent(popup); + q->setFlag(QQuickItem::ItemIsFocusScope); + q->setAcceptedMouseButtons(Qt::AllButtons); +#if QT_CONFIG(quicktemplates2_multitouch) + q->setAcceptTouchEvents(true); +#endif +#if QT_CONFIG(cursor) + q->setCursor(Qt::ArrowCursor); +#endif + + q->connect(popup, &QQuickPopup::paletteChanged, q, &QQuickItem::paletteChanged); + q->connect(popup, &QQuickPopup::paletteCreated, q, &QQuickItem::paletteCreated); + +#if QT_CONFIG(quicktemplates2_hover) + // TODO: switch to QStyleHints::useHoverEffects in Qt 5.8 + q->setHoverEnabled(true); + // setAcceptHoverEvents(QGuiApplication::styleHints()->useHoverEffects()); + // connect(QGuiApplication::styleHints(), &QStyleHints::useHoverEffectsChanged, this, &QQuickItem::setAcceptHoverEvents); +#endif +} + void QQuickPopupItemPrivate::implicitWidthChanged() { QQuickPagePrivate::implicitWidthChanged(); @@ -127,25 +151,15 @@ void QQuickPopupItemPrivate::executeBackground(bool complete) QQuickPopupItem::QQuickPopupItem(QQuickPopup *popup) : QQuickPage(*(new QQuickPopupItemPrivate(popup)), nullptr) { - setParent(popup); - setFlag(ItemIsFocusScope); - setAcceptedMouseButtons(Qt::AllButtons); -#if QT_CONFIG(quicktemplates2_multitouch) - setAcceptTouchEvents(true); -#endif -#if QT_CONFIG(cursor) - setCursor(Qt::ArrowCursor); -#endif - - connect(popup, &QQuickPopup::paletteChanged, this, &QQuickItem::paletteChanged); - connect(popup, &QQuickPopup::paletteCreated, this, &QQuickItem::paletteCreated); + Q_D(QQuickPopupItem); + d->init(); +} -#if QT_CONFIG(quicktemplates2_hover) - // TODO: switch to QStyleHints::useHoverEffects in Qt 5.8 - setHoverEnabled(true); - // setAcceptHoverEvents(QGuiApplication::styleHints()->useHoverEffects()); - // connect(QGuiApplication::styleHints(), &QStyleHints::useHoverEffectsChanged, this, &QQuickItem::setAcceptHoverEvents); -#endif +QQuickPopupItem::QQuickPopupItem(QQuickPopupItemPrivate &dd) : + QQuickPage(dd, nullptr) +{ + Q_D(QQuickPopupItem); + d->init(); } void QQuickPopupItem::grabShortcut() diff --git a/src/quicktemplates2/qquickpopupitem_p.h b/src/quicktemplates2/qquickpopupitem_p.h new file mode 100644 index 00000000..db444ddc --- /dev/null +++ b/src/quicktemplates2/qquickpopupitem_p.h @@ -0,0 +1,119 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Quick Templates 2 module 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$ +** +****************************************************************************/ + +#ifndef QQUICKPOPUPITEM_P_H +#define QQUICKPOPUPITEM_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include +#include + +QT_BEGIN_NAMESPACE + +class QQuickPopup; +class QQuickPopupItemPrivate; + +class QQuickPopupItem : public QQuickPage +{ + Q_OBJECT + +public: + explicit QQuickPopupItem(QQuickPopup *popup); + + void grabShortcut(); + void ungrabShortcut(); + +protected: + void updatePolish() override; + + bool event(QEvent *event) override; + bool childMouseEventFilter(QQuickItem *child, QEvent *event) override; + void focusInEvent(QFocusEvent *event) override; + void focusOutEvent(QFocusEvent *event) override; + void keyPressEvent(QKeyEvent *event) override; + void keyReleaseEvent(QKeyEvent *event) override; + void mousePressEvent(QMouseEvent *event) override; + void mouseMoveEvent(QMouseEvent *event) override; + void mouseReleaseEvent(QMouseEvent *event) override; + void mouseDoubleClickEvent(QMouseEvent *event) override; + void mouseUngrabEvent() override; +#if QT_CONFIG(quicktemplates2_multitouch) + void touchEvent(QTouchEvent *event) override; + void touchUngrabEvent() override; +#endif +#if QT_CONFIG(wheelevent) + void wheelEvent(QWheelEvent *event) override; +#endif + + void contentItemChange(QQuickItem *newItem, QQuickItem *oldItem) override; + void contentSizeChange(const QSizeF &newSize, const QSizeF &oldSize) override; + void fontChange(const QFont &newFont, const QFont &oldFont) override; + void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override; + void localeChange(const QLocale &newLocale, const QLocale &oldLocale) override; + void mirrorChange() override; + void itemChange(ItemChange change, const ItemChangeData &data) override; + void paddingChange(const QMarginsF &newPadding, const QMarginsF &oldPadding) override; + void enabledChange() override; + + QFont defaultFont() const override; + +#if QT_CONFIG(accessibility) + QAccessible::Role accessibleRole() const override; + void accessibilityActiveChanged(bool active) override; +#endif + +protected: + QQuickPopupItem(QQuickPopupItemPrivate &dd); + +private: + Q_DISABLE_COPY(QQuickPopupItem) + Q_DECLARE_PRIVATE(QQuickPopupItem) + friend class QQuickPopup; +}; + +QT_END_NAMESPACE + +#endif // QQUICKPOPUPITEM_P_H diff --git a/src/quicktemplates2/qquickpopupitem_p_p.h b/src/quicktemplates2/qquickpopupitem_p_p.h index e4ef6d9c..b3224be7 100644 --- a/src/quicktemplates2/qquickpopupitem_p_p.h +++ b/src/quicktemplates2/qquickpopupitem_p_p.h @@ -48,68 +48,11 @@ // We mean it. // -#include -#include +#include QT_BEGIN_NAMESPACE class QQuickPopup; -class QQuickPopupItemPrivate; - -class QQuickPopupItem : public QQuickPage -{ - Q_OBJECT - -public: - explicit QQuickPopupItem(QQuickPopup *popup); - - void grabShortcut(); - void ungrabShortcut(); - -protected: - void updatePolish() override; - - bool event(QEvent *event) override; - bool childMouseEventFilter(QQuickItem *child, QEvent *event) override; - void focusInEvent(QFocusEvent *event) override; - void focusOutEvent(QFocusEvent *event) override; - void keyPressEvent(QKeyEvent *event) override; - void keyReleaseEvent(QKeyEvent *event) override; - void mousePressEvent(QMouseEvent *event) override; - void mouseMoveEvent(QMouseEvent *event) override; - void mouseReleaseEvent(QMouseEvent *event) override; - void mouseDoubleClickEvent(QMouseEvent *event) override; - void mouseUngrabEvent() override; -#if QT_CONFIG(quicktemplates2_multitouch) - void touchEvent(QTouchEvent *event) override; - void touchUngrabEvent() override; -#endif -#if QT_CONFIG(wheelevent) - void wheelEvent(QWheelEvent *event) override; -#endif - - void contentItemChange(QQuickItem *newItem, QQuickItem *oldItem) override; - void contentSizeChange(const QSizeF &newSize, const QSizeF &oldSize) override; - void fontChange(const QFont &newFont, const QFont &oldFont) override; - void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override; - void localeChange(const QLocale &newLocale, const QLocale &oldLocale) override; - void mirrorChange() override; - void itemChange(ItemChange change, const ItemChangeData &data) override; - void paddingChange(const QMarginsF &newPadding, const QMarginsF &oldPadding) override; - void enabledChange() override; - - QFont defaultFont() const override; - -#if QT_CONFIG(accessibility) - QAccessible::Role accessibleRole() const override; - void accessibilityActiveChanged(bool active) override; -#endif - -private: - Q_DISABLE_COPY(QQuickPopupItem) - Q_DECLARE_PRIVATE(QQuickPopupItem) - friend class QQuickPopup; -}; class QQuickPopupItemPrivate : public QQuickPagePrivate { @@ -118,6 +61,8 @@ class QQuickPopupItemPrivate : public QQuickPagePrivate public: QQuickPopupItemPrivate(QQuickPopup *popup); + void init(); + void implicitWidthChanged() override; void implicitHeightChanged() override; -- cgit v1.2.3