E5Gui/E5ErrorMessage.py

Sun, 02 Jun 2013 17:10:24 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 02 Jun 2013 17:10:24 +0200
changeset 2695
13c6301cbed3
parent 2683
ef93fc7332a2
child 2696
c25359355787
permissions
-rw-r--r--

Fixed an issue in the new error message window.

2683
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
3 # Copyright (c) 2013 Detlev Offenbach <detlev@die-offenbachs.de>
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing a specialized error message dialog.
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 from PyQt4.QtCore import qInstallMsgHandler, QCoreApplication, QtDebugMsg, \
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 QtWarningMsg, QtCriticalMsg, QtFatalMsg, QThread, QMetaObject, Qt, Q_ARG
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 from PyQt4.QtGui import QErrorMessage, qApp
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 import Utilities
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 __msgHandlerDialog = None
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 __origMsgHandler = None
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 class E5ErrorMessage(QErrorMessage):
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 """
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 Class implementing a specialized error message dialog.
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 """
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 def __init__(self, parent=None):
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 """
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 Constructor
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 @param parent reference to the parent widget (QWidget)
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 """
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 super().__init__(parent)
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 # TODO: make this list configurable by the user
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 self.__filters = [
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 "QFont::",
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 "QCocoaMenu::removeMenuItem",
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 "QCocoaMenu::insertNative",
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 ",type id:"
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 ]
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 def __filterMessage(self, message):
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 """
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 Private method to filter messages.
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 @param message message to be checked (string)
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 @return flag indicating that the message should be filtered out (boolean)
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 """
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 for filter in self.__filters:
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 if filter in message:
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 return True
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 return False
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 def showMessage(self, message, msgType=""):
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 """
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 Public method to show a message.
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 @param message error message to be shown (string)
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 @param msgType type of the error message (string)
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 """
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 if not self.__filterMessage(message):
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 if msgType:
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 super().showMessage(message, msgType)
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 else:
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 super().showMessage(message)
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 def messageHandler(msgType, message):
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 """
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 Module function handling messages.
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 @param msgType type of the message (integer, QtMsgType)
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 @param message message to be shown (bytes)
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 """
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 if __msgHandlerDialog:
2695
13c6301cbed3 Fixed an issue in the new error message window.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2683
diff changeset
76 try:
13c6301cbed3 Fixed an issue in the new error message window.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2683
diff changeset
77 if msgType == QtDebugMsg:
13c6301cbed3 Fixed an issue in the new error message window.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2683
diff changeset
78 messageType = QCoreApplication.translate("E5ErrorMessage", "Debug Message:")
13c6301cbed3 Fixed an issue in the new error message window.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2683
diff changeset
79 elif msgType == QtWarningMsg:
13c6301cbed3 Fixed an issue in the new error message window.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2683
diff changeset
80 messageType = QCoreApplication.translate("E5ErrorMessage", "Warning:")
13c6301cbed3 Fixed an issue in the new error message window.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2683
diff changeset
81 elif msgType == QtCriticalMsg:
13c6301cbed3 Fixed an issue in the new error message window.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2683
diff changeset
82 messageType = QCoreApplication.translate("E5ErrorMessage", "Critical:")
13c6301cbed3 Fixed an issue in the new error message window.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2683
diff changeset
83 elif msgType == QtFatalMsg:
13c6301cbed3 Fixed an issue in the new error message window.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2683
diff changeset
84 messageType = QCoreApplication.translate("E5ErrorMessage", "Fatal Error:")
13c6301cbed3 Fixed an issue in the new error message window.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2683
diff changeset
85 msg = "<p><b>{0}</b></p><p>{1}</p>".format(
13c6301cbed3 Fixed an issue in the new error message window.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2683
diff changeset
86 messageType, Utilities.html_uencode(message.decode()))
13c6301cbed3 Fixed an issue in the new error message window.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2683
diff changeset
87 if QThread.currentThread() == qApp.thread():
13c6301cbed3 Fixed an issue in the new error message window.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2683
diff changeset
88 __msgHandlerDialog.showMessage(msg)
13c6301cbed3 Fixed an issue in the new error message window.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2683
diff changeset
89 else:
13c6301cbed3 Fixed an issue in the new error message window.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2683
diff changeset
90 QMetaObject.invokeMethod(
13c6301cbed3 Fixed an issue in the new error message window.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2683
diff changeset
91 __msgHandlerDialog,
13c6301cbed3 Fixed an issue in the new error message window.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2683
diff changeset
92 "showMessage",
13c6301cbed3 Fixed an issue in the new error message window.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2683
diff changeset
93 Qt.QueuedConnection,
13c6301cbed3 Fixed an issue in the new error message window.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2683
diff changeset
94 Q_ARG(str, msg))
13c6301cbed3 Fixed an issue in the new error message window.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2683
diff changeset
95 return
13c6301cbed3 Fixed an issue in the new error message window.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2683
diff changeset
96 except RuntimeError:
13c6301cbed3 Fixed an issue in the new error message window.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2683
diff changeset
97 pass
2683
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98 elif __origMsgHandler:
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99 __origMsgHandler(msgType, message)
2695
13c6301cbed3 Fixed an issue in the new error message window.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2683
diff changeset
100 return
13c6301cbed3 Fixed an issue in the new error message window.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2683
diff changeset
101
13c6301cbed3 Fixed an issue in the new error message window.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2683
diff changeset
102 if msgType == QtDebugMsg:
13c6301cbed3 Fixed an issue in the new error message window.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2683
diff changeset
103 messageType = QCoreApplication.translate("E5ErrorMessage", "Debug Message")
13c6301cbed3 Fixed an issue in the new error message window.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2683
diff changeset
104 elif msgType == QtWarningMsg:
13c6301cbed3 Fixed an issue in the new error message window.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2683
diff changeset
105 messageType = QCoreApplication.translate("E5ErrorMessage", "Warning")
13c6301cbed3 Fixed an issue in the new error message window.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2683
diff changeset
106 elif msgType == QtCriticalMsg:
13c6301cbed3 Fixed an issue in the new error message window.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2683
diff changeset
107 messageType = QCoreApplication.translate("E5ErrorMessage", "Critical")
13c6301cbed3 Fixed an issue in the new error message window.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2683
diff changeset
108 elif msgType == QtFatalMsg:
13c6301cbed3 Fixed an issue in the new error message window.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2683
diff changeset
109 messageType = QCoreApplication.translate("E5ErrorMessage", "Fatal Error")
13c6301cbed3 Fixed an issue in the new error message window.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2683
diff changeset
110 print("{0}: {1}".format(messageType, message.decode()))
2683
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113 def qtHandler():
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114 """
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115 Module function to install an E5ErrorMessage dialog as the global
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116 message handler.
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117 """
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118 global __msgHandlerDialog, __origMsgHandler
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
119
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120 if __msgHandlerDialog is None:
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121 # Install an E5ErrorMessage dialog as the global message handler.
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122 __msgHandlerDialog = E5ErrorMessage()
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123 __origMsgHandler = qInstallMsgHandler(messageHandler)
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124
ef93fc7332a2 Added a specialized error message handler allowing to filter messages.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
125 return __msgHandlerDialog

eric ide

mercurial