2010-10-07 21:27:26 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# --------------------------------------------------------------------------- #
|
2010-12-26 11:04:47 +00:00
|
|
|
# Copyright (c) 2008-2011 Raoul Snyman #
|
|
|
|
# Portions copyright (c) 2008-2011 Tim Bentley, Jonathan Corwin, Michael #
|
2011-03-24 19:04:02 +00:00
|
|
|
# Gorven, Scott Guerrieri, Matthias Hub, Meinert Jordan, Armin Köhler, #
|
|
|
|
# Andreas Preikschat, Mattias Põldaru, Christian Richter, Philip Ridout, #
|
|
|
|
# Maikel Stuivenberg, Martin Thompson, Jon Tibble, Frode Woldsund #
|
2010-10-07 21:27:26 +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; version 2 of the License. #
|
|
|
|
# #
|
|
|
|
# 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, write to the Free Software Foundation, Inc., 59 #
|
|
|
|
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
|
|
|
|
###############################################################################
|
|
|
|
|
2010-10-21 07:42:13 +00:00
|
|
|
import logging
|
|
|
|
import re
|
|
|
|
|
2010-10-07 21:27:26 +00:00
|
|
|
from PyQt4 import QtCore, QtGui
|
|
|
|
|
2010-10-08 16:07:52 +00:00
|
|
|
from openlp.core.utils import translate
|
2011-03-31 08:43:19 +00:00
|
|
|
from openlp.core.utils.actions import ActionList
|
2010-10-07 21:27:26 +00:00
|
|
|
from shortcutlistdialog import Ui_ShortcutListDialog
|
|
|
|
|
2010-10-21 07:42:13 +00:00
|
|
|
REMOVE_AMPERSAND = re.compile(r'&{1}')
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2010-10-07 21:27:26 +00:00
|
|
|
class ShortcutListForm(QtGui.QDialog, Ui_ShortcutListDialog):
|
|
|
|
"""
|
|
|
|
The shortcut list dialog
|
|
|
|
"""
|
2011-03-30 10:27:27 +00:00
|
|
|
#TODO: do not close on ESC
|
2011-03-29 13:56:49 +00:00
|
|
|
def __init__(self, parent=None):
|
2010-10-07 21:27:26 +00:00
|
|
|
QtGui.QDialog.__init__(self, parent)
|
|
|
|
self.setupUi(self)
|
2011-03-30 16:34:40 +00:00
|
|
|
self.column = -1
|
2011-03-29 13:56:49 +00:00
|
|
|
self.shortcutButton.setText(u'')
|
2011-04-01 14:28:25 +00:00
|
|
|
self.shortcutButton.setEnabled(False)
|
2011-01-02 22:24:14 +00:00
|
|
|
QtCore.QObject.connect(self.shortcutButton,
|
|
|
|
QtCore.SIGNAL(u'toggled(bool)'), self.onShortcutButtonClicked)
|
2011-03-29 13:56:49 +00:00
|
|
|
QtCore.QObject.connect(self.treeWidget,
|
|
|
|
QtCore.SIGNAL(u'itemPressed(QTreeWidgetItem*, int)'),
|
|
|
|
self.onItemPressed)
|
2011-03-31 12:50:38 +00:00
|
|
|
QtCore.QObject.connect(self.treeWidget,
|
|
|
|
QtCore.SIGNAL(u'itemDoubleClicked(QTreeWidgetItem*, int)'),
|
|
|
|
self.onItemDoubleClicked)
|
|
|
|
QtCore.QObject.connect(self.clearShortcutButton,
|
|
|
|
QtCore.SIGNAL(u'clicked(bool)'), self.onClearShortcutButtonClicked)
|
2010-10-08 16:07:52 +00:00
|
|
|
|
|
|
|
def keyReleaseEvent(self, event):
|
|
|
|
Qt = QtCore.Qt
|
2011-03-29 13:56:49 +00:00
|
|
|
if not self.shortcutButton.isChecked():
|
2010-10-08 16:07:52 +00:00
|
|
|
return
|
|
|
|
key = event.key()
|
|
|
|
if key == Qt.Key_Shift or key == Qt.Key_Control or \
|
|
|
|
key == Qt.Key_Meta or key == Qt.Key_Alt:
|
|
|
|
return
|
|
|
|
key_string = QtGui.QKeySequence(key).toString()
|
|
|
|
if event.modifiers() & Qt.ControlModifier == Qt.ControlModifier:
|
|
|
|
key_string = u'Ctrl+' + key_string
|
|
|
|
if event.modifiers() & Qt.AltModifier == Qt.AltModifier:
|
|
|
|
key_string = u'Alt+' + key_string
|
|
|
|
if event.modifiers() & Qt.ShiftModifier == Qt.ShiftModifier:
|
2010-11-03 17:19:44 +00:00
|
|
|
key_string = u'Shift+' + key_string
|
2010-10-08 16:07:52 +00:00
|
|
|
key_sequence = QtGui.QKeySequence(key_string)
|
2011-04-01 14:28:25 +00:00
|
|
|
# The item/action we are attempting to change.
|
|
|
|
changing_item = self.treeWidget.currentItem()
|
|
|
|
changing_action = changing_item.data(0, QtCore.Qt.UserRole).toPyObject()
|
|
|
|
shortcut_valid = True
|
|
|
|
for category in ActionList.categories:
|
|
|
|
for action in category.actions:
|
|
|
|
shortcuts = action.shortcuts()
|
|
|
|
if key_sequence not in shortcuts:
|
|
|
|
continue
|
|
|
|
if action is changing_action:
|
|
|
|
continue
|
|
|
|
# Have the same parentWidget, thus they cannot have the same
|
|
|
|
# shortcut.
|
2011-04-02 13:08:54 +00:00
|
|
|
if action.parent() is changing_action.parent():
|
2011-04-01 14:28:25 +00:00
|
|
|
shortcut_valid = False
|
2011-04-02 13:08:54 +00:00
|
|
|
if action.shortcutContext() in [QtCore.Qt.WindowShortcut,
|
|
|
|
QtCore.Qt.ApplicationShortcut]:
|
2011-04-01 14:28:25 +00:00
|
|
|
shortcut_valid = False
|
2011-04-02 13:08:54 +00:00
|
|
|
if changing_action.shortcutContext() in \
|
|
|
|
[QtCore.Qt.WindowShortcut, QtCore.Qt.ApplicationShortcut]:
|
2011-04-01 14:28:25 +00:00
|
|
|
shortcut_valid = False
|
|
|
|
if not shortcut_valid:
|
2011-03-29 13:56:49 +00:00
|
|
|
QtGui.QMessageBox.warning(self,
|
2010-10-08 16:07:52 +00:00
|
|
|
translate('OpenLP.ShortcutListDialog', 'Duplicate Shortcut'),
|
|
|
|
unicode(translate('OpenLP.ShortcutListDialog', 'The shortcut '
|
2011-03-29 13:56:49 +00:00
|
|
|
'"%s" is already assigned to another action, please '
|
|
|
|
'use a different shortcut.')) % key_sequence.toString(),
|
2010-10-08 16:07:52 +00:00
|
|
|
QtGui.QMessageBox.StandardButtons(QtGui.QMessageBox.Ok),
|
|
|
|
QtGui.QMessageBox.Ok
|
|
|
|
)
|
|
|
|
else:
|
2011-01-02 22:24:14 +00:00
|
|
|
self.shortcutButton.setText(key_sequence.toString())
|
2011-03-29 13:56:49 +00:00
|
|
|
self.shortcutButton.setChecked(False)
|
2010-10-08 16:07:52 +00:00
|
|
|
|
2011-03-29 13:56:49 +00:00
|
|
|
def exec_(self):
|
2011-03-31 12:50:38 +00:00
|
|
|
self.reloadShortcutList()
|
2011-04-01 14:28:25 +00:00
|
|
|
self.shortcutButton.setChecked(False)
|
|
|
|
self.shortcutButton.setEnabled(False)
|
|
|
|
self.shortcutButton.setText(u'')
|
2010-10-21 07:42:13 +00:00
|
|
|
return QtGui.QDialog.exec_(self)
|
|
|
|
|
2011-03-31 12:50:38 +00:00
|
|
|
def reloadShortcutList(self):
|
2011-03-30 18:52:21 +00:00
|
|
|
"""
|
|
|
|
Reload the ``treeWidget`` list to add new and remove old actions.
|
|
|
|
"""
|
2011-03-30 10:12:39 +00:00
|
|
|
self.treeWidget.clear()
|
2011-03-31 08:43:19 +00:00
|
|
|
for category in ActionList.categories:
|
2011-03-29 19:52:21 +00:00
|
|
|
item = QtGui.QTreeWidgetItem([category.name])
|
2011-03-30 10:27:27 +00:00
|
|
|
for action in category.actions:
|
2010-10-28 06:36:18 +00:00
|
|
|
actionText = REMOVE_AMPERSAND.sub('', unicode(action.text()))
|
2011-03-31 12:50:38 +00:00
|
|
|
actionItem = QtGui.QTreeWidgetItem([actionText])
|
2010-10-28 06:36:18 +00:00
|
|
|
actionItem.setIcon(0, action.icon())
|
2011-03-29 19:52:21 +00:00
|
|
|
actionItem.setData(0,
|
|
|
|
QtCore.Qt.UserRole, QtCore.QVariant(action))
|
2010-10-28 06:36:18 +00:00
|
|
|
item.addChild(actionItem)
|
|
|
|
item.setExpanded(True)
|
2011-01-02 22:24:14 +00:00
|
|
|
self.treeWidget.addTopLevelItem(item)
|
2011-03-31 12:50:38 +00:00
|
|
|
self.refreshShortcutList()
|
|
|
|
|
|
|
|
def refreshShortcutList(self):
|
|
|
|
"""
|
|
|
|
This refreshes the item's shortcuts shown in the list. Note, this
|
|
|
|
neither adds new actions nor removes old actions.
|
|
|
|
"""
|
|
|
|
iterator = QtGui.QTreeWidgetItemIterator(self.treeWidget)
|
|
|
|
while iterator.value():
|
|
|
|
item = iterator.value()
|
|
|
|
iterator += 1
|
|
|
|
action = item.data(0, QtCore.Qt.UserRole).toPyObject()
|
|
|
|
if action is None:
|
|
|
|
continue
|
|
|
|
if len(action.shortcuts()) == 0:
|
|
|
|
item.setText(1, u'')
|
|
|
|
item.setText(2, u'')
|
|
|
|
elif len(action.shortcuts()) == 1:
|
|
|
|
item.setText(1, action.shortcuts()[0].toString())
|
|
|
|
item.setText(2, u'')
|
|
|
|
else:
|
|
|
|
item.setText(1, action.shortcuts()[0].toString())
|
|
|
|
item.setText(2, action.shortcuts()[1].toString())
|
2010-10-21 07:42:13 +00:00
|
|
|
|
2011-01-02 22:24:14 +00:00
|
|
|
def onShortcutButtonClicked(self, toggled):
|
2011-03-30 18:52:21 +00:00
|
|
|
"""
|
|
|
|
Save the new shortcut to the action if the button is unchanged.
|
|
|
|
"""
|
2011-03-29 13:56:49 +00:00
|
|
|
if toggled:
|
|
|
|
return
|
|
|
|
item = self.treeWidget.currentItem()
|
2011-04-01 14:28:25 +00:00
|
|
|
if item is None:
|
|
|
|
return
|
2011-03-29 13:56:49 +00:00
|
|
|
action = item.data(0, QtCore.Qt.UserRole).toPyObject()
|
|
|
|
if action is None:
|
|
|
|
return
|
2011-03-30 16:34:40 +00:00
|
|
|
shortcuts = []
|
2011-03-31 12:50:38 +00:00
|
|
|
# We are changing the primary shortcut.
|
2011-03-30 16:34:40 +00:00
|
|
|
if self.column == 1:
|
|
|
|
shortcuts.append(QtGui.QKeySequence(self.shortcutButton.text()))
|
|
|
|
if len(action.shortcuts()) == 2:
|
|
|
|
shortcuts.append(action.shortcuts()[1])
|
2011-03-31 12:50:38 +00:00
|
|
|
# We are changing the secondary shortcut.
|
2011-03-30 16:34:40 +00:00
|
|
|
elif self.column == 2:
|
|
|
|
if len(action.shortcuts()) == 1:
|
|
|
|
shortcuts.append(action.shortcuts()[0])
|
|
|
|
shortcuts.append(QtGui.QKeySequence(self.shortcutButton.text()))
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
action.setShortcuts(shortcuts)
|
2011-03-31 12:50:38 +00:00
|
|
|
self.refreshShortcutList()
|
|
|
|
|
|
|
|
def onItemDoubleClicked(self, item, column):
|
|
|
|
"""
|
2011-04-01 14:28:25 +00:00
|
|
|
A item has been double clicked. ``The shortcutButton`` will be checked
|
|
|
|
and the item's shortcut will be displayed.
|
2011-03-31 12:50:38 +00:00
|
|
|
"""
|
|
|
|
action = item.data(0, QtCore.Qt.UserRole).toPyObject()
|
2011-04-01 14:28:25 +00:00
|
|
|
if action is None:
|
|
|
|
return
|
2011-03-31 12:50:38 +00:00
|
|
|
self.shortcutButton.setChecked(True)
|
2011-04-02 13:08:54 +00:00
|
|
|
self.shortcutButton.setFocus(QtCore.Qt.OtherFocusReason)
|
2011-03-31 12:50:38 +00:00
|
|
|
self.onItemPressed(item, column)
|
2011-03-29 13:56:49 +00:00
|
|
|
|
|
|
|
def onItemPressed(self, item, column):
|
2011-03-30 18:52:21 +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 16:34:40 +00:00
|
|
|
self.column = column
|
2011-03-29 13:56:49 +00:00
|
|
|
action = item.data(0, QtCore.Qt.UserRole).toPyObject()
|
2011-03-30 16:34:40 +00:00
|
|
|
self.shortcutButton.setEnabled(True)
|
|
|
|
text = u''
|
2011-03-30 17:50:36 +00:00
|
|
|
if action is None or column not in [1, 2]:
|
2011-04-01 14:28:25 +00:00
|
|
|
self.shortcutButton.setChecked(False)
|
2011-03-30 16:34:40 +00:00
|
|
|
self.shortcutButton.setEnabled(False)
|
2011-03-30 17:50:36 +00:00
|
|
|
elif column == 1 and len(action.shortcuts()) != 0:
|
2011-03-30 16:34:40 +00:00
|
|
|
text = action.shortcuts()[0].toString()
|
2011-03-30 17:50:36 +00:00
|
|
|
elif len(action.shortcuts()) == 2 and len(action.shortcuts()) != 0:
|
2011-03-30 16:34:40 +00:00
|
|
|
text = action.shortcuts()[1].toString()
|
2011-03-29 13:56:49 +00:00
|
|
|
self.shortcutButton.setText(text)
|
2011-03-29 19:52:21 +00:00
|
|
|
|
2011-03-31 12:50:38 +00:00
|
|
|
def onClearShortcutButtonClicked(self, toggled):
|
|
|
|
"""
|
|
|
|
Restore the defaults of this
|
|
|
|
"""
|
|
|
|
item = self.treeWidget.currentItem()
|
2011-04-01 14:28:25 +00:00
|
|
|
self.shortcutButton.setChecked(False)
|
2011-03-31 12:50:38 +00:00
|
|
|
if item is None:
|
|
|
|
return
|
|
|
|
action = item.data(0, QtCore.Qt.UserRole).toPyObject()
|
|
|
|
if action is None:
|
|
|
|
return
|
|
|
|
action.setShortcuts(action.defaultShortcuts)
|
|
|
|
self.refreshShortcutList()
|
2011-04-01 14:28:25 +00:00
|
|
|
self.onItemPressed(item, self.column)
|
2011-03-31 12:50:38 +00:00
|
|
|
|
2011-03-30 17:50:36 +00:00
|
|
|
def save(self):
|
2011-03-29 19:52:21 +00:00
|
|
|
"""
|
2011-03-30 17: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
|
|
|
"""
|
|
|
|
settings = QtCore.QSettings()
|
|
|
|
settings.beginGroup(u'shortcuts')
|
2011-03-31 08:43:19 +00:00
|
|
|
for category in ActionList.categories:
|
2011-03-30 16:34:40 +00:00
|
|
|
for action in category.actions:
|
2011-03-30 17:50:36 +00:00
|
|
|
settings.setValue(
|
|
|
|
action.objectName(), QtCore.QVariant(action.shortcuts()))
|
2011-03-29 19:52:21 +00:00
|
|
|
settings.endGroup()
|