File indexing completed on 2024-05-19 04:59:09
0001 /* ============================================================ 0002 * Falkon - Qt web browser 0003 * Copyright (C) 2010-2017 David Rosca <nowrep@gmail.com> 0004 * 0005 * This program is free software: you can redistribute it and/or modify 0006 * it under the terms of the GNU General Public License as published by 0007 * the Free Software Foundation, either version 3 of the License, or 0008 * (at your option) any later version. 0009 * 0010 * This program is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 * GNU General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU General Public License 0016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0017 * ============================================================ */ 0018 #include "pagethumbnailer.h" 0019 #include "scripts.h" 0020 #include "webview.h" 0021 0022 #include <QTimer> 0023 #include <QApplication> 0024 0025 #include <QQmlContext> 0026 #include <QQuickItem> 0027 #include <QQuickWidget> 0028 0029 PageThumbnailer::PageThumbnailer(QObject* parent) 0030 : QObject(parent) 0031 , m_view(new QQuickWidget()) 0032 , m_size(QSize(450, 253) * qApp->devicePixelRatio()) 0033 , m_loadTitle(false) 0034 { 0035 m_view->setAttribute(Qt::WA_DontShowOnScreen); 0036 m_view->setSource(QUrl(QSL("qrc:data/thumbnailer.qml"))); 0037 m_view->rootContext()->setContextProperty(QSL("thumbnailer"), this); 0038 m_view->show(); 0039 } 0040 0041 void PageThumbnailer::setSize(const QSize &size) 0042 { 0043 if (size.isValid()) { 0044 m_size = size; 0045 } 0046 } 0047 0048 void PageThumbnailer::setUrl(const QUrl &url) 0049 { 0050 if (url.isValid()) { 0051 m_url = url; 0052 } 0053 } 0054 0055 QUrl PageThumbnailer::url() 0056 { 0057 return m_url; 0058 } 0059 0060 bool PageThumbnailer::loadTitle() 0061 { 0062 return m_loadTitle; 0063 } 0064 0065 void PageThumbnailer::setLoadTitle(bool load) 0066 { 0067 m_loadTitle = load; 0068 } 0069 0070 QString PageThumbnailer::title() 0071 { 0072 QString title = m_title.isEmpty() ? m_url.host() : m_title; 0073 if (title.isEmpty()) { 0074 title = m_url.toString(); 0075 } 0076 return title; 0077 } 0078 0079 void PageThumbnailer::start() 0080 { 0081 if (m_view->rootObject() && WebView::isUrlValid(m_url)) { 0082 m_view->rootObject()->setProperty("url", m_url); 0083 } else { 0084 QTimer::singleShot(500, this, [this]() { 0085 Q_EMIT thumbnailCreated(QPixmap()); 0086 }); 0087 } 0088 } 0089 0090 QString PageThumbnailer::afterLoadScript() const 0091 { 0092 return Scripts::setCss(QSL("::-webkit-scrollbar{display:none;}")); 0093 } 0094 0095 void PageThumbnailer::createThumbnail(bool status) 0096 { 0097 if (!status) { 0098 Q_EMIT thumbnailCreated(QPixmap()); 0099 return; 0100 } 0101 0102 QTimer::singleShot(1000, this, [this]() { 0103 m_title = m_view->rootObject()->property("title").toString().trimmed(); 0104 Q_EMIT thumbnailCreated(QPixmap::fromImage(m_view->grabFramebuffer().scaled(m_size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation))); 0105 }); 0106 } 0107 0108 PageThumbnailer::~PageThumbnailer() 0109 { 0110 m_view->deleteLater(); 0111 }