aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2018-01-08 15:34:11 +0100
committerAqua-sama <aqua@iserlohn-fortress.net>2018-01-08 15:34:11 +0100
commit6b2326d0b61d588a7b9d1a8a1b2ca434cda1799f (patch)
treed0564ae69966fcf1d8dcc62eded78b2ba1413098 /src
parentAdded .clang-format (diff)
downloadsmolbote-6b2326d0b61d588a7b9d1a8a1b2ca434cda1799f.tar.xz
Fixed bugs with QDockWidgets
- QDockWidgets are now deleted on close - Opening a new BookmarksWidget no longer leaves an empty dock widget - Fixed crash with deleting dock widgets
Diffstat (limited to 'src')
-rw-r--r--src/mainwindow.cpp56
-rw-r--r--src/mainwindow.h5
2 files changed, 38 insertions, 23 deletions
diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp
index a2b9ff1..f57cab9 100644
--- a/src/mainwindow.cpp
+++ b/src/mainwindow.cpp
@@ -7,24 +7,18 @@
*/
#include "mainwindow.h"
+#include "browser.h"
#include "forms/aboutdialog.h"
+#include "forms/searchform.h"
#include "ui_mainwindow.h"
#include "widgets/mainwindowmenubar.h"
-#include <QMessageBox>
-
#include <QDockWidget>
-
-#include <settings/configuration.h>
-
+#include <QMessageBox>
#include <bookmarks/bookmarkswidget.h>
#include <downloads/downloadswidget.h>
-
-#include "browser.h"
-#include <settings/settingsdialog.h>
-
#include <navigation/urllineedit.h>
-
-#include "forms/searchform.h"
+#include <settings/configuration.h>
+#include <settings/settingsdialog.h>
MainWindow::MainWindow(std::shared_ptr<Configuration> config, QWidget *parent)
: QMainWindow(parent)
@@ -93,7 +87,8 @@ MainWindow::MainWindow(std::shared_ptr<Configuration> config, QWidget *parent)
url.replace("$term", t);
tabBar->currentView()->load(QUrl::fromUserInput(url));
});
- connect(tabBar, SIGNAL(currentTabChanged(WebView *)), this, SLOT(handleTabChanged(WebView *)));
+ connect(tabBar, &MainWindowTabBar::currentTabChanged, this, &MainWindow::handleTabChanged);
+ //connect(tabBar, SIGNAL(currentTabChanged(WebView *)), this, SLOT(handleTabChanged(WebView *)));
// loading bar
ui->statusBar->addPermanentWidget(m_progressBar);
@@ -136,12 +131,17 @@ MainWindow::~MainWindow()
void MainWindow::addTabbedDock(Qt::DockWidgetArea area, QWidget *widget)
{
- // get all dock widgets
- QList<QDockWidget *> allDockWidgets = findChildren<QDockWidget *>();
-
// make a list of widgets in the area we want
+ // this way we can append the new dock widget to the last one
QVector<QDockWidget *> areaDockWidgets;
- for(QDockWidget *w : allDockWidgets) {
+ for(QDockWidget *w : findChildren<QDockWidget *>()) {
+ // check if widget is already shown
+ if(w->widget() == widget) {
+ // in this case, close the dock and return
+ w->close();
+ return;
+ }
+
if(dockWidgetArea(w) == area) {
areaDockWidgets.append(w);
}
@@ -149,13 +149,29 @@ void MainWindow::addTabbedDock(Qt::DockWidgetArea area, QWidget *widget)
// create a dock widget
QDockWidget *dock = new QDockWidget(widget->windowTitle(), this);
-
- // release the held widget when deleted
- connect(dock, &QDockWidget::destroyed, this, [dock]() {
- dock->widget()->setParent(nullptr);
+ dock->setAttribute(Qt::WA_DeleteOnClose, true);
+
+ // when the dock widget becomes invisble, release the docked widget
+ // setting the widget makes the dock its parent; setting parent back to nullptr
+ // makes the dock not show the widget any more
+ connect(dock, &QDockWidget::visibilityChanged, [dock](bool visible) {
+ if(!visible && dock->widget()) {
+ dock->widget()->setParent(nullptr);
+ }
});
dock->setWidget(widget);
+ // the same widget may be shown by docks in other windows
+ // in that case, they grab ownership and the current dock won't be showing anything
+ // so the current widget needs to be closed
+ auto *w = dynamic_cast<BookmarksWidget *>(widget);
+ w->closeOthers();
+ if(w) {
+ connect(w, &BookmarksWidget::closeOthersSignal, dock, [dock]() {
+ dock->close();
+ });
+ }
+
if(areaDockWidgets.empty()) {
// no other widgets
addDockWidget(area, dock);
diff --git a/src/mainwindow.h b/src/mainwindow.h
index c62ebe3..8febb71 100644
--- a/src/mainwindow.h
+++ b/src/mainwindow.h
@@ -43,7 +43,8 @@ class MainWindow : public QMainWindow
friend class MainWindowMenuBar;
public:
- MainWindow(std::shared_ptr<Configuration> config, QWidget *parent = nullptr);
+ explicit MainWindow(std::shared_ptr<Configuration> config, QWidget *parent = nullptr);
+ Q_DISABLE_COPY(MainWindow)
~MainWindow() override;
void addTabbedDock(Qt::DockWidgetArea area, QWidget *widget);
@@ -71,8 +72,6 @@ private slots:
void handleTitleUpdated(const QString &title);
private:
- Q_DISABLE_COPY(MainWindow)
-
Ui::MainWindow *ui;
SearchForm *m_searchBox;