parameter_set_widget.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import rospy
4 import actionlib
5 import std_msgs.msg
6 import vigir_footstep_planning_msgs.msg
7 
8 from python_qt_binding.QtCore import Qt, QSize, Signal, Slot
9 from python_qt_binding.QtGui import QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QComboBox, QIcon
10 
11 from vigir_generic_params.msg import GetParameterSetNamesAction, GetParameterSetNamesGoal, GetParameterSetNamesResult
14 
15 
16 # widget for parameter set selection
18 
19  topic_changed_signal = Signal(str)
20  param_cleared_signal = Signal()
21  param_changed_signal = Signal(str)
22 
23  def __init__(self, parent=None, logger=Logger()):
24  QWidgetWithLogger.__init__(self, parent, logger)
25 
26  # start widget
27  vbox = QVBoxLayout()
28  vbox.setMargin(0)
29  vbox.setContentsMargins(0, 0, 0, 0)
30 
31  # parameter action server topic selection
32  topic_widget = QTopicWidget(self, 'vigir_generic_params/GetParameterSetNamesAction', True)
33  vbox.addWidget(topic_widget)
34 
35  # parameter set selection
37  self.parameter_set_selection_widget.param_cleared_signal.connect(self.param_cleared)
38  self.parameter_set_selection_widget.param_changed_signal.connect(self.param_changed)
39  topic_widget.topic_changed_signal.connect(self.topic_changed)
40  topic_widget.topic_changed_signal.connect(self.parameter_set_selection_widget.set_topic_name)
41  vbox.addWidget(self.parameter_set_selection_widget)
42 
43  # end widget
44  self.setLayout(vbox)
45 
46  # init widget
47  topic_widget.emit_topic_name()
48 
50  return self.parameter_set_selection_widget.current_parameter_set_name()
51 
52  @Slot(str)
53  def topic_changed(self, name):
54  self.topic_changed_signal.emit(name)
55 
56  @Slot()
57  def param_cleared(self):
58  self.param_cleared_signal.emit()
59 
60  @Slot(str)
61  def param_changed(self, name):
62  self.param_changed_signal.emit(name)
63 
64 
65 # widget for parameter set selection
67 
68  NO_PARAM_SET_TEXT = "No parameters available!"
69  SELECT_TEXT = "<Select>"
70 
71  param_cleared_signal = Signal()
72  param_changed_signal = Signal(str)
73 
74  def __init__(self, parent=None, logger=Logger()):
75  QWidgetWithLogger.__init__(self, parent, logger)
76 
77  # start widget
78  hbox = QHBoxLayout()
79  hbox.setMargin(0)
80  hbox.setContentsMargins(0, 0, 0, 0)
81 
82  # get system icon
83  icon = QIcon.fromTheme("view-refresh")
84  size = icon.actualSize(QSize(32, 32))
85 
86  # add combo box
87  self.parameter_set_names_combo_box = QComboBox()
88  self.parameter_set_names_combo_box.currentIndexChanged[str].connect(self.param_changed)
89  hbox.addWidget(self.parameter_set_names_combo_box)
90 
91  # add refresh button
93  self.get_all_parameter_set_names_button.clicked.connect(self._get_all_parameter_set_names)
94 
95  self.get_all_parameter_set_names_button.setIcon(icon)
96  self.get_all_parameter_set_names_button.setFixedSize(size.width()+2, size.height()+2)
97 
98  hbox.addWidget(self.get_all_parameter_set_names_button)
99 
100  # end widget
101  self.setLayout(hbox)
102 
103  # init widget
105 
106  def _init_action_client(self, topic_name):
107  self.get_parameter_set_names_client = actionlib.SimpleActionClient(topic_name, GetParameterSetNamesAction)
108  print "Parameter set topic changed: " + topic_name
109 
110  @Slot(str)
111  def set_topic_name(self, topic_name):
112  if len(topic_name) > 0:
113  self._init_action_client(topic_name)
115  else:
117 
119  self.parameter_set_names_combo_box.setEnabled(False)
120  self.parameter_set_names_combo_box.blockSignals(True)
121  self.parameter_set_names_combo_box.clear()
122  self.parameter_set_names_combo_box.addItem(self.NO_PARAM_SET_TEXT)
123  self.get_all_parameter_set_names_button.setEnabled(False)
124  self.param_cleared_signal.emit()
125 
127  if self.parameter_set_names_combo_box.currentText() == self.NO_PARAM_SET_TEXT:
128  return str()
129  else:
130  return self.parameter_set_names_combo_box.currentText()
131 
132  @Slot(str)
133  def param_changed(self, name):
134  self.param_changed_signal.emit(name)
135 
136  # parameter set names handler
138  if self.get_parameter_set_names_client.wait_for_server(rospy.Duration(0.5)):
139  self.logger.log_info("Requesting current list of parameter set names.")
140  goal = GetParameterSetNamesGoal()
141  self.get_parameter_set_names_client.send_goal(goal)
142 
143  # waiting for getting list of parameter set names
144  if self.get_parameter_set_names_client.wait_for_result(rospy.Duration(1.0)):
145  result = self.get_parameter_set_names_client.get_result()
146 
147  self.logger.log_info("Received " + str(len(result.names)) + " parameter set names.")
148 
149  self.parameter_set_names_combo_box.blockSignals(True)
150  self.parameter_set_names_combo_box.clear()
151  self.parameter_set_names_combo_box.addItem(self.SELECT_TEXT)
152  self.parameter_set_names_combo_box.setItemData(0, 0, Qt.UserRole-1)
153  self.param_cleared_signal.emit()
154 
155  for name in result.names:
156  self.parameter_set_names_combo_box.addItem(name.data)
157 
158  self.parameter_set_names_combo_box.setEnabled(True)
159  self.parameter_set_names_combo_box.blockSignals(False)
160  self.get_all_parameter_set_names_button.setEnabled(True)
161  else:
162  self.logger.log_error("Didn't received any results. Check communcation!")
164  else:
165  self.logger.log_error("Can't connect to footstep planner parameter action server!")


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