Continued porting the web browser. QtWebEngine

Mon, 07 Mar 2016 20:08:06 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Mon, 07 Mar 2016 20:08:06 +0100
branch
QtWebEngine
changeset 4822
f1b9899847dc
parent 4821
0ce09efa00da
child 4823
cd467825a12d

Continued porting the web browser.

- added the JavaScript console

WebBrowser/WebBrowserJavaScriptConsole.py file | annotate | diff | comparison | revisions
WebBrowser/WebBrowserPage.py file | annotate | diff | comparison | revisions
WebBrowser/WebBrowserWindow.py file | annotate | diff | comparison | revisions
eric6.e4p file | annotate | diff | comparison | revisions
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WebBrowser/WebBrowserJavaScriptConsole.py	Mon Mar 07 20:08:06 2016 +0100
@@ -0,0 +1,107 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2016 Detlev Offenbach <detlev@die-offenbachs.de>
+#
+
+"""
+Module implementing a JavaScript console widget.
+"""
+
+from __future__ import unicode_literals
+
+from PyQt5.QtCore import Qt
+from PyQt5.QtGui import QTextCursor
+from PyQt5.QtWidgets import QTextEdit, QMenu
+from PyQt5.QtWebEngineWidgets import QWebEnginePage
+
+
+class WebBrowserJavaScriptConsole(QTextEdit):
+    """
+    Class implementing a JavaScript console widget.
+    """
+    def __init__(self, parent=None):
+        """
+        Constructor
+        
+        @param parent reference to the parent widget (QWidget)
+        """
+        super(WebBrowserJavaScriptConsole, self).__init__(parent)
+        self.setAcceptRichText(False)
+        self.setLineWrapMode(QTextEdit.NoWrap)
+        self.setReadOnly(True)
+        
+        # create the context menu
+        self.__menu = QMenu(self)
+        self.__menu.addAction(self.tr('Clear'), self.clear)
+        self.__menu.addAction(self.tr('Copy'), self.copy)
+        self.__menu.addSeparator()
+        self.__menu.addAction(self.tr('Select All'), self.selectAll)
+        
+        self.setContextMenuPolicy(Qt.CustomContextMenu)
+        self.customContextMenuRequested.connect(self.__handleShowContextMenu)
+        
+        self.__levelStrings = {
+            QWebEnginePage.InfoMessageLevel: self.tr("Info"),
+            QWebEnginePage.WarningMessageLevel: self.tr("Warning"),
+            QWebEnginePage.ErrorMessageLevel: self.tr("Error"),
+        }
+    
+    def __handleShowContextMenu(self, coord):
+        """
+        Private slot to show the context menu.
+        
+        @param coord the position of the mouse pointer (QPoint)
+        """
+        coord = self.mapToGlobal(coord)
+        self.__menu.popup(coord)
+    
+    def __appendText(self, txt):
+        """
+        Private method to append text to the end.
+        
+        @param txt text to insert (string)
+        """
+        tc = self.textCursor()
+        tc.movePosition(QTextCursor.End)
+        self.setTextCursor(tc)
+        self.insertPlainText(txt)
+        self.ensureCursorVisible()
+    
+    def keyPressEvent(self, evt):
+        """
+        Protected method handling key press events.
+        
+        @param evt key press event (QKeyEvent)
+        """
+        if evt.modifiers() == Qt.ControlModifier:
+            if evt.key() == Qt.Key_C:
+                self.copy()
+                evt.accept()
+                return
+            elif evt.key() == Qt.Key_A:
+                self.selectAll()
+                evt.accept()
+                return
+    
+    def javaScriptConsoleMessage(self, level, message, lineNumber,  sourceId):
+        """
+        Public method to show a console message.
+        
+        @param level severity
+        @type QWebEnginePage.JavaScriptConsoleMessageLevel
+        @param message message to be shown
+        @type str
+        @param lineNumber line number of an error
+        @type int
+        @param sourceId source URL causing the error
+        @type str
+        """
+        txt = self.tr("[{0}] {1}\n").format(
+            self.__levelStrings[level], message)
+        self.__appendText(txt)
+        
+        if lineNumber:
+            self.__appendText(self.tr("at line {0}\n").format(lineNumber))
+        
+        if sourceId:
+            self.__appendText(self.tr("URL: {0}\n").format(sourceId))
--- a/WebBrowser/WebBrowserPage.py	Mon Mar 07 19:24:20 2016 +0100
+++ b/WebBrowser/WebBrowserPage.py	Mon Mar 07 20:08:06 2016 +0100
@@ -751,6 +751,18 @@
     ## Methods below deal with JavaScript messages
     ##############################################
     
-    # TODO: JavaScript messages: do this right and add the others
     def javaScriptConsoleMessage(self, level, message, lineNumber,  sourceId):
