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