2 ## This file is part of the sigrok-meter project.
4 ## Copyright (C) 2015 Jens Steinhauser <jens.steinhauser@gmail.com>
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/>.
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.
42 super(self
.__class
__, self
).__init
__()
45 self
._default
= default
46 self
._serialize
= s
if s
else (lambda x
: x
)
47 self
._deserialize
= d
if d
else (lambda x
: x
)
51 s
= QtCore
.QSettings()
52 v
= s
.value(self
._key
, self
._default
)
53 self
._value
= self
._deserialize
(v
)
57 def setValue(self
, value
):
58 if value
!= self
._value
:
59 s
= QtCore
.QSettings()
60 s
.setValue(self
._key
, self
._serialize
(value
))
63 self
.changed
.emit(self
._value
)
65 class _SettingsGroup(object):
66 '''Dummy class to group multiple 'Setting' objects together.'''
69 _default_loglevel
= 'WARN'
72 '''Converts a string into a sr.LogLevel.'''
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
88 '''Converts a sr.LogLevel into a string.'''
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.
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