00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 from __future__ import division
00011 import os
00012
00013 import rospkg
00014 from python_qt_binding import loadUi
00015 from python_qt_binding.QtCore import Qt, QEvent, SIGNAL
00016 from python_qt_binding.QtGui import QDialog, QCursor, QSpacerItem
00017
00018 from rocon_qt_library.utils import show_message
00019 from .utils import get_qpixmap, create_label_textedit_pair, create_label
00020
00021 class QtRappDialog(QDialog):
00022
00023 def __init__(self, parent, rapp, start_rapp_hook, stop_rapp_hook, is_running=False):
00024 super(QtRappDialog, self).__init__(parent)
00025
00026 rospack = rospkg.RosPack()
00027 path = os.path.join(rospack.get_path('rocon_qt_app_manager'),'ui','app_dialog.ui')
00028 loadUi(path, self)
00029 self._rapp = rapp
00030 self._start_rapp_hook = start_rapp_hook
00031 self._stop_rapp_hook = stop_rapp_hook
00032 self._is_running = is_running
00033
00034 self._parameters_items = []
00035 self._remappings_items = []
00036
00037 self._init_rapp_infos()
00038
00039 self.setFocusPolicy(Qt.StrongFocus)
00040 self.installEventFilter(self)
00041
00042 def _init_rapp_infos(self):
00043 self._init_overview()
00044 self._init_start_stop_buttons()
00045 self._init_implementations()
00046 self._init_public_parameters()
00047 self._init_public_interface()
00048
00049
00050 def _init_overview(self):
00051 self.setWindowTitle(self._rapp['display_name'])
00052
00053 pixmap= get_qpixmap(self._rapp['icon'])
00054 self.rapp_icon.setPixmap(pixmap)
00055
00056 self.rapp_name.setText("%s(%s)"%(self._rapp['display_name'], self._rapp['name']))
00057 self.rapp_description.setText(self._rapp['description'])
00058
00059 def _init_start_stop_buttons(self):
00060 if self._is_running:
00061 self.start_button.setEnabled(False)
00062 self.stop_button.setEnabled(True)
00063 else:
00064 self.start_button.setEnabled(True)
00065 self.stop_button.setEnabled(False)
00066
00067 self.start_button.pressed.connect(self._press_start_button)
00068 self.stop_button.pressed.connect(self._press_stop_button)
00069
00070 def _init_implementations(self):
00071 for impl in self._rapp['implementations']:
00072 self.rapp_impls.addItem(impl)
00073
00074 idx = self.rapp_impls.findText(self._rapp['preferred'])
00075
00076 if idx >= 0:
00077 self.rapp_impls.setCurrentIndex(idx)
00078
00079
00080 def _init_public_parameters(self):
00081 if len(self._rapp['public_parameters']) > 0:
00082 self.parameters.setColumnStretch(1,0)
00083 self.parameters.setRowStretch(2,0)
00084 for p in self._rapp['public_parameters']:
00085 name, textedit= create_label_textedit_pair(p.key, p.value)
00086 self.parameters.addWidget(name)
00087 self.parameters.addWidget(textedit)
00088 self._parameters_items.append((p.key,textedit))
00089 else:
00090 label = create_label("No public parameters")
00091 self.parameters.addWidget(label)
00092
00093 def _init_public_interface(self):
00094 flag = False
00095 if len(self._rapp['public_interface']):
00096 self.remappings.setColumnStretch(1,0)
00097 self.remappings.setRowStretch(2,0)
00098
00099 for i in self._rapp['public_interface']:
00100 type_name = i.key
00101 remap_list = eval(i.value)
00102
00103 if len(remap_list) > 0:
00104 qname = create_label(type_name, is_bold=True)
00105 self.remappings.addWidget(qname)
00106 self.remappings.addItem(QSpacerItem(20,40))
00107 for remap in remap_list:
00108 key = remap['name']
00109 remap_type = remap['type']
00110 n = "%s [%s]"%(key, remap_type)
00111 name, textedit = create_label_textedit_pair(n, key)
00112 self.remappings.addWidget(name)
00113 self.remappings.addWidget(textedit)
00114 self._remappings_items.append((key, textedit))
00115 flag= True
00116
00117 if not flag:
00118 label = create_label("No public interface")
00119 self.remappings.addWidget(label)
00120
00121 def _press_start_button(self):
00122 name, remappings, parameters = self._prepare_start_rapp()
00123 result = self._start_rapp_hook(name, remappings, parameters)
00124
00125 if result.started:
00126 self.start_button.setEnabled(False)
00127 self.stop_button.setEnabled(True)
00128 show_message(self, str(result.started), result.message)
00129
00130 def _press_stop_button(self):
00131 result = self._stop_rapp_hook()
00132 if result.stopped:
00133 self.start_button.setEnabled(True)
00134 self.stop_button.setEnabled(False)
00135 show_message(self, str(result.stopped), result.message)
00136
00137 def _prepare_start_rapp(self):
00138
00139 parameters = [(k, v.toPlainText().strip()) for k, v in self._parameters_items]
00140 remappings = [(k, v.toPlainText().strip()) for k, v in self._remappings_items if k != v.toPlainText().strip()]
00141 impl = self.rapp_impls.currentText()
00142
00143 return impl, remappings, parameters
00144
00145
00146
00147 def showEvent(self, event):
00148 geom = self.frameGeometry()
00149 geom.moveTopLeft(QCursor.pos())
00150 self.setGeometry(geom)
00151 super(QDialog, self).showEvent(event)
00152
00153 def eventFilter(self, obj, event):
00154 if event.type() == QEvent.WindowDeactivate:
00155 self.close()
00156 return True
00157 return False