home *** CD-ROM | disk | FTP | other *** search
/ tusportal.tus.k12.pa.us / tusportal.tus.k12.pa.us.tar / tusportal.tus.k12.pa.us / Wyse / latest-image.raw / 0.img / usr / lib / pygtk / 2.0 / demos / treemodel.py < prev    next >
Text File  |  2010-05-11  |  4KB  |  121 lines

  1. #!/usr/bin/env python
  2. '''Tree View/Generic Tree Model
  3.  
  4. This test is designed to demonstrate creating a new type of tree model
  5. in python for use with the new tree widget in gtk 2.0.'''
  6.  
  7. import gtk
  8. import gobject
  9.  
  10. # to create a new GtkTreeModel from python, you must derive from
  11. # TreeModel.
  12. class MyTreeModel(gtk.GenericTreeModel):
  13.     '''This class represents the model of a tree.  The iterators used
  14.     to represent positions are converted to python objects when passed
  15.     to the on_* methods.  This means you can use any python object to
  16.     represent a node in the tree.  The None object represents a NULL
  17.     iterator.
  18.  
  19.     In this tree, we use simple tuples to represent nodes, which also
  20.     happen to be the tree paths for those nodes.  This model is a tree
  21.     of depth 3 with 5 nodes at each level of the tree.  The values in
  22.     the tree are just the string representations of the nodes.'''
  23.  
  24.     TREE_DEPTH = 4
  25.     TREE_SIBLINGS = 5
  26.     def __init__(self):
  27.         '''constructor for the model.  Make sure you call
  28.         PyTreeModel.__init__'''
  29.         gtk.GenericTreeModel.__init__(self)
  30.  
  31.     # the implementations for TreeModel methods are prefixed with on_
  32.     def on_get_flags(self):
  33.         '''returns the GtkTreeModelFlags for this particular type of model'''
  34.         return 0
  35.     def on_get_n_columns(self):
  36.         '''returns the number of columns in the model'''
  37.         return 1
  38.     def on_get_column_type(self, index):
  39.         '''returns the type of a column in the model'''
  40.         return gobject.TYPE_STRING
  41.     def on_get_path(self, node):
  42.         '''returns the tree path(a tuple of indices at the various
  43.         levels) for a particular node.'''
  44.         return node
  45.     def on_get_iter(self, path):
  46.         '''returns the node corresponding to the given path.  In our
  47.         case, the node is the path'''
  48.         return path
  49.     def on_get_value(self, node, column):
  50.         '''returns the value stored in a particular column for the node'''
  51.         assert column == 0
  52.         return `node`
  53.     def on_iter_next(self, node):
  54.         '''returns the next node at this level of the tree'''
  55.         if node != None:
  56.             if node[-1] == self.TREE_SIBLINGS - 1: # last node at level
  57.                 return None
  58.             return node[:-1] +(node[-1]+1,)
  59.     def on_iter_children(self, node):
  60.         '''returns the first child of this node'''
  61.         if node == None: # top of tree
  62.             return(0,)
  63.         if len(node) >= self.TREE_DEPTH: # no more levels
  64.             return None
  65.         return node +(0,)
  66.     def on_iter_has_child(self, node):
  67.         '''returns true if this node has children'''
  68.         return node == None or len(node) < self.TREE_DEPTH
  69.     def on_iter_n_children(self, node):
  70.         '''returns the number of children of this node'''
  71.         if node == None or len(node) < self.TREE_DEPTH:
  72.             return self.TREE_SIBLINGS
  73.         else:
  74.             return 0
  75.     def on_iter_nth_child(self, node, n):
  76.         '''returns the nth child of this node'''
  77.         if node == None:
  78.             return(n,)
  79.         if len(node) < self.TREE_DEPTH and n < self.TREE_SIBLINGS:
  80.             return node +(n,)
  81.         else:
  82.             return None
  83.     def on_iter_parent(self, node):
  84.         '''returns the parent of this node'''
  85.         assert node != None
  86.         if len(node) == 0:
  87.             return None
  88.         else:
  89.             return node[:-1]
  90.  
  91. class GenericTreeModelDemo(gtk.Window):
  92.     def __init__(self, parent=None):
  93.         gtk.Window.__init__(self)
  94.         try:
  95.             self.set_screen(parent.get_screen())
  96.         except AttributeError:
  97.             self.connect('destroy', lambda *w: gtk.main_quit())
  98.         self.set_title(self.__class__.__name__)
  99.  
  100.         scrolled_window = gtk.ScrolledWindow()
  101.         scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
  102.         self.add(scrolled_window)
  103.  
  104.         model = MyTreeModel()
  105.         model = model.filter_new()
  106.         tree_view = gtk.TreeView(model)
  107.         cell = gtk.CellRendererText()
  108.         # the text in the column comes from column 0
  109.         column = gtk.TreeViewColumn("tuples", cell, text=0)
  110.         tree_view.append_column(column)
  111.  
  112.         scrolled_window.add(tree_view)
  113.         self.show_all()
  114.  
  115. def main():
  116.     GenericTreeModelDemo()
  117.     gtk.main()
  118.  
  119. if __name__ == '__main__':
  120.     main()
  121.