--- a/MqttMonitor/MqttMonitorWidget.py Mon Sep 10 17:38:39 2018 +0200 +++ b/MqttMonitor/MqttMonitorWidget.py Mon Sep 10 19:46:58 2018 +0200 @@ -19,7 +19,7 @@ import copy from PyQt5.QtCore import pyqtSlot, QTimer -from PyQt5.QtGui import QTextCursor +from PyQt5.QtGui import QFont from PyQt5.QtWidgets import QWidget, QDialog from E5Gui import E5MessageBox @@ -34,16 +34,6 @@ import Utilities -# TODO: change messages display to use a tree widget -# first row topic -# second row qos and message -# -# include capability to filter on topic - -# TODO: change log display to a tree widget -# two columns with log level and message -# colorize entries depending on log level - class MqttMonitorWidget(QWidget, Ui_MqttMonitorWidget): """ Class implementing the MQTT Monitor widget. @@ -75,6 +65,12 @@ self.publishPayloadFilePicker.setMode(E5PathPickerModes.OpenFileMode) self.publishPayloadFilePicker.setFilters(self.tr("All Files (*)")) + self.__messagesFormat = self.messagesEdit.currentCharFormat() + self.__messagesTopicFormat = self.messagesEdit.currentCharFormat() + self.__messagesTopicFormat.setFontWeight(QFont.Bold) + self.__messagesQosFormat = self.messagesEdit.currentCharFormat() + self.__messagesQosFormat.setFontItalic(True) + for logLevel in (MqttClient.LogDisabled, MqttClient.LogDebug, MqttClient.LogInfo, @@ -273,13 +269,8 @@ pass txt = self.tr("{0}: {1}").format(mqttLogLevelString(level), message) - if not txt.endswith(("\r\n", "\r", "\n")): - txt += "\n" - tc = self.logEdit.textCursor() - tc.movePosition(QTextCursor.End) - self.logEdit.setTextCursor(tc) - self.logEdit.insertPlainText(Utilities.filterAnsiSequences(txt)) + self.logEdit.appendPlainText(Utilities.filterAnsiSequences(txt)) self.logEdit.ensureCursorVisible() @pyqtSlot(str, bytes, int, bool) @@ -300,7 +291,7 @@ # handle broker status messages self.__handleBrokerStatusMessage(topic, payload) else: - self.__appendMessage(topic, payload) + self.__appendMessage(topic, payload, qos) @pyqtSlot(int) def __messagePublished(self, mid): @@ -586,6 +577,10 @@ # successfully sent self.__setBrokerStatusSubscribed(True) + ####################################################################### + ## Utility methods + ####################################################################### + def __setBrokerStatusSubscribed(self, subscribed): """ Private method to set the subscription status for the broker status @@ -604,10 +599,6 @@ self.brokerStatusButton.setToolTip( self.tr("Press to activate the status display")) - ####################################################################### - ## Utility methods - ####################################################################### - def __addBrokerToRecent(self, host, port): """ Private method to add a host name to the list of recently connected @@ -706,7 +697,7 @@ topicIndex = self.publishTopicComboBox.findText(currentTopic) self.publishTopicComboBox.setCurrentIndex(topicIndex) - def __appendMessage(self, topic, payload): + def __appendMessage(self, topic, payload, qos): """ Private method to append a received message to the output. @@ -714,16 +705,19 @@ @type str @param payload payload of the received message @type bytes + @param qos quality of service indicator (0, 1, 2) + @type int """ payloadStr = str(payload, encoding="utf-8", errors="replace") - txt = self.tr("{0} -> {1}").format(topic, payloadStr) - if not txt.endswith(("\r\n", "\r", "\n")): - txt += "\n" - tc = self.messagesEdit.textCursor() - tc.movePosition(QTextCursor.End) - self.messagesEdit.setTextCursor(tc) - self.messagesEdit.insertPlainText(Utilities.filterAnsiSequences(txt)) + self.messagesEdit.setCurrentCharFormat(self.__messagesTopicFormat) + self.messagesEdit.appendPlainText(topic) + self.messagesEdit.setCurrentCharFormat(self.__messagesQFormat) + self.messagesEdit.appendPlainText(self.tr("QoS: {0}").format(qos)) + self.messagesEdit.setCurrentCharFormat(self.__messagesFormat) + self.messagesEdit.appendPlainText( + Utilities.filterAnsiSequences(payloadStr)) + self.messagesEdit.appendPlainText(60 * "#") self.messagesEdit.ensureCursorVisible() def __handleBrokerStatusMessage(self, topic, payload):