aboutsummaryrefslogtreecommitdiff
path: root/src/webengine/widgets/pagemenu.cpp
blob: deff51126b22da1e210992d77807f9a2a7fcc1da (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 * 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);
        });
    }
}