--- a/eric6/WebBrowser/Tools/WebBrowserTools.py Thu Sep 24 19:51:19 2020 +0200 +++ b/eric6/WebBrowser/Tools/WebBrowserTools.py Sat Sep 26 10:58:18 2020 +0200 @@ -10,6 +10,7 @@ import os import re +import mimetypes from PyQt5.QtCore import ( QFile, QByteArray, QUrl, QCoreApplication, QBuffer, QIODevice @@ -17,6 +18,13 @@ from PyQt5.QtGui import QPixmap +WebBrowserDataDirectory = { + "html": os.path.join(os.path.dirname(__file__), "..", "data", "html"), + "icons": os.path.join(os.path.dirname(__file__), "..", "data", "icons"), + "js": os.path.join(os.path.dirname(__file__), "..", "data", "javascript"), +} + + def readAllFileContents(filename): """ Function to read the string contents of the given file. @@ -171,7 +179,7 @@ return QByteArray() -def pixmapToDataUrl(pixmap): +def pixmapToDataUrl(pixmap, mimetype="image/png"): """ Module function to convert a pixmap to a data: URL. @@ -182,11 +190,41 @@ """ data = bytes(pixmapToByteArray(pixmap)).decode() if data: - return QUrl("data:image/png;base64," + data) + return QUrl("data:{0};base64,{1}".format(mimetype, data)) else: return QUrl() +def pixmapFileToDataUrl(pixmapFile, asString=False): + """ + Module function to load a pixmap file and convert the pixmap to a + data: URL. + + Note: If the given pixmap file path is not absolute, it is assumed to + denote a pixmap file in the icons data directory. + + @param pixmapFile file name of the pixmap file + @type str + @param asString flag indicating a string representation is requested + @type bool + @return data: URL + @rtype QUrl or str + """ + if not os.path.isabs(pixmapFile): + pixmapFile = os.path.join(WebBrowserDataDirectory["icons"], pixmapFile) + + mime = mimetypes.guess_type(pixmapFile, strict=False)[0] + if mime is None: + # assume PNG file + mime = "image/png" + url = pixmapToDataUrl(QPixmap(pixmapFile), mimetype=mime) + + if asString: + return url.toString() + else: + return url + + def getWebEngineVersions(): """ Module function to extract the web engine version from the default user @@ -211,3 +249,42 @@ webengineVersion = QCoreApplication.translate( "WebBrowserTools", "<unknown>") return (chromeVersion, webengineVersion) + + +def getHtmlPage(pageFileName): + """ + Module function to load a HTML page. + + Note: If the given HTML file path is not absolute, it is assumed to + denote a HTML file in the html data directory. + + @param pageFileName file name of the HTML file + @type str + @return HTML page + @rtype str + """ + if not os.path.isabs(pageFileName): + pageFileName = os.path.join( + WebBrowserDataDirectory["html"], pageFileName) + + return readAllFileContents(pageFileName) + + +def getJavascript(jsFileName): + """ + Module function to load a JavaScript source file. + + Note: If the given JavaScript source file path is not absolute, it is + assumed to denote a JavaScript source file in the javascript data + directory. + + @param jsFileName file name of the JavaScript source file + @type str + @return JavaScript source + @rtype str + """ + if not os.path.isabs(jsFileName): + jsFileName = os.path.join( + WebBrowserDataDirectory["js"], jsFileName) + + return readAllFileContents(jsFileName)