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

« back to all changes in this revision

Viewing changes to frescobaldi_app/preferences/__init__.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
The Preferences Dialog.
 
22
"""
 
23
 
 
24
from __future__ import unicode_literals
 
25
 
 
26
 
 
27
from PyQt4.QtCore import QSettings, QSize, Qt, pyqtSignal
 
28
from PyQt4.QtGui import (
 
29
    QDialog, QDialogButtonBox, QGroupBox, QHBoxLayout, QListWidget,
 
30
    QListWidgetItem, QStackedWidget, QVBoxLayout, QWidget)
 
31
 
 
32
import app
 
33
import util
 
34
import icons
 
35
import widgets
 
36
 
 
37
 
 
38
_prefsindex = 0 # global setting for selected prefs page but not saved on exit
 
39
 
 
40
 
 
41
class PreferencesDialog(QDialog):
 
42
    
 
43
    def __init__(self, mainwindow):
 
44
        super(PreferencesDialog, self).__init__(mainwindow)
 
45
        self.setWindowModality(Qt.WindowModal)
 
46
        self.addAction(mainwindow.actionCollection.help_whatsthis)
 
47
        layout = QVBoxLayout()
 
48
        layout.setSpacing(10)
 
49
        self.setLayout(layout)
 
50
        
 
51
        # listview to the left, stacked widget to the right
 
52
        top = QHBoxLayout()
 
53
        layout.addLayout(top)
 
54
        
 
55
        self.pagelist = QListWidget(self)
 
56
        self.stack = QStackedWidget(self)
 
57
        top.addWidget(self.pagelist, 0)
 
58
        top.addWidget(self.stack, 2)
 
59
        
 
60
        layout.addWidget(widgets.Separator(self))
 
61
        
 
62
        b = self.buttons = QDialogButtonBox(self)
 
63
        b.setStandardButtons(
 
64
            QDialogButtonBox.Ok
 
65
            | QDialogButtonBox.Cancel
 
66
            | QDialogButtonBox.Apply
 
67
            | QDialogButtonBox.Reset
 
68
            | QDialogButtonBox.Help)
 
69
        layout.addWidget(b)
 
70
        b.accepted.connect(self.accept)
 
71
        b.rejected.connect(self.reject)
 
72
        b.button(QDialogButtonBox.Apply).clicked.connect(self.saveSettings)
 
73
        b.button(QDialogButtonBox.Reset).clicked.connect(self.loadSettings)
 
74
        b.button(QDialogButtonBox.Apply).setEnabled(False)
 
75
        
 
76
        # fill the pagelist
 
77
        self.pagelist.setIconSize(QSize(32, 32))
 
78
        self.pagelist.setSpacing(2)
 
79
        for item in (
 
80
            General,
 
81
            LilyPond,
 
82
            Midi,
 
83
            Helpers,
 
84
            Paths,
 
85
            Documentation,
 
86
            Shortcuts,
 
87
            FontsColors,
 
88
            Tools,
 
89
                ):
 
90
            self.pagelist.addItem(item())
 
91
        self.pagelist.currentItemChanged.connect(self.slotCurrentItemChanged)
 
92
        
 
93
        app.translateUI(self, 100)
 
94
        # read our size and selected page
 
95
        util.saveDialogSize(self, "preferences/dialog/size", QSize(500, 300))
 
96
        self.pagelist.setCurrentRow(_prefsindex)
 
97
        
 
98
    def translateUI(self):
 
99
        self.pagelist.setFixedWidth(self.pagelist.sizeHintForColumn(0) + 12)
 
100
        self.setWindowTitle(app.caption(_("Preferences")))
 
101
    
 
102
    def done(self, result):
 
103
        if result and self.buttons.button(QDialogButtonBox.Apply).isEnabled():
 
104
            self.saveSettings()
 
105
        # save our size and selected page
 
106
        global _prefsindex
 
107
        _prefsindex = self.pagelist.currentRow()
 
108
        super(PreferencesDialog, self).done(result)
 
109
    
 
110
    def pages(self):
 
111
        """Yields the settings pages that are already instantiated."""
 
112
        for n in range(self.stack.count()):
 
113
            yield self.stack.widget(n)
 
114
    
 
115
    def loadSettings(self):
 
116
        """Loads the settings on reset."""
 
117
        for page in self.pages():
 
118
            page.loadSettings()
 
119
            page.hasChanges = False
 
120
        self.buttons.button(QDialogButtonBox.Apply).setEnabled(False)
 
121
            
 
122
    def saveSettings(self):
 
123
        """Saves the settings and applies them."""
 
124
        for page in self.pages():
 
125
            if page.hasChanges:
 
126
                page.saveSettings()
 
127
                page.hasChanges = False
 
128
        self.buttons.button(QDialogButtonBox.Apply).setEnabled(False)
 
129
        
 
130
        # emit the signal
 
131
        app.settingsChanged()
 
132
    
 
133
    def slotCurrentItemChanged(self, item):
 
134
        item.activate()
 
135
        
 
136
    def changed(self):
 
137
        """Call this to enable the Apply button."""
 
138
        self.buttons.button(QDialogButtonBox.Apply).setEnabled(True)
 
139
 
 
140
 
 
141
class PrefsItemBase(QListWidgetItem):
 
142
    def __init__(self):
 
143
        super(PrefsItemBase, self).__init__()
 
144
        self._widget = None
 
145
        self.setIcon(icons.get(self.iconName))
 
146
        app.translateUI(self)
 
147
    
 
148
    def activate(self):
 
149
        dlg = self.listWidget().parentWidget()
 
150
        if self._widget is None:
 
151
            w = self._widget = self.widget(dlg)
 
152
            dlg.stack.addWidget(w)
 
153
            w.loadSettings()
 
154
            w.changed.connect(dlg.changed)
 
155
            w.changed.connect(w.markChanged)
 
156
        dlg.stack.setCurrentWidget(self._widget)
 
157
 
 
158
 
 
159
class General(PrefsItemBase):
 
160
    iconName = "preferences-system"
 
161
    def translateUI(self):
 
162
        self.setText(_("General Preferences"))
 
163
 
 
164
    def widget(self, dlg):
 
165
        import general
 
166
        return general.GeneralPrefs(dlg)
 
167
        
 
168
 
 
169
class LilyPond(PrefsItemBase):
 
170
    iconName = "lilypond-run"
 
171
    def translateUI(self):
 
172
        self.setText(_("LilyPond Preferences"))
 
173
        
 
174
    def widget(self, dlg):
 
175
        import lilypond
 
176
        return lilypond.LilyPondPrefs(dlg)
 
177
 
 
178
 
 
179
class Midi(PrefsItemBase):
 
180
    iconName = "audio-volume-medium"
 
181
    def translateUI(self):
 
182
        self.setText(_("MIDI Settings"))
 
183
    
 
184
    def widget(self, dlg):
 
185
        from . import midi
 
186
        return midi.MidiPrefs(dlg)
 
187
 
 
188
 
 
189
class Helpers(PrefsItemBase):
 
190
    iconName = "applications-other"
 
191
    def translateUI(self):
 
192
        self.setText(_("Helper Applications"))
 
193
        
 
194
    def widget(self, dlg):
 
195
        import helpers
 
196
        return helpers.Helpers(dlg)
 
197
 
 
198
 
 
199
class Paths(PrefsItemBase):
 
200
    iconName = "folder-open"
 
201
    def translateUI(self):
 
202
        self.setText(_("Paths"))
 
203
        
 
204
    def widget(self, dlg):
 
205
        from . import paths
 
206
        return paths.Paths(dlg)
 
207
 
 
208
 
 
209
class Documentation(PrefsItemBase):
 
210
    iconName = "help-contents"
 
211
    def translateUI(self):
 
212
        self.setText(_("LilyPond Documentation"))
 
213
        
 
214
    def widget(self, dlg):
 
215
        from . import documentation
 
216
        return documentation.Documentation(dlg)
 
217
 
 
218
 
 
219
class Shortcuts(PrefsItemBase):
 
220
    iconName = "configure-shortcuts"
 
221
    def translateUI(self):
 
222
        self.setText(_("Keyboard Shortcuts"))
 
223
        
 
224
    def widget(self, dlg):
 
225
        from . import shortcuts
 
226
        return shortcuts.Shortcuts(dlg)
 
227
        
 
228
 
 
229
class FontsColors(PrefsItemBase):
 
230
    iconName = "applications-graphics"
 
231
    def translateUI(self):
 
232
        self.setText(_("Fonts & Colors"))
 
233
        
 
234
    def widget(self, dlg):
 
235
        from . import fontscolors
 
236
        return fontscolors.FontsColors(dlg)
 
237
 
 
238
 
 
239
class Tools(PrefsItemBase):
 
240
    iconName = "preferences-other"
 
241
    def translateUI(self):
 
242
        self.setText(_("Tools"))
 
243
        
 
244
    def widget(self, dlg):
 
245
        from . import tools
 
246
        return tools.Tools(dlg)
 
247
 
 
248
 
 
249
class Page(QWidget):
 
250
    """Base class for settings pages."""
 
251
    changed = pyqtSignal()
 
252
    hasChanges = False
 
253
    
 
254
    def markChanged(self):
 
255
        """Called when something changes in the dialog."""
 
256
        self.hasChanges = True
 
257
    
 
258
    def loadSettings(self):
 
259
        """Should load settings from config into our widget."""
 
260
        
 
261
    def saveSettings(self):
 
262
        """Should write settings from our widget to config."""
 
263
 
 
264
    
 
265
class GroupsPage(Page):
 
266
    """Base class for a Page with SettingsGroups.
 
267
    
 
268
    The load and save methods of the SettingsGroup groups are automatically called.
 
269
    
 
270
    """
 
271
    def __init__(self, dialog):
 
272
        super(GroupsPage, self).__init__(dialog)
 
273
        self.groups = []
 
274
        
 
275
    def loadSettings(self):
 
276
        for group in self.groups:
 
277
            group.loadSettings()
 
278
            
 
279
    def saveSettings(self):
 
280
        for group in self.groups:
 
281
            group.saveSettings()
 
282
            
 
283
 
 
284
class Group(QGroupBox):
 
285
    """This is a QGroupBox that auto-adds itself to a Page."""
 
286
    changed = pyqtSignal()
 
287
    
 
288
    def __init__(self, page):
 
289
        super(Group, self).__init__()
 
290
        page.groups.append(self)
 
291
        self.changed.connect(page.changed)
 
292
        
 
293
    def loadSettings(self):
 
294
        """Should load settings from config into our widget."""
 
295
        
 
296
    def saveSettings(self):
 
297
        """Should write settings from our widget to config."""
 
298