parameter_set_widget.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import rospy
00004 import actionlib
00005 import std_msgs.msg
00006 import vigir_footstep_planning_msgs.msg
00007 
00008 from python_qt_binding.QtCore import Qt, QSize, Signal, Slot
00009 from python_qt_binding.QtGui import QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QComboBox, QIcon
00010 
00011 from vigir_generic_params.msg import GetParameterSetNamesAction, GetParameterSetNamesGoal, GetParameterSetNamesResult
00012 from vigir_footstep_planning_lib.logging import *
00013 from vigir_footstep_planning_lib.topic_widget import *
00014 
00015 
00016 # widget for parameter set selection
00017 class QParameterSetWidget(QWidgetWithLogger):
00018 
00019     topic_changed_signal = Signal(str)
00020     param_cleared_signal = Signal()
00021     param_changed_signal = Signal(str)
00022 
00023     def __init__(self, parent=None, logger=Logger()):
00024         QWidgetWithLogger.__init__(self, parent, logger)
00025 
00026         # start widget
00027         vbox = QVBoxLayout()
00028         vbox.setMargin(0)
00029         vbox.setContentsMargins(0, 0, 0, 0)
00030 
00031         # parameter action server topic selection
00032         topic_widget = QTopicWidget(self, 'vigir_generic_params/GetParameterSetNamesAction', True)
00033         vbox.addWidget(topic_widget)
00034 
00035         # parameter set selection
00036         self.parameter_set_selection_widget = QParameterSetSelectionWidget(self, logger)
00037         self.parameter_set_selection_widget.param_cleared_signal.connect(self.param_cleared)
00038         self.parameter_set_selection_widget.param_changed_signal.connect(self.param_changed)
00039         topic_widget.topic_changed_signal.connect(self.topic_changed)
00040         topic_widget.topic_changed_signal.connect(self.parameter_set_selection_widget.set_topic_name)
00041         vbox.addWidget(self.parameter_set_selection_widget)
00042 
00043         # end widget
00044         self.setLayout(vbox)
00045 
00046         # init widget
00047         topic_widget.emit_topic_name()
00048 
00049     def current_parameter_set_name(self):
00050         return self.parameter_set_selection_widget.current_parameter_set_name()
00051 
00052     @Slot(str)
00053     def topic_changed(self, name):
00054         self.topic_changed_signal.emit(name)
00055 
00056     @Slot()
00057     def param_cleared(self):
00058         self.param_cleared_signal.emit()
00059 
00060     @Slot(str)
00061     def param_changed(self, name):
00062         self.param_changed_signal.emit(name)
00063 
00064 
00065 # widget for parameter set selection
00066 class QParameterSetSelectionWidget(QWidgetWithLogger):
00067 
00068     NO_PARAM_SET_TEXT = "No parameters available!"
00069     SELECT_TEXT = "<Select>"
00070 
00071     param_cleared_signal = Signal()
00072     param_changed_signal = Signal(str)
00073 
00074     def __init__(self, parent=None, logger=Logger()):
00075         QWidgetWithLogger.__init__(self, parent, logger)
00076 
00077         # start widget
00078         hbox = QHBoxLayout()
00079         hbox.setMargin(0)
00080         hbox.setContentsMargins(0, 0, 0, 0)
00081 
00082         # get system icon
00083         icon = QIcon.fromTheme("view-refresh")
00084         size = icon.actualSize(QSize(32, 32))
00085 
00086         # add combo box
00087         self.parameter_set_names_combo_box = QComboBox()
00088         self.parameter_set_names_combo_box.currentIndexChanged[str].connect(self.param_changed)
00089         hbox.addWidget(self.parameter_set_names_combo_box)
00090 
00091         # add refresh button
00092         self.get_all_parameter_set_names_button = QPushButton()
00093         self.get_all_parameter_set_names_button.clicked.connect(self._get_all_parameter_set_names)
00094 
00095         self.get_all_parameter_set_names_button.setIcon(icon)
00096         self.get_all_parameter_set_names_button.setFixedSize(size.width()+2, size.height()+2)
00097 
00098         hbox.addWidget(self.get_all_parameter_set_names_button)
00099 
00100         # end widget
00101         self.setLayout(hbox)
00102 
00103         # init widget
00104         self.reset_parameter_set_selection()
00105 
00106     def _init_action_client(self, topic_name):
00107         self.get_parameter_set_names_client = actionlib.SimpleActionClient(topic_name, GetParameterSetNamesAction)
00108         print "Parameter set topic changed: " + topic_name
00109 
00110     @Slot(str)
00111     def set_topic_name(self, topic_name):
00112         if len(topic_name) > 0:
00113             self._init_action_client(topic_name)
00114             self._get_all_parameter_set_names()
00115         else:
00116             self.reset_parameter_set_selection()
00117 
00118     def reset_parameter_set_selection(self):
00119         self.parameter_set_names_combo_box.setEnabled(False)
00120         self.parameter_set_names_combo_box.blockSignals(True)
00121         self.parameter_set_names_combo_box.clear()
00122         self.parameter_set_names_combo_box.addItem(self.NO_PARAM_SET_TEXT)
00123         self.get_all_parameter_set_names_button.setEnabled(False)
00124         self.param_cleared_signal.emit()
00125 
00126     def current_parameter_set_name(self):
00127         if self.parameter_set_names_combo_box.currentText() == self.NO_PARAM_SET_TEXT:
00128             return str()
00129         else:
00130             return self.parameter_set_names_combo_box.currentText()
00131 
00132     @Slot(str)
00133     def param_changed(self, name):
00134         self.param_changed_signal.emit(name)
00135 
00136     # parameter set names handler
00137     def _get_all_parameter_set_names(self):
00138         if self.get_parameter_set_names_client.wait_for_server(rospy.Duration(0.5)):
00139             self.logger.log_info("Requesting current list of parameter set names.")
00140             goal = GetParameterSetNamesGoal()
00141             self.get_parameter_set_names_client.send_goal(goal)
00142 
00143             # waiting for getting list of parameter set names
00144             if self.get_parameter_set_names_client.wait_for_result(rospy.Duration(1.0)):
00145                 result = self.get_parameter_set_names_client.get_result()
00146 
00147                 self.logger.log_info("Received " + str(len(result.names)) + " parameter set names.")
00148 
00149                 self.parameter_set_names_combo_box.blockSignals(True)
00150                 self.parameter_set_names_combo_box.clear()
00151                 self.parameter_set_names_combo_box.addItem(self.SELECT_TEXT)
00152                 self.parameter_set_names_combo_box.setItemData(0, 0, Qt.UserRole-1)
00153                 self.param_cleared_signal.emit()
00154 
00155                 for name in result.names:
00156                     self.parameter_set_names_combo_box.addItem(name.data)
00157 
00158                 self.parameter_set_names_combo_box.setEnabled(True)
00159                 self.parameter_set_names_combo_box.blockSignals(False)
00160                 self.get_all_parameter_set_names_button.setEnabled(True)
00161             else:
00162                 self.logger.log_error("Didn't received any results. Check communcation!")
00163                 self.reset_parameter_set_selection()
00164         else:
00165             self.logger.log_error("Can't connect to footstep planner parameter action server!")
00166             self.reset_parameter_set_selection()


vigir_footstep_planning_lib
Author(s): Alexander Stumpf
autogenerated on Sat Jun 8 2019 19:01:44