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, 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         # create submenus
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         # create action
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         # add action to menu
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'])


qt_gui
Author(s): Dirk Thomas
autogenerated on Thu Jun 6 2019 18:07:34