diff options
Diffstat (limited to 'src/widgets/kernel/qwidgetwindow.cpp')
-rw-r--r-- | src/widgets/kernel/qwidgetwindow.cpp | 102 |
1 files changed, 77 insertions, 25 deletions
diff --git a/src/widgets/kernel/qwidgetwindow.cpp b/src/widgets/kernel/qwidgetwindow.cpp index 7756893369..03dde9ca69 100644 --- a/src/widgets/kernel/qwidgetwindow.cpp +++ b/src/widgets/kernel/qwidgetwindow.cpp @@ -39,21 +39,25 @@ public: void setVisible(bool visible) override { Q_Q(QWidgetWindow); + qCDebug(lcWidgetShowHide) << "Setting visibility of" << q->widget() + << "to" << visible << "via QWidgetWindowPrivate"; + if (QWidget *widget = q->widget()) { - // Check if the widget was already hidden, as this indicates it was done - // explicitly and not because the parent window in this case made it hidden. - // In which case do not automatically show the widget when the parent - // window is shown. - const bool wasExplicitShowHide = widget->testAttribute(Qt::WA_WState_ExplicitShowHide); - const bool wasHidden = widget->testAttribute(Qt::WA_WState_Hidden); - QWidgetPrivate::get(widget)->setVisible(visible); - if (wasExplicitShowHide) { - widget->setAttribute(Qt::WA_WState_ExplicitShowHide, wasExplicitShowHide); - widget->setAttribute(Qt::WA_WState_Hidden, wasHidden); - } - } else { - QWindowPrivate::setVisible(visible); + // If the widget's visible state is already matching the new QWindow + // visible state we assume the widget has already synced up. + if (visible != widget->isVisible()) + QWidgetPrivate::get(widget)->setVisible(visible); } + + // If we end up calling QWidgetPrivate::setVisible() above, we will + // in most cases recurse back into setNativeWindowVisibility() to + // update the QWindow state. But during QWidget::destroy() this is + // not the case, as Qt::WA_WState_Created has been unset by the time + // we check if we should call hide_helper(). We handle this case, as + // well as the cases where we don't call QWidgetPrivate::setVisible(), + // by syncing up the QWindow state here if needed. + if (q->isVisible() != visible) + QWindowPrivate::setVisible(visible); } QWindow *eventReceiver() override { @@ -73,6 +77,39 @@ public: widget->focusWidget()->clearFocus(); } + void setFocusToTarget(FocusTarget target, Qt::FocusReason reason) override + { + Q_Q(QWidgetWindow); + QWidget *widget = q->widget(); + if (!widget) + return; + QWidget *newFocusWidget = nullptr; + + switch (target) { + case FocusTarget::First: + newFocusWidget = q->getFocusWidget(QWidgetWindow::FirstFocusWidget); + break; + case FocusTarget::Last: + newFocusWidget = q->getFocusWidget(QWidgetWindow::LastFocusWidget); + break; + case FocusTarget::Next: { + QWidget *focusWidget = widget->focusWidget() ? widget->focusWidget() : widget; + newFocusWidget = focusWidget->nextInFocusChain() ? focusWidget->nextInFocusChain() : focusWidget; + break; + } + case FocusTarget::Prev: { + QWidget *focusWidget = widget->focusWidget() ? widget->focusWidget() : widget; + newFocusWidget = focusWidget->previousInFocusChain() ? focusWidget->previousInFocusChain() : focusWidget; + break; + } + default: + break; + } + + if (newFocusWidget) + newFocusWidget->setFocus(reason); + } + QRectF closestAcceptableGeometry(const QRectF &rect) const override; void processSafeAreaMarginsChanged() override @@ -134,6 +171,21 @@ QWidgetWindow::QWidgetWindow(QWidget *widget) QWidgetWindow::~QWidgetWindow() { + if (!m_widget) + return; + + QTLWExtra *topData = QWidgetPrivate::get(m_widget)->topData(); + Q_ASSERT(topData); + + // The QPlaformBackingStore may hold a reference to the window, + // so the backingstore needs to be deleted first. + topData->repaintManager.reset(nullptr); + delete topData->backingStore; + topData->backingStore = nullptr; + topData->widgetTextures.clear(); + + // Too late to do anything beyond this point + topData->window = nullptr; } #if QT_CONFIG(accessibility) @@ -170,6 +222,9 @@ QObject *QWidgetWindow::focusObject() const void QWidgetWindow::setNativeWindowVisibility(bool visible) { Q_D(QWidgetWindow); + qCDebug(lcWidgetShowHide) << "Setting visibility of" << this + << "to" << visible << "via QWidgetWindow::setNativeWindowVisibility"; + // Call base class setVisible() implementation to run the QWindow // visibility logic. Don't call QWidgetWindowPrivate::setVisible() // since that will recurse back into QWidget code. @@ -450,16 +505,13 @@ void QWidgetWindow::handleNonClientAreaMouseEvent(QMouseEvent *e) void QWidgetWindow::handleMouseEvent(QMouseEvent *event) { - static const QEvent::Type contextMenuTrigger = - QGuiApplicationPrivate::platformTheme()->themeHint(QPlatformTheme::ContextMenuOnMouseRelease).toBool() ? - QEvent::MouseButtonRelease : QEvent::MouseButtonPress; if (QApplicationPrivate::inPopupMode()) { QPointer<QWidget> activePopupWidget = QApplication::activePopupWidget(); - QPoint mapped = event->position().toPoint(); + QPointF mapped = event->position(); if (activePopupWidget != m_widget) - mapped = activePopupWidget->mapFromGlobal(event->globalPosition().toPoint()); + mapped = activePopupWidget->mapFromGlobal(event->globalPosition()); bool releaseAfter = false; - QWidget *popupChild = activePopupWidget->childAt(mapped); + QWidget *popupChild = activePopupWidget->childAt(mapped.toPoint()); if (activePopupWidget != qt_popup_down) { qt_button_down = nullptr; @@ -486,15 +538,15 @@ void QWidgetWindow::handleMouseEvent(QMouseEvent *event) // deliver event qt_replay_popup_mouse_event = false; QPointer<QWidget> receiver = activePopupWidget; - QPoint widgetPos = mapped; + QPointF widgetPos = mapped; if (qt_button_down) receiver = qt_button_down; else if (popupChild) receiver = popupChild; if (receiver != activePopupWidget) - widgetPos = receiver->mapFromGlobal(event->globalPosition().toPoint()); + widgetPos = receiver->mapFromGlobal(event->globalPosition()); - const bool reallyUnderMouse = activePopupWidget->rect().contains(mapped); + const bool reallyUnderMouse = activePopupWidget->rect().contains(mapped.toPoint()); const bool underMouse = activePopupWidget->underMouse(); if (underMouse != reallyUnderMouse) { if (reallyUnderMouse) { @@ -569,7 +621,7 @@ void QWidgetWindow::handleMouseEvent(QMouseEvent *event) } qt_replay_popup_mouse_event = false; #ifndef QT_NO_CONTEXTMENU - } else if (event->type() == contextMenuTrigger + } else if (event->type() == QGuiApplicationPrivate::contextMenuEventType() && event->button() == Qt::RightButton && (openPopupCount == oldOpenPopupCount)) { QWidget *receiver = activePopupWidget; @@ -582,7 +634,6 @@ void QWidgetWindow::handleMouseEvent(QMouseEvent *event) QApplication::forwardEvent(receiver, &e, event); } #else - Q_UNUSED(contextMenuTrigger); Q_UNUSED(oldOpenPopupCount); } #endif @@ -629,7 +680,8 @@ void QWidgetWindow::handleMouseEvent(QMouseEvent *event) event->setAccepted(translated.isAccepted()); } #ifndef QT_NO_CONTEXTMENU - if (event->type() == contextMenuTrigger && event->button() == Qt::RightButton + if (event->type() == QGuiApplicationPrivate::contextMenuEventType() + && event->button() == Qt::RightButton && m_widget->rect().contains(event->position().toPoint())) { QContextMenuEvent e(QContextMenuEvent::Mouse, mapped, event->globalPosition().toPoint(), event->modifiers()); QGuiApplication::forwardEvent(receiver, &e, event); |