changeset 44:be990ac6e478

saving the preference "start maximized" from GUI implemented
author Dirk Olmes <dirk@xanthippe.ping.de>
date Fri, 14 May 2010 15:02:15 +0200
parents 12ed8b5fa08c
children 0604e374c1d6
files Preference.py Preferences.py PreferencesDialog.py
diffstat 3 files changed, 27 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/Preference.py	Fri May 14 08:31:46 2010 +0200
+++ b/Preference.py	Fri May 14 15:02:15 2010 +0200
@@ -1,12 +1,8 @@
 
 class Preference(object):
     @staticmethod
-    def valueForKey(key, session):
-        pref = session.query(Preference).filter(Preference.key == key).first()
-        if pref is not None:
-            return pref.value
-        else:
-            return None
+    def forKey(key, session):
+        return session.query(Preference).filter(Preference.key == key).first()
     
     def __init__(self, key, value):
         self.key = key
--- a/Preferences.py	Fri May 14 08:31:46 2010 +0200
+++ b/Preferences.py	Fri May 14 15:02:15 2010 +0200
@@ -7,10 +7,27 @@
 class Preferences(object):
     def __init__(self, session):
         self.session = session
+        self.cache = {}
+        
+    def _cachedPreference(self, key, defaultValue=None):
+        if self.cache.has_key(key):
+            return self.cache[key]
+        else:
+            pref = Preference.forKey(key, self.session)
+            if pref is None:
+                pref = Preference(key, defaultValue)
+                self.session.add(pref)
+            self.cache[key] = pref
+            return pref
         
     def startMaximized(self):
-        value = Preference.valueForKey(START_MAXIMIZED, self.session)
-        if value is not None:
-            return util.str2bool(value)
-        else:
-            return False
+        pref = self._cachedPreference(START_MAXIMIZED, False)
+        return util.str2bool(pref.value)
+
+    def setStartMaximized(self, flag):
+        """ Convert flag to boolean so that a defined value is stored in the DB """
+        boolValue = False
+        if flag:
+            boolValue = True
+        pref = self._cachedPreference(START_MAXIMIZED)
+        pref.value = boolValue
--- a/PreferencesDialog.py	Fri May 14 08:31:46 2010 +0200
+++ b/PreferencesDialog.py	Fri May 14 15:02:15 2010 +0200
@@ -17,14 +17,14 @@
         self.ui.startMaximized.setChecked(maximized)
 
     def startMaximizedChanged(self, change):
-        print("start maximized: " + str(change))
+        self.preferences.setStartMaximized(change)
 
     def accept(self):
-        # TODO save the session
+        self.session.commit()
         QtGui.QDialog.accept(self)
         
     def reject(self):
-        # TODO revert the session
+        self.session.rollback()
         QtGui.QDialog.reject(self)
         
         
\ No newline at end of file