WebBrowser/SpeedDial/PageThumbnailer.py

Sun, 03 Apr 2016 17:07:25 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 03 Apr 2016 17:07:25 +0200
changeset 4917
682750cc7bd5
parent 4913
e16573640cb8
child 4959
8cae2a004559
permissions
-rw-r--r--

Corrected some code style issues.

# -*- coding: utf-8 -*-

# Copyright (c) 2012 - 2016 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing an object to create a thumbnail image of a web site.
"""

from __future__ import unicode_literals

from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject, QSize, Qt, QUrl, \
    QTimer
from PyQt5.QtGui import QPixmap
from PyQt5.QtQuickWidgets import QQuickWidget


class PageThumbnailer(QObject):
    """
    Class implementing a thumbnail creator for web sites.
    
    @signal thumbnailCreated(QPixmap) emitted after the thumbnail has been
        created
    """
    thumbnailCreated = pyqtSignal(QPixmap)
    
    def __init__(self, parent=None):
        """
        Constructor
        
        @param parent reference to the parent object (QObject)
        """
        super(PageThumbnailer, self).__init__(parent)
        
        self.__size = QSize(231, 130)
        self.__loadTitle = False
        self.__title = ""
        self.__url = QUrl()
        
        self.__view = QQuickWidget()
        self.__view.setAttribute(Qt.WA_DontShowOnScreen)
        self.__view.setSource(QUrl("qrc:qml/thumbnailer.qml"))
        self.__view.rootContext().setContextProperty("thumbnailer", self)
        self.__view.show()
    
    def setSize(self, size):
        """
        Public method to set the size of the image.
        
        @param size size of the image (QSize)
        """
        if size.isValid():
            self.__size = QSize(size)
    
    def setUrl(self, url):
        """
        Public method to set the URL of the site to be thumbnailed.
        
        @param url URL of the web site (QUrl)
        """
        if url.isValid():
            self.__url = QUrl(url)
    
    def url(self):
        """
        Public method to get the URL of the thumbnail.
        
        @return URL of the thumbnail (QUrl)
        """
        return QUrl(self.__url)
    
    def loadTitle(self):
        """
        Public method to check, if the title is loaded from the web site.
        
        @return flag indicating, that the title is loaded (boolean)
        """
        return self.__loadTitle
    
    def setLoadTitle(self, load):
        """
        Public method to set a flag indicating to load the title from
        the web site.
        
        @param load flag indicating to load the title (boolean)
        """
        self.__loadTitle = load
    
    def title(self):
        """
        Public method to get the title of the thumbnail.
        
        @return title of the thumbnail (string)
        """
        if self.__title:
            title = self.__title
        else:
            title = self.__url.host()
        if not title:
            title = self.__url.toString()
        return title
    
    def start(self):
        """
        Public method to start the thumbnailing action.
        """
        if self.__view.rootObject():
            self.__view.rootObject().setProperty("url", self.__url)
        else:
            QTimer.singleShot(0, lambda: self.thumbnailCreated.emit(QPixmap()))
    
    @pyqtSlot(bool)
    def createThumbnail(self, status):
        """
        Public slot creating the thumbnail of the web site.
        
        @param status flag indicating a successful load of the web site
            (boolean)
        """
        if not status:
            self.thumbnailCreated.emit(QPixmap())
            return
        
        QTimer.singleShot(1000, self.__grabThumbnail)
    
    def __grabThumbnail(self):
        """
        Private slot to grab the thumbnail image from the view.
        """
        self.__title = self.__view.rootObject().property("title")
        pixmap = QPixmap.fromImage(
            self.__view.grabFramebuffer().scaled(
                self.__size, Qt.KeepAspectRatioByExpanding,
                Qt.SmoothTransformation))
        self.thumbnailCreated.emit(pixmap)

eric ide

mercurial