eric6/WebBrowser/SpeedDial/PageThumbnailer.py

changeset 6942
2602857055c5
parent 6645
ad476851d7e0
child 7229
53054eb5b15a
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2012 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing an object to create a thumbnail image of a web site.
8 """
9
10 from __future__ import unicode_literals
11
12 from PyQt5.QtCore import pyqtSignal, QObject, QSize, Qt, QUrl, QTimer
13 from PyQt5.QtGui import QPixmap, QImage, QPainter
14 from PyQt5.QtWebEngineWidgets import QWebEngineView
15
16
17 class PageThumbnailer(QObject):
18 """
19 Class implementing a thumbnail creator for web sites.
20
21 @signal thumbnailCreated(QPixmap) emitted after the thumbnail has been
22 created
23 """
24 thumbnailCreated = pyqtSignal(QPixmap)
25
26 def __init__(self, parent=None):
27 """
28 Constructor
29
30 @param parent reference to the parent object (QObject)
31 """
32 super(PageThumbnailer, self).__init__(parent)
33
34 self.__size = QSize(231, 130)
35 self.__loadTitle = False
36 self.__title = ""
37 self.__url = QUrl()
38
39 self.__view = QWebEngineView()
40 self.__view.setAttribute(Qt.WA_DontShowOnScreen)
41 self.__view.resize(1920, 1080)
42 self.__view.show()
43
44 def setSize(self, size):
45 """
46 Public method to set the size of the image.
47
48 @param size size of the image (QSize)
49 """
50 if size.isValid():
51 self.__size = QSize(size)
52
53 def setUrl(self, url):
54 """
55 Public method to set the URL of the site to be thumbnailed.
56
57 @param url URL of the web site (QUrl)
58 """
59 if url.isValid():
60 self.__url = QUrl(url)
61
62 def url(self):
63 """
64 Public method to get the URL of the thumbnail.
65
66 @return URL of the thumbnail (QUrl)
67 """
68 return QUrl(self.__url)
69
70 def loadTitle(self):
71 """
72 Public method to check, if the title is loaded from the web site.
73
74 @return flag indicating, that the title is loaded (boolean)
75 """
76 return self.__loadTitle
77
78 def setLoadTitle(self, load):
79 """
80 Public method to set a flag indicating to load the title from
81 the web site.
82
83 @param load flag indicating to load the title (boolean)
84 """
85 self.__loadTitle = load
86
87 def title(self):
88 """
89 Public method to get the title of the thumbnail.
90
91 @return title of the thumbnail (string)
92 """
93 if self.__title:
94 title = self.__title
95 else:
96 title = self.__url.host()
97 if not title:
98 title = self.__url.toString()
99 return title
100
101 def start(self):
102 """
103 Public method to start the thumbnailing action.
104 """
105 self.__view.loadFinished.connect(self.__createThumbnail)
106 self.__view.load(self.__url)
107
108 def __createThumbnail(self, status):
109 """
110 Private slot creating the thumbnail of the web site.
111
112 @param status flag indicating a successful load of the web site
113 (boolean)
114 """
115 if not status:
116 self.thumbnailCreated.emit(QPixmap())
117 return
118
119 QTimer.singleShot(1000, self.__grabThumbnail)
120
121 def __grabThumbnail(self):
122 """
123 Private slot to grab the thumbnail image from the view.
124 """
125 self.__title = self.__view.title()
126
127 image = QImage(self.__view.size(), QImage.Format_ARGB32)
128 painter = QPainter(image)
129 self.__view.render(painter)
130 painter.end()
131
132 scaledImage = image.scaled(self.__size,
133 Qt.KeepAspectRatioByExpanding,
134 Qt.SmoothTransformation)
135
136 self.thumbnailCreated.emit(QPixmap.fromImage(scaledImage))

eric ide

mercurial