]> sigrok.org Git - sigrok-meter.git/blob - settings.py
Save and restore settings.
[sigrok-meter.git] / settings.py
1 ##
2 ## This file is part of the sigrok-meter project.
3 ##
4 ## Copyright (C) 2015 Jens Steinhauser <jens.steinhauser@gmail.com>
5 ##
6 ## This program is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 2 of the License, or
9 ## (at your option) any later version.
10 ##
11 ## This program is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ## GNU General Public License for more details.
15 ##
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, write to the Free Software
18 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19 ##
20
21 import qtcompat
22
23 QtCore = qtcompat.QtCore
24 QtGui = qtcompat.QtGui
25
26 class Setting(QtCore.QObject):
27     '''Wrapper class around the raw 'QSettings' class that emits signals
28     when the value of the setting changes.'''
29
30     '''Signal emitted when the setting has changed.'''
31     changed = QtCore.Signal(object)
32
33     def __init__(self, key, default=None):
34         super(self.__class__, self).__init__()
35
36         self._key = key
37         self._default = default
38         self._value = None
39
40     def value(self):
41         s = QtCore.QSettings()
42         self._value = s.value(self._key, self._default)
43         return self._value
44
45     @QtCore.Slot(object)
46     def setValue(self, value):
47         if value != self._value:
48             s = QtCore.QSettings()
49             s.setValue(self._key, value)
50             s.sync()
51             self._value = value
52             self.changed.emit(self._value)
53
54 class _SettingsGroup(object):
55     '''Dummy class to group multiple 'Setting' objects together.'''
56     pass
57
58 def init():
59     '''Creates the 'Settings' objects for all known settings and places them
60     into the module's namespace.
61
62     A QApplication must have been created before this function can be called.
63     '''
64
65     app = QtGui.QApplication.instance()
66     app.setApplicationName('sigrok-meter')
67     app.setOrganizationName('sigrok')
68     app.setOrganizationDomain('sigrok.org')
69
70     mainwindow = _SettingsGroup()
71     mainwindow.size = Setting('mainwindow/size', QtCore.QSize(900, 550))
72     mainwindow.pos  = Setting('mainwindow/pos')
73     globals()['mainwindow'] = mainwindow