integrated second button(s), clean ups

This commit is contained in:
Andreas Preikschat 2011-04-07 17:37:07 +02:00
parent 8d8d660fdd
commit dab699371c
2 changed files with 150 additions and 99 deletions

View File

@ -30,70 +30,67 @@ from openlp.core.lib import translate, build_icon
class Ui_ShortcutListDialog(object):
def setupUi(self, shortcutListDialog):
shortcutListDialog.setObjectName("shortcutListDialog")
shortcutListDialog.setObjectName(u'shortcutListDialog')
shortcutListDialog.resize(500, 438)
self.shortcutListLayout = QtGui.QVBoxLayout(shortcutListDialog)
self.shortcutListLayout.setSpacing(8)
self.shortcutListLayout.setMargin(8)
self.shortcutListLayout.setObjectName("shortcutListLayout")
self.shortcutListLayout.setObjectName(u'shortcutListLayout')
self.treeWidget = QtGui.QTreeWidget(shortcutListDialog)
self.treeWidget.setObjectName(u'treeWidget')
self.treeWidget.setAlternatingRowColors(True)
self.treeWidget.setObjectName("treeWidget")
self.treeWidget.setColumnCount(3)
self.shortcutListLayout.addWidget(self.treeWidget)
self.detailsLayout = QtGui.QGridLayout()
self.detailsLayout.setSpacing(8)
self.detailsLayout.setObjectName(u'detailsLayout')
self.detailsLayout.setContentsMargins(-1, 0, -1, -1)
self.detailsLayout.setObjectName("detailsLayout")
self.defaultRadioButton = QtGui.QRadioButton(shortcutListDialog)
self.defaultRadioButton.setObjectName(u'defaultRadioButton')
self.defaultRadioButton.setChecked(True)
self.defaultRadioButton.setObjectName("defaultRadioButton")
self.detailsLayout.addWidget(self.defaultRadioButton, 0, 0, 1, 1)
self.customRadioButton = QtGui.QRadioButton(shortcutListDialog)
self.customRadioButton.setObjectName("customRadioButton")
self.customRadioButton.setObjectName(u'customRadioButton')
self.detailsLayout.addWidget(self.customRadioButton, 1, 0, 1, 1)
self.primaryLayout = QtGui.QHBoxLayout()
self.primaryLayout.setSpacing(8)
self.primaryLayout.setObjectName("primaryLayout")
self.primaryLayout.setObjectName(u'primaryLayout')
self.primaryPushButton = QtGui.QPushButton(shortcutListDialog)
self.primaryPushButton.setObjectName(u'primaryPushButton')
self.primaryPushButton.setMinimumSize(QtCore.QSize(84, 0))
self.primaryPushButton.setIcon(build_icon(u':/system/system_configure_shortcuts.png'))
self.primaryPushButton.setIcon(
build_icon(u':/system/system_configure_shortcuts.png'))
self.primaryPushButton.setCheckable(True)
self.primaryPushButton.setChecked(False)
self.primaryPushButton.setObjectName("primaryPushButton")
self.primaryLayout.addWidget(self.primaryPushButton)
self.clearPrimaryButton = QtGui.QToolButton(shortcutListDialog)
self.clearPrimaryButton.setObjectName(u'clearPrimaryButton')
self.clearPrimaryButton.setMinimumSize(QtCore.QSize(0, 16))
self.clearPrimaryButton.setText("")
self.clearPrimaryButton.setIcon(build_icon(u':/system/clear_shortcut.png'))
self.clearPrimaryButton.setObjectName("clearPrimaryButton")
self.clearPrimaryButton.setIcon(
build_icon(u':/system/clear_shortcut.png'))
self.primaryLayout.addWidget(self.clearPrimaryButton)
self.detailsLayout.addLayout(self.primaryLayout, 1, 1, 1, 1)
self.alternateLayout = QtGui.QHBoxLayout()
self.alternateLayout.setSpacing(8)
self.alternateLayout.setObjectName("alternateLayout")
self.alternateLayout.setObjectName(u'alternateLayout')
self.alternatePushButton = QtGui.QPushButton(shortcutListDialog)
self.alternatePushButton.setIcon(build_icon(u':/system/system_configure_shortcuts.png'))
self.alternatePushButton.setObjectName("alternatePushButton")
self.alternatePushButton.setObjectName(u'alternatePushButton')
self.alternatePushButton.setCheckable(True)
self.alternatePushButton.setIcon(
build_icon(u':/system/system_configure_shortcuts.png'))
self.alternateLayout.addWidget(self.alternatePushButton)
self.clearAlternateButton = QtGui.QToolButton(shortcutListDialog)
self.clearAlternateButton.setText("")
self.clearAlternateButton.setIcon(build_icon(u':/system/clear_shortcut.png'))
self.clearAlternateButton.setObjectName("clearAlternateButton")
self.clearAlternateButton.setObjectName(u'clearAlternateButton')
self.clearAlternateButton.setIcon(
build_icon(u':/system/clear_shortcut.png'))
self.alternateLayout.addWidget(self.clearAlternateButton)
self.detailsLayout.addLayout(self.alternateLayout, 1, 2, 1, 1)
self.primaryLabel = QtGui.QLabel(shortcutListDialog)
self.primaryLabel.setObjectName("primaryLabel")
self.primaryLabel.setObjectName(u'primaryLabel')
self.detailsLayout.addWidget(self.primaryLabel, 0, 1, 1, 1)
self.alternateLabel = QtGui.QLabel(shortcutListDialog)
self.alternateLabel.setObjectName("alternateLabel")
self.alternateLabel.setObjectName(u'alternateLabel')
self.detailsLayout.addWidget(self.alternateLabel, 0, 2, 1, 1)
self.shortcutListLayout.addLayout(self.detailsLayout)
self.buttonBox = QtGui.QDialogButtonBox(shortcutListDialog)
self.buttonBox.setObjectName(u'buttonBox')
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel |
QtGui.QDialogButtonBox.Ok|QtGui.QDialogButtonBox.Reset)
self.buttonBox.setObjectName("buttonBox")
QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.RestoreDefaults)
self.shortcutListLayout.addWidget(self.buttonBox)
self.retranslateUi(shortcutListDialog)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(u'accepted()'),
@ -112,10 +109,17 @@ class Ui_ShortcutListDialog(object):
translate('OpenLP.ShortcutListDialog', 'Action'),
translate('OpenLP.ShortcutListDialog', 'Shortcut'),
translate('OpenLP.ShortcutListDialog', 'Alternate')])
self.primaryPushButton.setText(
translate('OpenLP.ShortcutListDialog', 'Capture shortcut:'))
self.alternatePushButton.setText(
translate('OpenLP.ShortcutListDialog', 'Capture shortcut:'))
#self.clearShortcutButton.setToolTip(
#translate('OpenLP.ShortcutListDialog',
#'Restore the default shortcut(s) of this action.'))
self.defaultRadioButton.setText(
translate('OpenLP.ShortcutListDialog', 'Default'))
self.customRadioButton.setText(
translate('OpenLP.ShortcutListDialog', 'Custom'))
self.primaryPushButton.setToolTip(
translate('OpenLP.ShortcutListDialog', 'Capture shortcut.'))
self.alternatePushButton.setToolTip(
translate('OpenLP.ShortcutListDialog', 'Capture shortcut.'))
self.clearPrimaryButton.setToolTip(
translate('OpenLP.ShortcutListDialog',
'Restore the default shortcut(s) of this action.'))
self.clearAlternateButton.setToolTip(
translate('OpenLP.ShortcutListDialog',
'Restore the default shortcut(s) of this action.'))

