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, write to the Free Software
18 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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, conv=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 conv: Function used to convert the setting to the correct type.
41 super(self.__class__, self).__init__()
44 self._default = default
49 s = QtCore.QSettings()
50 v = s.value(self._key, self._default)
51 self._value = self._conv(v) if self._conv else v
55 def setValue(self, value):
56 if value != self._value:
57 s = QtCore.QSettings()
58 s.setValue(self._key, value)
61 self.changed.emit(self._value)
63 class _SettingsGroup(object):
64 '''Dummy class to group multiple 'Setting' objects together.'''
68 '''Creates the 'Settings' objects for all known settings and places them
69 into the module's namespace.
71 A QApplication must have been created before this function can be called.
74 app = QtGui.QApplication.instance()
75 app.setApplicationName('sigrok-meter')
76 app.setOrganizationName('sigrok')
77 app.setOrganizationDomain('sigrok.org')
79 mainwindow = _SettingsGroup()
80 mainwindow.size = Setting('mainwindow/size', QtCore.QSize(900, 550))
81 mainwindow.pos = Setting('mainwindow/pos')
82 globals()['mainwindow'] = mainwindow
84 graph = _SettingsGroup()
85 graph.backlog = Setting('graph/backlog', 30, conv=int)
86 globals()['graph'] = graph