-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsettings.py
117 lines (95 loc) · 3.65 KB
/
settings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
##
## This file is part of the sigrok-meter project.
##
## Copyright (C) 2015 Jens Steinhauser <[email protected]>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, see <http://www.gnu.org/licenses/>.
##
import qtcompat
import sigrok.core as sr
QtCore = qtcompat.QtCore
QtGui = qtcompat.QtGui
class Setting(QtCore.QObject):
'''Wrapper class around the raw 'QSettings' class that emits signals
when the value of the setting changes.'''
'''Signal emitted when the setting has changed.'''
changed = QtCore.Signal(object)
def __init__(self, key, default=None, s=None, d=None):
'''Initializes the Settings object.
:param key: The key of the used 'QSettings' object.
:param default: Value returned if the setting doesn't already exist.
:param s: Function used to serialize the value into a string.
:param d: Function used to convert a string into a value.
'''
super(self.__class__, self).__init__()
self._key = key
self._default = default
self._serialize = s if s else (lambda x: x)
self._deserialize = d if d else (lambda x: x)
self._value = None
def value(self):
s = QtCore.QSettings()
v = s.value(self._key, self._default)
self._value = self._deserialize(v)
return self._value
@QtCore.Slot(object)
def setValue(self, value):
if value != self._value:
s = QtCore.QSettings()
s.setValue(self._key, self._serialize(value))
s.sync()
self._value = value
self.changed.emit(self._value)
class _SettingsGroup(object):
'''Dummy class to group multiple 'Setting' objects together.'''
pass
_default_loglevel = 'WARN'
def _d_loglevel(s):
'''Converts a string into a sr.LogLevel.'''
d = {
'NONE': sr.LogLevel.NONE,
'ERR': sr.LogLevel.ERR,
'WARN': sr.LogLevel.WARN,
'INFO': sr.LogLevel.INFO,
'DBG': sr.LogLevel.DBG,
'SPEW': sr.LogLevel.SPEW
}
if not (s in d):
s = _default_loglevel
return d[s]
def _s_loglevel(l):
'''Converts a sr.LogLevel into a string.'''
return l.name
def init():
'''Creates the 'Settings' objects for all known settings and places them
into the module's namespace.
A QApplication must have been created before this function can be called.
'''
app = QtGui.QApplication.instance()
app.setApplicationName('sigrok-meter')
app.setOrganizationName('sigrok')
app.setOrganizationDomain('sigrok.org')
mainwindow = _SettingsGroup()
mainwindow.size = Setting('mainwindow/size', QtCore.QSize(900, 550))
mainwindow.pos = Setting('mainwindow/pos')
globals()['mainwindow'] = mainwindow
graph = _SettingsGroup()
graph.backlog = Setting('graph/backlog', 30, d=int)
globals()['graph'] = graph
logging = _SettingsGroup()
logging.level = Setting('logging/level', _default_loglevel,
s=_s_loglevel, d=_d_loglevel)
logging.lines = Setting('logging/lines', 1000, d=int)
logging.filename = Setting('logging/filename', '')
globals()['logging'] = logging