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