Skip to content

Commit e37e4fd

Browse files
Added context menu for dock area title bar to enable closing of area and other areas via context menu and to enable detaching of dock area via context menu
1 parent c9a9753 commit e37e4fd

12 files changed

+74
-21
lines changed

src/DockAreaTabBar.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,11 @@ void CDockAreaTabBar::mouseDoubleClickEvent(QMouseEvent *event)
233233

234234

235235
//============================================================================
236-
CFloatingDockContainer* CDockAreaTabBar::makeAreaFloating(const QPoint& Pos)
236+
CFloatingDockContainer* CDockAreaTabBar::makeAreaFloating(const QPoint& Offset)
237237
{
238238
QSize Size = d->DockArea->size();
239239
CFloatingDockContainer* FloatingWidget = new CFloatingDockContainer(d->DockArea);
240-
FloatingWidget->startFloating(Pos, Size);
240+
FloatingWidget->startFloating(Offset, Size);
241241
auto TopLevelDockWidget = FloatingWidget->topLevelDockWidget();
242242
if (TopLevelDockWidget)
243243
{
@@ -249,9 +249,9 @@ CFloatingDockContainer* CDockAreaTabBar::makeAreaFloating(const QPoint& Pos)
249249

250250

251251
//============================================================================
252-
void CDockAreaTabBar::startFloating(const QPoint& Pos)
252+
void CDockAreaTabBar::startFloating(const QPoint& Offset)
253253
{
254-
d->FloatingWidget = makeAreaFloating(Pos);
254+
d->FloatingWidget = makeAreaFloating(Offset);
255255
}
256256

257257

src/DockAreaTabBar.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@ private slots:
8484
/**
8585
* Starts floating
8686
*/
87-
void startFloating(const QPoint& Pos);
87+
void startFloating(const QPoint& Offset);
8888

8989
/**
90-
* Makes the dock area loating
90+
* Makes the dock area floating
9191
*/
92-
CFloatingDockContainer* makeAreaFloating(const QPoint& Pos);
92+
CFloatingDockContainer* makeAreaFloating(const QPoint& Offset);
9393

9494

9595
public:

src/DockAreaTitleBar.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "DockWidgetTab.h"
4848
#include "DockAreaTabBar.h"
4949

50+
#include <iostream>
5051

5152
namespace ads
5253
{
@@ -173,6 +174,10 @@ void DockAreaTitleBarPrivate::createTabBar()
173174
_this->connect(TabBar, SIGNAL(tabMoved(int, int)), SLOT(markTabsMenuOutdated()));
174175
_this->connect(TabBar, SIGNAL(currentChanged(int)), SLOT(onCurrentTabChanged(int)));
175176
_this->connect(TabBar, SIGNAL(tabBarClicked(int)), SIGNAL(tabBarClicked(int)));
177+
178+
TabBar->setContextMenuPolicy(Qt::CustomContextMenu);
179+
_this->connect(TabBar, SIGNAL(customContextMenuRequested(const QPoint&)),
180+
SLOT(showContextMenu(const QPoint&)));
176181
}
177182

178183

@@ -310,6 +315,19 @@ void CDockAreaTitleBar::setVisible(bool Visible)
310315
}
311316

312317

318+
//============================================================================
319+
void CDockAreaTitleBar::showContextMenu(const QPoint& pos)
320+
{
321+
QMenu Menu(this);
322+
Menu.addAction(tr("Detach Area"), this, SLOT(onUndockButtonClicked()));
323+
Menu.addSeparator();
324+
auto Action = Menu.addAction(tr("Close Area"), this, SLOT(onCloseButtonClicked()));
325+
Action->setEnabled(d->DockArea->features().testFlag(CDockWidget::DockWidgetClosable));
326+
Menu.addAction(tr("Close Other Areas"), d->DockArea, SLOT(closeOtherAreas()));
327+
Menu.exec(mapToGlobal(pos));
328+
}
329+
330+
313331
} // namespace ads
314332

315333
//---------------------------------------------------------------------------

src/DockAreaTitleBar.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ private slots:
5959
void onUndockButtonClicked();
6060
void onTabsMenuActionTriggered(QAction* Action);
6161
void onCurrentTabChanged(int Index);
62+
void showContextMenu(const QPoint& pos);
6263

6364
public:
6465
using Super = QFrame;

src/DockAreaWidget.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,13 @@ void CDockAreaWidget::closeArea()
779779
DockWidget->toggleView(false);
780780
}
781781
}
782+
783+
784+
//============================================================================
785+
void CDockAreaWidget::closeOtherAreas()
786+
{
787+
dockContainer()->closeOtherAreas(this);
788+
}
782789
} // namespace ads
783790

784791
//---------------------------------------------------------------------------

