summaryrefslogtreecommitdiff
path: root/src/frontend/btwebenginepage.cpp
blob: 584a6d5d3e1a017e83934301688a2cd82b7477fb (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 BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2016 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/

// This class encapsulates most differences between QWebPage and QWebEnginePage
// Javascript issues are handled in BtHtmlReadDisplay

#include "btwebenginepage.h"
#include "btwebengineview.h"

#include "frontend/display/bthtmljsobject.h"
#include "util/directory.h"

#include <QDebug>
#include <QPainter>
#include <QPrinter>
#ifdef USEWEBENGINE
#include <QWebChannel>
#else
#include <QWebPage>
#endif

#ifdef USEWEBENGINE

BtWebEnginePage::BtWebEnginePage(QObject *parent)
    :QWebEnginePage(parent),
      m_webChannel(new QWebChannel(this)) {

    setWebChannel(m_webChannel);
}

bool BtWebEnginePage::acceptNavigationRequest(
        const QUrl& url, NavigationType type, bool isMainFrame) {
    if (type == QWebEnginePage::NavigationTypeLinkClicked) {
        emit linkClicked(url);
        return false;
    }
    return QWebEnginePage::acceptNavigationRequest(url, type, isMainFrame);
}

void BtWebEnginePage::addJavaScriptObject(const QString &name, QObject *object) {
    object->setObjectName(name);
    m_webChannel->registerObject(name, object);
}

void BtWebEnginePage::selectAll() {
    triggerAction(QWebEnginePage::SelectAll);
}

void BtWebEnginePage::print(QPrinter *printer) {
    QPainter painter;
    painter.begin(printer);
    BtWebEngineView *btWebEngineView = btView();
    btWebEngineView->render(&painter);
    painter.end();
}

void BtWebEnginePage::javaScriptConsoleMessage(
        JavaScriptConsoleMessageLevel /*level*/,
        const QString & message, int /*lineNumber*/,
        const QString & /*sourceID*/) {
    qWarning() << "javascript console :" << message;
}

#else

BtWebEnginePage::BtWebEnginePage(QObject *parent)
    :QWebPage(parent) {
    settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
    setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
}

void BtWebEnginePage::addJavaScriptObject(const QString &name, QObject *object) {
    object->setObjectName(name);
    mainFrame()->addToJavaScriptWindowObject(object->objectName(), object);
}

void BtWebEnginePage::selectAll() {
    triggerAction(QWebPage::SelectAll);
}

void BtWebEnginePage::print(QPrinter *printer) {
    mainFrame()->print(printer);
}

void BtWebEnginePage::setHtml(const QString& text) {
    // TODO - test
}

#endif

BtWebEngineView * BtWebEnginePage::btView() const {
    return qobject_cast<BtWebEngineView*>(view());
}