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