aboutsummaryrefslogtreecommitdiff
path: root/src/webengine/webview.cpp
blob: 7734aa00cf31d9d3d5f6bc3b7620864187d3ca9b (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
/*
 * 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/mainwindow.h"
#include "mainwindow/widgets/tabbar.h"
#include "widgets/pagemenu.h"
#include "widgets/pagetoolsmenu.h"
#include <QDialog>
#include <QStatusBar>
#include <QVBoxLayout>

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;
        if(m_loadProgress == 100) {
            m_loaded = true;
            emit loaded();
        }
    });

    m_pageMenu = new PageMenu(this);
    m_toolsMenu = new PageToolsMenu(this);
}

WebView::~WebView()
{
    delete m_pageMenu;
}

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

void WebView::setPage(QWebEnginePage *page)
{
    Q_CHECK_PTR(page);
    // make sure the page gets cleaned up if we replace it by taking ownership
    page->setParent(this);
    connect(page, &QWebEnginePage::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, 3000);
    }
}

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

WebView *createWebView(const QUrl &url, WebEngineProfile *profile, MainWindow *parent)
{
    auto *view = new WebView(parent);
    auto *page = new WebPage(profile);
    view->setPage(page);
    page->load(url);

    return view;
}