From c2557c6cd825dd881137c576db867f7f311ec791 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Tue, 29 May 2018 17:13:13 +0200 Subject: Refactor Subwindow --- src/CMakeLists.txt | 4 +- src/browser.cpp | 2 +- src/mainwindow/mainwindow.cpp | 20 ++--- src/mainwindow/mainwindow.h | 6 +- src/mainwindow/subwindow.cpp | 204 ++++++++++++++++++++++++++++++++++++++++++ src/mainwindow/subwindow.h | 51 +++++++++++ src/mainwindow/window.cpp | 199 ---------------------------------------- src/mainwindow/window.h | 52 ----------- src/webengine/webview.cpp | 6 +- src/webengine/webview.h | 4 +- 10 files changed, 276 insertions(+), 272 deletions(-) create mode 100644 src/mainwindow/subwindow.cpp create mode 100644 src/mainwindow/subwindow.h delete mode 100644 src/mainwindow/window.cpp delete mode 100644 src/mainwindow/window.h (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c326c4b..d0ac915 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -18,8 +18,8 @@ add_executable(poi # main window mainwindow/mainwindow.cpp mainwindow/mainwindow.h - mainwindow/window.cpp - mainwindow/window.h + mainwindow/subwindow.cpp + mainwindow/subwindow.h mainwindow/widgets/dockwidget.cpp mainwindow/widgets/dockwidget.h mainwindow/widgets/navigationbar.cpp diff --git a/src/browser.cpp b/src/browser.cpp index 525f02c..41c73df 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -9,7 +9,7 @@ #include "browser.h" #include "addressbar/addressbar.h" #include "mainwindow/mainwindow.h" -#include "mainwindow/window.h" +#include "mainwindow/subwindow.h" #include "webengine/urlinterceptor.h" #include "webengine/webprofile.h" #include diff --git a/src/mainwindow/mainwindow.cpp b/src/mainwindow/mainwindow.cpp index c078d81..7e49ab8 100644 --- a/src/mainwindow/mainwindow.cpp +++ b/src/mainwindow/mainwindow.cpp @@ -11,7 +11,7 @@ #include "webengine/webview.h" #include "widgets/navigationbar.h" #include "widgets/searchform.h" -#include "window.h" +#include "subwindow.h" #include #include #include "widgets/dockwidget.h" @@ -70,7 +70,7 @@ MainWindow::MainWindow(std::shared_ptr &config, QWidget *parent) disconnect(statusBarConnection); subWindowAction->setMenu(nullptr); - auto *w = qobject_cast(window); + auto *w = qobject_cast(window); if(w == nullptr) { // no current subwindow, clear everything setView(nullptr); @@ -78,8 +78,8 @@ MainWindow::MainWindow(std::shared_ptr &config, QWidget *parent) } else { setView(w->currentView()); subWindowAction->setMenu(w->systemMenu()); - viewChangedConnection = connect(w, &Window::currentViewChanged, this, &MainWindow::setView); - statusBarConnection = connect(w, &Window::showStatusMessage, statusBar(), &QStatusBar::showMessage); + viewChangedConnection = connect(w, &SubWindow::currentViewChanged, this, &MainWindow::setView); + statusBarConnection = connect(w, &SubWindow::showStatusMessage, statusBar(), &QStatusBar::showMessage); } }); @@ -112,7 +112,7 @@ void MainWindow::createMenuBar() Q_CHECK_PTR(mdiArea); auto *smolboteMenu = menuBar()->addMenu(qApp->applicationDisplayName()); - smolboteMenu->addAction(tr("New tab group"), this, [this]() { + smolboteMenu->addAction(tr("New subwindow"), this, [this]() { createSubWindow(); }, QKeySequence(m_config->value("mainwindow.shortcuts.newGroup").value().c_str())); @@ -187,7 +187,7 @@ void MainWindow::removeDockWidget(QWidget *widget) void MainWindow::createTab(const QUrl &url) { - auto *w = qobject_cast(mdiArea->currentSubWindow()); + auto *w = qobject_cast(mdiArea->currentSubWindow()); if(w == nullptr) { w = createSubWindow(url.toString()); } else { @@ -195,14 +195,14 @@ void MainWindow::createTab(const QUrl &url) } } -Window *MainWindow::currentSubWindow() const +SubWindow *MainWindow::currentSubWindow() const { - return qobject_cast(mdiArea->currentSubWindow()); + return qobject_cast(mdiArea->currentSubWindow()); } -Window *MainWindow::createSubWindow(const QString &url) +SubWindow *MainWindow::createSubWindow(const QString &url) { - auto *w = new Window(m_config->section("window"), this); + auto *w = new SubWindow(m_config->section("window"), this); mdiArea->addSubWindow(w); w->showMaximized(); w->setFocus(); diff --git a/src/mainwindow/mainwindow.h b/src/mainwindow/mainwindow.h index 8f511a3..af713fc 100644 --- a/src/mainwindow/mainwindow.h +++ b/src/mainwindow/mainwindow.h @@ -16,7 +16,7 @@ class Browser; class QMdiArea; class Configuration; -class Window; +class SubWindow; class AddressBar; class SearchForm; class WebView; @@ -42,11 +42,11 @@ public: void addDockWidget(Qt::DockWidgetArea area, QWidget *widget); void removeDockWidget(QWidget *widget); - Window *currentSubWindow() const; + SubWindow *currentSubWindow() const; public slots: void createTab(const QUrl &url); - Window *createSubWindow(const QString &url = QString()); + SubWindow *createSubWindow(const QString &url = QString()); void setView(WebView *view); diff --git a/src/mainwindow/subwindow.cpp b/src/mainwindow/subwindow.cpp new file mode 100644 index 0000000..497e6d0 --- /dev/null +++ b/src/mainwindow/subwindow.cpp @@ -0,0 +1,204 @@ +/* + * 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 "subwindow.h" +#include "browser.h" +#include "webengine/webprofile.h" +#include "webengine/webview.h" +#include "widgets/tabwidget.h" +#include +#include +#include +#include +#include +#include +#include +#include + +SubWindow::SubWindow(const QHash &config, QWidget *parent, Qt::WindowFlags flags) + : QMdiSubWindow(parent, flags) + , tabWidget(new TabWidget(this)) +{ + // delete this window when it closes + setAttribute(Qt::WA_DeleteOnClose, true); + + resize(800, 600); + setWidget(tabWidget); + + profile = WebProfile::defaultProfile(); + + // system menu + { + auto *menu = systemMenu(); + menu->addSeparator(); + + auto *profileName_action = menu->addAction(tr("Profile: %1").arg(profile->name())); + profileName_action->setEnabled(false); + auto *loadProfile_menu = menu->addMenu(tr("Load profile")); + + Browser *browser = qobject_cast(qApp); + Q_CHECK_PTR(browser); + + for(const QString &name : browser->profiles()) { + auto *loadAction = loadProfile_menu->addAction(name); + connect(loadAction, &QAction::triggered, this, [name, browser, profileName_action, this]() { + auto *profile = browser->profile(name); + this->setProfile(profile); + profileName_action->setText(tr("Profile: %1").arg(name)); + }); + } + +#ifdef QT_DEBUG + menu->addSeparator(); + menu->addAction(tr("Debug menu"))->setEnabled(false); + auto *saveSession_action = menu->addAction(tr("Save session")); + menu->addAction(tr("Load session"))->setEnabled(false); + setSystemMenu(menu); + + connect(saveSession_action, &QAction::triggered, [this]() { + QJsonDocument doc(session()); + qDebug("%s", qUtf8Printable(doc.toJson())); + }); +#endif + } + + // new tab button + auto *newTab_button = new QToolButton(this); + newTab_button->setIcon(style()->standardIcon(QStyle::SP_FileIcon)); + newTab_button->setToolTip(tr("Add tab")); + newTab_button->setShortcut(QKeySequence(config.value("window.shortcuts.new"))); + connect(newTab_button, &QToolButton::clicked, this, [=]() { + auto index = addTab(WebProfile::defaultProfile()->newtab()); + tabWidget->setCurrentIndex(index); + }); + tabWidget->setCornerWidget(newTab_button, Qt::TopRightCorner); + + // general actions + auto *closeTab_shortcut = new QShortcut(QKeySequence(config.value("window.shortcuts.close")), this); + connect(closeTab_shortcut, &QShortcut::activated, this, [=]() { + tabWidget->deleteTab(tabWidget->currentIndex()); + }); + + auto *leftTab_shortcut = new QShortcut(QKeySequence(config.value("window.shortcuts.left")), this); + connect(leftTab_shortcut, &QShortcut::activated, this, [=]() { + tabWidget->setCurrentIndex(qMax(0, tabWidget->currentIndex() - 1)); + }); + + auto *rightTab_shortcut = new QShortcut(QKeySequence(config.value("window.shortcuts.right")), this); + connect(rightTab_shortcut, &QShortcut::activated, this, [=]() { + tabWidget->setCurrentIndex(qMin(tabWidget->currentIndex() + 1, tabWidget->count() - 1)); + }); + + connect(tabWidget, &TabWidget::currentChanged, [this](int index) { + if(index < 0) { + // last tab has been closed + close(); + } else { + auto *view = dynamic_cast(tabWidget->widget(index)); + Q_CHECK_PTR(view); + + disconnect(titleConnection); + disconnect(linkHoveredConnection); + + titleConnection = connect(view, &WebView::titleChanged, this, [this](const QString &title) { + auto *v = qobject_cast(sender()); + this->setWindowTitle(QString("%1 :%2").arg(title, v->profile()->name())); + }); + setWindowTitle(QString("%1 :%2").arg(view->title(), view->profile()->name())); + + linkHoveredConnection = connect(view->page(), &WebPage::linkHovered, this, [this](const QString &url) { + if(!url.isEmpty()) + emit showStatusMessage(url, 3000); + }); + + emit currentViewChanged(view); + } + }); +} + +SubWindow::~SubWindow() +{ + delete tabWidget; +} + +WebView *SubWindow::currentView() +{ + return qobject_cast(tabWidget->currentWidget()); +} + +WebView *SubWindow::view(int index) const +{ + return qobject_cast(tabWidget->widget(index)); +} + +void SubWindow::setProfile(WebProfile *profile) +{ + Q_CHECK_PTR(profile); + this->profile = profile; + for(int i = 0; i < tabWidget->count(); ++i) { + auto *view = qobject_cast(tabWidget->widget(i)); + view->setProfile(profile); + } +} + +int SubWindow::addTab(const QUrl &url) +{ + auto *view = new WebView(profile, this); + if(!url.isEmpty()) + view->load(url); + return tabWidget->addTab(view); +} + +void SubWindow::setCurrentTab(int index) +{ + tabWidget->setCurrentIndex(index); +} + +QJsonObject SubWindow::session() const +{ + QJsonObject obj; + obj.insert("profile", QJsonValue("")); + + QJsonArray tabs; + for(int i = 0; i < tabWidget->count(); ++i) { + auto *view = qobject_cast(tabWidget->widget(i)); + if(view) { + tabs.append(view->url().toString()); + } + } + obj.insert("tabs", tabs); + + return obj; +} + +void SubWindow::restoreSession(const QJsonObject &sessionData) +{ + auto *browser = qobject_cast(qApp); + Q_CHECK_PTR(browser); + + Q_ASSERT_X(sessionData.value("profile") != QJsonValue::Undefined, "Window::restoreSession", "no profile in json"); + if(browser->profiles().contains(sessionData.value("profile").toString())) + profile = browser->profile(sessionData.value("profile").toString()); + + Q_ASSERT_X(sessionData.value("tabs") != QJsonValue::Undefined, "Window::restoreSession", "no tabs in json"); + const QJsonArray tabs = sessionData.value("tabs").toArray(); + + if(tabs.count() == 0) { + // open a newtab + auto *view = new WebView(profile, this); + view->load(profile->newtab()); + tabWidget->addTab(view); + return; + } + + for(const auto tab : tabs) { + auto *view = new WebView(profile, this); + view->load(QUrl::fromUserInput(tab.toString())); + tabWidget->addTab(view); + } +} diff --git a/src/mainwindow/subwindow.h b/src/mainwindow/subwindow.h new file mode 100644 index 0000000..ac1610a --- /dev/null +++ b/src/mainwindow/subwindow.h @@ -0,0 +1,51 @@ +/* + * 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_SUBWINDOW_H +#define SMOLBOTE_SUBWINDOW_H + +#include +#include +#include + +class TabWidget; +class WebView; +class WebProfile; +class SubWindow : public QMdiSubWindow +{ + Q_OBJECT + +public: + explicit SubWindow(const QHash &config, QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags()); + ~SubWindow() override; + + WebView *currentView(); + WebView *view(int index) const; + + void setProfile(WebProfile *profile); + + QJsonObject session() const; + void restoreSession(const QJsonObject &sessionData); + +signals: + void currentViewChanged(WebView *view); + void showStatusMessage(const QString &message, int timeout = 0); + +public slots: + int addTab(const QUrl &url = QUrl()); + void setCurrentTab(int index); + +private: + WebProfile *profile; + TabWidget *tabWidget; + + QMetaObject::Connection titleConnection; + QMetaObject::Connection linkHoveredConnection; +}; + +#endif // SMOLBOTE_SUBWINDOW_H diff --git a/src/mainwindow/window.cpp b/src/mainwindow/window.cpp deleted file mode 100644 index 273aa0a..0000000 --- a/src/mainwindow/window.cpp +++ /dev/null @@ -1,199 +0,0 @@ -/* - * 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 "window.h" -#include "webengine/webprofile.h" -#include "webengine/webview.h" -#include "widgets/tabwidget.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include "browser.h" - -Window::Window(const QHash &config, QWidget *parent, Qt::WindowFlags flags) - : QMdiSubWindow(parent, flags) - , tabWidget(new TabWidget(this)) -{ - // delete this window when it closes - setAttribute(Qt::WA_DeleteOnClose, true); - - resize(800, 600); - setWidget(tabWidget); - - profile = WebProfile::defaultProfile(); - - { - auto *menu = systemMenu(); - menu->addSeparator(); - - auto *profileName_action = menu->addAction(tr("Profile: %1").arg(profile->isOffTheRecord() ? tr("off-the-record") : profile->storageName())); - profileName_action->setEnabled(false); - auto *loadProfile_menu = menu->addMenu(tr("Load profile")); - - Browser *instance = qobject_cast(qApp); - Q_ASSERT_X(instance != nullptr, "Window::Window", "Could not cast instance"); - for(const QString &name : instance->profiles()) { - auto *loadAction = loadProfile_menu->addAction(name); - connect(loadAction, &QAction::triggered, this, [name, instance, profileName_action, this]() { - auto *profile = instance->profile(name); - this->setProfile(profile); - profileName_action->setText(tr("Profile: %1").arg(name)); - }); - } - -#ifdef QT_DEBUG - menu->addSeparator(); - menu->addAction(tr("Debug menu"))->setEnabled(false); - auto *saveSession_action = menu->addAction(tr("Save session")); - menu->addAction(tr("Load session"))->setEnabled(false); - setSystemMenu(menu); - - connect(saveSession_action, &QAction::triggered, [this]() { - QJsonDocument doc(session()); - qDebug("%s", qUtf8Printable(doc.toJson())); - }); -#endif - } - - // new tab button - auto *newTab_button = new QToolButton(this); - newTab_button->setIcon(style()->standardIcon(QStyle::SP_FileIcon)); - newTab_button->setToolTip(tr("Add tab")); - newTab_button->setShortcut(QKeySequence(config.value("window.shortcuts.new"))); - connect(newTab_button, &QToolButton::clicked, this, [=]() { - auto index = addTab(WebProfile::defaultProfile()->newtab()); - tabWidget->setCurrentIndex(index); - }); - tabWidget->setCornerWidget(newTab_button, Qt::TopRightCorner); - - // general actions - auto *closeTab_shortcut = new QShortcut(QKeySequence(config.value("window.shortcuts.close")), this); - connect(closeTab_shortcut, &QShortcut::activated, this, [=]() { - tabWidget->deleteTab(tabWidget->currentIndex()); - }); - - auto *leftTab_shortcut = new QShortcut(QKeySequence(config.value("window.shortcuts.left")), this); - connect(leftTab_shortcut, &QShortcut::activated, this, [=]() { - tabWidget->setCurrentIndex(qMax(0, tabWidget->currentIndex() - 1)); - }); - - auto *rightTab_shortcut = new QShortcut(QKeySequence(config.value("window.shortcuts.right")), this); - connect(rightTab_shortcut, &QShortcut::activated, this, [=]() { - tabWidget->setCurrentIndex(qMin(tabWidget->currentIndex() + 1, tabWidget->count() - 1)); - }); - - connect(tabWidget, &TabWidget::currentChanged, [this](int index) { - if(index < 0) { - // last tab has been closed - close(); - } else { - auto *view = dynamic_cast(tabWidget->widget(index)); - Q_CHECK_PTR(view); - - disconnect(titleConnection); - disconnect(linkHoveredConnection); - - titleConnection = connect(view, &WebView::titleChanged, this, [this](const QString &title) { - auto *v = qobject_cast(sender()); - this->setWindowTitle(QString("%1 :%2").arg(title, v->profile()->name())); - }); - setWindowTitle(QString("%1 :%2").arg(view->title(), view->profile()->name())); - - linkHoveredConnection = connect(view->page(), &WebPage::linkHovered, this, [this](const QString &url) { - if(!url.isEmpty()) - emit showStatusMessage(url, 3000); - }); - - emit currentViewChanged(view); - } - }); -} - -Window::~Window() -{ - delete tabWidget; -} - -WebView *Window::currentView() -{ - return qobject_cast(tabWidget->currentWidget()); -} - -WebView *Window::view(int index) const -{ - return qobject_cast(tabWidget->widget(index)); -} - -void Window::setProfile(WebProfile *profile) -{ - Q_ASSERT_X(profile != nullptr, "Window::setProfile", "Tried to set null profile"); - this->profile = profile; - for(int i = 0; i < tabWidget->count(); ++i) { - auto *view = qobject_cast(tabWidget->widget(i)); - view->setProfile(profile); - } -} - -int Window::addTab(const QUrl &url) -{ - auto *view = new WebView(profile, this); - if(!url.isEmpty()) - view->load(url); - return tabWidget->addTab(view); -} - -void Window::swapToTab(int index) -{ - tabWidget->setCurrentIndex(index); -} - -QJsonObject Window::session() const -{ - QJsonObject obj; - obj.insert("profile", QJsonValue("")); - - QJsonArray tabs; - for(int i = 0; i < tabWidget->count(); ++i) { - auto *view = qobject_cast(tabWidget->widget(i)); - if(view) { - tabs.append(view->url().toString()); - } - } - obj.insert("tabs", tabs); - - return obj; -} - -void Window::restoreSession(const QJsonObject &sessionData) -{ - Q_ASSERT_X(sessionData.value("profile") != QJsonValue::Undefined, "Window::restoreSession", "no profile in json"); - auto *profile = WebProfile::defaultProfile(); - - Q_ASSERT_X(sessionData.value("tabs") != QJsonValue::Undefined, "Window::restoreSession", "no tabs in json"); - const QJsonArray tabs = sessionData.value("tabs").toArray(); - - if(tabs.count() == 0) { - // open a newtab - auto *view = new WebView(profile, this); - view->load(profile->newtab()); - tabWidget->addTab(view); - return; - } - - for(const auto tab : tabs) { - auto *view = new WebView(profile, this); - view->load(QUrl::fromUserInput(tab.toString())); - tabWidget->addTab(view); - } - -} diff --git a/src/mainwindow/window.h b/src/mainwindow/window.h deleted file mode 100644 index 6431466..0000000 --- a/src/mainwindow/window.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * 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_WINDOW_H -#define SMOLBOTE_WINDOW_H - -#include -#include -#include - -class TabWidget; -class WebView; -class WebProfile; -class Window : public QMdiSubWindow -{ - Q_OBJECT - -public: - explicit Window(const QHash &config, QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags()); - ~Window() override; - - WebView *currentView(); - WebView *view(int index) const; - - void setProfile(WebProfile *profile); - - QJsonObject session() const; - void restoreSession(const QJsonObject &sessionData); - -signals: - void currentViewChanged(WebView *view); - void showStatusMessage(const QString &message, int timeout = 0); - -public slots: - int addTab(const QUrl &url = QUrl()); - - void swapToTab(int index); - -private: - WebProfile *profile; - TabWidget *tabWidget; - - QMetaObject::Connection titleConnection; - QMetaObject::Connection linkHoveredConnection; -}; - -#endif // SMOLBOTE_WINDOW_H diff --git a/src/webengine/webview.cpp b/src/webengine/webview.cpp index d5f88d0..c6fd4a4 100644 --- a/src/webengine/webview.cpp +++ b/src/webengine/webview.cpp @@ -7,7 +7,7 @@ */ #include "webview.h" -#include "mainwindow/window.h" +#include "mainwindow/subwindow.h" #include "webpage.h" #include "webprofile.h" #include "widgets/pagemenu.h" @@ -23,7 +23,7 @@ WebView::WebView(WebProfile *profile, QWidget *parent) m_profile = profile; setPage(new WebPage(profile, this)); - m_parentWindow = qobject_cast(parent); + m_parentWindow = qobject_cast(parent); // load status and progress connect(this, &QWebEngineView::loadStarted, this, [this]() { @@ -86,7 +86,7 @@ WebView *WebView::createWindow(QWebEnginePage::WebWindowType type) case QWebEnginePage::WebBrowserTab: // a web browser tab - m_parentWindow->swapToTab(index); + m_parentWindow->setCurrentTab(index); break; case QWebEnginePage::WebDialog: diff --git a/src/webengine/webview.h b/src/webengine/webview.h index 378296e..4d648d7 100644 --- a/src/webengine/webview.h +++ b/src/webengine/webview.h @@ -14,7 +14,7 @@ class QMenu; class WebProfile; -class Window; +class SubWindow; class WebView : public QWebEngineView { Q_OBJECT @@ -57,7 +57,7 @@ protected: WebView *createWindow(QWebEnginePage::WebWindowType type) override; private: - Window *m_parentWindow = nullptr; + SubWindow *m_parentWindow = nullptr; WebProfile *m_profile = nullptr; QMenu *m_pageMenu = nullptr; -- cgit v1.2.1