menu_manager.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
32 from python_qt_binding.QtWidgets import QAction, QMenu
33 
34 
35 class MenuManager(QObject):
36  """Menu manager containing alphabetically ordered items and other items before and after."""
37 
38  def __init__(self, menu):
39  super(MenuManager, self).__init__()
40  self.setObjectName('MenuManager')
41 
42  self.menu = menu
43  self._prefixes_separator = None
44  self._ordered_items = []
45  self._suffixes_separator = None
46 
47  # get already existing items from menu
48  for action in menu.actions():
49  menu = action.menu()
50  if menu is not None:
51  self._ordered_items.append(menu)
52  else:
53  self._ordered_items.append(action)
54 
55  def add_prefix(self, item):
56  if self._prefixes_separator is None:
57  before = self._ordered_items[0] if self._ordered_items else None
58  if isinstance(before, QMenu):
59  before = before.menuAction()
60  self._prefixes_separator = self.menu.insertSeparator(before)
61  self._insert_item(self._prefixes_separator, item)
62 
63  def add_item(self, new_item):
64  for i, item in enumerate(self._ordered_items):
65  if self._get_item_label(item) > self._get_item_label(new_item):
66  self._insert_item(item, new_item)
67  self._ordered_items.insert(i, new_item)
68  return
69 
70  before = self._suffixes_separator or None
71  self._insert_item(before, new_item)
72  self._ordered_items.append(new_item)
73 
74  def add_suffix(self, item):
75  if self._suffixes_separator is None:
76  self._suffixes_separator = self.menu.addSeparator()
77  self._insert_item(None, item)
78 
79  def count_items(self):
80  return len(self._ordered_items)
81 
82  def contains_item(self, name):
83  return self.get_item(name) is not None
84 
85  def get_item(self, name):
86  if isinstance(name, QAction) or isinstance(name, QMenu):
87  if name in self._ordered_items:
88  return name
89  return None
90  for item in self._ordered_items:
91  if self._get_item_label(item) == name:
92  return item
93  return None
94 
95  def contains_menu(self, name):
96  return self.get_menu(name) is not None
97 
98  def get_menu(self, name):
99  for item in self._ordered_items:
100  if self._get_item_label(item) == name and isinstance(item, QMenu):
101  return item
102  return None
103 
104  def get_items(self):
105  names = []
106  for item in self._ordered_items:
107  names.append(self._get_item_label(item))
108  return names
109 
110  def set_item_checked(self, name, flag):
111  item = self.get_item(name)
112  if item is not None:
113  item.setChecked(flag)
114 
115  def set_item_disabled(self, name, flag):
116  item = self.get_item(name)
117  if item is not None:
118  item.setDisabled(flag)
119 
120  def remove_item(self, name):
121  item = self.get_item(name)
122  if item is not None:
123  self.menu.removeAction(item)
124  self._ordered_items.remove(item)
125  item.deleteLater()
126 
127  def _insert_item(self, before, item):
128  if isinstance(before, QMenu):
129  before = before.menuAction()
130  if isinstance(item, QAction):
131  self.menu.insertAction(before, item)
132  elif isinstance(item, QMenu):
133  self.menu.insertMenu(before, item)
134  elif item is None:
135  self.menu.insertSeparator(before)
136  else:
137  raise UserWarning('unknown item type')
138 
139  def _get_item_label(self, item):
140  if isinstance(item, QAction):
141  return item.text()
142  elif isinstance(item, QMenu):
143  return item.title()
144  else:
145  raise UserWarning('unknown item type')
qt_gui.menu_manager.MenuManager.add_prefix
def add_prefix(self, item)
Definition: menu_manager.py:55
qt_gui.menu_manager.MenuManager.add_item
def add_item(self, new_item)
Definition: menu_manager.py:63
qt_gui.menu_manager.MenuManager._insert_item
def _insert_item(self, before, item)
Definition: menu_manager.py:127
qt_gui.menu_manager.MenuManager.get_menu
def get_menu(self, name)
Definition: menu_manager.py:98
qt_gui.menu_manager.MenuManager.add_suffix
def add_suffix(self, item)
Definition: menu_manager.py:74
qt_gui.menu_manager.MenuManager.count_items
def count_items(self)
Definition: menu_manager.py:79
qt_gui.menu_manager.MenuManager.remove_item
def remove_item(self, name)
Definition: menu_manager.py:120
qt_gui.menu_manager.MenuManager._suffixes_separator
_suffixes_separator
Definition: menu_manager.py:45
qt_gui.menu_manager.MenuManager.contains_menu
def contains_menu(self, name)
Definition: menu_manager.py:95
qt_gui.menu_manager.MenuManager._get_item_label
def _get_item_label(self, item)
Definition: menu_manager.py:139
qt_gui.menu_manager.MenuManager.get_item
def get_item(self, name)
Definition: menu_manager.py:85
qt_gui.menu_manager.MenuManager.set_item_checked
def set_item_checked(self, name, flag)
Definition: menu_manager.py:110
qt_gui.menu_manager.MenuManager.get_items
def get_items(self)
Definition: menu_manager.py:104
qt_gui.menu_manager.MenuManager._ordered_items
_ordered_items
Definition: menu_manager.py:44
qt_gui.menu_manager.MenuManager._prefixes_separator
_prefixes_separator
Definition: menu_manager.py:43
qt_gui.menu_manager.MenuManager.contains_item
def contains_item(self, name)
Definition: menu_manager.py:82
qt_gui.menu_manager.MenuManager.menu
menu
Definition: menu_manager.py:42
qt_gui.menu_manager.MenuManager.__init__
def __init__(self, menu)
Definition: menu_manager.py:38
qt_gui.menu_manager.MenuManager.set_item_disabled
def set_item_disabled(self, name, flag)
Definition: menu_manager.py:115
qt_gui.menu_manager.MenuManager
Definition: menu_manager.py:35


qt_gui
Author(s): Dirk Thomas
autogenerated on Sat Jun 25 2022 02:15:05