def _add_track_label(self, track_name, background_color=None): label = QLabel(track_name) label.setMargin(5) palette = label.palette() palette.setColor(label.foregroundRole(), Qt.black) if background_color is not None: label.setAutoFillBackground(True) palette.setColor(label.backgroundRole(), background_color) label.setPalette(palette) label.setFont(QFont('sans', 13)) self.track_label_layout.addWidget(label)
class PingGUI(Plugin): def __init__(self, context): super(PingGUI, self).__init__(context) # Give QObjects reasonable names self.setObjectName('PingGUI') self.msg = None # Create a container widget and give it a layout self._container = QWidget() self._layout = QVBoxLayout() self._container.setLayout(self._layout) self._label = QLabel("xx ms latency") p = self._label.palette() p.setColor(self._label.backgroundRole(), Qt.red) self._label.setPalette(p) self._layout.addWidget(self._label) self.set_bg_color(100, 100, 100) rospy.Subscriber("/ping/delay", Float64, self.ping_cb) context.add_widget(self._container) self._update_plot_timer = QTimer(self) self._update_plot_timer.timeout.connect(self.update_gui) self._update_plot_timer.start(1) def update_gui(self): if not self.msg: return msg = self.msg # msec # 100 -> green, 1000 -> red # normalize within 1000 ~ 100 orig_latency = msg.data msg_data = orig_latency if msg.data > 1000: msg_data = 1000 elif msg.data < 100: msg_data = 100 ratio = (msg_data - 100) / (1000 - 100) color_r = ratio * 255.0 color_g = (1 - ratio) * 255.0 # devnull = open(os.devnull, "w") # with RedirectStdStreams(stdout=devnull, stderr=devnull): self.set_bg_color(color_r, color_g, 0) self._label.setText("%d ms latency" % (orig_latency)) def set_bg_color(self, r, g, b): self._label.setStyleSheet("QLabel { display: block; background-color: rgba(%d, %d, %d, 255); text-align: center; font-size: 30px;}" % (r, g, b)) def ping_cb(self, msg): self.msg = msg def shutdown_plugin(self): pass def save_settings(self, plugin_settings, instance_settings): pass def restore_settings(self, plugin_settings, instance_settings): pass
class PingGUI(Plugin): def __init__(self, context): super(PingGUI, self).__init__(context) # Give QObjects reasonable names self.setObjectName("PingGUI") self.msg = None # Create a container widget and give it a layout self._container = QWidget() self._layout = QVBoxLayout() self._container.setLayout(self._layout) self._label = QLabel("xx ms latency") p = self._label.palette() p.setColor(self._label.backgroundRole(), Qt.red) self._label.setPalette(p) self._layout.addWidget(self._label) self.set_bg_color(100, 100, 100) rospy.Subscriber("/ping/delay", Float64, self.ping_cb) context.add_widget(self._container) self._update_plot_timer = QTimer(self) self._update_plot_timer.timeout.connect(self.update_gui) self._update_plot_timer.start(1) def update_gui(self): if not self.msg: return msg = self.msg # msec # 100 -> green, 1000 -> red # normalize within 1000 ~ 100 orig_latency = msg.data msg_data = orig_latency if msg.data > 1000: msg_data = 1000 elif msg.data < 100: msg_data = 100 ratio = (msg_data - 100) / (1000 - 100) color_r = ratio * 255.0 color_g = (1 - ratio) * 255.0 # devnull = open(os.devnull, "w") # with RedirectStdStreams(stdout=devnull, stderr=devnull): self.set_bg_color(color_r, color_g, 0) self._label.setText("%d ms latency" % (orig_latency)) def set_bg_color(self, r, g, b): self._label.setStyleSheet( "QLabel { display: block; background-color: rgba(%d, %d, %d, 255); text-align: center; font-size: 30px;}" % (r, g, b) ) def ping_cb(self, msg): self.msg = msg def shutdown_plugin(self): pass def save_settings(self, plugin_settings, instance_settings): pass def restore_settings(self, plugin_settings, instance_settings): pass