|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2016 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a JavaScript console widget. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 from PyQt5.QtCore import Qt |
|
13 from PyQt5.QtGui import QTextCursor |
|
14 from PyQt5.QtWidgets import QTextEdit, QMenu |
|
15 from PyQt5.QtWebEngineWidgets import QWebEnginePage |
|
16 |
|
17 |
|
18 class WebBrowserJavaScriptConsole(QTextEdit): |
|
19 """ |
|
20 Class implementing a JavaScript console widget. |
|
21 """ |
|
22 def __init__(self, parent=None): |
|
23 """ |
|
24 Constructor |
|
25 |
|
26 @param parent reference to the parent widget (QWidget) |
|
27 """ |
|
28 super(WebBrowserJavaScriptConsole, self).__init__(parent) |
|
29 self.setAcceptRichText(False) |
|
30 self.setLineWrapMode(QTextEdit.NoWrap) |
|
31 self.setReadOnly(True) |
|
32 |
|
33 # create the context menu |
|
34 self.__menu = QMenu(self) |
|
35 self.__menu.addAction(self.tr('Clear'), self.clear) |
|
36 self.__menu.addAction(self.tr('Copy'), self.copy) |
|
37 self.__menu.addSeparator() |
|
38 self.__menu.addAction(self.tr('Select All'), self.selectAll) |
|
39 |
|
40 self.setContextMenuPolicy(Qt.CustomContextMenu) |
|
41 self.customContextMenuRequested.connect(self.__handleShowContextMenu) |
|
42 |
|
43 self.__levelStrings = { |
|
44 QWebEnginePage.InfoMessageLevel: self.tr("Info"), |
|
45 QWebEnginePage.WarningMessageLevel: self.tr("Warning"), |
|
46 QWebEnginePage.ErrorMessageLevel: self.tr("Error"), |
|
47 } |
|
48 |
|
49 def __handleShowContextMenu(self, coord): |
|
50 """ |
|
51 Private slot to show the context menu. |
|
52 |
|
53 @param coord the position of the mouse pointer (QPoint) |
|
54 """ |
|
55 coord = self.mapToGlobal(coord) |
|
56 self.__menu.popup(coord) |
|
57 |
|
58 def __appendText(self, txt): |
|
59 """ |
|
60 Private method to append text to the end. |
|
61 |
|
62 @param txt text to insert (string) |
|
63 """ |
|
64 tc = self.textCursor() |
|
65 tc.movePosition(QTextCursor.End) |
|
66 self.setTextCursor(tc) |
|
67 self.insertPlainText(txt) |
|
68 self.ensureCursorVisible() |
|
69 |
|
70 def keyPressEvent(self, evt): |
|
71 """ |
|
72 Protected method handling key press events. |
|
73 |
|
74 @param evt key press event (QKeyEvent) |
|
75 """ |
|
76 if evt.modifiers() == Qt.ControlModifier: |
|
77 if evt.key() == Qt.Key_C: |
|
78 self.copy() |
|
79 evt.accept() |
|
80 return |
|
81 elif evt.key() == Qt.Key_A: |
|
82 self.selectAll() |
|
83 evt.accept() |
|
84 return |
|
85 |
|
86 def javaScriptConsoleMessage(self, level, message, lineNumber, sourceId): |
|
87 """ |
|
88 Public method to show a console message. |
|
89 |
|
90 @param level severity |
|
91 @type QWebEnginePage.JavaScriptConsoleMessageLevel |
|
92 @param message message to be shown |
|
93 @type str |
|
94 @param lineNumber line number of an error |
|
95 @type int |
|
96 @param sourceId source URL causing the error |
|
97 @type str |
|
98 """ |
|
99 txt = self.tr("[{0}] {1}\n").format( |
|
100 self.__levelStrings[level], message) |
|
101 self.__appendText(txt) |
|
102 |
|
103 if lineNumber: |
|
104 self.__appendText(self.tr("at line {0}\n").format(lineNumber)) |
|
105 |
|
106 if sourceId: |
|
107 self.__appendText(self.tr("URL: {0}\n").format(sourceId)) |