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