8 """ |
8 """ |
9 |
9 |
10 |
10 |
11 import os |
11 import os |
12 import re |
12 import re |
|
13 import mimetypes |
13 |
14 |
14 from PyQt5.QtCore import ( |
15 from PyQt5.QtCore import ( |
15 QFile, QByteArray, QUrl, QCoreApplication, QBuffer, QIODevice |
16 QFile, QByteArray, QUrl, QCoreApplication, QBuffer, QIODevice |
16 ) |
17 ) |
17 from PyQt5.QtGui import QPixmap |
18 from PyQt5.QtGui import QPixmap |
|
19 |
|
20 |
|
21 WebBrowserDataDirectory = { |
|
22 "html": os.path.join(os.path.dirname(__file__), "..", "data", "html"), |
|
23 "icons": os.path.join(os.path.dirname(__file__), "..", "data", "icons"), |
|
24 "js": os.path.join(os.path.dirname(__file__), "..", "data", "javascript"), |
|
25 } |
18 |
26 |
19 |
27 |
20 def readAllFileContents(filename): |
28 def readAllFileContents(filename): |
21 """ |
29 """ |
22 Function to read the string contents of the given file. |
30 Function to read the string contents of the given file. |
169 return buffer.buffer().toBase64() |
177 return buffer.buffer().toBase64() |
170 |
178 |
171 return QByteArray() |
179 return QByteArray() |
172 |
180 |
173 |
181 |
174 def pixmapToDataUrl(pixmap): |
182 def pixmapToDataUrl(pixmap, mimetype="image/png"): |
175 """ |
183 """ |
176 Module function to convert a pixmap to a data: URL. |
184 Module function to convert a pixmap to a data: URL. |
177 |
185 |
178 @param pixmap pixmap to be converted |
186 @param pixmap pixmap to be converted |
179 @type QPixmap |
187 @type QPixmap |
180 @return data: URL |
188 @return data: URL |
181 @rtype QUrl |
189 @rtype QUrl |
182 """ |
190 """ |
183 data = bytes(pixmapToByteArray(pixmap)).decode() |
191 data = bytes(pixmapToByteArray(pixmap)).decode() |
184 if data: |
192 if data: |
185 return QUrl("data:image/png;base64," + data) |
193 return QUrl("data:{0};base64,{1}".format(mimetype, data)) |
186 else: |
194 else: |
187 return QUrl() |
195 return QUrl() |
|
196 |
|
197 |
|
198 def pixmapFileToDataUrl(pixmapFile, asString=False): |
|
199 """ |
|
200 Module function to load a pixmap file and convert the pixmap to a |
|
201 data: URL. |
|
202 |
|
203 Note: If the given pixmap file path is not absolute, it is assumed to |
|
204 denote a pixmap file in the icons data directory. |
|
205 |
|
206 @param pixmapFile file name of the pixmap file |
|
207 @type str |
|
208 @param asString flag indicating a string representation is requested |
|
209 @type bool |
|
210 @return data: URL |
|
211 @rtype QUrl or str |
|
212 """ |
|
213 if not os.path.isabs(pixmapFile): |
|
214 pixmapFile = os.path.join(WebBrowserDataDirectory["icons"], pixmapFile) |
|
215 |
|
216 mime = mimetypes.guess_type(pixmapFile, strict=False)[0] |
|
217 if mime is None: |
|
218 # assume PNG file |
|
219 mime = "image/png" |
|
220 url = pixmapToDataUrl(QPixmap(pixmapFile), mimetype=mime) |
|
221 |
|
222 if asString: |
|
223 return url.toString() |
|
224 else: |
|
225 return url |
188 |
226 |
189 |
227 |
190 def getWebEngineVersions(): |
228 def getWebEngineVersions(): |
191 """ |
229 """ |
192 Module function to extract the web engine version from the default user |
230 Module function to extract the web engine version from the default user |
209 webengineVersion = match.group(1) |
247 webengineVersion = match.group(1) |
210 else: |
248 else: |
211 webengineVersion = QCoreApplication.translate( |
249 webengineVersion = QCoreApplication.translate( |
212 "WebBrowserTools", "<unknown>") |
250 "WebBrowserTools", "<unknown>") |
213 return (chromeVersion, webengineVersion) |
251 return (chromeVersion, webengineVersion) |
|
252 |
|
253 |
|
254 def getHtmlPage(pageFileName): |
|
255 """ |
|
256 Module function to load a HTML page. |
|
257 |
|
258 Note: If the given HTML file path is not absolute, it is assumed to |
|
259 denote a HTML file in the html data directory. |
|
260 |
|
261 @param pageFileName file name of the HTML file |
|
262 @type str |
|
263 @return HTML page |
|
264 @rtype str |
|
265 """ |
|
266 if not os.path.isabs(pageFileName): |
|
267 pageFileName = os.path.join( |
|
268 WebBrowserDataDirectory["html"], pageFileName) |
|
269 |
|
270 return readAllFileContents(pageFileName) |
|
271 |
|
272 |
|
273 def getJavascript(jsFileName): |
|
274 """ |
|
275 Module function to load a JavaScript source file. |
|
276 |
|
277 Note: If the given JavaScript source file path is not absolute, it is |
|
278 assumed to denote a JavaScript source file in the javascript data |
|
279 directory. |
|
280 |
|
281 @param jsFileName file name of the JavaScript source file |
|
282 @type str |
|
283 @return JavaScript source |
|
284 @rtype str |
|
285 """ |
|
286 if not os.path.isabs(jsFileName): |
|
287 jsFileName = os.path.join( |
|
288 WebBrowserDataDirectory["js"], jsFileName) |
|
289 |
|
290 return readAllFileContents(jsFileName) |