Exemple #1
0
    def insertImage(self, path, image=None):
        qurl = Qt.QUrl.fromLocalFile(path)
        url = qurl.toString()
        self.log.info("insertImage(): %s: %s", path, url)

        if not image:
            image = loadQImageFix(path)
            if not image:
                return

        qbaimage = Qt.QByteArray()
        qb = Qt.QBuffer(qbaimage)
        qb.open(Qt.QIODevice.WriteOnly)
        image.save(qb, 'PNG')
        del image
        qb.close()

        data = Qt.QByteArray()
        data.append(b'data:image/png;base64,')
        data.append(qbaimage.toBase64())
        qbaimage.clear()
        del qbaimage

        strdata = str(data, 'ascii')
        data.clear()
        del data

        imagef = Qt.QTextImageFormat()
        imagef.setName(strdata)

        cursor = self.editor.textCursor()
        cursor.insertImage(imagef)
        del imagef
Exemple #2
0
    def loadResourceRemote(self, url):
        # TODO: show wait/progress dialog/info
        try:
            ret = requests.get(url)
            if ret.status_code not in (200,):
                return None

            data = Qt.QByteArray(ret.content)
            image = Qt.QPixmap()
            image.loadFromData(data)
            data.clear()
            return image
        except Exception as e:
            self.log.error("Failed to retrive remote image: %s: %s", e.__class__.__name__, e)