topic_widget.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import rospy
4 
5 from python_qt_binding.QtCore import Qt, QSize, QRegExp, Signal, Slot
6 from python_qt_binding.QtGui import QWidget, QRegExpValidator, QHBoxLayout, QPushButton, QComboBox, QIcon
7 
8 
9 # widget for topic selection
10 class QTopicWidget(QWidget):
11 
12  topic_changed_signal = Signal(str)
13 
14  def __init__(self, parent=None, topic_type=str(), is_action_topic=False):
15  QWidget.__init__(self, parent)
16 
17  if is_action_topic:
18  self.topic_type = topic_type + "Goal"
19  else:
20  self.topic_type = topic_type
21  self.is_action_topic = is_action_topic
22 
23  # start widget
24  hbox = QHBoxLayout()
25  hbox.setMargin(0)
26  hbox.setContentsMargins(0, 0, 0, 0)
27 
28  # topic combo box
29  self.topic_combo_box = QComboBox()
30  self.topic_combo_box.setEnabled(False)
31  self.topic_combo_box.blockSignals(True)
32  self.topic_combo_box.setValidator(QRegExpValidator(QRegExp('((\d|\w|/)(?!//))*'), self))
33  self.topic_combo_box.currentIndexChanged[str].connect(self.topic_changed)
34  hbox.addWidget(self.topic_combo_box)
35 
36  # get system icon
37  icon = QIcon.fromTheme("view-refresh")
38  size = icon.actualSize(QSize(32, 32))
39 
40  # add refresh button
41  refresh_topics_button = QPushButton()
42  refresh_topics_button.clicked.connect(self.update_topic_list)
43  refresh_topics_button.setIcon(icon)
44  refresh_topics_button.setFixedSize(size.width()+2, size.height()+2)
45  hbox.addWidget(refresh_topics_button)
46 
47  # end widget
48  self.setLayout(hbox)
49 
50  # init widget
51  self.update_topic_list()
52 
53  def emit_topic_name(self):
54  self.topic_changed_signal.emit(self.current_topic())
55 
56  def set_editable(self, enable):
57  self.topic_combo_box.setEditable(enable)
58 
59  def current_topic(self):
60  if self.topic_combo_box.isEnabled():
61  return self.topic_combo_box.currentText()
62  else:
63  return str()
64 
65  @Slot(str)
66  def topic_changed(self, topic_name):
67  self.topic_changed_signal.emit(topic_name)
68 
69  @Slot()
70  def update_topic_list(self):
71  self.topic_combo_box.clear()
72  self.topic_combo_box.setEnabled(False)
73  self.topic_combo_box.blockSignals(True)
74  self.topic_combo_box.addItem('Updating...')
75 
76  # get topic list
77  _, _, topic_type = rospy.get_master().getTopicTypes()
78  topic_dict = dict(topic_type)
79  # filter list
80  topic_dict_filtered = dict()
81  for k, v in topic_dict.items():
82  if (len(topic_type) == 0) or (v == self.topic_type):
83  if self.is_action_topic:
84  topic_dict_filtered[k[:-5]] = v
85  else:
86  topic_dict_filtered[k] = v
87 
88  self.topic_combo_box.clear()
89  self.topic_combo_box.addItems(sorted(topic_dict_filtered.keys()))
90 
91  if self.topic_combo_box.count() > 0:
92  self.topic_combo_box.setEnabled(True)
93  self.topic_combo_box.blockSignals(False)
94  self.topic_changed(self.topic_combo_box.currentText())
95  else:
96  self.topic_combo_box.addItem('No topics available!')
def __init__(self, parent=None, topic_type=str(), is_action_topic=False)
Definition: topic_widget.py:14


vigir_footstep_planning_lib
Author(s): Alexander Stumpf
autogenerated on Mon Jun 10 2019 15:47:33