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): class Ui_ShortcutListDialog(object):
def setupUi(self, shortcutListDialog): def setupUi(self, shortcutListDialog):
shortcutListDialog.setObjectName("shortcutListDialog") shortcutListDialog.setObjectName(u'shortcutListDialog')
shortcutListDialog.resize(500, 438) shortcutListDialog.resize(500, 438)
self.shortcutListLayout = QtGui.QVBoxLayout(shortcutListDialog) self.shortcutListLayout = QtGui.QVBoxLayout(shortcutListDialog)
self.shortcutListLayout.setSpacing(8) self.shortcutListLayout.setObjectName(u'shortcutListLayout')
self.shortcutListLayout.setMargin(8)
self.shortcutListLayout.setObjectName("shortcutListLayout")
self.treeWidget = QtGui.QTreeWidget(shortcutListDialog) self.treeWidget = QtGui.QTreeWidget(shortcutListDialog)
self.treeWidget.setObjectName(u'treeWidget')
self.treeWidget.setAlternatingRowColors(True) self.treeWidget.setAlternatingRowColors(True)
self.treeWidget.setObjectName("treeWidget")
self.treeWidget.setColumnCount(3) self.treeWidget.setColumnCount(3)
self.shortcutListLayout.addWidget(self.treeWidget) self.shortcutListLayout.addWidget(self.treeWidget)
self.detailsLayout = QtGui.QGridLayout() self.detailsLayout = QtGui.QGridLayout()
self.detailsLayout.setSpacing(8) self.detailsLayout.setObjectName(u'detailsLayout')
self.detailsLayout.setContentsMargins(-1, 0, -1, -1) self.detailsLayout.setContentsMargins(-1, 0, -1, -1)
self.detailsLayout.setObjectName("detailsLayout")
self.defaultRadioButton = QtGui.QRadioButton(shortcutListDialog) self.defaultRadioButton = QtGui.QRadioButton(shortcutListDialog)
self.defaultRadioButton.setObjectName(u'defaultRadioButton')
self.defaultRadioButton.setChecked(True) self.defaultRadioButton.setChecked(True)
self.defaultRadioButton.setObjectName("defaultRadioButton")
self.detailsLayout.addWidget(self.defaultRadioButton, 0, 0, 1, 1) self.detailsLayout.addWidget(self.defaultRadioButton, 0, 0, 1, 1)
self.customRadioButton = QtGui.QRadioButton(shortcutListDialog) self.customRadioButton = QtGui.QRadioButton(shortcutListDialog)
self.customRadioButton.setObjectName("customRadioButton") self.customRadioButton.setObjectName(u'customRadioButton')
self.detailsLayout.addWidget(self.customRadioButton, 1, 0, 1, 1) self.detailsLayout.addWidget(self.customRadioButton, 1, 0, 1, 1)
self.primaryLayout = QtGui.QHBoxLayout() self.primaryLayout = QtGui.QHBoxLayout()
self.primaryLayout.setSpacing(8) self.primaryLayout.setObjectName(u'primaryLayout')
self.primaryLayout.setObjectName("primaryLayout")
self.primaryPushButton = QtGui.QPushButton(shortcutListDialog) self.primaryPushButton = QtGui.QPushButton(shortcutListDialog)
self.primaryPushButton.setObjectName(u'primaryPushButton')
self.primaryPushButton.setMinimumSize(QtCore.QSize(84, 0)) 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.setCheckable(True)
self.primaryPushButton.setChecked(False)
self.primaryPushButton.setObjectName("primaryPushButton")
self.primaryLayout.addWidget(self.primaryPushButton) self.primaryLayout.addWidget(self.primaryPushButton)
self.clearPrimaryButton = QtGui.QToolButton(shortcutListDialog) self.clearPrimaryButton = QtGui.QToolButton(shortcutListDialog)
self.clearPrimaryButton.setObjectName(u'clearPrimaryButton')
self.clearPrimaryButton.setMinimumSize(QtCore.QSize(0, 16)) self.clearPrimaryButton.setMinimumSize(QtCore.QSize(0, 16))
self.clearPrimaryButton.setText("") self.clearPrimaryButton.setIcon(
self.clearPrimaryButton.setIcon(build_icon(u':/system/clear_shortcut.png')) build_icon(u':/system/clear_shortcut.png'))
self.clearPrimaryButton.setObjectName("clearPrimaryButton")
self.primaryLayout.addWidget(self.clearPrimaryButton) self.primaryLayout.addWidget(self.clearPrimaryButton)
self.detailsLayout.addLayout(self.primaryLayout, 1, 1, 1, 1) self.detailsLayout.addLayout(self.primaryLayout, 1, 1, 1, 1)
self.alternateLayout = QtGui.QHBoxLayout() self.alternateLayout = QtGui.QHBoxLayout()
self.alternateLayout.setSpacing(8) self.alternateLayout.setObjectName(u'alternateLayout')
self.alternateLayout.setObjectName("alternateLayout")
self.alternatePushButton = QtGui.QPushButton(shortcutListDialog) self.alternatePushButton = QtGui.QPushButton(shortcutListDialog)
self.alternatePushButton.setIcon(build_icon(u':/system/system_configure_shortcuts.png')) self.alternatePushButton.setObjectName(u'alternatePushButton')
self.alternatePushButton.setObjectName("alternatePushButton") self.alternatePushButton.setCheckable(True)
self.alternatePushButton.setIcon(
build_icon(u':/system/system_configure_shortcuts.png'))
self.alternateLayout.addWidget(self.alternatePushButton) self.alternateLayout.addWidget(self.alternatePushButton)
self.clearAlternateButton = QtGui.QToolButton(shortcutListDialog) self.clearAlternateButton = QtGui.QToolButton(shortcutListDialog)
self.clearAlternateButton.setText("") self.clearAlternateButton.setObjectName(u'clearAlternateButton')
self.clearAlternateButton.setIcon(build_icon(u':/system/clear_shortcut.png')) self.clearAlternateButton.setIcon(
self.clearAlternateButton.setObjectName("clearAlternateButton") build_icon(u':/system/clear_shortcut.png'))
self.alternateLayout.addWidget(self.clearAlternateButton) self.alternateLayout.addWidget(self.clearAlternateButton)
self.detailsLayout.addLayout(self.alternateLayout, 1, 2, 1, 1) self.detailsLayout.addLayout(self.alternateLayout, 1, 2, 1, 1)
self.primaryLabel = QtGui.QLabel(shortcutListDialog) self.primaryLabel = QtGui.QLabel(shortcutListDialog)
self.primaryLabel.setObjectName("primaryLabel") self.primaryLabel.setObjectName(u'primaryLabel')
self.detailsLayout.addWidget(self.primaryLabel, 0, 1, 1, 1) self.detailsLayout.addWidget(self.primaryLabel, 0, 1, 1, 1)
self.alternateLabel = QtGui.QLabel(shortcutListDialog) self.alternateLabel = QtGui.QLabel(shortcutListDialog)
self.alternateLabel.setObjectName("alternateLabel") self.alternateLabel.setObjectName(u'alternateLabel')
self.detailsLayout.addWidget(self.alternateLabel, 0, 2, 1, 1) self.detailsLayout.addWidget(self.alternateLabel, 0, 2, 1, 1)
self.shortcutListLayout.addLayout(self.detailsLayout) self.shortcutListLayout.addLayout(self.detailsLayout)
self.buttonBox = QtGui.QDialogButtonBox(shortcutListDialog) self.buttonBox = QtGui.QDialogButtonBox(shortcutListDialog)
self.buttonBox.setObjectName(u'buttonBox')
self.buttonBox.setOrientation(QtCore.Qt.Horizontal) self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel | self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel |
QtGui.QDialogButtonBox.Ok|QtGui.QDialogButtonBox.Reset) QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.RestoreDefaults)
self.buttonBox.setObjectName("buttonBox")
self.shortcutListLayout.addWidget(self.buttonBox) self.shortcutListLayout.addWidget(self.buttonBox)
self.retranslateUi(shortcutListDialog) self.retranslateUi(shortcutListDialog)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(u'accepted()'), QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(u'accepted()'),
@ -112,10 +109,17 @@ class Ui_ShortcutListDialog(object):
translate('OpenLP.ShortcutListDialog', 'Action'), translate('OpenLP.ShortcutListDialog', 'Action'),
translate('OpenLP.ShortcutListDialog', 'Shortcut'), translate('OpenLP.ShortcutListDialog', 'Shortcut'),
translate('OpenLP.ShortcutListDialog', 'Alternate')]) translate('OpenLP.ShortcutListDialog', 'Alternate')])
self.primaryPushButton.setText( self.defaultRadioButton.setText(
translate('OpenLP.ShortcutListDialog', 'Capture shortcut:')) translate('OpenLP.ShortcutListDialog', 'Default'))
self.alternatePushButton.setText( self.customRadioButton.setText(
translate('OpenLP.ShortcutListDialog', 'Capture shortcut:')) translate('OpenLP.ShortcutListDialog', 'Custom'))
#self.clearShortcutButton.setToolTip( self.primaryPushButton.setToolTip(
#translate('OpenLP.ShortcutListDialog', translate('OpenLP.ShortcutListDialog', 'Capture shortcut.'))
#'Restore the default shortcut(s) of this action.')) 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): def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent) QtGui.QDialog.__init__(self, parent)
self.setupUi(self) self.setupUi(self)
self.column = -1
self.changedActions = {} self.changedActions = {}
self.primaryPushButton.setText(u'')
self.alternatePushButton.setText(u'')
self.primaryPushButton.setEnabled(False)
self.alternatePushButton.setEnabled(False)
QtCore.QObject.connect(self.primaryPushButton, QtCore.QObject.connect(self.primaryPushButton,
QtCore.SIGNAL(u'toggled(bool)'), self.onPrimaryPushButtonClicked) 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.QObject.connect(self.treeWidget,
QtCore.SIGNAL(u'itemPressed(QTreeWidgetItem*, int)'), QtCore.SIGNAL(u'itemPressed(QTreeWidgetItem*, int)'),
self.onItemPressed) self.onItemPressed)
@ -61,14 +58,15 @@ class ShortcutListForm(QtGui.QDialog, Ui_ShortcutListDialog):
self.onItemDoubleClicked) self.onItemDoubleClicked)
QtCore.QObject.connect(self.clearPrimaryButton, QtCore.QObject.connect(self.clearPrimaryButton,
QtCore.SIGNAL(u'clicked(bool)'), self.onClearPrimaryButtonClicked) QtCore.SIGNAL(u'clicked(bool)'), self.onClearPrimaryButtonClicked)
# QtCore.QObject.connect(self.clearShortcutButton, QtCore.QObject.connect(self.clearAlternateButton,
# QtCore.SIGNAL(u'clicked(bool)'), self.onClearPrimaryButtonClicked) QtCore.SIGNAL(u'clicked(bool)'), self.onClearAlternateButtonClicked)
QtCore.QObject.connect(self.buttonBox, QtCore.QObject.connect(self.buttonBox,
QtCore.SIGNAL(u'clicked(QAbstractButton*)'), QtCore.SIGNAL(u'clicked(QAbstractButton*)'),
self.onRestoreDefaultsClicked) self.onRestoreDefaultsClicked)
def keyPressEvent(self, event): def keyPressEvent(self, event):
if self.primaryPushButton.isChecked(): if self.primaryPushButton.isChecked() or \
self.alternatePushButton.isChecked():
event.ignore() event.ignore()
elif event.key() == QtCore.Qt.Key_Escape: elif event.key() == QtCore.Qt.Key_Escape:
event.accept() event.accept()
@ -76,7 +74,8 @@ class ShortcutListForm(QtGui.QDialog, Ui_ShortcutListDialog):
def keyReleaseEvent(self, event): def keyReleaseEvent(self, event):
Qt = QtCore.Qt Qt = QtCore.Qt
if not self.primaryPushButton.isChecked(): if not self.primaryPushButton.isChecked() and \
not self.alternatePushButton.isChecked():
return return
key = event.key() key = event.key()
if key == Qt.Key_Shift or key == Qt.Key_Control or \ 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: if event.modifiers() & Qt.ShiftModifier == Qt.ShiftModifier:
key_string = u'Shift+' + key_string key_string = u'Shift+' + key_string
key_sequence = QtGui.QKeySequence(key_string) key_sequence = QtGui.QKeySequence(key_string)
# The item/action we are attempting to change. # The action we are attempting to change.
changing_item = self.treeWidget.currentItem() changing_action = self._currentItemAction()
changing_action = changing_item.data(0, QtCore.Qt.UserRole).toPyObject()
shortcut_valid = True shortcut_valid = True
for category in ActionList.categories: for category in ActionList.categories:
for action in category.actions: for action in category.actions:
@ -100,11 +98,18 @@ class ShortcutListForm(QtGui.QDialog, Ui_ShortcutListDialog):
if key_sequence not in shortcuts: if key_sequence not in shortcuts:
continue continue
if action is changing_action: if action is changing_action:
continue if self.primaryPushButton.isChecked() and \
# Have the same parentWidget, thus they cannot have the same shortcuts.index(key_sequence) == 0:
# shortcut. 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(): if action.parent() is changing_action.parent():
shortcut_valid = False 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, if action.shortcutContext() in [QtCore.Qt.WindowShortcut,
QtCore.Qt.ApplicationShortcut]: QtCore.Qt.ApplicationShortcut]:
shortcut_valid = False shortcut_valid = False
@ -121,8 +126,12 @@ class ShortcutListForm(QtGui.QDialog, Ui_ShortcutListDialog):
QtGui.QMessageBox.Ok QtGui.QMessageBox.Ok
) )
else: else:
self.primaryPushButton.setText(key_sequence.toString()) if self.primaryPushButton.isChecked():
self.primaryPushButton.setChecked(False) 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): def exec_(self):
self.changedActions = {} self.changedActions = {}
@ -130,6 +139,9 @@ class ShortcutListForm(QtGui.QDialog, Ui_ShortcutListDialog):
self.primaryPushButton.setChecked(False) self.primaryPushButton.setChecked(False)
self.primaryPushButton.setEnabled(False) self.primaryPushButton.setEnabled(False)
self.primaryPushButton.setText(u'') self.primaryPushButton.setText(u'')
self.alternatePushButton.setChecked(False)
self.alternatePushButton.setEnabled(False)
self.alternatePushButton.setText(u'')
return QtGui.QDialog.exec_(self) return QtGui.QDialog.exec_(self)
def reloadShortcutList(self): def reloadShortcutList(self):
@ -162,7 +174,7 @@ class ShortcutListForm(QtGui.QDialog, Ui_ShortcutListDialog):
while iterator.value(): while iterator.value():
item = iterator.value() item = iterator.value()
iterator += 1 iterator += 1
action = item.data(0, QtCore.Qt.UserRole).toPyObject() action = self._currentItemAction(item)
if action is None: if action is None:
continue continue
shortcuts = self._actionShortcuts(action) 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. Save the new shortcut to the action if the button is unchanged.
""" """
if toggled: if toggled:
self.alternatePushButton.setChecked(False)
return return
item = self.treeWidget.currentItem() action = self._currentItemAction()
if item is None:
return
action = item.data(0, QtCore.Qt.UserRole).toPyObject()
if action is None: if action is None:
return return
shortcuts = self._actionShortcuts(action) shortcuts = self._actionShortcuts(action)
new_shortcuts = [] new_shortcuts = []
# We are changing the primary shortcut. new_shortcuts.append(
if self.column in [0, 1]: QtGui.QKeySequence(self.primaryPushButton.text()))
new_shortcuts.append( if len(shortcuts) == 2:
QtGui.QKeySequence(self.primaryPushButton.text())) new_shortcuts.append(shortcuts[1])
if len(shortcuts) == 2: self.changedActions[action] = new_shortcuts
new_shortcuts.append(shortcuts[1]) self.refreshShortcutList()
# We are changing the secondary shortcut.
elif self.column == 2: def onAlternatePushButtonClicked(self, toggled):
if len(shortcuts) != 0: """
new_shortcuts.append(shortcuts[0]) """
new_shortcuts.append( if toggled:
QtGui.QKeySequence(self.primaryPushButton.text())) self.primaryPushButton.setChecked(False)
else:
return 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.changedActions[action] = new_shortcuts
self.refreshShortcutList() self.refreshShortcutList()
@ -212,47 +230,39 @@ class ShortcutListForm(QtGui.QDialog, Ui_ShortcutListDialog):
A item has been double clicked. ``The primaryPushButton`` will be A item has been double clicked. ``The primaryPushButton`` will be
checked and the item's shortcut will be displayed. 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: if action is None:
return return
self.primaryPushButton.setChecked(True) self.primaryPushButton.setChecked(column in [0, 1])
self.primaryPushButton.setFocus(QtCore.Qt.OtherFocusReason) self.alternatePushButton.setChecked(column not in [0, 1])
self.onItemPressed(item, column) 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 A item has been pressed. We adjust the button's text to the action's
shortcut which is encapsulate in the item. shortcut which is encapsulate in the item.
""" """
self.column = column action = self._currentItemAction(item)
action = item.data(0, QtCore.Qt.UserRole).toPyObject() self.primaryPushButton.setEnabled(action is not None)
text = u'' self.alternatePushButton.setEnabled(action is not None)
primary_text = u''
alternate_text = u''
if action is None: if action is None:
self.primaryPushButton.setChecked(False) self.primaryPushButton.setChecked(False)
self.primaryPushButton.setEnabled(False) self.alternatePushButton.setChecked(False)
else: else:
self.primaryPushButton.setEnabled(True)
shortcuts = self._actionShortcuts(action) shortcuts = self._actionShortcuts(action)
if column != 2 and len(shortcuts) != 0: if len(shortcuts) == 1:
text = shortcuts[0].toString() primary_text = shortcuts[0].toString()
elif len(shortcuts) == 2: elif len(shortcuts) == 2:
text = shortcuts[1].toString() primary_text = shortcuts[0].toString()
self.primaryPushButton.setText(text) alternate_text = shortcuts[1].toString()
self.primaryPushButton.setText(primary_text)
def onClearPrimaryButtonClicked(self, toggled): self.alternatePushButton.setText(alternate_text)
"""
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)
def onRestoreDefaultsClicked(self, button): def onRestoreDefaultsClicked(self, button):
""" """
@ -269,6 +279,8 @@ class ShortcutListForm(QtGui.QDialog, Ui_ShortcutListDialog):
return return
self.primaryPushButton.setChecked(False) self.primaryPushButton.setChecked(False)
self.primaryPushButton.setText(u'') self.primaryPushButton.setText(u'')
self.alternatePushButton.setChecked(False)
self.alternatePushButton.setText(u'')
for category in ActionList.categories: for category in ActionList.categories:
for action in category.actions: for action in category.actions:
self.changedActions[action] = action.defaultShortcuts self.changedActions[action] = action.defaultShortcuts
@ -292,6 +304,30 @@ class ShortcutListForm(QtGui.QDialog, Ui_ShortcutListDialog):
action.objectName(), QtCore.QVariant(action.shortcuts())) action.objectName(), QtCore.QVariant(action.shortcuts()))
settings.endGroup() 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): def _actionShortcuts(self, action):
""" """
This returns the shortcuts for the given ``action``, which also includes 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): if self.changedActions.has_key(action):
return self.changedActions[action] return self.changedActions[action]
return action.shortcuts() 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()