aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJ-P Nurmi <jpnurmi@qt.io>2017-06-16 14:55:00 +0200
committerJ-P Nurmi <jpnurmi@qt.io>2017-06-23 17:51:53 +0000
commit8e147d3a5acad2aaedd03103340854e088f2029a (patch)
tree5dc2ba5c450a5544eb4ba1c468e12b3f774a7311
parent83c31defc6a8dce29073f626aa2aaf7b26e4f029 (diff)
Move visual overlay background management to QQuickPopup
During the early days, there was only one visual overlay background dimmer, so QQuickOverlay was the natural choice to manage it. It was soon realized that multiple modal popups and drawers needed each their own background dimming. Now the implementation becomes simpler when each popup manages its own background dimming. This paves the road for the upcoming changes making it possible to provide background dimming without ApplicationWindow. Task-number: QTBUG-61336 Change-Id: I621db4efe4aa74c147f81f7aee8affebddb00d49 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
-rw-r--r--src/quicktemplates2/qquickdrawer.cpp10
-rw-r--r--src/quicktemplates2/qquickdrawer_p_p.h2
-rw-r--r--src/quicktemplates2/qquickoverlay.cpp123
-rw-r--r--src/quicktemplates2/qquickoverlay_p_p.h7
-rw-r--r--src/quicktemplates2/qquickpopup.cpp91
-rw-r--r--src/quicktemplates2/qquickpopup_p_p.h6
6 files changed, 110 insertions, 129 deletions
diff --git a/src/quicktemplates2/qquickdrawer.cpp b/src/quicktemplates2/qquickdrawer.cpp
index 66c84ea2..8e77b966 100644
--- a/src/quicktemplates2/qquickdrawer.cpp
+++ b/src/quicktemplates2/qquickdrawer.cpp
@@ -226,6 +226,16 @@ void QQuickDrawerPrivate::reposition()
QQuickPopupPrivate::reposition();
}
+void QQuickDrawerPrivate::showOverlay()
+{
+ // managed in setPosition()
+}
+
+void QQuickDrawerPrivate::hideOverlay()
+{
+ // managed in setPosition()
+}
+
void QQuickDrawerPrivate::resizeOverlay()
{
if (!dimmer || !window)
diff --git a/src/quicktemplates2/qquickdrawer_p_p.h b/src/quicktemplates2/qquickdrawer_p_p.h
index 92de9010..72a83343 100644
--- a/src/quicktemplates2/qquickdrawer_p_p.h
+++ b/src/quicktemplates2/qquickdrawer_p_p.h
@@ -70,6 +70,8 @@ public:
qreal positionAt(const QPointF &point) const;
void reposition() override;
+ void showOverlay() override;
+ void hideOverlay() override;
void resizeOverlay() override;
bool startDrag(QEvent *event);
diff --git a/src/quicktemplates2/qquickoverlay.cpp b/src/quicktemplates2/qquickoverlay.cpp
index a4aaa60d..8ea97069 100644
--- a/src/quicktemplates2/qquickoverlay.cpp
+++ b/src/quicktemplates2/qquickoverlay.cpp
@@ -47,103 +47,6 @@
QT_BEGIN_NAMESPACE
-void QQuickOverlayPrivate::popupAboutToShow()
-{
- Q_Q(QQuickOverlay);
- QQuickPopup *popup = qobject_cast<QQuickPopup *>(q->sender());
- if (!popup || !popup->dim())
- return;
-
- // use QQmlProperty instead of QQuickItem::setOpacity() to trigger QML Behaviors
- QQuickPopupPrivate *p = QQuickPopupPrivate::get(popup);
- if (p->dimmer)
- QQmlProperty::write(p->dimmer, QStringLiteral("opacity"), 1.0);
-}
-
-void QQuickOverlayPrivate::popupAboutToHide()
-{
- Q_Q(QQuickOverlay);
- QQuickPopup *popup = qobject_cast<QQuickPopup *>(q->sender());
- if (!popup || !popup->dim())
- return;
-
- // use QQmlProperty instead of QQuickItem::setOpacity() to trigger QML Behaviors
- QQuickPopupPrivate *p = QQuickPopupPrivate::get(popup);
- if (p->dimmer)
- QQmlProperty::write(p->dimmer, QStringLiteral("opacity"), 0.0);
-}
-
-static QQuickItem *createDimmer(QQmlComponent *component, QQuickPopup *popup, QQuickItem *parent)
-{
- QQuickItem *item = nullptr;
- if (component) {
- QQmlContext *creationContext = component->creationContext();
- if (!creationContext)
- creationContext = qmlContext(parent);
- QQmlContext *context = new QQmlContext(creationContext, parent);
- context->setContextObject(popup);
- item = qobject_cast<QQuickItem*>(component->beginCreate(context));
- }
-
- // when there is no overlay component available (with plain QQuickWindow),
- // use a plain QQuickItem as a fallback to block hover events
- if (!item && popup->isModal())
- item = new QQuickItem;
-
- if (item) {
- item->setOpacity(popup->isVisible() ? 1.0 : 0.0);
- item->setParentItem(parent);
- item->stackBefore(popup->popupItem());
- item->setZ(popup->z());
- if (popup->isModal()) {
- item->setAcceptedMouseButtons(Qt::AllButtons);
-#if QT_CONFIG(cursor)
- item->setCursor(Qt::ArrowCursor);
-#endif
-#if QT_CONFIG(quicktemplates2_hover)
- // TODO: switch to QStyleHints::useHoverEffects in Qt 5.8
- item->setAcceptHoverEvents(true);
- // item->setAcceptHoverEvents(QGuiApplication::styleHints()->useHoverEffects());
- // connect(QGuiApplication::styleHints(), &QStyleHints::useHoverEffectsChanged, item, &QQuickItem::setAcceptHoverEvents);
-#endif
- }
- if (component)
- component->completeCreate();
- }
- return item;
-}
-
-void QQuickOverlayPrivate::createOverlay(QQuickPopup *popup)
-{
- Q_Q(QQuickOverlay);
- QQuickPopupPrivate *p = QQuickPopupPrivate::get(popup);
- if (!p->dimmer)
- p->dimmer = createDimmer(popup->isModal() ? modal : modeless, popup, q);
- p->resizeOverlay();
-}
-
-void QQuickOverlayPrivate::destroyOverlay(QQuickPopup *popup)
-{
- QQuickPopupPrivate *p = QQuickPopupPrivate::get(popup);
- if (p->dimmer) {
- p->dimmer->setParentItem(nullptr);
- p->dimmer->deleteLater();
- p->dimmer = nullptr;
- }
-}
-
-void QQuickOverlayPrivate::toggleOverlay()
-{
- Q_Q(QQuickOverlay);
- QQuickPopup *popup = qobject_cast<QQuickPopup *>(q->sender());
- if (!popup)
- return;
-
- destroyOverlay(popup);
- if (popup->dim())
- createOverlay(popup);
-}
-
QVector<QQuickPopup *> QQuickOverlayPrivate::stackingOrderPopups() const
{
const QList<QQuickItem *> children = paintOrderChildItems();
@@ -423,32 +326,8 @@ void QQuickOverlay::itemChange(ItemChange change, const ItemChangeData &data)
Q_D(QQuickOverlay);
QQuickItem::itemChange(change, data);
- QQuickPopup *popup = nullptr;
- if (change == ItemChildAddedChange || change == ItemChildRemovedChange) {
- popup = qobject_cast<QQuickPopup *>(data.item->parent());
+ if (change == ItemChildAddedChange || change == ItemChildRemovedChange)
setVisible(!d->allDrawers.isEmpty() || !childItems().isEmpty());
- }
- if (!popup)
- return;
-
- if (change == ItemChildAddedChange) {
- if (popup->dim())
- d->createOverlay(popup);
- QObjectPrivate::connect(popup, &QQuickPopup::dimChanged, d, &QQuickOverlayPrivate::toggleOverlay);
- QObjectPrivate::connect(popup, &QQuickPopup::modalChanged, d, &QQuickOverlayPrivate::toggleOverlay);
- if (!qobject_cast<QQuickDrawer *>(popup)) {
- QObjectPrivate::connect(popup, &QQuickPopup::aboutToShow, d, &QQuickOverlayPrivate::popupAboutToShow);
- QObjectPrivate::connect(popup, &QQuickPopup::aboutToHide, d, &QQuickOverlayPrivate::popupAboutToHide);
- }
- } else if (change == ItemChildRemovedChange) {
- d->destroyOverlay(popup);
- QObjectPrivate::disconnect(popup, &QQuickPopup::dimChanged, d, &QQuickOverlayPrivate::toggleOverlay);
- QObjectPrivate::disconnect(popup, &QQuickPopup::modalChanged, d, &QQuickOverlayPrivate::toggleOverlay);
- if (!qobject_cast<QQuickDrawer *>(popup)) {
- QObjectPrivate::disconnect(popup, &QQuickPopup::aboutToShow, d, &QQuickOverlayPrivate::popupAboutToShow);
- QObjectPrivate::disconnect(popup, &QQuickPopup::aboutToHide, d, &QQuickOverlayPrivate::popupAboutToHide);
- }
- }
}
void QQuickOverlay::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
diff --git a/src/quicktemplates2/qquickoverlay_p_p.h b/src/quicktemplates2/qquickoverlay_p_p.h
index 05177a49..772b9b09 100644
--- a/src/quicktemplates2/qquickoverlay_p_p.h
+++ b/src/quicktemplates2/qquickoverlay_p_p.h
@@ -84,13 +84,6 @@ public:
void removePopup(QQuickPopup *popup);
void setMouseGrabberPopup(QQuickPopup *popup);
- void popupAboutToShow();
- void popupAboutToHide();
-
- void createOverlay(QQuickPopup *popup);
- void destroyOverlay(QQuickPopup *popup);
- void toggleOverlay();
-
QVector<QQuickPopup *> stackingOrderPopups() const;
QVector<QQuickDrawer *> stackingOrderDrawers() const;
diff --git a/src/quicktemplates2/qquickpopup.cpp b/src/quicktemplates2/qquickpopup.cpp
index 18579b90..1ccd1040 100644
--- a/src/quicktemplates2/qquickpopup.cpp
+++ b/src/quicktemplates2/qquickpopup.cpp
@@ -437,6 +437,9 @@ bool QQuickPopupPrivate::prepareEnterTransition()
if (transitionState != EnterTransition) {
popupItem->setParentItem(QQuickOverlay::overlay(window));
+ if (dim)
+ createOverlay();
+ showOverlay();
emit q->aboutToShow();
visible = true;
transitionState = EnterTransition;
@@ -461,6 +464,7 @@ bool QQuickPopupPrivate::prepareExitTransition()
popupItem->setFocus(false);
}
transitionState = ExitTransition;
+ hideOverlay();
emit q->aboutToHide();
emit q->openedChanged();
}
@@ -483,6 +487,7 @@ void QQuickPopupPrivate::finalizeExitTransition()
positioner->setParentItem(nullptr);
popupItem->setParentItem(nullptr);
popupItem->setVisible(false);
+ destroyOverlay();
if (hadActiveFocusBeforeExitTransition && window) {
if (!qobject_cast<QQuickPopupItem *>(window->activeFocusItem())) {
@@ -603,6 +608,88 @@ void QQuickPopupPrivate::reposition()
positioner->reposition();
}
+static QQuickItem *createDimmer(QQmlComponent *component, QQuickPopup *popup, QQuickItem *parent)
+{
+ QQuickItem *item = nullptr;
+ if (component) {
+ QQmlContext *creationContext = component->creationContext();
+ if (!creationContext)
+ creationContext = qmlContext(parent);
+ QQmlContext *context = new QQmlContext(creationContext, parent);
+ context->setContextObject(popup);
+ item = qobject_cast<QQuickItem*>(component->beginCreate(context));
+ }
+
+ // when there is no overlay component available (with plain QQuickWindow),
+ // use a plain QQuickItem as a fallback to block hover events
+ if (!item && popup->isModal())
+ item = new QQuickItem;
+
+ if (item) {
+ item->setOpacity(popup->isVisible() ? 1.0 : 0.0);
+ item->setParentItem(parent);
+ item->stackBefore(popup->popupItem());
+ item->setZ(popup->z());
+ if (popup->isModal()) {
+ item->setAcceptedMouseButtons(Qt::AllButtons);
+#if QT_CONFIG(cursor)
+ item->setCursor(Qt::ArrowCursor);
+#endif
+#if QT_CONFIG(quicktemplates2_hover)
+ // TODO: switch to QStyleHints::useHoverEffects in Qt 5.8
+ item->setAcceptHoverEvents(true);
+ // item->setAcceptHoverEvents(QGuiApplication::styleHints()->useHoverEffects());
+ // connect(QGuiApplication::styleHints(), &QStyleHints::useHoverEffectsChanged, item, &QQuickItem::setAcceptHoverEvents);
+#endif
+ }
+ if (component)
+ component->completeCreate();
+ }
+ return item;
+}
+
+void QQuickPopupPrivate::createOverlay()
+{
+ Q_Q(QQuickPopup);
+ QQuickOverlay *overlay = QQuickOverlay::overlay(window);
+ if (!overlay)
+ return;
+
+ if (!dimmer)
+ dimmer = createDimmer(modal ? overlay->modal() : overlay->modeless(), q, overlay);
+ resizeOverlay();
+}
+
+void QQuickPopupPrivate::destroyOverlay()
+{
+ if (dimmer) {
+ dimmer->setParentItem(nullptr);
+ dimmer->deleteLater();
+ dimmer = nullptr;
+ }
+}
+
+void QQuickPopupPrivate::toggleOverlay()
+{
+ destroyOverlay();
+ if (dim)
+ createOverlay();
+}
+
+void QQuickPopupPrivate::showOverlay()
+{
+ // use QQmlProperty instead of QQuickItem::setOpacity() to trigger QML Behaviors
+ if (dim && dimmer)
+ QQmlProperty::write(dimmer, QStringLiteral("opacity"), 1.0);
+}
+
+void QQuickPopupPrivate::hideOverlay()
+{
+ // use QQmlProperty instead of QQuickItem::setOpacity() to trigger QML Behaviors
+ if (dim && dimmer)
+ QQmlProperty::write(dimmer, QStringLiteral("opacity"), 0.0);
+}
+
void QQuickPopupPrivate::resizeOverlay()
{
if (!dimmer)
@@ -1660,6 +1747,8 @@ void QQuickPopup::setModal(bool modal)
if (d->modal == modal)
return;
d->modal = modal;
+ if (d->complete && d->visible)
+ d->toggleOverlay();
emit modalChanged();
if (!d->hasDim) {
@@ -1693,6 +1782,8 @@ void QQuickPopup::setDim(bool dim)
return;
d->dim = dim;
+ if (d->complete && d->visible)
+ d->toggleOverlay();
emit dimChanged();
}
diff --git a/src/quicktemplates2/qquickpopup_p_p.h b/src/quicktemplates2/qquickpopup_p_p.h
index 857bb6ae..ae0bf7cd 100644
--- a/src/quicktemplates2/qquickpopup_p_p.h
+++ b/src/quicktemplates2/qquickpopup_p_p.h
@@ -115,6 +115,12 @@ public:
#endif
virtual void reposition();
+
+ void createOverlay();
+ void destroyOverlay();
+ void toggleOverlay();
+ virtual void showOverlay();
+ virtual void hideOverlay();
virtual void resizeOverlay();
virtual bool prepareEnterTransition();