aboutsummaryrefslogtreecommitdiff
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
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
-rw-r--r--CMakeLists.txt16
-rw-r--r--lib/bookmarks/bookmarkswidget.cpp6
-rw-r--r--lib/bookmarks/bookmarkswidget.h3
-rw-r--r--src/mainwindow.cpp56
-rw-r--r--src/mainwindow.h5
5 files changed, 61 insertions, 25 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 600ac8e..fdc3b2e 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.1.0)
project(smolbote)
# Options
+option(CompilerWarnings "Compiler warnings" ON)
+option(QtDeprecatedWarnings "Qt deprecated warnings" ON)
option(UseLibCpp "Use libc++ over stdlibc++ (requires clang)" OFF)
option(CLikeConfig "Use a C-like style for the config" ON)
@@ -20,7 +22,19 @@ set(CMAKE_AUTORCC ON)
# Global C++ settings
set(CMAKE_CXX_STANDARD 17)
-add_definitions(-DQT_DEPRECATED_WARNINGS -DQT_DISABLE_DEPRECATED_BEFORE=0x050900)
+
+if(CompilerWarnings)
+ if(CMAKE_COMPILER_IS_GNUCC)
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
+ endif()
+ if(MSVC)
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
+ endif()
+endif(CompilerWarnings)
+
+if(QtDeprecatedWarnings)
+ add_definitions(-DQT_DEPRECATED_WARNINGS -DQT_DISABLE_DEPRECATED_BEFORE=0x050900)
+endif(QtDeprecatedWarnings)
# link to libc++
if (UseLibCpp)
diff --git a/lib/bookmarks/bookmarkswidget.cpp b/lib/bookmarks/bookmarkswidget.cpp
index 8aafe37..17e1732 100644
--- a/lib/bookmarks/bookmarkswidget.cpp
+++ b/lib/bookmarks/bookmarkswidget.cpp
@@ -73,4 +73,8 @@ void BookmarksWidget::expandNodes(BookmarkItem *node)
expandNodes(childNode);
}
}
-} \ No newline at end of file
+}
+
+void BookmarksWidget::closeOthers() {
+ emit closeOthersSignal();
+}
diff --git a/lib/bookmarks/bookmarkswidget.h b/lib/bookmarks/bookmarkswidget.h
index f82a512..fa830fa 100644
--- a/lib/bookmarks/bookmarkswidget.h
+++ b/lib/bookmarks/bookmarkswidget.h
@@ -29,8 +29,11 @@ public:
void save();
QAbstractItemModel *model() const;
+ void closeOthers();
+
signals:
void openUrl(const QUrl &url);
+ void closeOthersSignal();
private:
void expandNodes(BookmarkItem *node);
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;