-        print("JS-console:", message, lineNumber, sourceId)
+        """
+        Public method to show a console message.
+        
+        @param level severity
+        @type QWebEnginePage.JavaScriptConsoleMessageLevel
+        @param message message to be shown
+        @type str
+        @param lineNumber line number of an error
+        @type int
+        @param sourceId source URL causing the error
+        @type str
+        """
+        self.view().mainWindow().javascriptConsole().javaScriptConsoleMessage(
+            level, message, lineNumber,  sourceId)
--- a/WebBrowser/WebBrowserWindow.py	Mon Mar 07 19:24:20 2016 +0100
+++ b/WebBrowser/WebBrowserWindow.py	Mon Mar 07 20:08:06 2016 +0100
@@ -241,6 +241,19 @@
 ##                self.__searchDock.setWidget(self.__searchWindow)
 ##                self.addDockWidget(Qt.LeftDockWidgetArea, self.__searchDock)
 ##            
+            # JavaScript Console window
+            from .WebBrowserJavaScriptConsole import \
+                WebBrowserJavaScriptConsole
+            self.__javascriptConsole = WebBrowserJavaScriptConsole(self)
+            self.__javascriptConsoleDock = QDockWidget(
+                self.tr("JavaScript Console"))
+            self.__javascriptConsoleDock.setObjectName("JavascriptConsole")
+            self.__javascriptConsoleDock.setAllowedAreas(
+                Qt.BottomDockWidgetArea | Qt.TopDockWidgetArea)
+            self.__javascriptConsoleDock.setWidget(self.__javascriptConsole)
+            self.addDockWidget(Qt.BottomDockWidgetArea,
+                               self.__javascriptConsoleDock)
+            
             if Preferences.getWebBrowser("SaveGeometry"):
                 g = Preferences.getGeometry("WebBrowserGeometry")
             else:
@@ -1754,6 +1767,21 @@
             self.zoomValuesAct.triggered.connect(self.__showZoomValuesDialog)
         self.__actions.append(self.zoomValuesAct)
         
+        self.showJavaScriptConsoleAct = E5Action(
+            self.tr('JavaScript Console'),
+            self.tr('JavaScript Console'),
+            0, 0, self, 'webbrowser_show_javascript_console')
+        self.showJavaScriptConsoleAct.setStatusTip(self.tr(
+            'Toggle the JavaScript console window'))
+        self.showJavaScriptConsoleAct.setWhatsThis(self.tr(
+            """<b>JavaScript Console</b>"""
+            """<p>This toggles the JavaScript console window.</p>"""
+        ))
+        if not self.__initShortcutsOnly:
+            self.showJavaScriptConsoleAct.triggered.connect(
+                self.__toggleJavaScriptConsole)
+        self.__actions.append(self.showJavaScriptConsoleAct)
+        
         self.backAct.setEnabled(False)
         self.forwardAct.setEnabled(False)
         
@@ -1925,6 +1953,7 @@
         menu = mb.addMenu(self.tr("&Window"))
         menu.setTearOffEnabled(True)
         menu.addAction(self.showDownloadManagerAct)
+        menu.addAction(self.showJavaScriptConsoleAct)
 ##        if WebBrowserWindow.UseQtHelp:
 ##            menu.addSeparator()
 ##            menu.addAction(self.showTocAct)
@@ -3463,6 +3492,24 @@
         editor.setLanguage("dummy.html")
         editor.show()
     
+    def __toggleJavaScriptConsole(self):
+        """
+        Private slot to toggle the JavaScript console.
+        """
+        if self.__javascriptConsoleDock.isVisible():
+            self.__javascriptConsoleDock.hide()
+        else:
+            self.__javascriptConsoleDock.show()
+    
+    def javascriptConsole(self):
+        """
+        Public method to get a reference to the JavaScript console widget.
+        
+        @return reference to the JavaScript console
+        @rtype WebBrowserJavaScriptConsole
+        """
+        return self.__javascriptConsole
+    
     @classmethod
     def icon(cls, url):
         """
--- a/eric6.e4p	Mon Mar 07 19:24:20 2016 +0100
+++ b/eric6.e4p	Mon Mar 07 20:08:06 2016 +0100
@@ -1398,6 +1398,7 @@
     <Source>WebBrowser/VirusTotal/VirusTotalWhoisDialog.py</Source>
     <Source>WebBrowser/VirusTotal/__init__.py</Source>
     <Source>WebBrowser/WebBrowserClearPrivateDataDialog.py</Source>
+    <Source>WebBrowser/WebBrowserJavaScriptConsole.py</Source>
     <Source>WebBrowser/WebBrowserLanguagesDialog.py</Source>
     <Source>WebBrowser/WebBrowserPage.py</Source>
     <Source>WebBrowser/WebBrowserSnap.py</Source>

eric ide

mercurial