Exemple #1
0
def create_toolbutton(parent, text=None, shortcut=None, icon=None, tip=None,
                      toggled=None, triggered=None,
                      autoraise=True, text_beside_icon=False,
                      section=None, option=None, id_=None, plugin=None,
                      context_name=None, register_toolbutton=False):
    """Create a QToolButton"""
    button = QToolButton(parent)
    if text is not None:
        button.setText(text)
    if icon is not None:
        if is_text_string(icon):
            icon = ima.get_icon(icon)
        button.setIcon(icon)
    if text is not None or tip is not None:
        button.setToolTip(text if tip is None else tip)
    if text_beside_icon:
        button.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
    button.setAutoRaise(autoraise)
    if triggered is not None:
        button.clicked.connect(triggered)
    if toggled is not None:
        setup_toggled_action(button, toggled, section, option)
    if shortcut is not None:
        button.setShortcut(shortcut)
    if id_ is not None:
        button.ID = id_

    if register_toolbutton:
        TOOLBUTTON_REGISTRY.register_reference(
            button, id_, plugin, context_name)
    return button
Exemple #2
0
def create_action(parent,
                  text,
                  shortcut=None,
                  icon=None,
                  tip=None,
                  toggled=None,
                  triggered=None,
                  data=None,
                  menurole=None,
                  context=Qt.WindowShortcut,
                  option=None,
                  section=None,
                  id_=None,
                  plugin=None,
                  context_name=None,
                  register_action=False,
                  overwrite=False):
    """Create a QAction"""
    action = SpyderAction(text, parent, action_id=id_)
    if triggered is not None:
        action.triggered.connect(triggered)
    if toggled is not None:
        setup_toggled_action(action, toggled, section, option)
    if icon is not None:
        if is_text_string(icon):
            icon = ima.get_icon(icon)
        action.setIcon(icon)
    if tip is not None:
        action.setToolTip(tip)
        action.setStatusTip(tip)
    if data is not None:
        action.setData(to_qvariant(data))
    if menurole is not None:
        action.setMenuRole(menurole)

    # Workround for Mac because setting context=Qt.WidgetShortcut
    # there doesn't have any effect
    if sys.platform == 'darwin':
        action._shown_shortcut = None
        if context == Qt.WidgetShortcut:
            if shortcut is not None:
                action._shown_shortcut = shortcut
            else:
                # This is going to be filled by
                # main.register_shortcut
                action._shown_shortcut = 'missing'
        else:
            if shortcut is not None:
                action.setShortcut(shortcut)
            action.setShortcutContext(context)
    else:
        if shortcut is not None:
            action.setShortcut(shortcut)
        action.setShortcutContext(context)

    if register_action:
        ACTION_REGISTRY.register_reference(action, id_, plugin, context_name,
                                           overwrite)
    return action
Exemple #3
0
def create_python_script_action(parent, text, icon, package, module, args=[]):
    """Create action to run a GUI based Python script"""
    if is_text_string(icon):
        icon = ima.get_icon(icon)
    if programs.python_script_exists(package, module):
        return create_action(parent, text, icon=icon,
                             triggered=lambda:
                             programs.run_python_script(package, module, args))
Exemple #4
0
def create_program_action(parent, text, name, icon=None, nt_name=None):
    """Create action to run a program"""
    if is_text_string(icon):
        icon = ima.get_icon(icon)
    if os.name == 'nt' and nt_name is not None:
        name = nt_name
    path = programs.find_program(name)
    if path is not None:
        return create_action(parent, text, icon=icon,
                             triggered=lambda: programs.run_program(name))
Exemple #5
0
def get_filetype_icon(fname):
    """Return file type icon"""
    ext = osp.splitext(fname)[1]
    if ext.startswith('.'):
        ext = ext[1:]
    return ima.get_icon("%s.png" % ext, ima.icon('FileIcon'))