resource_chooser.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # License: BSD
00004 #   https://raw.github.com/robotics-in-concert/rocon_qt_gui/license/LICENSE
00005 #
00006 ##############################################################################
00007 # Imports
00008 ##############################################################################
00009 from __future__ import division
00010 import os
00011 
00012 import rospy
00013 import rospkg
00014 import rocon_uri
00015 import threading
00016 from python_qt_binding import loadUi
00017 from python_qt_binding.QtCore import Qt, Signal, SIGNAL
00018 from python_qt_binding.QtGui import QWidget, QMessageBox, QTreeWidgetItem, QBrush, QColor
00019 
00020 ##############################################################################
00021 # Class
00022 ##############################################################################
00023 
00024 class QResourceChooser(QWidget):
00025     '''
00026       Captures resource from concert
00027     '''
00028     def __init__(self, parent=None):
00029         super(QResourceChooser, self).__init__()
00030         self._load_ui()
00031         self._init_events()
00032         self._init_callbacks()
00033         self.current_captured_resource = None
00034         self.current_resource = None
00035         self.resource_item_list = {}
00036         self._lock = threading.Lock()
00037 
00038 
00039     def _load_ui(self): 
00040         rospack = rospkg.RosPack() 
00041         ui_file = os.path.join(rospack.get_path('rocon_qt_library'), 'ui', 'resource_chooser.ui')
00042         loadUi(ui_file, self)
00043 
00044     def set_callbacks(self, capture_resource_event_callbacks=[], release_resource_event_callbacks=[]):
00045         self._callback['capture_event'] = capture_resource_event_callbacks
00046         self._callback['release_event'] = release_resource_event_callbacks
00047         
00048     def _init_events(self):
00049         # List item click event
00050         self.resource_list_tree_widget.itemClicked.connect(self._resource_selected)
00051         self.resource_list_tree_widget.itemDoubleClicked.connect(self._resource_double_clicked)
00052 
00053         # Button event connect
00054         self.capture_resource_btn.pressed.connect(self._capture_resource)
00055         self.release_resource_btn.pressed.connect(self._release_resource)
00056 
00057         # Event emitter
00058         self.connect(self, SIGNAL("capture"), self._show_capture_resource_message)
00059         self.connect(self, SIGNAL("release"), self._show_release_resource_message)
00060         self.connect(self, SIGNAL("error"), self._show_error_resource_message)
00061         self.connect(self, SIGNAL("refresh_resource_list"), self._refresh_resource_list)
00062 
00063     def _init_callbacks(self):
00064         self._callback = {}
00065         self._callback['capture_event'] = []
00066         self._callback['release_event'] = []
00067 
00068     def _resource_selected(self, item):
00069         self._lock.acquire()
00070         if not item in self.resource_item_list.keys():
00071             print "HAS NO KEY"
00072         else:
00073             self.current_resource = self.resource_item_list[item]
00074             if self.current_resource == self.current_captured_resource:
00075                 self.release_resource_btn.setEnabled(True)
00076             else:
00077                 self.release_resource_btn.setEnabled(False)
00078         self._lock.release()
00079 
00080     def _resource_double_clicked(self):
00081         if self.current_captured_resource == None:
00082             self._capture_resource()
00083         else:
00084             self._release_resource()
00085 
00086     def _capture_resource(self):
00087         if self.current_resource == None:
00088             print "No resource is selected"
00089         elif self.current_captured_resource:
00090             print "Already one robot has been captured"
00091         else:
00092             for callback in self._callback['capture_event']:
00093                 callback(self.current_resource)
00094 
00095             self.setDisabled(True)
00096 
00097     def _release_resource(self):
00098 
00099         if self.current_captured_resource == None:
00100             print "No resource has been captured yet!"
00101         elif self.current_captured_resource != self.current_resource:
00102             print "cannot release not captured robot.(Captured: %s)" % self.current_captured_resource['name'].string
00103         else:
00104             for callback in self._callback['release_event']:
00105                 callback(self.current_captured_resource)
00106             self.setDisabled(True)
00107 
00108     def capture_resource_callback(self, uri, msg):
00109         try:
00110             self.emit(SIGNAL("capture"), msg)
00111         except: 
00112             pass
00113 
00114     def release_resource_callback(self, uri, msg):
00115         try:
00116             self.emit(SIGNAL("release"), msg)
00117         except:
00118             pass
00119 
00120     def error_resource_callback(self, rtn):
00121         try:
00122             self.emit(SIGNAL("error"), rtn)
00123         except:
00124             pass
00125     
00126     def _show_capture_resource_message(self, rtn):
00127         if rtn:
00128             QMessageBox.warning(self, 'SUCCESS', "CAPTURE!!!!", QMessageBox.Ok | QMessageBox.Ok)
00129         else:
00130             QMessageBox.warning(self, 'FAIL', "FAIURE CAPTURE!!!!", QMessageBox.Ok | QMessageBox.Ok)
00131         self._lock.acquire()
00132         if rtn:
00133             for k in self.resource_item_list.keys():
00134                 if self.resource_item_list[k] == self.current_resource:
00135                     k.setBackground(0, QBrush(Qt.SolidPattern))
00136                     k.setBackgroundColor(0, QColor(0, 255, 0, 255))
00137                     resource_name = k.text(0)
00138                     k.setText(0, str(resource_name) + " (captured)")
00139                 else:
00140                     k.setBackground(0, QBrush(Qt.NoBrush))
00141                     k.setBackgroundColor(0, QColor(0, 0, 0, 0))
00142                     resource_name = k.text(0)
00143             self.capture_resource_btn.setEnabled(False)
00144             self.release_resource_btn.setEnabled(True)
00145             self.current_captured_resource = self.current_resource
00146         self.setDisabled(False)
00147         self._lock.release()
00148 
00149     def _show_release_resource_message(self, rtn):
00150         if rtn:
00151             QMessageBox.warning(self, 'SUCCESS', "RELEASE!!!!", QMessageBox.Ok | QMessageBox.Ok)
00152         else:
00153             QMessageBox.warning(self, 'FAIL', "FAIURE RELEASE!!!!", QMessageBox.Ok | QMessageBox.Ok)
00154 
00155         self._lock.acquire()
00156         if rtn:
00157             for k in self.resource_item_list.keys():
00158                 if self.resource_item_list[k] == self.current_captured_resource:
00159                     k.setBackground(0, QBrush(Qt.NoBrush))
00160                     k.setBackgroundColor(0, QColor(0, 0, 0, 0))
00161                     resource_name= k.text(0)
00162                     k.setText(0, resource_name[:resource_name.find(" (captured)")])
00163         self.setDisabled(False)
00164         self.capture_resource_btn.setEnabled(True)
00165         self.release_resource_btn.setEnabled(False)
00166         self.current_captured_resource= None
00167         self._lock.release()
00168 
00169     def _show_error_resource_message(self, error_message):
00170         QMessageBox.warning(self, 'ERROR', error_message, QMessageBox.Ok | QMessageBox.Ok)
00171         self.setDisabled(False)
00172         self.capture_resource_btn.setEnabled(True)
00173         self.release_resource_btn.setEnabled(True)
00174 
00175     def refresh_resource_list_callback(self, resource_list):
00176         try:
00177             self.emit(SIGNAL("refresh_resource_list"), resource_list)
00178         except: 
00179             pass
00180 
00181 
00182     def _refresh_resource_list(self, resource_list):
00183         self._lock.acquire()
00184         self.resource_list_tree_widget.clear()
00185         self.resource_item_list = {}
00186 
00187         for r in resource_list:
00188             uri = rocon_uri.parse(r)
00189             resource_item = QTreeWidgetItem(self.resource_list_tree_widget)
00190             resource_item.setText(0, uri.name.string)
00191             self.resource_item_list[resource_item] = r
00192         self._lock.release()
00193 
00194     def shutdown(self):
00195         self._release_resource()


rocon_qt_library
Author(s): Donguk Lee
autogenerated on Fri Feb 12 2016 02:50:13