tree for svenne
[talkcutter.git] / src / tree.py
1 #!/usr/bin/env python
2 # vim: ts=4:sw=4:tw=78:nowrap
3 import pygtk
4 pygtk.require("2.0")
5 import gtk, gobject
6
7 tasks =  {
8     "Buy groceries": "Go to Asda after work",
9     "Do some programming": "Remember to update your software",
10     "Power up systems": "Turn on the client but leave the server",
11     "Watch some tv": "Remember to catch ER"
12     } 
13
14 class tree:
15     def __init__(self):
16         Store = InfoModel()     
17         Display = DisplayModel()
18
19         self.mdl = Store.get_model()
20         self.view = Display.make_view( self.mdl )
21         self.view.show_all()
22
23     def get_tree(self):
24         return self.view
25       
26
27 class GUI_Controller:
28     """ The GUI class is the controller for our application """
29     def __init__(self):
30         # setup the main window
31         self.root = gtk.Window(type=gtk.WINDOW_TOPLEVEL)
32         self.root.set_title("CellRenderer Example")
33         self.root.connect("destroy", self.destroy_cb)
34
35         myTree = tree()
36         self.root.add(myTree.get_tree())
37
38         self.root.show_all()
39         
40         return
41     def destroy_cb(self, *kw):
42         """ Destroy callback to shutdown the app """
43         gtk.main_quit()
44         return
45     def run(self):
46         """ run is called to set off the GTK mainloop """
47         gtk.main()
48         return  
49
50 class InfoModel:
51     """ The model class holds the information we want to display """
52     def __init__(self):
53         """ Sets up and populates our gtk.TreeStore """
54         self.tree_store = gtk.TreeStore( gobject.TYPE_STRING,
55                                          gobject.TYPE_BOOLEAN )
56         # places the global people data into the list
57         # we form a simple tree.
58         for item in tasks.keys():
59             parent = self.tree_store.append( None, (item, None) )
60             self.tree_store.append( parent, (tasks[item],None) )
61         return
62     def get_model(self):
63         """ Returns the model """
64         if self.tree_store:
65             return self.tree_store 
66         else:
67             return None
68
69 class DisplayModel:
70     """ Displays the Info_Model model in a view """
71     def make_view( self, model ):
72         """ Form a view for the Tree Model """
73         self.view = gtk.TreeView( model )
74         # setup the text cell renderer and allows these
75         # cells to be edited.
76         self.renderer = gtk.CellRendererText()
77         self.renderer.set_property( 'editable', True )
78         self.renderer.connect( 'edited', self.col0_edited_cb, model )
79
80         # The toggle cellrenderer is setup and we allow it to be
81         # changed (toggled) by the user.
82         self.renderer1 = gtk.CellRendererToggle()
83         self.renderer1.set_property('activatable', True)
84         self.renderer1.connect( 'toggled', self.col1_toggled_cb, model )
85                 
86         # Connect column0 of the display with column 0 in our list model
87         # The renderer will then display whatever is in column 0 of
88         # our model .
89         self.column0 = gtk.TreeViewColumn("Name", self.renderer, text=0)
90                 
91         # The columns active state is attached to the second column
92         # in the model.  So when the model says True then the button
93         # will show as active e.g on.
94         self.column1 = gtk.TreeViewColumn("Complete", self.renderer1 )
95         self.column1.add_attribute( self.renderer1, "active", 1)
96         self.view.append_column( self.column0 )
97         self.view.append_column( self.column1 )
98         return self.view
99     def col0_edited_cb( self, cell, path, new_text, model ):
100         """
101         Called when a text cell is edited.  It puts the new text
102         in the model so that it is displayed properly.
103         """
104         print "Change '%s' to '%s'" % (model[path][0], new_text)
105         model[path][0] = new_text
106         return
107     def col1_toggled_cb( self, cell, path, model ):
108         """
109         Sets the toggled state on the toggle button to true or false.
110         """
111         model[path][1] = not model[path][1]
112         print "Toggle '%s' to: %s" % (model[path][0], model[path][1],)
113         return
114
115 if __name__ == '__main__':
116     myGUI = GUI_Controller()
117     myGUI.run()