View File

@ -45,14 +45,11 @@ class ShortcutListForm(QtGui.QDialog, Ui_ShortcutListDialog):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
self.setupUi(self)
self.column = -1
self.changedActions = {}
self.primaryPushButton.setText(u'')
self.alternatePushButton.setText(u'')
self.primaryPushButton.setEnabled(False)
self.alternatePushButton.setEnabled(False)
QtCore.QObject.connect(self.primaryPushButton,
QtCore.SIGNAL(u'toggled(bool)'), self.onPrimaryPushButtonClicked)
QtCore.QObject.connect(self.alternatePushButton,
QtCore.SIGNAL(u'toggled(bool)'), self.onAlternatePushButtonClicked)
QtCore.QObject.connect(self.treeWidget,
QtCore.SIGNAL(u'itemPressed(QTreeWidgetItem*, int)'),
self.onItemPressed)
@ -61,14 +58,15 @@ class ShortcutListForm(QtGui.QDialog, Ui_ShortcutListDialog):
self.onItemDoubleClicked)
QtCore.QObject.connect(self.clearPrimaryButton,
QtCore.SIGNAL(u'clicked(bool)'), self.onClearPrimaryButtonClicked)
# QtCore.QObject.connect(self.clearShortcutButton,
# QtCore.SIGNAL(u'clicked(bool)'), self.onClearPrimaryButtonClicked)
QtCore.QObject.connect(self.clearAlternateButton,
QtCore.SIGNAL(u'clicked(bool)'), self.onClearAlternateButtonClicked)
QtCore.QObject.connect(self.buttonBox,
QtCore.SIGNAL(u'clicked(QAbstractButton*)'),
self.onRestoreDefaultsClicked)
def keyPressEvent(self, event):
if self.primaryPushButton.isChecked():
if self.primaryPushButton.isChecked() or \
self.alternatePushButton.isChecked():
event.ignore()
elif event.key() == QtCore.Qt.Key_Escape:
event.accept()
@ -76,7 +74,8 @@ class ShortcutListForm(QtGui.QDialog, Ui_ShortcutListDialog):
def keyReleaseEvent(self, event):
Qt = QtCore.Qt
if not self.primaryPushButton.isChecked():
if not self.primaryPushButton.isChecked() and \
not self.alternatePushButton.isChecked():
return
key = event.key()
if key == Qt.Key_Shift or key == Qt.Key_Control or \
@ -90,9 +89,8 @@ class ShortcutListForm(QtGui.QDialog, Ui_ShortcutListDialog):
if event.modifiers() & Qt.ShiftModifier == Qt.ShiftModifier:
key_string = u'Shift+' + key_string
key_sequence = QtGui.QKeySequence(key_string)
# The item/action we are attempting to change.
changing_item = self.treeWidget.currentItem()
changing_action = changing_item.data(0, QtCore.Qt.UserRole).toPyObject()
# The action we are attempting to change.
changing_action = self._currentItemAction()
shortcut_valid = True
for category in ActionList.categories:
for action in category.actions:
@ -100,11 +98,18 @@ class ShortcutListForm(QtGui.QDialog, Ui_ShortcutListDialog):
if key_sequence not in shortcuts:
continue
if action is changing_action:
continue
# Have the same parentWidget, thus they cannot have the same
# shortcut.
if self.primaryPushButton.isChecked() and \
shortcuts.index(key_sequence) == 0:
continue
if self.alternatePushButton.isChecked() and \
shortcuts.index(key_sequence) == 1:
continue
# Have the same parent, thus they cannot have the same shortcut.
if action.parent() is changing_action.parent():
shortcut_valid = False
# The new shortcut is already assigned, but if both shortcuts
# are only valid in a different widget the new shortcut is
# vaild, because they will not interfere.
if action.shortcutContext() in [QtCore.Qt.WindowShortcut,
QtCore.Qt.ApplicationShortcut]:
shortcut_valid = False
@ -121,8 +126,12 @@ class ShortcutListForm(QtGui.QDialog, Ui_ShortcutListDialog):
QtGui.QMessageBox.Ok
)
else:
self.primaryPushButton.setText(key_sequence.toString())
self.primaryPushButton.setChecked(False)
if self.primaryPushButton.isChecked():
self.primaryPushButton.setText(key_sequence.toString())
self.primaryPushButton.setChecked(False)
elif self.alternatePushButton.isChecked():
self.alternatePushButton.setText(key_sequence.toString())
self.alternatePushButton.setChecked(False)
def exec_(self):
self.changedActions = {}
@ -130,6 +139,9 @@ class ShortcutListForm(QtGui.QDialog, Ui_ShortcutListDialog):
self.primaryPushButton.setChecked(False)
self.primaryPushButton.setEnabled(False)
self.primaryPushButton.setText(u'')
self.alternatePushButton.setChecked(False)
self.alternatePushButton.setEnabled(False)
self.alternatePushButton.setText(u'')
return QtGui.QDialog.exec_(self)
def reloadShortcutList(self):
@ -162,7 +174,7 @@ class ShortcutListForm(QtGui.QDialog, Ui_ShortcutListDialog):
while iterator.value():
item = iterator.value()
iterator += 1
action = item.data(0, QtCore.Qt.UserRole).toPyObject()
action = self._currentItemAction(item)
if action is None:
continue
shortcuts = self._actionShortcuts(action)
@ -181,29 +193,35 @@ class ShortcutListForm(QtGui.QDialog, Ui_ShortcutListDialog):
Save the new shortcut to the action if the button is unchanged.
"""
if toggled:
self.alternatePushButton.setChecked(False)
return
item = self.treeWidget.currentItem()
if item is None:
return
action = item.data(0, QtCore.Qt.UserRole).toPyObject()
action = self._currentItemAction()
if action is None:
return
shortcuts = self._actionShortcuts(action)
new_shortcuts = []
# We are changing the primary shortcut.
if self.column in [0, 1]:
new_shortcuts.append(
QtGui.QKeySequence(self.primaryPushButton.text()))
if len(shortcuts) == 2:
new_shortcuts.append(shortcuts[1])
# We are changing the secondary shortcut.
elif self.column == 2:
if len(shortcuts) != 0:
new_shortcuts.append(shortcuts[0])
new_shortcuts.append(
QtGui.QKeySequence(self.primaryPushButton.text()))
else:
new_shortcuts.append(
QtGui.QKeySequence(self.primaryPushButton.text()))
if len(shortcuts) == 2:
new_shortcuts.append(shortcuts[1])
self.changedActions[action] = new_shortcuts
self.refreshShortcutList()
def onAlternatePushButtonClicked(self, toggled):
"""
"""
if toggled:
self.primaryPushButton.setChecked(False)
return
action = self._currentItemAction()
if action is None:
return
shortcuts = self._actionShortcuts(action)
new_shortcuts = []
if len(shortcuts) != 0:
new_shortcuts.append(shortcuts[0])
new_shortcuts.append(
QtGui.QKeySequence(self.alternatePushButton.text()))
self.changedActions[action] = new_shortcuts
self.refreshShortcutList()
@ -212,47 +230,39 @@ class ShortcutListForm(QtGui.QDialog, Ui_ShortcutListDialog):
A item has been double clicked. ``The primaryPushButton`` will be
checked and the item's shortcut will be displayed.
"""
action = item.data(0, QtCore.Qt.UserRole).toPyObject()
action = self._currentItemAction(item)
if action is None:
return
self.primaryPushButton.setChecked(True)
self.primaryPushButton.setFocus(QtCore.Qt.OtherFocusReason)
self.onItemPressed(item, column)
self.primaryPushButton.setChecked(column in [0, 1])
self.alternatePushButton.setChecked(column not in [0, 1])
if column in [0, 1]:
self.primaryPushButton.setFocus(QtCore.Qt.OtherFocusReason)
else:
self.alternatePushButton.setFocus(QtCore.Qt.OtherFocusReason)
self.onItemPressed()
def onItemPressed(self, item, column):
def onItemPressed(self, item=None, column=-1):
"""
A item has been pressed. We adjust the button's text to the action's
shortcut which is encapsulate in the item.
"""
self.column = column
action = item.data(0, QtCore.Qt.UserRole).toPyObject()
text = u''
action = self._currentItemAction(item)
self.primaryPushButton.setEnabled(action is not None)
self.alternatePushButton.setEnabled(action is not None)
primary_text = u''
alternate_text = u''
if action is None:
self.primaryPushButton.setChecked(False)
self.primaryPushButton.setEnabled(False)
self.alternatePushButton.setChecked(False)
else:
self.primaryPushButton.setEnabled(True)
shortcuts = self._actionShortcuts(action)
if column != 2 and len(shortcuts) != 0:
text = shortcuts[0].toString()
if len(shortcuts) == 1:
primary_text = shortcuts[0].toString()
elif len(shortcuts) == 2:
text = shortcuts[1].toString()
self.primaryPushButton.setText(text)
def onClearPrimaryButtonClicked(self, toggled):
"""
Restore the defaults of this
"""
item = self.treeWidget.currentItem()
self.primaryPushButton.setChecked(False)
if item is None:
return
action = item.data(0, QtCore.Qt.UserRole).toPyObject()
if action is None:
return
self.changedActions[action] = action.defaultShortcuts
self.refreshShortcutList()
self.onItemPressed(item, self.column)
primary_text = shortcuts[0].toString()
alternate_text = shortcuts[1].toString()
self.primaryPushButton.setText(primary_text)
self.alternatePushButton.setText(alternate_text)
def onRestoreDefaultsClicked(self, button):
"""
@ -269,6 +279,8 @@ class ShortcutListForm(QtGui.QDialog, Ui_ShortcutListDialog):
return
self.primaryPushButton.setChecked(False)
self.primaryPushButton.setText(u'')
self.alternatePushButton.setChecked(False)
self.alternatePushButton.setText(u'')
for category in ActionList.categories:
for action in category.actions:
self.changedActions[action] = action.defaultShortcuts
@ -292,6 +304,30 @@ class ShortcutListForm(QtGui.QDialog, Ui_ShortcutListDialog):
action.objectName(), QtCore.QVariant(action.shortcuts()))
settings.endGroup()
def onClearPrimaryButtonClicked(self, toggled):
"""
Restore the defaults of this action.
"""
self._clearButtonClicked(self.primaryPushButton)
def onClearAlternateButtonClicked(self, toggled):
"""
Restore the defaults of this action.
"""
self._clearButtonClicked(self.alternatePushButton)
def _clearButtonClicked(self, button):
"""
Restore the defaults of this action. The given button will be unchecked.
"""
button.setChecked(False)
action = self._currentItemAction()
if action is None:
return
self.changedActions[action] = action.defaultShortcuts
self.refreshShortcutList()
self.onItemPressed()
def _actionShortcuts(self, action):
"""
This returns the shortcuts for the given ``action``, which also includes
@ -301,3 +337,14 @@ class ShortcutListForm(QtGui.QDialog, Ui_ShortcutListDialog):
if self.changedActions.has_key(action):
return self.changedActions[action]
return action.shortcuts()
def _currentItemAction(self, item=None):
"""
Returns the action of the current item if no item is given, otherwise
we return the action of the given item.
"""
if item is None:
item = self.treeWidget.currentItem()
if item is None:
return
return item.data(0, QtCore.Qt.UserRole).toPyObject()