diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/browser.cpp | 1 | ||||
-rw-r--r-- | src/mainwindow/mainwindow.cpp | 61 | ||||
-rw-r--r-- | src/mainwindow/mainwindow.h | 5 | ||||
-rw-r--r-- | src/mainwindow/mainwindow.ui | 37 | ||||
-rw-r--r-- | src/webengine/webview.cpp | 41 | ||||
-rw-r--r-- | src/webengine/webview.h | 9 | ||||
-rw-r--r-- | src/webengine/widgets/pagemenu.cpp | 122 | ||||
-rw-r--r-- | src/webengine/widgets/pagemenu.h | 29 |
9 files changed, 135 insertions, 172 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2f80ad1..16a21f1 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -42,8 +42,6 @@ set(poi_SRC webengine/webview.h webengine/widgets/pagetoolsmenu.cpp webengine/widgets/pagetoolsmenu.h - webengine/widgets/pagemenu.cpp - webengine/widgets/pagemenu.h # plugin interfaces plugin.h diff --git a/src/browser.cpp b/src/browser.cpp index 29eaea5..46018fb 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -186,6 +186,7 @@ MainWindow *Browser::createWindow() // the window will delete itself when it closes, so we don't need to delete it MainWindow *window = new MainWindow(m_config); connect(window->addressBar, &AddressBar::complete, m_bookmarks.get(), &BookmarksWidget::search); + connect(window, &MainWindow::createBookmark, m_bookmarks.get(), &BookmarksWidget::addBookmark); auto *bookmarksAction = new QAction(tr("Bookmarks"), window); m_config->setShortcut(bookmarksAction, "bookmarks.shortcut"); diff --git a/src/mainwindow/mainwindow.cpp b/src/mainwindow/mainwindow.cpp index 741b544..ff8c911 100644 --- a/src/mainwindow/mainwindow.cpp +++ b/src/mainwindow/mainwindow.cpp @@ -32,6 +32,10 @@ #include <webprofile.h> #include "session.h" #include <QFileDialog> +#include <QPrinter> +#include <QPrinterInfo> +#include <QPrintDialog> +#include "profilemanager.h" #ifdef PLASMA_BLUR #include <KWindowEffects> @@ -116,6 +120,38 @@ MainWindow::MainWindow(const std::unique_ptr<Configuration> &config, QWidget *pa subWindowAction = ui->actionCurrentWindow; } + // 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) { + 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); + } + }); + + pageLoadProfileMenu = ui->menuPage->addMenu(tr("Load Profile")); + connect(pageLoadProfileMenu, &QMenu::aboutToShow, this, &MainWindow::updatePageLoadProfileMenu); + } + navigationToolBar = new NavigationBar(config->section("navigation"), this); navigationToolBar->setMovable(config->value<bool>("navigation.movable").value()); addToolBar(Qt::TopToolBarArea, navigationToolBar); @@ -290,7 +326,6 @@ void MainWindow::setView(WebView *view) currentView = view; if(view) { - addressBar->setPageMenu(view->pageMenu()); addressBar->setToolsMenu(view->toolsMenu()); connect(view, &WebView::urlChanged, addressBar, &AddressBar::setUrl); @@ -300,7 +335,6 @@ void MainWindow::setView(WebView *view) addressBar->setProgress(100); } else { - addressBar->setPageMenu(nullptr); addressBar->setToolsMenu(nullptr); addressBar->setUrl(QUrl()); @@ -311,6 +345,29 @@ void MainWindow::setView(WebView *view) searchBox->setView(view); } +void MainWindow::updatePageLoadProfileMenu() +{ + Q_CHECK_PTR(pageLoadProfileMenu); + pageLoadProfileMenu->clear(); + + if(currentView == nullptr) + return; + + auto *browser = qobject_cast<Browser *>(qApp); + Q_CHECK_PTR(browser); + + ProfileIterator it(ProfileManager::profileList()); + while(it.hasNext()) { + it.next(); + auto *profile = it.value(); + auto *loadAction = pageLoadProfileMenu->addAction(profile->name()); + + connect(loadAction, &QAction::triggered, this, [=]() { + currentView->setProfile(profile); + }); + } +} + void MainWindow::closeEvent(QCloseEvent *event) { if(mdiArea->subWindowList().count() > 1) { diff --git a/src/mainwindow/mainwindow.h b/src/mainwindow/mainwindow.h index d695dd8..3768348 100644 --- a/src/mainwindow/mainwindow.h +++ b/src/mainwindow/mainwindow.h @@ -49,12 +49,16 @@ public: const QVector<SubWindow *> subWindows() const; SubWindow *currentSubWindow() const; +signals: + void createBookmark(const QString &title, const QString &url); + public slots: void createTab(const QUrl &url); SubWindow *createSubWindow(const std::unique_ptr<Configuration> &config, WebProfile *profile); private slots: void setView(WebView *view); + void updatePageLoadProfileMenu(); protected: void closeEvent(QCloseEvent *event) override; @@ -63,6 +67,7 @@ private: Ui::MainWindow *ui; QAction *subWindowAction = nullptr; QMenu *toolsMenu = nullptr; + QMenu *pageLoadProfileMenu = nullptr; NavigationBar *navigationToolBar = nullptr; AddressBar *addressBar = nullptr; diff --git a/src/mainwindow/mainwindow.ui b/src/mainwindow/mainwindow.ui index 72e0b39..698e1c4 100644 --- a/src/mainwindow/mainwindow.ui +++ b/src/mainwindow/mainwindow.ui @@ -56,9 +56,20 @@ <string>Too&ls</string> </property> </widget> + <widget class="QMenu" name="menuPage"> + <property name="title"> + <string>Pa&ge</string> + </property> + <addaction name="actionBookmarkPage"/> + <addaction name="actionSavePage"/> + <addaction name="actionPrintPage"/> + <addaction name="actionPrintPageToPdf"/> + <addaction name="separator"/> + </widget> <addaction name="menusmolbote"/> <addaction name="menuSession"/> <addaction name="menuWindow"/> + <addaction name="menuPage"/> <addaction name="menuTools"/> </widget> <widget class="QStatusBar" name="statusbar"/> @@ -99,17 +110,37 @@ </action> <action name="actionTileWindows"> <property name="text"> - <string>Tile Windows</string> + <string>&Tile Windows</string> </property> </action> <action name="actionCascadeWindows"> <property name="text"> - <string>Cascade Windows</string> + <string>&Cascade Windows</string> </property> </action> <action name="actionCurrentWindow"> <property name="text"> - <string>Current Window</string> + <string>Current &Window</string> + </property> + </action> + <action name="actionBookmarkPage"> + <property name="text"> + <string>&Create Bookmark</string> + </property> + </action> + <action name="actionSavePage"> + <property name="text"> + <string>&Save Page</string> + </property> + </action> + <action name="actionPrintPage"> + <property name="text"> + <string>&Print Page</string> + </property> + </action> + <action name="actionPrintPageToPdf"> + <property name="text"> + <string>P&rint to PDF</string> </property> </action> </widget> diff --git a/src/webengine/webview.cpp b/src/webengine/webview.cpp index f1b52b7..9380b13 100644 --- a/src/webengine/webview.cpp +++ b/src/webengine/webview.cpp @@ -10,11 +10,13 @@ #include "mainwindow/subwindow.h" #include "webpage.h" #include "webprofile.h" -#include "widgets/pagemenu.h" #include "widgets/pagetoolsmenu.h" #include <QDialog> #include <QStatusBar> #include <QVBoxLayout> +#include <QContextMenuEvent> +#include <QWidgetAction> +#include <QSlider> WebView::WebView(WebProfile *profile, QWidget *parent) : QWebEngineView(parent) @@ -41,15 +43,9 @@ WebView::WebView(WebProfile *profile, QWidget *parent) }); #endif - m_pageMenu = new PageMenu(this); m_toolsMenu = new PageToolsMenu(this); } -WebView::~WebView() -{ - delete m_pageMenu; -} - void WebView::setProfile(WebProfile *profile) { m_profile = profile; @@ -101,6 +97,37 @@ WebView *WebView::createWindow(QWebEnginePage::WebWindowType type) return view; } +void WebView::contextMenuEvent(QContextMenuEvent *event) +{ + QMenu *menu = page()->createStandardContextMenu(); + + // zoom widget + menu->addSeparator(); + { + auto *zoomSlider = new QSlider(Qt::Horizontal); + zoomSlider->setMinimum(5); + zoomSlider->setMaximum(50); + zoomSlider->setValue(10); + + auto *zoomAction = menu->addAction(tr("Zoom: %1x").arg(zoomFactor())); + connect(zoomAction, &QAction::triggered, this, [zoomSlider]() { + zoomSlider->setValue(10); + }); + + connect(zoomSlider, &QSlider::valueChanged, this, [this, zoomAction](int value) { + zoomAction->setText(tr("Zoom: %1x").arg(static_cast<qreal>(value) / 10)); + setZoomFactor(static_cast<qreal>(value) / 10); + }); + + auto *zoomWidgetAction = new QWidgetAction(this); + zoomWidgetAction->setDefaultWidget(zoomSlider); + + menu->addAction(zoomWidgetAction); + } + + menu->popup(event->globalPos()); +} + void WebView::triggerViewAction(WebView::ViewAction action) { switch(action) { diff --git a/src/webengine/webview.h b/src/webengine/webview.h index 0717e27..2d07244 100644 --- a/src/webengine/webview.h +++ b/src/webengine/webview.h @@ -25,13 +25,8 @@ public: }; explicit WebView(WebProfile *profile = nullptr, QWidget *parent = nullptr); - ~WebView() override; + ~WebView() = default; - QMenu *pageMenu() - { - Q_CHECK_PTR(m_pageMenu); - return m_pageMenu; - } QMenu *toolsMenu() { Q_CHECK_PTR(m_toolsMenu); @@ -56,12 +51,12 @@ signals: protected: WebView *createWindow(QWebEnginePage::WebWindowType type) override; + void contextMenuEvent(QContextMenuEvent *event) override; private: SubWindow *m_parentWindow = nullptr; WebProfile *m_profile = nullptr; - QMenu *m_pageMenu = nullptr; QMenu *m_toolsMenu = nullptr; bool m_loaded; diff --git a/src/webengine/widgets/pagemenu.cpp b/src/webengine/widgets/pagemenu.cpp deleted file mode 100644 index deff511..0000000 --- a/src/webengine/widgets/pagemenu.cpp +++ /dev/null @@ -1,122 +0,0 @@ -/* - * 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/smolbote.hg - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "pagemenu.h" -#include "../webview.h" -#include "browser.h" -#include <QFileDialog> -#include <QLabel> -#include <QPrintDialog> -#include <QPrinterInfo> -#include <QSlider> -#include <QStyle> -#include <QToolButton> -#include <QVBoxLayout> -#include <QWidgetAction> -#include <bookmarks/bookmarkswidget.h> -#include <webprofile.h> -#include "profilemanager.h" - -PageMenu::PageMenu(WebView *parent) - : QMenu(tr("Page"), parent) -{ - setMinimumWidth(240); - view = parent; - - auto *bookmarkAction = addAction(tr("Bookmark page")); - connect(bookmarkAction, &QAction::triggered, parent, [parent]() { - auto *browser = qobject_cast<Browser *>(qApp); - Q_CHECK_PTR(browser); - browser->bookmarks()->addBookmark(parent->title(), parent->url().toString()); - }); - - auto *savePageAction = addAction(tr("Save Page")); - connect(savePageAction, &QAction::triggered, parent, [parent]() { - parent->triggerPageAction(QWebEnginePage::SavePage); - }); - - auto *printAction = addAction(tr("Print Page")); - connect(printAction, &QAction::triggered, parent, [parent]() { - auto *printer = new QPrinter(QPrinterInfo::defaultPrinter()); - auto *dlg = new QPrintDialog(printer, nullptr); - if(dlg->exec() == QDialog::Accepted) { - parent->page()->print(printer, [printer](bool success) { - qDebug("print %s", success ? "ok" : "failed"); - delete printer; - }); - } - delete dlg; - }); - - auto *printPdfAction = addAction(tr("Print to PDF")); - connect(printPdfAction, &QAction::triggered, parent, [parent]() { - const QString path = QFileDialog::getSaveFileName(parent, tr("Print to PDF"), QDir::homePath(), tr("PDF files (*.pdf)")); - parent->page()->printToPdf(path); - }); - - addSeparator(); - - auto *zoomWidgetAction = new QWidgetAction(this); - { - auto *widget = new QWidget(this); - zoomWidgetAction->setDefaultWidget(widget); - - auto *layout = new QVBoxLayout(widget); - widget->setLayout(layout); - - QLabel *zoomLabel = new QLabel(tr("Zoom: 1x")); - layout->addWidget(zoomLabel); - - auto *zoomLayout = new QHBoxLayout(); - layout->addLayout(zoomLayout); - - auto *zoomSlider = new QSlider(Qt::Horizontal); - zoomSlider->setMinimum(5); - zoomSlider->setMaximum(50); - zoomSlider->setValue(10); - zoomLayout->addWidget(zoomSlider); - - auto *zoomResetButton = new QToolButton(widget); - zoomResetButton->setIcon(widget->style()->standardIcon(QStyle::SP_BrowserReload)); - zoomLayout->addWidget(zoomResetButton); - - connect(zoomResetButton, &QToolButton::clicked, [zoomSlider]() { - zoomSlider->setValue(10); - }); - connect(zoomSlider, &QSlider::valueChanged, parent, [parent, zoomLabel](int value) { - zoomLabel->setText(tr("Zoom: %1x").arg(static_cast<qreal>(value) / 10)); - parent->setZoomFactor(static_cast<qreal>(value) / 10); - }); - } - addAction(zoomWidgetAction); - - addSeparator(); - - loadProfile_menu = addMenu(tr("Load Profile")); - connect(this, &PageMenu::aboutToShow, this, &PageMenu::updateLoadProfileMenu); -} - -void PageMenu::updateLoadProfileMenu() -{ - Q_CHECK_PTR(loadProfile_menu); - loadProfile_menu->clear(); - - auto *browser = qobject_cast<Browser *>(qApp); - Q_CHECK_PTR(browser); - - ProfileIterator it(ProfileManager::profileList()); - while(it.hasNext()) { - it.next(); - auto *profile =it.value(); - auto *loadAction = loadProfile_menu->addAction(profile->name()); - - connect(loadAction, &QAction::triggered, this, [=]() { - view->setProfile(profile); - }); - } -} diff --git a/src/webengine/widgets/pagemenu.h b/src/webengine/widgets/pagemenu.h deleted file mode 100644 index 23e0e20..0000000 --- a/src/webengine/widgets/pagemenu.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * 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/smolbote.hg - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#ifndef SMOLBOTE_PAGEMENU_H -#define SMOLBOTE_PAGEMENU_H - -#include <QMenu> - -class WebView; -class PageMenu : public QMenu -{ - Q_OBJECT -public: - explicit PageMenu(WebView *parent = nullptr); - -private slots: - void updateLoadProfileMenu(); - -private: - QMenu *loadProfile_menu = nullptr; - WebView *view = nullptr; -}; - -#endif //SMOLBOTE_PAGEMENU_H |