eric6/WebBrowser/WebBrowserView.py

changeset 7271
2cac5b7abcce
parent 7249
0bf517e60f54
child 7286
7eb04391adf7
child 7356
d975331c5daa
equal deleted inserted replaced
7270:41d09cf20415 7271:2cac5b7abcce
9 """ 9 """
10 10
11 11
12 import os 12 import os
13 13
14 from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt, QUrl, QFileInfo, QTimer, \ 14 from PyQt5.QtCore import (
15 QEvent, QPoint, QPointF, QDateTime, QStandardPaths, QByteArray, \ 15 pyqtSignal, pyqtSlot, Qt, QUrl, QFileInfo, QTimer, QEvent, QPoint,
16 QIODevice, QDataStream 16 QPointF, QDateTime, QStandardPaths, QByteArray, QIODevice, QDataStream
17 from PyQt5.QtGui import QDesktopServices, QClipboard, QIcon, \ 17 )
18 QContextMenuEvent, QPixmap, QCursor 18 from PyQt5.QtGui import (
19 QDesktopServices, QClipboard, QIcon, QContextMenuEvent, QPixmap, QCursor
20 )
19 from PyQt5.QtWidgets import qApp, QStyle, QMenu, QApplication, QDialog 21 from PyQt5.QtWidgets import qApp, QStyle, QMenu, QApplication, QDialog
20 from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage, \ 22 from PyQt5.QtWebEngineWidgets import (
21 QWebEngineDownloadItem 23 QWebEngineView, QWebEnginePage, QWebEngineDownloadItem
24 )
22 25
23 from E5Gui import E5MessageBox, E5FileDialog 26 from E5Gui import E5MessageBox, E5FileDialog
24 27
25 from WebBrowser.WebBrowserWindow import WebBrowserWindow 28 from WebBrowser.WebBrowserWindow import WebBrowserWindow
26 from .WebBrowserPage import WebBrowserPage 29 from .WebBrowserPage import WebBrowserPage
206 Public method to load a web site. 209 Public method to load a web site.
207 210
208 @param url URL to be loaded 211 @param url URL to be loaded
209 @type QUrl 212 @type QUrl
210 """ 213 """
211 if self.__page is not None and \ 214 if (
215 self.__page is not None and
212 not self.__page.acceptNavigationRequest( 216 not self.__page.acceptNavigationRequest(
213 url, QWebEnginePage.NavigationTypeTyped, True): 217 url, QWebEnginePage.NavigationTypeTyped, True)
218 ):
214 return 219 return
215 220
216 super(WebBrowserView, self).load(url) 221 super(WebBrowserView, self).load(url)
217 222
218 if not self.__firstLoad: 223 if not self.__firstLoad:
242 name.setUrl("file:///" + Utilities.fromNativeSeparators( 247 name.setUrl("file:///" + Utilities.fromNativeSeparators(
243 name.toString())) 248 name.toString()))
244 else: 249 else:
245 name.setUrl("file://" + name.toString()) 250 name.setUrl("file://" + name.toString())
246 251
247 if len(name.scheme()) == 1 or \ 252 if (
248 name.scheme() == "file": 253 len(name.scheme()) == 1 or
254 name.scheme() == "file"
255 ):
249 # name is a local file 256 # name is a local file
250 if name.scheme() and len(name.scheme()) == 1: 257 if name.scheme() and len(name.scheme()) == 1:
251 # it is a local path on win os 258 # it is a local path on win os
252 name = QUrl.fromLocalFile(name.toString()) 259 name = QUrl.fromLocalFile(name.toString())
253 260
549 556
550 if not hitTest.isContentEditable() and not hitTest.isContentSelected(): 557 if not hitTest.isContentEditable() and not hitTest.isContentSelected():
551 self.__menu.addSeparator() 558 self.__menu.addSeparator()
552 self.__menu.addAction(self.__mw.adBlockIcon().menuAction()) 559 self.__menu.addAction(self.__mw.adBlockIcon().menuAction())
553 560
554 if qVersionTuple() >= (5, 11, 0) or \ 561 if (
555 Preferences.getWebBrowser("WebInspectorEnabled"): 562 qVersionTuple() >= (5, 11, 0) or
563 Preferences.getWebBrowser("WebInspectorEnabled")
564 ):
556 self.__menu.addSeparator() 565 self.__menu.addSeparator()
557 self.__menu.addAction( 566 self.__menu.addAction(
558 UI.PixmapCache.getIcon("webInspector.png"), 567 UI.PixmapCache.getIcon("webInspector.png"),
559 self.tr("Inspect Element..."), self.__webInspector) 568 self.tr("Inspect Element..."), self.__webInspector)
560 569
589 menu.addAction(self.tr("No suggestions")).setEnabled(False) 598 menu.addAction(self.tr("No suggestions")).setEnabled(False)
590 599
591 menu.addSeparator() 600 menu.addSeparator()
592 spellCheckActionCount = len(menu.actions()) 601 spellCheckActionCount = len(menu.actions())
593 602
594 if not hitTest.linkUrl().isEmpty() and \ 603 if (
595 hitTest.linkUrl().scheme() != "javascript": 604 not hitTest.linkUrl().isEmpty() and
605 hitTest.linkUrl().scheme() != "javascript"
606 ):
596 self.__createLinkContextMenu(menu, hitTest) 607 self.__createLinkContextMenu(menu, hitTest)
597 608
598 if not hitTest.imageUrl().isEmpty(): 609 if not hitTest.imageUrl().isEmpty():
599 self.__createImageContextMenu(menu, hitTest) 610 self.__createImageContextMenu(menu, hitTest)
600 611
677 UI.PixmapCache.getIcon("mailSend.png"), 688 UI.PixmapCache.getIcon("mailSend.png"),
678 self.tr("Send Link")) 689 self.tr("Send Link"))
679 act.setData(hitTest.linkUrl()) 690 act.setData(hitTest.linkUrl())
680 act.triggered.connect( 691 act.triggered.connect(
681 lambda: self.__sendLink(act)) 692 lambda: self.__sendLink(act))
682 if Preferences.getWebBrowser("VirusTotalEnabled") and \ 693 if (
683 Preferences.getWebBrowser("VirusTotalServiceKey") != "": 694 Preferences.getWebBrowser("VirusTotalEnabled") and
695 Preferences.getWebBrowser("VirusTotalServiceKey") != ""
696 ):
684 act = menu.addAction( 697 act = menu.addAction(
685 UI.PixmapCache.getIcon("virustotal.png"), 698 UI.PixmapCache.getIcon("virustotal.png"),
686 self.tr("Scan Link with VirusTotal")) 699 self.tr("Scan Link with VirusTotal"))
687 act.setData(hitTest.linkUrl()) 700 act.setData(hitTest.linkUrl())
688 act.triggered.connect( 701 act.triggered.connect(
753 UI.PixmapCache.getIcon("adBlockPlus.png"), 766 UI.PixmapCache.getIcon("adBlockPlus.png"),
754 self.tr("Block Image")) 767 self.tr("Block Image"))
755 act.setData(hitTest.imageUrl().toString()) 768 act.setData(hitTest.imageUrl().toString())
756 act.triggered.connect( 769 act.triggered.connect(
757 lambda: self.__blockImage(act)) 770 lambda: self.__blockImage(act))
758 if Preferences.getWebBrowser("VirusTotalEnabled") and \ 771 if (
759 Preferences.getWebBrowser("VirusTotalServiceKey") != "": 772 Preferences.getWebBrowser("VirusTotalEnabled") and
773 Preferences.getWebBrowser("VirusTotalServiceKey") != ""
774 ):
760 act = menu.addAction( 775 act = menu.addAction(
761 UI.PixmapCache.getIcon("virustotal.png"), 776 UI.PixmapCache.getIcon("virustotal.png"),
762 self.tr("Scan Image with VirusTotal")) 777 self.tr("Scan Image with VirusTotal"))
763 act.setData(hitTest.imageUrl()) 778 act.setData(hitTest.imageUrl())
764 act.triggered.connect( 779 act.triggered.connect(
833 engineName = self.__mw.openSearchManager().currentEngineName() 848 engineName = self.__mw.openSearchManager().currentEngineName()
834 if engineName: 849 if engineName:
835 menu.addAction(self.tr("Search with '{0}'").format(engineName), 850 menu.addAction(self.tr("Search with '{0}'").format(engineName),
836 self.__searchDefaultRequested) 851 self.__searchDefaultRequested)
837 852
838 from .OpenSearch.OpenSearchEngineAction import \ 853 from .OpenSearch.OpenSearchEngineAction import (
839 OpenSearchEngineAction 854 OpenSearchEngineAction
855 )
840 856
841 self.__searchMenu = menu.addMenu(self.tr("Search with...")) 857 self.__searchMenu = menu.addMenu(self.tr("Search with..."))
842 engineNames = self.__mw.openSearchManager().allEnginesNames() 858 engineNames = self.__mw.openSearchManager().allEnginesNames()
843 for engineName in engineNames: 859 for engineName in engineNames:
844 engine = self.__mw.openSearchManager().engine(engineName) 860 engine = self.__mw.openSearchManager().engine(engineName)
1035 Private method to check a URL for validity. 1051 Private method to check a URL for validity.
1036 1052
1037 @param url URL to be checked (QUrl) 1053 @param url URL to be checked (QUrl)
1038 @return flag indicating a valid URL (boolean) 1054 @return flag indicating a valid URL (boolean)
1039 """ 1055 """
1040 return url.isValid() and \ 1056 return (
1041 bool(url.host()) and \ 1057 url.isValid() and
1042 bool(url.scheme()) and \ 1058 bool(url.host()) and
1059 bool(url.scheme()) and
1043 "." in url.host() 1060 "." in url.host()
1061 )
1044 1062
1045 def __replaceMisspelledWord(self, act): 1063 def __replaceMisspelledWord(self, act):
1046 """ 1064 """
1047 Private slot to replace a misspelled word under the context menu. 1065 Private slot to replace a misspelled word under the context menu.
1048 1066
1337 Protected method called by a drop event. 1355 Protected method called by a drop event.
1338 1356
1339 @param evt reference to the drop event (QDropEvent) 1357 @param evt reference to the drop event (QDropEvent)
1340 """ 1358 """
1341 super(WebBrowserView, self).dropEvent(evt) 1359 super(WebBrowserView, self).dropEvent(evt)
1342 if not evt.isAccepted() and \ 1360 if (
1343 evt.source() != self and \ 1361 not evt.isAccepted() and
1344 evt.possibleActions() & Qt.CopyAction: 1362 evt.source() != self and
1363 evt.possibleActions() & Qt.CopyAction
1364 ):
1345 url = QUrl() 1365 url = QUrl()
1346 if len(evt.mimeData().urls()) > 0: 1366 if len(evt.mimeData().urls()) > 0:
1347 url = evt.mimeData().urls()[0] 1367 url = evt.mimeData().urls()[0]
1348 if not url.isValid(): 1368 if not url.isValid():
1349 url = QUrl(evt.mimeData().text()) 1369 url = QUrl(evt.mimeData().text())
1381 evt.accept() 1401 evt.accept()
1382 return 1402 return
1383 1403
1384 accepted = evt.isAccepted() 1404 accepted = evt.isAccepted()
1385 self.__page.event(evt) 1405 self.__page.event(evt)
1386 if not evt.isAccepted() and \ 1406 if (
1387 self.__mw.eventMouseButtons() & Qt.MidButton: 1407 not evt.isAccepted() and
1408 self.__mw.eventMouseButtons() & Qt.MidButton
1409 ):
1388 url = QUrl(QApplication.clipboard().text(QClipboard.Selection)) 1410 url = QUrl(QApplication.clipboard().text(QClipboard.Selection))
1389 if not url.isEmpty() and \ 1411 if (
1390 url.isValid() and \ 1412 not url.isEmpty() and
1391 url.scheme() != "": 1413 url.isValid() and
1414 url.scheme() != ""
1415 ):
1392 self.__mw.setEventMouseButtons(Qt.NoButton) 1416 self.__mw.setEventMouseButtons(Qt.NoButton)
1393 self.__mw.setEventKeyboardModifiers(Qt.NoModifier) 1417 self.__mw.setEventKeyboardModifiers(Qt.NoModifier)
1394 self.setSource(url) 1418 self.setSource(url)
1395 evt.setAccepted(accepted) 1419 evt.setAccepted(accepted)
1396 1420
1511 @param evt reference to event to be processed 1535 @param evt reference to event to be processed
1512 @type QEvent 1536 @type QEvent
1513 @return flag indicating that the event should be filtered out 1537 @return flag indicating that the event should be filtered out
1514 @rtype bool 1538 @rtype bool
1515 """ 1539 """
1516 if obj is self and evt.type() == QEvent.ParentChange and \ 1540 if (
1517 self.parentWidget() is not None: 1541 obj is self and
1542 evt.type() == QEvent.ParentChange and
1543 self.parentWidget() is not None
1544 ):
1518 self.parentWidget().installEventFilter(self) 1545 self.parentWidget().installEventFilter(self)
1519 1546
1520 # find the render widget receiving events for the web page 1547 # find the render widget receiving events for the web page
1521 if obj is self and evt.type() == QEvent.ChildAdded: 1548 if obj is self and evt.type() == QEvent.ChildAdded:
1522 if qVersionTuple() >= (5, 11, 0): 1549 if qVersionTuple() >= (5, 11, 0):
1523 QTimer.singleShot(0, self.__setRwhvqt) 1550 QTimer.singleShot(0, self.__setRwhvqt)
1524 1551
1525 # forward events to WebBrowserView 1552 # forward events to WebBrowserView
1526 if obj is self.__rwhvqt and \ 1553 if (
1527 evt.type() in [QEvent.KeyPress, QEvent.KeyRelease, 1554 obj is self.__rwhvqt and
1528 QEvent.MouseButtonPress, QEvent.MouseButtonRelease, 1555 evt.type() in [QEvent.KeyPress, QEvent.KeyRelease,
1529 QEvent.MouseMove, QEvent.Wheel, QEvent.Gesture]: 1556 QEvent.MouseButtonPress, QEvent.MouseButtonRelease,
1557 QEvent.MouseMove, QEvent.Wheel, QEvent.Gesture]
1558 ):
1530 wasAccepted = evt.isAccepted() 1559 wasAccepted = evt.isAccepted()
1531 evt.setAccepted(False) 1560 evt.setAccepted(False)
1532 if evt.type() == QEvent.KeyPress: 1561 if evt.type() == QEvent.KeyPress:
1533 self._keyPressEvent(evt) 1562 self._keyPressEvent(evt)
1534 elif evt.type() == QEvent.KeyRelease: 1563 elif evt.type() == QEvent.KeyRelease:
1545 self._gestureEvent(evt) 1574 self._gestureEvent(evt)
1546 ret = evt.isAccepted() 1575 ret = evt.isAccepted()
1547 evt.setAccepted(wasAccepted) 1576 evt.setAccepted(wasAccepted)
1548 return ret 1577 return ret
1549 1578
1550 if obj is self.parentWidget() and \ 1579 if (
1551 evt.type() in [QEvent.KeyPress, QEvent.KeyRelease]: 1580 obj is self.parentWidget() and
1581 evt.type() in [QEvent.KeyPress, QEvent.KeyRelease]
1582 ):
1552 wasAccepted = evt.isAccepted() 1583 wasAccepted = evt.isAccepted()
1553 evt.setAccepted(False) 1584 evt.setAccepted(False)
1554 if evt.type() == QEvent.KeyPress: 1585 if evt.type() == QEvent.KeyPress:
1555 self._keyPressEvent(evt) 1586 self._keyPressEvent(evt)
1556 elif evt.type() == QEvent.KeyRelease: 1587 elif evt.type() == QEvent.KeyRelease:
2267 clientCertificateSelection.selectNone() 2298 clientCertificateSelection.selectNone()
2268 elif len(certificates) == 1: 2299 elif len(certificates) == 1:
2269 clientCertificateSelection.select(certificates[0]) 2300 clientCertificateSelection.select(certificates[0])
2270 else: 2301 else:
2271 certificate = None 2302 certificate = None
2272 from E5Network.E5SslCertificateSelectionDialog import \ 2303 from E5Network.E5SslCertificateSelectionDialog import (
2273 E5SslCertificateSelectionDialog 2304 E5SslCertificateSelectionDialog
2305 )
2274 dlg = E5SslCertificateSelectionDialog(certificates, self) 2306 dlg = E5SslCertificateSelectionDialog(certificates, self)
2275 if dlg.exec_() == QDialog.Accepted: 2307 if dlg.exec_() == QDialog.Accepted:
2276 certificate = dlg.getSelectedCertificate() 2308 certificate = dlg.getSelectedCertificate()
2277 2309
2278 if certificate is None: 2310 if certificate is None:

eric ide

mercurial