]> sigrok.org Git - sigrok-meter.git/blame - settings.py
Allow saving the log messages to a file.
[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
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
21import qtcompat
a6fe45e1 22import sigrok.core as sr
2abf5a93
JS
23
24QtCore = qtcompat.QtCore
25QtGui = qtcompat.QtGui
26
27class Setting(QtCore.QObject):
28 '''Wrapper class around the raw 'QSettings' class that emits signals
29 when the value of the setting changes.'''
30
31 '''Signal emitted when the setting has changed.'''
32 changed = QtCore.Signal(object)
33
a6fe45e1 34 def __init__(self, key, default=None, s=None, d=None):
ac584e86
JS
35 '''Initializes the Settings object.
36
37 :param key: The key of the used 'QSettings' object.
38 :param default: Value returned if the setting doesn't already exist.
a6fe45e1
JS
39 :param s: Function used to serialize the value into a string.
40 :param d: Function used to convert a string into a value.
ac584e86
JS
41 '''
42
2abf5a93
JS
43 super(self.__class__, self).__init__()
44
45 self._key = key
46 self._default = default
a6fe45e1
JS
47 self._serialize = s if s else (lambda x: x)
48 self._deserialize = d if d else (lambda x: x)
2abf5a93
JS
49 self._value = None
50
51 def value(self):
52 s = QtCore.QSettings()
ac584e86 53 v = s.value(self._key, self._default)
a6fe45e1 54 self._value = self._deserialize(v)
2abf5a93
JS
55 return self._value
56
57 @QtCore.Slot(object)
58 def setValue(self, value):
59 if value != self._value:
60 s = QtCore.QSettings()
a6fe45e1 61 s.setValue(self._key, self._serialize(value))
2abf5a93
JS
62 s.sync()
63 self._value = value
64 self.changed.emit(self._value)
65
66class _SettingsGroup(object):
67 '''Dummy class to group multiple 'Setting' objects together.'''
68 pass
69
a6fe45e1
JS
70_default_loglevel = 'WARN'
71
72def _d_loglevel(s):
73 '''Converts a string into a sr.LogLevel.'''
74 d = {
75 'NONE': sr.LogLevel.NONE,
76 'ERR': sr.LogLevel.ERR,
77 'WARN': sr.LogLevel.WARN,
78 'INFO': sr.LogLevel.INFO,
79 'DBG': sr.LogLevel.DBG,
80 'SPEW': sr.LogLevel.SPEW
81 }
82
83 if not (s in d):
84 s = _default_loglevel
85
86 return d[s]
87
88def _s_loglevel(l):
89 '''Converts a sr.LogLevel into a string.'''
90 return l.name
91
2abf5a93
JS
92def init():
93 '''Creates the 'Settings' objects for all known settings and places them
94 into the module's namespace.
95
96 A QApplication must have been created before this function can be called.
97 '''
98
99 app = QtGui.QApplication.instance()
100 app.setApplicationName('sigrok-meter')
101 app.setOrganizationName('sigrok')
102 app.setOrganizationDomain('sigrok.org')
103
104 mainwindow = _SettingsGroup()
105 mainwindow.size = Setting('mainwindow/size', QtCore.QSize(900, 550))
106 mainwindow.pos = Setting('mainwindow/pos')
107 globals()['mainwindow'] = mainwindow
ac584e86
JS
108
109 graph = _SettingsGroup()
a6fe45e1 110 graph.backlog = Setting('graph/backlog', 30, d=int)
ac584e86 111 globals()['graph'] = graph
a6fe45e1
JS
112
113 logging = _SettingsGroup()
114 logging.level = Setting('logging/level', _default_loglevel,
115 s=_s_loglevel, d=_d_loglevel)
116 logging.lines = Setting('logging/lines', 1000, d=int)
c46081ed 117 logging.filename = Setting('logging/filename', '')
a6fe45e1 118 globals()['logging'] = logging