py_console.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 # Copyright (c) 2012, Dorian Scholz
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions
00008 # are met:
00009 #
00010 #  * Redistributions of source code must retain the above copyright
00011 #    notice, this list of conditions and the following disclaimer.
00012 #  * Redistributions in binary form must reproduce the above
00013 #    copyright notice, this list of conditions and the following
00014 #    disclaimer in the documentation and/or other materials provided
00015 #    with the distribution.
00016 #  * Neither the name of Willow Garage, Inc. nor the names of its
00017 #    contributors may be used to endorse or promote products derived
00018 #    from this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 # POSSIBILITY OF SUCH DAMAGE.
00032 
00033 from python_qt_binding.QtWidgets import QVBoxLayout, QWidget
00034 from qt_gui.plugin import Plugin
00035 from qt_gui_py_common.simple_settings_dialog import SimpleSettingsDialog
00036 from rqt_py_console.py_console_widget import PyConsoleWidget
00037 
00038 try:
00039     from spyder_console_widget import SpyderConsoleWidget
00040     _has_spyderlib = True
00041 except ImportError:
00042     _has_spyderlib = False
00043 
00044 
00045 class PyConsole(Plugin):
00046 
00047     """
00048     Plugin providing an interactive Python console
00049     """
00050 
00051     def __init__(self, context):
00052         super(PyConsole, self).__init__(context)
00053         self.setObjectName('PyConsole')
00054 
00055         self._context = context
00056         self._use_spyderlib = _has_spyderlib
00057         self._console_widget = None
00058         self._widget = QWidget()
00059         self._widget.setLayout(QVBoxLayout())
00060         self._widget.layout().setContentsMargins(0, 0, 0, 0)
00061         if context.serial_number() > 1:
00062             self._widget.setWindowTitle(
00063                 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(
00078                 self._widget.windowTitle() + (' (%d)' % self._context.serial_number()))
00079 
00080         self._widget.layout().addWidget(self._console_widget)
00081 
00082     def save_settings(self, plugin_settings, instance_settings):
00083         instance_settings.set_value('use_spyderlib', self._use_spyderlib)
00084 
00085     def restore_settings(self, plugin_settings, instance_settings):
00086         self._use_spyderlib = _has_spyderlib and (
00087             instance_settings.value('use_spyderlib', True) in [True, 'true'])
00088         self._switch_console_widget()
00089 
00090     def trigger_configuration(self):
00091         options = [
00092             {'title': 'SpyderConsole',
00093                 'description': 'Advanced Python console with tab-completion (needs spyderlib to be installed).', 'enabled': _has_spyderlib},
00094             {'title': 'PyConsole', 'description': 'Simple Python console.'},
00095         ]
00096         dialog = SimpleSettingsDialog(title='PyConsole Options')
00097         dialog.add_exclusive_option_group(
00098             title='Console Type', options=options, selected_index=int(not self._use_spyderlib))
00099         console_type = dialog.get_settings()[0]
00100         new_use_spyderlib = {0: True, 1: False}.get(
00101             console_type['selected_index'], self._use_spyderlib)
00102         if self._use_spyderlib != new_use_spyderlib:
00103             self._use_spyderlib = new_use_spyderlib
00104             self._switch_console_widget()
00105 
00106     def shutdown_console_widget(self):
00107         if self._console_widget is not None and hasattr(self._console_widget, 'shutdown'):
00108             self._console_widget.shutdown()
00109 
00110     def shutdown_plugin(self):
00111         self.shutdown_console_widget()


rqt_py_console
Author(s): Dorian Scholz
autogenerated on Thu Jun 6 2019 18:04:28