From 1d37a6867c351668b76d5071d9a5fcd03718e275 Mon Sep 17 00:00:00 2001 From: David Rosca Date: Fri, 9 Oct 2015 19:31:08 +0200 Subject: [PATCH] PageThumbnailer: Rewrite to load thumbnails with QML WebEngineView This way it is possible to get thumbnail even without showing the webview. --- src/lib/data/data.qrc | 3 ++- src/lib/data/data/thumbnailer.qml | 15 ++++++++++++++ src/lib/lib.pro | 2 +- src/lib/tools/pagethumbnailer.cpp | 34 ++++++++++++++++--------------- src/lib/tools/pagethumbnailer.h | 6 ++---- 5 files changed, 38 insertions(+), 22 deletions(-) create mode 100644 src/lib/data/data/thumbnailer.qml diff --git a/src/lib/data/data.qrc b/src/lib/data/data.qrc index 4922d2a36..048e05325 100644 --- a/src/lib/data/data.qrc +++ b/src/lib/data/data.qrc @@ -1,7 +1,8 @@ + data/bookmarks.json data/browsedata.db data/profiles.ini - data/bookmarks.json + data/thumbnailer.qml diff --git a/src/lib/data/data/thumbnailer.qml b/src/lib/data/data/thumbnailer.qml new file mode 100644 index 000000000..14d4db478 --- /dev/null +++ b/src/lib/data/data/thumbnailer.qml @@ -0,0 +1,15 @@ +import QtQuick 2.2 +import QtWebEngine 1.0 + +WebEngineView { + width: 1280 + height: 720 + + onLoadingChanged: { + if (loadRequest.status == WebEngineView.LoadStartedStatus) + return; + + var ok = loadRequest.status == WebEngineView.LoadSucceededStatus; + thumbnailer.createThumbnail(ok); + } +} diff --git a/src/lib/lib.pro b/src/lib/lib.pro index d5f1978d9..efd7e524a 100644 --- a/src/lib/lib.pro +++ b/src/lib/lib.pro @@ -1,4 +1,4 @@ -QT += webenginewidgets webenginewidgets-private webchannel network widgets sql script +QT += webenginewidgets webenginewidgets-private webchannel network widgets sql script quickwidgets TARGET = QupZilla TEMPLATE = lib diff --git a/src/lib/tools/pagethumbnailer.cpp b/src/lib/tools/pagethumbnailer.cpp index a8cd95e07..27c9b3018 100644 --- a/src/lib/tools/pagethumbnailer.cpp +++ b/src/lib/tools/pagethumbnailer.cpp @@ -19,24 +19,21 @@ #include #include -#include + +#include +#include +#include PageThumbnailer::PageThumbnailer(QObject* parent) : QObject(parent) - , m_view(new QWebEngineView()) + , m_view(new QQuickWidget()) , m_size(QSize(450, 253)) , m_loadTitle(false) { - // Every page should fit in this resolution - m_view->resize(QSize(1280, 720)); - - // Well ... - QWidget *w = QApplication::activeWindow(); + m_view->setAttribute(Qt::WA_DontShowOnScreen); + m_view->setSource(QUrl(QSL("qrc:data/thumbnailer.qml"))); + m_view->rootContext()->setContextProperty(QSL("thumbnailer"), this); m_view->show(); - if (w) { - QApplication::setActiveWindow(w); - w->activateWindow(); - } } void PageThumbnailer::setSize(const QSize &size) @@ -79,9 +76,14 @@ QString PageThumbnailer::title() void PageThumbnailer::start() { - m_view->load(m_url); - - connect(m_view, &QWebEngineView::loadFinished, this, &PageThumbnailer::createThumbnail); + if (m_view->rootObject()) { + m_view->rootObject()->setProperty("url", m_url); + } + else { + QTimer::singleShot(0, this, [this]() { + emit thumbnailCreated(QPixmap()); + }); + } } void PageThumbnailer::createThumbnail(bool status) @@ -92,8 +94,8 @@ void PageThumbnailer::createThumbnail(bool status) } QTimer::singleShot(1000, this, [this]() { - m_title = m_view->title().trimmed(); - emit thumbnailCreated(m_view->grab().scaled(m_size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation)); + m_title = m_view->rootObject()->property("title").toString().trimmed(); + emit thumbnailCreated(QPixmap::fromImage(m_view->grabFramebuffer().scaled(m_size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation))); }); } diff --git a/src/lib/tools/pagethumbnailer.h b/src/lib/tools/pagethumbnailer.h index 7a1797a90..2b75f1cea 100644 --- a/src/lib/tools/pagethumbnailer.h +++ b/src/lib/tools/pagethumbnailer.h @@ -24,7 +24,7 @@ #include "qzcommon.h" -class QWebEngineView; +class QQuickWidget; class QPixmap; class QUPZILLA_EXPORT PageThumbnailer : public QObject @@ -49,12 +49,10 @@ signals: void thumbnailCreated(const QPixmap &); public slots: - -private slots: void createThumbnail(bool status); private: - QWebEngineView* m_view; + QQuickWidget *m_view; QSize m_size; QUrl m_url;