Go to the documentation of this file.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 os
00032
00033 from python_qt_binding.QtCore import QObject, QSignalMapper, Signal
00034 from python_qt_binding.QtGui import QAction, QIcon, QMenu
00035
00036 from .icon_loader import get_icon
00037 from .menu_manager import MenuManager
00038
00039
00040 class PluginMenu(QObject):
00041
00042 """Menu of available plugins to load and running plugin instances to unload."""
00043
00044 load_plugin_signal = Signal(str)
00045 unload_plugin_signal = Signal(str)
00046
00047 def __init__(self, menu_bar, plugin_manager):
00048 super(PluginMenu, self).__init__()
00049 self.setObjectName('PluginMenu')
00050
00051 plugin_menu = menu_bar.addMenu(menu_bar.tr('&Plugins'))
00052 running_menu = menu_bar.addMenu(menu_bar.tr('&Running'))
00053 self._plugin_menu_manager = MenuManager(plugin_menu)
00054 self._plugin_mapper = QSignalMapper(plugin_menu)
00055 self._plugin_mapper.mapped[str].connect(self.load_plugin_signal)
00056 self._running_menu_manager = MenuManager(running_menu)
00057 self._running_mapper = QSignalMapper(running_menu)
00058 self._running_mapper.mapped[str].connect(self.unload_plugin_signal)
00059
00060 self._instances = {}
00061
00062 def add_plugin(self, plugin_descriptor):
00063 base_path = plugin_descriptor.attributes().get('plugin_path')
00064
00065 menu_manager = self._plugin_menu_manager
00066
00067 for group in plugin_descriptor.groups():
00068 label = group['label']
00069 if menu_manager.contains_menu(label):
00070 submenu = menu_manager.get_menu(label)
00071 else:
00072 submenu = QMenu(label, menu_manager.menu)
00073 menu_action = submenu.menuAction()
00074 self._enrich_action(menu_action, group, base_path)
00075 menu_manager.add_item(submenu)
00076 menu_manager = MenuManager(submenu)
00077
00078 action_attributes = plugin_descriptor.action_attributes()
00079 action = QAction(action_attributes['label'], menu_manager.menu)
00080 self._enrich_action(action, action_attributes, base_path)
00081
00082 self._plugin_mapper.setMapping(action, plugin_descriptor.plugin_id())
00083 action.triggered.connect(self._plugin_mapper.map)
00084
00085 not_available = plugin_descriptor.attributes().get('not_available')
00086 if not_available:
00087 action.setEnabled(False)
00088 action.setStatusTip(self.tr('Plugin is not available: %s') % not_available)
00089
00090
00091 menu_manager.add_item(action)
00092
00093 def add_plugin_prefix(self, plugin_descriptor):
00094 action_attributes = plugin_descriptor.action_attributes()
00095 action = QAction(action_attributes['label'], self._plugin_menu_manager.menu)
00096 self._enrich_action(action, action_attributes)
00097 self._plugin_mapper.setMapping(action, plugin_descriptor.plugin_id())
00098 action.triggered.connect(self._plugin_mapper.map)
00099 self._plugin_menu_manager.add_prefix(action)
00100
00101 def add_instance(self, plugin_descriptor, instance_id):
00102 action_attributes = plugin_descriptor.action_attributes()
00103
00104 label = self.tr('Close:') + ' ' + action_attributes['label']
00105 if instance_id.serial_number != 1:
00106 label = label + ' (%s)' % str(instance_id.serial_number)
00107 action = QAction(label, self._running_menu_manager.menu)
00108 base_path = plugin_descriptor.attributes().get('plugin_path')
00109 self._enrich_action(action, action_attributes, base_path)
00110
00111 self._running_mapper.setMapping(action, str(instance_id))
00112 action.triggered.connect(self._running_mapper.map)
00113
00114 self._running_menu_manager.add_item(action)
00115 self._instances[instance_id] = action
00116
00117 def remove_instance(self, instance_id):
00118 action = self._instances[instance_id]
00119 self._running_mapper.removeMappings(action)
00120 self._running_menu_manager.remove_item(action)
00121
00122 def _enrich_action(self, action, action_attributes, base_path=None):
00123 if 'icon' in action_attributes and action_attributes['icon'] is not None:
00124 icon = get_icon(action_attributes['icon'], action_attributes.get('icontype', None), base_path)
00125 action.setIcon(icon)
00126
00127 if 'statustip' in action_attributes:
00128 action.setStatusTip(action_attributes['statustip'])