From 4eedf60d76a047f63b0991eee0b623e9be854c76 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Thu, 13 Dec 2018 12:49:28 +0100 Subject: MainWindow: rework menu bar Split off menu bar into its own class out of MainWindow Menu bar now has a 'Find in menus' function --- src/mainwindow/mainwindow.cpp | 130 ++--------------- src/mainwindow/mainwindow.h | 17 +-- src/mainwindow/mainwindow.ui | 202 -------------------------- src/mainwindow/menubar.cpp | 239 +++++++++++++++++++++++++++++++ src/mainwindow/menubar.h | 28 ++++ src/mainwindow/widgets/menusearch.cpp | 17 +++ src/mainwindow/widgets/menusearch.h | 20 +++ src/mainwindow/widgets/navigationbar.cpp | 4 +- 8 files changed, 322 insertions(+), 335 deletions(-) delete mode 100644 src/mainwindow/mainwindow.ui create mode 100644 src/mainwindow/menubar.cpp create mode 100644 src/mainwindow/menubar.h create mode 100644 src/mainwindow/widgets/menusearch.cpp create mode 100644 src/mainwindow/widgets/menusearch.h (limited to 'src/mainwindow') diff --git a/src/mainwindow/mainwindow.cpp b/src/mainwindow/mainwindow.cpp index 1544e79..ea024c5 100644 --- a/src/mainwindow/mainwindow.cpp +++ b/src/mainwindow/mainwindow.cpp @@ -11,11 +11,11 @@ #include "browser.h" #include "config.h" #include "configuration.h" +#include "menubar.h" #include "profilemanager.h" #include "session/session.h" #include "session/sessiondialog.h" #include "subwindow/subwindow.h" -#include "ui_mainwindow.h" #include "webengine/webview.h" #include "webprofile.h" #include "widgets/dockwidget.h" @@ -31,9 +31,6 @@ #include #include #include -#include -#include -#include #include #include #include @@ -71,12 +68,13 @@ inline QDialog *createDevToolsDialog(QWebEnginePage *page) MainWindow::MainWindow(const std::unique_ptr &config, QWidget *parent) : QMainWindow(parent) - , ui(new Ui::MainWindow) + , configuration(config.get()) , mdiArea(new QMdiArea(this)) { Q_ASSERT(config); - ui->setupUi(this); + m_menuBar = new MenuBar(config.get(), this); + this->setMenuBar(m_menuBar); #ifdef CONFIG_PLASMA_BLUR setAttribute(Qt::WA_TranslucentBackground, true); @@ -91,98 +89,6 @@ MainWindow::MainWindow(const std::unique_ptr &config, QWidget *pa } show(); - // connect smolbote menu - { - connect(ui->actionNewSubwindow, &QAction::triggered, this, [this, &config]() { - auto *profile = WebProfile::defaultProfile(); - auto *window = createSubWindow(config.get(), 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 - { - const QString sessionPath = config->value("browser.session.path").value(); - connect(ui->actionSaveSession, &QAction::triggered, this, [this, sessionPath]() { - const QString filename = QFileDialog::getSaveFileName(this, tr("Save Session"), sessionPath, tr("JSON (*.json)")); - QFile output(filename); - if(output.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) { - auto *browser = qobject_cast(qApp); - auto data = Session::_session(browser->windows()); - output.write(QJsonDocument(data).toJson()); - output.close(); - } - }); - - connect(ui->actionLoadSession, &QAction::triggered, this, [this]() { - auto *sessionDialog = new SessionDialog(this); - sessionDialog->exec(); - }); - } - - // connect window menu - { - connect(ui->actionTileSubwindows, &QAction::triggered, mdiArea, &QMdiArea::tileSubWindows); - config->setShortcut(ui->actionTileSubwindows, "mainwindow.shortcuts.tileWindows"); - - connect(ui->actionCascadeSubwindows, &QAction::triggered, mdiArea, &QMdiArea::cascadeSubWindows); - config->setShortcut(ui->actionCascadeSubwindows, "mainwindow.shortcuts.cascadeWindows"); - } - - // connect page menu - { - connect(ui->actionBookmarkPage, &QAction::triggered, this, [this]() { - if(currentView != nullptr) - emit createBookmark(currentView->title(), currentView->url().toString()); - }); - connect(ui->actionSavePage, &QAction::triggered, this, [this]() { - if(currentView != nullptr) - currentView->triggerPageAction(QWebEnginePage::SavePage); - }); - connect(ui->actionPrintPage, &QAction::triggered, this, [this]() { - if(currentView != nullptr) { - auto *printer = new QPrinter(QPrinterInfo::defaultPrinter()); - QPrintDialog dlg(printer, this); - if(dlg.exec() == QDialog::Accepted) { - currentView->page()->print(printer, [printer](bool success) { - Q_UNUSED(success); - delete printer; - }); - } - } - }); - connect(ui->actionPrintPageToPdf, &QAction::triggered, this, [this]() { - if(currentView != nullptr) { - const QString path = QFileDialog::getSaveFileName(this, tr("Print to PDF"), QDir::homePath(), tr("PDF files (*.pdf)")); - currentView->page()->printToPdf(path); - } - }); - - connect(ui->actionDeveloperTools, &QAction::triggered, this, [this]() { - if(currentView != nullptr) - createDevToolsDialog(currentView->page())->show(); - }); - - pageLoadProfileMenu = ui->menuPage->addMenu(tr("Load Profile")); - connect(pageLoadProfileMenu, &QMenu::aboutToShow, this, &MainWindow::updatePageLoadProfileMenu); - } - // current subwindow shortcut { QAction *subwindowMenuAction = new QAction(this); @@ -221,16 +127,13 @@ MainWindow::MainWindow(const std::unique_ptr &config, QWidget *pa disconnect(viewChangedConnection); disconnect(searchBoxConnection); disconnect(statusBarConnection); - ui->actionCurrentSubwindow->setMenu(nullptr); auto *w = qobject_cast(window); if(w == nullptr) { // no current subwindow, clear everything setView(nullptr); - ui->actionCurrentSubwindow->setMenu(nullptr); } else { setView(w->currentView()); - ui->actionCurrentSubwindow->setMenu(w->systemMenu()); viewChangedConnection = connect(w, &SubWindow::currentViewChanged, this, &MainWindow::setView); statusBarConnection = connect(w, &SubWindow::showStatusMessage, statusBar(), &QStatusBar::showMessage); } @@ -340,7 +243,7 @@ SubWindow *MainWindow::currentSubWindow() const return qobject_cast(mdiArea->currentSubWindow()); } -SubWindow *MainWindow::createSubWindow(const Configuration *config, WebProfile *profile) +SubWindow *MainWindow::createSubWindow(WebProfile *profile, bool openProfileNewtab) { bool shouldMaximize = true; // if there is a current window, use its maximize state @@ -348,7 +251,8 @@ SubWindow *MainWindow::createSubWindow(const Configuration *config, WebProfile * shouldMaximize = currentWindow->isMaximized(); } - auto *w = new SubWindow(config, this); + auto *w = new SubWindow(configuration, this); + w->setProfile(profile); mdiArea->addSubWindow(w); if(shouldMaximize) @@ -357,6 +261,10 @@ SubWindow *MainWindow::createSubWindow(const Configuration *config, WebProfile * w->show(); w->setFocus(); + + if(openProfileNewtab) + w->addTab(w->profile()->newtab()); + return w; } @@ -384,22 +292,6 @@ void MainWindow::setView(WebView *view) searchBox->setView(view); } -void MainWindow::updatePageLoadProfileMenu() -{ - Q_CHECK_PTR(pageLoadProfileMenu); - pageLoadProfileMenu->clear(); - - if(currentView == nullptr) - return; - - auto *profileManager = dynamic_cast(qApp)->getProfileManager(); - pageLoadProfileMenu->addActions(profileManager->createProfileMenu([this](WebProfile *profile) { - this->currentView->setProfile(profile); - }, - this) - ->actions()); -} - void MainWindow::closeEvent(QCloseEvent *event) { if(mdiArea->subWindowList().count() > 1) { diff --git a/src/mainwindow/mainwindow.h b/src/mainwindow/mainwindow.h index de77540..44ee633 100644 --- a/src/mainwindow/mainwindow.h +++ b/src/mainwindow/mainwindow.h @@ -22,15 +22,11 @@ class SearchForm; class WebView; class NavigationBar; class WebProfile; - -namespace Ui -{ -class MainWindow; -} - +class MenuBar; class MainWindow : public QMainWindow { friend class Browser; + friend class MenuBar; Q_OBJECT @@ -49,22 +45,19 @@ public: const QVector subWindows() const; SubWindow *currentSubWindow() const; -signals: - void createBookmark(const QString &title, const QString &url); - public slots: void createTab(const QUrl &url); - SubWindow *createSubWindow(const Configuration *config, WebProfile *profile); + SubWindow *createSubWindow(WebProfile *profile = nullptr, bool openProfileNewtab = false); private slots: void setView(WebView *view); - void updatePageLoadProfileMenu(); protected: void closeEvent(QCloseEvent *event) override; private: - Ui::MainWindow *ui; + const Configuration *configuration = nullptr; + MenuBar *m_menuBar = nullptr; QMenu *toolsMenu = nullptr; QMenu *pageLoadProfileMenu = nullptr; diff --git a/src/mainwindow/mainwindow.ui b/src/mainwindow/mainwindow.ui deleted file mode 100644 index 19cecbb..0000000 --- a/src/mainwindow/mainwindow.ui +++ /dev/null @@ -1,202 +0,0 @@ - - - MainWindow - - - - 0 - 0 - 800 - 600 - - - - MainWindow - - - - - - 0 - 0 - 800 - 30 - - - - - s&molbote - - - - - - - - - - - - Sessio&n - - - - - - - S&ubwindow - - - - - - - - - Too&ls - - - - - - - - - - Pa&ge - - - - - - - - - - - - - - - - - - &New Subwindow - - - - - - - - New &Window - - - - - - - - &About - - - - - A&bout Qt - - - - - - - - &Quit - - - - - &Save Session - - - - - &Load Session - - - - - &Tile Subwindows - - - - - &Cascade Subwindows - - - - - Current &Subwindow - - - - - &Create Bookmark - - - - - - - - &Save Page - - - - - - - - &Print Page - - - - - - - - P&rint to PDF - - - - - &Developer Tools - - - - - - - - &Bookmarks - - - - - - - - &Downloads - - - - - - - - &Add Plugin - - - - - - diff --git a/src/mainwindow/menubar.cpp b/src/mainwindow/menubar.cpp new file mode 100644 index 0000000..0429ffd --- /dev/null +++ b/src/mainwindow/menubar.cpp @@ -0,0 +1,239 @@ +/* + * 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/gitea/aqua/smolbote + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#include "menubar.h" +#include "bookmarkswidget.h" +#include "browser.h" +#include "configuration.h" +#include "downloadswidget.h" +#include "mainwindow.h" +#include "profilemanager.h" +#include "session/sessiondialog.h" +#include "subwindow/subwindow.h" +#include "webengine/webview.h" +#include "widgets/menusearch.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +MenuBar::MenuBar(const Configuration *config, MainWindow *parent) + : QMenuBar(parent) +{ + auto *browser = qobject_cast(qApp); + Q_CHECK_PTR(browser); + + smolbote = this->addMenu(qApp->applicationName()); + { + auto *findMenu = smolbote->addMenu(tr("Find in menus")); + + auto *findWidget = new QWidgetAction(this); + auto *find_lineEdit = new MenuSearch(this); + findWidget->setDefaultWidget(find_lineEdit); + findMenu->addAction(findWidget); + + connect(findMenu, &QMenu::aboutToShow, [findMenu, find_lineEdit]() { + find_lineEdit->clear(); + const auto actions = findMenu->actions(); + for(int i = 1; i < actions.length(); i++) + findMenu->removeAction(actions.at(i)); + find_lineEdit->setFocus(); + }); + + connect(find_lineEdit, &QLineEdit::textEdited, [this, findMenu](const QString &text) { + // clear menu + const auto actions = findMenu->actions(); + for(int i = 1; i < actions.length(); i++) + findMenu->removeAction(actions.at(i)); + + if(text.isEmpty()) + return; + + // findChildren + for(QAction *a : this->findChildren()) { + if(a->text().contains(text)) + findMenu->addAction(a); + } + }); + + smolbote->addSeparator(); + + const QString sessionPath = config->value("browser.session.path").value(); + smolbote->addAction(tr("Save Session"), parent, [parent, sessionPath]() { + const QString filename = QFileDialog::getSaveFileName(parent, tr("Save Session"), sessionPath, tr("JSON (*.json)")); + QFile output(filename); + if(output.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) { + auto *browser = qobject_cast(qApp); + auto data = Session::_session(browser->windows()); + output.write(QJsonDocument(data).toJson()); + output.close(); + } + }); + + smolbote->addAction(tr("Open Session"), parent, [parent]() { + auto *sessionDialog = new SessionDialog(parent); + sessionDialog->exec(); + }); + + smolbote->addSeparator(); + auto *actionBookmarks = smolbote->addAction(tr("Bookmarks"), browser, [browser, parent]() { + browser->showWidget(browser->bookmarks(), parent); + }); + config->setShortcut(actionBookmarks, "bookmarks.shortcut"); + + auto *actionDownloads = smolbote->addAction(tr("Downloads"), browser, [browser, parent]() { + browser->showWidget(browser->downloads(), parent); + }); + config->setShortcut(actionDownloads, "downloads.shortcut"); + + smolbote->addSeparator(); + smolbote->addAction(tr("Load Plugin"), browser, [browser]() { + const QString path = QFileDialog::getOpenFileName(nullptr, tr("Select Plugin"), QDir::homePath(), tr("Plugins (*.so)")); + browser->addPlugin(path); + }); + + pluginInsertLocation = smolbote->addSeparator(); + + auto *actionAbout = smolbote->addAction(tr("About"), browser, &Browser::about); + config->setShortcut(actionAbout, "mainwindow.shortcuts.about"); + + smolbote->addAction(tr("Help")); + + //smolbote->addAction(tr("Check for updates")); + + smolbote->addSeparator(); + + auto *actionQuit = smolbote->addAction(tr("Quit"), qApp, &QApplication::quit); + config->setShortcut(actionQuit, "mainwindow.shortcuts.quit"); + } + + auto *window = this->addMenu(tr("Window")); + { + auto *actionNewWindow = window->addAction(tr("New Window"), browser, &Browser::createWindow); + config->setShortcut(actionNewWindow, "mainwindow.shortcuts.newWindow"); + + auto *actionNewSubwindow = window->addAction(tr("New Subwindow"), parent, [parent]() { + parent->createSubWindow(nullptr, true); + }); + config->setShortcut(actionNewSubwindow, "mainwindow.shortcuts.newGroup"); + + window->addSeparator(); + + auto *actionTileSubwindows = window->addAction(tr("Tile Subwindows"), parent->mdiArea, &QMdiArea::tileSubWindows); + config->setShortcut(actionTileSubwindows, "mainwindow.shortcuts.tileWindows"); + + auto *actionCascadeSubwindows = window->addAction(tr("Cascade Subwindows"), parent->mdiArea, &QMdiArea::cascadeSubWindows); + config->setShortcut(actionCascadeSubwindows, "mainwindow.shortcuts.cascadeWindows"); + } + + auto *subwindow = this->addMenu(tr("Subwindow")); + { + auto *actionNewTab = subwindow->addAction(tr("New Tab"), parent, [parent]() { + auto *_subwindow = parent->currentSubWindow(); + if(_subwindow != nullptr) { + const int index = _subwindow->addTab(); + _subwindow->setCurrentTab(index); + } + }); + config->setShortcut(actionNewTab, "subwindow.shortcuts.new"); + + subwindow->addSeparator(); + + subwindow->addAction(tr("Restore last tab"), parent, [parent]() { + auto *_subwindow = parent->currentSubWindow(); + if(_subwindow != nullptr) { + const int index = _subwindow->restoreLastTab(); + _subwindow->setCurrentTab(index); + } + }); + + auto *restoreTabsMenu = subwindow->addMenu(tr("Restore previous tab")); + connect(restoreTabsMenu, &QMenu::aboutToShow, parent, [parent, restoreTabsMenu]() { + restoreTabsMenu->clear(); + auto *_subwindow = parent->currentSubWindow(); + if(_subwindow != nullptr) { + _subwindow->restoreTabMenu(restoreTabsMenu); + } + }); + + subwindow->addSeparator(); + + auto *subwindowProfile = subwindow->addMenu(tr("Subwindow Profile")); + connect(subwindowProfile, &QMenu::aboutToShow, subwindowProfile, [=]() { + subwindowProfile->clear(); + + auto *_subwindow = parent->currentSubWindow(); + if(_subwindow != nullptr) { + browser->getProfileManager()->profilePickerMenu(subwindowProfile, _subwindow->profile(), [_subwindow](WebProfile *profile) { + _subwindow->setProfile(profile); + }); + } + }); + } + + auto *page = this->addMenu(tr("Page")); + { + page->addAction(tr("Create Bookmark"), parent, [browser, parent]() { + if(parent->currentView != nullptr) + browser->bookmarks()->addBookmark(parent->currentView->title(), parent->currentView->url().toString()); + }); + + page->addSeparator(); + + page->addAction(tr("Save"), parent, [parent]() { + if(parent->currentView != nullptr) + parent->currentView->triggerPageAction(QWebEnginePage::SavePage); + }); + page->addAction(tr("Print"), parent, [parent]() { + if(parent->currentView != nullptr) { + auto *printer = new QPrinter(QPrinterInfo::defaultPrinter()); + QPrintDialog dlg(printer, parent); + if(dlg.exec() == QDialog::Accepted) { + parent->currentView->page()->print(printer, [printer](bool success) { + Q_UNUSED(success); + delete printer; + }); + } + } + }); + + page->addAction(tr("Print to PDF"), parent, [parent]() { + if(parent->currentView != nullptr) { + const QString path = QFileDialog::getSaveFileName(parent, tr("Print to PDF"), QDir::homePath(), tr("PDF files (*.pdf)")); + parent->currentView->page()->printToPdf(path); + } + }); + + page->addSeparator(); + + auto pageProfile = page->addMenu(tr("Page Profile")); + connect(pageProfile, &QMenu::aboutToShow, pageProfile, [=]() { + pageProfile->clear(); + + if(parent->currentView != nullptr) { + browser->getProfileManager()->profilePickerMenu(pageProfile, parent->currentView->profile(), [parent](WebProfile *profile) { + parent->currentView->setProfile(profile); + }); + } + }); + + page->addSeparator(); + page->addAction(tr("Developer Tools")); + page->addAction(tr("View Source")); + } +} + +QAction *MenuBar::insertPlugin(QMenu *menu) +{ + return smolbote->insertMenu(pluginInsertLocation, menu); +} diff --git a/src/mainwindow/menubar.h b/src/mainwindow/menubar.h new file mode 100644 index 0000000..38b1286 --- /dev/null +++ b/src/mainwindow/menubar.h @@ -0,0 +1,28 @@ +/* + * 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/gitea/aqua/smolbote + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#ifndef SMOLBOTE_MENUBAR_H +#define SMOLBOTE_MENUBAR_H + +#include + +class Configuration; +class MainWindow; +class MenuBar : public QMenuBar +{ +public: + MenuBar(const Configuration *config, MainWindow *parent = nullptr); + + QAction *insertPlugin(QMenu *menu); + +private: + QMenu *smolbote = nullptr; + QAction *pluginInsertLocation = nullptr; +}; + +#endif // SMOLBOTE_MENUBAR_H diff --git a/src/mainwindow/widgets/menusearch.cpp b/src/mainwindow/widgets/menusearch.cpp new file mode 100644 index 0000000..2905d6d --- /dev/null +++ b/src/mainwindow/widgets/menusearch.cpp @@ -0,0 +1,17 @@ +/* + * 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/gitea/aqua/smolbote + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#include "menusearch.h" + +MenuSearch::MenuSearch(QWidget *parent) + : QLineEdit(parent) +{ + setMinimumWidth(300); + setPlaceholderText(tr("Find...")); + setClearButtonEnabled(true); +} diff --git a/src/mainwindow/widgets/menusearch.h b/src/mainwindow/widgets/menusearch.h new file mode 100644 index 0000000..e20604c --- /dev/null +++ b/src/mainwindow/widgets/menusearch.h @@ -0,0 +1,20 @@ +/* + * 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/gitea/aqua/smolbote + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#ifndef SMOLBOTE_MENUSEARCH_H +#define SMOLBOTE_MENUSEARCH_H + +#include + +class MenuSearch : public QLineEdit +{ +public: + MenuSearch(QWidget *parent = nullptr); +}; + +#endif // SMOLBOTE_MENUSEARCH_H diff --git a/src/mainwindow/widgets/navigationbar.cpp b/src/mainwindow/widgets/navigationbar.cpp index 61d17fa..c56d342 100644 --- a/src/mainwindow/widgets/navigationbar.cpp +++ b/src/mainwindow/widgets/navigationbar.cpp @@ -7,8 +7,9 @@ */ #include "navigationbar.h" -#include "urllineedit.h" #include "configuration.h" +#include "urllineedit.h" +#include "util.h" #include "webengine/webview.h" #include #include @@ -17,7 +18,6 @@ #include #include #include -#include "util.h" NavigationBar::NavigationBar(const Configuration *config, QWidget *parent) : QToolBar(parent) -- cgit v1.2.1