doc: update IRC reference to Libera.Chat
[sigrok-meter/gsi.git] / settings.py
blob8f060d3918239c54ff78e43598d82410063974c6
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.
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.
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
20 import qtcompat
21 import sigrok.core as sr
23 QtCore = qtcompat.QtCore
24 QtGui = qtcompat.QtGui
26 class Setting(QtCore.QObject):
27 '''Wrapper class around the raw 'QSettings' class that emits signals
28 when the value of the setting changes.'''
30 '''Signal emitted when the setting has changed.'''
31 changed = QtCore.Signal(object)
33 def __init__(self, key, default=None, s=None, d=None):
34 '''Initializes the Settings object.
36 :param key: The key of the used 'QSettings' object.
37 :param default: Value returned if the setting doesn't already exist.
38 :param s: Function used to serialize the value into a string.
39 :param d: Function used to convert a string into a value.
40 '''
42 super(self.__class__, self).__init__()
44 self._key = key
45 self._default = default
46 self._serialize = s if s else (lambda x: x)
47 self._deserialize = d if d else (lambda x: x)
48 self._value = None
50 def value(self):
51 s = QtCore.QSettings()
52 v = s.value(self._key, self._default)
53 self._value = self._deserialize(v)
54 return self._value
56 @QtCore.Slot(object)
57 def setValue(self, value):
58 if value != self._value:
59 s = QtCore.QSettings()
60 s.setValue(self._key, self._serialize(value))
61 s.sync()
62 self._value = value
63 self.changed.emit(self._value)
65 class _SettingsGroup(object):
66 '''Dummy class to group multiple 'Setting' objects together.'''
67 pass
69 _default_loglevel = 'WARN'
71 def _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
82 if not (s in d):
83 s = _default_loglevel
85 return d[s]
87 def _s_loglevel(l):
88 '''Converts a sr.LogLevel into a string.'''
89 return l.name
91 def init():
92 '''Creates the 'Settings' objects for all known settings and places them
93 into the module's namespace.
95 A QApplication must have been created before this function can be called.
96 '''
98 app = QtGui.QApplication.instance()
99 app.setApplicationName('sigrok-meter')
100 app.setOrganizationName('sigrok')
101 app.setOrganizationDomain('sigrok.org')
103 mainwindow = _SettingsGroup()
104 mainwindow.size = Setting('mainwindow/size', QtCore.QSize(900, 550))
105 mainwindow.pos = Setting('mainwindow/pos')
106 globals()['mainwindow'] = mainwindow
108 graph = _SettingsGroup()
109 graph.backlog = Setting('graph/backlog', 30, d=int)
110 globals()['graph'] = graph
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)
116 logging.filename = Setting('logging/filename', '')
117 globals()['logging'] = logging