def create_popup_menu(*args, **kwargs):
    msg = 'Deprecated, please use mmSolver.ui.menuutils.create_popup_menu'
    warnings.warn(msg)
    import mmSolver.ui.menuutils as menu_utils
    return menu_utils.create_popup_menu(*args, **kwargs)
Example #2
0
def create_item(parents, func_def, is_first_item, is_last_item):
    """
    Create a shelf or menu item with a function definition.

    :param parents: The list of parent items this item will be created
                    under.
    :type parents: [str, ..]

    :param func_def: Function definition.
    :type func_def: {str: .., ..}

    :param is_first_item: Will this item be the start of the hierarchy?
    :type is_first_item: bool

    :param is_last_item: Will this item be the end of the hierarchy?
    :type is_last_item: bool

    :returns: List of items created.
    :rtype: [{}, ..]
    """
    key_suffix = const.KEY_SUFFIX_SHELF
    popup = lookup_value(func_def, const.POPUP_KEY, key_suffix)

    items = []
    for parent in parents:
        if is_first_item:
            # The first item in a shelf must be a button.
            key_suffix = const.KEY_SUFFIX_SHELF
            name = lookup_value(func_def, const.NAME_KEY, key_suffix)
            icon = lookup_value(func_def, const.ICON_KEY, key_suffix)
            tooltip = lookup_value(func_def, const.TOOLTIP_KEY, key_suffix)
            divider = lookup_value(func_def, const.DIVIDER_KEY, key_suffix)
            cmdLang = lookup_value(func_def, const.CMD_LANG_KEY, key_suffix)
            command = lookup_value(func_def, const.CMD_KEY, key_suffix)
            if isinstance(command, (list, tuple)):
                command = str(os.linesep).join(command)

            if divider:
                item = shelf_utils.create_shelf_separator(parent=parent)
                items.append(item)
            else:
                item = shelf_utils.create_shelf_button(parent=parent,
                                                       name=name,
                                                       tooltip=tooltip,
                                                       icon=icon,
                                                       cmd=command,
                                                       cmdLanguage=cmdLang)
                items.append(item)
        elif is_last_item:
            # Last item must be a menu item, not a menu or popup menu.
            key_suffix = const.KEY_SUFFIX_MENU
            name = lookup_value(func_def, const.NAME_KEY, key_suffix)
            tooltip = lookup_value(func_def, const.TOOLTIP_KEY, key_suffix)
            divider = lookup_value(func_def, const.DIVIDER_KEY, key_suffix)
            popup = lookup_value(func_def, const.POPUP_KEY, key_suffix)
            cmdLang = lookup_value(func_def, const.CMD_LANG_KEY, key_suffix)
            command = lookup_value(func_def, const.CMD_KEY, key_suffix)
            if isinstance(command, (list, tuple)):
                command = str(os.linesep).join(command)

            item = menu_utils.create_menu_item(
                parent=parent,
                name=name,
                tooltip=tooltip,
                cmd=command,
                cmdLanguage=cmdLang,
                divider=divider,
                subMenu=False,
            )
            items.append(item)
        elif popup:
            key_suffix = const.KEY_SUFFIX_MENU
            popupBtn = lookup_value(func_def, const.POPUP_BUTTON_KEY,
                                    key_suffix)
            popupPostCmd = lookup_value(func_def, const.POPUP_POST_CMD_KEY,
                                        key_suffix)
            if isinstance(popupPostCmd, (list, tuple)):
                popupPostCmd = str(os.linesep).join(popupPostCmd)

            # Create list of popup buttons, for each button-click used
            # to open them.
            popupBtnMap = {'left': 1, 'middle': 2, 'right': 3}
            popupBtnIndexList = []
            if popupBtn is None:
                popupBtnIndexList = [None]
            else:
                assert isinstance(popupBtn, (list, tuple))
                assert len(popupBtn) > 0
                for btn in popupBtn:
                    index = popupBtnMap.get(btn, None)
                    if index not in popupBtnIndexList:
                        popupBtnIndexList.append(index)

            # A pop-up menu on top of a shelf button. One menu may be
            # created for each mouse button needing to be clicked to
            # activate the pop-up menu..
            for index in popupBtnIndexList:
                item = menu_utils.create_popup_menu(parent=parent,
                                                    postCmd=popupPostCmd,
                                                    button=index)
                items.append(item)
        else:
            # A sub-menu entry, a menu item that will contain other menu
            # items.
            key_suffix = const.KEY_SUFFIX_MENU
            name = lookup_value(func_def, const.NAME_KEY, key_suffix)
            tooltip = lookup_value(func_def, const.TOOLTIP_KEY, key_suffix)
            divider = lookup_value(func_def, const.DIVIDER_KEY, key_suffix)
            tearoff = lookup_value(func_def, const.TEAR_OFF_KEY, key_suffix)

            item = menu_utils.create_menu_item(
                parent=parent,
                name=name,
                tooltip=tooltip,
                divider=divider,
                subMenu=True,
                tearOff=tearoff,
            )
            items.append(item)
    return items