plugin_handler_direct.py
Go to the documentation of this file.
1 # Copyright (c) 2011, Dirk Thomas, Dorian Scholz, TU Darmstadt
2 # All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions
6 # are met:
7 #
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following
12 # disclaimer in the documentation and/or other materials provided
13 # with the distribution.
14 # * Neither the name of the TU Darmstadt nor the names of its
15 # contributors may be used to endorse or promote products derived
16 # from this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 # POSSIBILITY OF SUCH DAMAGE.
30 
31 import traceback
32 
33 from python_qt_binding.QtCore import qCritical, QEvent, QObject, qWarning, Slot
34 
35 from qt_gui.plugin_context import PluginContext
36 from qt_gui.plugin_handler import PluginHandler
37 
38 
40  """Handler for directly passing invocations between the framework and one `Plugin` instance."""
41 
42  def __init__(self, parent, main_window, instance_id,
43  application_context, container_manager, argv):
44  super(PluginHandlerDirect, self).__init__(
45  parent, main_window, instance_id, application_context, container_manager, argv)
46  self.setObjectName('PluginHandlerDirect')
47  self._context = None
48  self._plugin = None
49 
50  def load(self, plugin_provider, callback=None):
51  self._context = PluginContext(self)
52  super(PluginHandlerDirect, self).load(plugin_provider, callback)
53 
54  def _load(self):
55  self._plugin = self._plugin_provider.load(self._instance_id.plugin_id, self._context)
56  if hasattr(self._plugin, 'has_configuration'):
57  self._plugin_has_configuration = self._plugin.has_configuration()
58  else:
59  self._plugin_has_configuration = hasattr(self._plugin, 'trigger_configuration')
60  self._update_title_bars()
62 
63  def _emit_load_completed(self, exception=None):
64  if exception is None and hasattr(self._plugin, 'installEventFilter'):
65  # emit close_signal when deferred delete event for plugin is received
66  self._plugin.installEventFilter(self)
67  super(PluginHandlerDirect, self)._emit_load_completed(exception)
68 
69  def eventFilter(self, watched, event):
70  if event.type() == QEvent.DeferredDelete:
71  # TOOD: check if ignore() is necessary
72  event.ignore()
73  self.close_signal.emit(str(self._instance_id))
74  return True
75  return QObject.eventFilter(self, watched, event)
76 
77  def shutdown_plugin(self, callback):
78  if hasattr(self._plugin, 'removeEventFilter'):
79  self._plugin.removeEventFilter(self)
80  super(PluginHandlerDirect, self).shutdown_plugin(callback)
81  if hasattr(self._plugin, 'deleteLater'):
82  self._plugin.deleteLater()
83 
84  def _shutdown_plugin(self):
85  if hasattr(self._plugin, 'shutdown_plugin'):
86  try:
88  except Exception:
89  qCritical(
90  'PluginHandlerDirect._shutdown_plugin() plugin "%s" raised an '
91  'exception:\n%s' % (str(self._instance_id), traceback.format_exc()))
93 
94  def _delete_widget(self, widget):
95  # only delete widgets which are not at the same time the plugin
96  if widget != self._plugin:
97  super(PluginHandlerDirect, self)._delete_widget(widget)
98 
99  def _unload(self):
100  self._plugin_provider.unload(self._plugin)
101  self._plugin = None
103 
104  def _save_settings(self, plugin_settings, instance_settings):
105  if hasattr(self._plugin, 'save_settings'):
106  plugin_settings_plugin = plugin_settings.get_settings('plugin')
107  instance_settings_plugin = instance_settings.get_settings('plugin')
108  try:
109  self._plugin.save_settings(plugin_settings_plugin, instance_settings_plugin)
110  except Exception:
111  qCritical(
112  'PluginHandlerDirect._save_settings() plugin "%s" raised an exception:\n%s' %
113  (str(self._instance_id), traceback.format_exc()))
115 
116  def _restore_settings(self, plugin_settings, instance_settings):
117  if hasattr(self._plugin, 'restore_settings'):
118  plugin_settings_plugin = plugin_settings.get_settings('plugin')
119  instance_settings_plugin = instance_settings.get_settings('plugin')
120  try:
121  self._plugin.restore_settings(plugin_settings_plugin, instance_settings_plugin)
122  except Exception:
123  qCritical(
124  'PluginHandlerDirect._restore_settings() plugin "%s" raised an exception:\n%s' %
125  (str(self._instance_id), traceback.format_exc()))
127 
128  # pointer to QWidget must be used for PySide to work (at least with 1.0.1)
129  @Slot('QWidget*')
130  def add_widget(self, widget):
131  if widget in self._widgets:
132  qWarning('PluginHandlerDirect.add_widget() widget "%s" already added' %
133  widget.objectName())
134  return
135  dock_widget = self._create_dock_widget()
136  self._add_dock_widget(dock_widget, widget)
137 
138  # pointer to QToolBar must be used for PySide to work (at least with 1.0.1)
139  @Slot('QToolBar*')
140  def add_toolbar(self, toolbar):
141  if toolbar in self._toolbars:
142  qWarning('PluginHandlerDirect.add_toolbar() toolbar "%s" already added' %
143  toolbar.objectName())
144  return
145  self._add_toolbar(toolbar)
146 
147  @Slot()
148  def close_plugin(self):
149  # only non-standalone plugins are closable
150  if self._application_context.options.standalone_plugin is None:
151  self._emit_close_plugin()
152 
153  @Slot()
154  def reload_plugin(self):
155  self._emit_reload_signal()
qt_gui.plugin_handler.PluginHandler.save_settings
def save_settings(self, plugin_settings, instance_settings, callback=None)
Definition: plugin_handler.py:181
qt_gui.plugin_handler_direct.PluginHandlerDirect
Definition: plugin_handler_direct.py:39
qt_gui.plugin_handler.PluginHandler._instance_id
_instance_id
Definition: plugin_handler.py:61
qt_gui.plugin_handler_direct.PluginHandlerDirect.eventFilter
def eventFilter(self, watched, event)
Definition: plugin_handler_direct.py:69
qt_gui.plugin_handler.PluginHandler._plugin_provider
_plugin_provider
Definition: plugin_handler.py:69
qt_gui.plugin_handler.PluginHandler._create_dock_widget
def _create_dock_widget(self)
Definition: plugin_handler.py:250
qt_gui.plugin_handler_direct.PluginHandlerDirect.load
def load(self, plugin_provider, callback=None)
Definition: plugin_handler_direct.py:50
qt_gui.plugin_handler.PluginHandler._widgets
_widgets
Definition: plugin_handler.py:76
qt_gui.plugin_handler_direct.PluginHandlerDirect.close_plugin
def close_plugin(self)
Definition: plugin_handler_direct.py:148
qt_gui.plugin_context.PluginContext
Definition: plugin_context.py:34
qt_gui.plugin_handler_direct.PluginHandlerDirect._delete_widget
def _delete_widget(self, widget)
Definition: plugin_handler_direct.py:94
qt_gui.plugin_handler.PluginHandler._plugin_has_configuration
_plugin_has_configuration
Definition: plugin_handler.py:73
qt_gui.plugin_handler.PluginHandler._emit_close_plugin
def _emit_close_plugin(self)
Definition: plugin_handler.py:428
qt_gui.plugin_handler.PluginHandler
Definition: plugin_handler.py:42
qt_gui.plugin_handler.PluginHandler._toolbars
_toolbars
Definition: plugin_handler.py:78
qt_gui.plugin_handler.PluginHandler.emit_restore_settings_completed
def emit_restore_settings_completed(self)
Definition: plugin_handler.py:240
qt_gui.plugin_handler_direct.PluginHandlerDirect.shutdown_plugin
def shutdown_plugin(self, callback)
Definition: plugin_handler_direct.py:77
qt_gui.plugin_handler_direct.PluginHandlerDirect._restore_settings
def _restore_settings(self, plugin_settings, instance_settings)
Definition: plugin_handler_direct.py:116
qt_gui.plugin_handler.PluginHandler.unload
def unload(self, callback=None)
Definition: plugin_handler.py:158
qt_gui.plugin_handler_direct.PluginHandlerDirect.add_widget
def add_widget(self, widget)
Definition: plugin_handler_direct.py:130
qt_gui.plugin_handler.PluginHandler._emit_load_completed
def _emit_load_completed(self, exception=None)
Definition: plugin_handler.py:109
qt_gui.plugin_handler.PluginHandler.close_signal
close_signal
Definition: plugin_handler.py:51
qt_gui.plugin_handler_direct.PluginHandlerDirect._plugin
_plugin
Definition: plugin_handler_direct.py:47
qt_gui.plugin_handler_direct.PluginHandlerDirect._save_settings
def _save_settings(self, plugin_settings, instance_settings)
Definition: plugin_handler_direct.py:104
qt_gui.plugin_context
Definition: plugin_context.py:1
qt_gui.plugin_handler_direct.PluginHandlerDirect.__init__
def __init__(self, parent, main_window, instance_id, application_context, container_manager, argv)
Definition: plugin_handler_direct.py:42
qt_gui.plugin_handler.PluginHandler._update_title_bars
def _update_title_bars(self)
Definition: plugin_handler.py:299
qt_gui.plugin_handler_direct.PluginHandlerDirect.reload_plugin
def reload_plugin(self)
Definition: plugin_handler_direct.py:154
qt_gui.plugin_handler.PluginHandler._add_dock_widget
def _add_dock_widget(self, dock_widget, widget)
Definition: plugin_handler.py:318
qt_gui.plugin_handler_direct.PluginHandlerDirect._unload
def _unload(self)
Definition: plugin_handler_direct.py:99
qt_gui.plugin_handler_direct.PluginHandlerDirect._context
_context
Definition: plugin_handler_direct.py:46
qt_gui.plugin_handler
Definition: plugin_handler.py:1
qt_gui.plugin_handler.PluginHandler.emit_save_settings_completed
def emit_save_settings_completed(self)
Definition: plugin_handler.py:200
qt_gui.plugin_handler_direct.PluginHandlerDirect._load
def _load(self)
Definition: plugin_handler_direct.py:54
qt_gui.plugin_handler_direct.PluginHandlerDirect._shutdown_plugin
def _shutdown_plugin(self)
Definition: plugin_handler_direct.py:84
qt_gui.plugin_handler_direct.PluginHandlerDirect.add_toolbar
def add_toolbar(self, toolbar)
Definition: plugin_handler_direct.py:140
qt_gui.plugin_handler.PluginHandler._emit_reload_signal
def _emit_reload_signal(self)
Definition: plugin_handler.py:312
qt_gui.plugin_handler.PluginHandler.emit_shutdown_plugin_completed
def emit_shutdown_plugin_completed(self)
Definition: plugin_handler.py:145
qt_gui.plugin_handler_direct.PluginHandlerDirect._emit_load_completed
def _emit_load_completed(self, exception=None)
Definition: plugin_handler_direct.py:63
qt_gui.plugin_handler.PluginHandler._emit_unload_completed
def _emit_unload_completed(self)
Definition: plugin_handler.py:175
qt_gui.plugin_handler.PluginHandler._add_toolbar
def _add_toolbar(self, toolbar)
Definition: plugin_handler.py:390
qt_gui.plugin_handler.PluginHandler.restore_settings
def restore_settings(self, plugin_settings, instance_settings, callback=None)
Definition: plugin_handler.py:221
qt_gui.plugin_handler.PluginHandler._application_context
_application_context
Definition: plugin_handler.py:62


qt_gui
Author(s): Dirk Thomas
autogenerated on Sat Jun 25 2022 02:15:05