aboutsummaryrefslogtreecommitdiff
path: root/src/webengine/webview.cpp
blob: b7258eac19a30bb2a819f7338dbe5f11b5d07bfd (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 * 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"
#include "mainwindow/subwindow.h"
#include "webpage.h"
#include "webprofile.h"
#include <QContextMenuEvent>
#include <QDialog>
#include <QSlider>
#include <QStatusBar>
#include <QToolButton>
#include <QVBoxLayout>
#include <QWebEngineContextMenuData>
#include <QWebEngineHistoryItem>
#include <QWidgetAction>
#include "profilemanager.h"
#include <QMenu>

inline QAction *historyAction(QWebEngineView *view, const QWebEngineHistoryItem &item)
{
    QAction *action = new QAction(view);
    if(item.title().isEmpty())
        action->setText(item.url().toString());
    else
        action->setText(QObject::tr("%1 (%2)").arg(item.title(), item.url().toString()));

    QObject::connect(action, &QAction::triggered, view, [view, item]() {
        view->history()->goToItem(item);
    });
    return action;
}

WebView::WebView(WebProfile *profile, QWidget *parent)
    : QWebEngineView(parent)
{
    Q_CHECK_PTR(profile);
    m_profile = profile;
    setPage(new WebPage(profile, this));

    m_parentWindow = qobject_cast<SubWindow *>(parent);

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

#if defined(QTBUG_65223_WORKAROUND)
    connect(this, &QWebEngineView::loadProgress, this, [this](int progress) {
        if(progress == 100) {
            emit loadFinished(true);
        }
    });
#endif
}

void WebView::setProfile(WebProfile *profile)
{
    m_profile = profile;
    const auto url = this->url();
    setPage(new WebPage(profile, this));
    this->load(url);
}

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

void WebView::search(const QString &term)
{
    const QString searchUrl = m_profile->search().arg(QString(QUrl::toPercentEncoding(term)));
    load(searchUrl);
}

WebView *WebView::createWindow(QWebEnginePage::WebWindowType type)
{
    if(m_parentWindow == nullptr) {
        qDebug("WebView::createWindow: parent window not found, returning nullptr");
        return nullptr;
    }

    // parent Window has been found
    auto index = m_parentWindow->addTab();
    WebView *view = m_parentWindow->view(index);
    switch(type) {
    case QWebEnginePage::WebBrowserWindow:
        // a complete web browser window
        break;

    case QWebEnginePage::WebBrowserTab:
        // a web browser tab
        m_parentWindow->setCurrentTab(index);
        break;

    case QWebEnginePage::WebDialog:
        // a window without decorations
        break;

    case QWebEnginePage::WebBrowserBackgroundTab:
        // a web browser tab, but don't swap to it
        break;
    }

    return view;
}

void WebView::contextMenuEvent(QContextMenuEvent *event)
{
    QMenu *menu = nullptr;
    const auto ctxdata = page()->contextMenuData();

    if(ctxdata.mediaType() == QWebEngineContextMenuData::MediaTypeNone) {
        menu = new QMenu(this);

        // navigation
        {
            auto *backMenu = new QMenu(tr("Back"), this);
            if(!history()->canGoBack()) {
                backMenu->setEnabled(false);
            } else {
                connect(backMenu, &QMenu::aboutToShow, this, [this, backMenu]() {
                    backMenu->clear();
                    const auto backItems = history()->backItems(10);
                    for(const QWebEngineHistoryItem &item : backItems) {
                        backMenu->addAction(historyAction(this, item));
                    }
                });
            }
            menu->addMenu(backMenu);

            auto *forwardMenu = new QMenu(tr("Forward"), this);
            if(!history()->canGoForward()) {
                forwardMenu->setEnabled(false);
            } else {
                connect(forwardMenu, &QMenu::aboutToShow, this, [this, forwardMenu]() {
                    forwardMenu->clear();
                    const auto forwardItems = history()->forwardItems(10);
                    for(const QWebEngineHistoryItem &item : forwardItems) {
                        forwardMenu->addAction(historyAction(this, item));
                    }
                });
            }
            menu->addMenu(forwardMenu);

            connect(menu->addAction(tr("Reload")), &QAction::triggered, this, [this]() {
                page()->triggerAction(QWebEnginePage::Reload);
            });

            connect(menu->addAction(tr("Reload and bypass Cache")), &QAction::triggered, this, [this]() {
                page()->triggerAction(QWebEnginePage::ReloadAndBypassCache);
            });
        }

        if(!ctxdata.linkUrl().isEmpty()) {
            menu->addSeparator();
            connect(menu->addAction(tr("Open link in new tab")), &QAction::triggered, this, [this, ctxdata]() {
                createWindow(QWebEnginePage::WebBrowserTab)->load(ctxdata.linkUrl());
            });

            auto *newTabMenu = new QMenu(tr("Open link in new tab with profile"), this);
            ProfileIterator it(ProfileManager::profileList());
            while(it.hasNext()) {
                it.next();
                connect(newTabMenu->addAction(it.key()), &QAction::triggered, this, [this, ctxdata, it]() {
                    auto *view = createWindow(QWebEnginePage::WebBrowserTab);
                    view->setProfile(it.value());
                    view->load(ctxdata.linkUrl());
                });
            }
            menu->addMenu(newTabMenu);

            connect(menu->addAction(tr("Open link in new window")), &QAction::triggered, this, [this, ctxdata]() {
                createWindow(QWebEnginePage::WebBrowserWindow)->load(ctxdata.linkUrl());
            });
        }
    } else {
        menu = page()->createStandardContextMenu();
    }

    // zoom widget
    {
        menu->addSeparator();

        auto *zoomSlider = new QSlider(Qt::Horizontal);
        zoomSlider->setMinimum(5);
        zoomSlider->setMaximum(50);
        zoomSlider->setValue(zoomFactor() * 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->setMinimumWidth(250);
    menu->popup(event->globalPos());
}

void WebView::triggerViewAction(WebView::ViewAction action)
{
    switch(action) {
    case GoHome:
        load(m_profile->homepage());
        break;
    case BookmarkPage:
        emit newBookmark(this->title(), this->url());
        break;
    }
}