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