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, Qt, QSize, QEvent
00015 from python_qt_binding.QtGui import QIcon, QWidget, QColor, QMainWindow, QMessageBox
00016
00017 from rocon_console import console
00018 import rocon_interactions.web_interactions as web_interactions
00019
00020 from rocon_remocon.interactive_client_interface import InteractiveClientInterface
00021 from . import utils
00022
00023
00024
00025
00026
00027 class QInteractionsChooser(QMainWindow):
00028
00029 def __init__(self, interactive_client_interface=None):
00030 self.binded_function = {}
00031 self.cur_selected_role = ''
00032 self.cur_selected_interaction = None
00033 self.interactions = {}
00034 self.interactive_client_interface = interactive_client_interface
00035 self.interactions_widget = QWidget()
00036
00037 rospack = rospkg.RosPack()
00038 path = os.path.join(rospack.get_path('rocon_remocon'), 'ui', 'interactions_list.ui')
00039 loadUi(path, self.interactions_widget)
00040
00041
00042 self.interactions_widget.interactions_list_widget.setIconSize(QSize(50, 50))
00043 self.interactions_widget.interactions_list_widget.itemDoubleClicked.connect(self._start_interaction)
00044 self.interactions_widget.back_btn.pressed.connect(self._back)
00045 self.interactions_widget.interactions_list_widget.itemClicked.connect(self._display_interaction_info)
00046 self.interactions_widget.stop_interactions_button.pressed.connect(self._stop_interaction)
00047 self.interactions_widget.stop_interactions_button.setDisabled(True)
00048 self.interactions_widget.closeEvent = self._close_event
00049
00050 console.logdebug('init QInteractionsChooser')
00051
00052 def _back(self):
00053 if 'back' in self.binded_function.keys() and self.binded_function['back'] is not None:
00054 self.binded_function['back']()
00055 else:
00056 console.logdebug("Interactions Chooser : None binded functione: shutdown")
00057
00058 def _close_event(self, event):
00059 """
00060 Re-implementation of close event handlers for the interaction chooser's children
00061 (i.e. role and interactions list widgets).
00062 """
00063 console.logdebug("Interactions Chooser : remocon shutting down.")
00064 if 'shutdown' in self.binded_function.keys() and self.binded_function['shutdown'] is not None:
00065 self.binded_function['shutdown']()
00066 else:
00067 console.logdebug("Interactions Chooser : None binded functione: shutdown")
00068
00069
00070
00071
00072 def _display_interaction_info(self, Item):
00073 """
00074 Display the currently selected interaction's information. Triggered
00075 when single-clicking on it in the interactions list view.
00076 """
00077 list_widget = Item.listWidget()
00078 cur_index = list_widget.count() - list_widget.currentRow() - 1
00079 for k in self.interactions.values():
00080 if(k.index == cur_index):
00081 self.cur_selected_interaction = k
00082 break
00083 self.interactions_widget.app_info.clear()
00084 info_text = "<html>"
00085 info_text += "<p>-------------------------------------------</p>"
00086 web_interaction = web_interactions.parse(self.cur_selected_interaction.name)
00087 name = self.cur_selected_interaction.name if web_interaction is None else web_interaction.url
00088 info_text += "<p><b>name: </b>" + name + "</p>"
00089 info_text += "<p><b> ---------------------</b>" + "</p>"
00090 info_text += "<p><b>compatibility: </b>" + self.cur_selected_interaction.compatibility + "</p>"
00091 info_text += "<p><b>display name: </b>" + self.cur_selected_interaction.display_name + "</p>"
00092 info_text += "<p><b>description: </b>" + self.cur_selected_interaction.description + "</p>"
00093 info_text += "<p><b>namespace: </b>" + self.cur_selected_interaction.namespace + "</p>"
00094 info_text += "<p><b>max: </b>" + str(self.cur_selected_interaction.max) + "</p>"
00095 info_text += "<p><b> ---------------------</b>" + "</p>"
00096 info_text += "<p><b>remappings: </b>" + str(self.cur_selected_interaction.remappings) + "</p>"
00097 info_text += "<p><b>parameters: </b>" + str(self.cur_selected_interaction.parameters) + "</p>"
00098 info_text += "</html>"
00099
00100 self.interactions_widget.app_info.appendHtml(info_text)
00101 self._set_stop_interactions_button()
00102
00103
00104
00105
00106
00107 def _set_stop_interactions_button(self):
00108 """
00109 Disable or enable the stop button depending on whether the
00110 selected interaction has any currently launched processes,
00111 """
00112 if not self.interactions:
00113 console.logwarn("No interactions")
00114 return
00115 if self.cur_selected_interaction.launch_list:
00116 console.logdebug("Interactions Chooser : enabling stop interactions button [%s]" % self.cur_selected_interaction.display_name)
00117 self.interactions_widget.stop_interactions_button.setEnabled(True)
00118 else:
00119 console.logdebug("Interactions Chooser : disabling stop interactions button [%s]" % self.cur_selected_interaction.display_name)
00120 self.interactions_widget.stop_interactions_button.setEnabled(False)
00121
00122
00123
00124
00125
00126 def _start_interaction(self):
00127 """
00128 Start selected interactions when user hits start button and does doubleclicking interaction item.
00129 The interactions can be launched in duplicate.
00130 """
00131 console.logdebug("Interactions Chooser : starting interaction [%s]" % str(self.cur_selected_interaction.name))
00132 (result, message) = self.interactive_client_interface.start_interaction(self.cur_selected_role,
00133 self.cur_selected_interaction.hash)
00134 if result:
00135 if self.cur_selected_interaction.is_paired_type():
00136 self.refresh_interactions_list()
00137 self.interactions_widget.stop_interactions_button.setDisabled(False)
00138 else:
00139 QMessageBox.warning(self.interactions_widget, 'Start Interaction Failed', "%s." % message.capitalize(), QMessageBox.Ok)
00140 console.logwarn("Interactions Chooser : start interaction failed [%s]" % message)
00141
00142 def _stop_interaction(self):
00143 """
00144 Stop running interactions when user hits `stop` or 'all stop interactions button` button.
00145 If no interactions is running, buttons are disabled.
00146 """
00147 console.logdebug("Interactions Chooser : stopping interaction %s " % str(self.cur_selected_interaction.name))
00148 (result, message) = self.interactive_client_interface.stop_interaction(self.cur_selected_interaction.hash)
00149 if result:
00150 if self.cur_selected_interaction.is_paired_type():
00151 self.refresh_interactions_list()
00152 self._set_stop_interactions_button()
00153
00154 else:
00155 QMessageBox.warning(self.interactions_widget, 'Stop Interaction Failed', "%s." % message.capitalize(), QMessageBox.Ok)
00156 console.logwarn("Interactions Chooser : stop interaction failed [%s]" % message)
00157
00158 def bind_function(self, name, function_handle):
00159 """
00160 Binding external function to map with ui button
00161 """
00162 self.binded_function[name] = function_handle
00163
00164 def show(self, pos=None):
00165 """
00166 Showing the interactions chooser
00167 """
00168 self.interactions_widget.show()
00169 if pos is not None:
00170 self.interactions_widget.move(pos)
00171
00172 if self.interactive_client_interface.has_running_interactions():
00173 self.interactions_widget.stop_interactions_button.setEnabled(True)
00174 else:
00175 self.interactions_widget.stop_interactions_button.setEnabled(False)
00176
00177 def hide(self):
00178 """
00179 Hiding the interactions chooser
00180 """
00181 self.interactions_widget.hide()
00182
00183 def select_role(self, role):
00184 """
00185 Take the selected role to get a list of interaction.
00186
00187 :param role: role name from role chooser.
00188 :type role: string
00189 """
00190 self.cur_selected_role = role
00191 self.interactive_client_interface.select_role(self.cur_selected_role)
00192 self.refresh_interactions_list()
00193
00194 def refresh_interactions_list(self):
00195 """
00196 This just does a complete redraw of the interactions with the
00197 currently selected role. It's a bit brute force doing this
00198 every time the interactions' 'state' changes, but this suffices for now.
00199 """
00200 console.logdebug("Interactions Chooser : refreshing the interactions list")
00201 self.interactions = self.interactive_client_interface.interactions(self.cur_selected_role)
00202 self.interactions_widget.interactions_list_widget.clear()
00203 index = 0
00204 for interaction in self.interactions.values():
00205 interaction.index = index
00206 index = index + 1
00207
00208 self.interactions_widget.interactions_list_widget.insertItem(0, interaction.display_name)
00209
00210
00211 if self.interactive_client_interface.pairing == interaction.hash:
00212 self.interactions_widget.interactions_list_widget.item(0).setBackground(QColor(100, 100, 150))
00213
00214
00215 font = self.interactions_widget.interactions_list_widget.item(0).font()
00216 font.setPointSize(13)
00217 self.interactions_widget.interactions_list_widget.item(0).setFont(font)
00218
00219
00220 icon = interaction.icon
00221 if icon == "unknown.png":
00222 icon = QIcon(self.icon_paths['unknown'])
00223 self.interactions_widget.interactions_list_widget.item(0).setIcon(icon)
00224 elif len(icon):
00225 icon = QIcon(os.path.join(utils.get_icon_cache_home(), icon))
00226 self.interactions_widget.interactions_list_widget.item(0).setIcon(icon)
00227 else:
00228 console.logdebug("%s : No icon" % str(self.rocon_master_name))