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 import argparse
00034 from python_qt_binding.QtCore import qVersion
00035 from qt_gui.plugin import Plugin
00036 from qt_gui_py_common.simple_settings_dialog import SimpleSettingsDialog
00037
00038 from rqt_shell.shell_widget import ShellWidget
00039
00040 try:
00041 if qVersion().startswith('5.'):
00042 raise ImportError('embedding is not support with Qt 5')
00043 from xterm_widget import XTermWidget, is_xterm_available
00044 _has_xterm = is_xterm_available()
00045 except ImportError:
00046 XTermWidget = None
00047 _has_xterm = False
00048 try:
00049 if qVersion().startswith('5.'):
00050 raise ImportError('spyderlib does not support Qt 5 yet')
00051 from spyder_shell_widget import SpyderShellWidget
00052 _has_spyderlib = True
00053 except ImportError:
00054 SpyderShellWidget = None
00055 _has_spyderlib = False
00056
00057
00058 class Shell(Plugin):
00059 """
00060 Plugin providing an interactive shell
00061 """
00062
00063 shell_types = [
00064 {
00065 'title': 'XTerm',
00066 'widget_class': XTermWidget,
00067 'description': 'Fully functional embedded XTerm (needs xterm, only works on X11 with Qt 4).',
00068 'enabled': _has_xterm,
00069 },
00070 {
00071 'title': 'SpyderShell',
00072 'widget_class': SpyderShellWidget,
00073 'description': 'Advanced shell (needs spyderlib, only works with Qt 4).',
00074 'enabled': _has_spyderlib,
00075 },
00076 {
00077 'title': 'SimpleShell',
00078 'widget_class': ShellWidget,
00079 'description': 'Simple shell for executing non-interactive finite commands.',
00080 'enabled': True,
00081 },
00082 ]
00083
00084 def __init__(self, context):
00085 super(Shell, self).__init__(context)
00086 self._context = context
00087 self.setObjectName('Shell')
00088 self._args = self._parse_args(context.argv())
00089
00090 self._widget = None
00091
00092 def _parse_args(self, argv):
00093 parser = argparse.ArgumentParser(prog='rqt_shell')
00094 Shell.add_arguments(parser)
00095 return parser.parse_args(argv)
00096
00097 @staticmethod
00098 def add_arguments(parser):
00099 group = parser.add_argument_group('Options for rqt_shell plugin')
00100 group.add_argument('-i', '--init-script',
00101 help="Executes this script before staring a $SHELL subshell. \
00102 Doesn't work for SimpleShell. \
00103 Always export any variables you intend on using in $SHELL")
00104
00105 def _switch_shell_widget(self):
00106
00107 while not self.shell_types[self._shell_type_index]['enabled']:
00108 self._shell_type_index += 1
00109 selected_shell = self.shell_types[self._shell_type_index]
00110
00111 if self._widget is not None:
00112 if hasattr(self._widget, 'close_signal'):
00113 self._widget.close_signal.disconnect(self._context.close_plugin)
00114 self._context.remove_widget(self._widget)
00115 self._widget.close()
00116
00117 self._widget = selected_shell['widget_class'](script_path=self._args.init_script)
00118 self._widget.setWindowTitle(selected_shell['title'])
00119 if self._context.serial_number() > 1:
00120 self._widget.setWindowTitle(self._widget.windowTitle() + (' (%d)' % self._context.serial_number()))
00121 self._context.add_widget(self._widget)
00122 if hasattr(self._widget, 'close_signal'):
00123 self._widget.close_signal.connect(self._context.close_plugin)
00124
00125 def save_settings(self, plugin_settings, instance_settings):
00126 instance_settings.set_value('shell_type', self._shell_type_index)
00127
00128 def restore_settings(self, plugin_settings, instance_settings):
00129 self._shell_type_index = int(instance_settings.value('shell_type', 0))
00130 self._switch_shell_widget()
00131
00132 def trigger_configuration(self):
00133 dialog = SimpleSettingsDialog(title='Shell Options')
00134 dialog.add_exclusive_option_group(title='Shell Type', options=self.shell_types, selected_index=self._shell_type_index)
00135 shell_type = dialog.get_settings()[0]
00136 if shell_type is not None and self._shell_type_index != shell_type['selected_index']:
00137 self._shell_type_index = shell_type['selected_index']
00138 self._context.reload_plugin()
00139
00140 def shutdown_plugin(self):
00141 if self._widget is not None and hasattr(self._widget, 'shutdown'):
00142 self._widget.shutdown()
00143