def getImage(self, docid, name): localname = resourceNameToLocal(name, ext='.png') path = self.localImageNamePath(docid, localname) self.log.info("getImage(): %s: %s: %s", docid, name, path) try: f = Qt.QFile(path) if not f.open(Qt.QFile.ReadOnly): self.log.error("getImage(): could not open file: %s", path) return None data = f.readAll() f.close() del f image = Qt.QPixmap() image.loadFromData(data) data.clear() del data return image except Exception as e: self.log.error("getImage(): exception: %s: %s: %s", path, e.__class__.__name__, e) return None
def loadResource(self, p_int, _qurl): url = _qurl.toString() if url.startswith('data:image/'): return super(RTDocument, self).loadResource(p_int, _qurl) self.editor.log.info("loadResource(): %s", url) scheme = _qurl.scheme() image = self.editor.project.doc.getImage(self.docid, url) if image: self.editor.doc.addResource(Qt.QTextDocument.ImageResource, _qurl, image) return image if scheme: if scheme in ('http', 'https'): self.editor.log.info("Trying retrive remote image: %s", url) # remote image get it from network image = self.loadResourceRemote(url) if image: self.editor.doc.addResource(Qt.QTextDocument.ImageResource, _qurl, image) return image if scheme == 'file': try: filename = Qt.QDir.toNativeSeparators(_qurl.toLocalFile()) self.editor.log.info("Trying retrive local image: %s", filename) f = Qt.QFile(filename) if not f.open(Qt.QFile.ReadOnly): self.log.error("loadResource(): could not open file: %s", url) return self.loadResourceMissing(_qurl) data = f.readAll() f.close() del f image = Qt.QPixmap() image.loadFromData(data) data.clear() del data if image: self.editor.doc.addResource(Qt.QTextDocument.ImageResource, _qurl, image) return image except Exception as e: self.log.error("Failed to load image: %s: %s", e.__class__.__name__, e) res = super(RTDocument, self).loadResource(p_int, _qurl) if res: return res return self.loadResourceMissing(_qurl)