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 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
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
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
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
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
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
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()