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()
def unload(self, callback=None)
def _restore_settings(self, plugin_settings, instance_settings)
def save_settings(self, plugin_settings, instance_settings, callback=None)
def __init__(self, parent, main_window, instance_id, application_context, container_manager, argv)
def _emit_load_completed(self, exception=None)
def _save_settings(self, plugin_settings, instance_settings)
def restore_settings(self, plugin_settings, instance_settings, callback=None)
def load(self, plugin_provider, callback=None)
def _add_dock_widget(self, dock_widget, widget)


qt_gui
Author(s): Dirk Thomas
autogenerated on Fri Jun 24 2022 02:42:37