Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 from python_qt_binding.QtCore import QObject
00032 from python_qt_binding.QtGui import QAction, QMenu
00033
00034
00035 class MenuManager(QObject):
00036
00037 """Manager for a menu containing alphabetically ordered items as well as additional items before and afterwards."""
00038
00039 def __init__(self, menu):
00040 super(MenuManager, self).__init__()
00041 self.setObjectName('MenuManager')
00042
00043 self.menu = menu
00044 self._prefixes_separator = None
00045 self._ordered_items = []
00046 self._suffixes_separator = None
00047
00048
00049 for action in menu.actions():
00050 menu = action.menu()
00051 if menu is not None:
00052 self._ordered_items.append(menu)
00053 else:
00054 self._ordered_items.append(action)
00055
00056 def add_prefix(self, item):
00057 if self._prefixes_separator is None:
00058 before = self._ordered_items[0] if self._ordered_items else None
00059 if isinstance(before, QMenu):
00060 before = before.menuAction()
00061 self._prefixes_separator = self.menu.insertSeparator(before)
00062 self._insert_item(self._prefixes_separator, item)
00063
00064 def add_item(self, new_item):
00065 for i, item in enumerate(self._ordered_items):
00066 if self._get_item_label(item) > self._get_item_label(new_item):
00067 self._insert_item(item, new_item)
00068 self._ordered_items.insert(i, new_item)
00069 return
00070
00071 before = self._suffixes_separator or None
00072 self._insert_item(before, new_item)
00073 self._ordered_items.append(new_item)
00074
00075 def add_suffix(self, item):
00076 if self._suffixes_separator is None:
00077 self._suffixes_separator = self.menu.addSeparator()
00078 self._insert_item(None, item)
00079
00080 def count_items(self):
00081 return len(self._ordered_items)
00082
00083 def contains_item(self, name):
00084 return self.get_item(name) is not None
00085
00086 def get_item(self, name):
00087 if isinstance(name, QAction) or isinstance(name, QMenu):
00088 if name in self._ordered_items:
00089 return name
00090 return None
00091 for item in self._ordered_items:
00092 if self._get_item_label(item) == name:
00093 return item
00094 return None
00095
00096 def contains_menu(self, name):
00097 return self.get_menu(name) is not None
00098
00099 def get_menu(self, name):
00100 for item in self._ordered_items:
00101 if self._get_item_label(item) == name and isinstance(item, QMenu):
00102 return item
00103 return None
00104
00105 def get_items(self):
00106 names = []
00107 for item in self._ordered_items:
00108 names.append(self._get_item_label(item))
00109 return names
00110
00111 def set_item_checked(self, name, flag):
00112 item = self.get_item(name)
00113 if item is not None:
00114 item.setChecked(flag)
00115
00116 def set_item_disabled(self, name, flag):
00117 item = self.get_item(name)
00118 if item is not None:
00119 item.setDisabled(flag)
00120
00121 def remove_item(self, name):
00122 item = self.get_item(name)
00123 if item is not None:
00124 self.menu.removeAction(item)
00125 self._ordered_items.remove(item)
00126 item.deleteLater()
00127
00128 def _insert_item(self, before, item):
00129 if isinstance(before, QMenu):
00130 before = before.menuAction()
00131 if isinstance(item, QAction):
00132 self.menu.insertAction(before, item)
00133 elif isinstance(item, QMenu):
00134 self.menu.insertMenu(before, item)
00135 elif item is None:
00136 self.menu.insertSeparator(before)
00137 else:
00138 raise UserWarning('unknown item type')
00139
00140 def _get_item_label(self, item):
00141 if isinstance(item, QAction):
00142 return item.text()
00143 elif isinstance(item, QMenu):
00144 return item.title()
00145 else:
00146 raise UserWarning('unknown item type')