From aa8198eec380659fd3538e058b50c24b0f88743c Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sun, 1 Jul 2018 18:13:01 +0200 Subject: Code cleanup Clean up MainWindow Configuration is now a std::unique_ptr Connect downloads and request interceptor to all profiles --- src/CMakeLists.txt | 1 + src/browser.cpp | 37 ++++++---- src/browser.h | 8 +-- src/main.cpp | 45 +++++------- src/mainwindow/mainwindow.cpp | 164 ++++++++++++++++++------------------------ src/mainwindow/mainwindow.h | 16 +++-- src/mainwindow/mainwindow.ui | 118 ++++++++++++++++++++++++++++++ src/mainwindow/subwindow.cpp | 11 +-- src/mainwindow/subwindow.h | 3 +- 9 files changed, 249 insertions(+), 154 deletions(-) create mode 100644 src/mainwindow/mainwindow.ui (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bb8f229..8778b18 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -22,6 +22,7 @@ add_executable(poi # main window mainwindow/mainwindow.cpp mainwindow/mainwindow.h + mainwindow/mainwindow.ui mainwindow/subwindow.cpp mainwindow/subwindow.h mainwindow/widgets/dockwidget.cpp diff --git a/src/browser.cpp b/src/browser.cpp index b793bb1..6ffcaaa 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -52,14 +52,16 @@ void Browser::about() dlg->exec(); } -void Browser::setConfiguration(std::shared_ptr &config) +void Browser::setConfiguration(std::unique_ptr &config) { Q_ASSERT(config); - m_config = config; + m_config = std::move(config); } void Browser::registerPlugin(const Plugin &plugin) { + Q_ASSERT(m_config); + if(plugin.instance->inherits("ProfileInterface")) { auto *profileEditor = qobject_cast(plugin.instance); Q_ASSERT_X(profileEditor != nullptr, "Browser::setup", "profile interface cast failed"); @@ -88,6 +90,21 @@ void Browser::setup(const QString &defaultProfile) { Q_ASSERT_X(m_config, "Browser::setup", "Configuration not set"); + auto stylesheet = m_config->value("browser.stylesheet"); + if(stylesheet) { + QFile f(stylesheet.value()); + if(f.open(QIODevice::ReadOnly)) { + setStyleSheet(f.readAll()); + f.close(); + } + } + + // downloads + m_downloads = std::make_unique(m_config->value("downloads.path").value()); + // url request filter + m_urlFilter = std::make_unique(m_config->value("filter.path").value()); + // cookie request filter + // load profiles { const auto defaults = m_config->section("profile"); @@ -98,6 +115,8 @@ void Browser::setup(const QString &defaultProfile) for(const QFileInfo &f : entries) { auto *profile = ProfileManager::loadProfile(f.absoluteFilePath(), defaults); + connect(profile, &WebProfile::downloadRequested, m_downloads.get(), &DownloadsWidget::addDownload); + profile->setRequestInterceptor(m_urlFilter.get()); emit registerProfile(profile); } } @@ -106,26 +125,18 @@ void Browser::setup(const QString &defaultProfile) if(ProfileManager::profile(defaultProfile) == nullptr) { // if this profile has not been added, it doesn't have a path auto *profile = ProfileManager::loadProfile(QString(), defaults); + connect(profile, &WebProfile::downloadRequested, m_downloads.get(), &DownloadsWidget::addDownload); + profile->setRequestInterceptor(m_urlFilter.get()); emit registerProfile(profile); } WebProfile::setDefaultProfile(ProfileManager::profile(defaultProfile)); } - // url request filter - m_urlFilter = std::make_shared(QString::fromStdString(m_config->value("filter.path").value())); - WebProfile::defaultProfile()->setRequestInterceptor(m_urlFilter.get()); - - // cookie request filter - // bookmarks m_bookmarks = std::make_shared(QString::fromStdString(m_config->value("bookmarks.path").value())); connect(m_bookmarks.get(), &BookmarksWidget::openUrl, this, [this](const QUrl &url) { m_windows.last()->createTab(url); }); - - // downloads - m_downloads = std::make_shared(QString::fromStdString(m_config->value("downloads.path").value())); - connect(WebProfile::defaultProfile(), &WebProfile::downloadRequested, m_downloads.get(), &DownloadsWidget::addDownload); } void Browser::createSession(const QJsonObject &object) @@ -152,7 +163,7 @@ void Browser::createSession(const QJsonObject &object) } } if(window == nullptr) - window = mainwindow->createSubWindow(profile); + window = mainwindow->createSubWindow(m_config, profile); const QJsonArray tabs = subwindow.value("tabs").toArray(); if(tabs.isEmpty()) diff --git a/src/browser.h b/src/browser.h index 0fda721..3405e85 100644 --- a/src/browser.h +++ b/src/browser.h @@ -36,7 +36,7 @@ public slots: void about(); public: - void setConfiguration(std::shared_ptr &config); + void setConfiguration(std::unique_ptr &config); void registerPlugin(const Plugin &plugin); void setup(const QString &defaultProfile); @@ -56,10 +56,10 @@ public slots: private: Q_DISABLE_COPY(Browser) - std::shared_ptr m_config; + std::unique_ptr m_config; std::shared_ptr m_bookmarks; - std::shared_ptr m_downloads; - std::shared_ptr m_urlFilter; + std::unique_ptr m_downloads; + std::unique_ptr m_urlFilter; QVector m_windows; QVector m_plugins; diff --git a/src/main.cpp b/src/main.cpp index 85bf5f0..00a57d5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,7 +20,7 @@ int main(int argc, char **argv) { // create and load configuration - std::shared_ptr config = std::make_shared(); + std::unique_ptr config = std::make_unique(nullptr); #ifdef QT_DEBUG QObject::connect(config.get(), &Configuration::settingChanged, [](const std::string &path, const QString &value) { qDebug("!!! setting changed %s=[%s]", path.c_str(), qUtf8Printable(value)); @@ -83,7 +83,6 @@ int main(int argc, char **argv) Browser app(argc, argv); // set this, otherwise the webview becomes black when using a stylesheet app.setAttribute(Qt::AA_DontCreateNativeWidgetSiblings, true); - app.setConfiguration(config); // translator if(config->exists("browser.locale")) { @@ -102,15 +101,21 @@ int main(int argc, char **argv) delete translator; } + // command line arguments + auto arguments = config->value>("args"); + + auto socket = config->value("socket"); + auto session = config->value("browser.session"); + auto profile = config->value("profile.default"); + + app.setConfiguration(config); + app.setup(profile.value()); - app.setup(QString::fromStdString(config->value("profile.default").value())); for(const Plugin &plugin : plugins) { app.registerPlugin(plugin); } - auto arguments = config->value>("args"); QStringList urls; - if(arguments) { for(const auto &u : arguments.value()) { if(pluginCommands.contains(QString::fromStdString(u))) { @@ -122,43 +127,25 @@ int main(int argc, char **argv) } // set up socket - bool isSingleInstance = app.bindLocalSocket(QString::fromStdString(config->value("socket").value())); + bool isSingleInstance = app.bindLocalSocket(socket.value()); + if(isSingleInstance) { #ifdef QT_DEBUG - qDebug("bindLocalSocket(%s) = %s", qUtf8Printable(QString::fromStdString(config->value("socket").value())), isSingleInstance ? "true" : "false"); + qDebug("Local socket bound"); #endif - // if we are the only instance, set up the browser - if(isSingleInstance) { - auto stylesheet = config->value("browser.stylesheet"); - if(stylesheet) { - QFile f(QString::fromStdString(stylesheet.value())); - if(f.open(QIODevice::ReadOnly)) { - app.setStyleSheet(f.readAll()); - f.close(); - } - } - QObject::connect(&app, &Browser::messageAvailable, &app, &Browser::createSession); } - if(config->exists("session")) { - QFile sessionJson(config->value("session").value()); + if(session) { + QFile sessionJson(session.value()); if(sessionJson.open(QIODevice::ReadOnly | QIODevice::Text)) { app.sendMessage(sessionJson.readAll()); sessionJson.close(); } else { qWarning("Could not open session [%s].", qUtf8Printable(sessionJson.fileName())); } - } else if(config->exists("browser.session")) { - QFile sessionJson(config->value("browser.session").value()); - if(sessionJson.open(QIODevice::ReadOnly | QIODevice::Text)) { - app.sendMessage(sessionJson.readAll()); - sessionJson.close(); - } else { - qWarning("Could not open browser.session [%s].", qUtf8Printable(sessionJson.fileName())); - } } else - app.sendMessage(Session::toJsonObject(config->value("profile.default").value(), urls)); + app.sendMessage(Session::toJsonObject(profile.value(), urls)); if(isSingleInstance) return app.exec(); 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 #endif -MainWindow::MainWindow(std::shared_ptr &config, QWidget *parent) +MainWindow::MainWindow(const std::unique_ptr &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 &config, QWidget *parent) #endif // create UI - setWindowTitle(QString::fromStdString(config->value("mainwindow.title").value())); + setWindowTitle(config->value("mainwindow.title").value()); resize(config->value("mainwindow.width").value(), config->value("mainwindow.height").value()); if(config->value("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(qApp); + if(browser) + browser->createWindow(); + }); + config->setShortcut(ui->actionNewWindow, "mainwindow.shortcuts.newWindow"); + + connect(ui->actionAbout, &QAction::triggered, qobject_cast(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(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("navigation.movable").value()); @@ -117,7 +175,7 @@ MainWindow::MainWindow(std::shared_ptr &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(qApp); - if(browser) - browser->createWindow(); - }); - m_config->setShortcut(windowAction, "mainwindow.shortcuts.newWindow"); - - smolboteMenu->addSeparator(); - - auto *aboutAction = smolboteMenu->addAction(tr("About"), qobject_cast(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(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(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(mdiArea->currentSubWindow()); } -SubWindow *MainWindow::createSubWindow(WebProfile *profile) +SubWindow *MainWindow::createSubWindow(const std::unique_ptr &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 &config, QWidget *parent = nullptr); + explicit MainWindow(const std::unique_ptr &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 &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 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 @@ + + + MainWindow + + + + 0 + 0 + 800 + 600 + + + + MainWindow + + + + + + 0 + 0 + 800 + 23 + + + + + s&molbote + + + + + + + + + + + + Session + + + + + + + Wi&ndow + + + + + + + + + Too&ls + + + + + + + + + + + &New Subwindow + + + + + New &Window + + + + + &About + + + + + A&bout Qt + + + + + &Quit + + + + + &Save Session + + + + + &Load Session + + + + + Tile Windows + + + + + Cascade Windows + + + + + Current Window + + + + + + 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 #include #include "profilemanager.h" +#include -SubWindow::SubWindow(const QHash &config, QWidget *parent, Qt::WindowFlags flags) +SubWindow::SubWindow(const std::unique_ptr &config, QWidget *parent, Qt::WindowFlags flags) : QMdiSubWindow(parent, flags) , tabWidget(new TabWidget(this)) { @@ -62,7 +63,7 @@ SubWindow::SubWindow(const QHash &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("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 &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("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("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("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 &config, QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags()); + explicit SubWindow(const std::unique_ptr &config, QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags()); ~SubWindow() override; WebView *currentView(); -- cgit v1.2.1