[pygobject: 1/2] GTK overrides: Do type conversion to column types of ListStore and TreeStore in set_value



commit 7d997b6fe88343776c4d67a9f3437ba0c4122da0
Author: Sebastian Pölsterl <sebp k-d-w org>
Date:   Tue Jan 18 13:12:36 2011 +0100

    GTK overrides: Do type conversion to column types of ListStore and TreeStore in set_value

 gi/overrides/Gtk.py     |   28 ++++++++++++++++++----------
 tests/test_overrides.py |   12 +++++++++++-
 2 files changed, 29 insertions(+), 11 deletions(-)
---
diff --git a/gi/overrides/Gtk.py b/gi/overrides/Gtk.py
index 8f1befa..86ca3c4 100644
--- a/gi/overrides/Gtk.py
+++ b/gi/overrides/Gtk.py
@@ -706,12 +706,14 @@ class TreeModel(Gtk.TreeModel):
 
         for i in range(n_columns):
             value = row[i]
+            self.set_value(treeiter, i, value)
 
+    def _convert_value(self, treeiter, column, value):
             if value is None:
-                continue
+                return
             
             # we may need to convert to a basic type
-            type_ = self.get_column_type(i)
+            type_ = self.get_column_type(column)
             if type_ == gobject.TYPE_PYOBJECT:
                 pass # short-circut branching
             elif type_ == gobject.TYPE_STRING:
@@ -721,14 +723,14 @@ class TreeModel(Gtk.TreeModel):
                     if isinstance(value, unicode):
                         value = value.encode('UTF-8')
                     else:
-                        raise ValueError('Expected string or unicode for row %i but got %s%s' % (i, value, type(value)))
+                        raise ValueError('Expected string or unicode for column %i but got %s%s' % (column, value, type(value)))
                 else:
-                    raise ValueError('Expected a string for row %i but got %s' % (i, type(value)))
+                    raise ValueError('Expected a string for column %i but got %s' % (column, type(value)))
             elif type_ == gobject.TYPE_FLOAT or type_ == gobject.TYPE_DOUBLE:
                 if isinstance(value, float):
                     value = float(value)
                 else:
-                    raise ValueError('Expected a float for row %i but got %s' % (i, type(value)))
+                    raise ValueError('Expected a float for column %i but got %s' % (column, type(value)))
             elif type_ == gobject.TYPE_LONG or type_ == gobject.TYPE_INT:
                 if isinstance(value, int):
                     value = int(value)
@@ -736,11 +738,11 @@ class TreeModel(Gtk.TreeModel):
                     if isinstance(value, long):
                         value = long(value)
                     else:
-                        raise ValueError('Expected an long for row %i but got %s' % i, type(value))
+                        raise ValueError('Expected an long for column %i but got %s' % (column, type(value)))
                 else:
-                    raise ValueError('Expected an interger for row %i but got %s' % i, type(value))
+                    raise ValueError('Expected an interger for column %i but got %s' % (column, type(value)))
 
-            self.set_value(treeiter, i, value)
+            return value
 
     def get(self, treeiter, *columns):
         n_columns = self.get_n_columns()
@@ -807,7 +809,6 @@ class ListStore(Gtk.ListStore, TreeModel, TreeSortable):
 
         return treeiter
 
-
     def insert_after(self, sibling, row=None):
         treeiter = Gtk.ListStore.insert_after(self, sibling)
 
@@ -816,6 +817,10 @@ class ListStore(Gtk.ListStore, TreeModel, TreeSortable):
 
         return treeiter
 
+    def set_value(self, treeiter, column, value):
+        value = self._convert_value(treeiter, column, value)
+        Gtk.ListStore.set_value(self, treeiter, column, value)
+
 
 ListStore = override(ListStore)
 __all__.append('ListStore')
@@ -978,7 +983,6 @@ class TreeStore(Gtk.TreeStore, TreeModel, TreeSortable):
 
         return treeiter
 
-
     def insert_after(self, parent, sibling, row=None):
         treeiter = Gtk.TreeStore.insert_after(self, parent, sibling)
 
@@ -987,6 +991,10 @@ class TreeStore(Gtk.TreeStore, TreeModel, TreeSortable):
 
         return treeiter
 
+    def set_value(self, treeiter, column, value):
+        value = self._convert_value(treeiter, column, value)
+        Gtk.TreeStore.set_value(self, treeiter, column, value)
+
 
 TreeStore = override(TreeStore)
 __all__.append('TreeStore')
diff --git a/tests/test_overrides.py b/tests/test_overrides.py
index ba91b7c..075ba77 100644
--- a/tests/test_overrides.py
+++ b/tests/test_overrides.py
@@ -491,7 +491,7 @@ class TestGtk(unittest.TestCase):
         test_pylist = [1,"2", "3"]
 
         list_store = Gtk.ListStore(int, str, 'GIOverrideTreeAPITest', object, object, object)
-        for i in range(94):
+        for i in range(93):
             label = 'this is row #%d' % i
             testobj = TestGtk.TestClass(self, i, label)
             parent = list_store.append((i,
@@ -501,6 +501,16 @@ class TestGtk(unittest.TestCase):
                                         test_pydict,
                                         test_pylist))
 
+        i = 93
+        label = u'this is row #93'
+        treeiter = list_store.append()
+        list_store.set_value(treeiter, 0, i)
+        list_store.set_value(treeiter, 1, label)
+        list_store.set_value(treeiter, 2, TestGtk.TestClass(self, i, label))
+        list_store.set_value(treeiter, 3, test_pyobj)
+        list_store.set_value(treeiter, 4, test_pydict)
+        list_store.set_value(treeiter, 5, test_pylist)
+
         # test automatic unicode->str conversion
         i = 94
         label = u'this is row #94'



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