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 pyqtSignal, QCoreApplication |
10 from PyQt6.QtCore import pyqtSignal, QUrl |
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>""") |
|
27 |
11 |
28 |
12 |
29 class HelpViewerImpl: |
13 class HelpViewerImpl: |
30 """ |
14 """ |
31 Class implementing the help viewer base class. |
15 Class implementing the help viewer base class. |
32 |
16 |
33 This is the base class of help viewer implementations and defines the |
17 This is the base class of help viewer implementations and defines the |
34 interface. Als subclasses must implement the these methods. |
18 interface. Als subclasses must implement the these methods. |
|
19 |
|
20 @signal titleChanged() emitted to indicate a change of the page title |
|
21 @signal loadFinished(ok) emitted to indicate the completion of a page load |
|
22 @signal zoomChanged() emitted to indicate a change of the zoom level |
35 """ |
23 """ |
36 titleChanged = pyqtSignal() |
24 titleChanged = pyqtSignal() |
37 loadFinished = pyqtSignal(bool) |
25 loadFinished = pyqtSignal(bool) |
38 zoomChanged = pyqtSignal() |
26 zoomChanged = pyqtSignal() |
39 |
27 |
44 @param engine reference to the help engine |
32 @param engine reference to the help engine |
45 @type QHelpEngine |
33 @type QHelpEngine |
46 """ |
34 """ |
47 self._engine = engine |
35 self._engine = engine |
48 |
36 |
49 def setUrl(self, url): |
37 def setLink(self, url): |
50 """ |
38 """ |
51 Public method to set the URL of the document to be shown. |
39 Public method to set the URL of the document to be shown. |
52 |
40 |
53 @param url URL of the document |
41 @param url URL of the document |
54 @type QUrl |
42 @type QUrl |
55 @exception RuntimeError raised when not implemented |
43 @exception RuntimeError raised when not implemented |
56 """ |
44 """ |
57 raise RuntimeError("Not implemented") |
45 raise RuntimeError("Not implemented") |
58 |
46 |
59 def url(self): |
47 def link(self): |
60 """ |
48 """ |
61 Public method to get the URL of the shown document. |
49 Public method to get the URL of the shown document. |
62 |
50 |
63 @return url URL of the document |
51 @return URL of the document |
64 @rtype QUrl |
52 @rtype QUrl |
65 @exception RuntimeError raised when not implemented |
53 @exception RuntimeError raised when not implemented |
66 """ |
54 """ |
67 raise RuntimeError("Not implemented") |
55 raise RuntimeError("Not implemented") |
68 return None |
56 return QUrl() |
69 |
57 |
70 def getData(self, url): |
58 def pageTitle(self): |
71 """ |
|
72 Public method to get the data to be shown. |
|
73 |
|
74 @param url URL to be loaded |
|
75 @type QUrl |
|
76 @return data to be shown |
|
77 @rtype str |
|
78 """ |
|
79 scheme = url.scheme() |
|
80 if scheme == "about": |
|
81 if url.toString() == "about:blank": |
|
82 return AboutBlank |
|
83 else: |
|
84 return PageNotFound.format(url.toString()) |
|
85 elif scheme in ("file", ""): |
|
86 filePath = url.toLocalFile() |
|
87 try: |
|
88 with open(filePath, "r", encoding="utf-8") as f: |
|
89 htmlText = f.read() |
|
90 return htmlText |
|
91 except OSError: |
|
92 return PageNotFound.format(url.toString()) |
|
93 elif scheme == "qthelp": |
|
94 if self._engine.findFile(url).isValid(): |
|
95 data = bytes(self._engine.fileData(url)).decode("utf-8") |
|
96 if not data: |
|
97 data = PageNotFound.format(url.toString()) |
|
98 return data |
|
99 else: |
|
100 return PageNotFound.format(url.toString()) |
|
101 else: |
|
102 # None is an indicator that we cannot handle the request |
|
103 return None |
|
104 |
|
105 def title(self): |
|
106 """ |
59 """ |
107 Public method get the page title. |
60 Public method get the page title. |
108 |
61 |
109 @return page title |
62 @return page title |
110 @rtype str |
63 @rtype str |
126 def isBackwardAvailable(self): |
79 def isBackwardAvailable(self): |
127 """ |
80 """ |
128 Public method to check, if stepping backward through the history is |
81 Public method to check, if stepping backward through the history is |
129 available. |
82 available. |
130 |
83 |
|
84 @return flag indicating backward stepping is available |
|
85 @rtype bool |
131 @exception RuntimeError raised when not implemented |
86 @exception RuntimeError raised when not implemented |
132 """ |
87 """ |
133 raise RuntimeError("Not implemented") |
88 raise RuntimeError("Not implemented") |
134 return False |
89 return False |
135 |
90 |
136 def isForwardAvailable(self): |
91 def isForwardAvailable(self): |
137 """ |
92 """ |
138 Public method to check, if stepping forward through the history is |
93 Public method to check, if stepping forward through the history is |
139 available. |
94 available. |
140 |
95 |
|
96 @return flag indicating forward stepping is available |
|
97 @rtype bool |
141 @exception RuntimeError raised when not implemented |
98 @exception RuntimeError raised when not implemented |
142 """ |
99 """ |
143 raise RuntimeError("Not implemented") |
100 raise RuntimeError("Not implemented") |
144 return False |
101 return False |
145 |
102 |