plugin_menu.py
Go to the documentation of this file.
1 # Copyright (c) 2011, Dirk Thomas, Dorian Scholz, TU Darmstadt
2 # All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions
6 # are met:
7 #
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following
12 # disclaimer in the documentation and/or other materials provided
13 # with the distribution.
14 # * Neither the name of the TU Darmstadt nor the names of its
15 # contributors may be used to endorse or promote products derived
16 # from this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 # POSSIBILITY OF SUCH DAMAGE.
30 
31 from python_qt_binding.QtCore import QObject, QSignalMapper, Signal, Slot
32 from python_qt_binding.QtWidgets import QAction, QMenu
33 
34 from qt_gui.icon_loader import get_icon
35 from qt_gui.menu_manager import MenuManager
36 from qt_gui.plugin_instance_id import PluginInstanceId
37 
38 
39 class PluginMenu(QObject):
40  """Menu of available plugins to load and running plugin instances to unload."""
41 
42  load_plugin_signal = Signal(str)
43  unload_plugin_signal = Signal(str)
44 
45  def __init__(self, menu_bar, plugin_manager):
46  super(PluginMenu, self).__init__()
47  self.setObjectName('PluginMenu')
48 
49  plugin_menu = menu_bar.addMenu(menu_bar.tr('&Plugins'))
50  running_menu = menu_bar.addMenu(menu_bar.tr('&Running'))
51  self._plugin_menu_manager = MenuManager(plugin_menu)
52  self._plugin_mapper = QSignalMapper(plugin_menu)
53  self._plugin_mapper.mapped[str].connect(self.load_plugin_signal)
54  self._running_menu_manager = MenuManager(running_menu)
55  action = QAction(
56  ' Hidden action to work around QTBUG-52582', self._running_menu_manager.menu)
57  action.setVisible(False)
58  self._running_menu_manager.add_item(action)
59  self._running_mapper = QSignalMapper(running_menu)
60  self._running_mapper.mapped[str].connect(self.unload_plugin_signal)
61 
62  self._instances = {}
63 
64  def add_plugin(self, plugin_descriptor):
65  base_path = plugin_descriptor.attributes().get('plugin_path')
66 
67  menu_manager = self._plugin_menu_manager
68  # create submenus
69  for group in plugin_descriptor.groups():
70  label = group['label']
71  if menu_manager.contains_menu(label):
72  submenu = menu_manager.get_menu(label)
73  else:
74  submenu = QMenu(label, menu_manager.menu)
75  menu_action = submenu.menuAction()
76  self._enrich_action(menu_action, group, base_path)
77  menu_manager.add_item(submenu)
78  menu_manager = MenuManager(submenu)
79  # create action
80  action_attributes = plugin_descriptor.action_attributes()
81  action = QAction(action_attributes['label'], menu_manager.menu)
82  self._enrich_action(action, action_attributes, base_path)
83 
84  self._plugin_mapper.setMapping(action, plugin_descriptor.plugin_id())
85  action.triggered.connect(self._plugin_mapper.map)
86 
87  not_available = plugin_descriptor.attributes().get('not_available')
88  if not_available:
89  action.setEnabled(False)
90  action.setStatusTip(self.tr('Plugin is not available: %s') % not_available)
91 
92  # add action to menu
93  menu_manager.add_item(action)
94 
95  def add_plugin_prefix(self, plugin_descriptor):
96  action_attributes = plugin_descriptor.action_attributes()
97  action = QAction(action_attributes['label'], self._plugin_menu_manager.menu)
98  self._enrich_action(action, action_attributes)
99  self._plugin_mapper.setMapping(action, plugin_descriptor.plugin_id())
100  action.triggered.connect(self._plugin_mapper.map)
101  self._plugin_menu_manager.add_prefix(action)
102 
103  def add_instance(self, plugin_descriptor, instance_id):
104  action_attributes = plugin_descriptor.action_attributes()
105  action = QAction(self._get_instance_label(
106  str(instance_id)), self._running_menu_manager.menu)
107  base_path = plugin_descriptor.attributes().get('plugin_path')
108  self._enrich_action(action, action_attributes, base_path)
109 
110  self._running_mapper.setMapping(action, str(instance_id))
111  action.triggered.connect(self._running_mapper.map)
112 
113  self._running_menu_manager.add_item(action)
114  self._instances[instance_id] = action
115 
116  def remove_instance(self, instance_id):
117  action = self._instances[instance_id]
118  self._running_mapper.removeMappings(action)
119  self._running_menu_manager.remove_item(action)
120 
121  @Slot(str, str)
122  def update_plugin_instance_label(self, instance_id_str, label):
123  instance_id = PluginInstanceId(instance_id=instance_id_str)
124  action = self._instances[instance_id]
125  action.setText(self._get_instance_label(label))
126 
127  def _get_instance_label(self, label):
128  return self.tr('Close:') + ' ' + label
129 
130  def _enrich_action(self, action, action_attributes, base_path=None):
131  if 'icon' in action_attributes and action_attributes['icon'] is not None:
132  icon = get_icon(
133  action_attributes['icon'], action_attributes.get('icontype', None), base_path)
134  action.setIcon(icon)
135 
136  if 'statustip' in action_attributes:
137  action.setStatusTip(action_attributes['statustip'])
def _enrich_action(self, action, action_attributes, base_path=None)
Definition: plugin_menu.py:130
def remove_instance(self, instance_id)
Definition: plugin_menu.py:116
def add_plugin(self, plugin_descriptor)
Definition: plugin_menu.py:64
def _get_instance_label(self, label)
Definition: plugin_menu.py:127
def update_plugin_instance_label(self, instance_id_str, label)
Definition: plugin_menu.py:122
def __init__(self, menu_bar, plugin_manager)
Definition: plugin_menu.py:45
def add_instance(self, plugin_descriptor, instance_id)
Definition: plugin_menu.py:103
def get_icon(name, type_=None, base_path=None)
Definition: icon_loader.py:39
def add_plugin_prefix(self, plugin_descriptor)
Definition: plugin_menu.py:95


qt_gui
Author(s): Dirk Thomas
autogenerated on Tue Apr 13 2021 03:03:12