menu_manager.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 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         # get already existing items from menu
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')


qt_gui
Author(s): Dirk Thomas
autogenerated on Fri Feb 3 2017 03:42:12