plugins.py
Go to the documentation of this file.
00001 import rospy
00002 import time
00003 
00004 from bwi_msgs.srv import QuestionDialog, QuestionDialogResponse, \
00005                          QuestionDialogRequest
00006 from functools import partial
00007 from qt_gui.plugin import Plugin
00008 from python_qt_binding.QtGui import QFont, QHBoxLayout, QLabel, QLineEdit, \
00009                                     QPushButton, QTextBrowser, QVBoxLayout, \
00010                                     QWidget
00011 from python_qt_binding.QtCore import SIGNAL
00012 
00013 class QuestionDialogPlugin(Plugin):
00014 
00015     def __init__(self, context):
00016         super(QuestionDialogPlugin, self).__init__(context)
00017         # Give QObjects reasonable names
00018         self.setObjectName('QuestionDialogPlugin')
00019 
00020         font_size = rospy.get_param("~font_size", 40)
00021 
00022         # Create QWidget
00023         self._widget = QWidget()
00024         self._widget.setFont(QFont("Times", font_size, QFont.Bold))
00025         self._layout = QVBoxLayout(self._widget)
00026         self._text_browser = QTextBrowser(self._widget)
00027         self._layout.addWidget(self._text_browser)
00028         self._button_layout = QHBoxLayout()
00029         self._layout.addLayout(self._button_layout)
00030 
00031         # layout = QVBoxLayout(self._widget)
00032         # layout.addWidget(self.button)
00033         self._widget.setObjectName('QuestionDialogPluginUI')
00034         if context.serial_number() > 1:
00035             self._widget.setWindowTitle(self._widget.windowTitle() +
00036                                         (' (%d)' % context.serial_number()))
00037         context.add_widget(self._widget)
00038 
00039         # Setup service provider
00040         self.service = rospy.Service('question_dialog', QuestionDialog,
00041                                      self.service_callback)
00042         self.response_ready = False
00043         self.response = None
00044         self.buttons = []
00045         self.text_label = None
00046         self.text_input = None
00047 
00048         self.connect(self._widget, SIGNAL("update"), self.update)
00049         self.connect(self._widget, SIGNAL("timeout"), self.timeout)
00050 
00051     def shutdown_plugin(self):
00052         self.service.shutdown()
00053 
00054     def service_callback(self, req):
00055         self.response_ready = False
00056         self.request = req
00057         self._widget.emit(SIGNAL("update"))
00058         # Start timer against wall clock here instead of the ros clock.
00059         start_time = time.time()
00060         while not self.response_ready:
00061             if self.request != req:
00062                 # The request got preempted by a new request.
00063                 return QuestionDialogResponse(QuestionDialogRequest.PREEMPTED, "")
00064             if req.timeout != QuestionDialogRequest.NO_TIMEOUT:
00065                 current_time = time.time()
00066                 if current_time - start_time > req.timeout:
00067                     self._widget.emit(SIGNAL("timeout"))
00068                     return QuestionDialogResponse(
00069                             QuestionDialogRequest.TIMED_OUT, "")
00070             time.sleep(0.2)
00071         return self.response
00072 
00073     def update(self):
00074         self.clean()
00075         req = self.request
00076         self._text_browser.setText(req.message)
00077         if req.type == QuestionDialogRequest.DISPLAY:
00078             # All done, nothing more too see here.
00079             self.response = QuestionDialogResponse(
00080                     QuestionDialogRequest.NO_RESPONSE, "")
00081             self.response_ready = True
00082         elif req.type == QuestionDialogRequest.CHOICE_QUESTION:
00083             for index, options in enumerate(req.options):
00084                 button = QPushButton(options, self._widget)
00085                 button.clicked.connect(partial(self.handle_button, index))
00086                 self._button_layout.addWidget(button)
00087                 self.buttons.append(button)
00088         elif req.type == QuestionDialogRequest.TEXT_QUESTION:
00089             self.text_label = QLabel("Enter here: ", self._widget)
00090             self._button_layout.addWidget(self.text_label)
00091             self.text_input = QLineEdit(self._widget)
00092             self.text_input.editingFinished.connect(self.handle_text)
00093             self._button_layout.addWidget(self.text_input)
00094 
00095     def timeout(self):
00096         self._text_browser.setText("Oh no! The request timed out.")
00097         self.clean()
00098 
00099     def clean(self):
00100         while self._button_layout.count():
00101             item = self._button_layout.takeAt(0)
00102             item.widget().deleteLater()
00103         self.buttons = []
00104         self.text_input = None
00105         self.text_label = None
00106 
00107     def handle_button(self, index):
00108         self.response = QuestionDialogResponse(index, "")
00109         self.clean()
00110         self.response_ready = True
00111 
00112     def handle_text(self):
00113         self.response = QuestionDialogResponse(
00114             QuestionDialogRequest.TEXT_RESPONSE,
00115             self.text_input.text())
00116         self.clean()
00117         self.response_ready = True
00118 
00119     def save_settings(self, plugin_settings, instance_settings):
00120         # TODO save intrinsic configuration, usually using:
00121         # instance_settings.set_value(k, v)
00122         pass
00123 
00124     def restore_settings(self, plugin_settings, instance_settings):
00125         # TODO restore intrinsic configuration, usually using:
00126         # v = instance_settings.value(k)
00127         pass
00128 
00129     #def trigger_configuration(self):
00130         # Comment in to signal that the plugin has a way to configure
00131         # This will enable a setting button (gear icon) in each dock widget title bar
00132         # Usually used to open a modal configuration dialog


segbot_gui
Author(s): Piyush Khandelwal
autogenerated on Thu Aug 27 2015 15:11:29