def extend_menu(menu, items, useSeparatorName=False): actions = [] for item in items: if item == "-": action = menu.addSeparator() actions.append(action) elif isinstance(item, basestring) and item.startswith("--"): name = item[item.rfind("-") + 1:] action = menu.addSeparator() action.setObjectName(text2objectname(name, prefix="section")) if useSeparatorName: action.setText(name) actions.append(action) elif isinstance(item, dict) and 'items' in item: submenu, subactions = create_menu(menu, item) subaction = menu.addMenu(submenu) actions.append(subaction) actions.extend(subactions) elif isinstance(item, dict): action = create_action(menu, item) actions.append(action) menu.addAction(action) elif isinstance(item, QtGui.QAction): menu.addAction(item) elif isinstance(item, QtGui.QMenu): menu.addMenu(item) elif isinstance(item, list): actionGroup = QtGui.QActionGroup(menu) actions.append(actionGroup) actionGroup.setExclusive(False) map(menu.addAction, item) map(lambda action: action.setActionGroup(actionGroup), item) elif isinstance(item, tuple): actionGroup = QtGui.QActionGroup(menu) actions.append(actionGroup) actionGroup.setExclusive(True) map(menu.addAction, item) map(lambda action: action.setActionGroup(actionGroup), item) else: raise Exception("%s" % item) return actions
def extend_menu(menu, items, useSeparatorName = False): actions = [] for item in items: if item == "-": action = menu.addSeparator() actions.append(action) elif isinstance(item, basestring) and item.startswith("--"): name = item[item.rfind("-") + 1:] action = menu.addSeparator() action.setObjectName(text2objectname(name, prefix = "section")) if useSeparatorName: action.setText(name) actions.append(action) elif isinstance(item, dict) and 'items' in item: submenu, subactions = create_menu(menu, item) subaction = menu.addMenu(submenu) actions.append(subaction) actions.extend(subactions) elif isinstance(item, dict): action = create_action(menu, item) actions.append(action) menu.addAction(action) elif isinstance(item, QtGui.QAction): menu.addAction(item) elif isinstance(item, QtGui.QMenu): menu.addMenu(item) elif isinstance(item, list): actionGroup = QtGui.QActionGroup(menu) actions.append(actionGroup) actionGroup.setExclusive(False) map(menu.addAction, item) map(lambda action: action.setActionGroup(actionGroup), item) elif isinstance(item, tuple): actionGroup = QtGui.QActionGroup(menu) actions.append(actionGroup) actionGroup.setExclusive(True) map(menu.addAction, item) map(lambda action: action.setActionGroup(actionGroup), item) else: raise Exception("%s" % item) return actions
def extend_menu(rootMenu, settings, dispatcher = None, separatorName = False, sequence_handler = None, icon_resolver = None): collectedObjects = [ rootMenu ] for item in settings: objects = None if item == "-": objects = rootMenu.addSeparator() objects.setObjectName(text_to_objectname("None", prefix = "separator")) elif isinstance(item, six.string_types) and item.startswith("--"): name = item[item.rfind("-") + 1:] objects = rootMenu.addSeparator() objects.setObjectName(text_to_objectname(name, prefix = "separator")) if separatorName: objects.setText(name) elif isinstance(item, dict) and 'items' in item: objects = create_menu(rootMenu.parent(), item, dispatcher = dispatcher, separatorName = separatorName, allObjects = True, sequence_handler = sequence_handler, icon_resolver = icon_resolver) add_actions(rootMenu, [ objects[0] ], item.get("before", None)) elif isinstance(item, dict): objects = create_action(rootMenu.parent(), item, dispatcher = dispatcher, sequence_handler = sequence_handler, icon_resolver = icon_resolver) add_actions(rootMenu, [ objects ], item.get("before", None)) elif isinstance(item, QtGui.QAction): rootMenu.addAction(item) objects = item elif isinstance(item, QtGui.QMenu): objects = rootMenu.addMenu(item) elif isinstance(item, (tuple, list)): actionGroup = QtGui.QActionGroup(rootMenu.parent()) actionGroup.setExclusive(isinstance(item, tuple)) objects = [] for action in item: if isinstance(action, dict): action = create_action(rootMenu.parent(), action, dispatcher = dispatcher, sequence_handler = sequence_handler, icon_resolver = icon_resolver) if action == "-": action = rootMenu.addSeparator() action.setObjectName(text_to_objectname("None", prefix = "separator")) elif isinstance(action, str) and action.startswith("--"): name = action[action.rfind("-") + 1:] action = rootMenu.addSeparator() action.setObjectName(text_to_objectname(name, prefix = "separator")) if separatorName: action.setText(name) else: rootMenu.addAction(action) action.setActionGroup(actionGroup) objects.append(action) else: raise Exception("%s" % item) if objects is not None: getattr(collectedObjects, isinstance(objects, (tuple, list)) and "extend" or "append")(objects) return collectedObjects
def extend_menu(rootMenu, settings, dispatcher = None, separatorName = False, sequence_handler = None, icon_handler = None): collectedObjects = [] for item in settings: if item == "-": separator = rootMenu.addSeparator() separator.setObjectName(text_to_objectname("None", prefix = "separator")) collectedObjects.append(separator) elif isinstance(item, six.string_types) and item.startswith("--"): name = item[item.rfind("-") + 1:] separator = rootMenu.addSeparator() separator.setObjectName(text_to_objectname(name, prefix = "separator")) if separatorName: separator.setText(name) collectedObjects.append(separator) elif isinstance(item, dict) and 'items' in item: menu, objects = create_menu(rootMenu.parent(), item, dispatcher = dispatcher, separatorName = separatorName, sequence_handler = sequence_handler, icon_handler = icon_handler) add_actions(rootMenu, [ menu ], item.get("before", None), prefix="actionMenu") collectedObjects.append(menu) collectedObjects.extend(objects) elif isinstance(item, dict): action = create_action(rootMenu.parent(), item, dispatcher = dispatcher, sequence_handler = sequence_handler, icon_handler = icon_handler) add_actions(rootMenu, [ action ], item.get("before", None), prefix="action") collectedObjects.append(action) elif isinstance(item, QtWidgets.QAction): rootMenu.addAction(item) collectedObjects.append(item) elif isinstance(item, QtWidgets.QMenu): rootMenu.addMenu(item) collectedObjects.append(item) elif isinstance(item, (tuple, list)): actionGroup = QtWidgets.QActionGroup(rootMenu.parent()) actionGroup.setExclusive(isinstance(item, tuple)) for action in item: if isinstance(action, dict): action = create_action(rootMenu.parent(), action, dispatcher = dispatcher, sequence_handler = sequence_handler, icon_handler = icon_handler) if action == "-": action = rootMenu.addSeparator() action.setObjectName(text_to_objectname("None", prefix = "separator")) elif isinstance(action, str) and action.startswith("--"): name = action[action.rfind("-") + 1:] action = rootMenu.addSeparator() action.setObjectName(text_to_objectname(name, prefix = "separator")) if separatorName: action.setText(name) else: rootMenu.addAction(action) action.setActionGroup(actionGroup) collectedObjects.append(action) else: raise Exception("%s" % item) return collectedObjects