interactive_client_ui.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 
00010 import os
00011 import rospkg
00012 
00013 from python_qt_binding import loadUi
00014 from python_qt_binding.QtCore import Signal, Qt, QSize, QEvent
00015 from python_qt_binding.QtGui import QIcon, QWidget, QMainWindow, QVBoxLayout, QMessageBox
00016 
00017 from rocon_console import console
00018 import rocon_interactions.web_interactions as web_interactions
00019 
00020 from . import utils
00021 from .interactive_client_interface import InteractiveClientInterface
00022 from .master_chooser import QMasterChooser
00023 from .role_chooser import QRoleChooser
00024 from .interactions_chooser import QInteractionsChooser
00025 
00026 ##############################################################################
00027 # Interactive Client UI
00028 ##############################################################################
00029 
00030 
00031 class InteractiveClientUI(QMainWindow):
00032 
00033     # pyqt signals are always defined as class attributes
00034     signal_interactions_updated = Signal()
00035 
00036     def __init__(self, parent, title, application, rocon_master_uri='localhost', host_name='localhost', with_rqt=False):
00037         super(InteractiveClientUI, self).__init__(parent)
00038         self.rocon_master_uri = rocon_master_uri
00039         self.host_name = host_name
00040         self.with_rqt = with_rqt
00041         self.cur_selected_interaction = None
00042         self.cur_selected_role = 0
00043         self.interactions = {}
00044 
00045         # desktop taskbar icon
00046         self.application = application
00047         if self.application:
00048             rospack = rospkg.RosPack()
00049             icon_file = os.path.join(rospack.get_path('rocon_icons'), 'icons', 'rocon_logo.png')
00050             self.application.setWindowIcon(QIcon(icon_file))
00051 
00052         # create a few directories for caching icons and ...
00053         utils.setup_home_dirs()
00054 
00055         # connect to the ros master with init node
00056         self.interactive_client_interface = InteractiveClientInterface(stop_interaction_postexec_fn=lambda: self.signal_interactions_updated.emit())
00057 
00058         if self.with_rqt:
00059             (result, message) = self.interactive_client_interface._connect(self.rocon_master_uri, self.host_name)
00060         else:
00061             (result, message) = self.interactive_client_interface._connect_with_ros_init_node(self.rocon_master_uri, self.host_name)
00062 
00063         if not result:
00064             QMessageBox.warning(self, 'Connection Failed', "%s." % message.capitalize(), QMessageBox.Ok)
00065             self._switch_to_master_chooser()
00066             return
00067 
00068         # interactive_client_ui widget setting
00069         self._interactive_client_ui_widget = QWidget()
00070         self._interactive_client_ui_layout = QVBoxLayout()
00071 
00072         self._role_chooser = QRoleChooser(self.interactive_client_interface, with_rqt)
00073         self._role_chooser.bind_function('shutdown', self._switch_to_master_chooser)
00074         self._role_chooser.bind_function('back', self._switch_to_master_chooser)
00075         self._role_chooser.bind_function('select_role', self._switch_to_interactions_list)
00076 
00077         self._interactions_chooser = QInteractionsChooser(self.interactive_client_interface)
00078         self._interactions_chooser.bind_function('shutdown', self._switch_to_master_chooser)
00079         self._interactions_chooser.bind_function('back', self._switch_to_role_list)
00080 
00081         self._interactive_client_ui_layout.addWidget(self._interactions_chooser.interactions_widget)
00082         self._interactive_client_ui_layout.addWidget(self._role_chooser.roles_widget)
00083         self._interactive_client_ui_widget.setLayout(self._interactive_client_ui_layout)
00084 
00085         self.signal_interactions_updated.connect(self.interactions_updated_relay)
00086 
00087         self._init()
00088 
00089     def _init(self):
00090         """
00091         Initialization of interactive client UI. First, show the role chooser and hide interactions chooser.
00092         """
00093         if (len(self._role_chooser.role_list) != 1):
00094             self._interactive_client_ui_widget.setWindowTitle('Role Chooser')
00095             self._interactive_client_ui_widget.show()
00096             self._role_chooser.show()
00097             self._interactions_chooser.hide()
00098         else:
00099             self._switch_to_interactions_list()
00100 
00101     def get_main_ui_handle(self):
00102         """
00103         Returning an instance of interactive client ui widget. It is used to handle it in other widget.
00104 
00105         :return: return main qt widget.
00106         :rtype: python_qt_binding.QtGui.QWidget
00107         """
00108         return self._interactive_client_ui_widget
00109 
00110     def shutdown(self):
00111         """
00112         Public method to enable shutdown of the script - this function is primarily for
00113         shutting down the InteractiveClientUI from external signals (e.g. CTRL-C on the command
00114         line).
00115         """
00116         self.interactive_client_interface.shutdown()
00117 
00118     def _switch_to_master_chooser(self):
00119         """
00120         Switch to master chooser from role chooser. If it was launced by rqt or rqt standalone, It is just shutdown.
00121         """
00122         self.shutdown()
00123         if not self.with_rqt:
00124             console.logdebug("InteractiveClientUI : switching back to the master chooser")
00125             os.execv(QMasterChooser.rocon_remocon_script, ['', self.host_name])
00126 
00127     def _switch_to_interactions_list(self):
00128         """
00129         Take the selected role and switch to an interactions view of that role.
00130         """
00131         console.logdebug("InteractiveClientUI : switching to the interactions list")
00132         self._interactive_client_ui_widget.setWindowTitle('Interactions Chooser')
00133         self._interactions_chooser.select_role(self._role_chooser.cur_selected_role)
00134         self._interactions_chooser.show(self._role_chooser.pos())
00135         self._role_chooser.hide()
00136 
00137     def _switch_to_role_list(self):
00138         """
00139         Switch to role chooser from interactions chooser.
00140         """
00141         self._interactive_client_ui_widget.setWindowTitle('Role Chooser')
00142         console.logdebug("InteractiveClientUI : switching to the role list")
00143         self._role_chooser.show(self._interactions_chooser.interactions_widget.pos())
00144         self._interactions_chooser.hide()
00145 
00146     def interactions_updated_relay(self):
00147         """
00148         Called by the underlying interactive client whenever the gui needs to be updated with
00149         fresh information. Using this relay saves us from having to embed qt functions in the
00150         underlying class but makes sure we signal across threads so the gui can update things
00151         in its own thread.
00152 
00153         Currently this only handles updates caused by termination of an interaction. If we wished to
00154         handle additional situations, we should use an argument here indicating what kind of interaction
00155         update occurred.
00156         """
00157         console.logdebug("InteractiveClientUI : interactions_updated_relay")
00158         self._interactions_chooser.refresh_interactions_list()
00159         self._role_chooser.refresh_role_list()


rocon_remocon
Author(s): Daniel Stonier, Donguk Lee
autogenerated on Fri Feb 12 2016 02:50:18