Package node_manager_fkie :: Module run_dialog
[frames] | no frames]

Source Code for Module node_manager_fkie.run_dialog

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2012, Fraunhofer FKIE/US, Alexander Tiderko 
  4  # All rights reserved. 
  5  # 
  6  # Redistribution and use in source and binary forms, with or without 
  7  # modification, are permitted provided that the following conditions 
  8  # are met: 
  9  # 
 10  #  * Redistributions of source code must retain the above copyright 
 11  #    notice, this list of conditions and the following disclaimer. 
 12  #  * Redistributions in binary form must reproduce the above 
 13  #    copyright notice, this list of conditions and the following 
 14  #    disclaimer in the documentation and/or other materials provided 
 15  #    with the distribution. 
 16  #  * Neither the name of Fraunhofer nor the names of its 
 17  #    contributors may be used to endorse or promote products derived 
 18  #    from this software without specific prior written permission. 
 19  # 
 20  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 21  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 22  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 23  # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 24  # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 25  # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 26  # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 27  # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 28  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 29  # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 30  # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  # POSSIBILITY OF SUCH DAMAGE. 
 32   
 33  from PySide import QtGui 
 34  from PySide import QtCore 
 35   
 36  import os 
 37   
 38  import node_manager_fkie as nm 
 39  from detailed_msg_box import WarningMessageBox 
 40   
 41   
