webenginepage.cpp 3.14 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
   Copyright (C) 2016-2017 Laurent Montel <montel@kde.org>
Laurent Montel's avatar
Laurent Montel committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
Laurent Montel's avatar
Laurent Montel committed
18 19 20 21 22 23
*/

#include "webenginepage.h"
#include "webhittest.h"
#include "webhittestresult.h"

24 25
#include <KLocalizedString>

Laurent Montel's avatar
Laurent Montel committed
26
#include <QEventLoop>
27
#include <QWebEngineDownloadItem>
Laurent Montel's avatar
Laurent Montel committed
28 29
#include <QPointer>
#include <QTimer>
30 31
#include <QFileDialog>
#include <QWebEngineProfile>
Laurent Montel's avatar
Laurent Montel committed
32
#include <QPrinter>
Laurent Montel's avatar
Laurent Montel committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
using namespace WebEngineViewer;

class WebEngineViewer::WebEnginePagePrivate
{
public:
    WebEnginePagePrivate()
    {

    }
};

WebEnginePage::WebEnginePage(QObject *parent)
    : QWebEnginePage(parent),
      d(new WebEnginePagePrivate)
{
48
    init();
Laurent Montel's avatar
Laurent Montel committed
49 50
}

51 52 53 54
WebEnginePage::WebEnginePage(QWebEngineProfile *profile, QObject *parent)
    : QWebEnginePage(profile, parent),
      d(new WebEnginePagePrivate)
{
55
    init();
56 57
}

Laurent Montel's avatar
Laurent Montel committed
58 59 60 61 62
WebEnginePage::~WebEnginePage()
{
    delete d;
}

63 64 65 66 67
void WebEnginePage::init()
{
    connect(profile(), &QWebEngineProfile::downloadRequested, this, &WebEnginePage::saveHtml);
}

Laurent Montel's avatar
Laurent Montel committed
68 69 70 71 72
WebEngineViewer::WebHitTest *WebEnginePage::hitTestContent(const QPoint &pos)
{
    return new WebHitTest(this, pos);
}

73 74 75 76 77 78 79 80 81 82
void WebEnginePage::saveHtml(QWebEngineDownloadItem *download)
{
    const QString fileName = QFileDialog::getSaveFileName(view(), i18n("Save HTML Page"));
    if (!fileName.isEmpty()) {
        download->setSavePageFormat(QWebEngineDownloadItem::SingleHtmlSaveFormat);
        download->setPath(fileName);
        download->accept();
    }
}

83 84 85 86
bool WebEnginePage::acceptNavigationRequest(const QUrl &url, NavigationType type, bool isMainFrame)
{
    if (isMainFrame && type == NavigationTypeLinkClicked) {
        Q_EMIT urlClicked(url);
Laurent Montel's avatar
Laurent Montel committed
87
        return false;
88
    }
Laurent Montel's avatar
Laurent Montel committed
89
    return true;
90 91
}

Laurent Montel's avatar
Laurent Montel committed
92
void WebEnginePage::javaScriptConsoleMessage(QWebEnginePage::JavaScriptConsoleMessageLevel level, const QString &message, int lineNumber, const QString &sourceID)
93 94 95
{
    Q_UNUSED(level);
    Q_UNUSED(sourceID);
96
    qDebug() << lineNumber << ":" << message;
97 98
    Q_EMIT showConsoleMessage(message);
}
Laurent Montel's avatar
Laurent Montel committed
99 100 101

bool WebEnginePage::execPrintPreviewPage(QPrinter *printer, int timeout)
{
102
#ifndef WEBENGINEVIEWER_PRINT_SUPPORT
Laurent Montel's avatar
Laurent Montel committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    QPointer<QEventLoop> loop = new QEventLoop;
    bool result = false;
    QTimer::singleShot(timeout, loop.data(), &QEventLoop::quit);

    print(printer, [loop, &result](bool res) {
        if (loop && loop->isRunning()) {
            result = res;
            loop->quit();
        }
    });

    loop->exec();
    delete loop;

    return result;
118 119 120
#else
    return false;
#endif
Laurent Montel's avatar
Laurent Montel committed
121
}