label.py
Go to the documentation of this file.
1 from distutils.version import LooseVersion
2 import math
3 import os
4 import sys
5 from threading import Lock
6 
7 import python_qt_binding
8 import python_qt_binding.QtCore as QtCore
9 from python_qt_binding.QtCore import QEvent
10 from python_qt_binding.QtCore import QSize
11 from python_qt_binding.QtCore import Qt
12 from python_qt_binding.QtCore import QTimer
13 from python_qt_binding.QtCore import qWarning
14 from python_qt_binding.QtCore import Slot
15 import python_qt_binding.QtGui as QtGui
16 from python_qt_binding.QtGui import QBrush
17 from python_qt_binding.QtGui import QColor
18 from python_qt_binding.QtGui import QFont
19 from python_qt_binding.QtGui import QIcon
20 from python_qt_binding.QtGui import QPainter
21 from python_qt_binding.QtGui import QPen
22 import yaml
23 
24 from resource_retriever import get_filename
25 import roslib
26 import rospy
27 from rqt_gui_py.plugin import Plugin
28 import rqt_plot
29 from std_msgs.msg import Bool
30 from std_msgs.msg import Time
31 from std_msgs.msg import String
32 
33 from .util import get_slot_type_field_names
34 from .hist import ROSData
35 from .button_general import LineEditDialog
36 
37 if LooseVersion(python_qt_binding.QT_BINDING_VERSION).version[0] >= 5:
38  from python_qt_binding.QtWidgets import QAction
39  from python_qt_binding.QtWidgets import QComboBox
40  from python_qt_binding.QtWidgets import QLabel
41  from python_qt_binding.QtWidgets import QMenu
42  from python_qt_binding.QtWidgets import QMessageBox
43  from python_qt_binding.QtWidgets import QSizePolicy
44  from python_qt_binding.QtWidgets import QVBoxLayout
45  from python_qt_binding.QtWidgets import QWidget
46 
47 else:
48  from python_qt_binding.QtGui import QAction
49  from python_qt_binding.QtGui import QComboBox
50  from python_qt_binding.QtGui import QLabel
51  from python_qt_binding.QtGui import QMenu
52  from python_qt_binding.QtGui import QMessageBox
53  from python_qt_binding.QtGui import QSizePolicy
54  from python_qt_binding.QtGui import QVBoxLayout
55  from python_qt_binding.QtGui import QWidget
56 
57 
59  """
60  rqt plugin to provide simple label
61  """
62  def __init__(self, context):
63  super(StringLabel, self).__init__(context)
64  self.setObjectName("StringLabel")
66  context.add_widget(self._widget)
67 
68  def save_settings(self, plugin_settings, instance_settings):
69  self._widget.save_settings(plugin_settings, instance_settings)
70 
71  def restore_settings(self, plugin_settings, instance_settings):
72  self._widget.restore_settings(plugin_settings, instance_settings)
73 
75  self._widget.trigger_configuration()
76 
77 
78 class StringLabelWidget(QWidget):
79  def __init__(self):
80  super(StringLabelWidget, self).__init__()
81  self.lock = Lock()
82  vbox = QVBoxLayout(self)
83  self.label = QLabel()
84  self.label.setAlignment(Qt.AlignLeft)
85  self.label.setSizePolicy(
86  QSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored))
87  font = QFont("Helvetica", 14)
88  self.label.setFont(font)
89  self.label.setWordWrap(True)
90  vbox.addWidget(self.label)
91  self.string_sub = None
92  self._string_topics = []
93  self._update_topic_timer = QTimer(self)
94  self._update_topic_timer.timeout.connect(self.updateTopics)
95  self._update_topic_timer.start(1000)
96  self._active_topic = None
97  # to update label visualization
98  self._dialog = LineEditDialog()
99  self._rosdata = None
100  self._start_time = rospy.get_time()
101  self._update_label_timer = QTimer(self)
102  self._update_label_timer.timeout.connect(self.updateLabel)
103  self._update_label_timer.start(40)
104 
106  self._dialog.exec_()
107  self.setupSubscriber(self._dialog.value)
108 
109  def updateLabel(self):
110  if not self._rosdata:
111  return
112  try:
113  _, data_y = self._rosdata.next()
115  self._rosdata = None
116  return
117  if len(data_y) == 0:
118  return
119  latest = data_y[-1] # get latest data
120  # supports std_msgs/String as well as string data nested in rosmsg
121  if type(latest) == String:
122  self.string = latest.data
123  else:
124  self.string = latest
125  try:
126  self.label.setText(self.string)
127  except TypeError as e:
128  rospy.logwarn(e)
129 
130  def updateTopics(self):
131  need_to_update = False
132  for (topic, topic_type) in rospy.get_published_topics():
133  msg = roslib.message.get_message_class(topic_type)
134  field_names = get_slot_type_field_names(msg, slot_type='string')
135  for field in field_names:
136  string_topic = topic + field
137  if string_topic not in self._string_topics:
138  self._string_topics.append(string_topic)
139  need_to_update = True
140  if need_to_update:
141  self._string_topics = sorted(self._string_topics)
142  self._dialog.combo_box.clear()
143  for topic in self._string_topics:
144  self._dialog.combo_box.addItem(topic)
145  if self._active_topic:
146  if self._active_topic not in self._string_topics:
147  self._string_topics.append(self._active_topic)
148  self._dialog.combo_box.addItem(self._active_topic)
149  self._dialog.combo_box.setCurrentIndex(
150  self._string_topics.index(self._active_topic))
151 
152  def setupSubscriber(self, topic):
153  if not self._rosdata:
154  self._rosdata = ROSData(topic, self._start_time)
155  else:
156  if self._rosdata != topic:
157  self._rosdata.close()
158  self._rosdata = ROSData(topic, self._start_time)
159  else:
160  rospy.logwarn("%s is already subscribed", topic)
161  self._active_topic = topic
162 
163  def onActivated(self, number):
164  self.setupSubscriber(self._string_topics[number])
165 
166  def save_settings(self, plugin_settings, instance_settings):
167  if self._active_topic:
168  instance_settings.set_value("active_topic", self._active_topic)
169 
170  def restore_settings(self, plugin_settings, instance_settings):
171  if instance_settings.value("active_topic"):
172  topic = instance_settings.value("active_topic")
173  self._dialog.combo_box.addItem(topic)
174  self.setupSubscriber(topic)
def save_settings(self, plugin_settings, instance_settings)
Definition: label.py:68
def save_settings(self, plugin_settings, instance_settings)
Definition: label.py:166
def restore_settings(self, plugin_settings, instance_settings)
Definition: label.py:71
def get_slot_type_field_names(msg, slot_type, field_name=None, found=None)
Definition: util.py:7
def restore_settings(self, plugin_settings, instance_settings)
Definition: label.py:170
def __init__(self, context)
Definition: label.py:62


jsk_rqt_plugins
Author(s):
autogenerated on Fri Feb 5 2021 04:03:20