/[ascend]/trunk/pygtk/libviewpy.py
ViewVC logotype

Contents of /trunk/pygtk/libviewpy.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 532 - (show annotations) (download) (as text)
Mon Apr 24 02:23:08 2006 UTC (18 years, 1 month ago) by johnpye
File MIME type: text/x-python
File size: 4297 byte(s)
Removed 'interface' directory in trunk/pygtk/interface (moved everything up a level)
Made corresponding changes to SCons* and spec files.
1 #!/usr/bin/env python
2
3 import pygtk
4 pygtk.require('2.0')
5 import gtk
6
7 class Browser:
8 def make_row( self, piter, name, value ):
9 info = repr(value)
10 if not hasattr(value, "__dict__"):
11 if len(info) > 80:
12 # it's a big list, or dict etc.
13 info = info[:80] + "..."
14 _piter = self.treestore.append( piter, [ name, type(value).__name__, info ] )
15 return _piter
16
17 def make_instance( self, value, piter ):
18 if hasattr( value, "__dict__" ):
19 for _name, _value in value.__dict__.items():
20 _piter = self.make_row( piter, "."+_name, _value )
21 _path = self.treestore.get_path( _piter )
22 self.otank[ _path ] = (_name, _value)
23
24 def make_children(self, value, piter ):
25 children = []
26 if hasattr(value,"children"):
27 children=value.children;
28 for child in children:
29 _name = child.name;
30 _piter = self.make_row(piter,_name,child)
31 _path = self.treestore.get_path(_piter)
32 self.otank[_path]=(_name,child)
33
34 def make_mapping( self, value, piter ):
35 keys = []
36 if hasattr( value, "keys" ):
37 keys = value.keys()
38 elif hasattr( value, "__len__"):
39 keys = range( len(value) )
40 for key in keys:
41 _name = "[%s]"%str(key)
42 _piter = self.make_row( piter, _name, value[key] )
43 _path = self.treestore.get_path( _piter )
44 self.otank[ _path ] = (_name, value[key])
45
46 def make(self, name=None, value=None, path=None, depth=1):
47 if path is None:
48 # make root node
49 piter = self.make_row( None, name, value )
50 path = self.treestore.get_path( piter )
51 self.otank[ path ] = (name, value)
52 else:
53 name, value = self.otank[ path ]
54
55 piter = self.treestore.get_iter( path )
56 if not self.treestore.iter_has_child( piter ):
57 #self.make_mapping( value, piter )
58 #self.make_instance( value, piter )
59 self.make_children(value,piter)
60
61 if depth:
62 for i in range( self.treestore.iter_n_children( piter ) ):
63 self.make( path = path+(i,), depth = depth - 1 )
64
65 def row_expanded( self, treeview, piter, path ):
66 self.make( path = path )
67
68 def delete_event(self, widget, event, data=None):
69 gtk.main_quit()
70 return gtk.FALSE
71
72 def __init__(self, name, value):
73 self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
74 self.window.set_title("Browser")
75 self.window.set_size_request(512, 320)
76 self.window.connect("delete_event", self.delete_event)
77
78 # we will store the name, the type name, and the repr
79 columns = [str,str,str]
80 self.treestore = gtk.TreeStore(*columns)
81
82 # the otank tells us what object we put at each node in the tree
83 self.otank = {} # map path -> (name,value)
84 self.make( name, value )
85
86 self.treeview = gtk.TreeView(self.treestore)
87 self.treeview.connect("row-expanded", self.row_expanded )
88
89 self.tvcolumns = [ gtk.TreeViewColumn() for _type in columns ]
90 i = 0
91 for tvcolumn in self.tvcolumns:
92 self.treeview.append_column(tvcolumn)
93 cell = gtk.CellRendererText()
94 tvcolumn.pack_start(cell, True)
95 tvcolumn.add_attribute(cell, 'text', i)
96 i = i + 1
97
98 self.window.add(self.treeview)
99 self.window.show_all()
100
101 def dump( name, value ):
102 browser = Browser( name, value )
103 gtk.main()
104
105 def test():
106 class Thing:
107 def __init__(self,name,value):
108 self.children = []
109 self.name=name
110 self.value=value
111 self.index=0
112 def findChildren(self):
113 print "FINDING CHILDREN OF ", self.name
114 def addChild(self,thing):
115 print thing.name,"is a child of ",self.name
116 self.children.append(thing);
117 def __repr__(self):
118 return self.name
119 def __iter__(self):
120 for i in range(len(self.children)):
121 yield self.children[i]
122
123 a = Thing("a","A value")
124 b = Thing("b","B Another value")
125 c = Thing("c","C Another value")
126 d = Thing("d","D Yet another value")
127 a.addChild(b)
128 b.addChild(c)
129 c.addChild(d)
130 d.addChild(a) # circular chain
131 dump( "a", a )
132
133 if __name__ == "__main__":
134 test()

Properties

Name Value
svn:executable *

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22