diff -r fd172973428e -r 85503ff2fce9 eric7/HelpViewer/HelpViewerImpl.py --- a/eric7/HelpViewer/HelpViewerImpl.py Thu Oct 07 20:22:02 2021 +0200 +++ b/eric7/HelpViewer/HelpViewerImpl.py Mon Oct 11 19:59:45 2021 +0200 @@ -7,18 +7,106 @@ Module implementing the help viewer base class. """ -from PyQt6.QtCore import QObject +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(QObject): +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. """ - def __init__(self, parent=None): + titleChanged = pyqtSignal() + loadFinished = pyqtSignal(bool) + + def __init__(self, engine): """ Constructor - @param parent reference to the parent widget - @type QWidget + @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 """ - super().__init__(parent) + 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")