eric7/HelpViewer/HelpViewerImplQTB.py

branch
eric7
changeset 8700
112271b4a2eb
parent 8697
936662560d04
child 8702
131ef7267fd4
equal deleted inserted replaced
8699:8438a5a0437f 8700:112271b4a2eb
5 5
6 """ 6 """
7 Module implementing the QTextBrowser based help viewer class. 7 Module implementing the QTextBrowser based help viewer class.
8 """ 8 """
9 9
10 import contextlib
10 import functools 11 import functools
11 12
12 from PyQt6.QtCore import ( 13 from PyQt6.QtCore import (
13 pyqtSlot, Qt, QByteArray, QUrl, QEvent, QCoreApplication, QPoint 14 pyqtSlot, Qt, QByteArray, QUrl, QEvent, QCoreApplication, QPoint
14 ) 15 )
90 @param url URL of resource to load 91 @param url URL of resource to load
91 @type QUrl 92 @type QUrl
92 @param type_ type of the resource to load 93 @param type_ type of the resource to load
93 @type QTextDocument.ResourceType 94 @type QTextDocument.ResourceType
94 """ 95 """
95 data = self.__getData(url) 96 if not self.__canLoadResource(url):
96 if data is None:
97 QDesktopServices.openUrl(url) 97 QDesktopServices.openUrl(url)
98 return 98 return
99 99
100 if url != QUrl("about:blank"): 100 super().doSetSource(url, type_)
101 super().doSetSource(url, type) 101
102
103 self.setHtml(data)
104 self.sourceChanged.emit(url) 102 self.sourceChanged.emit(url)
105 self.loadFinished.emit(True) 103 self.loadFinished.emit(True)
106 104
107 def __getData(self, url): 105 def loadResource(self, type_, name):
108 """ 106 """
109 Private method to get the data to be shown. 107 Public method to load data of the specified type from the resource with
110 108 the given name.
111 @param url URL to be loaded 109
110 @param type_ resource type
111 @type int
112 @param name resource name
112 @type QUrl 113 @type QUrl
113 @return data to be shown 114 @return byte array containing the loaded data
114 @rtype str 115 @rtype QByteArray
116 """
117 ba = QByteArray()
118 scheme = name.scheme()
119
120 if type_ < 4: # QTextDocument.ResourceType.MarkdownResource
121 if scheme == "about":
122 if name.toString() == "about:blank":
123 return QByteArray(AboutBlank.encode("utf-8"))
124 elif scheme in ("file", ""):
125 filePath = name.toLocalFile()
126 with contextlib.suppress(OSError), open(filePath, "rb") as f:
127 ba = QByteArray(f.read())
128 elif scheme == "qthelp":
129 url = self._engine.findFile(name)
130 if url.isValid():
131 ba = self._engine.fileData(url)
132
133 if name.toString().lower().endswith(".svg"):
134 image = QImage()
135 image.loadFromData(ba, "svg")
136 if not image.isNull():
137 return image
138
139 if ba.isEmpty():
140 ba = QByteArray(
141 PageNotFound.format(name.toString()).encode("utf-8")
142 )
143
144 return ba
145
146 def __canLoadResource(self, url):
147 """
148 Private method to check, if the given resource can be loaded.
149
150 @param url URL of resource to be loaded
151 @type QUrl
152 @return flag indicating, that the given URL can be handled
153 @rtype bool
115 """ 154 """
116 scheme = url.scheme() 155 scheme = url.scheme()
117 if scheme == "about": 156 return scheme in ("about", "qthelp", "file", "")
118 if url.toString() == "about:blank":
119 return AboutBlank
120 else:
121 return PageNotFound.format(url.toString())
122 elif scheme in ("file", ""):
123 filePath = url.toLocalFile()
124 try:
125 with open(filePath, "r", encoding="utf-8") as f:
126 htmlText = f.read()
127 return htmlText
128 except OSError:
129 return PageNotFound.format(url.toString())
130 elif scheme == "qthelp":
131 if self._engine.findFile(url).isValid():
132 data = bytes(self._engine.fileData(url)).decode("utf-8")
133 if not data:
134 data = PageNotFound.format(url.toString())
135 return data
136 else:
137 return PageNotFound.format(url.toString())
138 else:
139 # None is an indicator that we cannot handle the request
140 return None
141 157
142 def pageTitle(self): 158 def pageTitle(self):
143 """ 159 """
144 Public method get the page title. 160 Public method get the page title.
145 161
157 173
158 if not titleStr or titleStr == "about:blank": 174 if not titleStr or titleStr == "about:blank":
159 titleStr = self.tr("Empty Page") 175 titleStr = self.tr("Empty Page")
160 176
161 return titleStr 177 return titleStr
162
163 def loadResource(self, type_, name):
164 """
165 Public method to load data of the specified type from the resource with
166 the given name.
167
168 @param type_ resource type
169 @type int
170 @param name resource name
171 @type QUrl
172 @return byte array containing the loaded data
173 @rtype QByteArray
174 """
175 ba = QByteArray()
176
177 if type_ < 4: # QTextDocument.ResourceType.MarkdownResource
178 # TODO: change to use getData()
179 url = self._engine.findFile(name)
180 ba = self._engine.fileData(url)
181 if url.toString().lower().endswith(".svg"):
182 image = QImage()
183 image.loadFromData(ba, "svg")
184 if not image.isNull():
185 return image
186
187 return ba
188 178
189 def mousePressEvent(self, evt): 179 def mousePressEvent(self, evt):
190 """ 180 """
191 Protected method called by a mouse press event. 181 Protected method called by a mouse press event.
192 182

eric ide

mercurial