From 930c972169249251ef238a454d0126f2b7fec2c5 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Fri, 15 Dec 2017 21:21:01 +0100 Subject: WebView pageMenu implementation --- smolbote.qbs | 14 ++++-- src/browser.cpp | 5 -- src/main.cpp | 5 ++ src/mainwindow.cpp | 7 +-- src/webengine/webview.cpp | 98 ++++++++++++++++++++++++++++++++++++--- src/webengine/webview.h | 3 +- src/widgets/mainwindowmenubar.cpp | 14 +----- src/widgets/mainwindowmenubar.h | 2 - 8 files changed, 111 insertions(+), 37 deletions(-) diff --git a/smolbote.qbs b/smolbote.qbs index 8229ba3..4544cbe 100644 --- a/smolbote.qbs +++ b/smolbote.qbs @@ -75,11 +75,7 @@ Project { Group { name: "main" files: [ - "src/browser.cpp", - "src/browser.h", "src/main.cpp", - "src/singleapplication.cpp", - "src/singleapplication.h", ] cpp.defines: { if(project.deprecatedWarnings) @@ -90,6 +86,16 @@ Project { } } + Group { + name: "Browser" + files: [ + "src/browser.cpp", + "src/browser.h", + "src/singleapplication.cpp", + "src/singleapplication.h", + ] + } + Group { name: "Main Window" files: [ diff --git a/src/browser.cpp b/src/browser.cpp index 37071af..ab244fd 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -31,11 +31,6 @@ Browser::Browser(int &argc, char *argv[]) : { setApplicationName("smolbote"); setWindowIcon(QIcon(":/icon.svg")); -#ifdef GIT_VERSION - setApplicationVersion(GIT_VERSION); -#else - setApplicationVersion("1.0.0"); -#endif } Browser::~Browser() diff --git a/src/main.cpp b/src/main.cpp index 1707657..87ee004 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -64,6 +64,11 @@ int main(int argc, char *argv[]) { // Create application object Browser instance(argc, argv); +#ifdef GIT_VERSION + instance.setApplicationVersion(GIT_VERSION); +#else + instance.setApplicationVersion("1.0.0"); +#endif QCommandLineParser parser; parser.setApplicationDescription("yet another Qt browser"); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 70dc7ea..732bc70 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -74,11 +74,6 @@ MainWindow::MainWindow(std::shared_ptr config, QWidget *parent) : menuBar = new MainWindowMenuBar(config, this); menuBar->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred); - connect(menuBar->printAction(), &QAction::triggered, this, [&]() { - const QString path = QFileDialog::getSaveFileName(this, tr("Print to PDF"), QDir::homePath(), tr("PDF files (*.pdf)")); - m_currentView->page()->printToPdf(path); - }); - // Add the toolbars // tabToolBar: main menu and tab list tabToolBar->setMovable(m_config->value("browser.ui.tabtoolbarMovable").value()); @@ -290,7 +285,7 @@ void MainWindow::handleTabChanged(WebView *view) connect(view, &WebView::urlChanged, m_addressBar, &UrlLineEdit::setUrl); m_addressBar->setUrl(view->url()); - m_addressBar->pageAction()->setMenu(view->menu()); + m_addressBar->pageAction()->setMenu(view->pageMenu()); connect(view, SIGNAL(titleChanged(QString)), this, SLOT(handleTitleUpdated(QString))); connect(view, SIGNAL(linkHovered(QString)), ui->statusBar, SLOT(showMessage(QString))); diff --git a/src/webengine/webview.cpp b/src/webengine/webview.cpp index 50041b8..d3e987a 100644 --- a/src/webengine/webview.cpp +++ b/src/webengine/webview.cpp @@ -21,18 +21,102 @@ #include "webview.h" #include +// copy page URL +#include +#include + +// zoom widget +#include +#include +#include +#include +#include +#include +#include + +// printer support +#include +#include +#include +#include + WebView::WebView(QWidget *parent) : QWebEngineView(parent) { m_pageMenu = new QMenu(); - m_pageMenu->addAction(tr("TODO: Copy page URL")); - m_pageMenu->addAction(tr("TODO: Bookmark page")); + m_pageMenu->setMinimumWidth(240); + + QAction *copyUrlAction = m_pageMenu->addAction(tr("Copy page URL")); + connect(copyUrlAction, &QAction::triggered, [this]() { + qApp->clipboard()->setText(this->url().toString()); + }); + + QAction *bookmarkAction = m_pageMenu->addAction(tr("TODO: Bookmark page")); + connect(bookmarkAction, &QAction::triggered, this, [this]() { + emit newBookmark(this->title(), this->url()); + }); + m_pageMenu->addSeparator(); - m_pageMenu->addAction(tr("TODO: Zoom level: 100%")); + + QWidgetAction *zoomWidgetAction = new QWidgetAction(m_pageMenu); + { + QWidget *widget = new QWidget(m_pageMenu); + zoomWidgetAction->setDefaultWidget(widget); + + QVBoxLayout *layout = new QVBoxLayout(widget); + widget->setLayout(layout); + + QLabel *zoomLabel = new QLabel(tr("Zoom: 1x")); + layout->addWidget(zoomLabel); + + QHBoxLayout *zoomLayout = new QHBoxLayout(); + layout->addLayout(zoomLayout); + + QSlider *zoomSlider = new QSlider(Qt::Horizontal); + zoomSlider->setMinimum(5); + zoomSlider->setMaximum(50); + zoomSlider->setValue(10); + zoomLayout->addWidget(zoomSlider); + + QToolButton *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, this, [this, zoomLabel](int value) { + zoomLabel->setText(tr("Zoom: %1x").arg(static_cast(value) / 10)); + this->setZoomFactor(static_cast(value) / 10); + }); + } + m_pageMenu->addAction(zoomWidgetAction); + m_pageMenu->addSeparator(); - m_pageMenu->addAction(tr("TODO: Save Page")); - m_pageMenu->addAction(tr("TODO: Print page")); - m_pageMenu->addAction(tr("TODO: Print to PDF")); + + QAction *savePageAction = m_pageMenu->addAction(tr("TODO: Save Page")); + connect(savePageAction, &QAction::triggered, this, [this]() { + this->triggerPageAction(QWebEnginePage::SavePage); + }); + + QAction *printAction = m_pageMenu->addAction(tr("Print page")); + connect(printAction, &QAction::triggered, [this]() { + QPrinter *printer = new QPrinter(QPrinterInfo::defaultPrinter()); + QPrintDialog *dlg = new QPrintDialog(printer, nullptr); + if(dlg->exec() == QDialog::Accepted) { + this->page()->print(printer, [printer](bool success) { + qDebug("print %s", success ? "ok" : "failed"); + delete printer; + }); + } + delete dlg; + }); + + QAction *printPdfAction = m_pageMenu->addAction(tr("Print to PDF")); + connect(printPdfAction, &QAction::triggered, [this]() { + const QString path = QFileDialog::getSaveFileName(this, tr("Print to PDF"), QDir::homePath(), tr("PDF files (*.pdf)")); + this->page()->printToPdf(path); + }); } WebView::~WebView() @@ -40,7 +124,7 @@ WebView::~WebView() delete m_pageMenu; } -QMenu *WebView::menu() +QMenu *WebView::pageMenu() { Q_CHECK_PTR(m_pageMenu); return m_pageMenu; diff --git a/src/webengine/webview.h b/src/webengine/webview.h index 95d12d9..deecd35 100644 --- a/src/webengine/webview.h +++ b/src/webengine/webview.h @@ -31,12 +31,13 @@ public: explicit WebView(QWidget *parent = nullptr); ~WebView(); - QMenu *menu(); + QMenu *pageMenu(); void setPage(QWebEnginePage *page); signals: void linkHovered(const QString &url); + void newBookmark(const QString &title, const QUrl &url); private slots: void handleLinkHovered(const QString &url); diff --git a/src/widgets/mainwindowmenubar.cpp b/src/widgets/mainwindowmenubar.cpp index aef61b4..cdd473a 100644 --- a/src/widgets/mainwindowmenubar.cpp +++ b/src/widgets/mainwindowmenubar.cpp @@ -75,30 +75,20 @@ MainWindowMenuBar::MainWindowMenuBar(std::shared_ptr config, Main QMenu *profileMenu = new QMenu(tr("Profile"), this); addMenu(profileMenu); //profileMenu->addAction(tr("Profiles"), this, SLOT(handleLoadProfile())); - - // Page menu - QMenu *pageMenu = new QMenu(tr("Page"), this); - addMenu(pageMenu); - pageMenu->addAction(tr("Print"))->setEnabled(false); - m_printAction = pageMenu->addAction(tr("Print to PDF")); - pageMenu->addAction(tr("Zoom"))->setEnabled(false); } QAction *MainWindowMenuBar::bookmarksAction() { + Q_CHECK_PTR(m_bookmarksAction); return m_bookmarksAction; } QAction *MainWindowMenuBar::downloadsAction() { + Q_CHECK_PTR(m_downloadsAction); return m_downloadsAction; } -QAction *MainWindowMenuBar::printAction() -{ - return m_printAction; -} - void MainWindowMenuBar::handleLoadProfile(MainWindow *window) { ProfilesDialog *dlg = new ProfilesDialog(window, this); diff --git a/src/widgets/mainwindowmenubar.h b/src/widgets/mainwindowmenubar.h index bbfc3fc..5a7094b 100644 --- a/src/widgets/mainwindowmenubar.h +++ b/src/widgets/mainwindowmenubar.h @@ -34,7 +34,6 @@ public: QAction *bookmarksAction(); QAction *downloadsAction(); - QAction *printAction(); private slots: void handleLoadProfile(MainWindow *window); @@ -42,7 +41,6 @@ private slots: private: QAction *m_bookmarksAction; QAction *m_downloadsAction; - QAction *m_printAction; }; #endif // MAINWINDOWMENUBAR_H -- cgit v1.2.1