topic_widget.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import rospy
00004 
00005 from python_qt_binding.QtCore import Qt, QSize, QRegExp, Signal, Slot
00006 from python_qt_binding.QtGui import QWidget, QRegExpValidator, QHBoxLayout, QPushButton, QComboBox, QIcon
00007 
00008 
00009 # widget for topic selection
00010 class QTopicWidget(QWidget):
00011 
00012     topic_changed_signal = Signal(str)
00013 
00014     def __init__(self, parent=None, topic_type=str(), is_action_topic=False):
00015         QWidget.__init__(self, parent)
00016 
00017         if is_action_topic:
00018             self.topic_type = topic_type + "Goal"
00019         else:
00020             self.topic_type = topic_type
00021         self.is_action_topic = is_action_topic
00022 
00023         # start widget
00024         hbox = QHBoxLayout()
00025         hbox.setMargin(0)
00026         hbox.setContentsMargins(0, 0, 0, 0)
00027 
00028         # topic combo box
00029         self.topic_combo_box = QComboBox()
00030         self.topic_combo_box.setEnabled(False)
00031         self.topic_combo_box.blockSignals(True)
00032         self.topic_combo_box.setValidator(QRegExpValidator(QRegExp('((\d|\w|/)(?!//))*'), self))
00033         self.topic_combo_box.currentIndexChanged[str].connect(self.topic_changed)
00034         hbox.addWidget(self.topic_combo_box)
00035 
00036         # get system icon
00037         icon = QIcon.fromTheme("view-refresh")
00038         size = icon.actualSize(QSize(32, 32))
00039 
00040         # add refresh button
00041         refresh_topics_button = QPushButton()
00042         refresh_topics_button.clicked.connect(self.update_topic_list)
00043         refresh_topics_button.setIcon(icon)
00044         refresh_topics_button.setFixedSize(size.width()+2, size.height()+2)
00045         hbox.addWidget(refresh_topics_button)
00046 
00047         # end widget
00048         self.setLayout(hbox)
00049 
00050         # init widget
00051         self.update_topic_list()
00052 
00053     def emit_topic_name(self):
00054         self.topic_changed_signal.emit(self.current_topic())
00055 
00056     def set_editable(self, enable):
00057         self.topic_combo_box.setEditable(enable)
00058 
00059     def current_topic(self):
00060         if self.topic_combo_box.isEnabled():
00061             return self.topic_combo_box.currentText()
00062         else:
00063             return str()
00064 
00065     @Slot(str)
00066     def topic_changed(self, topic_name):
00067         self.topic_changed_signal.emit(topic_name)
00068 
00069     @Slot()
00070     def update_topic_list(self):
00071         self.topic_combo_box.clear()
00072         self.topic_combo_box.setEnabled(False)
00073         self.topic_combo_box.blockSignals(True)
00074         self.topic_combo_box.addItem('Updating...')
00075 
00076         # get topic list
00077         _, _, topic_type = rospy.get_master().getTopicTypes()
00078         topic_dict = dict(topic_type)
00079         # filter list
00080         topic_dict_filtered = dict()
00081         for k, v in topic_dict.items():
00082             if (len(topic_type) == 0) or (v == self.topic_type):
00083                 if self.is_action_topic:
00084                     topic_dict_filtered[k[:-5]] = v
00085                 else:
00086                     topic_dict_filtered[k] = v
00087 
00088         self.topic_combo_box.clear()
00089         self.topic_combo_box.addItems(sorted(topic_dict_filtered.keys()))
00090 
00091         if self.topic_combo_box.count() > 0:
00092             self.topic_combo_box.setEnabled(True)
00093             self.topic_combo_box.blockSignals(False)
00094             self.topic_changed(self.topic_combo_box.currentText())
00095         else:
00096             self.topic_combo_box.addItem('No topics available!')


vigir_footstep_planning_lib
Author(s): Alexander Stumpf
autogenerated on Sat Jul 15 2017 02:47:56