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


segbot_gui
Author(s): Piyush Khandelwal
autogenerated on Mon Oct 6 2014 07:30:33