Falkon Develop
Cross-platform Qt-based web browser
pagethumbnailer.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "pagethumbnailer.h"
19#include "scripts.h"
20#include "webview.h"
21
22#include <QTimer>
23#include <QApplication>
24
25#include <QQmlContext>
26#include <QQuickItem>
27#include <QQuickWidget>
28
30 : QObject(parent)
31 , m_view(new QQuickWidget())
32 , m_size(QSize(450, 253) * qApp->devicePixelRatio())
33 , m_loadTitle(false)
34{
35 m_view->setAttribute(Qt::WA_DontShowOnScreen);
36 m_view->setSource(QUrl(QSL("qrc:data/thumbnailer.qml")));
37 m_view->rootContext()->setContextProperty(QSL("thumbnailer"), this);
38 m_view->show();
39}
40
41void PageThumbnailer::setSize(const QSize &size)
42{
43 if (size.isValid()) {
44 m_size = size;
45 }
46}
47
48void PageThumbnailer::setUrl(const QUrl &url)
49{
50 if (url.isValid()) {
51 m_url = url;
52 }
53}
54
56{
57 return m_url;
58}
59
61{
62 return m_loadTitle;
63}
64
66{
67 m_loadTitle = load;
68}
69
71{
72 QString title = m_title.isEmpty() ? m_url.host() : m_title;
73 if (title.isEmpty()) {
74 title = m_url.toString();
75 }
76 return title;
77}
78
80{
81 if (m_view->rootObject() && WebView::isUrlValid(m_url)) {
82 m_view->rootObject()->setProperty("url", m_url);
83 } else {
84 QTimer::singleShot(500, this, [this]() {
85 Q_EMIT thumbnailCreated(QPixmap());
86 });
87 }
88}
89
91{
92 return Scripts::setCss(QSL("::-webkit-scrollbar{display:none;}"));
93}
94
96{
97 if (!status) {
98 Q_EMIT thumbnailCreated(QPixmap());
99 return;
100 }
101
102 QTimer::singleShot(1000, this, [this]() {
103 m_title = m_view->rootObject()->property("title").toString().trimmed();
104 Q_EMIT thumbnailCreated(QPixmap::fromImage(m_view->grabFramebuffer().scaled(m_size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation)));
105 });
106}
107
109{
110 m_view->deleteLater();
111}
void setSize(const QSize &size)
void createThumbnail(bool status)
void thumbnailCreated(const QPixmap &)
void setUrl(const QUrl &url)
void setLoadTitle(bool load)
PageThumbnailer(QObject *parent=nullptr)
QString afterLoadScript() const
static QString setCss(const QString &css)
Definition: scripts.cpp:200
static bool isUrlValid(const QUrl &url)
Definition: webview.cpp:257
#define QSL(x)
Definition: qzcommon.h:40