From 6cef4cea2c599b3925ee06464254acfe806e3ebf Mon Sep 17 00:00:00 2001 From: Sergio Martins Date: Fri, 7 Jan 2022 14:59:33 +0000 Subject: [PATCH] Port frameParent() to firstParentOfType() --- src/private/DockRegistry.cpp | 2 +- src/private/TitleBar.cpp | 6 ++---- src/private/TitleBar_p.h | 5 ++--- src/private/Utils_p.h | 15 ++++++++++----- tests/tst_docks.cpp | 12 +++++++++++- 5 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/private/DockRegistry.cpp b/src/private/DockRegistry.cpp index c73487d1..0d94037e 100644 --- a/src/private/DockRegistry.cpp +++ b/src/private/DockRegistry.cpp @@ -711,7 +711,7 @@ bool DockRegistry::eventFilter(QObject *watched, QEvent *event) } } else if (event->type() == QEvent::MouseButtonPress) { // When clicking on a MDI Frame we raise the window - if (Frame *f = parentFrame(watched)) { + if (Frame *f = firstParentOfType(watched)) { if (f->isMDI()) f->raise(); } diff --git a/src/private/TitleBar.cpp b/src/private/TitleBar.cpp index b22e17de..1b02e3a1 100644 --- a/src/private/TitleBar.cpp +++ b/src/private/TitleBar.cpp @@ -18,6 +18,7 @@ #include "FrameworkWidgetFactory.h" #include "Config.h" #include "MainWindowBase.h" +#include "MDILayoutWidget_p.h" #include #include @@ -100,10 +101,7 @@ MainWindowBase *TitleBar::mainWindow() const bool TitleBar::isMDI() const { - if (auto mw = mainWindow()) - return mw->isMDI(); - - return false; + return firstParentOfType(this) != nullptr; } void TitleBar::updateButtons() diff --git a/src/private/TitleBar_p.h b/src/private/TitleBar_p.h index 3c8a6aee..0c9820db 100644 --- a/src/private/TitleBar_p.h +++ b/src/private/TitleBar_p.h @@ -112,9 +112,8 @@ public: ///Returns nullptr otherwise MainWindowBase *mainWindow() const; - /// @brief Returns if this title bar is in a main window in MDI mode - /// By default false. Only relevant if your main window was constructed with the - /// MainWindowOption_MDI option + /// @brief Returns if this title bar belongs to a dock widget which is in MDI mode (inside a MDI area) + /// For example in a main window created with MainWindowOption_MDI option bool isMDI() const override; /// @brief updates the close button enabled state diff --git a/src/private/Utils_p.h b/src/private/Utils_p.h index eb455486..226d5030 100644 --- a/src/private/Utils_p.h +++ b/src/private/Utils_p.h @@ -362,13 +362,18 @@ inline bool scalingFactorIsSupported(qreal factor) #endif } -/// @brief Returns the parent frame which the specified object is in, if any -inline Frame *parentFrame(QObject *from) +template +inline T* firstParentOfType(const QObject *child) { - auto p = from; + auto p = const_cast(child); while (p) { - if (auto frame = qobject_cast(p)) - return frame; + if (qobject_cast(p)) { + // Ignore QObject hierarchies spanning though multiple windows + return nullptr; + } + + if (auto candidate = qobject_cast(p)) + return candidate; p = p->parent(); } diff --git a/tests/tst_docks.cpp b/tests/tst_docks.cpp index d27c084e..f65fa7e6 100644 --- a/tests/tst_docks.cpp +++ b/tests/tst_docks.cpp @@ -7526,4 +7526,14 @@ void TestDocks::tst_mdi_mixed_with_docking() mdiArea->addDockWidget(mdiWidget1, QPoint(10, 10)); mdiArea->addDockWidget(mdiWidget2, QPoint(50, 50)); -} \ No newline at end of file + + Frame *frameMDI1 = mdiWidget1->d->frame(); + Frame *frame1 = dock1->d->frame(); + QVERIFY(!frame1->isMDI()); + QVERIFY(frameMDI1->isMDI()); + QVERIFY(!frame1->mdiLayoutWidget()); + QVERIFY(frameMDI1->mdiLayoutWidget()); + + QVERIFY(!dock1->titleBar()->isMDI()); + QVERIFY(mdiWidget1->titleBar()->isMDI()); +}