eric7/HelpViewer/HelpViewerImpl.py

branch
eric7
changeset 8680
85503ff2fce9
parent 8679
fd172973428e
child 8681
6285e8374d99
equal deleted inserted replaced
8679:fd172973428e 8680:85503ff2fce9
5 5
6 """ 6 """
7 Module implementing the help viewer base class. 7 Module implementing the help viewer base class.
8 """ 8 """
9 9
10 from PyQt6.QtCore import QObject 10 from PyQt6.QtCore import pyqtSignal, QCoreApplication
11
12 AboutBlank = QCoreApplication.translate(
13 "HelpViewer",
14 "<html>"
15 "<head><title>about:blank</title></head>"
16 "<body></body>"
17 "</html>")
18
19 PageNotFound = QCoreApplication.translate(
20 "HelpViewer",
21 """<html>"""
22 """<head><title>Error 404...</title></head>"""
23 """<body><div align="center"><br><br>"""
24 """<h1>The page could not be found</h1><br>"""
25 """<h3>'{0}'</h3></div></body>"""
26 """</html>""")
11 27
12 28
13 class HelpViewerImpl(QObject): 29 class HelpViewerImpl:
14 """ 30 """
15 Class implementing the help viewer base class. 31 Class implementing the help viewer base class.
32
33 This is the base class of help viewer implementations and defines the
34 interface. Als subclasses must implement the these methods.
16 """ 35 """
17 def __init__(self, parent=None): 36 titleChanged = pyqtSignal()
37 loadFinished = pyqtSignal(bool)
38
39 def __init__(self, engine):
18 """ 40 """
19 Constructor 41 Constructor
20 42
21 @param parent reference to the parent widget 43 @param engine reference to the help engine
22 @type QWidget 44 @type QHelpEngine
23 """ 45 """
24 super().__init__(parent) 46 self._engine = engine
47
48 def setUrl(self, url):
49 """
50 Public method to set the URL of the document to be shown.
51
52 @param url source of the document
53 @type QUrl
54 @exception RuntimeError raised when not implemented
55 """
56 raise RuntimeError("Not implemented")
57
58 def getData(self, url):
59 """
60 Public method to get the data to be shown.
61
62 @param url URL to be loaded
63 @type QUrl
64 @return data to be shown
65 @rtype str
66 """
67 scheme = url.scheme()
68 if scheme == "about":
69 if url.toString() == "about:blank":
70 return AboutBlank
71 else:
72 return PageNotFound.format(url.toString())
73 elif scheme in ("file", ""):
74 filePath = url.toLocalFile()
75 try:
76 with open(filePath, "r", encoding="utf-8") as f:
77 htmlText = f.read()
78 return htmlText
79 except OSError:
80 return PageNotFound.format(url.toString())
81 elif scheme == "qthelp":
82 if self._engine.findFile(url).isValid():
83 data = bytes(self._engine.fileData(url)).decode("utf-8")
84 if not data:
85 data = PageNotFound.format(url.toString())
86 return data
87 else:
88 return PageNotFound.format(url.toString())
89 else:
90 # None is an indicator that we cannot handle the request
91 return None
92
93 def title(self):
94 """
95 Public method get the page title.
96
97 @return page title
98 @rtype str
99 @exception RuntimeError raised when not implemented
100 """
101 raise RuntimeError("Not implemented")
102 return ""
103
104 def gotoHistory(self, index):
105 """
106 Public method to step through the history.
107
108 @param index history index (<0 backward, >0 forward)
109 @type int
110 @exception RuntimeError raised when not implemented
111 """
112 raise RuntimeError("Not implemented")

eric ide

mercurial