Skip to content

Commit 0de1a9c

Browse files
Properly implemented support for DockWidgetFloatable feature - now detaching a DockWidget or a DockAre that is not floatable is not possible (support for DockWidgetMovable feature is not implemented yet)
1 parent c45327a commit 0de1a9c

File tree

6 files changed

+72
-21
lines changed

6 files changed

+72
-21
lines changed

demo/MainWindow.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,32 @@ static ads::CDockWidget* createLongTextLabelDockWidget(QMenu* ViewMenu)
8888
}
8989

9090

91+
/**
92+
* Function returns a features string with closable (c), movable (m) and floatable (f)
93+
* features. i.e. The following string is for a not closable but movable and floatable
94+
* widget: c- m+ f+
95+
*/
96+
static QString featuresString(ads::CDockWidget* DockWidget)
97+
{
98+
auto f = DockWidget->features();
99+
return QString("c%1 m%2 f%3")
100+
.arg(f.testFlag(ads::CDockWidget::DockWidgetClosable) ? "+" : "-")
101+
.arg(f.testFlag(ads::CDockWidget::DockWidgetMovable) ? "+" : "-")
102+
.arg(f.testFlag(ads::CDockWidget::DockWidgetFloatable) ? "+" : "-");
103+
}
104+
105+
106+
/**
107+
* Appends the string returned by featuresString() to the window title of
108+
* the given DockWidget
109+
*/
110+
static void appendFeaturStringToWindowTitle(ads::CDockWidget* DockWidget)
111+
{
112+
DockWidget->setWindowTitle(DockWidget->windowTitle()
113+
+ QString(" (%1)").arg(featuresString(DockWidget)));
114+
}
115+
116+
91117
//============================================================================
92118
static ads::CDockWidget* createCalendarDockWidget(QMenu* ViewMenu)
93119
{
@@ -110,7 +136,8 @@ static ads::CDockWidget* createFileSystemTreeDockWidget(QMenu* ViewMenu)
110136
QFileSystemModel* m = new QFileSystemModel(w);
111137
m->setRootPath(QDir::currentPath());
112138
w->setModel(m);
113-
ads::CDockWidget* DockWidget = new ads::CDockWidget(QString("Filesystem %1").arg(FileSystemCount++));
139+
ads::CDockWidget* DockWidget = new ads::CDockWidget(QString("Filesystem %1")
140+
.arg(FileSystemCount++));
114141
DockWidget->setWidget(w);
115142
ViewMenu->addAction(DockWidget->toggleViewAction());
116143
return DockWidget;
@@ -185,6 +212,8 @@ void MainWindowPrivate::createContent()
185212
ToolBar->addAction(ui.actionSaveState);
186213
ToolBar->addAction(ui.actionRestoreState);
187214
FileSystemWidget->setFeature(ads::CDockWidget::DockWidgetMovable, false);
215+
FileSystemWidget->setFeature(ads::CDockWidget::DockWidgetFloatable, false);
216+
appendFeaturStringToWindowTitle(FileSystemWidget);
188217
auto TopDockArea = DockManager->addDockWidget(ads::TopDockWidgetArea, FileSystemWidget);
189218
DockWidget = createCalendarDockWidget(ViewMenu);
190219
DockWidget->setFeature(ads::CDockWidget::DockWidgetClosable, false);

src/DockAreaTabBar.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,13 @@ void CDockAreaTabBar::mouseMoveEvent(QMouseEvent* ev)
205205
return;
206206
}
207207

208+
// If one single dock widget in this area is not floatable then the whole
209+
// area is not floatable
210+
if (!d->DockArea->features().testFlag(CDockWidget::DockWidgetFloatable))
211+
{
212+
return;
213+
}
214+
208215
int DragDistance = (d->DragStartMousePos - ev->pos()).manhattanLength();
209216
if (DragDistance >= CDockManager::startDragDistance())
210217
{
@@ -228,6 +235,11 @@ void CDockAreaTabBar::mouseDoubleClickEvent(QMouseEvent *event)
228235
{
229236
return;
230237
}
238+
239+
if (!d->DockArea->features().testFlag(CDockWidget::DockWidgetFloatable))
240+
{
241+
return;
242+
}
231243
makeAreaFloating(event->pos(), DraggingInactive);
232244
}
233245

