]> sigrok.org Git - sigrok-meter.git/blob - settings.py
Allow changing of the recording time.
[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, conv=None):
34         '''Initializes the Settings object.
35
36         :param key: The key of the used 'QSettings' object.
37         :param default: Value returned if the setting doesn't already exist.
38         :param conv: Function used to convert the setting to the correct type.
39         '''
40
41         super(self.__class__, self).__init__()
42
43         self._key = key
44         self._default = default
45         self._conv = conv
46         self._value = None
47
48     def value(self):
49         s = QtCore.QSettings()
50         v = s.value(self._key, self._default)
51         self._value = self._conv(v) if self._conv else v
52         return self._value
53
54     @QtCore.Slot(object)
55     def setValue(self, value):
56         if value != self._value:
57             s = QtCore.QSettings()
58             s.setValue(self._key, value)
59             s.sync()
60             self._value = value
61             self.changed.emit(self._value)
62
63 class _SettingsGroup(object):
64     '''Dummy class to group multiple 'Setting' objects together.'''
65     pass
66
67 def init():
68     '''Creates the 'Settings' objects for all known settings and places them
69     into the module's namespace.
70
71     A QApplication must have been created before this function can be called.
72     '''
73
74     app = QtGui.QApplication.instance()
75     app.setApplicationName('sigrok-meter')
76     app.setOrganizationName('sigrok')
77     app.setOrganizationDomain('sigrok.org')
78
79     mainwindow = _SettingsGroup()
80     mainwindow.size = Setting('mainwindow/size', QtCore.QSize(900, 550))
81     mainwindow.pos  = Setting('mainwindow/pos')
82     globals()['mainwindow'] = mainwindow
83
84     graph = _SettingsGroup()
85     graph.backlog = Setting('graph/backlog', 30, conv=int)
86     globals()['graph'] = graph