1
# This file is part of the Frescobaldi project, http://www.frescobaldi.org/
3
# Copyright (c) 2008 - 2011 by Wilbert Berendsen
5
# This program is free software; you can redistribute it and/or
6
# modify it under the terms of the GNU General Public License
7
# as published by the Free Software Foundation; either version 2
8
# of the License, or (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
# See http://www.gnu.org/licenses/ for more information.
21
A dialog to edit the keyboard shortcuts for an action.
24
from __future__ import unicode_literals
26
from PyQt4.QtCore import Qt
27
from PyQt4.QtGui import (
28
QDialog, QDialogButtonBox, QGridLayout, QHBoxLayout, QKeySequence, QLabel,
29
QRadioButton, QVBoxLayout)
33
from . import Separator
34
from keysequencewidget import KeySequenceWidget
37
class ShortcutEditDialog(QDialog):
38
"""A modal dialog to view and/or edit keyboard shortcuts."""
40
def __init__(self, parent=None):
41
super(ShortcutEditDialog, self).__init__(parent)
42
self.setMinimumWidth(400)
45
layout = QVBoxLayout()
47
self.setLayout(layout)
51
p = self.toppixmap = QLabel()
52
l = self.toplabel = QLabel()
60
grid.setColumnStretch(1, 2)
61
layout.addLayout(grid)
63
self.buttonDefault = QRadioButton(self)
64
self.buttonNone = QRadioButton(self)
65
self.buttonCustom = QRadioButton(self)
66
grid.addWidget(self.buttonDefault, 0, 0, 1, 2)
67
grid.addWidget(self.buttonNone, 1, 0, 1, 2)
68
grid.addWidget(self.buttonCustom, 2, 0, 1, 2)
74
l.setStyleSheet("margin-left: 2em;")
75
l.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
76
b = KeySequenceWidget(self)
77
b.keySequenceChanged.connect(self.slotKeySequenceChanged)
79
self.keylabels.append(l)
80
self.keybuttons.append(b)
81
grid.addWidget(l, num+3, 0)
82
grid.addWidget(b, num+3, 1)
84
layout.addWidget(Separator(self))
86
b = QDialogButtonBox(self)
87
b.setStandardButtons(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
89
b.accepted.connect(self.accept)
90
b.rejected.connect(self.reject)
93
def translateUI(self):
94
self.setWindowTitle(app.caption(_("window title", "Edit Shortcut")))
95
self.buttonNone.setText(_("&No shortcut"))
96
self.buttonCustom.setText(_("Use a &custom shortcut:"))
98
self.keylabels[num].setText(_("Alternative #{num}:").format(num=num) if num else _("Primary shortcut:"))
100
def slotKeySequenceChanged(self):
101
"""Called when one of the keysequence buttons has changed."""
102
self.buttonCustom.setChecked(True)
104
def editAction(self, action, default=None):
106
self._action = action
107
self._default = default
108
self.toplabel.setText('<p>{0}</p>'.format(
109
_("Here you can edit the shortcuts for {name}").format(
110
name='<br/><b>{0}</b>:'.format(action.text()))))
111
self.toppixmap.setPixmap(action.icon().pixmap(32))
112
shortcuts = action.shortcuts()
113
self.buttonDefault.setVisible(default is not None)
114
if default is not None and shortcuts == default:
115
self.buttonDefault.setChecked(True)
117
self.buttonCustom.setChecked(True)
119
self.buttonNone.setChecked(True)
120
for num, key in enumerate(shortcuts[:4]):
121
self.keybuttons[num].setShortcut(key)
122
for num in range(len(shortcuts), 4):
123
self.keybuttons[num].clear()
125
ds = "; ".join(key.toString(QKeySequence.NativeText) for key in default)
127
ds = _("no keyboard shortcut", "none")
128
self.buttonDefault.setText(_("Use &default shortcut ({name})").format(name=ds))
132
def done(self, result):
135
if self.buttonDefault.isChecked():
136
shortcuts = self._default
137
elif self.buttonCustom.isChecked():
139
seq = self.keybuttons[num].shortcut()
140
if not seq.isEmpty():
141
shortcuts.append(seq)
142
self._action.setShortcuts(shortcuts)
143
super(ShortcutEditDialog, self).done(result)