[pitivi] settings: add support for receiving signals from properties



commit 159728e113eab47390b70ced515fdc41c92d3f2c
Author: Brandon Lewis <brandon_lewis berkeley edu>
Date:   Mon Apr 13 12:09:48 2009 -0700

    settings: add support for receiving signals from properties
---
 pitivi/settings.py      |   31 ++++++++++++++++++++++++++++---
 pitivi/ui/mainwindow.py |    1 -
 2 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/pitivi/settings.py b/pitivi/settings.py
index 9e53469..c16917d 100644
--- a/pitivi/settings.py
+++ b/pitivi/settings.py
@@ -107,7 +107,22 @@ def xdg_config_dirs():
 class ConfigError(Exception):
     pass
 
-class GlobalSettings(object):
+class Notification(object):
+
+    """A descriptor to help with the implementation of signals"""
+
+    def __init__(self, attrname):
+        self.attrname = "_" + attrname
+        self.signame = attrname + "Changed"
+
+    def __get__(self, instance, unused):
+        return getattr(instance, self.attrname)
+
+    def __set__(self, instance, value):
+        setattr(instance, self.attrname, value)
+        instance.emit(self.signame)
+
+class GlobalSettings(object, Signallable):
     """
     Global PiTiVi settings.
 
@@ -260,7 +275,7 @@ class GlobalSettings(object):
 
     @classmethod
     def addConfigOption(cls, attrname, type_=None, section=None, key=None,
-        environment=None, default=None):
+        environment=None, default=None, notify=False, prefs_group=None):
         """
         Add a configuration option.
 
@@ -282,6 +297,11 @@ class GlobalSettings(object):
         @param key: the key under which this option is to be saved. Can be none if
         this option should not be saved.
         @type key: C{str}
+        @param notify: whether or not this attribute should emit notification
+        signals when modified (default is False).
+        @type notify: C{boolean}
+        @param prefs_group: use this if you would like a widget to change this
+        option to be automatically created in the user preferences panel
         """
         if section and not section in cls.options:
             raise ConfigError("You must add the section \"%s\" first." %
@@ -301,7 +321,12 @@ class GlobalSettings(object):
                 " type or a default." % attrname)
         if not type_:
             type_ = type(default)
-        setattr(cls, attrname, default)
+        if notify:
+            setattr(cls, attrname, Notification(attrname))
+            setattr(cls, "_" + attrname, default)
+            cls.__signals__[attrname + 'Changed'] = []
+        else:
+            setattr(cls, attrname, default)
         if section and key:
             cls.options[section][attrname] = type_, key, environment
         cls.environment.add(environment)
diff --git a/pitivi/ui/mainwindow.py b/pitivi/ui/mainwindow.py
index b520515..10f10b3 100644
--- a/pitivi/ui/mainwindow.py
+++ b/pitivi/ui/mainwindow.py
@@ -320,7 +320,6 @@ class PitiviMainWindow(gtk.Window, Loggable):
         self.connect("configure-event", self._configureCb)
         self.connect("key-press-event", self._keyPressEventCb)
 
-
         # main menu & toolbar
         vbox = gtk.VBox(False)
         self.add(vbox)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]