]> sigrok.org Git - sigrok-meter.git/blame - settings.py
license: remove FSF postal address from boiler plate license text
[sigrok-meter.git] / settings.py
CommitLineData
2abf5a93
JS
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
0b63748b 17## along with this program; if not, see <http://www.gnu.org/licenses/>.
2abf5a93
JS
18##
19
20import qtcompat
a6fe45e1 21import sigrok.core as sr
2abf5a93
JS
22
23QtCore = qtcompat.QtCore
24QtGui = qtcompat.QtGui
25
26class 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
a6fe45e1 33 def __init__(self, key, default=None, s=None, d=None):
ac584e86
JS
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.
a6fe45e1
JS
38 :param s: Function used to serialize the value into a string.
39 :param d: Function used to convert a string into a value.
ac584e86
JS
40 '''
41
2abf5a93
JS
42 super(self.__class__, self).__init__()
43
44 self._key = key
45 self._default = default
a6fe45e1
JS
46 self._serialize = s if s else (lambda x: x)
47 self._deserialize = d if d else (lambda x: x)
2abf5a93
JS
48 self._value = None
49
50 def value(self):
51 s = QtCore.QSettings()
ac584e86 52 v = s.value(self._key, self._default)
a6fe45e1 53 self._value = self._deserialize(v)
2abf5a93
JS
54 return self._value
55
56 @QtCore.Slot(object)
57 def setValue(self, value):
58 if value != self._value:
59 s = QtCore.QSettings()
a6fe45e1 60 s.setValue(self._key, self._serialize(value))
2abf5a93
JS
61 s.sync()
62 self._value = value
63 self.changed.emit(self._value)
64
65class _SettingsGroup(object):
66 '''Dummy class to group multiple 'Setting' objects together.'''
67 pass
68
a6fe45e1
JS
69_default_loglevel = 'WARN'
70
71def _d_loglevel(s):
72 '''Converts a string into a sr.LogLevel.'''
73 d = {
74 'NONE': sr.LogLevel.NONE,
75 'ERR': sr.LogLevel.ERR,
76 'WARN': sr.LogLevel.WARN,
77 'INFO': sr.LogLevel.INFO,
78 'DBG': sr.LogLevel.DBG,
79 'SPEW': sr.LogLevel.SPEW
80 }
81
82 if not (s in d):
83 s = _default_loglevel
84
85 return d[s]
86
87def _s_loglevel(l):
88 '''Converts a sr.LogLevel into a string.'''
89 return l.name
90
2abf5a93
JS
91def init():
92 '''Creates the 'Settings' objects for all known settings and places them
93 into the module's namespace.
94
95 A QApplication must have been created before this function can be called.
96 '''
97
98 app = QtGui.QApplication.instance()
99 app.setApplicationName('sigrok-meter')
100 app.setOrganizationName('sigrok')
101 app.setOrganizationDomain('sigrok.org')
102
103 mainwindow = _SettingsGroup()
104 mainwindow.size = Setting('mainwindow/size', QtCore.QSize(900, 550))
105 mainwindow.pos = Setting('mainwindow/pos')
106 globals()['mainwindow'] = mainwindow
ac584e86
JS
107
108 graph = _SettingsGroup()
a6fe45e1 109 graph.backlog = Setting('graph/backlog', 30, d=int)
ac584e86 110 globals()['graph'] = graph
a6fe45e1
JS
111
112 logging = _SettingsGroup()
113 logging.level = Setting('logging/level', _default_loglevel,
114 s=_s_loglevel, d=_d_loglevel)
115 logging.lines = Setting('logging/lines', 1000, d=int)
c46081ed 116 logging.filename = Setting('logging/filename', '')
a6fe45e1 117 globals()['logging'] = logging