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 .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         # create submenus
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         # create action
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         # add action to menu
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         # create action
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                 # see http://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html
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'])


qt_gui
Author(s): Dirk Thomas
autogenerated on Fri Jan 3 2014 11:44:00