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


jsk_rqt_plugins
Author(s):
autogenerated on Sun Sep 13 2015 22:29:48