--- a/eric6/WebBrowser/WebBrowserView.py Mon Sep 09 18:52:08 2019 +0200 +++ b/eric6/WebBrowser/WebBrowserView.py Thu Oct 03 11:12:10 2019 +0200 @@ -8,22 +8,20 @@ Module implementing the web browser using QWebEngineView. """ -from __future__ import unicode_literals -try: - str = unicode # __IGNORE_EXCEPTION__ -except NameError: - pass import os -from PyQt5.QtCore import pyqtSignal, pyqtSlot, PYQT_VERSION, Qt, QUrl, \ - QFileInfo, QTimer, QEvent, QPoint, QPointF, QDateTime, QStandardPaths, \ - QByteArray, QIODevice, QDataStream -from PyQt5.QtGui import QDesktopServices, QClipboard, QIcon, \ - QContextMenuEvent, QPixmap, QCursor +from PyQt5.QtCore import ( + pyqtSignal, pyqtSlot, Qt, QUrl, QFileInfo, QTimer, QEvent, QPoint, + QPointF, QDateTime, QStandardPaths, QByteArray, QIODevice, QDataStream +) +from PyQt5.QtGui import ( + QDesktopServices, QClipboard, QIcon, QContextMenuEvent, QPixmap, QCursor +) from PyQt5.QtWidgets import qApp, QStyle, QMenu, QApplication, QDialog -from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage, \ - QWebEngineDownloadItem +from PyQt5.QtWebEngineWidgets import ( + QWebEngineView, QWebEnginePage, QWebEngineDownloadItem +) from E5Gui import E5MessageBox, E5FileDialog @@ -133,11 +131,10 @@ self.__restoreData = None - if qVersionTuple() >= (5, 8, 0): - if self.parentWidget() is not None: - self.parentWidget().installEventFilter(self) + if self.parentWidget() is not None: + self.parentWidget().installEventFilter(self) - if qVersionTuple() >= (5, 8, 0) and qVersionTuple() < (5, 11, 0): + if qVersionTuple() < (5, 11, 0): lay = self.layout() lay.currentChanged.connect( lambda: QTimer.singleShot(0, self.__setRwhvqt)) @@ -214,9 +211,11 @@ @param url URL to be loaded @type QUrl """ - if self.__page is not None and \ + if ( + self.__page is not None and not self.__page.acceptNavigationRequest( - url, QWebEnginePage.NavigationTypeTyped, True): + url, QWebEnginePage.NavigationTypeTyped, True) + ): return super(WebBrowserView, self).load(url) @@ -250,8 +249,10 @@ else: name.setUrl("file://" + name.toString()) - if len(name.scheme()) == 1 or \ - name.scheme() == "file": + if ( + len(name.scheme()) == 1 or + name.scheme() == "file" + ): # name is a local file if name.scheme() and len(name.scheme()) == 1: # it is a local path on win os @@ -557,8 +558,10 @@ self.__menu.addSeparator() self.__menu.addAction(self.__mw.adBlockIcon().menuAction()) - if qVersionTuple() >= (5, 11, 0) or \ - Preferences.getWebBrowser("WebInspectorEnabled"): + if ( + qVersionTuple() >= (5, 11, 0) or + Preferences.getWebBrowser("WebInspectorEnabled") + ): self.__menu.addSeparator() self.__menu.addAction( UI.PixmapCache.getIcon("webInspector.png"), @@ -578,29 +581,29 @@ @type WebHitTestResult """ spellCheckActionCount = 0 - if qVersionTuple() >= (5, 7, 0) and PYQT_VERSION >= 0x50700: - contextMenuData = self.page().contextMenuData() - hitTest.updateWithContextMenuData(contextMenuData) + contextMenuData = self.page().contextMenuData() + hitTest.updateWithContextMenuData(contextMenuData) + + if bool(contextMenuData.misspelledWord()): + boldFont = menu.font() + boldFont.setBold(True) - if qVersionTuple() >= (5, 8, 0) and PYQT_VERSION >= 0x50800 and \ - bool(contextMenuData.misspelledWord()): - boldFont = menu.font() - boldFont.setBold(True) - - for suggestion in contextMenuData.spellCheckerSuggestions(): - act = menu.addAction(suggestion) - act.setFont(boldFont) - act.triggered.connect( - lambda: self.__replaceMisspelledWord)(act) - - if not bool(menu.actions()): - menu.addAction(self.tr("No suggestions")).setEnabled(False) - - menu.addSeparator() - spellCheckActionCount = len(menu.actions()) + for suggestion in contextMenuData.spellCheckerSuggestions(): + act = menu.addAction(suggestion) + act.setFont(boldFont) + act.triggered.connect( + lambda: self.__replaceMisspelledWord)(act) + + if not bool(menu.actions()): + menu.addAction(self.tr("No suggestions")).setEnabled(False) + + menu.addSeparator() + spellCheckActionCount = len(menu.actions()) - if not hitTest.linkUrl().isEmpty() and \ - hitTest.linkUrl().scheme() != "javascript": + if ( + not hitTest.linkUrl().isEmpty() and + hitTest.linkUrl().scheme() != "javascript" + ): self.__createLinkContextMenu(menu, hitTest) if not hitTest.imageUrl().isEmpty(): @@ -687,8 +690,10 @@ act.setData(hitTest.linkUrl()) act.triggered.connect( lambda: self.__sendLink(act)) - if Preferences.getWebBrowser("VirusTotalEnabled") and \ - Preferences.getWebBrowser("VirusTotalServiceKey") != "": + if ( + Preferences.getWebBrowser("VirusTotalEnabled") and + Preferences.getWebBrowser("VirusTotalServiceKey") != "" + ): act = menu.addAction( UI.PixmapCache.getIcon("virustotal.png"), self.tr("Scan Link with VirusTotal")) @@ -763,8 +768,10 @@ act.setData(hitTest.imageUrl().toString()) act.triggered.connect( lambda: self.__blockImage(act)) - if Preferences.getWebBrowser("VirusTotalEnabled") and \ - Preferences.getWebBrowser("VirusTotalServiceKey") != "": + if ( + Preferences.getWebBrowser("VirusTotalEnabled") and + Preferences.getWebBrowser("VirusTotalServiceKey") != "" + ): act = menu.addAction( UI.PixmapCache.getIcon("virustotal.png"), self.tr("Scan Image with VirusTotal")) @@ -843,8 +850,9 @@ menu.addAction(self.tr("Search with '{0}'").format(engineName), self.__searchDefaultRequested) - from .OpenSearch.OpenSearchEngineAction import \ + from .OpenSearch.OpenSearchEngineAction import ( OpenSearchEngineAction + ) self.__searchMenu = menu.addMenu(self.tr("Search with...")) engineNames = self.__mw.openSearchManager().allEnginesNames() @@ -1045,10 +1053,12 @@ @param url URL to be checked (QUrl) @return flag indicating a valid URL (boolean) """ - return url.isValid() and \ - bool(url.host()) and \ - bool(url.scheme()) and \ + return ( + url.isValid() and + bool(url.host()) and + bool(url.scheme()) and "." in url.host() + ) def __replaceMisspelledWord(self, act): """ @@ -1347,9 +1357,11 @@ @param evt reference to the drop event (QDropEvent) """ super(WebBrowserView, self).dropEvent(evt) - if not evt.isAccepted() and \ - evt.source() != self and \ - evt.possibleActions() & Qt.CopyAction: + if ( + not evt.isAccepted() and + evt.source() != self and + evt.possibleActions() & Qt.CopyAction + ): url = QUrl() if len(evt.mimeData().urls()) > 0: url = evt.mimeData().urls()[0] @@ -1391,12 +1403,16 @@ accepted = evt.isAccepted() self.__page.event(evt) - if not evt.isAccepted() and \ - self.__mw.eventMouseButtons() & Qt.MidButton: + if ( + not evt.isAccepted() and + self.__mw.eventMouseButtons() & Qt.MidButton + ): url = QUrl(QApplication.clipboard().text(QClipboard.Selection)) - if not url.isEmpty() and \ - url.isValid() and \ - url.scheme() != "": + if ( + not url.isEmpty() and + url.isValid() and + url.scheme() != "" + ): self.__mw.setEventMouseButtons(Qt.NoButton) self.__mw.setEventKeyboardModifiers(Qt.NoModifier) self.setSource(url) @@ -1521,29 +1537,25 @@ @return flag indicating that the event should be filtered out @rtype bool """ - if obj is self and evt.type() == QEvent.ParentChange and \ - self.parentWidget() is not None: + if ( + obj is self and + evt.type() == QEvent.ParentChange and + self.parentWidget() is not None + ): self.parentWidget().installEventFilter(self) # find the render widget receiving events for the web page if obj is self and evt.type() == QEvent.ChildAdded: - if qVersionTuple() < (5, 8, 0): - child = evt.child() - if child and child.inherits( - "QtWebEngineCore::" - "RenderWidgetHostViewQtDelegateWidget"): - self.__rwhvqt = child - self.grabGesture(Qt.PinchGesture) - self.__rwhvqt.grabGesture(Qt.PinchGesture) - self.__rwhvqt.installEventFilter(self) - elif qVersionTuple() >= (5, 11, 0): + if qVersionTuple() >= (5, 11, 0): QTimer.singleShot(0, self.__setRwhvqt) # forward events to WebBrowserView - if obj is self.__rwhvqt and \ - evt.type() in [QEvent.KeyPress, QEvent.KeyRelease, - QEvent.MouseButtonPress, QEvent.MouseButtonRelease, - QEvent.MouseMove, QEvent.Wheel, QEvent.Gesture]: + if ( + obj is self.__rwhvqt and + evt.type() in [QEvent.KeyPress, QEvent.KeyRelease, + QEvent.MouseButtonPress, QEvent.MouseButtonRelease, + QEvent.MouseMove, QEvent.Wheel, QEvent.Gesture] + ): wasAccepted = evt.isAccepted() evt.setAccepted(False) if evt.type() == QEvent.KeyPress: @@ -1564,8 +1576,10 @@ evt.setAccepted(wasAccepted) return ret - if obj is self.parentWidget() and \ - evt.type() in [QEvent.KeyPress, QEvent.KeyRelease]: + if ( + obj is self.parentWidget() and + evt.type() in [QEvent.KeyPress, QEvent.KeyRelease] + ): wasAccepted = evt.isAccepted() evt.setAccepted(False) if evt.type() == QEvent.KeyPress: @@ -1861,13 +1875,9 @@ if url.isEmpty(): return - if qVersionTuple() >= (5, 8, 0) and PYQT_VERSION >= 0x50800: - # since Qt 5.8.0 - fileName, savePageFormat = self.__getSavePageFileNameAndFormat() - if fileName: - self.page().save(fileName, savePageFormat) - else: - self.triggerPageAction(QWebEnginePage.SavePage) + fileName, savePageFormat = self.__getSavePageFileNameAndFormat() + if fileName: + self.page().save(fileName, savePageFormat) def __getSavePageFileNameAndFormat(self): """ @@ -2244,7 +2254,7 @@ @type QWebEngineQuotaRequest """ acceptRequest = Preferences.getWebBrowser("AcceptQuotaRequest") - # yes/no/ask (0, 1, 2) + # map yes/no/ask from (0, 1, 2) if acceptRequest == 0: # always yes ok = True @@ -2290,8 +2300,9 @@ clientCertificateSelection.select(certificates[0]) else: certificate = None - from E5Network.E5SslCertificateSelectionDialog import \ + from E5Network.E5SslCertificateSelectionDialog import ( E5SslCertificateSelectionDialog + ) dlg = E5SslCertificateSelectionDialog(certificates, self) if dlg.exec_() == QDialog.Accepted: certificate = dlg.getSelectedCertificate()