make use of **kwargs in action creation methods

This commit is contained in:
M2j 2012-02-27 19:58:34 +01:00
parent 3a6a402b56
commit fe5f3c8874
1 changed files with 43 additions and 45 deletions

View File

@ -280,18 +280,15 @@ def create_up_down_push_button_set(parent):
QtCore.SIGNAL(u'clicked()'), parent.onDownButtonClicked) QtCore.SIGNAL(u'clicked()'), parent.onDownButtonClicked)
return up_button, down_button return up_button, down_button
def create_action(parent, name, text=u'', icon=None, tooltip=u'', statustip=u'', def create_action(parent, name, **kwargs):
checked=None, enabled=True, visible=True, shortcuts=None,
context=QtCore.Qt.WindowShortcut, category=None, trigger=None):
""" """
Return an action with the object name set and the given parameters. Return an action with the object name set and the given parameters.
Parameters with default values are not set in case they stay unchanged.
``parent`` ``parent``
A QtCore.QObject or ``None`` for the actions parent. A QtCore.QObject for the actions parent (required).
``name`` ``name``
A string which is set as object name. A string which is set as object name (required).
``text`` ``text``
A string for the action text. A string for the action text.
@ -307,7 +304,7 @@ def create_action(parent, name, text=u'', icon=None, tooltip=u'', statustip=u'',
A string for the action status tip. A string for the action status tip.
``checked`` ``checked``
Either ``None`` for a not checkable action or bool for the state. A bool for the state. If ``None`` the Action is not checkable.
``enabled`` ``enabled``
False in case the action should be disabled. False in case the action should be disabled.
@ -319,65 +316,66 @@ def create_action(parent, name, text=u'', icon=None, tooltip=u'', statustip=u'',
A QList<QKeySequence> (or a list of strings) which are set as shortcuts. A QList<QKeySequence> (or a list of strings) which are set as shortcuts.
``context`` ``context``
A context for the shortcut execution (only will be set if ``shortcuts`` A context for the shortcut execution (only will be set together with
is not None) ``shortcuts``).
``category`` ``category``
The category the action should be listed in the shortcut dialog. If you A category the action should be listed in the shortcut dialog.
not wish, that this action is added to the shortcut dialog, then do not
state any.
``trigger`` ``triggers``
A slot which is connected to the actions ``triggered()`` slot. A slot which is connected to the actions ``triggered()`` slot.
""" """
action = QtGui.QAction(parent) action = QtGui.QAction(parent)
action.setObjectName(name) action.setObjectName(name)
if text: if kwargs.get(u'text'):
action.setText(text) action.setText(kwargs[u'text'])
if icon is not None: if kwargs.get(u'icon'):
action.setIcon(build_icon(icon)) action.setIcon(build_icon(kwargs[u'icon']))
if tooltip: if kwargs.get('tooltip'):
action.setToolTip(tooltip) action.setToolTip(kwargs['tooltip'])
if statustip: if kwargs.get('statustip'):
action.setStatusTip(statustip) action.setStatusTip(kwargs['statustip'])
if checked is not None: if kwargs.get('checked') is not None:
action.setCheckable(True) action.setCheckable(True)
action.setChecked(checked) action.setChecked(kwargs['checked'])
if not enabled: if not kwargs.get('enabled'):
action.setEnabled(enabled) action.setEnabled(False)
if not visible: if not kwargs.get('visible'):
action.setVisible(visible) action.setVisible(False)
if shortcuts is not None: if kwargs.get('shortcuts'):
action.setShortcuts(shortcuts) action.setShortcuts(kwargs['shortcuts'])
action.setShortcutContext(context) action.setShortcutContext(kwargs.get('context',
if category is not None: QtCore.Qt.WindowShortcut))
if kwargs.get('category'):
action_list = ActionList.get_instance() action_list = ActionList.get_instance()
action_list.add_action(action, category) action_list.add_action(action, kwargs['category'])
if trigger is not None: if kwargs.get('triggers'):
QtCore.QObject.connect(action, QtCore.SIGNAL(u'triggered(bool)'), QtCore.QObject.connect(action, QtCore.SIGNAL(u'triggered(bool)'),
trigger) kwargs['triggers'])
return action return action
def base_action(parent, name, category=None): def base_action(parent, name, category=None, **kwargs):
return create_action(parent, name, category=category) return create_action(parent, name, category=None, **kwargs)
def checkable_action(parent, name, checked=None, category=None): def checkable_action(parent, name, checked=None, category=None, **kwargs):
return create_action(parent, name, checked=bool(checked), category=category) return create_action(parent, name, checked=bool(checked), category=category,
**kwargs)
def icon_action(parent, name, icon, checked=None, category=None): def icon_action(parent, name, icon, checked=None, category=None, **kwargs):
return create_action(parent, name, icon=icon, checked=checked, return create_action(parent, name, icon=icon, checked=checked,
category=category) category=category, **kwargs)
def shortcut_action(parent, name, shortcuts, function, icon=None, checked=None, def shortcut_action(parent, name, shortcuts, function, icon=None, checked=None,
category=None, context=QtCore.Qt.WindowShortcut): category=None, context=QtCore.Qt.WindowShortcut, **kwargs):
return create_action(parent, name, icon=icon, checked=checked, return create_action(parent, name, icon=icon, checked=checked,
shortcuts=shortcuts, context=context, category=category, shortcuts=shortcuts, context=context, category=category,
trigger=function) triggers=function, **kwargs)
def context_menu_action(base, icon, text, slot, shortcuts=None, category=None, def context_menu_action(base, icon, text, slot, shortcuts=None, category=None,
context=QtCore.Qt.WidgetShortcut): context=QtCore.Qt.WidgetShortcut, **kwargs):
return create_action(parent=base, name=u'', icon=icon, text=text, return create_action(parent=base, name=u'', icon=icon, text=text,
trigger=slot, shortcuts=shortcuts, category=category, context=context) triggers=slot, shortcuts=shortcuts, category=category, context=context,
**kwargs)
def context_menu(base, icon, text): def context_menu(base, icon, text):
""" """