From 2105800b94edcae55c6e226d8dc3ec6e1c204ab2 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sun, 6 May 2018 12:02:19 +0200 Subject: Tabifying dock widgets --- src/mainwindow/mainwindow.cpp | 29 +++++++++++++++++++++-------- src/mainwindow/mainwindow.h | 1 + src/mainwindow/widgets/dockwidget.cpp | 25 +++++++++++++++++++++++++ src/mainwindow/widgets/dockwidget.h | 23 +++++++++++++++++++++++ 4 files changed, 70 insertions(+), 8 deletions(-) create mode 100644 src/mainwindow/widgets/dockwidget.cpp create mode 100644 src/mainwindow/widgets/dockwidget.h (limited to 'src/mainwindow') diff --git a/src/mainwindow/mainwindow.cpp b/src/mainwindow/mainwindow.cpp index eba02c6..5129abc 100644 --- a/src/mainwindow/mainwindow.cpp +++ b/src/mainwindow/mainwindow.cpp @@ -14,7 +14,7 @@ #include "window.h" #include #include -#include +#include "widgets/dockwidget.h" #include #include #include @@ -153,17 +153,30 @@ void MainWindow::addAction(ActionLocation where, QAction *action) void MainWindow::addDockWidget(Qt::DockWidgetArea area, QWidget *widget) { - QDockWidget *dock = new QDockWidget(widget->windowTitle(), this); - dock->setAttribute(Qt::WA_DeleteOnClose, true); + QList docks; + for(QDockWidget *dock : findChildren()) { + if(dockWidgetArea(dock) == area) + docks.append(dock); + } + + DockWidget *dock = new DockWidget(widget->windowTitle(), this); dock->setWidget(widget); - connect(dock, &QDockWidget::visibilityChanged, [dock](bool visible) { - if(!visible && dock->widget()) { + if(docks.empty()) + QMainWindow::addDockWidget(area, dock); + else + tabifyDockWidget(docks.last(), dock); +} + +void MainWindow::removeDockWidget(QWidget *widget) +{ + const auto docks = this->findChildren(); + for(QDockWidget *dock : docks) { + if(dock->widget() == widget) { dock->widget()->setParent(nullptr); + dock->close(); } - }); - - QMainWindow::addDockWidget(area, dock); + } } void MainWindow::createTab(const QUrl &url) diff --git a/src/mainwindow/mainwindow.h b/src/mainwindow/mainwindow.h index 2532189..8f511a3 100644 --- a/src/mainwindow/mainwindow.h +++ b/src/mainwindow/mainwindow.h @@ -40,6 +40,7 @@ public: void addAction(ActionLocation where, QAction *action); void addDockWidget(Qt::DockWidgetArea area, QWidget *widget); + void removeDockWidget(QWidget *widget); Window *currentSubWindow() const; diff --git a/src/mainwindow/widgets/dockwidget.cpp b/src/mainwindow/widgets/dockwidget.cpp new file mode 100644 index 0000000..f70e26f --- /dev/null +++ b/src/mainwindow/widgets/dockwidget.cpp @@ -0,0 +1,25 @@ +/* + * This file is part of smolbote. It's copyrighted by the contributors recorded + * in the version control history of the file, available from its original + * location: https://neueland.iserlohn-fortress.net/smolbote.hg + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#include "dockwidget.h" +#include + +DockWidget::DockWidget(const QString &title, QWidget *parent, Qt::WindowFlags flags) + : QDockWidget(title, parent, flags) +{ + setAttribute(Qt::WA_DeleteOnClose, true); +} + +DockWidget::~DockWidget() +{ + // dock widget takes ownership of its widget, so closing the dock widget + // causes it to delete the widget as well + // clear the widget's parent to prevent this + if(widget()) + widget()->setParent(nullptr); +} diff --git a/src/mainwindow/widgets/dockwidget.h b/src/mainwindow/widgets/dockwidget.h new file mode 100644 index 0000000..35a4cc7 --- /dev/null +++ b/src/mainwindow/widgets/dockwidget.h @@ -0,0 +1,23 @@ +/* + * This file is part of smolbote. It's copyrighted by the contributors recorded + * in the version control history of the file, available from its original + * location: https://neueland.iserlohn-fortress.net/smolbote.hg + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#ifndef SMOLBOTE_DOCKWIDGET_H +#define SMOLBOTE_DOCKWIDGET_H + +#include + + +class DockWidget : public QDockWidget +{ +public: + DockWidget(const QString &title, QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags()); + ~DockWidget(); + +}; + +#endif // SMOLBOTE_DOCKWIDGET_H -- cgit v1.2.1