00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 from __future__ import division
00011 import os
00012
00013 from python_qt_binding import loadUi
00014 from python_qt_binding.QtCore import Qt, Signal, SIGNAL, SLOT
00015 from python_qt_binding.QtGui import QWidget, QTreeWidgetItem, QTextEdit, QPushButton, QPlainTextEdit, QGridLayout, QLabel
00016
00017
00018 import rospkg
00019
00020 from qt_gui.plugin import Plugin
00021 import rocon_qt_library.utils as utils
00022
00023 from .admin_app_interface import AdminAppInterface
00024 from rocon_python_comms.exceptions import NotFoundException
00025
00026
00027
00028
00029
00030
00031 class AdminApp(Plugin):
00032
00033 _refresh_service_list_signal = Signal()
00034
00035 def __init__(self, context):
00036 self._context = context
00037 super(AdminApp, self).__init__(context)
00038 self.initialised = False
00039 self.setObjectName('Admin App')
00040
00041 self.is_setting_dlg_live = False
00042 self._widget = QWidget()
00043
00044
00045 rospack = rospkg.RosPack()
00046 ui_file = os.path.join(rospack.get_path('concert_admin_app'), 'ui', 'admin_app.ui')
00047 self._widget.setObjectName('AdminApphUi')
00048 loadUi(ui_file, self._widget, {})
00049
00050 if context.serial_number() > 1:
00051 self._widget.setWindowTitle(self._widget.windowTitle() + (' (%d)' % context.serial_number()))
00052
00053 self.current_service = None
00054
00055 self.params_layout = None
00056 self.params_layout_items = []
00057
00058 self._init_admin_app_interface()
00059 self._init_event()
00060 self._init_widget()
00061 context.add_widget(self._widget)
00062
00063 def _init_admin_app_interface(self):
00064 self.admin_app_interface = AdminAppInterface()
00065 try:
00066 self.admin_app_interface._init_admin_app_interface()
00067 self.admin_app_interface._reg_event_callback(self._refresh_service)
00068 except NotFoundException as e:
00069 self.emit(SIGNAL("show_message"), self._widget, "Failed", e)
00070
00071 def _init_event(self):
00072 self._widget.enable_disable_btn.pressed.connect(self._toggle_service)
00073 self._widget.refresh_btn.pressed.connect(self._refresh_service)
00074 self._widget.save_configuration_btn.pressed.connect(self._save_parmeters)
00075 self._widget.service_tree_widget.itemClicked.connect(self._select_service_tree_item)
00076 self._refresh_service_list_signal.connect(self._update_service_list)
00077 self.connect(self, SIGNAL("show_message"), utils.show_message)
00078
00079 def _init_widget(self):
00080 self.params_layout = self._widget.findChildren(QGridLayout, "params_layout")[0]
00081 self.params_layout_items = []
00082
00083 self._update_service_list()
00084
00085 if not self.current_service:
00086 self._widget.enable_disable_btn.setDisabled(True)
00087
00088 def _save_parmeters(self):
00089 params = {}
00090 result = False
00091 msg = ""
00092 if not self.current_service:
00093 msg = "Not selected service"
00094 elif self.current_service['enabled']:
00095 msg = "service is already enabled!"
00096 elif self.params_layout_items:
00097 for item in self.params_layout_items:
00098 params[item[0].text()] = item[1].toPlainText().strip()
00099 (result, msg) = self.admin_app_interface.set_srv_parameters(self.current_service['name'], params)
00100 else:
00101 msg = "No params infomation"
00102
00103 if result:
00104 self.emit(SIGNAL("show_message"), self._widget, "Success", "Saved Parameters")
00105 else:
00106 self.emit(SIGNAL("show_message"), self._widget, "Failed", msg)
00107
00108 def _toggle_service(self):
00109 if self.current_service['enabled']:
00110 print "Disable Service: %s" % self.current_service['name']
00111 (success, message) = self.admin_app_interface.disable_service(self.current_service['name'])
00112 self._set_enable_params_layout(True)
00113 else:
00114 print "Enable Service: %s" % self.current_service['name']
00115 (success, message) = self.admin_app_interface.eable_service(self.current_service['name'])
00116 self._set_enable_params_layout(False)
00117
00118 if success:
00119 self.emit(SIGNAL("show_message"), self._widget, "Success", message)
00120 else:
00121 self.emit(SIGNAL("show_message"), self._widget, "Failure", message)
00122
00123 self.current_service = None
00124 self._widget.enable_disable_btn.setDisabled(True)
00125 self._widget.enable_disable_btn.setText("Enable/Disable")
00126
00127 def _refresh_service(self):
00128 self._refresh_service_list_signal.emit()
00129
00130 def _update_service_list(self):
00131 self._widget.service_tree_widget.clear()
00132 self._widget.service_info_text.clear()
00133 self._widgetitem_service_pair = {}
00134 service_list = self.admin_app_interface.service_list
00135
00136 for k in service_list.values():
00137
00138 service_item = QTreeWidgetItem(self._widget.service_tree_widget)
00139
00140 service_item.setText(0, k['name'])
00141
00142
00143 font = service_item.font(0)
00144 font.setPointSize(20)
00145 font.setBold(True)
00146 service_item.setFont(0, font)
00147
00148 self._widgetitem_service_pair[service_item] = k
00149
00150 def _set_service_info(self, service_name):
00151 service_list = self.admin_app_interface.service_list
00152 self._widget.service_info_text.clear()
00153 self._widget.service_info_text.appendHtml(service_list[service_name]['context'])
00154
00155 def _select_service_tree_item(self, item):
00156 if item.parent() == None:
00157 selected_service = self._widgetitem_service_pair[item]
00158
00159 print '_select_service: ' + selected_service['name']
00160 self._set_service_info(selected_service['name'])
00161 self.current_service = selected_service
00162 self._set_parameter_layout()
00163
00164 if self.current_service['enabled']:
00165 self._widget.enable_disable_btn.setText("Disable")
00166 self._widget.enable_disable_btn.setDisabled(False)
00167 else:
00168 self._widget.enable_disable_btn.setText("Enable")
00169 self._widget.enable_disable_btn.setDisabled(False)
00170
00171 def _set_parameter_layout(self):
00172
00173 params = self.admin_app_interface.get_srv_parameters(self.current_service['parameters_detail'])
00174 is_enabled = self.current_service['enabled']
00175
00176 if self.params_layout_items:
00177 for item in self.params_layout_items:
00178 self.params_layout.removeWidget(item[0])
00179 self.params_layout.removeWidget(item[1])
00180 item[0].setParent(None)
00181 item[1].setParent(None)
00182 self.params_layout_items = []
00183
00184 if params:
00185 self.params_layout.setColumnStretch(1, 0)
00186 self.params_layout.setRowStretch(2, 0)
00187 for param in params.keys():
00188 label = QLabel(param)
00189 self.params_layout.addWidget(label)
00190 value = QTextEdit(str(params[param]))
00191 value.setMaximumHeight(30)
00192 self.params_layout.addWidget(value)
00193 self.params_layout_items.append((label, value))
00194
00195 self._set_enable_params_layout(not is_enabled)
00196
00197 def _set_enable_params_layout(self, enable):
00198 if self.params_layout_items:
00199 self._widget.save_configuration_btn.setEnabled(enable)
00200 for item in self.params_layout_items:
00201 item[0].setEnabled(enable)
00202 item[1].setEnabled(enable)