aboutsummaryrefslogtreecommitdiff
path: root/src/webengine/webview.cpp
blob: 263fb672483cf2608b864773a8788cd9d6a543ac (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * 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 "webview.h"

// copy page URL
#include <QApplication>
#include <QClipboard>

// zoom widget
#include <QLabel>
#include <QSlider>
#include <QStyle>
#include <QToolButton>
#include <QVBoxLayout>
#include <QWidgetAction>

// printer support
#include <QFileDialog>
#include <QPrintDialog>
#include <QPrinter>
#include <QPrinterInfo>

#include "src/mainwindow/mainwindow.h"
#include <QStatusBar>

// ssl errors
#include "src/addressbar/urllineedit.h"

WebView::WebView(MainWindow *parentMainWindow, QWidget *parent)
    : QWebEngineView(parent)
{
    Q_CHECK_PTR(parentMainWindow);
    m_parent = parentMainWindow;

    // load status and progress
    connect(this, &QWebEngineView::loadStarted, this, [this]() {
        m_loaded = false;
    });
    connect(this, &QWebEngineView::loadFinished, this, [this]() {
        m_loaded = true;
    });
    connect(this, &QWebEngineView::loadProgress, this, [this](int progress) {
        m_loadProgress = progress;
    });

    m_pageMenu = new QMenu();
    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();

    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<qreal>(value) / 10));
            this->setZoomFactor(static_cast<qreal>(value) / 10);
        });
    }
    m_pageMenu->addAction(zoomWidgetAction);

    m_pageMenu->addSeparator();

    QAction *savePageAction = m_pageMenu->addAction(tr("TODO: Save Page"));
    connect(savePageAction, &QAction::triggered, this, [this]() {
        this->triggerPageAction(QWebEnginePage::SavePage);
    });

#if QT_VERSION >= QT_VERSION_CHECK(5, 8, 0)
    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;
    });
#endif

    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()
{
    delete m_pageMenu;
}

QMenu *WebView::pageMenu()
{
    Q_CHECK_PTR(m_pageMenu);
    return m_pageMenu;
}

void WebView::setPage(WebPage *page)
{
    Q_CHECK_PTR(page);
    // make sure the page gets cleaned up if we replace it by taking ownership
    page->setParent(this);
    connect(page, &WebPage::linkHovered, this, &WebView::handleLinkHovered);
    QWebEngineView::setPage(page);
}

bool WebView::isLoaded() const
{
    return m_loaded;
}

int WebView::loadProgress() const
{
    return m_loadProgress;
}

WebView *WebView::createWindow(QWebEnginePage::WebWindowType type)
{
    WebView *view = new WebView(m_parent);
    switch(type) {
    case QWebEnginePage::WebBrowserWindow:
        // a complete web browser window
        m_parent->newWindow()->tabBar->addTab(view);
        break;

    case QWebEnginePage::WebBrowserTab:
        // a web browser tab
        m_parent->tabBar->setCurrentIndex(m_parent->tabBar->addTab(view));
        break;

    case QWebEnginePage::WebDialog:
        // a window without decorations
        m_parent->newWindow()->tabBar->addTab(view);
        break;

    case QWebEnginePage::WebBrowserBackgroundTab:
        // a web browser tab, but don't swap to it
        m_parent->tabBar->addTab(view);
        break;
    }

    return view;
}

void WebView::handleLinkHovered(const QString &url)
{
    if(isVisible()) {
        m_parent->statusBar()->showMessage(url);
    }
}