42 -class RunDialog(QtGui.QDialog):
43 ''' 44 A dialog to run a ROS node without configuration 45 ''' 46
47 - def __init__(self, host, parent=None):
48 QtGui.QDialog.__init__(self, parent) 49 self.host = host 50 self.setWindowTitle('Run') 51 self.verticalLayout = QtGui.QVBoxLayout(self) 52 self.verticalLayout.setObjectName("verticalLayout") 53 54 self.content = QtGui.QWidget() 55 self.contentLayout = QtGui.QFormLayout(self.content) 56 self.contentLayout.setVerticalSpacing(0) 57 self.verticalLayout.addWidget(self.content) 58 59 # fill the input fields 60 self.root_paths = [os.path.normpath(p) for p in os.getenv("ROS_PACKAGE_PATH").split(':')] 61 self.packages = {} 62 for p in self.root_paths: 63 ret = self._getPackages(p) 64 self.packages = dict(ret.items() + self.packages.items()) 65 66 package_label = QtGui.QLabel("Package:", self.content) 67 self.package_field = QtGui.QComboBox(self.content) 68 self.package_field.setInsertPolicy(QtGui.QComboBox.InsertAlphabetically) 69 self.package_field.setSizePolicy(QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Fixed)) 70 self.package_field.setEditable(True) 71 self.contentLayout.addRow(package_label, self.package_field) 72 binary_label = QtGui.QLabel("Binary:", self.content) 73 self.binary_field = QtGui.QComboBox(self.content) 74 # self.binary_field.setSizeAdjustPolicy(QtGui.QComboBox.AdjustToContents) 75 self.binary_field.setSizePolicy(QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Fixed)) 76 self.binary_field.setEditable(True) 77 self.contentLayout.addRow(binary_label, self.binary_field) 78 ns_name_label = QtGui.QLabel("NS/Name:", self.content) 79 self.ns_field = QtGui.QComboBox(self.content) 80 self.ns_field.setSizePolicy(QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Fixed)) 81 self.ns_field.setEditable(True) 82 ns_history = nm.history().cachedParamValues('run_dialog/NS') 83 ns_history.insert(0, '/') 84 self.ns_field.addItems(ns_history) 85 self.name_field = QtGui.QLineEdit(self.content) 86 self.name_field.setEnabled(False) 87 horizontalLayout = QtGui.QHBoxLayout() 88 horizontalLayout.addWidget(self.ns_field) 89 horizontalLayout.addWidget(self.name_field) 90 self.contentLayout.addRow(ns_name_label, horizontalLayout) 91 args_label = QtGui.QLabel("Args:", self.content) 92 self.args_field = QtGui.QComboBox(self.content) 93 self.args_field.setSizeAdjustPolicy(QtGui.QComboBox.AdjustToMinimumContentsLength) 94 # self.args_field.setSizePolicy(QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Fixed)) 95 self.args_field.setEditable(True) 96 self.contentLayout.addRow(args_label, self.args_field) 97 args_history = nm.history().cachedParamValues('run_dialog/Args') 98 args_history.insert(0, '') 99 self.args_field.addItems(args_history) 100 101 host_label = QtGui.QLabel("Host:", self.content) 102 self.host_field = QtGui.QComboBox(self.content) 103 # self.host_field.setSizeAdjustPolicy(QtGui.QComboBox.AdjustToContents) 104 self.host_field.setSizePolicy(QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Fixed)) 105 self.host_field.setEditable(True) 106 host_label.setBuddy(self.host_field) 107 self.contentLayout.addRow(host_label, self.host_field) 108 self.host_history = host_history = nm.history().cachedParamValues('/Host') 109 if self.host in host_history: 110 host_history.remove(self.host) 111 host_history.insert(0, self.host) 112 self.host_field.addItems(host_history) 113 114 master_label = QtGui.QLabel("ROS Master URI:", self.content) 115 self.master_field = QtGui.QComboBox(self.content) 116 self.master_field.setSizePolicy(QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Fixed)) 117 self.master_field.setEditable(True) 118 master_label.setBuddy(self.host_field) 119 self.contentLayout.addRow(master_label, self.master_field) 120 self.master_history = master_history = nm.history().cachedParamValues('/Optional Parameter/ROS Master URI') 121 self.masteruri = "ROS_MASTER_URI" 122 if self.masteruri in master_history: 123 master_history.remove(self.masteruri) 124 master_history.insert(0, self.masteruri) 125 self.master_field.addItems(master_history) 126 127 self.buttonBox = QtGui.QDialogButtonBox(self) 128 self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Ok|QtGui.QDialogButtonBox.Cancel) 129 self.buttonBox.setOrientation(QtCore.Qt.Horizontal) 130 self.buttonBox.setObjectName("buttonBox") 131 self.verticalLayout.addWidget(self.buttonBox) 132 133 QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("accepted()"), self.accept) 134 QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), self.reject) 135 QtCore.QMetaObject.connectSlotsByName(self) 136 self.package_field.activated[str].connect(self.on_package_selected) 137 self.package_field.textChanged.connect(self.on_package_selected) 138 self.binary_field.activated[str].connect(self.on_binary_selected) 139 140 self.package_field.setFocus(QtCore.Qt.TabFocusReason) 141 self.package = '' 142 self.binary = '' 143 144 packages = self.packages.keys() 145 packages.sort() 146 self.package_field.addItems(packages) 147 if packages: 148 self.on_package_selected(packages[0])
149
150 - def runSelected(self):
151 ''' 152 Runs the selected node, or do nothing. 153 ''' 154 self.binary = self.binary_field.currentText() 155 self.host = self.host_field.currentText() if self.host_field.currentText() else self.host 156 self.masteruri = self.master_field.currentText() if self.master_field.currentText() else self.masteruri 157 if not self.host in self.host_history and self.host != 'localhost' and self.host != '127.0.0.1': 158 nm.history().add2HostHistory(self.host) 159 ns = self.ns_field.currentText() 160 if ns and ns != '/': 161 nm.history().addParamCache('run_dialog/NS', ns) 162 args = self.args_field.currentText() 163 if args: 164 nm.history().addParamCache('run_dialog/Args', args) 165 if self.package and self.binary: 166 nm.history().addParamCache('/Host', self.host) 167 try: 168 nm.starter().runNodeWithoutConfig(self.host, self.package, self.binary, str(self.name_field.text()), str(''.join(['__ns:=', ns, ' ', args])).split(' '), None if self.masteruri == 'ROS_MASTER_URI' else self.masteruri) 169 except Exception as e: 170 WarningMessageBox(QtGui.QMessageBox.Warning, "Run node", 171 ''.join(['Run node ', str(self.binary), '[', str(self.package), '] failed!']), 172 str(e)).exec_()
173
174 - def _getPackages(self, path):
175 result = {} 176 if os.path.isdir(path): 177 fileList = os.listdir(path) 178 if 'manifest.xml' in fileList: 179 return {os.path.basename(path) : path} 180 for f in fileList: 181 ret = self._getPackages(os.path.sep.join([path, f])) 182 result = dict(ret.items() + result.items()) 183 return result
184
185 - def _getBinaries(self, path):
186 result = {} 187 if os.path.isdir(path): 188 fileList = os.listdir(path) 189 for f in fileList: 190 if f and f[0] != '.' and not f in ['build'] and not f.endswith('.cfg') and not f.endswith('.so'): 191 ret = self._getBinaries(os.path.sep.join([path, f])) 192 result = dict(ret.items() + result.items()) 193 elif os.path.isfile(path) and os.access(path, os.X_OK): 194 # create a selection for binaries 195 return {os.path.basename(path) : path} 196 return result
197
198 - def on_package_selected(self, package):
199 self.binary_field.clear() 200 if self.packages.has_key(package): 201 self.binary_field.setEnabled(True) 202 self.args_field.setEnabled(True) 203 self.ns_field.setEnabled(True) 204 self.name_field.setEnabled(True) 205 path = self.packages[package] 206 binaries = self._getBinaries(path).keys() 207 binaries.sort() 208 self.binary_field.addItems(binaries) 209 self.package = package 210 root, ext = os.path.splitext(os.path.basename(self.binary_field.currentText())) 211 self.name_field.setText(root)
212
213 - def on_binary_selected(self, binary):
214 root, ext = os.path.splitext(os.path.basename(binary)) 215 self.name_field.setText(root)
216