WebBrowser/Network/QtHelpSchemeHandler.py

changeset 6507
54054456ca58
parent 6120
4c60a21ce6dd
child 6530
25b9dcfd6fcc
child 6542
dac410f3aff4
equal deleted inserted replaced
6506:ff6172ce89a9 6507:54054456ca58
10 from __future__ import unicode_literals 10 from __future__ import unicode_literals
11 11
12 import mimetypes 12 import mimetypes
13 import os 13 import os
14 14
15 from PyQt5.QtCore import pyqtSignal, QByteArray, QIODevice, QBuffer 15 from PyQt5.QtCore import pyqtSignal, QByteArray, QIODevice, QBuffer, QTimer, \
16 QMutex, QMutexLocker
16 from PyQt5.QtWebEngineCore import QWebEngineUrlSchemeHandler, \ 17 from PyQt5.QtWebEngineCore import QWebEngineUrlSchemeHandler, \
17 QWebEngineUrlRequestJob 18 QWebEngineUrlRequestJob
18 19
19 QtDocPath = "qthelp://org.qt-project." 20 QtDocPath = "qthelp://org.qt-project."
20 21
116 @param parent reference to the parent object 117 @param parent reference to the parent object
117 @type QObject 118 @type QObject
118 """ 119 """
119 super(QtHelpSchemeReply, self).__init__(parent) 120 super(QtHelpSchemeReply, self).__init__(parent)
120 121
121 url = job.requestUrl() 122 self.__job = job
123 self.__engine = engine
124 self.__mutex = QMutex()
125
126 self.__buffer = QBuffer()
127
128 # determine mimetype
129 url = self.__job.requestUrl()
122 strUrl = url.toString() 130 strUrl = url.toString()
123
124 self.__buffer = QBuffer()
125 131
126 # For some reason the url to load maybe wrong (passed from web engine) 132 # For some reason the url to load maybe wrong (passed from web engine)
127 # though the css file and the references inside should work that way. 133 # though the css file and the references inside should work that way.
128 # One possible problem might be that the css is loaded at the same 134 # One possible problem might be that the css is loaded at the same
129 # level as the html, thus a path inside the css like 135 # level as the html, thus a path inside the css like
130 # (../images/foo.png) might cd out of the virtual folder 136 # (../images/foo.png) might cd out of the virtual folder
131 if not engine.findFile(url).isValid(): 137 if not self.__engine.findFile(url).isValid():
132 if strUrl.startswith(QtDocPath): 138 if strUrl.startswith(QtDocPath):
133 newUrl = job.requestUrl() 139 newUrl = self.__job.requestUrl()
134 if not newUrl.path().startswith("/qdoc/"): 140 if not newUrl.path().startswith("/qdoc/"):
135 newUrl.setPath("/qdoc" + newUrl.path()) 141 newUrl.setPath("/qdoc" + newUrl.path())
136 url = newUrl 142 url = newUrl
137 strUrl = url.toString() 143 strUrl = url.toString()
138 144
139 self.__mimeType = mimetypes.guess_type(strUrl)[0] 145 self.__mimeType = mimetypes.guess_type(strUrl)[0]
140 if self.__mimeType is None: 146 if self.__mimeType is None:
141 # do our own (limited) guessing 147 # do our own (limited) guessing
142 self.__mimeType = self.__mimeFromUrl(url) 148 self.__mimeType = self.__mimeFromUrl(url)
143 149
144 if engine.findFile(url).isValid(): 150 QTimer.singleShot(0, lambda: self.__loadQtHelpPage(url))
145 data = engine.fileData(url) 151
152 def __loadQtHelpPage(self, url):
153 """
154 Private method to load the requested QtHelp page.
155
156 @param url URL of the requested page
157 @type QUrl
158 """
159
160 if self.__engine.findFile(url).isValid():
161 data = self.__engine.fileData(url)
146 else: 162 else:
147 data = QByteArray(self.tr( 163 data = QByteArray(self.tr(
148 """<html>""" 164 """<html>"""
149 """<head><title>Error 404...</title></head>""" 165 """<head><title>Error 404...</title></head>"""
150 """<body><div align="center"><br><br>""" 166 """<body><div align="center"><br><br>"""
151 """<h1>The page could not be found</h1><br>""" 167 """<h1>The page could not be found</h1><br>"""
152 """<h3>'{0}'</h3></div></body>""" 168 """<h3>'{0}'</h3></div></body>"""
153 """</html>""").format(strUrl) 169 """</html>""").format(url.toString())
154 .encode("utf-8")) 170 .encode("utf-8"))
155 171
172 lock = QMutexLocker(self.__mutex)
156 self.__buffer.setData(data) 173 self.__buffer.setData(data)
157 self.__buffer.open(QIODevice.ReadOnly) 174 self.__buffer.open(QIODevice.ReadOnly)
175 lock.unlock()
176
158 self.open(QIODevice.ReadOnly) 177 self.open(QIODevice.ReadOnly)
178 self.readyRead.emit()
159 179
160 def bytesAvailable(self): 180 def bytesAvailable(self):
161 """ 181 """
162 Public method to get the number of available bytes. 182 Public method to get the number of available bytes.
163 183
164 @return number of available bytes 184 @return number of available bytes
165 @rtype int 185 @rtype int
166 """ 186 """
187 lock = QMutexLocker(self.__mutex) # __IGNORE_WARNING__
167 return self.__buffer.bytesAvailable() 188 return self.__buffer.bytesAvailable()
168 189
169 def readData(self, maxlen): 190 def readData(self, maxlen):
170 """ 191 """
171 Public method to retrieve data from the reply object. 192 Public method to retrieve data from the reply object.
172 193
173 @param maxlen maximum number of bytes to read (integer) 194 @param maxlen maximum number of bytes to read (integer)
174 @return string containing the data (bytes) 195 @return string containing the data (bytes)
175 """ 196 """
197 lock = QMutexLocker(self.__mutex) # __IGNORE_WARNING__
176 return self.__buffer.read(maxlen) 198 return self.__buffer.read(maxlen)
177 199
178 def close(self): 200 def close(self):
179 """ 201 """
180 Public method used to cloase the reply. 202 Public method used to cloase the reply.

eric ide

mercurial