diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2020-04-13 15:44:09 +0300 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2020-04-13 15:44:09 +0300 |
commit | 396bc0c1721af8d3ee970228e7df457f6b2c73d5 (patch) | |
tree | 5aee4f0faec3fdfe616e1684dcb1736be9126bc1 /src/mainwindow | |
parent | Add singleapplication.wrap (diff) | |
download | smolbote-396bc0c1721af8d3ee970228e7df457f6b2c73d5.tar.xz |
Rewrite Session backend
Add session.hpp, containing structs that describe session data
MainWindow, SubWindow and WebView can be created from
Session::structs
Opening new window will automatically open a default subwindow and tab
if none were specified
Add lib/session_formats
Add JsonSession, to serialize/deserialize Session structs into JSON
- add some tests
clang-tidy:
- fix various warnings
- disable modernize-use-trailing-return-type check
Diffstat (limited to 'src/mainwindow')
-rw-r--r-- | src/mainwindow/mainwindow.cpp | 54 | ||||
-rw-r--r-- | src/mainwindow/mainwindow.h | 16 | ||||
-rw-r--r-- | src/mainwindow/menubar.cpp | 17 |
3 files changed, 68 insertions, 19 deletions
diff --git a/src/mainwindow/mainwindow.cpp b/src/mainwindow/mainwindow.cpp index b607a63..e16f34f 100644 --- a/src/mainwindow/mainwindow.cpp +++ b/src/mainwindow/mainwindow.cpp @@ -14,6 +14,7 @@ #include "configuration.h" #include "menubar.h" #include "webengine/webprofile.h" +#include "webengine/webprofilemanager.h" #include "webengine/webview.h" #include "widgets/dockwidget.h" #include "widgets/navigationbar.h" @@ -23,10 +24,9 @@ #include <QMessageBox> #include <QStatusBar> -MainWindow::MainWindow(QWidget *parent) +MainWindow::MainWindow(const Session::MainWindow &data, QWidget *parent) : QMainWindow(parent) { - Configuration config; // create UI @@ -46,7 +46,7 @@ MainWindow::MainWindow(QWidget *parent) addressBar = new AddressBar(this); navigationToolBar->addWidget(addressBar); - Browser *app = qobject_cast<Browser *>(qApp); + auto *app = qobject_cast<Browser *>(qApp); this->addToolBarBreak(); this->addToolBar(new BookmarksToolbar(app->bookmarks()->model(), this)); @@ -65,6 +65,14 @@ MainWindow::MainWindow(QWidget *parent) searchBox->setVisible(!searchBox->isVisible()); }); QMainWindow::addAction(searchAction); + + for(const auto &s : data.subwindows) { + createSubWindow(s); + } + if(m_subwindows.isEmpty()) { + const Session::SubWindow s; + createSubWindow(s); + } } void MainWindow::addDockWidget(Qt::DockWidgetArea area, QWidget *widget) @@ -72,18 +80,20 @@ void MainWindow::addDockWidget(Qt::DockWidgetArea area, QWidget *widget) QDockWidget *lastDock = nullptr; const auto docks = findChildren<QDockWidget *>(); for(QDockWidget *dock : docks) { - if(dockWidgetArea(dock) == area) + if(dockWidgetArea(dock) == area) { lastDock = dock; + } } - DockWidget *dock = new DockWidget(widget->windowTitle(), this); + auto *dock = new DockWidget(widget->windowTitle(), this); dock->setMinimumWidth(460); dock->setWidget(widget); - if(lastDock == nullptr) + if(lastDock == nullptr) { QMainWindow::addDockWidget(area, dock); - else + } else { tabifyDockWidget(lastDock, dock); + } } void MainWindow::removeDockWidget(QWidget *widget) @@ -105,8 +115,23 @@ void MainWindow::createTab(const QUrl &url) } } -SubWindow *MainWindow::createSubWindow(WebProfile *profile, bool openProfileNewtab) +void MainWindow::createTab(const Session::WebView &data) +{ + auto *w = qobject_cast<SubWindow *>(centralWidget()); + if(w != nullptr) { + w->addTab(data); + } +} + +SubWindow *MainWindow::createSubWindow(const Session::SubWindow &data) { + const auto *profileManager = WebProfileManager::instance(); + Q_CHECK_PTR(profileManager); + + auto *profile = profileManager->profile(data.profile); + if(profile == nullptr) { + profile = WebProfile::defaultProfile(); + } auto *w = new SubWindow(this); w->setProfile(profile); m_subwindows.append(w); @@ -114,8 +139,9 @@ SubWindow *MainWindow::createSubWindow(WebProfile *profile, bool openProfileNewt m_menuBar->insertSubWindow(w); connect(w, &SubWindow::windowTitleChanged, this, [this, w](const QString &title) { - if(w == currentSubWindow()) + if(w == currentSubWindow()) { setWindowTitle(QString("[%1] - %2").arg(title, defaultWindowTitle)); + } }); connect(w, &SubWindow::aboutToClose, this, [this, w]() { @@ -128,9 +154,13 @@ SubWindow *MainWindow::createSubWindow(WebProfile *profile, bool openProfileNewt } }); - if(openProfileNewtab) - w->addTab(w->profile()->newtab()); - + if(data.tabs.count() == 0) { + w->addTab(profile->newtab()); + return w; + } + for(const auto &tab : data.tabs) { + w->addTab(tab); + } return w; } diff --git a/src/mainwindow/mainwindow.h b/src/mainwindow/mainwindow.h index 98bd6ee..2b9f82b 100644 --- a/src/mainwindow/mainwindow.h +++ b/src/mainwindow/mainwindow.h @@ -9,7 +9,10 @@ #ifndef SMOLBOTE_MAINWINDOW_H #define SMOLBOTE_MAINWINDOW_H +#include "session.hpp" #include "subwindow/subwindow.h" +#include <QJsonArray> +#include <QJsonObject> #include <QMainWindow> #include <QUrl> @@ -31,9 +34,17 @@ public: ToolsMenu }; - explicit MainWindow(QWidget *parent = nullptr); + explicit MainWindow(const Session::MainWindow &data, QWidget *parent = nullptr); MainWindow(const MainWindow &) = delete; ~MainWindow() = default; + [[nodiscard]] Session::MainWindow serialize() const + { + QVector<Session::SubWindow> subwindows(m_subwindows.size()); + for(int i = 0; i < m_subwindows.size(); ++i) { + subwindows[i] = m_subwindows.at(i)->serialize(); + } + return { subwindows }; + } void addDockWidget(Qt::DockWidgetArea area, QWidget *widget); void removeDockWidget(QWidget *widget); @@ -53,7 +64,8 @@ public: public slots: void createTab(const QUrl &url); - SubWindow *createSubWindow(WebProfile *profile = nullptr, bool openProfileNewtab = false); + void createTab(const Session::WebView &data); + SubWindow *createSubWindow(const Session::SubWindow &data); void setCurrentSubWindow(SubWindow *subwindow); private slots: diff --git a/src/mainwindow/menubar.cpp b/src/mainwindow/menubar.cpp index c0bda61..83cafee 100644 --- a/src/mainwindow/menubar.cpp +++ b/src/mainwindow/menubar.cpp @@ -33,14 +33,16 @@ inline void run_if(SubWindow *_subwindow, const std::function<void(SubWindow *, int)> &f) { - if(_subwindow != nullptr) + if(_subwindow != nullptr) { f(_subwindow, _subwindow->currentTabIndex()); + } } inline void trigger_if(WebView *_view, QWebEnginePage::WebAction action) { - if(_view != nullptr) + if(_view != nullptr) { _view->triggerPageAction(action); + } } inline QDialog *createDevToolsDialog(QWebEnginePage *page) @@ -88,8 +90,9 @@ MenuBar::MenuBar(MainWindow *parent) connect(findMenu, &QMenu::aboutToShow, [findMenu, find_lineEdit]() { find_lineEdit->clear(); const auto actions = findMenu->actions(); - for(int i = 1; i < actions.length(); i++) + for(int i = 1; i < actions.length(); i++) { findMenu->removeAction(actions.at(i)); + } find_lineEdit->setFocus(); }); @@ -155,11 +158,15 @@ MenuBar::MenuBar(MainWindow *parent) window = this->addMenu(tr("&Window")); { - auto *actionNewWindow = window->addAction(tr("New Window"), browser, &Browser::createWindow); + auto *actionNewWindow = window->addAction(tr("New Window"), browser, [browser]() { + const Session::MainWindow window; + browser->open({ window }, false); + }); conf.shortcut<QAction>(*actionNewWindow, "shortcuts.window.newwindow"); auto *actionNewSubwindow = window->addAction(tr("New Subwindow"), parent, [parent]() { - parent->createSubWindow(nullptr, true); + const Session::SubWindow session; + parent->createSubWindow(session); }); conf.shortcut<QAction>(*actionNewSubwindow, "shortcuts.window.newgroup"); |