WebBrowser/Tools/WebBrowserTools.py

branch
QtWebEngine
changeset 4865
4adc526bc4b3
parent 4810
f68d0446609e
child 5389
9b1c800daff3
equal deleted inserted replaced
4864:00a215a67f25 4865:4adc526bc4b3
14 pass 14 pass
15 15
16 import os 16 import os
17 import re 17 import re
18 18
19 from PyQt5.QtCore import QFile, QByteArray, QUrl, QCoreApplication 19 from PyQt5.QtCore import QFile, QByteArray, QUrl, QCoreApplication, QBuffer, \
20 QIODevice
20 from PyQt5.QtGui import QPixmap 21 from PyQt5.QtGui import QPixmap
21 22
22 23
23 def readAllFileContents(filename): 24 def readAllFileContents(filename):
24 """ 25 """
148 pixmap = QPixmap() 149 pixmap = QPixmap()
149 barray = QByteArray.fromBase64(data) 150 barray = QByteArray.fromBase64(data)
150 pixmap.loadFromData(barray) 151 pixmap.loadFromData(barray)
151 152
152 return pixmap 153 return pixmap
154
155
156 def pixmapToByteArray(pixmap):
157 """
158 Module function to convert a pixmap to a byte array containing the pixmap
159 as a PNG encoded as base64.
160
161 @param pixmap pixmap to be converted
162 @type QPixmap
163 @return byte array containing the pixmap
164 @rtype QByteArray
165 """
166 byteArray = QByteArray()
167 buffer = QBuffer(byteArray)
168 buffer.open(QIODevice.WriteOnly)
169 if pixmap.save(buffer, "PNG"):
170 return buffer.buffer().toBase64()
171
172 return QByteArray()
173
174
175 def pixmapToDataUrl(pixmap):
176 """
177 Module function to convert a pixmap to a data: URL.
178
179 @param pixmap pixmap to be converted
180 @type QPixmap
181 @return data: URL
182 @rtype QUrl
183 """
184 data = bytes(pixmapToByteArray(pixmap)).decode()
185 if data:
186 return QUrl("data:image/png;base64," + data)
187 else:
188 return QUrl()
153 189
154 190
155 def getWebEngineVersions(): 191 def getWebEngineVersions():
156 """ 192 """
157 Module function to extract the web engine version from the default user 193 Module function to extract the web engine version from the default user

eric ide

mercurial