diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-07-01 18:13:01 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-07-01 18:13:01 +0200 |
commit | aa8198eec380659fd3538e058b50c24b0f88743c (patch) | |
tree | b9ae32ac4192de87054a8892d4e654a45737a04e /src/mainwindow | |
parent | Add browser.locale and browser.translation (diff) | |
download | smolbote-aa8198eec380659fd3538e058b50c24b0f88743c.tar.xz |
Code cleanup
Clean up MainWindow
Configuration is now a std::unique_ptr
Connect downloads and request interceptor to all profiles
Diffstat (limited to 'src/mainwindow')
-rw-r--r-- | src/mainwindow/mainwindow.cpp | 164 | ||||
-rw-r--r-- | src/mainwindow/mainwindow.h | 16 | ||||
-rw-r--r-- | src/mainwindow/mainwindow.ui | 118 | ||||
-rw-r--r-- | src/mainwindow/subwindow.cpp | 11 | ||||
-rw-r--r-- | src/mainwindow/subwindow.h | 3 |
5 files changed, 204 insertions, 108 deletions
diff --git a/src/mainwindow/mainwindow.cpp b/src/mainwindow/mainwindow.cpp index 7adc9db..741b544 100644 --- a/src/mainwindow/mainwindow.cpp +++ b/src/mainwindow/mainwindow.cpp @@ -7,6 +7,7 @@ */ #include "mainwindow.h" +#include "ui_mainwindow.h" #include "addressbar/addressbar.h" #include "browser.h" #include "subwindow.h" @@ -36,12 +37,14 @@ #include <KWindowEffects> #endif -MainWindow::MainWindow(std::shared_ptr<Configuration> &config, QWidget *parent) +MainWindow::MainWindow(const std::unique_ptr<Configuration> &config, QWidget *parent) : QMainWindow(parent) + , ui(new Ui::MainWindow) , mdiArea(new QMdiArea(this)) { Q_ASSERT(config); - m_config = config; + + ui->setupUi(this); #ifdef PLASMA_BLUR setAttribute(Qt::WA_TranslucentBackground, true); @@ -49,14 +52,69 @@ MainWindow::MainWindow(std::shared_ptr<Configuration> &config, QWidget *parent) #endif // create UI - setWindowTitle(QString::fromStdString(config->value<std::string>("mainwindow.title").value())); + setWindowTitle(config->value<QString>("mainwindow.title").value()); resize(config->value<int>("mainwindow.width").value(), config->value<int>("mainwindow.height").value()); if(config->value<bool>("mainwindow.maximized").value()) { setWindowState(Qt::WindowMaximized); } show(); - createMenuBar(); + // connect smolbote menu + { + connect(ui->actionNewSubwindow, &QAction::triggered, this, [this, &config]() { + auto *profile = WebProfile::defaultProfile(); + auto *window = createSubWindow(config, profile); + window->addTab(profile->newtab(), profile); + }); + config->setShortcut(ui->actionNewSubwindow, "mainwindow.shortcuts.newGroup"); + + connect(ui->actionNewWindow, &QAction::triggered, this, []() { + auto *browser = qobject_cast<Browser *>(qApp); + if(browser) + browser->createWindow(); + }); + config->setShortcut(ui->actionNewWindow, "mainwindow.shortcuts.newWindow"); + + connect(ui->actionAbout, &QAction::triggered, qobject_cast<Browser *>(qApp), &Browser::about); + config->setShortcut(ui->actionAbout, "mainwindow.shortcuts.about"); + + connect(ui->actionAboutQt, &QAction::triggered, qApp, &QApplication::aboutQt); + + connect(ui->actionQuit, &QAction::triggered, qApp, &QApplication::quit); + config->setShortcut(ui->actionQuit, "mainwindow.shortcuts.quit"); + } + + // connect session menu + { + connect(ui->actionSaveSession, &QAction::triggered, this, [this]() { + const QString filename = QFileDialog::getSaveFileName(this, tr("Save Session"), QDir::homePath(), tr("JSON (*.json)")); + QFile output(filename); + if(output.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) { + output.write(QJsonDocument(Session::toJsonObject(this)).toJson()); + output.close(); + } + }); + connect(ui->actionLoadSession, &QAction::triggered, this, [this]() { + const QString filename = QFileDialog::getOpenFileName(this, tr("Load Session"), QDir::homePath(), tr("JSON (*.json)")); + QFile json(filename); + if(json.open(QIODevice::ReadOnly | QIODevice::Text)) { + auto *browser = qobject_cast<Browser *>(qApp); + browser->sendMessage(json.readAll()); + json.close(); + } + }); + } + + // connect window menu + { + connect(ui->actionTileWindows, &QAction::triggered, mdiArea, &QMdiArea::tileSubWindows); + config->setShortcut(ui->actionTileWindows, "mainwindow.shortcuts.tileWindows"); + + connect(ui->actionCascadeWindows, &QAction::triggered, mdiArea, &QMdiArea::cascadeSubWindows); + config->setShortcut(ui->actionCascadeWindows, "mainwindow.shortcuts.cascadeWindows"); + + subWindowAction = ui->actionCurrentWindow; + } navigationToolBar = new NavigationBar(config->section("navigation"), this); navigationToolBar->setMovable(config->value<bool>("navigation.movable").value()); @@ -117,7 +175,7 @@ MainWindow::MainWindow(std::shared_ptr<Configuration> &config, QWidget *parent) // search box auto *searchAction = new QAction(this); - m_config->setShortcut(searchAction, "mainwindow.shortcuts.search"); + config->setShortcut(searchAction, "mainwindow.shortcuts.search"); connect(searchAction, &QAction::triggered, this, [=]() { /* QTBUG-18665 * When focusing out of the search box and hiding it, the first @@ -143,87 +201,11 @@ MainWindow::~MainWindow() disconnect(addressBar); } -void MainWindow::createMenuBar() -{ - Q_CHECK_PTR(mdiArea); - - // smolbote menu - auto *smolboteMenu = menuBar()->addMenu(qApp->applicationDisplayName()); - - auto *subwindowAction = smolboteMenu->addAction(tr("New subwindow"), this, [this]() { - createSubWindow(); - }); - m_config->setShortcut(subwindowAction, "mainwindow.shortcuts.newGroup"); - - auto *windowAction = smolboteMenu->addAction(tr("New window"), this, []() { - auto *browser = qobject_cast<Browser *>(qApp); - if(browser) - browser->createWindow(); - }); - m_config->setShortcut(windowAction, "mainwindow.shortcuts.newWindow"); - - smolboteMenu->addSeparator(); - - auto *aboutAction = smolboteMenu->addAction(tr("About"), qobject_cast<Browser *>(qApp), &Browser::about); - m_config->setShortcut(aboutAction, "mainwindow.shortcuts.about"); - smolboteMenu->addAction(tr("About Qt"), qApp, &QApplication::aboutQt); - - smolboteMenu->addSeparator(); - - auto *quitAction = smolboteMenu->addAction(tr("Quit"), qApp, &QApplication::quit); - m_config->setShortcut(quitAction, "mainwindow.shortcuts.quit"); - - // session menu - auto *sessionMenu = menuBar()->addMenu(tr("Session")); - - sessionMenu->addAction(tr("Save Session"), this, [this]() { - const QString filename = QFileDialog::getSaveFileName(this, tr("Save Session"), QDir::homePath(), tr("JSON (*.json)")); - QFile output(filename); - if(output.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) { - output.write(QJsonDocument(Session::toJsonObject(this)).toJson()); - output.close(); - } - }); - sessionMenu->addAction(tr("Load Session"), this, [this]() { - const QString filename = QFileDialog::getOpenFileName(this, tr("Load Session"), QDir::homePath(), tr("JSON (*.json)")); - QFile json(filename); - if(json.open(QIODevice::ReadOnly | QIODevice::Text)) { - auto *browser = qobject_cast<Browser *>(qApp); - browser->sendMessage(json.readAll()); - json.close(); - } - }); - - // window menu - auto *windowMenu = menuBar()->addMenu(tr("Window")); - - auto *tileAction = windowMenu->addAction(tr("Tile windows"), mdiArea, &QMdiArea::tileSubWindows); - m_config->setShortcut(tileAction, "mainwindow.shortcuts.tileWindows"); - - auto *cascadeAction = windowMenu->addAction(tr("Cascade windows"), mdiArea, &QMdiArea::cascadeSubWindows); - m_config->setShortcut(cascadeAction, "mainwindow.shortcuts.cascadeWindows"); - - subWindowAction = windowMenu->addAction(tr("Current window")); - - // tools menu - toolsMenu = menuBar()->addMenu(tr("Tools")); - - // debug menu -#ifdef QT_DEBUG - auto *debugMenu = menuBar()->addMenu(tr("Debug")); - - debugMenu->addAction(tr("Print window session"), [this]() { - auto json = Session::toJsonObject(this); - qDebug("session data >>>\n%s\n<<<", qUtf8Printable(QJsonDocument(json).toJson())); - }); -#endif -} - void MainWindow::addAction(ActionLocation where, QAction *action) { switch(where) { case ToolsMenu: - toolsMenu->addAction(action); + ui->menuTools->addAction(action); break; default: QMainWindow::addAction(action); @@ -264,7 +246,9 @@ void MainWindow::createTab(const QUrl &url) { auto *w = qobject_cast<SubWindow *>(mdiArea->currentSubWindow()); if(w == nullptr) { - w = createSubWindow(url.toString()); + //w = createSubWindow(url.toString()); +// w = createSubWindow(WebProfile::defaultProfile()); +// w->addTab(url); } else { w->addTab(url); } @@ -287,9 +271,9 @@ SubWindow *MainWindow::currentSubWindow() const return qobject_cast<SubWindow *>(mdiArea->currentSubWindow()); } -SubWindow *MainWindow::createSubWindow(WebProfile *profile) +SubWindow *MainWindow::createSubWindow(const std::unique_ptr<Configuration> &config, WebProfile *profile) { - auto *w = new SubWindow(m_config->section("window"), this); + auto *w = new SubWindow(config, this); w->setProfile(profile); mdiArea->addSubWindow(w); w->showMaximized(); @@ -297,16 +281,6 @@ SubWindow *MainWindow::createSubWindow(WebProfile *profile) return w; } -SubWindow *MainWindow::createSubWindow(const QString &url) -{ - auto *w = new SubWindow(m_config->section("window"), this); - mdiArea->addSubWindow(w); - w->showMaximized(); - w->setFocus(); - w->addTab(url); - return w; -} - void MainWindow::setView(WebView *view) { if(currentView) { diff --git a/src/mainwindow/mainwindow.h b/src/mainwindow/mainwindow.h index 287602a..d695dd8 100644 --- a/src/mainwindow/mainwindow.h +++ b/src/mainwindow/mainwindow.h @@ -22,6 +22,11 @@ class SearchForm; class WebView; class NavigationBar; class WebProfile; + +namespace Ui { +class MainWindow; +} + class MainWindow : public QMainWindow { friend class Browser; @@ -33,12 +38,10 @@ public: ToolsMenu }; - explicit MainWindow(std::shared_ptr<Configuration> &config, QWidget *parent = nullptr); + explicit MainWindow(const std::unique_ptr<Configuration> &config, QWidget *parent = nullptr); Q_DISABLE_COPY(MainWindow) ~MainWindow() override; - void createMenuBar(); - void addAction(ActionLocation where, QAction *action); void addDockWidget(Qt::DockWidgetArea area, QWidget *widget); void removeDockWidget(QWidget *widget); @@ -48,15 +51,16 @@ public: public slots: void createTab(const QUrl &url); - SubWindow *createSubWindow(WebProfile *profile); - SubWindow *createSubWindow(const QString &url = QString()); + SubWindow *createSubWindow(const std::unique_ptr<Configuration> &config, WebProfile *profile); +private slots: void setView(WebView *view); protected: void closeEvent(QCloseEvent *event) override; private: + Ui::MainWindow *ui; QAction *subWindowAction = nullptr; QMenu *toolsMenu = nullptr; @@ -67,8 +71,6 @@ private: QMdiArea *mdiArea; WebView *currentView = nullptr; - std::shared_ptr<Configuration> m_config; - QMetaObject::Connection viewChangedConnection; QMetaObject::Connection searchBoxConnection; QMetaObject::Connection statusBarConnection; diff --git a/src/mainwindow/mainwindow.ui b/src/mainwindow/mainwindow.ui new file mode 100644 index 0000000..72e0b39 --- /dev/null +++ b/src/mainwindow/mainwindow.ui @@ -0,0 +1,118 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>MainWindow</class> + <widget class="QMainWindow" name="MainWindow"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>800</width> + <height>600</height> + </rect> + </property> + <property name="windowTitle"> + <string>MainWindow</string> + </property> + <widget class="QWidget" name="centralwidget"/> + <widget class="QMenuBar" name="menubar"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>800</width> + <height>23</height> + </rect> + </property> + <widget class="QMenu" name="menusmolbote"> + <property name="title"> + <string>s&molbote</string> + </property> + <addaction name="actionNewSubwindow"/> + <addaction name="actionNewWindow"/> + <addaction name="separator"/> + <addaction name="actionAbout"/> + <addaction name="actionAboutQt"/> + <addaction name="separator"/> + <addaction name="actionQuit"/> + </widget> + <widget class="QMenu" name="menuSession"> + <property name="title"> + <string>Session</string> + </property> + <addaction name="actionSaveSession"/> + <addaction name="actionLoadSession"/> + </widget> + <widget class="QMenu" name="menuWindow"> + <property name="title"> + <string>Wi&ndow</string> + </property> + <addaction name="actionTileWindows"/> + <addaction name="actionCascadeWindows"/> + <addaction name="separator"/> + <addaction name="actionCurrentWindow"/> + </widget> + <widget class="QMenu" name="menuTools"> + <property name="title"> + <string>Too&ls</string> + </property> + </widget> + <addaction name="menusmolbote"/> + <addaction name="menuSession"/> + <addaction name="menuWindow"/> + <addaction name="menuTools"/> + </widget> + <widget class="QStatusBar" name="statusbar"/> + <action name="actionNewSubwindow"> + <property name="text"> + <string>&New Subwindow</string> + </property> + </action> + <action name="actionNewWindow"> + <property name="text"> + <string>New &Window</string> + </property> + </action> + <action name="actionAbout"> + <property name="text"> + <string>&About</string> + </property> + </action> + <action name="actionAboutQt"> + <property name="text"> + <string>A&bout Qt</string> + </property> + </action> + <action name="actionQuit"> + <property name="text"> + <string>&Quit</string> + </property> + </action> + <action name="actionSaveSession"> + <property name="text"> + <string>&Save Session</string> + </property> + </action> + <action name="actionLoadSession"> + <property name="text"> + <string>&Load Session</string> + </property> + </action> + <action name="actionTileWindows"> + <property name="text"> + <string>Tile Windows</string> + </property> + </action> + <action name="actionCascadeWindows"> + <property name="text"> + <string>Cascade Windows</string> + </property> + </action> + <action name="actionCurrentWindow"> + <property name="text"> + <string>Current Window</string> + </property> + </action> + </widget> + <resources/> + <connections/> +</ui> diff --git a/src/mainwindow/subwindow.cpp b/src/mainwindow/subwindow.cpp index 5a1eeda..342fce8 100644 --- a/src/mainwindow/subwindow.cpp +++ b/src/mainwindow/subwindow.cpp @@ -20,8 +20,9 @@ #include <QToolButton> #include <webprofile.h> #include "profilemanager.h" +#include <configuration.h> -SubWindow::SubWindow(const QHash<QString, QString> &config, QWidget *parent, Qt::WindowFlags flags) +SubWindow::SubWindow(const std::unique_ptr<Configuration> &config, QWidget *parent, Qt::WindowFlags flags) : QMdiSubWindow(parent, flags) , tabWidget(new TabWidget(this)) { @@ -62,7 +63,7 @@ SubWindow::SubWindow(const QHash<QString, QString> &config, QWidget *parent, Qt: 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"))); + newTab_button->setShortcut(QKeySequence(config->value<QString>("window.shortcuts.new").value())); connect(newTab_button, &QToolButton::clicked, this, [=]() { auto index = addTab(WebProfile::defaultProfile()->newtab()); tabWidget->setCurrentIndex(index); @@ -70,17 +71,17 @@ SubWindow::SubWindow(const QHash<QString, QString> &config, QWidget *parent, Qt: tabWidget->setCornerWidget(newTab_button, Qt::TopRightCorner); // general actions - auto *closeTab_shortcut = new QShortcut(QKeySequence(config.value("window.shortcuts.close")), this); + auto *closeTab_shortcut = new QShortcut(QKeySequence(config->value<QString>("window.shortcuts.close").value()), this); connect(closeTab_shortcut, &QShortcut::activated, this, [=]() { tabWidget->deleteTab(tabWidget->currentIndex()); }); - auto *leftTab_shortcut = new QShortcut(QKeySequence(config.value("window.shortcuts.left")), this); + auto *leftTab_shortcut = new QShortcut(QKeySequence(config->value<QString>("window.shortcuts.left").value()), 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); + auto *rightTab_shortcut = new QShortcut(QKeySequence(config->value<QString>("window.shortcuts.right").value()), this); connect(rightTab_shortcut, &QShortcut::activated, this, [=]() { tabWidget->setCurrentIndex(qMin(tabWidget->currentIndex() + 1, tabWidget->count() - 1)); }); diff --git a/src/mainwindow/subwindow.h b/src/mainwindow/subwindow.h index 1283481..b7c4aee 100644 --- a/src/mainwindow/subwindow.h +++ b/src/mainwindow/subwindow.h @@ -16,12 +16,13 @@ class TabWidget; class WebView; class WebProfile; +class Configuration; class SubWindow : public QMdiSubWindow { Q_OBJECT public: - explicit SubWindow(const QHash<QString, QString> &config, QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags()); + explicit SubWindow(const std::unique_ptr<Configuration> &config, QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags()); ~SubWindow() override; WebView *currentView(); |