7 Module implementing an object to create a thumbnail image of a web site. |
7 Module implementing an object to create a thumbnail image of a web site. |
8 """ |
8 """ |
9 |
9 |
10 from __future__ import unicode_literals |
10 from __future__ import unicode_literals |
11 |
11 |
12 from PyQt5.QtCore import pyqtSignal, QObject, QSize, Qt, QUrl |
12 from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject, QSize, Qt, QUrl, \ |
13 from PyQt5.QtGui import QPixmap, QImage, QPainter |
13 QTimer |
14 from PyQt5.QtWebKitWidgets import QWebPage |
14 from PyQt5.QtGui import QPixmap |
15 |
15 from PyQt5.QtQuickWidgets import QQuickWidget |
16 from ..Network.NetworkAccessManagerProxy import NetworkAccessManagerProxy |
|
17 |
16 |
18 |
17 |
19 class PageThumbnailer(QObject): |
18 class PageThumbnailer(QObject): |
20 """ |
19 """ |
21 Class implementing a thumbnail creator for web sites. |
20 Class implementing a thumbnail creator for web sites. |
31 |
30 |
32 @param parent reference to the parent object (QObject) |
31 @param parent reference to the parent object (QObject) |
33 """ |
32 """ |
34 super(PageThumbnailer, self).__init__(parent) |
33 super(PageThumbnailer, self).__init__(parent) |
35 |
34 |
36 self.__page = QWebPage(self) |
|
37 self.__size = QSize(231, 130) |
35 self.__size = QSize(231, 130) |
|
36 ## self.__size = QSize(450, 253) |
38 self.__loadTitle = False |
37 self.__loadTitle = False |
39 self.__title = "" |
38 self.__title = "" |
40 self.__url = QUrl() |
39 self.__url = QUrl() |
41 |
40 |
42 self.__proxy = NetworkAccessManagerProxy(self) |
41 self.__view = QQuickWidget() |
43 import Helpviewer.HelpWindow |
42 # TODO: uncomment once PyQt5 is fixed |
44 self.__proxy.setPrimaryNetworkAccessManager( |
43 ## self.__view.setAttribute(Qt.WA_DontShowOnScreen) |
45 Helpviewer.HelpWindow.HelpWindow.networkAccessManager()) |
44 self.__view.setSource(QUrl("qrc:qml/thumbnailer.qml")) |
46 self.__page.setNetworkAccessManager(self.__proxy) |
45 self.__view.rootContext().setContextProperty("thumbnailer", self) |
47 |
46 self.__view.show() |
48 self.__page.mainFrame().setScrollBarPolicy( |
|
49 Qt.Horizontal, Qt.ScrollBarAlwaysOff) |
|
50 self.__page.mainFrame().setScrollBarPolicy( |
|
51 Qt.Vertical, Qt.ScrollBarAlwaysOff) |
|
52 |
|
53 # Full HD |
|
54 # Every page should fit in this resolution |
|
55 self.__page.setViewportSize(QSize(1920, 1080)) |
|
56 |
47 |
57 def setSize(self, size): |
48 def setSize(self, size): |
58 """ |
49 """ |
59 Public method to set the size of the image. |
50 Public method to set the size of the image. |
60 |
51 |
101 """ |
92 """ |
102 Public method to get the title of the thumbnail. |
93 Public method to get the title of the thumbnail. |
103 |
94 |
104 @return title of the thumbnail (string) |
95 @return title of the thumbnail (string) |
105 """ |
96 """ |
106 return self.__title |
97 if self.__title: |
|
98 title = self.__title |
|
99 else: |
|
100 title = self.__url.host() |
|
101 if not title: |
|
102 title = self.__url.toString() |
|
103 return title |
107 |
104 |
108 def start(self): |
105 def start(self): |
109 """ |
106 """ |
110 Public method to start the thumbnailing action. |
107 Public method to start the thumbnailing action. |
111 """ |
108 """ |
112 self.__page.loadFinished.connect(self.__createThumbnail) |
109 if self.__view.rootObject(): |
113 self.__page.mainFrame().load(self.__url) |
110 self.__view.rootObject().setProperty("url", self.__url) |
|
111 else: |
|
112 QTimer.singleShot(0, lambda: self.thumbnailCreated.emit(QPixmap())) |
114 |
113 |
115 def __createThumbnail(self, status): |
114 @pyqtSlot(bool) |
|
115 def createThumbnail(self, status): |
116 """ |
116 """ |
117 Private slot creating the thumbnail of the web site. |
117 Private slot creating the thumbnail of the web site. |
118 |
118 |
119 @param status flag indicating a successful load of the web site |
119 @param status flag indicating a successful load of the web site |
120 (boolean) |
120 (boolean) |
121 """ |
121 """ |
122 if not status: |
122 if not status: |
123 self.thumbnailCreated.emit(QPixmap()) |
123 self.thumbnailCreated.emit(QPixmap()) |
124 return |
124 return |
125 |
125 |
126 self.__title = self.__page.mainFrame().title() |
126 QTimer.singleShot(1000, self.__grabThumbnail) |
127 |
127 |
128 image = QImage(self.__page.viewportSize(), QImage.Format_ARGB32) |
128 def __grabThumbnail(self): |
129 painter = QPainter(image) |
129 """ |
130 self.__page.mainFrame().render(painter) |
130 Private slot to grab the thumbnail image from the view. |
131 painter.end() |
131 """ |
132 |
132 self.__title = self.__view.rootObject().property("title") |
133 scaledImage = image.scaled(self.__size, |
133 pixmap = QPixmap.fromImage( |
134 Qt.KeepAspectRatioByExpanding, |
134 self.__view.grabFramebuffer().scaled( |
135 Qt.SmoothTransformation) |
135 self.__size, Qt.KeepAspectRatioByExpanding, |
136 |
136 ## self.__size, Qt.IgnoreAspectRatio, |
137 self.thumbnailCreated.emit(QPixmap.fromImage(scaledImage)) |
137 Qt.SmoothTransformation)) |
|
138 self.thumbnailCreated.emit(pixmap) |