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