openlp/openlp/core/ui/shortcutlistform.py

484 lines
23 KiB
Python
Raw Normal View History

2010-10-07 21:27:26 +00:00
# -*- coding: utf-8 -*-
2019-04-13 13:00:22 +00:00
##########################################################################
# OpenLP - Open Source Lyrics Projection #
# ---------------------------------------------------------------------- #
2022-02-01 10:10:57 +00:00
# Copyright (c) 2008-2022 OpenLP Developers #
2019-04-13 13:00:22 +00:00
# ---------------------------------------------------------------------- #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
##########################################################################
2013-02-01 21:34:23 +00:00
"""
2017-10-07 07:05:07 +00:00
The :mod:`~openlp.core.ui.shortcutlistform` module contains the form class
"""
import logging
import re
2015-11-07 00:49:40 +00:00
from PyQt5 import QtCore, QtGui, QtWidgets
2010-10-07 21:27:26 +00:00
2016-03-31 16:34:22 +00:00
from openlp.core.common.actions import ActionList
2017-10-07 07:05:07 +00:00
from openlp.core.common.i18n import translate
2017-10-23 22:09:57 +00:00
from openlp.core.common.mixins import RegistryProperties
2017-10-07 07:05:07 +00:00
from openlp.core.ui.shortcutlistdialog import Ui_ShortcutListDialog
2010-10-07 21:27:26 +00:00
2018-10-02 04:39:42 +00:00
2019-07-03 13:23:23 +00:00
REMOVE_AMPERSAND = re.compile(r'&')
log = logging.getLogger(__name__)
2015-11-07 00:49:40 +00:00
class ShortcutListForm(QtWidgets.QDialog, Ui_ShortcutListDialog, RegistryProperties):
2010-10-07 21:27:26 +00:00
"""
The shortcut list dialog
"""
2011-04-02 14:22:08 +00:00
2011-03-29 13:56:49 +00:00
def __init__(self, parent=None):
2013-02-01 21:34:23 +00:00
"""
Constructor
"""
super(ShortcutListForm, self).__init__(parent, QtCore.Qt.WindowSystemMenuHint | QtCore.Qt.WindowTitleHint |
QtCore.Qt.WindowCloseButtonHint)
self.setup_ui(self)
2013-12-24 15:55:01 +00:00
self.changed_actions = {}
2011-04-09 16:11:02 +00:00
self.action_list = ActionList.get_instance()
self.dialog_was_shown = False
2013-12-24 20:45:29 +00:00
self.primary_push_button.toggled.connect(self.on_primary_push_button_clicked)
self.alternate_push_button.toggled.connect(self.on_alternate_push_button_clicked)
self.tree_widget.currentItemChanged.connect(self.on_current_item_changed)
self.tree_widget.itemDoubleClicked.connect(self.on_item_double_clicked)
self.clear_primary_button.clicked.connect(self.on_clear_primary_button_clicked)
self.clear_alternate_button.clicked.connect(self.on_clear_alternate_button_clicked)
2013-12-24 15:55:01 +00:00
self.button_box.clicked.connect(self.on_restore_defaults_clicked)
2013-12-24 20:45:29 +00:00
self.default_radio_button.clicked.connect(self.on_default_radio_button_clicked)
self.custom_radio_button.clicked.connect(self.on_custom_radio_button_clicked)
2011-04-02 14:22:08 +00:00
def keyPressEvent(self, event):
2013-02-01 21:34:23 +00:00
"""
Respond to certain key presses
"""
if event.key() == QtCore.Qt.Key_Space:
self.keyReleaseEvent(event)
2013-12-24 20:45:29 +00:00
elif self.primary_push_button.isChecked() or self.alternate_push_button.isChecked():
self.keyReleaseEvent(event)
2011-04-03 13:24:51 +00:00
elif event.key() == QtCore.Qt.Key_Escape:
event.accept()
self.close()
def keyReleaseEvent(self, event):
2013-02-01 21:34:23 +00:00
"""
Respond to certain key presses
"""
2013-12-24 20:45:29 +00:00
if not self.primary_push_button.isChecked() and not self.alternate_push_button.isChecked():
return
# Do not continue, as the event is for the dialog (close it).
if self.dialog_was_shown and event.key() in (QtCore.Qt.Key_Escape, QtCore.Qt.Key_Enter, QtCore.Qt.Key_Return):
self.dialog_was_shown = False
return
key = event.key()
2013-02-20 07:29:03 +00:00
if key in (QtCore.Qt.Key_Shift, QtCore.Qt.Key_Control, QtCore.Qt.Key_Meta, QtCore.Qt.Key_Alt):
return
2012-10-11 10:53:39 +00:00
key_string = QtGui.QKeySequence(key).toString()
2012-12-29 13:35:16 +00:00
if event.modifiers() & QtCore.Qt.ControlModifier == QtCore.Qt.ControlModifier:
2013-08-31 18:17:38 +00:00
key_string = 'Ctrl+' + key_string
if event.modifiers() & QtCore.Qt.AltModifier == QtCore.Qt.AltModifier:
2013-08-31 18:17:38 +00:00
key_string = 'Alt+' + key_string
2012-12-29 13:35:16 +00:00
if event.modifiers() & QtCore.Qt.ShiftModifier == QtCore.Qt.ShiftModifier:
2013-08-31 18:17:38 +00:00
key_string = 'Shift+' + key_string
2012-12-29 13:35:16 +00:00
if event.modifiers() & QtCore.Qt.MetaModifier == QtCore.Qt.MetaModifier:
2013-08-31 18:17:38 +00:00
key_string = 'Meta+' + key_string
key_sequence = QtGui.QKeySequence(key_string)
2013-12-24 15:55:01 +00:00
if self._validiate_shortcut(self._current_item_action(), key_sequence):
2013-12-24 20:45:29 +00:00
if self.primary_push_button.isChecked():
self._adjust_button(self.primary_push_button, False,
text=self.get_shortcut_string(key_sequence, for_display=True))
2013-12-24 20:45:29 +00:00
elif self.alternate_push_button.isChecked():
self._adjust_button(self.alternate_push_button, False,
text=self.get_shortcut_string(key_sequence, for_display=True))
2015-11-07 00:49:40 +00:00
def exec(self):
2013-02-01 21:34:23 +00:00
"""
Execute the dialog
"""
2013-12-24 15:55:01 +00:00
self.changed_actions = {}
self.reload_shortcut_list()
2013-12-24 20:45:29 +00:00
self._adjust_button(self.primary_push_button, False, False, '')
self._adjust_button(self.alternate_push_button, False, False, '')
2015-11-07 00:49:40 +00:00
return QtWidgets.QDialog.exec(self)
2013-12-24 15:55:01 +00:00
def reload_shortcut_list(self):
2011-03-30 18:52:21 +00:00
"""
2013-12-24 20:45:29 +00:00
Reload the ``tree_widget`` list to add new and remove old actions.
2011-03-30 18:52:21 +00:00
"""
2013-12-24 20:45:29 +00:00
self.tree_widget.clear()
2011-04-09 16:11:02 +00:00
for category in self.action_list.categories:
2011-04-03 14:20:55 +00:00
# Check if the category is for internal use only.
if category.name is None:
continue
2015-11-07 00:49:40 +00:00
item = QtWidgets.QTreeWidgetItem([category.name])
2011-03-30 10:27:27 +00:00
for action in category.actions:
2013-04-28 18:22:36 +00:00
action_text = REMOVE_AMPERSAND.sub('', action.text())
2015-11-07 00:49:40 +00:00
action_item = QtWidgets.QTreeWidgetItem([action_text])
2013-04-28 18:22:36 +00:00
action_item.setIcon(0, action.icon())
action_item.setData(0, QtCore.Qt.UserRole, action)
tool_tip_text = action.toolTip()
# Only display tool tips if they are helpful.
if tool_tip_text != action_text:
# Display the tool tip in all three colums.
action_item.setToolTip(0, tool_tip_text)
action_item.setToolTip(1, tool_tip_text)
action_item.setToolTip(2, tool_tip_text)
item.addChild(action_item)
2013-12-24 20:45:29 +00:00
self.tree_widget.addTopLevelItem(item)
2011-04-04 17:33:42 +00:00
item.setExpanded(True)
2013-12-24 15:55:01 +00:00
self.refresh_shortcut_list()
2013-12-24 15:55:01 +00:00
def refresh_shortcut_list(self):
"""
2013-02-19 10:50:36 +00:00
This refreshes the item's shortcuts shown in the list. Note, this neither adds new actions nor removes old
actions.
"""
2015-11-07 00:49:40 +00:00
iterator = QtWidgets.QTreeWidgetItemIterator(self.tree_widget)
while iterator.value():
item = iterator.value()
iterator += 1
2013-12-24 15:55:01 +00:00
action = self._current_item_action(item)
if action is None:
continue
2013-12-24 15:55:01 +00:00
shortcuts = self._action_shortcuts(action)
if not shortcuts:
2013-08-31 18:17:38 +00:00
item.setText(1, '')
item.setText(2, '')
2011-04-03 13:24:51 +00:00
elif len(shortcuts) == 1:
item.setText(1, self.get_shortcut_string(shortcuts[0], for_display=True))
2013-08-31 18:17:38 +00:00
item.setText(2, '')
else:
item.setText(1, self.get_shortcut_string(shortcuts[0], for_display=True))
item.setText(2, self.get_shortcut_string(shortcuts[1], for_display=True))
2013-12-24 15:55:01 +00:00
self.on_current_item_changed()
2013-12-24 15:55:01 +00:00
def on_primary_push_button_clicked(self, toggled):
2011-03-30 18:52:21 +00:00
"""
Save the new primary shortcut.
2011-03-30 18:52:21 +00:00
"""
2013-12-24 20:45:29 +00:00
self.custom_radio_button.setChecked(True)
2011-03-29 13:56:49 +00:00
if toggled:
2013-12-24 20:45:29 +00:00
self.alternate_push_button.setChecked(False)
self.primary_push_button.setText('')
2011-03-29 13:56:49 +00:00
return
2013-12-24 15:55:01 +00:00
action = self._current_item_action()
2011-03-29 13:56:49 +00:00
if action is None:
return
2013-12-24 15:55:01 +00:00
shortcuts = self._action_shortcuts(action)
2013-12-24 20:45:29 +00:00
new_shortcuts = [QtGui.QKeySequence(self.primary_push_button.text())]
2011-04-07 15:37:07 +00:00
if len(shortcuts) == 2:
new_shortcuts.append(shortcuts[1])
2013-12-24 15:55:01 +00:00
self.changed_actions[action] = new_shortcuts
self.refresh_shortcut_list()
2011-04-07 15:37:07 +00:00
2013-12-24 15:55:01 +00:00
def on_alternate_push_button_clicked(self, toggled):
2011-04-07 15:37:07 +00:00
"""
Save the new alternate shortcut.
2011-04-07 15:37:07 +00:00
"""
2013-12-24 20:45:29 +00:00
self.custom_radio_button.setChecked(True)
2011-04-07 15:37:07 +00:00
if toggled:
2013-12-24 20:45:29 +00:00
self.primary_push_button.setChecked(False)
self.alternate_push_button.setText('')
return
2013-12-24 15:55:01 +00:00
action = self._current_item_action()
2011-04-07 15:37:07 +00:00
if action is None:
return
2013-12-24 15:55:01 +00:00
shortcuts = self._action_shortcuts(action)
2011-04-07 15:37:07 +00:00
new_shortcuts = []
if shortcuts:
2011-04-07 15:37:07 +00:00
new_shortcuts.append(shortcuts[0])
2013-12-24 20:45:29 +00:00
new_shortcuts.append(QtGui.QKeySequence(self.alternate_push_button.text()))
2013-12-24 15:55:01 +00:00
self.changed_actions[action] = new_shortcuts
2013-12-24 20:45:29 +00:00
if not self.primary_push_button.text():
2013-02-19 10:50:36 +00:00
# When we do not have a primary shortcut, the just entered alternate shortcut will automatically become the
# primary shortcut. That is why we have to adjust the primary button's text.
2013-12-24 20:45:29 +00:00
self.primary_push_button.setText(self.alternate_push_button.text())
self.alternate_push_button.setText('')
2013-12-24 15:55:01 +00:00
self.refresh_shortcut_list()
2013-12-24 15:55:01 +00:00
def on_item_double_clicked(self, item, column):
"""
2013-02-19 10:50:36 +00:00
A item has been double clicked. The ``primaryPushButton`` will be checked and the item's shortcut will be
displayed.
"""
2013-12-24 15:55:01 +00:00
action = self._current_item_action(item)
if action is None:
return
2013-12-24 20:45:29 +00:00
self.primary_push_button.setChecked(column in [0, 1])
self.alternate_push_button.setChecked(column not in [0, 1])
2011-04-07 15:37:07 +00:00
if column in [0, 1]:
2013-12-24 20:45:29 +00:00
self.primary_push_button.setText('')
self.primary_push_button.setFocus()
2011-04-07 15:37:07 +00:00
else:
2013-12-24 20:45:29 +00:00
self.alternate_push_button.setText('')
self.alternate_push_button.setFocus()
2011-03-29 13:56:49 +00:00
2013-12-24 15:55:01 +00:00
def on_current_item_changed(self, item=None, previousItem=None):
2011-03-30 18:52:21 +00:00
"""
2013-02-19 10:50:36 +00:00
A item has been pressed. We adjust the button's text to the action's shortcut which is encapsulate in the item.
2011-03-30 18:52:21 +00:00
"""
2013-12-24 15:55:01 +00:00
action = self._current_item_action(item)
2013-12-24 20:45:29 +00:00
self.primary_push_button.setEnabled(action is not None)
self.alternate_push_button.setEnabled(action is not None)
2013-08-31 18:17:38 +00:00
primary_text = ''
alternate_text = ''
primary_label_text = ''
alternate_label_text = ''
2011-04-03 14:20:55 +00:00
if action is None:
2013-12-24 20:45:29 +00:00
self.primary_push_button.setChecked(False)
self.alternate_push_button.setChecked(False)
2011-04-03 13:24:51 +00:00
else:
2014-04-22 10:29:15 +00:00
if action.default_shortcuts:
primary_label_text = self.get_shortcut_string(action.default_shortcuts[0], for_display=True)
2014-04-22 10:29:15 +00:00
if len(action.default_shortcuts) == 2:
alternate_label_text = self.get_shortcut_string(action.default_shortcuts[1], for_display=True)
2013-12-24 15:55:01 +00:00
shortcuts = self._action_shortcuts(action)
2013-02-19 10:50:36 +00:00
# We do not want to loose pending changes, that is why we have to keep the text when, this function has not
# been triggered by a signal.
if item is None:
2013-12-24 20:45:29 +00:00
primary_text = self.primary_push_button.text()
alternate_text = self.alternate_push_button.text()
elif len(shortcuts) == 1:
primary_text = self.get_shortcut_string(shortcuts[0], for_display=True)
2011-04-03 13:24:51 +00:00
elif len(shortcuts) == 2:
primary_text = self.get_shortcut_string(shortcuts[0], for_display=True)
alternate_text = self.get_shortcut_string(shortcuts[1], for_display=True)
2013-02-19 10:50:36 +00:00
# When we are capturing a new shortcut, we do not want, the buttons to display the current shortcut.
2013-12-24 20:45:29 +00:00
if self.primary_push_button.isChecked():
2013-08-31 18:17:38 +00:00
primary_text = ''
2013-12-24 20:45:29 +00:00
if self.alternate_push_button.isChecked():
2013-08-31 18:17:38 +00:00
alternate_text = ''
2013-12-24 20:45:29 +00:00
self.primary_push_button.setText(primary_text)
self.alternate_push_button.setText(alternate_text)
self.primary_label.setText(primary_label_text)
self.alternate_label.setText(alternate_label_text)
2013-02-19 10:50:36 +00:00
# We do not want to toggle and radio button, as the function has not been triggered by a signal.
if item is None:
return
2012-12-29 13:35:16 +00:00
if primary_label_text == primary_text and alternate_label_text == alternate_text:
2013-12-24 20:45:29 +00:00
self.default_radio_button.toggle()
else:
2013-12-24 20:45:29 +00:00
self.custom_radio_button.toggle()
2013-12-24 15:55:01 +00:00
def on_restore_defaults_clicked(self, button):
2011-04-02 14:22:08 +00:00
"""
Restores all default shortcuts.
"""
2015-11-07 00:49:40 +00:00
if self.button_box.buttonRole(button) != QtWidgets.QDialogButtonBox.ResetRole:
2011-04-02 14:22:08 +00:00
return
2015-11-07 00:49:40 +00:00
if QtWidgets.QMessageBox.question(self, translate('OpenLP.ShortcutListDialog', 'Restore Default Shortcuts'),
translate('OpenLP.ShortcutListDialog', 'Do you want to restore all '
'shortcuts to their defaults?')
2015-11-07 00:49:40 +00:00
) == QtWidgets.QMessageBox.No:
2011-04-02 14:22:08 +00:00
return
2013-12-24 20:45:29 +00:00
self._adjust_button(self.primary_push_button, False, text='')
self._adjust_button(self.alternate_push_button, False, text='')
2011-04-09 16:11:02 +00:00
for category in self.action_list.categories:
2011-04-02 14:22:08 +00:00
for action in category.actions:
2014-04-22 10:29:15 +00:00
self.changed_actions[action] = action.default_shortcuts
2013-12-24 15:55:01 +00:00
self.refresh_shortcut_list()
2011-04-02 14:22:08 +00:00
2013-12-24 15:55:01 +00:00
def on_default_radio_button_clicked(self, toggled):
"""
2013-02-19 10:50:36 +00:00
The default radio button has been clicked, which means we have to make sure, that we use the default shortcuts
for the action.
"""
if not toggled:
return
2013-12-24 15:55:01 +00:00
action = self._current_item_action()
if action is None:
return
2013-12-24 15:55:01 +00:00
temp_shortcuts = self._action_shortcuts(action)
2014-04-22 10:29:15 +00:00
self.changed_actions[action] = action.default_shortcuts
2013-12-24 15:55:01 +00:00
self.refresh_shortcut_list()
2013-08-31 18:17:38 +00:00
primary_button_text = ''
alternate_button_text = ''
if temp_shortcuts:
primary_button_text = self.get_shortcut_string(temp_shortcuts[0], for_display=True)
if len(temp_shortcuts) == 2:
alternate_button_text = self.get_shortcut_string(temp_shortcuts[1], for_display=True)
2013-12-24 20:45:29 +00:00
self.primary_push_button.setText(primary_button_text)
self.alternate_push_button.setText(alternate_button_text)
2013-12-24 15:55:01 +00:00
def on_custom_radio_button_clicked(self, toggled):
"""
2013-02-19 10:50:36 +00:00
The custom shortcut radio button was clicked, thus we have to restore the custom shortcuts by calling those
functions triggered by button clicks.
"""
if not toggled:
return
action = self._current_item_action()
2019-11-01 11:04:44 +00:00
if action is None:
QtWidgets.QMessageBox.information(self, translate('OpenLP.ShortcutListForm', 'Select an Action'),
translate('OpenLP.ShortcutListForm', 'Select an action and click one '
'of the buttons below to start '
'capturing a new primary or alternate shortcut, respectively.'))
else:
shortcuts = self._action_shortcuts(action)
self.refresh_shortcut_list()
primary_button_text = ''
alternate_button_text = ''
if shortcuts:
primary_button_text = self.get_shortcut_string(shortcuts[0], for_display=True)
if len(shortcuts) == 2:
alternate_button_text = self.get_shortcut_string(shortcuts[1], for_display=True)
self.primary_push_button.setText(primary_button_text)
self.alternate_push_button.setText(alternate_button_text)
2011-03-30 17:50:36 +00:00
def save(self):
2011-03-29 19:52:21 +00:00
"""
2013-02-19 10:50:36 +00:00
Save the shortcuts. **Note**, that we do not have to load the shortcuts, as they are loaded in
:class:`~openlp.core.utils.ActionList`.
2011-03-29 19:52:21 +00:00
"""
2011-04-09 16:11:02 +00:00
for category in self.action_list.categories:
2011-04-03 14:20:55 +00:00
# Check if the category is for internal use only.
if category.name is None:
continue
for action in category.actions:
2013-12-24 15:55:01 +00:00
if action in self.changed_actions:
old_shortcuts = list(map(self.get_shortcut_string, action.shortcuts()))
2013-12-24 15:55:01 +00:00
action.setShortcuts(self.changed_actions[action])
2011-12-10 14:03:41 +00:00
self.action_list.update_shortcut_map(action, old_shortcuts)
2020-06-06 16:05:36 +00:00
self.settings.setValue('shortcuts/' + action.objectName(), action.shortcuts())
2011-04-03 13:24:51 +00:00
2013-12-24 15:55:01 +00:00
def on_clear_primary_button_clicked(self, toggled):
2011-04-07 15:37:07 +00:00
"""
Restore the defaults of this action.
"""
2013-12-24 20:45:29 +00:00
self.primary_push_button.setChecked(False)
2013-12-24 15:55:01 +00:00
action = self._current_item_action()
if action is None:
return
2013-12-24 15:55:01 +00:00
shortcuts = self._action_shortcuts(action)
new_shortcuts = []
2014-04-22 10:29:15 +00:00
if action.default_shortcuts:
new_shortcuts.append(action.default_shortcuts[0])
2013-02-19 10:50:36 +00:00
# We have to check if the primary default shortcut is available. But we only have to check, if the action
# has a default primary shortcut (an "empty" shortcut is always valid and if the action does not have a
# default primary shortcut, then the alternative shortcut (not the default one) will become primary
# shortcut, thus the check will assume that an action were going to have the same shortcut twice.
2012-12-29 13:35:16 +00:00
if not self._validiate_shortcut(action, new_shortcuts[0]) and new_shortcuts[0] != shortcuts[0]:
return
if len(shortcuts) == 2:
new_shortcuts.append(shortcuts[1])
2013-12-24 15:55:01 +00:00
self.changed_actions[action] = new_shortcuts
self.refresh_shortcut_list()
2013-12-24 20:45:29 +00:00
self.on_current_item_changed(self.tree_widget.currentItem())
2011-04-07 15:37:07 +00:00
2013-12-24 15:55:01 +00:00
def on_clear_alternate_button_clicked(self, toggled):
2011-04-07 15:37:07 +00:00
"""
Restore the defaults of this action.
"""
2013-12-24 20:45:29 +00:00
self.alternate_push_button.setChecked(False)
2013-12-24 15:55:01 +00:00
action = self._current_item_action()
2011-04-07 15:37:07 +00:00
if action is None:
return
2013-12-24 15:55:01 +00:00
shortcuts = self._action_shortcuts(action)
new_shortcuts = []
if shortcuts:
new_shortcuts.append(shortcuts[0])
2014-04-22 10:29:15 +00:00
if len(action.default_shortcuts) == 2:
new_shortcuts.append(action.default_shortcuts[1])
if len(new_shortcuts) == 2:
if not self._validiate_shortcut(action, new_shortcuts[1]):
return
2013-12-24 15:55:01 +00:00
self.changed_actions[action] = new_shortcuts
self.refresh_shortcut_list()
2013-12-24 20:45:29 +00:00
self.on_current_item_changed(self.tree_widget.currentItem())
2011-04-07 15:37:07 +00:00
def _validiate_shortcut(self, changing_action, key_sequence):
"""
2013-02-19 10:50:36 +00:00
Checks if the given ``changing_action `` can use the given ``key_sequence``. Returns ``True`` if the
``key_sequence`` can be used by the action, otherwise displays a dialog and returns ``False``.
2014-03-17 19:05:55 +00:00
:param changing_action: The action which wants to use the ``key_sequence``.
:param key_sequence: The key sequence which the action want so use.
"""
is_valid = True
for category in self.action_list.categories:
for action in category.actions:
2013-12-24 15:55:01 +00:00
shortcuts = self._action_shortcuts(action)
if key_sequence not in shortcuts:
continue
if action is changing_action:
2013-12-24 20:45:29 +00:00
if self.primary_push_button.isChecked() and shortcuts.index(key_sequence) == 0:
continue
2013-12-24 20:45:29 +00:00
if self.alternate_push_button.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():
is_valid = False
2013-02-19 10:50:36 +00:00
# The new shortcut is already assigned, but if both shortcuts are only valid in a different widget the
2013-12-24 15:55:01 +00:00
# new shortcut is valid, because they will not interfere.
if action.shortcutContext() in [QtCore.Qt.WindowShortcut, QtCore.Qt.ApplicationShortcut]:
is_valid = False
2013-02-01 21:34:23 +00:00
if changing_action.shortcutContext() in [QtCore.Qt.WindowShortcut, QtCore.Qt.ApplicationShortcut]:
is_valid = False
if not is_valid:
2016-05-20 16:22:06 +00:00
text = translate('OpenLP.ShortcutListDialog',
'The shortcut "{key}" is already assigned to another action,\n'
'please use a different shortcut.'
2016-05-20 16:22:06 +00:00
).format(key=self.get_shortcut_string(key_sequence))
2013-02-20 07:02:48 +00:00
self.main_window.warning_message(translate('OpenLP.ShortcutListDialog', 'Duplicate Shortcut'),
text)
self.dialog_was_shown = True
return is_valid
2013-12-24 15:55:01 +00:00
def _action_shortcuts(self, action):
2011-04-03 13:24:51 +00:00
"""
2013-02-19 10:50:36 +00:00
This returns the shortcuts for the given ``action``, which also includes those shortcuts which are not saved
yet but already assigned (as changes are applied when closing the dialog).
2011-04-03 13:24:51 +00:00
"""
2013-12-24 15:55:01 +00:00
if action in self.changed_actions:
return self.changed_actions[action]
2011-04-03 13:24:51 +00:00
return action.shortcuts()
2011-04-07 15:37:07 +00:00
2013-12-24 15:55:01 +00:00
def _current_item_action(self, item=None):
2011-04-07 15:37:07 +00:00
"""
2013-02-19 10:50:36 +00:00
Returns the action of the given ``item``. If no item is given, we return the action of the current item of
2013-12-24 20:45:29 +00:00
the ``tree_widget``.
2011-04-07 15:37:07 +00:00
"""
if item is None:
2013-12-24 20:45:29 +00:00
item = self.tree_widget.currentItem()
2011-04-07 15:37:07 +00:00
if item is None:
return
2012-05-19 15:10:05 +00:00
return item.data(0, QtCore.Qt.UserRole)
2011-04-08 06:25:02 +00:00
2013-12-24 15:55:01 +00:00
def _adjust_button(self, button, checked=None, enabled=None, text=None):
2011-04-08 06:25:02 +00:00
"""
Can be called to adjust more properties of the given ``button`` at once.
"""
# Set the text before checking the button, because this emits a signal.
if text is not None:
button.setText(text)
2011-04-08 06:25:02 +00:00
if checked is not None:
button.setChecked(checked)
if enabled is not None:
button.setEnabled(enabled)
@staticmethod
def get_shortcut_string(shortcut, for_display=False):
if for_display:
if any(modifier in shortcut.toString() for modifier in ['Ctrl', 'Alt', 'Meta', 'Shift']):
sequence_format = QtGui.QKeySequence.NativeText
else:
sequence_format = QtGui.QKeySequence.PortableText
else:
sequence_format = QtGui.QKeySequence.PortableText
return shortcut.toString(sequence_format)