diff options
Diffstat (limited to 'src/mainwindow')
-rw-r--r-- | src/mainwindow/mainwindow.cpp | 29 | ||||
-rw-r--r-- | src/mainwindow/mainwindow.h | 1 | ||||
-rw-r--r-- | src/mainwindow/widgets/dockwidget.cpp | 25 | ||||
-rw-r--r-- | src/mainwindow/widgets/dockwidget.h | 23 |
4 files changed, 70 insertions, 8 deletions
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 <QApplication> #include <QCloseEvent> -#include <QDockWidget> +#include "widgets/dockwidget.h" #include <QLineEdit> #include <QMdiArea> #include <QMdiSubWindow> @@ -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<QDockWidget *> docks; + for(QDockWidget *dock : findChildren<QDockWidget *>()) { + 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<QDockWidget *>(); + 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 <QCloseEvent> + +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 <QDockWidget> + + +class DockWidget : public QDockWidget +{ +public: + DockWidget(const QString &title, QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags()); + ~DockWidget(); + +}; + +#endif // SMOLBOTE_DOCKWIDGET_H |