Port frameParent() to firstParentOfType()

This commit is contained in:
Sergio Martins
2022-01-07 14:59:33 +00:00
parent aea2bf971b
commit 6cef4cea2c
5 changed files with 26 additions and 14 deletions

View File

@@ -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<Frame>(watched)) {
if (f->isMDI())
f->raise();
}

View File

@@ -18,6 +18,7 @@
#include "FrameworkWidgetFactory.h"
#include "Config.h"
#include "MainWindowBase.h"
#include "MDILayoutWidget_p.h"
#include <QTimer>
#include <QWindowStateChangeEvent>
@@ -100,10 +101,7 @@ MainWindowBase *TitleBar::mainWindow() const
bool TitleBar::isMDI() const
{
if (auto mw = mainWindow())
return mw->isMDI();
return false;
return firstParentOfType<MDILayoutWidget>(this) != nullptr;
}
void TitleBar::updateButtons()

View File

@@ -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

View File

@@ -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 <typename T>
inline T* firstParentOfType(const QObject *child)
{
auto p = from;
auto p = const_cast<QObject *>(child);
while (p) {
if (auto frame = qobject_cast<Frame *>(p))
return frame;
if (qobject_cast<const QWindow*>(p)) {
// Ignore QObject hierarchies spanning though multiple windows
return nullptr;
}
if (auto candidate = qobject_cast<T *>(p))
return candidate;
p = p->parent();
}

View File

@@ -7526,4 +7526,14 @@ void TestDocks::tst_mdi_mixed_with_docking()
mdiArea->addDockWidget(mdiWidget1, QPoint(10, 10));
mdiArea->addDockWidget(mdiWidget2, QPoint(50, 50));
}
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());
}