Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 from __future__ import division
00011 import os
00012
00013
00014 from python_qt_binding import loadUi
00015 from python_qt_binding.QtCore import Signal, QSize, SIGNAL
00016 from python_qt_binding.QtGui import QListView, QWidget, QStandardItemModel, QStandardItem, QIcon, QPixmap
00017
00018 import rospkg
00019 from qt_app_manager_info import QtRappManagerInfo
00020
00021 from qt_gui.plugin import Plugin
00022
00023
00024 from rocon_qt_library.utils import show_message
00025 from .utils import QRappItem
00026 from .qt_rapp_status_dialog import QtRappDialog
00027
00028
00029
00030
00031
00032
00033
00034 class QtRappManager(Plugin):
00035 _update_rapps_signal = Signal()
00036
00037 def __init__(self, context):
00038 self._context = context
00039 super(QtRappManager, self).__init__(context)
00040
00041 self._init_ui(context)
00042 self._init_events()
00043 self._init_variables()
00044 self.spin()
00045
00046 def _init_ui(self, context):
00047 self._widget = QWidget()
00048 self.setObjectName('QtRappManger')
00049
00050 rospack = rospkg.RosPack()
00051 ui_file = os.path.join(rospack.get_path('rocon_qt_app_manager'), 'ui', 'qt_rapp_manager.ui')
00052 self._widget.setObjectName('QtRappManger')
00053 loadUi(ui_file, self._widget, {})
00054
00055 if context.serial_number() > 1:
00056 self._widget.setWindowTitle(self._widget.windowTitle() + (' (%d)' % context.serial_number()))
00057 context.add_widget(self._widget)
00058
00059
00060 self._widget.rapp_grid.setViewMode(QListView.IconMode)
00061
00062
00063 def _init_events(self):
00064
00065 self._widget.namespace_cbox.currentIndexChanged.connect(self._change_namespace)
00066
00067
00068 self._widget.rapp_grid.clicked.connect(self._rapp_single_click)
00069
00070 self._widget.rapp_grid.doubleClicked.connect(self._rapp_double_click)
00071
00072 def _init_variables(self):
00073 self.initialised = False
00074
00075
00076 self._qt_rapp_manager_info = QtRappManagerInfo(self._refresh_rapps)
00077 self._update_rapps_signal.connect(self._update_rapp_list)
00078
00079 self._rapp_view_model = QStandardItemModel()
00080 self._widget.rapp_grid.setModel(self._rapp_view_model)
00081 self._widget.rapp_grid.setWrapping(True)
00082 self._widget.rapp_grid.setIconSize(QSize(60,60))
00083 self._widget.rapp_grid.setSpacing(10)
00084
00085 self._selected_rapp = None
00086
00087 def spin(self):
00088 self._get_appmanager_namespaces()
00089
00090 def _cleanup_rapps(self):
00091 self._rapp_view_model.clear()
00092 pass
00093
00094 def _get_appmanager_namespaces(self):
00095 namespaces = self._qt_rapp_manager_info._get_namespaces()
00096 for namespace in namespaces:
00097 ns = namespace[:namespace.find('list_rapps')]
00098 self._widget.namespace_cbox.addItem(ns)
00099
00100 def _exit(self):
00101 pass
00102
00103
00104
00105
00106 def _change_namespace(self, event):
00107 self._qt_rapp_manager_info.select_rapp_manager(self._widget.namespace_cbox.currentText())
00108
00109 def _refresh_rapps(self):
00110 """
00111 Updates from qt_app_manager_info.
00112 """
00113 self._update_rapps_signal.emit()
00114
00115 def _update_rapp_list(self):
00116 """
00117 Rapp manager namespace event
00118 """
00119 self._cleanup_rapps()
00120 available_rapps = self._qt_rapp_manager_info.get_available_rapps()
00121 running_rapps = self._qt_rapp_manager_info.get_running_rapps()
00122
00123 rapp_items = []
00124 for r, v in available_rapps.items():
00125 if r in running_rapps.keys():
00126 item = QRappItem(v, running=True)
00127 else:
00128 item = QRappItem(v, running=False)
00129 self._rapp_view_model.appendRow(item)
00130
00131
00132 def _rapp_single_click(self, index):
00133 qrapp = self._rapp_view_model.item(index.row())
00134 rapp = qrapp.getRapp()
00135 self._create_rapp_dialog(rapp)
00136
00137 def _create_rapp_dialog(self, rapp):
00138 is_running = self._qt_rapp_manager_info.is_running_rapp(rapp)
00139 self._selected_rapp = rapp
00140 self._dialog = QtRappDialog(self._widget,rapp, self._qt_rapp_manager_info.start_rapp, self._qt_rapp_manager_info.stop_rapp, is_running)
00141 self._dialog.show()
00142
00143 def _rapp_double_click(self, item):
00144 running_rapps = self._qt_rapp_manager_info.get_running_rapps()
00145 if len(running_rapps) > 0:
00146 names = [r['display_name'] for r in running_rapps.values()]
00147 show_message(self._widget, "Error", "Rapp %s are already running"%names)
00148 else:
00149 self._start_rapp()
00150
00151 def _start_rapp(self):
00152 result = self._qt_rapp_manager_info.start_rapp(self._selected_rapp['name'], [], self._selected_rapp['public_parameters'])
00153 show_message(self._widget, str(result.started), result.message)
00154 self._selected_rapp = None
00155 return result
00156
00157 def _stop_rapp(self):
00158 result = self._qt_rapp_manager_info.stop_rapp()
00159 show_message(self._widget, str(result.stopped), result.message)
00160 self._selected_rapp = None
00161 return result
00162
00163
00164
00165
00166 def _select_rapp_tree_item(self, item):
00167 if not item in self.rapps.keys():
00168 print "HAS NO KEY"
00169 else:
00170 self.current_rapp = self.rapps[item]
00171 self._widget.rapp_info_text.clear()
00172 rapp_info = self.qt_rapp_manager_info._get_rapp_info(self.current_rapp)
00173 self._widget.rapp_info_text.appendHtml(rapp_info)
00174 self._update_rapp_parameter_layout(self.current_rapp)
00175 self._update_implementation_tree(self.current_rapp)
00176
00177 def _update_rapp_parameter_layout(self, rapp):
00178 parameters_layout = self._widget.rapp_parameter_layout
00179 clear_layout(parameters_layout)
00180
00181 for param in rapp['public_parameters']:
00182 one_param_layout = create_label_textedit_pair(param.key, param.value)
00183 parameters_layout.addLayout(one_param_layout)
00184
00185 def _update_implementation_tree(self, rapp):
00186 self._widget.implementation_tree_widget.clear()
00187 self.selected_impl = None
00188 self.impls = {}
00189 for impl in rapp['implementations']:
00190 impl_item = QTreeWidgetItem(self._widget.implementation_tree_widget)
00191 impl_item.setText(0, impl)
00192 self.impls[impl_item] = impl
00193
00194 def _select_implementation_tree_item(self, item):
00195 if not item in self.impls.keys():
00196 print "HAS NO KEY"
00197 else:
00198 self.selected_impl = self.impls[item]
00199
00200 def _get_public_parameters(self):
00201 public_parameters = {}
00202 parameters_layout = self._widget.rapp_parameter_layout
00203 for i in reversed(range(parameters_layout.count())):
00204 item = parameters_layout.itemAt(i)
00205 key_label = item.itemAt(0).widget()
00206 value_textbox = item.itemAt(1).widget()
00207 public_parameters[key_label.text()] = str(value_textbox.toPlainText())
00208 return public_parameters