--- a/eric7/HelpViewer/HelpViewerImplQTB.py Mon Oct 18 16:44:29 2021 +0200 +++ b/eric7/HelpViewer/HelpViewerImplQTB.py Mon Oct 18 16:45:05 2021 +0200 @@ -7,6 +7,7 @@ Module implementing the QTextBrowser based help viewer class. """ +import contextlib import functools from PyQt6.QtCore import ( @@ -92,52 +93,67 @@ @param type_ type of the resource to load @type QTextDocument.ResourceType """ - data = self.__getData(url) - if data is None: + if not self.__canLoadResource(url): QDesktopServices.openUrl(url) return - if url != QUrl("about:blank"): - super().doSetSource(url, type) + super().doSetSource(url, type_) - self.setHtml(data) self.sourceChanged.emit(url) self.loadFinished.emit(True) - def __getData(self, url): + def loadResource(self, type_, name): """ - Private method to get the data to be shown. + Public method to load data of the specified type from the resource with + the given name. + + @param type_ resource type + @type int + @param name resource name + @type QUrl + @return byte array containing the loaded data + @rtype QByteArray + """ + ba = QByteArray() + scheme = name.scheme() - @param url URL to be loaded + if type_ < 4: # QTextDocument.ResourceType.MarkdownResource + if scheme == "about": + if name.toString() == "about:blank": + return QByteArray(AboutBlank.encode("utf-8")) + elif scheme in ("file", ""): + filePath = name.toLocalFile() + with contextlib.suppress(OSError), open(filePath, "rb") as f: + ba = QByteArray(f.read()) + elif scheme == "qthelp": + url = self._engine.findFile(name) + if url.isValid(): + ba = self._engine.fileData(url) + + if name.toString().lower().endswith(".svg"): + image = QImage() + image.loadFromData(ba, "svg") + if not image.isNull(): + return image + + if ba.isEmpty(): + ba = QByteArray( + PageNotFound.format(name.toString()).encode("utf-8") + ) + + return ba + + def __canLoadResource(self, url): + """ + Private method to check, if the given resource can be loaded. + + @param url URL of resource to be loaded @type QUrl - @return data to be shown - @rtype str + @return flag indicating, that the given URL can be handled + @rtype bool """ scheme = url.scheme() - if scheme == "about": - if url.toString() == "about:blank": - return AboutBlank - else: - return PageNotFound.format(url.toString()) - elif scheme in ("file", ""): - filePath = url.toLocalFile() - try: - with open(filePath, "r", encoding="utf-8") as f: - htmlText = f.read() - return htmlText - except OSError: - return PageNotFound.format(url.toString()) - elif scheme == "qthelp": - if self._engine.findFile(url).isValid(): - data = bytes(self._engine.fileData(url)).decode("utf-8") - if not data: - data = PageNotFound.format(url.toString()) - return data - else: - return PageNotFound.format(url.toString()) - else: - # None is an indicator that we cannot handle the request - return None + return scheme in ("about", "qthelp", "file", "") def pageTitle(self): """ @@ -160,32 +176,6 @@ return titleStr - def loadResource(self, type_, name): - """ - Public method to load data of the specified type from the resource with - the given name. - - @param type_ resource type - @type int - @param name resource name - @type QUrl - @return byte array containing the loaded data - @rtype QByteArray - """ - ba = QByteArray() - - if type_ < 4: # QTextDocument.ResourceType.MarkdownResource - # TODO: change to use getData() - url = self._engine.findFile(name) - ba = self._engine.fileData(url) - if url.toString().lower().endswith(".svg"): - image = QImage() - image.loadFromData(ba, "svg") - if not image.isNull(): - return image - - return ba - def mousePressEvent(self, evt): """ Protected method called by a mouse press event.