[pygobject] add ListStore, TreeStore and TreeViewColumn APIs
- From: John Palmieri <johnp src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [pygobject] add ListStore, TreeStore and TreeViewColumn APIs
- Date: Wed, 23 Jun 2010 21:20:12 +0000 (UTC)
commit 8d9516a593a515290109401a9db7aa259b5aa35c
Author: John (J5) Palmieri <johnp redhat com>
Date: Wed Jun 23 17:04:33 2010 -0400
add ListStore, TreeStore and TreeViewColumn APIs
* this is enough to support the gtk-demo.py shell
* TreeStore and ListStore allow passing in as an argument list
of either python or GLib types to the constructor as a description
of the columns in the model
* TreeStore and ListStore override the append method, allowing
the application developer to send in a list of column values
for one row in the model. Unlike the append in C which
just returns an iter that you can then add data to,
this append actualy appends data in one step
* TreeViewColumn overrides the constructor to allow the adding
of attributes and a cell renderer when constructing the
column
https://bugzilla.gnome.org/show_bug.cgi?id=620405
gi/overrides/Gtk.py | 59 ++++++++++++++++++++++++++++++++++++++++
tests/test_overrides.py | 69 +++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 128 insertions(+), 0 deletions(-)
---
diff --git a/gi/overrides/Gtk.py b/gi/overrides/Gtk.py
index 886756e..0c3dcc5 100644
--- a/gi/overrides/Gtk.py
+++ b/gi/overrides/Gtk.py
@@ -340,6 +340,65 @@ class TextBuffer(Gtk.TextBuffer):
TextBuffer = override(TextBuffer)
__all__.append('TextBuffer')
+class ListStore(Gtk.ListStore):
+ def __init__(self, *column_types):
+ Gtk.ListStore.__init__(self)
+ self.set_column_types(column_types)
+
+ def append(self, row):
+ treeiter = Gtk.TreeIter()
+ Gtk.ListStore.append(self, treeiter)
+
+ n_columns = self.get_n_columns();
+ if len(row) != n_columns:
+ raise ValueError('row sequence has the incorrect number of elements')
+
+ for i in range(n_columns):
+ if row[i] is not None:
+ self.set_value(treeiter, i, row[i])
+
+ return treeiter
+
+ListStore = override(ListStore)
+__all__.append('ListStore')
+
+class TreeStore(Gtk.TreeStore):
+
+ def __init__(self, *column_types):
+ Gtk.TreeStore.__init__(self)
+ self.set_column_types(column_types)
+
+ def append(self, parent, row):
+ treeiter = Gtk.TreeIter()
+ Gtk.TreeStore.append(self, treeiter, parent)
+
+ n_columns = self.get_n_columns();
+ if len(row) != n_columns:
+ raise ValueError('row sequence has the incorrect number of elements')
+
+ for i in xrange(n_columns):
+ if row[i] is not None:
+ self.set_value(treeiter, i, row[i])
+
+ return treeiter
+
+TreeStore = override(TreeStore)
+__all__.append('TreeStore')
+
+class TreeViewColumn(Gtk.TreeViewColumn):
+ def __init__(self, title='',
+ cell_renderer=None,
+ **attributes):
+ Gtk.TreeViewColumn.__init__(self, title=title)
+ if cell_renderer:
+ self.pack_start(cell_renderer, True)
+
+ for (name, value) in attributes.iteritems():
+ self.add_attribute(cell_renderer, name, value)
+
+TreeViewColumn = override(TreeViewColumn)
+__all__.append('TreeViewColumn')
+
import sys
initialized, argv = Gtk.init_check(sys.argv)
diff --git a/tests/test_overrides.py b/tests/test_overrides.py
index 90f28ca..0509cfa 100644
--- a/tests/test_overrides.py
+++ b/tests/test_overrides.py
@@ -156,6 +156,75 @@ class TestGtk(unittest.TestCase):
button = dialog.get_widget_for_response (Gtk.ResponseType.CLOSE)
self.assertEquals(Gtk.STOCK_CLOSE, button.get_label())
+ def test_tree_api(self):
+ self.assertEquals(Gtk.TreeStore, overrides.Gtk.TreeStore)
+ self.assertEquals(Gtk.ListStore, overrides.Gtk.ListStore)
+ self.assertEquals(Gtk.TreeViewColumn, overrides.Gtk.TreeViewColumn)
+
+ class TestClass(GObject.GObject):
+ __gtype_name__ = "GIOverrideTreeAPITest"
+
+ def __init__(self, tester, int_value, string_value):
+ super(TestClass, self).__init__()
+ self.tester = tester
+ self.int_value = int_value
+ self.string_value = string_value
+
+ def check(self, int_value, string_value):
+ self.tester.assertEquals(int_value, self.int_value)
+ self.tester.assertEquals(string_value, self.string_value)
+
+ # check TreeStore
+ # FIXME: we should be able to pass strings like 'TestClass'
+ tree_store = Gtk.TreeStore(int, str, TestClass)
+ parent = None
+ for i in xrange(100):
+ label = 'this is child #%d' % i
+ testobj = TestClass(self, i, label)
+ parent = tree_store.append(parent, (i, label, testobj))
+
+ # walk the tree to see if the values were stored correctly
+ iter = Gtk.TreeIter()
+ parent = None
+ i = 0
+ while tree_store.iter_children(iter, parent):
+ i = tree_store.get_value(iter, 0)
+ s = tree_store.get_value(iter, 1)
+ obj = tree_store.get_value(iter, 2)
+ obj.check(i, s)
+ parent = iter
+ iter = Gtk.TreeIter()
+
+ self.assertEquals(i, 99)
+
+ # check ListStore
+ # FIXME: we should be able to pass strings like 'TestClass'
+ list_store = Gtk.ListStore(int, str, TestClass)
+ for i in xrange(100):
+ label = 'this is row #%d' % i
+ testobj = TestClass(self, i, label)
+ parent = list_store.append((i, label, testobj))
+
+ # walk the list to see if the values were stored correctly
+ iter = Gtk.TreeIter()
+ i = 0
+ has_more = list_store.get_iter_first(iter)
+ while has_more:
+ i = list_store.get_value(iter, 0)
+ s = list_store.get_value(iter, 1)
+ obj = list_store.get_value(iter, 2)
+ obj.check(i, s)
+ has_more = list_store.iter_next(iter)
+
+ self.assertEquals(i, 99)
+
+ # check to see that we can instantiate a TreeViewColumn
+ cell = Gtk.CellRendererText()
+ column = Gtk.TreeViewColumn(title='This is just a test',
+ cell_renderer=cell,
+ text=0,
+ style=2)
+
def test_text_buffer(self):
self.assertEquals(Gtk.TextBuffer, overrides.Gtk.TextBuffer)
buffer = Gtk.TextBuffer()
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]