From 02a13a86fab1f928d5dd40069d121ded549366f1 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Wed, 4 Mar 2015 15:07:53 +0100 Subject: [PATCH] Allow for rearranging tabified QDockWidgets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Allow to drag and drop tabs [ChangeLog][QtWidgets][Important Behavior Changes] The tabs for the tabified docks can be moved by the user. Task-number: QTBUG-1049 Task-number: QTBUG-2295 Task-number: QTBUG-4532 Task-number: QTBUG-18883 Task-number: QTBUG-35148 Change-Id: I7ef9d4987db081654bd5d648e14370b3d381a720 Reviewed-by: Friedemann Kleint Reviewed-by: Jørgen Lind Reviewed-by: Paul Olav Tvete --- src/widgets/widgets/qdockarealayout.cpp | 16 ++++++++++++++++ src/widgets/widgets/qdockarealayout_p.h | 3 +++ src/widgets/widgets/qmainwindowlayout.cpp | 12 ++++++++++++ src/widgets/widgets/qmainwindowlayout_p.h | 1 + 4 files changed, 32 insertions(+) diff --git a/src/widgets/widgets/qdockarealayout.cpp b/src/widgets/widgets/qdockarealayout.cpp index e71b9616fc..af0f2515fe 100644 --- a/src/widgets/widgets/qdockarealayout.cpp +++ b/src/widgets/widgets/qdockarealayout.cpp @@ -2244,6 +2244,22 @@ QRect QDockAreaLayoutInfo::tabContentRect() const return result; } + +int QDockAreaLayoutInfo::tabIndexToListIndex(int tabIndex) const +{ + Q_ASSERT(tabbed && tabBar); + quintptr data = qvariant_cast(tabBar->tabData(tabIndex)); + for (int i = 0; i < item_list.count(); ++i) { + if (tabId(item_list.at(i)) == data) + return i; + } + return -1; +} + +void QDockAreaLayoutInfo::moveTab(int from, int to) +{ + item_list.move(tabIndexToListIndex(from), tabIndexToListIndex(to)); +} #endif // QT_NO_TABBAR /****************************************************************************** diff --git a/src/widgets/widgets/qdockarealayout_p.h b/src/widgets/widgets/qdockarealayout_p.h index 7c67466c7b..64fae7ebf6 100644 --- a/src/widgets/widgets/qdockarealayout_p.h +++ b/src/widgets/widgets/qdockarealayout_p.h @@ -208,6 +208,9 @@ public: QSize tabBarSizeHint() const; QSet usedTabBars() const; + + int tabIndexToListIndex(int) const; + void moveTab(int from, int to); #endif // QT_NO_TABBAR }; diff --git a/src/widgets/widgets/qmainwindowlayout.cpp b/src/widgets/widgets/qmainwindowlayout.cpp index 046666f571..3620aa04fc 100644 --- a/src/widgets/widgets/qmainwindowlayout.cpp +++ b/src/widgets/widgets/qmainwindowlayout.cpp @@ -1280,7 +1280,9 @@ QTabBar *QMainWindowLayout::getTabBar() result->setDrawBase(true); result->setElideMode(Qt::ElideRight); result->setDocumentMode(_documentMode); + result->setMovable(true); connect(result, SIGNAL(currentChanged(int)), this, SLOT(tabChanged())); + connect(result, &QTabBar::tabMoved, this, &QMainWindowLayout::tabMoved); } usedTabBars.insert(result); @@ -1316,6 +1318,16 @@ void QMainWindowLayout::tabChanged() if (QWidget *w = centralWidget()) w->raise(); } + +void QMainWindowLayout::tabMoved(int from, int to) +{ + QTabBar *tb = qobject_cast(sender()); + Q_ASSERT(tb); + QDockAreaLayoutInfo *info = layoutState.dockAreaLayout.info(tb); + Q_ASSERT(info); + + info->moveTab(from, to); +} #endif // QT_NO_TABBAR bool QMainWindowLayout::startSeparatorMove(const QPoint &pos) diff --git a/src/widgets/widgets/qmainwindowlayout_p.h b/src/widgets/widgets/qmainwindowlayout_p.h index 9f84ee95db..8ccb4d303f 100644 --- a/src/widgets/widgets/qmainwindowlayout_p.h +++ b/src/widgets/widgets/qmainwindowlayout_p.h @@ -286,6 +286,7 @@ private Q_SLOTS: #ifndef QT_NO_DOCKWIDGET #ifndef QT_NO_TABBAR void tabChanged(); + void tabMoved(int from, int to); #endif #endif private: