plugin_handler_direct.py
Go to the documentation of this file.
00001 # Copyright (c) 2011, Dirk Thomas, Dorian Scholz, TU Darmstadt
00002 # All rights reserved.
00003 #
00004 # Redistribution and use in source and binary forms, with or without
00005 # modification, are permitted provided that the following conditions
00006 # are met:
00007 #
00008 #   * Redistributions of source code must retain the above copyright
00009 #     notice, this list of conditions and the following disclaimer.
00010 #   * Redistributions in binary form must reproduce the above
00011 #     copyright notice, this list of conditions and the following
00012 #     disclaimer in the documentation and/or other materials provided
00013 #     with the distribution.
00014 #   * Neither the name of the TU Darmstadt nor the names of its
00015 #     contributors may be used to endorse or promote products derived
00016 #     from this software without specific prior written permission.
00017 #
00018 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00021 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00022 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00023 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00024 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00026 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00027 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00028 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00029 # POSSIBILITY OF SUCH DAMAGE.
00030 
00031 import traceback
00032 
00033 from python_qt_binding.QtCore import qCritical, QEvent, QObject, qWarning, Slot
00034 
00035 from .plugin_context import PluginContext
00036 from .plugin_handler import PluginHandler
00037 
00038 
00039 class PluginHandlerDirect(PluginHandler):
00040 
00041     """Handler for directly passing invocations between the framework and one `Plugin` instance."""
00042 
00043     def __init__(self, parent, main_window, instance_id, application_context, container_manager, argv):
00044         super(PluginHandlerDirect, self).__init__(parent, main_window, instance_id, application_context, container_manager, argv)
00045         self.setObjectName('PluginHandlerDirect')
00046         self._context = None
00047         self._plugin = None
00048 
00049     def load(self, plugin_provider, callback=None):
00050         self._context = PluginContext(self)
00051         super(PluginHandlerDirect, self).load(plugin_provider, callback)
00052 
00053     def _load(self):
00054         self._plugin = self._plugin_provider.load(self._instance_id.plugin_id, self._context)
00055         if hasattr(self._plugin, 'has_configuration'):
00056             self._plugin_has_configuration = self._plugin.has_configuration()
00057         else:
00058             self._plugin_has_configuration = hasattr(self._plugin, 'trigger_configuration')
00059         self._update_title_bars()
00060         self._emit_load_completed()
00061 
00062     def _emit_load_completed(self, exception=None):
00063         if exception is None and hasattr(self._plugin, 'installEventFilter'):
00064             # emit close_signal when deferred delete event for plugin is received
00065             self._plugin.installEventFilter(self)
00066         super(PluginHandlerDirect, self)._emit_load_completed(exception)
00067 
00068     def eventFilter(self, watched, event):
00069         if event.type() == QEvent.DeferredDelete:
00070             # TOOD: check if ignore() is necessary
00071             event.ignore()
00072             self.close_signal.emit(str(self._instance_id))
00073             return True
00074         return QObject.eventFilter(self, watched, event)
00075 
00076     def shutdown_plugin(self, callback):
00077         if hasattr(self._plugin, 'removeEventFilter'):
00078             self._plugin.removeEventFilter(self)
00079         super(PluginHandlerDirect, self).shutdown_plugin(callback)
00080         if hasattr(self._plugin, 'deleteLater'):
00081             self._plugin.deleteLater()
00082 
00083     def _shutdown_plugin(self):
00084         if hasattr(self._plugin, 'shutdown_plugin'):
00085             try:
00086                 self._plugin.shutdown_plugin()
00087             except Exception:
00088                 qCritical('PluginHandlerDirect._shutdown_plugin() plugin "%s" raised an exception:\n%s' % (str(self._instance_id), traceback.format_exc()))
00089         self.emit_shutdown_plugin_completed()
00090 
00091     def _delete_widget(self, widget):
00092         # only delete widgets which are not at the same time the plugin
00093         if widget != self._plugin:
00094             super(PluginHandlerDirect, self)._delete_widget(widget)
00095 
00096     def _unload(self):
00097         self._plugin_provider.unload(self._plugin)
00098         self._plugin = None
00099         self._emit_unload_completed()
00100 
00101     def _save_settings(self, plugin_settings, instance_settings):
00102         if hasattr(self._plugin, 'save_settings'):
00103             plugin_settings_plugin = plugin_settings.get_settings('plugin')
00104             instance_settings_plugin = instance_settings.get_settings('plugin')
00105             try:
00106                 self._plugin.save_settings(plugin_settings_plugin, instance_settings_plugin)
00107             except Exception:
00108                 qCritical('PluginHandlerDirect._save_settings() plugin "%s" raised an exception:\n%s' % (str(self._instance_id), traceback.format_exc()))
00109         self.emit_save_settings_completed()
00110 
00111     def _restore_settings(self, plugin_settings, instance_settings):
00112         if hasattr(self._plugin, 'restore_settings'):
00113             plugin_settings_plugin = plugin_settings.get_settings('plugin')
00114             instance_settings_plugin = instance_settings.get_settings('plugin')
00115             try:
00116                 self._plugin.restore_settings(plugin_settings_plugin, instance_settings_plugin)
00117             except Exception:
00118                 qCritical('PluginHandlerDirect._restore_settings() plugin "%s" raised an exception:\n%s' % (str(self._instance_id), traceback.format_exc()))
00119         self.emit_restore_settings_completed()
00120 
00121     # pointer to QWidget must be used for PySide to work (at least with 1.0.1)
00122     @Slot('QWidget*')
00123     def add_widget(self, widget):
00124         if widget in self._widgets:
00125             qWarning('PluginHandlerDirect.add_widget() widget "%s" already added' % widget.objectName())
00126             return
00127         dock_widget = self._create_dock_widget()
00128         self._add_dock_widget(dock_widget, widget)
00129 
00130     # pointer to QToolBar must be used for PySide to work (at least with 1.0.1)
00131     @Slot('QToolBar*')
00132     def add_toolbar(self, toolbar):
00133         if toolbar in self._toolbars:
00134             qWarning('PluginHandlerDirect.add_toolbar() toolbar "%s" already added' % toolbar.objectName())
00135             return
00136         self._add_toolbar(toolbar)
00137 
00138     @Slot()
00139     def close_plugin(self):
00140         # only non-standalone plugins are closable
00141         if self._application_context.options.standalone_plugin is None:
00142             self._emit_close_plugin()
00143 
00144     @Slot()
00145     def reload_plugin(self):
00146         self._emit_reload_signal()


qt_gui
Author(s): Dirk Thomas
autogenerated on Fri Aug 28 2015 12:15:40