Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 import os
00011 import rospkg
00012
00013 from python_qt_binding import loadUi
00014 from python_qt_binding.QtCore import Signal, QSize
00015 from python_qt_binding.QtGui import QWidget
00016
00017 from rocon_console import console
00018 from rocon_remocon.interactive_client_interface import InteractiveClientInterface
00019 from . import utils
00020
00021
00022
00023
00024
00025
00026 class QRoleChooser():
00027
00028
00029
00030 def __init__(self, interactive_client_interface=None, with_rqt=False):
00031
00032 self.interactive_client_interface = interactive_client_interface
00033 self.with_rqt = with_rqt
00034 self.binded_function = {}
00035 self.role_list = []
00036 self.cur_selected_role = ''
00037 self.roles_widget = QWidget()
00038
00039 rospack = rospkg.RosPack()
00040 path = os.path.join(rospack.get_path('rocon_remocon'), 'ui', 'role_list.ui')
00041 loadUi(path, self.roles_widget)
00042
00043
00044 self.roles_widget.role_list_widget.setIconSize(QSize(50, 50))
00045 self.roles_widget.role_list_widget.itemDoubleClicked.connect(self._select_role)
00046 self.roles_widget.refresh_btn.pressed.connect(self.refresh_role_list)
00047 self.roles_widget.back_btn.pressed.connect(self._back)
00048 self.roles_widget.stop_all_interactions_button.pressed.connect(self._stop_all_interactions)
00049 self.roles_widget.closeEvent = self._close_event
00050 self._init()
00051
00052 def _init(self):
00053 """
00054 Initialization of role chooser. It it launced with rqt, the back button is disabled.
00055 Viewer of interactions chooser is launched at once when the role list has one role.
00056 """
00057 if self.with_rqt:
00058 self.roles_widget.back_btn.setEnabled(False)
00059 self.refresh_role_list()
00060 if len(self.role_list) == 1:
00061 self.cur_selected_role = self.role_list[0]
00062 self.interactive_client_interface.select_role(self.cur_selected_role)
00063
00064 def _back(self):
00065 """
00066 Public method to enable shutdown of the script - this function is primarily for
00067 shutting down the Role chooser from external signals (e.g. CTRL-C on the command
00068 line).
00069 """
00070 console.logdebug("Role Chooser : Back")
00071 if 'back' in self.binded_function.keys() and self.binded_function['back'] is not None:
00072 self.binded_function['back']()
00073
00074 def _close_event(self, event):
00075 """
00076 Re-implementation of close event handlers for the interaction chooser's children
00077 (i.e. role and interactions list widgets).
00078 """
00079 console.logdebug("Role Chooser : Role Chooser shutting down.")
00080 self._back()
00081
00082 def _select_role(self, item):
00083 """
00084 Take the selected role to switch interactions viewer as it.
00085
00086 :param item: qt list widget item of selected role. The user does double click on item wanted to launch
00087 :type item: python_qt_binding.QtGui.QListWidgetItem
00088 """
00089 console.logdebug("Role Chooser : switching to the interactions list")
00090 self.cur_selected_role = str(item.text())
00091 if 'select_role' in self.binded_function.keys() and self.binded_function['select_role'] is not None:
00092 self.binded_function['select_role']()
00093
00094 def _stop_all_interactions(self):
00095 """
00096 Stopping all running interactions. If no interactions is running, stop interactions button is disables.
00097 """
00098 console.logdebug("Role Chooser : stopping all running interactions")
00099 self.interactive_client_interface.stop_all_interactions()
00100 self.roles_widget.stop_all_interactions_button.setEnabled(False)
00101
00102 def bind_function(self, name, function_handle):
00103 """
00104 Binding external function to map with ui button
00105 """
00106 self.binded_function[name] = function_handle
00107
00108 def show(self, pos=None):
00109 """
00110 Showing the role chooser with rereshing role list
00111 """
00112
00113 self.roles_widget.show()
00114 if pos is not None:
00115 self.roles_widget.move(pos)
00116 self.refresh_role_list()
00117
00118 def hide(self):
00119 """
00120 Hiding the role chooser to show other widget
00121 """
00122 self.roles_widget.hide()
00123
00124 def pos(self):
00125 """
00126 Postion of role chooser
00127
00128 :return: xy position on desktop
00129 :rtype: python_qt_binding.QtCore.QPoint
00130 """
00131 return self.roles_widget.pos()
00132
00133 def refresh_role_list(self):
00134 """
00135 Update a list of roles. define status of all interaction stop button as checking running interactions.
00136 """
00137 if self.interactive_client_interface.has_running_interactions():
00138 self.roles_widget.stop_all_interactions_button.setEnabled(True)
00139 else:
00140 self.roles_widget.stop_all_interactions_button.setEnabled(False)
00141
00142 self.roles_widget.role_list_widget.clear()
00143 self.role_list = self.interactive_client_interface.get_role_list()
00144
00145 for role in reversed(self.role_list):
00146 self.roles_widget.role_list_widget.insertItem(0, role)
00147
00148 font = self.roles_widget.role_list_widget.item(0).font()
00149 font.setPointSize(13)
00150 self.roles_widget.role_list_widget.item(0).setFont(font)