src/eric7/EricWidgets/EricErrorMessage.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9413
80c06d472826
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
8 """ 8 """
9 9
10 import contextlib 10 import contextlib
11 11
12 from PyQt6.QtCore import ( 12 from PyQt6.QtCore import (
13 qInstallMessageHandler, Qt, Q_ARG, QSettings, QtMsgType, QThread, 13 qInstallMessageHandler,
14 QMetaObject 14 Qt,
15 Q_ARG,
16 QSettings,
17 QtMsgType,
18 QThread,
19 QMetaObject,
15 ) 20 )
16 from PyQt6.QtWidgets import QErrorMessage, QDialog 21 from PyQt6.QtWidgets import QErrorMessage, QDialog
17 22
18 from EricWidgets.EricApplication import ericApp 23 from EricWidgets.EricApplication import ericApp
19 24
27 32
28 _filterSettings = QSettings( 33 _filterSettings = QSettings(
29 QSettings.Format.IniFormat, 34 QSettings.Format.IniFormat,
30 QSettings.Scope.UserScope, 35 QSettings.Scope.UserScope,
31 Globals.settingsNameOrganization, 36 Globals.settingsNameOrganization,
32 "eric7messagefilters") 37 "eric7messagefilters",
38 )
33 _defaultFilters = [ 39 _defaultFilters = [
34 "QFont::", 40 "QFont::",
35 "QCocoaMenu::removeMenuItem", 41 "QCocoaMenu::removeMenuItem",
36 "QCocoaMenu::insertNative", 42 "QCocoaMenu::insertNative",
37 ",type id:", 43 ",type id:",
46 52
47 53
48 def filterMessage(message): 54 def filterMessage(message):
49 """ 55 """
50 Module function to filter messages. 56 Module function to filter messages.
51 57
52 @param message message to be checked 58 @param message message to be checked
53 @type str 59 @type str
54 @return flag indicating that the message should be filtered out 60 @return flag indicating that the message should be filtered out
55 @rtype bool 61 @rtype bool
56 """ 62 """
57 return any( 63 return any(
58 filterStr in message 64 filterStr in message
59 for filterStr in Globals.toList(_filterSettings.value( 65 for filterStr in Globals.toList(_filterSettings.value("MessageFilters", []))
60 "MessageFilters", [])) + _defaultFilters 66 + _defaultFilters
61 ) 67 )
62 68
63 69
64 class EricErrorMessage(QErrorMessage): 70 class EricErrorMessage(QErrorMessage):
65 """ 71 """
66 Class implementing a specialized error message dialog. 72 Class implementing a specialized error message dialog.
67 """ 73 """
74
68 def __init__(self, parent=None): 75 def __init__(self, parent=None):
69 """ 76 """
70 Constructor 77 Constructor
71 78
72 @param parent reference to the parent widget 79 @param parent reference to the parent widget
73 @type QWidget 80 @type QWidget
74 """ 81 """
75 super().__init__(parent) 82 super().__init__(parent)
76 83
77 def showMessage(self, message, msgType=""): 84 def showMessage(self, message, msgType=""):
78 """ 85 """
79 Public method to show a message. 86 Public method to show a message.
80 87
81 @param message error message to be shown 88 @param message error message to be shown
82 @type str 89 @type str
83 @param msgType type of the error message 90 @param msgType type of the error message
84 @type str 91 @type str
85 """ 92 """
86 if not filterMessage(message): 93 if not filterMessage(message):
87 if msgType: 94 if msgType:
88 super().showMessage(message, msgType) 95 super().showMessage(message, msgType)
89 else: 96 else:
90 super().showMessage(message) 97 super().showMessage(message)
91 98
92 def editMessageFilters(self): 99 def editMessageFilters(self):
93 """ 100 """
94 Public method to edit the list of message filters. 101 Public method to edit the list of message filters.
95 """ 102 """
96 from .EricErrorMessageFilterDialog import EricErrorMessageFilterDialog 103 from .EricErrorMessageFilterDialog import EricErrorMessageFilterDialog
104
97 dlg = EricErrorMessageFilterDialog( 105 dlg = EricErrorMessageFilterDialog(
98 Globals.toList(_filterSettings.value( 106 Globals.toList(_filterSettings.value("MessageFilters", []))
99 "MessageFilters", []))) 107 )
100 if dlg.exec() == QDialog.DialogCode.Accepted: 108 if dlg.exec() == QDialog.DialogCode.Accepted:
101 filters = dlg.getFilters() 109 filters = dlg.getFilters()
102 _filterSettings.setValue("MessageFilters", filters) 110 _filterSettings.setValue("MessageFilters", filters)
103 111
104 112
105 def messageHandler(msgType, context, message): 113 def messageHandler(msgType, context, message):
106 """ 114 """
107 Module function handling messages. 115 Module function handling messages.
108 116
109 @param msgType type of the message 117 @param msgType type of the message
110 @type int, QtMsgType 118 @type int, QtMsgType
111 @param context context information 119 @param context context information
112 @type QMessageLogContext 120 @type QMessageLogContext
113 @param message message to be shown 121 @param message message to be shown
116 if _msgHandlerDialog: 124 if _msgHandlerDialog:
117 if msgType.value < Preferences.getUI("MinimumMessageTypeSeverity"): 125 if msgType.value < Preferences.getUI("MinimumMessageTypeSeverity"):
118 # severity is lower than configured 126 # severity is lower than configured
119 # just ignore the message 127 # just ignore the message
120 return 128 return
121 129
122 with contextlib.suppress(RuntimeError): 130 with contextlib.suppress(RuntimeError):
123 if msgType == QtMsgType.QtDebugMsg: 131 if msgType == QtMsgType.QtDebugMsg:
124 messageType = "Debug Message:" 132 messageType = "Debug Message:"
125 elif msgType == QtMsgType.QtInfoMsg: 133 elif msgType == QtMsgType.QtInfoMsg:
126 messageType = "Info Message:" 134 messageType = "Info Message:"
141 ) 149 )
142 msg = ( 150 msg = (
143 ( 151 (
144 "<p><b>{0}</b></p><p>{1}</p><p>File: {2}</p>" 152 "<p><b>{0}</b></p><p>{1}</p><p>File: {2}</p>"
145 "<p>Line: {3}</p><p>Function: {4}</p>" 153 "<p>Line: {3}</p><p>Function: {4}</p>"
146 ).format(messageType, Utilities.html_uencode(message), 154 ).format(
147 context.file, context.line, context.function) 155 messageType,
148 if context.file is not None else 156 Utilities.html_uencode(message),
149 "<p><b>{0}</b></p><p>{1}</p>".format( 157 context.file,
150 messageType, Utilities.html_uencode(message)) 158 context.line,
159 context.function,
160 )
161 if context.file is not None
162 else "<p><b>{0}</b></p><p>{1}</p>".format(
163 messageType, Utilities.html_uencode(message)
164 )
151 ) 165 )
152 if QThread.currentThread() == ericApp().thread(): 166 if QThread.currentThread() == ericApp().thread():
153 _msgHandlerDialog.showMessage(msg) 167 _msgHandlerDialog.showMessage(msg)
154 else: 168 else:
155 QMetaObject.invokeMethod( 169 QMetaObject.invokeMethod(
156 _msgHandlerDialog, 170 _msgHandlerDialog,
157 "showMessage", 171 "showMessage",
158 Qt.ConnectionType.QueuedConnection, 172 Qt.ConnectionType.QueuedConnection,
159 Q_ARG(str, msg)) 173 Q_ARG(str, msg),
174 )
160 return 175 return
161 elif _origMsgHandler: 176 elif _origMsgHandler:
162 _origMsgHandler(msgType, message) 177 _origMsgHandler(msgType, message)
163 return 178 return
164 179
165 if msgType == QtMsgType.QtDebugMsg: 180 if msgType == QtMsgType.QtDebugMsg:
166 messageType = "Debug Message" 181 messageType = "Debug Message"
167 elif msgType == QtMsgType.QtInfoMsg: 182 elif msgType == QtMsgType.QtInfoMsg:
168 messageType = "Info Message:" 183 messageType = "Info Message:"
169 elif msgType == QtMsgType.QtWarningMsg: 184 elif msgType == QtMsgType.QtWarningMsg:
172 messageType = "Critical" 187 messageType = "Critical"
173 elif msgType == QtMsgType.QtFatalMsg: 188 elif msgType == QtMsgType.QtFatalMsg:
174 messageType = "Fatal Error" 189 messageType = "Fatal Error"
175 if isinstance(message, bytes): 190 if isinstance(message, bytes):
176 message = message.decode() 191 message = message.decode()
177 print("{0}: {1} in {2} at line {3} ({4})".format( 192 print(
178 messageType, message, context.file, context.line, 193 "{0}: {1} in {2} at line {3} ({4})".format(
179 context.function)) 194 messageType, message, context.file, context.line, context.function
195 )
196 )
180 197
181 198
182 def qtHandler(): 199 def qtHandler():
183 """ 200 """
184 Module function to install an EricErrorMessage dialog as the global 201 Module function to install an EricErrorMessage dialog as the global
185 message handler. 202 message handler.
186 203
187 @return reference to the message handler dialog 204 @return reference to the message handler dialog
188 @rtype EricErrorMessage 205 @rtype EricErrorMessage
189 """ 206 """
190 global _msgHandlerDialog, _origMsgHandler 207 global _msgHandlerDialog, _origMsgHandler
191 208
192 if _msgHandlerDialog is None: 209 if _msgHandlerDialog is None:
193 # Install an EricErrorMessage dialog as the global message handler. 210 # Install an EricErrorMessage dialog as the global message handler.
194 _msgHandlerDialog = EricErrorMessage() 211 _msgHandlerDialog = EricErrorMessage()
195 _origMsgHandler = qInstallMessageHandler(messageHandler) 212 _origMsgHandler = qInstallMessageHandler(messageHandler)
196 213
197 return _msgHandlerDialog 214 return _msgHandlerDialog
198 215
199 216
200 def editMessageFilters(): 217 def editMessageFilters():
201 """ 218 """
208 225
209 226
210 def messageHandlerInstalled(): 227 def messageHandlerInstalled():
211 """ 228 """
212 Module function to check, if a message handler was installed. 229 Module function to check, if a message handler was installed.
213 230
214 @return flag indicating an installed message handler 231 @return flag indicating an installed message handler
215 @rtype bool 232 @rtype bool
216 """ 233 """
217 return _msgHandlerDialog is not None 234 return _msgHandlerDialog is not None
218 235
236
219 # 237 #
220 # eflag: noqa = M801 238 # eflag: noqa = M801

eric ide

mercurial