plugin_menu.py
Go to the documentation of this file.
00001 # Copyright (c) 2011, Dirk Thomas, Dorian Scholz, TU Darmstadt
00002 # All rights reserved.
00003 #
00004 # Redistribution and use in source and binary forms, with or without
00005 # modification, are permitted provided that the following conditions
00006 # are met:
00007 #
00008 #   * Redistributions of source code must retain the above copyright
00009 #     notice, this list of conditions and the following disclaimer.
00010 #   * Redistributions in binary form must reproduce the above
00011 #     copyright notice, this list of conditions and the following
00012 #     disclaimer in the documentation and/or other materials provided
00013 #     with the distribution.
00014 #   * Neither the name of the TU Darmstadt nor the names of its
00015 #     contributors may be used to endorse or promote products derived
00016 #     from this software without specific prior written permission.
00017 #
00018 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00021 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00022 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00023 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00024 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00026 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00027 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00028 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00029 # POSSIBILITY OF SUCH DAMAGE.
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         # create submenus
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         # create action
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         # add action to menu
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         # create action
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'])


qt_gui
Author(s): Dirk Thomas
autogenerated on Mon Oct 6 2014 03:57:52