MqttMonitor/MqttMonitorWidget.py

changeset 47
185fac480ed2
parent 46
a777a37a91e7
child 48
41dd2bfee4e4
equal deleted inserted replaced
46:a777a37a91e7 47:185fac480ed2
16 16
17 import os 17 import os
18 import collections 18 import collections
19 import copy 19 import copy
20 20
21 from PyQt5.QtCore import pyqtSlot, QTimer 21 from PyQt5.QtCore import pyqtSlot, Qt, QTimer
22 from PyQt5.QtGui import QFont 22 from PyQt5.QtGui import QFont, QTextCursor, QBrush
23 from PyQt5.QtWidgets import QWidget, QDialog 23 from PyQt5.QtWidgets import QWidget, QDialog
24 24
25 from E5Gui import E5MessageBox 25 from E5Gui import E5MessageBox
26 from E5Gui.E5PathPicker import E5PathPickerModes 26 from E5Gui.E5PathPicker import E5PathPickerModes
27 27
70 self.__messagesTopicFormat.setFontWeight(QFont.Bold) 70 self.__messagesTopicFormat.setFontWeight(QFont.Bold)
71 self.__messagesQosFormat = self.messagesEdit.currentCharFormat() 71 self.__messagesQosFormat = self.messagesEdit.currentCharFormat()
72 self.__messagesQosFormat.setFontItalic(True) 72 self.__messagesQosFormat.setFontItalic(True)
73 73
74 self.__isAlternate = False 74 self.__isAlternate = False
75 self.__followLatestMessage = True
76
77 # TODO: Messages Edit improvements:
78 # 1. add check box for self.__followLatestMessage
79 # 2. add capability to search
80 # 3. add capability to save contents to a file
75 81
76 for logLevel in (MqttClient.LogDisabled, 82 for logLevel in (MqttClient.LogDisabled,
77 MqttClient.LogDebug, 83 MqttClient.LogDebug,
78 MqttClient.LogInfo, 84 MqttClient.LogInfo,
79 MqttClient.LogNotice, 85 MqttClient.LogNotice,
81 MqttClient.LogError): 87 MqttClient.LogError):
82 self.logLevelComboBox.addItem(mqttLogLevelString( 88 self.logLevelComboBox.addItem(mqttLogLevelString(
83 logLevel, isMqttLogLevel=False), logLevel) 89 logLevel, isMqttLogLevel=False), logLevel)
84 self.logLevelComboBox.setCurrentIndex( 90 self.logLevelComboBox.setCurrentIndex(
85 self.logLevelComboBox.count() - 1) 91 self.logLevelComboBox.count() - 1)
92
93 # TODO: Log Edit improvements:
94 # 1. add check box for self.__followLatestLogMessage
95 # 2. add capability to search
96 # 3. add configuration capability for colors
97
98 self.__logMessagesBackgrounds = {
99 MqttClient.LogDebug: QBrush(Qt.white),
100 MqttClient.LogInfo: QBrush(Qt.lightGray),
101 MqttClient.LogNotice: QBrush(Qt.green),
102 MqttClient.LogWarning: QBrush(Qt.yellow),
103 MqttClient.LogError: QBrush(Qt.red),
104 MqttClient.LogDisabled: QBrush(Qt.magenta)
105 # reuse LogDisabled for unknown log levels
106 }
107 self.__followLatestLogMessage = True
86 108
87 self.brokerWidget.setCurrentIndex(0) 109 self.brokerWidget.setCurrentIndex(0)
88 110
89 self.__connectionModeProfile = True 111 self.__connectionModeProfile = True
90 self.__setConnectionMode(True) # initial mode is 'profile connection' 112 self.__setConnectionMode(True) # initial mode is 'profile connection'
268 return 290 return
269 except KeyError: 291 except KeyError:
270 # always show unknown log levels 292 # always show unknown log levels
271 pass 293 pass
272 294
295 textCursor = self.logEdit.textCursor()
296 if not self.logEdit.document().isEmpty():
297 textCursor.movePosition(QTextCursor.End)
298 self.logEdit.setTextCursor(textCursor)
299 self.logEdit.insertPlainText("\n")
300
301 textBlockFormat = textCursor.blockFormat()
302 try:
303 textBlockFormat.setBackground(
304 self.__logMessagesBackgrounds[MqttClient.LogLevelMap[level]])
305 except KeyError:
306 textBlockFormat.setBackground(
307 self.__logMessagesBackgrounds[MqttClient.LogDisabled])
308 textCursor.setBlockFormat(textBlockFormat)
309 textCursor.movePosition(QTextCursor.End)
310 self.logEdit.setTextCursor(textCursor)
311
273 txt = self.tr("{0}: {1}").format(mqttLogLevelString(level), message) 312 txt = self.tr("{0}: {1}").format(mqttLogLevelString(level), message)
274 313 self.logEdit.insertPlainText(Utilities.filterAnsiSequences(txt))
275 self.logEdit.appendPlainText(Utilities.filterAnsiSequences(txt)) 314 if self.__followLatestLogMessage:
276 self.logEdit.ensureCursorVisible() 315 self.logEdit.ensureCursorVisible()
277 316
278 @pyqtSlot(str, bytes, int, bool) 317 @pyqtSlot(str, bytes, int, bool)
279 def __messageReceived(self, topic, payload, qos, retain): 318 def __messageReceived(self, topic, payload, qos, retain):
280 """ 319 """
281 Private slot to handle the receipt of a message. 320 Private slot to handle the receipt of a message.
708 @param payload payload of the received message 747 @param payload payload of the received message
709 @type bytes 748 @type bytes
710 @param qos quality of service indicator (0, 1, 2) 749 @param qos quality of service indicator (0, 1, 2)
711 @type int 750 @type int
712 """ 751 """
713 if self.messagesEdit.blockCount() != 1: 752 textCursor = self.messagesEdit.textCursor()
714 # empty document has block count of 1 753 if not self.messagesEdit.document().isEmpty():
754 textCursor.movePosition(QTextCursor.End)
755 self.messagesEdit.setTextCursor(textCursor)
715 self.messagesEdit.insertPlainText("\n") 756 self.messagesEdit.insertPlainText("\n")
716 757
717 payloadStr = str(payload, encoding="utf-8", errors="replace")
718
719 textCursor = self.messagesEdit.textCursor()
720 textBlockFormat = textCursor.blockFormat() 758 textBlockFormat = textCursor.blockFormat()
721 if self.__isAlternate: 759 if self.__isAlternate:
722 textBlockFormat.setBackground( 760 textBlockFormat.setBackground(
723 self.messagesEdit.palette().alternateBase()) 761 self.messagesEdit.palette().alternateBase())
724 else: 762 else:
725 textBlockFormat.setBackground( 763 textBlockFormat.setBackground(
726 self.messagesEdit.palette().base()) 764 self.messagesEdit.palette().base())
727 textCursor.setBlockFormat(textBlockFormat) 765 textCursor.setBlockFormat(textBlockFormat)
766 textCursor.movePosition(QTextCursor.End)
728 self.messagesEdit.setTextCursor(textCursor) 767 self.messagesEdit.setTextCursor(textCursor)
729 768
730 self.messagesEdit.setCurrentCharFormat(self.__messagesTopicFormat) 769 self.messagesEdit.setCurrentCharFormat(self.__messagesTopicFormat)
731 self.messagesEdit.insertPlainText(topic + "\n") 770 self.messagesEdit.insertPlainText(topic + "\n")
771
732 self.messagesEdit.setCurrentCharFormat(self.__messagesQosFormat) 772 self.messagesEdit.setCurrentCharFormat(self.__messagesQosFormat)
733 self.messagesEdit.insertPlainText(self.tr("QoS: {0}\n").format(qos)) 773 self.messagesEdit.insertPlainText(self.tr("QoS: {0}\n").format(qos))
774
775 payloadStr = str(payload, encoding="utf-8", errors="replace")
734 self.messagesEdit.setCurrentCharFormat(self.__messagesFormat) 776 self.messagesEdit.setCurrentCharFormat(self.__messagesFormat)
735 self.messagesEdit.insertPlainText( 777 self.messagesEdit.insertPlainText(
736 Utilities.filterAnsiSequences(payloadStr)) 778 Utilities.filterAnsiSequences(payloadStr))
737 self.messagesEdit.ensureCursorVisible() 779
780 if self.__followLatestMessage:
781 self.messagesEdit.ensureCursorVisible()
738 782
739 self.__isAlternate = not self.__isAlternate 783 self.__isAlternate = not self.__isAlternate
740 784
741 def __handleBrokerStatusMessage(self, topic, payload): 785 def __handleBrokerStatusMessage(self, topic, payload):
742 """ 786 """

eric ide

mercurial