Beispiel #1
0
def addaction(name='',
              icon_name='',
              tip='',
              checkable=False,
              slot=None,
              toolbar=None,
              menu=None):
    """Create a new action and add it eventually to a toolbar and a menu
    Parameters
    ----------
    name: (str) Displayed name if should be displayed (for instance in menus)
    icon_name: (str) png file name to produce the icon
    tip: (str) a tooltip to be displayed when hovering above the action
    checkable: (bool) set the checkable state of the action
    slot: (callable) Method or function that will be called when the action is triggered
    toolbar: (QToolBar) a toolbar where action should be added.
    menu: (QMenu) a menu where action should be added.
    """
    if icon_name != '':
        icon = QtGui.QIcon()
        if Path(icon_name).is_file():
            icon.addPixmap(QtGui.QPixmap(icon_name), QtGui.QIcon.Normal,
                           QtGui.QIcon.Off)
        else:
            icon.addPixmap(
                QtGui.QPixmap(f":/icons/Icon_Library/{icon_name}.png"),
                QtGui.QIcon.Normal, QtGui.QIcon.Off)
        action = QAction(icon, name, None)
    else:
        action = QAction(name)

    if slot is not None:
        action.connect_to(slot)
    action.setCheckable(checkable)
    action.setToolTip(tip)
    if toolbar is not None:
        toolbar.addAction(action)
    if menu is not None:
        menu.addAction(action)
    return action