Mon, 11 Oct 2021 19:59:45 +0200
Continued implementing the embedded help viewer widget.
# -*- coding: utf-8 -*- # Copyright (c) 2021 Detlev Offenbach <detlev@die-offenbachs.de> # """ Module implementing the help viewer base class. """ from PyQt6.QtCore import pyqtSignal, QCoreApplication AboutBlank = QCoreApplication.translate( "HelpViewer", "<html>" "<head><title>about:blank</title></head>" "<body></body>" "</html>") PageNotFound = QCoreApplication.translate( "HelpViewer", """<html>""" """<head><title>Error 404...</title></head>""" """<body><div align="center"><br><br>""" """<h1>The page could not be found</h1><br>""" """<h3>'{0}'</h3></div></body>""" """</html>""") class HelpViewerImpl: """ Class implementing the help viewer base class. This is the base class of help viewer implementations and defines the interface. Als subclasses must implement the these methods. """ titleChanged = pyqtSignal() loadFinished = pyqtSignal(bool) def __init__(self, engine): """ Constructor @param engine reference to the help engine @type QHelpEngine """ self._engine = engine def setUrl(self, url): """ Public method to set the URL of the document to be shown. @param url source of the document @type QUrl @exception RuntimeError raised when not implemented """ raise RuntimeError("Not implemented") def getData(self, url): """ Public method to get the data to be shown. @param url URL to be loaded @type QUrl @return data to be shown @rtype str """ 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 def title(self): """ Public method get the page title. @return page title @rtype str @exception RuntimeError raised when not implemented """ raise RuntimeError("Not implemented") return "" def gotoHistory(self, index): """ Public method to step through the history. @param index history index (<0 backward, >0 forward) @type int @exception RuntimeError raised when not implemented """ raise RuntimeError("Not implemented")