src/DockAreaTitleBar.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,10 @@ void CDockAreaTitleBar::onCloseButtonClicked()
288288
//============================================================================
289289
void CDockAreaTitleBar::onUndockButtonClicked()
290290
{
291-
d->TabBar->makeAreaFloating(mapFromGlobal(QCursor::pos()), DraggingInactive);
291+
if (d->DockArea->features().testFlag(CDockWidget::DockWidgetFloatable))
292+
{
293+
d->TabBar->makeAreaFloating(mapFromGlobal(QCursor::pos()), DraggingInactive);
294+
}
292295
}
293296

294297

@@ -343,9 +346,10 @@ void CDockAreaTitleBar::setVisible(bool Visible)
343346
void CDockAreaTitleBar::showContextMenu(const QPoint& pos)
344347
{
345348
QMenu Menu(this);
346-
Menu.addAction(tr("Detach Area"), this, SLOT(onUndockButtonClicked()));
349+
auto Action = Menu.addAction(tr("Detach Area"), this, SLOT(onUndockButtonClicked()));
350+
Action->setEnabled(d->DockArea->features().testFlag(CDockWidget::DockWidgetFloatable));
347351
Menu.addSeparator();
348-
auto Action = Menu.addAction(tr("Close Area"), this, SLOT(onCloseButtonClicked()));
352+
Action = Menu.addAction(tr("Close Area"), this, SLOT(onCloseButtonClicked()));
349353
Action->setEnabled(d->DockArea->features().testFlag(CDockWidget::DockWidgetClosable));
350354
Menu.addAction(tr("Close Other Areas"), d->DockArea, SLOT(closeOtherAreas()));
351355
Menu.exec(mapToGlobal(pos));

src/DockAreaWidget.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ struct DockAreaWidgetPrivate
241241
DockAreaLayout* ContentsLayout = nullptr;
242242
CDockAreaTitleBar* TitleBar = nullptr;
243243
CDockManager* DockManager = nullptr;
244-
bool UpdateCloseButton = false;
244+
bool UpdateTitleBarButtons = false;
245245

246246
/**
247247
* Private data constructor
@@ -295,9 +295,9 @@ struct DockAreaWidgetPrivate
295295
}
296296

297297
/**
298-
* Udpates the enable state of the close button
298+
* Udpates the enable state of the close and detach button
299299
*/
300-
void updateCloseButtonState();
300+
void updateTitleBarButtonStates();
301301
};
302302
// struct DockAreaWidgetPrivate
303303

@@ -322,17 +322,19 @@ void DockAreaWidgetPrivate::createTitleBar()
322322

323323

324324
//============================================================================
325-
void DockAreaWidgetPrivate::updateCloseButtonState()
325+
void DockAreaWidgetPrivate::updateTitleBarButtonStates()
326326
{
327327
if (_this->isHidden())
328328
{
329-
UpdateCloseButton = true;
329+
UpdateTitleBarButtons = true;
330330
return;
331331
}
332332

333333
TitleBar->button(TitleBarButtonClose)->setEnabled(
334334
_this->features().testFlag(CDockWidget::DockWidgetClosable));
335-
UpdateCloseButton = false;
335+
TitleBar->button(TitleBarButtonUndock)->setEnabled(
336+
_this->features().testFlag(CDockWidget::DockWidgetFloatable));
337+
UpdateTitleBarButtons = false;
336338
}
337339

338340

@@ -400,7 +402,7 @@ void CDockAreaWidget::insertDockWidget(int index, CDockWidget* DockWidget,
400402
setCurrentIndex(index);
401403
}
402404
DockWidget->setDockArea(this);
403-
d->updateCloseButtonState();
405+
d->updateTitleBarButtonStates();
404406
}
405407

406408

@@ -433,7 +435,7 @@ void CDockAreaWidget::removeDockWidget(CDockWidget* DockWidget)
433435
hideAreaWithNoVisibleContent();
434436
}
435437

