label.py
Go to the documentation of this file.
00001 from distutils.version import LooseVersion
00002 import math
00003 import os
00004 import sys
00005 from threading import Lock
00006 
00007 import python_qt_binding
00008 import python_qt_binding.QtCore as QtCore
00009 from python_qt_binding.QtCore import QEvent
00010 from python_qt_binding.QtCore import QSize
00011 from python_qt_binding.QtCore import Qt
00012 from python_qt_binding.QtCore import QTimer
00013 from python_qt_binding.QtCore import qWarning
00014 from python_qt_binding.QtCore import Slot
00015 import python_qt_binding.QtGui as QtGui
00016 from python_qt_binding.QtGui import QBrush
00017 from python_qt_binding.QtGui import QColor
00018 from python_qt_binding.QtGui import QFont
00019 from python_qt_binding.QtGui import QIcon
00020 from python_qt_binding.QtGui import QPainter
00021 from python_qt_binding.QtGui import QPen
00022 import yaml
00023 
00024 from resource_retriever import get_filename
00025 import roslib
00026 import rospy
00027 from rqt_gui_py.plugin import Plugin
00028 import rqt_plot
00029 from std_msgs.msg import Bool
00030 from std_msgs.msg import Time
00031 from std_msgs.msg import String
00032 
00033 from .util import get_slot_type_field_names
00034 from .hist import ROSData
00035 from .button_general import LineEditDialog
00036 
00037 if LooseVersion(python_qt_binding.QT_BINDING_VERSION).version[0] >= 5:
00038     from python_qt_binding.QtWidgets import QAction
00039     from python_qt_binding.QtWidgets import QComboBox
00040     from python_qt_binding.QtWidgets import QLabel
00041     from python_qt_binding.QtWidgets import QMenu
00042     from python_qt_binding.QtWidgets import QMessageBox
00043     from python_qt_binding.QtWidgets import QSizePolicy
00044     from python_qt_binding.QtWidgets import QVBoxLayout
00045     from python_qt_binding.QtWidgets import QWidget
00046 
00047 else:
00048     from python_qt_binding.QtGui import QAction
00049     from python_qt_binding.QtGui import QComboBox
00050     from python_qt_binding.QtGui import QLabel
00051     from python_qt_binding.QtGui import QMenu
00052     from python_qt_binding.QtGui import QMessageBox
00053     from python_qt_binding.QtGui import QSizePolicy
00054     from python_qt_binding.QtGui import QVBoxLayout
00055     from python_qt_binding.QtGui import QWidget
00056 
00057 
00058 class StringLabel(Plugin):
00059     """
00060     rqt plugin to provide simple label
00061     """
00062     def __init__(self, context):
00063         super(StringLabel, self).__init__(context)
00064         self.setObjectName("StringLabel")
00065         self._widget = StringLabelWidget()
00066         context.add_widget(self._widget)
00067 
00068     def save_settings(self, plugin_settings, instance_settings):
00069         self._widget.save_settings(plugin_settings, instance_settings)
00070 
00071     def restore_settings(self, plugin_settings, instance_settings):
00072         self._widget.restore_settings(plugin_settings, instance_settings)
00073 
00074     def trigger_configuration(self):
00075         self._widget.trigger_configuration()
00076 
00077 
00078 class StringLabelWidget(QWidget):
00079     def __init__(self):
00080         super(StringLabelWidget, self).__init__()
00081         self.lock = Lock()
00082         vbox = QVBoxLayout(self)
00083         self.label = QLabel()
00084         self.label.setAlignment(Qt.AlignLeft)
00085         self.label.setSizePolicy(
00086             QSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored))
00087         font = QFont("Helvetica", 14)
00088         self.label.setFont(font)
00089         self.label.setWordWrap(True)
00090         vbox.addWidget(self.label)
00091         self.string_sub = None
00092         self._string_topics = []
00093         self._update_topic_timer = QTimer(self)
00094         self._update_topic_timer.timeout.connect(self.updateTopics)
00095         self._update_topic_timer.start(1000)
00096         self._active_topic = None
00097         # to update label visualization
00098         self._dialog = LineEditDialog()
00099         self._rosdata = None
00100         self._start_time = rospy.get_time()
00101         self._update_label_timer = QTimer(self)
00102         self._update_label_timer.timeout.connect(self.updateLabel)
00103         self._update_label_timer.start(40)
00104 
00105     def trigger_configuration(self):
00106         self._dialog.exec_()
00107         self.setupSubscriber(self._dialog.value)
00108 
00109     def updateLabel(self):
00110         if not self._rosdata:
00111             return
00112         try:
00113             _, data_y = self._rosdata.next()
00114         except rqt_plot.rosplot.RosPlotException, e:
00115             self._rosdata = None
00116             return
00117         if len(data_y) == 0:
00118             return
00119         latest = data_y[-1]  # get latest data
00120         # supports std_msgs/String as well as string data nested in rosmsg
00121         if type(latest) == String:
00122             self.string = latest.data
00123         else:
00124             self.string = latest
00125         try:
00126             self.label.setText(self.string)
00127         except TypeError, e:
00128             rospy.logwarn(e)
00129 
00130     def updateTopics(self):
00131         need_to_update = False
00132         for (topic, topic_type) in rospy.get_published_topics():
00133             msg = roslib.message.get_message_class(topic_type)
00134             field_names = get_slot_type_field_names(msg, slot_type='string')
00135             for field in field_names:
00136                 string_topic = topic + field
00137                 if string_topic not in self._string_topics:
00138                     self._string_topics.append(string_topic)
00139                     need_to_update = True
00140         if need_to_update:
00141             self._string_topics = sorted(self._string_topics)
00142             self._dialog.combo_box.clear()
00143             for topic in self._string_topics:
00144                 self._dialog.combo_box.addItem(topic)
00145             if self._active_topic:
00146                 if self._active_topic not in self._string_topics:
00147                     self._string_topics.append(self._active_topic)
00148                     self._dialog.combo_box.addItem(self._active_topic)
00149                 self._dialog.combo_box.setCurrentIndex(
00150                     self._string_topics.index(self._active_topic))
00151 
00152     def setupSubscriber(self, topic):
00153         if not self._rosdata:
00154             self._rosdata = ROSData(topic, self._start_time)
00155         else:
00156             if self._rosdata != topic:
00157                 self._rosdata.close()
00158                 self._rosdata = ROSData(topic, self._start_time)
00159             else:
00160                 rospy.logwarn("%s is already subscribed", topic)
00161         self._active_topic = topic
00162 
00163     def onActivated(self, number):
00164         self.setupSubscriber(self._string_topics[number])
00165 
00166     def save_settings(self, plugin_settings, instance_settings):
00167         if self._active_topic:
00168             instance_settings.set_value("active_topic", self._active_topic)
00169 
00170     def restore_settings(self, plugin_settings, instance_settings):
00171         if instance_settings.value("active_topic"):
00172             topic = instance_settings.value("active_topic")
00173             self._dialog.combo_box.addItem(topic)
00174             self.setupSubscriber(topic)


jsk_rqt_plugins
Author(s):
autogenerated on Wed May 1 2019 02:40:16