~ubuntu-branches/ubuntu/vivid/frescobaldi/vivid

« back to all changes in this revision

Viewing changes to frescobaldi_app/widgets/shortcuteditdialog.py

  • Committer: Package Import Robot
  • Author(s): Ryan Kavanagh
  • Date: 2012-01-03 16:20:11 UTC
  • mfrom: (1.4.1)
  • Revision ID: package-import@ubuntu.com-20120103162011-tsjkwl4sntwmprea
Tags: 2.0.0-1
* New upstream release 
* Drop the following uneeded patches:
  + 01_checkmodules_no_python-kde4_build-dep.diff
  + 02_no_pyc.diff
  + 04_no_binary_lilypond_upgrades.diff
* Needs new dependency python-poppler-qt4
* Update debian/watch for new download path
* Update copyright file with new holders and years

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# This file is part of the Frescobaldi project, http://www.frescobaldi.org/
 
2
#
 
3
# Copyright (c) 2008 - 2011 by Wilbert Berendsen
 
4
#
 
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.
 
9
#
 
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.
 
14
#
 
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.
 
19
 
 
20
"""
 
21
A dialog to edit the keyboard shortcuts for an action.
 
22
"""
 
23
 
 
24
from __future__ import unicode_literals
 
25
 
 
26
from PyQt4.QtCore import Qt
 
27
from PyQt4.QtGui import (
 
28
    QDialog, QDialogButtonBox, QGridLayout, QHBoxLayout, QKeySequence, QLabel,
 
29
    QRadioButton, QVBoxLayout)
 
30
 
 
31
 
 
32
import app
 
33
from . import Separator
 
34
from keysequencewidget import KeySequenceWidget
 
35
 
 
36
 
 
37
class ShortcutEditDialog(QDialog):
 
38
    """A modal dialog to view and/or edit keyboard shortcuts."""
 
39
    
 
40
    def __init__(self, parent=None):
 
41
        super(ShortcutEditDialog, self).__init__(parent)
 
42
        self.setMinimumWidth(400)
 
43
        # create gui
 
44
        
 
45
        layout = QVBoxLayout()
 
46
        layout.setSpacing(10)
 
47
        self.setLayout(layout)
 
48
        
 
49
        top = QHBoxLayout()
 
50
        top.setSpacing(4)
 
51
        p = self.toppixmap = QLabel()
 
52
        l = self.toplabel = QLabel()
 
53
        l.setWordWrap(True)
 
54
        top.addWidget(p)
 
55
        top.addWidget(l, 1)
 
56
        layout.addLayout(top)
 
57
        
 
58
        grid = QGridLayout()
 
59
        grid.setSpacing(4)
 
60
        grid.setColumnStretch(1, 2)
 
61
        layout.addLayout(grid)
 
62
        
 
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)
 
69
        
 
70
        self.keybuttons = []
 
71
        self.keylabels = []
 
72
        for num in range(4):
 
73
            l = QLabel(self)
 
74
            l.setStyleSheet("margin-left: 2em;")
 
75
            l.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
 
76
            b = KeySequenceWidget(self)
 
77
            b.keySequenceChanged.connect(self.slotKeySequenceChanged)
 
78
            l.setBuddy(b)
 
79
            self.keylabels.append(l)
 
80
            self.keybuttons.append(b)
 
81
            grid.addWidget(l, num+3, 0)
 
82
            grid.addWidget(b, num+3, 1)
 
83
        
 
84
        layout.addWidget(Separator(self))
 
85
        
 
86
        b = QDialogButtonBox(self)
 
87
        b.setStandardButtons(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
 
88
        layout.addWidget(b)
 
89
        b.accepted.connect(self.accept)
 
90
        b.rejected.connect(self.reject)
 
91
        app.translateUI(self)
 
92
    
 
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:"))
 
97
        for num in range(4):
 
98
            self.keylabels[num].setText(_("Alternative #{num}:").format(num=num) if num else _("Primary shortcut:"))
 
99
    
 
100
    def slotKeySequenceChanged(self):
 
101
        """Called when one of the keysequence buttons has changed."""
 
102
        self.buttonCustom.setChecked(True)
 
103
        
 
104
    def editAction(self, action, default=None):
 
105
        # load the action
 
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)
 
116
        elif shortcuts:
 
117
            self.buttonCustom.setChecked(True)
 
118
        else:
 
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()
 
124
        if default:
 
125
            ds = "; ".join(key.toString(QKeySequence.NativeText) for key in default)
 
126
        else:
 
127
            ds = _("no keyboard shortcut", "none")
 
128
        self.buttonDefault.setText(_("Use &default shortcut ({name})").format(name=ds))
 
129
        
 
130
        return self.exec_()
 
131
        
 
132
    def done(self, result):
 
133
        if result:
 
134
            shortcuts = []
 
135
            if self.buttonDefault.isChecked():
 
136
                shortcuts = self._default
 
137
            elif self.buttonCustom.isChecked():
 
138
                for num in range(4):
 
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)
 
144