UI/Previewers/PreviewerHTML.py

changeset 4625
ac72a3d8f89e
parent 4615
7c090c9d389d
child 4631
5c1a96925da4
equal deleted inserted replaced
4624:aebdbffe02b6 4625:ac72a3d8f89e
49 self.titleLabel.setWordWrap(True) 49 self.titleLabel.setWordWrap(True)
50 self.titleLabel.setTextInteractionFlags(Qt.NoTextInteraction) 50 self.titleLabel.setTextInteractionFlags(Qt.NoTextInteraction)
51 self.__layout.addWidget(self.titleLabel) 51 self.__layout.addWidget(self.titleLabel)
52 52
53 try: 53 try:
54 from PyQt5.QtWebEngineWidgets import QWebEngineView
55 self.previewView = QWebEngineView(self)
56 self.__usesWebKit = False
57 except ImportError:
54 from PyQt5.QtWebKitWidgets import QWebPage, QWebView 58 from PyQt5.QtWebKitWidgets import QWebPage, QWebView
55 self.previewView = QWebView(self) 59 self.previewView = QWebView(self)
56 self.previewView.page().setLinkDelegationPolicy( 60 self.previewView.page().setLinkDelegationPolicy(
57 QWebPage.DelegateAllLinks) 61 QWebPage.DelegateAllLinks)
58 self.__usesWebKit = True 62 self.__usesWebKit = True
59 except ImportError:
60 from PyQt5.QtWebEngineWidgets import QWebEngineView
61 self.previewView = QWebEngineView(self)
62 self.__usesWebKit = False
63 63
64 sizePolicy = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding) 64 sizePolicy = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
65 sizePolicy.setHorizontalStretch(0) 65 sizePolicy.setHorizontalStretch(0)
66 sizePolicy.setVerticalStretch(0) 66 sizePolicy.setVerticalStretch(0)
67 sizePolicy.setHeightForWidth( 67 sizePolicy.setHeightForWidth(
198 @param filePath file path of the previewed editor (string) 198 @param filePath file path of the previewed editor (string)
199 @param html processed HTML text ready to be shown (string) 199 @param html processed HTML text ready to be shown (string)
200 """ 200 """
201 self.__previewedPath = Utilities.normcasepath( 201 self.__previewedPath = Utilities.normcasepath(
202 Utilities.fromNativeSeparators(filePath)) 202 Utilities.fromNativeSeparators(filePath))
203 self.__saveScrollBarPositions()
203 if self.__usesWebKit: 204 if self.__usesWebKit:
204 self.__saveScrollBarPositions()
205 self.previewView.page().mainFrame().contentsSizeChanged.connect( 205 self.previewView.page().mainFrame().contentsSizeChanged.connect(
206 self.__restoreScrollBarPositions)
207 else:
208 self.previewView.page().loadFinished.connect(
206 self.__restoreScrollBarPositions) 209 self.__restoreScrollBarPositions)
207 self.previewView.setHtml(html, baseUrl=QUrl.fromLocalFile(filePath)) 210 self.previewView.setHtml(html, baseUrl=QUrl.fromLocalFile(filePath))
208 211
209 @pyqtSlot(str) 212 @pyqtSlot(str)
210 def on_previewView_titleChanged(self, title): 213 def on_previewView_titleChanged(self, title):
231 self.__scrollBarPositions[self.__previewedPath] = pos 234 self.__scrollBarPositions[self.__previewedPath] = pos
232 self.__hScrollBarAtEnd[self.__previewedPath] = \ 235 self.__hScrollBarAtEnd[self.__previewedPath] = \
233 frame.scrollBarMaximum(Qt.Horizontal) == pos.x() 236 frame.scrollBarMaximum(Qt.Horizontal) == pos.x()
234 self.__vScrollBarAtEnd[self.__previewedPath] = \ 237 self.__vScrollBarAtEnd[self.__previewedPath] = \
235 frame.scrollBarMaximum(Qt.Vertical) == pos.y() 238 frame.scrollBarMaximum(Qt.Vertical) == pos.y()
239 else:
240 from PyQt5.QtCore import QPoint
241 pos = self.__execJavaScript(
242 "(function() {"
243 "var res = {"
244 " x: 0,"
245 " y: 0,"
246 "};"
247 "res.x = window.scrollX;"
248 "res.y = window.scrollY;"
249 "return res;"
250 "})()"
251 )
252 pos = QPoint(pos["x"], pos["y"])
253 self.__scrollBarPositions[self.__previewedPath] = pos
254 self.__hScrollBarAtEnd[self.__previewedPath] = False
255 self.__vScrollBarAtEnd[self.__previewedPath] = False
236 256
237 def __restoreScrollBarPositions(self): 257 def __restoreScrollBarPositions(self):
238 """ 258 """
239 Private method to restore scroll bar positions for a previewed editor. 259 Private method to restore scroll bar positions for a previewed editor.
240 """ 260 """
258 Qt.Horizontal, frame.scrollBarMaximum(Qt.Horizontal)) 278 Qt.Horizontal, frame.scrollBarMaximum(Qt.Horizontal))
259 279
260 if self.__vScrollBarAtEnd[self.__previewedPath]: 280 if self.__vScrollBarAtEnd[self.__previewedPath]:
261 frame.setScrollBarValue( 281 frame.setScrollBarValue(
262 Qt.Vertical, frame.scrollBarMaximum(Qt.Vertical)) 282 Qt.Vertical, frame.scrollBarMaximum(Qt.Vertical))
283 else:
284 if self.__previewedPath not in self.__scrollBarPositions:
285 return
286
287 pos = self.__scrollBarPositions[self.__previewedPath]
288 self.previewView.page().runJavaScript(
289 "window.scrollTo({0}, {1});".format(pos.x(), pos.y()))
263 290
264 @pyqtSlot(QUrl) 291 @pyqtSlot(QUrl)
265 def on_previewView_linkClicked(self, url): 292 def on_previewView_linkClicked(self, url):
266 """ 293 """
267 Private slot handling the clicking of a link. 294 Private slot handling the clicking of a link.
268 295
269 @param url url of the clicked link (QUrl) 296 @param url url of the clicked link (QUrl)
270 """ 297 """
271 e5App().getObject("UserInterface").launchHelpViewer(url.toString()) 298 e5App().getObject("UserInterface").launchHelpViewer(url.toString())
299
300 def __execJavaScript(self, script):
301 """
302 Private function to execute a JavaScript function Synchroneously.
303
304 @param script JavaScript script source to be executed
305 @type str
306 @return result of the script
307 @rtype depending upon script result
308 """
309 from PyQt5.QtCore import QEventLoop
310 loop = QEventLoop()
311 resultDict = {"res": None}
312
313 def resultCallback(res, resDict=resultDict):
314 if loop and loop.isRunning():
315 resDict["res"] = res
316 loop.quit()
317
318 self.previewView.page().runJavaScript(
319 script, resultCallback)
320
321 loop.exec_()
322 return resultDict["res"]
272 323
273 324
274 class PreviewProcessingThread(QThread): 325 class PreviewProcessingThread(QThread):
275 """ 326 """
276 Class implementing a thread to process some text into HTML usable by the 327 Class implementing a thread to process some text into HTML usable by the

eric ide

mercurial