MqttMonitor/MqttMonitorWidget.py

changeset 47
185fac480ed2
parent 46
a777a37a91e7
child 48
41dd2bfee4e4
--- a/MqttMonitor/MqttMonitorWidget.py	Tue Sep 11 19:30:00 2018 +0200
+++ b/MqttMonitor/MqttMonitorWidget.py	Wed Sep 12 19:36:33 2018 +0200
@@ -18,8 +18,8 @@
 import collections
 import copy
 
-from PyQt5.QtCore import pyqtSlot, QTimer
-from PyQt5.QtGui import QFont
+from PyQt5.QtCore import pyqtSlot, Qt, QTimer
+from PyQt5.QtGui import QFont, QTextCursor, QBrush
 from PyQt5.QtWidgets import QWidget, QDialog
 
 from E5Gui import E5MessageBox
@@ -72,6 +72,12 @@
         self.__messagesQosFormat.setFontItalic(True)
         
         self.__isAlternate = False
+        self.__followLatestMessage = True
+        
+        # TODO: Messages Edit improvements:
+        #   1. add check box for self.__followLatestMessage
+        #   2. add capability to search
+        #   3. add capability to save contents to a file
         
         for logLevel in (MqttClient.LogDisabled,
                          MqttClient.LogDebug,
@@ -84,6 +90,22 @@
         self.logLevelComboBox.setCurrentIndex(
             self.logLevelComboBox.count() - 1)
         
+        # TODO: Log Edit improvements:
+        #   1. add check box for self.__followLatestLogMessage
+        #   2. add capability to search
+        #   3. add configuration capability for colors
+        
+        self.__logMessagesBackgrounds = {
+            MqttClient.LogDebug: QBrush(Qt.white),
+            MqttClient.LogInfo: QBrush(Qt.lightGray),
+            MqttClient.LogNotice: QBrush(Qt.green),
+            MqttClient.LogWarning: QBrush(Qt.yellow),
+            MqttClient.LogError: QBrush(Qt.red),
+            MqttClient.LogDisabled: QBrush(Qt.magenta)
+            # reuse LogDisabled for unknown log levels
+        }
+        self.__followLatestLogMessage = True
+        
         self.brokerWidget.setCurrentIndex(0)
         
         self.__connectionModeProfile = True
@@ -270,10 +292,27 @@
             # always show unknown log levels
             pass
         
-        txt = self.tr("{0}: {1}").format(mqttLogLevelString(level), message)
+        textCursor = self.logEdit.textCursor()
+        if not self.logEdit.document().isEmpty():
+            textCursor.movePosition(QTextCursor.End)
+            self.logEdit.setTextCursor(textCursor)
+            self.logEdit.insertPlainText("\n")
         
-        self.logEdit.appendPlainText(Utilities.filterAnsiSequences(txt))
-        self.logEdit.ensureCursorVisible()
+        textBlockFormat = textCursor.blockFormat()
+        try:
+            textBlockFormat.setBackground(
+                self.__logMessagesBackgrounds[MqttClient.LogLevelMap[level]])
+        except KeyError:
+            textBlockFormat.setBackground(
+                self.__logMessagesBackgrounds[MqttClient.LogDisabled])
+        textCursor.setBlockFormat(textBlockFormat)
+        textCursor.movePosition(QTextCursor.End)
+        self.logEdit.setTextCursor(textCursor)
+        
+        txt = self.tr("{0}: {1}").format(mqttLogLevelString(level), message)
+        self.logEdit.insertPlainText(Utilities.filterAnsiSequences(txt))
+        if self.__followLatestLogMessage:
+            self.logEdit.ensureCursorVisible()
     
     @pyqtSlot(str, bytes, int, bool)
     def __messageReceived(self, topic, payload, qos, retain):
@@ -710,13 +749,12 @@
         @param qos quality of service indicator (0, 1, 2)
         @type int
         """
-        if self.messagesEdit.blockCount() != 1:
-            # empty document has block count of 1
+        textCursor = self.messagesEdit.textCursor()
+        if not self.messagesEdit.document().isEmpty():
+            textCursor.movePosition(QTextCursor.End)
+            self.messagesEdit.setTextCursor(textCursor)
             self.messagesEdit.insertPlainText("\n")
         
-        payloadStr = str(payload, encoding="utf-8", errors="replace")
-        
-        textCursor = self.messagesEdit.textCursor()
         textBlockFormat = textCursor.blockFormat()
         if self.__isAlternate:
             textBlockFormat.setBackground(
@@ -725,16 +763,22 @@
             textBlockFormat.setBackground(
                 self.messagesEdit.palette().base())
         textCursor.setBlockFormat(textBlockFormat)
+        textCursor.movePosition(QTextCursor.End)
         self.messagesEdit.setTextCursor(textCursor)
         
         self.messagesEdit.setCurrentCharFormat(self.__messagesTopicFormat)
         self.messagesEdit.insertPlainText(topic + "\n")
+        
         self.messagesEdit.setCurrentCharFormat(self.__messagesQosFormat)
         self.messagesEdit.insertPlainText(self.tr("QoS: {0}\n").format(qos))
+        
+        payloadStr = str(payload, encoding="utf-8", errors="replace")
         self.messagesEdit.setCurrentCharFormat(self.__messagesFormat)
         self.messagesEdit.insertPlainText(
             Utilities.filterAnsiSequences(payloadStr))
-        self.messagesEdit.ensureCursorVisible()
+        
+        if self.__followLatestMessage:
+            self.messagesEdit.ensureCursorVisible()
         
         self.__isAlternate = not self.__isAlternate
     

eric ide

mercurial