yes_no_button.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 from distutils.version import LooseVersion
00004 import os
00005 import rospkg
00006 import rospy
00007 import rosservice
00008 
00009 import python_qt_binding
00010 from python_qt_binding import loadUi
00011 from qt_gui.plugin import Plugin
00012 
00013 from jsk_gui_msgs.srv import YesNo
00014 from jsk_gui_msgs.srv import YesNoResponse
00015 
00016 # For both qt4 and qt5
00017 if LooseVersion(python_qt_binding.QT_BINDING_VERSION).version[0] >= 5:
00018     from python_qt_binding.QtWidgets import QWidget
00019 else:
00020     from python_qt_binding.QtGui import QWidget
00021 
00022 
00023 class YesNoButtonWidget(QWidget):
00024     def __init__(self):
00025         super(YesNoButtonWidget, self).__init__()
00026         rp = rospkg.RosPack()
00027         ui_file = os.path.join(
00028             rp.get_path('jsk_rqt_plugins'), 'resource', 'yes_no_button.ui')
00029         loadUi(ui_file, self)
00030         self.setObjectName('YesNoButtonUi')
00031         self.yes_button.clicked[bool].connect(self._clicked_yes_button)
00032         self.no_button.clicked[bool].connect(self._clicked_no_button)
00033         self.yes_button.setEnabled(False)
00034         self.no_button.setEnabled(False)
00035         self.yes = None
00036         service_name = rospy.get_namespace() + 'rqt_yn_btn'
00037         if service_name in rosservice.get_service_list():
00038             rospy.logwarn('{} is already advertised'.format(service_name))
00039             return
00040         self.srv = rospy.Service('rqt_yn_btn', YesNo, self._handle_yn_btn)
00041 
00042     def _clicked_yes_button(self):
00043         """Handle events of being clicked yes button."""
00044         self.yes = True
00045 
00046     def _clicked_no_button(self):
00047         """Handle events of being clicked no button."""
00048         self.yes = False
00049 
00050     def _handle_yn_btn(self, req):
00051         """Callback function of service,
00052 
00053         and handle enable/disable of the buttons.
00054         """
00055         self.message.setText(req.message)
00056         self.yes = None  # initialize
00057         self.yes_button.setEnabled(True)
00058         self.no_button.setEnabled(True)
00059         while self.yes is None:  # wait for user input
00060             rospy.sleep(1.)
00061         self.yes_button.setEnabled(False)
00062         self.no_button.setEnabled(False)
00063         return YesNoResponse(yes=self.yes)
00064 
00065     def __del__(self):
00066         self.srv.shutdown()
00067 
00068 
00069 class YesNoButton(Plugin):
00070     def __init__(self, context):
00071         super(YesNoButton, self).__init__(context)
00072         self.setObjectName('YesNoButton')
00073         self._widget = YesNoButtonWidget()
00074         context.add_widget(self._widget)


jsk_rqt_plugins
Author(s):
autogenerated on Wed May 1 2019 02:40:16