diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-05-29 17:13:13 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-05-29 17:13:13 +0200 |
commit | c2557c6cd825dd881137c576db867f7f311ec791 (patch) | |
tree | 8eba50c91fde7645b0cdd56b95590f8233012979 /src/mainwindow | |
parent | Update quickstart (diff) | |
download | smolbote-c2557c6cd825dd881137c576db867f7f311ec791.tar.xz |
Refactor Subwindow
Diffstat (limited to 'src/mainwindow')
-rw-r--r-- | src/mainwindow/mainwindow.cpp | 20 | ||||
-rw-r--r-- | src/mainwindow/mainwindow.h | 6 | ||||
-rw-r--r-- | src/mainwindow/subwindow.cpp (renamed from src/mainwindow/window.cpp) | 55 | ||||
-rw-r--r-- | src/mainwindow/subwindow.h (renamed from src/mainwindow/window.h) | 17 |
4 files changed, 51 insertions, 47 deletions
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 <QApplication> #include <QCloseEvent> #include "widgets/dockwidget.h" @@ -70,7 +70,7 @@ MainWindow::MainWindow(std::shared_ptr<Configuration> &config, QWidget *parent) disconnect(statusBarConnection); subWindowAction->setMenu(nullptr); - auto *w = qobject_cast<Window *>(window); + auto *w = qobject_cast<SubWindow *>(window); if(w == nullptr) { // no current subwindow, clear everything setView(nullptr); @@ -78,8 +78,8 @@ MainWindow::MainWindow(std::shared_ptr<Configuration> &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<std::string>("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<Window *>(mdiArea->currentSubWindow()); + auto *w = qobject_cast<SubWindow *>(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<Window *>(mdiArea->currentSubWindow()); + return qobject_cast<SubWindow *>(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/window.cpp b/src/mainwindow/subwindow.cpp index 273aa0a..497e6d0 100644 --- a/src/mainwindow/window.cpp +++ b/src/mainwindow/subwindow.cpp @@ -6,21 +6,21 @@ * SPDX-License-Identifier: GPL-3.0 */ -#include "window.h" +#include "subwindow.h" +#include "browser.h" #include "webengine/webprofile.h" #include "webengine/webview.h" #include "widgets/tabwidget.h" -#include <QToolButton> -#include <QStyle> #include <QAction> -#include <QShortcut> -#include <QMenu> -#include <QJsonObject> #include <QJsonArray> #include <QJsonDocument> -#include "browser.h" +#include <QJsonObject> +#include <QMenu> +#include <QShortcut> +#include <QStyle> +#include <QToolButton> -Window::Window(const QHash<QString, QString> &config, QWidget *parent, Qt::WindowFlags flags) +SubWindow::SubWindow(const QHash<QString, QString> &config, QWidget *parent, Qt::WindowFlags flags) : QMdiSubWindow(parent, flags) , tabWidget(new TabWidget(this)) { @@ -32,20 +32,22 @@ Window::Window(const QHash<QString, QString> &config, QWidget *parent, Qt::Windo profile = WebProfile::defaultProfile(); + // system menu { auto *menu = systemMenu(); menu->addSeparator(); - auto *profileName_action = menu->addAction(tr("Profile: %1").arg(profile->isOffTheRecord() ? tr("off-the-record") : profile->storageName())); + auto *profileName_action = menu->addAction(tr("Profile: %1").arg(profile->name())); profileName_action->setEnabled(false); auto *loadProfile_menu = menu->addMenu(tr("Load profile")); - Browser *instance = qobject_cast<Browser*>(qApp); - Q_ASSERT_X(instance != nullptr, "Window::Window", "Could not cast instance"); - for(const QString &name : instance->profiles()) { + Browser *browser = qobject_cast<Browser *>(qApp); + Q_CHECK_PTR(browser); + + for(const QString &name : browser->profiles()) { auto *loadAction = loadProfile_menu->addAction(name); - connect(loadAction, &QAction::triggered, this, [name, instance, profileName_action, this]() { - auto *profile = instance->profile(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)); }); @@ -119,24 +121,24 @@ Window::Window(const QHash<QString, QString> &config, QWidget *parent, Qt::Windo }); } -Window::~Window() +SubWindow::~SubWindow() { delete tabWidget; } -WebView *Window::currentView() +WebView *SubWindow::currentView() { return qobject_cast<WebView *>(tabWidget->currentWidget()); } -WebView *Window::view(int index) const +WebView *SubWindow::view(int index) const { return qobject_cast<WebView *>(tabWidget->widget(index)); } -void Window::setProfile(WebProfile *profile) +void SubWindow::setProfile(WebProfile *profile) { - Q_ASSERT_X(profile != nullptr, "Window::setProfile", "Tried to set null profile"); + Q_CHECK_PTR(profile); this->profile = profile; for(int i = 0; i < tabWidget->count(); ++i) { auto *view = qobject_cast<WebView *>(tabWidget->widget(i)); @@ -144,7 +146,7 @@ void Window::setProfile(WebProfile *profile) } } -int Window::addTab(const QUrl &url) +int SubWindow::addTab(const QUrl &url) { auto *view = new WebView(profile, this); if(!url.isEmpty()) @@ -152,12 +154,12 @@ int Window::addTab(const QUrl &url) return tabWidget->addTab(view); } -void Window::swapToTab(int index) +void SubWindow::setCurrentTab(int index) { tabWidget->setCurrentIndex(index); } -QJsonObject Window::session() const +QJsonObject SubWindow::session() const { QJsonObject obj; obj.insert("profile", QJsonValue("")); @@ -174,10 +176,14 @@ QJsonObject Window::session() const return obj; } -void Window::restoreSession(const QJsonObject &sessionData) +void SubWindow::restoreSession(const QJsonObject &sessionData) { + auto *browser = qobject_cast<Browser *>(qApp); + Q_CHECK_PTR(browser); + Q_ASSERT_X(sessionData.value("profile") != QJsonValue::Undefined, "Window::restoreSession", "no profile in json"); - auto *profile = WebProfile::defaultProfile(); + 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(); @@ -195,5 +201,4 @@ void Window::restoreSession(const QJsonObject &sessionData) view->load(QUrl::fromUserInput(tab.toString())); tabWidget->addTab(view); } - } diff --git a/src/mainwindow/window.h b/src/mainwindow/subwindow.h index 6431466..ac1610a 100644 --- a/src/mainwindow/window.h +++ b/src/mainwindow/subwindow.h @@ -6,23 +6,23 @@ * SPDX-License-Identifier: GPL-3.0 */ -#ifndef SMOLBOTE_WINDOW_H -#define SMOLBOTE_WINDOW_H +#ifndef SMOLBOTE_SUBWINDOW_H +#define SMOLBOTE_SUBWINDOW_H #include <QMdiSubWindow> -#include <memory> #include <QUrl> +#include <memory> class TabWidget; class WebView; class WebProfile; -class Window : public QMdiSubWindow +class SubWindow : public QMdiSubWindow { Q_OBJECT public: - explicit Window(const QHash<QString, QString> &config, QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags()); - ~Window() override; + explicit SubWindow(const QHash<QString, QString> &config, QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags()); + ~SubWindow() override; WebView *currentView(); WebView *view(int index) const; @@ -38,8 +38,7 @@ signals: public slots: int addTab(const QUrl &url = QUrl()); - - void swapToTab(int index); + void setCurrentTab(int index); private: WebProfile *profile; @@ -49,4 +48,4 @@ private: QMetaObject::Connection linkHoveredConnection; }; -#endif // SMOLBOTE_WINDOW_H +#endif // SMOLBOTE_SUBWINDOW_H |