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 roslib
00034 roslib.load_manifest('rqt_py_console')
00035
00036 from python_qt_binding.QtGui import QVBoxLayout, QWidget
00037 from qt_gui.plugin import Plugin
00038 from qt_gui_py_common.simple_settings_dialog import SimpleSettingsDialog
00039 from py_console_widget import PyConsoleWidget
00040
00041 try:
00042 from spyder_console_widget import SpyderConsoleWidget
00043 _has_spyderlib = True
00044 except ImportError:
00045 _has_spyderlib = False
00046
00047
00048 class PyConsole(Plugin):
00049 """
00050 Plugin providing an interactive Python console
00051 """
00052 def __init__(self, context):
00053 super(PyConsole, self).__init__(context)
00054 self.setObjectName('PyConsole')
00055
00056 self._context = context
00057 self._use_spyderlib = _has_spyderlib
00058 self._console_widget = None
00059 self._widget = QWidget()
00060 self._widget.setLayout(QVBoxLayout())
00061 self._widget.layout().setContentsMargins(0, 0, 0, 0)
00062 if context.serial_number() > 1:
00063 self._widget.setWindowTitle(self._widget.windowTitle() + (' (%d)' % context.serial_number()))
00064 self._context.add_widget(self._widget)
00065
00066 def _switch_console_widget(self):
00067 self._widget.layout().removeWidget(self._console_widget)
00068 self.shutdown_console_widget()
00069
00070 if _has_spyderlib and self._use_spyderlib:
00071 self._console_widget = SpyderConsoleWidget(self._context)
00072 self._widget.setWindowTitle('SpyderConsole')
00073 else:
00074 self._console_widget = PyConsoleWidget(self._context)
00075 self._widget.setWindowTitle('PyConsole')
00076 if self._context.serial_number() > 1:
00077 self._widget.setWindowTitle(self._widget.windowTitle() + (' (%d)' % self._context.serial_number()))
00078
00079 self._widget.layout().addWidget(self._console_widget)
00080
00081 def save_settings(self, plugin_settings, instance_settings):
00082 instance_settings.set_value('use_spyderlib', self._use_spyderlib)
00083
00084 def restore_settings(self, plugin_settings, instance_settings):
00085 self._use_spyderlib = _has_spyderlib and (instance_settings.value('use_spyderlib', True) in [True, 'true'])
00086 self._switch_console_widget()
00087
00088 def trigger_configuration(self):
00089 options = [
00090 {'title': 'SpyderConsole', 'description': 'Advanced Python console with tab-completion (needs spyderlib to be installed).', 'enabled': _has_spyderlib},
00091 {'title': 'PyConsole', 'description': 'Simple Python console.'},
00092 ]
00093 dialog = SimpleSettingsDialog(title='PyConsole Options')
00094 dialog.add_exclusive_option_group(title='Console Type', options=options, selected_index=int(not self._use_spyderlib))
00095 console_type = dialog.get_settings()[0]
00096 new_use_spyderlib = {0: True, 1: False}.get(console_type['selected_index'], self._use_spyderlib)
00097 if self._use_spyderlib != new_use_spyderlib:
00098 self._use_spyderlib = new_use_spyderlib
00099 self._switch_console_widget()
00100
00101 def shutdown_console_widget(self):
00102 if self._console_widget is not None and hasattr(self._console_widget, 'shutdown'):
00103 self._console_widget.shutdown()
00104
00105 def shutdown_plugin(self):
00106 self.shutdown_console_widget()