436-
d->updateCloseButtonState();
438+
d->updateTitleBarButtonStates();
437439
updateTitleBarVisibility();
438440
auto TopLevelDockWidget = DockContainer->topLevelDockWidget();
439441
if (TopLevelDockWidget)
@@ -765,9 +767,9 @@ void CDockAreaWidget::toggleView(bool Open)
765767
void CDockAreaWidget::setVisible(bool Visible)
766768
{
767769
Super::setVisible(Visible);
768-
if (d->UpdateCloseButton)
770+
if (d->UpdateTitleBarButtons)
769771
{
770-
d->updateCloseButtonState();
772+
d->updateTitleBarButtonStates();
771773
}
772774
}
773775

src/DockWidget.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ private slots:
141141
enum DockWidgetFeature
142142
{
143143
DockWidgetClosable = 0x01,
144-
DockWidgetMovable = 0x02,
144+
DockWidgetMovable = 0x02,///< this feature is not properly implemented yet and is ignored
145145
DockWidgetFloatable = 0x04,
146146
AllDockWidgetFeatures = DockWidgetClosable | DockWidgetMovable | DockWidgetFloatable,
147147
NoDockWidgetFeatures = 0x00

src/DockWidgetTab.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ void CDockWidgetTab::mouseMoveEvent(QMouseEvent* ev)
327327
}
328328

329329
// Floating is only allowed for widgets that are movable
330-
if (d->DockWidget->features().testFlag(CDockWidget::DockWidgetMovable))
330+
if (d->DockWidget->features().testFlag(CDockWidget::DockWidgetFloatable))
331331
{
332332
d->startFloating();
333333
}
@@ -351,9 +351,10 @@ void CDockWidgetTab::contextMenuEvent(QContextMenuEvent* ev)
351351

352352
d->DragStartMousePosition = ev->pos();
353353
QMenu Menu(this);
354-
Menu.addAction(tr("Detach"), this, SLOT(onDetachActionTriggered()));
354+
auto Action = Menu.addAction(tr("Detach"), this, SLOT(onDetachActionTriggered()));
355+
Action->setEnabled(d->DockWidget->features().testFlag(CDockWidget::DockWidgetFloatable));
355356
Menu.addSeparator();
356-
auto Action = Menu.addAction(tr("Close"), this, SIGNAL(closeRequested()));
357+
Action = Menu.addAction(tr("Close"), this, SIGNAL(closeRequested()));
357358
Action->setEnabled(isClosable());
358359
Menu.addAction(tr("Close Others"), this, SIGNAL(closeOtherTabsRequested()));
359360
Menu.exec(mapToGlobal(ev->pos()));
@@ -468,7 +469,8 @@ void CDockWidgetTab::mouseDoubleClickEvent(QMouseEvent *event)
468469
// If this is the last dock area in a dock container it does not make
469470
// sense to move it to a new floating widget and leave this one
470471
// empty
471-
if (!d->DockArea->dockContainer()->isFloating() || d->DockArea->dockWidgetsCount() > 1)
472+
if ((!d->DockArea->dockContainer()->isFloating() || d->DockArea->dockWidgetsCount() > 1)
473+
&& d->DockWidget->features().testFlag(CDockWidget::DockWidgetFloatable))
472474
{
473475
d->DragStartMousePosition = event->pos();
474476
d->startFloating(DraggingInactive);
@@ -504,13 +506,15 @@ bool CDockWidgetTab::isClosable() const
504506
//===========================================================================
505507
void CDockWidgetTab::onDetachActionTriggered()
506508
{
509+
if (!d->DockWidget->features().testFlag(CDockWidget::DockWidgetFloatable))
510+
{
511+
return;
512+
}
507513
d->DragStartMousePosition = mapFromGlobal(QCursor::pos());
508514
d->startFloating(DraggingInactive);
509515
}
510516

511517

512-
513-
514518
//============================================================================
515519
bool CDockWidgetTab::event(QEvent *e)
516520
{

0 commit comments

Comments
 (0)