12 try: |
12 try: |
13 str = unicode # __IGNORE_EXCEPTION__ |
13 str = unicode # __IGNORE_EXCEPTION__ |
14 except NameError: |
14 except NameError: |
15 pass |
15 pass |
16 |
16 |
17 from PyQt5.QtCore import pyqtSlot, QUrl, QTimer, QEventLoop, QPoint |
17 from PyQt5.QtCore import pyqtSlot, QUrl, QTimer, QEventLoop, QPoint, QPointF, \ |
|
18 QByteArray, QDataStream, QIODevice |
18 from PyQt5.QtGui import QDesktopServices |
19 from PyQt5.QtGui import QDesktopServices |
19 from PyQt5.QtWebEngineWidgets import QWebEnginePage, QWebEngineSettings, \ |
20 from PyQt5.QtWebEngineWidgets import QWebEnginePage, QWebEngineSettings, \ |
20 QWebEngineScript |
21 QWebEngineScript |
21 from PyQt5.QtWebChannel import QWebChannel |
22 from PyQt5.QtWebChannel import QWebChannel |
22 |
23 |
251 "window.scrollTo(window.scrollX + {0}, window.scrollY + {1})" |
252 "window.scrollTo(window.scrollX + {0}, window.scrollY + {1})" |
252 .format(x, y), |
253 .format(x, y), |
253 WebBrowserPage.SafeJsWorld |
254 WebBrowserPage.SafeJsWorld |
254 ) |
255 ) |
255 |
256 |
|
257 def scrollTo(self, pos): |
|
258 """ |
|
259 Public method to scroll to the given position. |
|
260 |
|
261 @param pos position to scroll to |
|
262 @type QPointF |
|
263 """ |
|
264 self.runJavaScript( |
|
265 "window.scrollTo({0}, {1});".format(pos.x(), pos.y()), |
|
266 WebBrowserPage.SafeJsWorld |
|
267 ) |
|
268 |
256 def mapToViewport(self, pos): |
269 def mapToViewport(self, pos): |
257 """ |
270 """ |
258 Public method to map a position to the viewport. |
271 Public method to map a position to the viewport. |
259 |
272 |
260 @param pos position to be mapped |
273 @param pos position to be mapped |
359 @param sourceId source URL causing the error |
372 @param sourceId source URL causing the error |
360 @type str |
373 @type str |
361 """ |
374 """ |
362 self.view().mainWindow().javascriptConsole().javaScriptConsoleMessage( |
375 self.view().mainWindow().javascriptConsole().javaScriptConsoleMessage( |
363 level, message, lineNumber, sourceId) |
376 level, message, lineNumber, sourceId) |
|
377 |
|
378 #################################################### |
|
379 ## Methods below implement session related functions |
|
380 #################################################### |
|
381 |
|
382 def getSessionData(self): |
|
383 """ |
|
384 Public method to populate the session data. |
|
385 |
|
386 @return dictionary containing the session data |
|
387 @rtype dict |
|
388 """ |
|
389 sessionData = {} |
|
390 |
|
391 # 1. zoom factor |
|
392 sessionData["ZoomFactor"] = self.zoomFactor() |
|
393 |
|
394 # 2. scroll position |
|
395 scrollPos = self.scrollPosition() |
|
396 sessionData["ScrollPosition"] = { |
|
397 "x": scrollPos.x(), |
|
398 "y": scrollPos.y(), |
|
399 } |
|
400 |
|
401 # 3. page history |
|
402 historyArray = QByteArray() |
|
403 stream = QDataStream(historyArray, QIODevice.WriteOnly) |
|
404 stream << self.history() |
|
405 sessionData["History"] = str( |
|
406 historyArray.toBase64(QByteArray.Base64UrlEncoding), |
|
407 encoding="ascii") |
|
408 |
|
409 return sessionData |
364 |
410 |
365 ################################################## |
411 ################################################## |
366 ## Methods below implement compatibility functions |
412 ## Methods below implement compatibility functions |
367 ################################################## |
413 ################################################## |
368 |
414 |
373 |
419 |
374 @return web site icon |
420 @return web site icon |
375 @rtype QIcon |
421 @rtype QIcon |
376 """ |
422 """ |
377 return self.view().icon() |
423 return self.view().icon() |
|
424 |
|
425 if not hasattr(QWebEnginePage, "scrollPosition"): |
|
426 def scrollPosition(self): |
|
427 """ |
|
428 Public method to get the scroll position of the web page. |
|
429 |
|
430 @return scroll position |
|
431 @rtype QPointF |
|
432 """ |
|
433 pos = self.execJavaScript( |
|
434 "(function() {" |
|
435 "var res = {" |
|
436 " x: 0," |
|
437 " y: 0," |
|
438 "};" |
|
439 "res.x = window.scrollX;" |
|
440 "res.y = window.scrollY;" |
|
441 "return res;" |
|
442 "})()", |
|
443 WebBrowserPage.SafeJsWorld |
|
444 ) |
|
445 if pos is not None: |
|
446 pos = QPointF(pos["x"], pos["y"]) |
|
447 else: |
|
448 pos = QPointF(0.0, 0.0) |
|
449 |
|
450 return pos |