From de5925b15909cb38e20ce79243dcd31db74e6978 Mon Sep 17 00:00:00 2001 From: Axel Spoerl Date: Wed, 15 Nov 2023 11:19:11 +0100 Subject: [PATCH] QDockAreaLayout: implement widget based add() and remove() The item_list of a QDockAreaLayoutInfo has abstraction methods for reading the item list. Adding to and removing from the item list is done directly, by using the QList api. Implement an abstraction, that takes a QWidget *. The argument may either be a QDockWidgetGroupWindow or a QDockWidget. Task-number: QTBUG-118578 Task-number: QTBUG-118579 Pick-to: 6.5 Change-Id: Ib2ccd7557a21a43b68f184fe4575018f2a97004b Reviewed-by: David Faure (cherry picked from commit 2c96f517714eb67a9af821141a90eed68b8714ed) Reviewed-by: Qt Cherry-pick Bot --- src/widgets/widgets/qdockarealayout.cpp | 31 +++++++++++++++++++++-- src/widgets/widgets/qdockarealayout_p.h | 2 ++ src/widgets/widgets/qmainwindowlayout.cpp | 2 +- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/widgets/widgets/qdockarealayout.cpp b/src/widgets/widgets/qdockarealayout.cpp index 27eed4075f9..da0e9871715 100644 --- a/src/widgets/widgets/qdockarealayout.cpp +++ b/src/widgets/widgets/qdockarealayout.cpp @@ -1018,6 +1018,14 @@ void QDockAreaLayoutInfo::remove(const QList &path) } } +void QDockAreaLayoutInfo::remove(QWidget *widget) +{ + const QList path = indexOf(widget); + if (path.isEmpty()) + return; + remove(path); +} + QLayoutItem *QDockAreaLayoutInfo::plug(const QList &path) { Q_ASSERT(!path.isEmpty()); @@ -1148,8 +1156,6 @@ bool QDockAreaLayoutInfo::insertGap(const QList &path, QLayoutItem *dockWid index = -index - 1; } -// dump(qDebug() << "insertGap() before:" << index << tabIndex, *this, QString()); - if (path.size() > 1) { QDockAreaLayoutItem &item = item_list[index]; @@ -1778,6 +1784,26 @@ QLayoutItem *QDockAreaLayoutInfo::takeAt(int *x, int index) return nullptr; } +// Add a dock widget or dock widget group window to the item list +void QDockAreaLayoutInfo::add(QWidget *widget) +{ + // Do not add twice + if (!indexOf(widget).isEmpty()) + return; + + if (auto *dockWidget = qobject_cast(widget)) { + item_list.append(QDockAreaLayoutItem(new QDockWidgetItem(dockWidget))); + return; + } + + if (auto *groupWindow = qobject_cast(widget)) { + item_list.append(QDockAreaLayoutItem(new QDockWidgetGroupWindowItem(groupWindow))); + return; + } + + qFatal("Coding error. Add supports only QDockWidget and QDockWidgetGroupWindow"); +} + void QDockAreaLayoutInfo::deleteAllLayoutItems() { for (int i = 0; i < item_list.size(); ++i) { @@ -1971,6 +1997,7 @@ bool QDockAreaLayoutInfo::restoreState(QDataStream &stream, QList if (testing) { //was it is not really added to the layout, we need to delete the object here delete item.widgetItem; + item.widgetItem = nullptr; } } } else if (nextMarker == SequenceMarker) { diff --git a/src/widgets/widgets/qdockarealayout_p.h b/src/widgets/widgets/qdockarealayout_p.h index 7f89a09b58b..bf7e4682e5d 100644 --- a/src/widgets/widgets/qdockarealayout_p.h +++ b/src/widgets/widgets/qdockarealayout_p.h @@ -109,6 +109,7 @@ public: QList gapIndex(const QPoint &pos, bool nestingEnabled, TabMode tabMode) const; void remove(const QList &path); + void remove(QWidget *widget); void unnest(int index); void split(int index, Qt::Orientation orientation, QLayoutItem *dockWidgetItem); #if QT_CONFIG(tabbar) @@ -156,6 +157,7 @@ public: QLayoutItem *itemAt(int *x, int index) const; QLayoutItem *takeAt(int *x, int index); + void add(QWidget *widget); void deleteAllLayoutItems(); QMainWindowLayout *mainWindowLayout() const; diff --git a/src/widgets/widgets/qmainwindowlayout.cpp b/src/widgets/widgets/qmainwindowlayout.cpp index be22d11ffe4..5df36fa8161 100644 --- a/src/widgets/widgets/qmainwindowlayout.cpp +++ b/src/widgets/widgets/qmainwindowlayout.cpp @@ -1224,7 +1224,7 @@ bool QMainWindowLayoutState::restoreState(QDataStream &_stream, if (info == nullptr) { continue; } - info->item_list.append(QDockAreaLayoutItem(new QDockWidgetItem(w))); + info->add(w); } } }