aboutsummaryrefslogtreecommitdiff
path: root/src/webengine/widgets/pagemenu.cpp
blob: a90d22390fbbb907b0ca0a7597c815cfcee4629a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * 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 <QApplication>
#include <QClipboard>
#include <QFileDialog>
#include <QLabel>
#include <QPrintDialog>
#include <QPrinterInfo>
#include <QSlider>
#include <QStyle>
#include <QToolButton>
#include <QVBoxLayout>
#include <QWidgetAction>

PageMenu::PageMenu(WebView *parent)
    : QMenu(parent)
{
    setMinimumWidth(240);

    auto *copyUrlAction = addAction(tr("Copy page URL"));
    connect(copyUrlAction, &QAction::triggered, parent, [parent]() {
        qApp->clipboard()->setText(parent->url().toString());
    });

    auto *bookmarkAction = addAction(tr("Bookmark page"));
    connect(bookmarkAction, &QAction::triggered, parent, [parent]() {
        parent->triggerViewAction(WebView::BookmarkPage);
    });

    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();

    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);
    });
}