Allow saving the log messages to a file.
[sigrok-meter/gsi.git] / settings.py
bloba04b0eba84fce6fecbf7e4612209db9073d6858f
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, write to the Free Software
18 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 import qtcompat
22 import sigrok.core as sr
24 QtCore = qtcompat.QtCore
25 QtGui = qtcompat.QtGui
27 class Setting(QtCore.QObject):
28 '''Wrapper class around the raw 'QSettings' class that emits signals
29 when the value of the setting changes.'''
31 '''Signal emitted when the setting has changed.'''
32 changed = QtCore.Signal(object)
34 def __init__(self, key, default=None, s=None, d=None):
35 '''Initializes the Settings object.
37 :param key: The key of the used 'QSettings' object.
38 :param default: Value returned if the setting doesn't already exist.
39 :param s: Function used to serialize the value into a string.
40 :param d: Function used to convert a string into a value.
41 '''
43 super(self.__class__, self).__init__()
45 self._key = key
46 self._default = default
47 self._serialize = s if s else (lambda x: x)
48 self._deserialize = d if d else (lambda x: x)
49 self._value = None
51 def value(self):
52 s = QtCore.QSettings()
53 v = s.value(self._key, self._default)
54 self._value = self._deserialize(v)
55 return self._value
57 @QtCore.Slot(object)
58 def setValue(self, value):
59 if value != self._value:
60 s = QtCore.QSettings()
61 s.setValue(self._key, self._serialize(value))
62 s.sync()
63 self._value = value
64 self.changed.emit(self._value)
66 class _SettingsGroup(object):
67 '''Dummy class to group multiple 'Setting' objects together.'''
68 pass
70 _default_loglevel = 'WARN'
72 def _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
83 if not (s in d):
84 s = _default_loglevel
86 return d[s]
88 def _s_loglevel(l):
89 '''Converts a sr.LogLevel into a string.'''
90 return l.name
92 def init():
93 '''Creates the 'Settings' objects for all known settings and places them
94 into the module's namespace.
96 A QApplication must have been created before this function can be called.
97 '''
99 app = QtGui.QApplication.instance()
100 app.setApplicationName('sigrok-meter')
101 app.setOrganizationName('sigrok')
102 app.setOrganizationDomain('sigrok.org')
104 mainwindow = _SettingsGroup()
105 mainwindow.size = Setting('mainwindow/size', QtCore.QSize(900, 550))
106 mainwindow.pos = Setting('mainwindow/pos')
107 globals()['mainwindow'] = mainwindow
109 graph = _SettingsGroup()
110 graph.backlog = Setting('graph/backlog', 30, d=int)
111 globals()['graph'] = graph
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)
117 logging.filename = Setting('logging/filename', '')
118 globals()['logging'] = logging