src/DockAreaWidget.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,11 @@ public slots:
259259
*/
260260
void closeArea();
261261

262+
/**
263+
* This function closes all other areas except of this area
264+
*/
265+
void closeOtherAreas();
266+
262267
signals:
263268
/**
264269
* This signal is emitted when user clicks on a tab at an index.

src/DockContainerWidget.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,6 +1378,19 @@ CFloatingDockContainer* CDockContainerWidget::floatingWidget() const
13781378
}
13791379

13801380

1381+
//============================================================================
1382+
void CDockContainerWidget::closeOtherAreas(CDockAreaWidget* KeepOpenArea)
1383+
{
1384+
for (const auto DockArea : d->DockAreas)
1385+
{
1386+
if (DockArea != KeepOpenArea && DockArea->features().testFlag(CDockWidget::DockWidgetClosable))
1387+
{
1388+
DockArea->closeArea();
1389+
}
1390+
}
1391+
}
1392+
1393+
13811394
} // namespace ads
13821395

13831396
#include "moc_DockContainerWidget.cpp"

src/DockContainerWidget.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@ class ADS_EXPORT CDockContainerWidget : public QFrame
233233
*/
234234
CFloatingDockContainer* floatingWidget() const;
235235

236+
/**
237+
* Call this function to close all dock areas except the KeepOpenArea
238+
*/
239+
void closeOtherAreas(CDockAreaWidget* KeepOpenArea);
240+
236241
signals:
237242
/**
238243
* This signal is emitted if one or multiple dock areas has been added to

src/DockManager.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,7 @@ void CDockManager::setConfigFlags(const ConfigFlags Flags)
759759
d->ConfigFlags = Flags;
760760
}
761761

762+
762763
} // namespace ads
763764

764765
//---------------------------------------------------------------------------

src/DockWidgetTab.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ struct DockWidgetTabPrivate
123123
* Returns true, if floating has been started and false if floating
124124
* is not possible for any reason
125125
*/
126-
bool startFloating();
126+
bool startFloating(eDragState DraggingState = DraggingFloatingWidget);
127127

128128
/**
129129
* Returns true if the given config flag is set
@@ -197,7 +197,7 @@ void DockWidgetTabPrivate::moveTab(QMouseEvent* ev)
197197

198198

199199
//============================================================================
200-
bool DockWidgetTabPrivate::startFloating()
200+
bool DockWidgetTabPrivate::startFloating(eDragState DraggingState)
201201
{
202202
auto dockContainer = DockWidget->dockContainer();
203203
qDebug() << "isFloating " << dockContainer->isFloating();
@@ -214,7 +214,7 @@ bool DockWidgetTabPrivate::startFloating()
214214
}
215215

216216
qDebug() << "startFloating";
217-
DragState = DraggingFloatingWidget;
217+
DragState = DraggingState;
218218
QSize Size = DockArea->size();
219219
CFloatingDockContainer* FloatingWidget = nullptr;
220220
if (DockArea->dockWidgetsCount() > 1)
@@ -230,10 +230,13 @@ bool DockWidgetTabPrivate::startFloating()
230230
}
231231

232232
FloatingWidget->startFloating(DragStartMousePosition, Size);
233-
auto Overlay = DockWidget->dockManager()->containerOverlay();
234-
Overlay->setAllowedAreas(OuterDockAreas);
235-
this->FloatingWidget = FloatingWidget;
236-
DockWidget->emitTopLevelChanged(true);
233+
if (DraggingFloatingWidget == DraggingState)
234+
{
235+
auto Overlay = DockWidget->dockManager()->containerOverlay();
236+
Overlay->setAllowedAreas(OuterDockAreas);
237+
this->FloatingWidget = FloatingWidget;
238+
}
239+
DockWidget->emitTopLevelChanged(true);
237240
return true;
238241
}
239242

@@ -471,7 +474,7 @@ void CDockWidgetTab::mouseDoubleClickEvent(QMouseEvent *event)
471474
if (!d->DockArea->dockContainer()->isFloating() || d->DockArea->dockWidgetsCount() > 1)
472475
{
473476
d->DragStartMousePosition = event->pos();
474-
d->startFloating();
477+
d->startFloating(DraggingInactive);
475478
}
476479

477480
Super::mouseDoubleClickEvent(event);
@@ -497,7 +500,7 @@ bool CDockWidgetTab::isClosable() const
497500
void CDockWidgetTab::onDetachActionTriggered()
498501
{
499502
d->DragStartMousePosition = mapFromGlobal(QCursor::pos());
500-
d->startFloating();
503+
d->startFloating(DraggingInactive);
501504
}
502505

503506
} // namespace ads

0 commit comments

Comments
 (0)