Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 import os
00036 import sys
00037
00038 from python_qt_binding import loadUi
00039 from python_qt_binding.QtCore import QModelIndex, Signal
00040 from python_qt_binding.QtGui import QDialog, QStandardItem, \
00041 QStandardItemModel
00042 from rosgraph import rosenv
00043 import roslaunch
00044 from roslaunch.core import RLException
00045 import rospkg
00046 import rospy
00047
00048
00049 from rqt_launch.node_proxy import NodeProxy
00050 from rqt_launch.node_controller import NodeController
00051 from rqt_launch.node_delegate import NodeDelegate
00052 from rqt_launch.status_indicator import StatusIndicator
00053 from rqt_py_common.rqt_roscomm_util import RqtRoscommUtil
00054
00055
00056 class LaunchWidget(QDialog):
00057 '''#TODO: comment
00058 '''
00059
00060
00061 sig_sysmsg = Signal(str)
00062
00063 def __init__(self, parent):
00064 '''
00065 @type parent: LaunchMain
00066 '''
00067 super(LaunchWidget, self).__init__()
00068 self._parent = parent
00069
00070 self._config = None
00071
00072
00073 self._port_roscore = 11311
00074
00075 self._run_id = None
00076
00077 self._rospack = rospkg.RosPack()
00078 ui_file = os.path.join(self._rospack.get_path('rqt_launch'),
00079 'resource', 'launch_widget.ui')
00080 loadUi(ui_file, self)
00081
00082
00083 self._datamodel = QStandardItemModel(0, 1)
00084
00085 master_uri = rosenv.get_master_uri()
00086 rospy.loginfo('LaunchWidget master_uri={}'.format(master_uri))
00087 self._delegate = NodeDelegate(master_uri,
00088 self._rospack)
00089 self._treeview.setModel(self._datamodel)
00090 self._treeview.setItemDelegate(self._delegate)
00091
00092
00093 self._node_controllers = []
00094
00095 self._pushbutton_start_all.clicked.connect(self._parent.start_all)
00096 self._pushbutton_stop_all.clicked.connect(self._parent.stop_all)
00097
00098 self._combobox_pkg.currentIndexChanged[str].connect(
00099 self._refresh_launchfiles)
00100
00101 self._combobox_launchfile_name.currentIndexChanged[str].connect(
00102 self._load_launchfile_slot)
00103 self._update_pkgs_contain_launchfiles()
00104
00105
00106 self._num_nodes_previous = 0
00107
00108 def _load_launchfile_slot(self, launchfile_name):
00109
00110
00111 if launchfile_name == None or launchfile_name == '':
00112 return
00113
00114 _config = None
00115
00116 rospy.logdebug('_load_launchfile_slot launchfile_name={}'.format(
00117 launchfile_name))
00118
00119 try:
00120 _config = self._create_launchconfig(launchfile_name,
00121 self._port_roscore)
00122
00123
00124
00125 except IndexError as e:
00126 msg = 'IndexError={} launchfile={}'.format(e.message,
00127 launchfile_name)
00128 rospy.logerr(msg)
00129 self.sig_sysmsg.emit(msg)
00130 return
00131 except RLException as e:
00132 msg = 'RLException={} launchfile={}'.format(e.message,
00133 launchfile_name)
00134 rospy.logerr(msg)
00135 self.sig_sysmsg.emit(msg)
00136 return
00137
00138 self._create_widgets_for_launchfile(_config)
00139
00140 def _create_launchconfig(self, launchfile_name, port_roscore=11311,
00141 folder_name_launchfile='launch'):
00142 '''
00143 @rtype: ROSLaunchConfig
00144 @raises RLException: raised by roslaunch.config.load_config_default.
00145 '''
00146
00147 pkg_name = self._combobox_pkg.currentText()
00148
00149 try:
00150 launchfile = os.path.join(self._rospack.get_path(pkg_name),
00151 folder_name_launchfile, launchfile_name)
00152 except IndexError as e:
00153 raise RLException('IndexError: {}'.format(e.message))
00154
00155 try:
00156 launch_config = roslaunch.config.load_config_default([launchfile],
00157 port_roscore)
00158 except rospkg.common.ResourceNotFound as e:
00159 raise RLException('ResourceNotFound: {}'.format(e.message))
00160 except RLException as e:
00161 raise e
00162
00163 return launch_config
00164
00165 def _create_widgets_for_launchfile(self, config):
00166 self._config = config
00167
00168
00169 self._node_controllers = []
00170
00171
00172
00173
00174
00175
00176 self._datamodel.beginResetModel()
00177 self._datamodel.endResetModel()
00178
00179
00180
00181 order_xmlelement = 0
00182
00183 for order_xmlelement, node in enumerate(self._config.nodes):
00184 proxy = NodeProxy(self._run_id, self._config.master.uri, node)
00185
00186
00187 status_label = StatusIndicator()
00188
00189 qindex_nodewidget = self._datamodel.index(order_xmlelement,
00190 0, QModelIndex())
00191 node_widget = self._delegate.create_node_widget(qindex_nodewidget,
00192 proxy.config,
00193 status_label)
00194
00195
00196
00197 std_item = QStandardItem(
00198
00199 )
00200 self._datamodel.setItem(order_xmlelement, 0, std_item)
00201
00202
00203 self._treeview.setIndexWidget(qindex_nodewidget, node_widget)
00204
00205 node_controller = NodeController(proxy, node_widget)
00206 self._node_controllers.append(node_controller)
00207
00208 node_widget.connect_start_stop_button( \
00209 node_controller.start_stop_slot)
00210 rospy.logdebug('loop #%d proxy.config.namespace=%s ' +
00211 'proxy.config.name=%s',
00212 order_xmlelement, proxy.config.namespace,
00213 proxy.config.name)
00214
00215 self._num_nodes_previous = order_xmlelement
00216
00217 self._parent.set_node_controllers(self._node_controllers)
00218
00219 def _update_pkgs_contain_launchfiles(self):
00220 '''
00221 Inspired by rqt_msg.MessageWidget._update_pkgs_contain_launchfiles
00222 '''
00223 packages = sorted([pkg_tuple[0]
00224 for pkg_tuple
00225 in RqtRoscommUtil.iterate_packages('launch')])
00226 self._package_list = packages
00227 rospy.logdebug('pkgs={}'.format(self._package_list))
00228 self._combobox_pkg.clear()
00229 self._combobox_pkg.addItems(self._package_list)
00230 self._combobox_pkg.setCurrentIndex(0)
00231
00232 def _refresh_launchfiles(self, package=None):
00233 '''
00234 Inspired by rqt_msg.MessageWidget._refresh_msgs
00235 '''
00236 if package is None or len(package) == 0:
00237 return
00238 self._launchfile_instances = []
00239
00240
00241 _launch_instance_list = RqtRoscommUtil.list_files(package,
00242 'launch')
00243
00244 rospy.logdebug('_refresh_launches package={} instance_list={}'.format(
00245 package,
00246 _launch_instance_list))
00247
00248 self._launchfile_instances = [x.split('/')[1]
00249 for x in _launch_instance_list]
00250
00251 self._combobox_launchfile_name.clear()
00252 self._combobox_launchfile_name.addItems(self._launchfile_instances)
00253
00254 def save_settings(self, plugin_settings, instance_settings):
00255
00256 pass
00257
00258 def restore_settings(self, plugin_settings, instance_settings):
00259
00260
00261
00262
00263 pass
00264
00265
00266 if __name__ == '__main__':
00267
00268
00269 from rqt_gui.main import Main
00270
00271 main = Main()
00272 sys.exit(main.main(sys.argv, standalone='rqt_launch'))