From: Jens Steinhauser Date: Tue, 13 Oct 2015 14:49:57 +0000 (+0200) Subject: Save and restore settings. X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=2abf5a93f7fbca40d4b761e34c4edee008a6b37d;p=sigrok-meter.git Save and restore settings. --- diff --git a/mainwindow.py b/mainwindow.py index 058fe5f..680dd97 100644 --- a/mainwindow.py +++ b/mainwindow.py @@ -25,6 +25,7 @@ import icons import multiplotwidget import os.path import qtcompat +import settings import textwrap import time import util @@ -134,7 +135,9 @@ class MainWindow(QtGui.QMainWindow): layout.setSpacing(0) layout.setContentsMargins(0, 0, 0, 0) - self.resize(900, 550) + self.resize(settings.mainwindow.size.value()) + if settings.mainwindow.pos.value(): + self.move(settings.mainwindow.pos.value()) def _setup_sidebar(self): self.sideBar = QtGui.QToolBar(self) @@ -374,6 +377,8 @@ class MainWindow(QtGui.QMainWindow): self.start_stop_acquisition() event.ignore() else: + settings.mainwindow.size.setValue(self.size()) + settings.mainwindow.pos.setValue(self.pos()) event.accept() @QtCore.Slot() diff --git a/settings.py b/settings.py new file mode 100644 index 0000000..79484e6 --- /dev/null +++ b/settings.py @@ -0,0 +1,73 @@ +## +## This file is part of the sigrok-meter project. +## +## Copyright (C) 2015 Jens Steinhauser +## +## This program is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 2 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program; if not, write to the Free Software +## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +## + +import qtcompat + +QtCore = qtcompat.QtCore +QtGui = qtcompat.QtGui + +class Setting(QtCore.QObject): + '''Wrapper class around the raw 'QSettings' class that emits signals + when the value of the setting changes.''' + + '''Signal emitted when the setting has changed.''' + changed = QtCore.Signal(object) + + def __init__(self, key, default=None): + super(self.__class__, self).__init__() + + self._key = key + self._default = default + self._value = None + + def value(self): + s = QtCore.QSettings() + self._value = s.value(self._key, self._default) + return self._value + + @QtCore.Slot(object) + def setValue(self, value): + if value != self._value: + s = QtCore.QSettings() + s.setValue(self._key, value) + s.sync() + self._value = value + self.changed.emit(self._value) + +class _SettingsGroup(object): + '''Dummy class to group multiple 'Setting' objects together.''' + pass + +def init(): + '''Creates the 'Settings' objects for all known settings and places them + into the module's namespace. + + A QApplication must have been created before this function can be called. + ''' + + app = QtGui.QApplication.instance() + app.setApplicationName('sigrok-meter') + app.setOrganizationName('sigrok') + app.setOrganizationDomain('sigrok.org') + + mainwindow = _SettingsGroup() + mainwindow.size = Setting('mainwindow/size', QtCore.QSize(900, 550)) + mainwindow.pos = Setting('mainwindow/pos') + globals()['mainwindow'] = mainwindow diff --git a/sigrok-meter b/sigrok-meter index 7ded286..a56cf68 100755 --- a/sigrok-meter +++ b/sigrok-meter @@ -107,6 +107,9 @@ if __name__ == '__main__': app = QtGui.QApplication([]) + # Initialize modules that need a QApplication to exist. + import settings + settings.init() import icons icons.load_icons()