]> sigrok.org Git - sigrok-meter.git/commitdiff
Save and restore settings.
authorJens Steinhauser <redacted>
Tue, 13 Oct 2015 14:49:57 +0000 (16:49 +0200)
committerJens Steinhauser <redacted>
Tue, 13 Oct 2015 19:22:47 +0000 (21:22 +0200)
mainwindow.py
settings.py [new file with mode: 0644]
sigrok-meter

index 058fe5fb54e7edb589999d11ef058e499ad3874b..680dd97b5eb5cded0d7cb7ba4b2ad304298975d7 100644 (file)
@@ -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 (file)
index 0000000..79484e6
--- /dev/null
@@ -0,0 +1,73 @@
+##
+## This file is part of the sigrok-meter project.
+##
+## Copyright (C) 2015 Jens Steinhauser <jens.steinhauser@gmail.com>
+##
+## 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
index 7ded2865cda5d5defaf3d1be2e4f137555ad3176..a56cf68f59caa8743854884afa343ae102ecf190 100755 (executable)
@@ -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()