Saturday, October 27, 2007

I'm always forgetting how to use the TreeView widget to display columnar data. In PyGTK (well, GTK+ in general) a TreeView is used to display trees (TreeStores) and columnar data (ListStores) like in the screenshot below. In this mini HOWTO I'm restricting the post to ListStores, however, many of the concepts below apply to TreeStores.)

A ListStore keeps track of the values you want to display in your TreeView. You create one like this: liststore = gtk.ListStore(type1, type2, type3, ...)

Where typen can be a Python type int, str, a PyGTK type gtk.Button..., or a gobject type like gobject.TYPE_CHAR or 'gchar'.

Once your ListStore or "model" is created, you'll want to put things in it and take things out.
To add a row you use append or prepend like so iter=liststore.append([1,2,3,'a','b','c']) iter=liststore.prepend([1,2,3,'a','b','c']) The iterator iter can be used by other calls like new_iter=liststore.insert_before(iter, [1,2,3,'a','b','c']) new_iter=liststore.insert_after(iter, row=[...]).

So what about accessing/changing things after the fact?
value=liststore.get_value(iter, column) will get a single column from a single row at iter.
values=liststore.get(iter,col1,col2,col3,...) gets multiple rows from iter just as vala, valb=liststore.get(iter, 0, 2) does.

Conversly, liststore.set(iter,col,val) sets a single column in iter to value. As does liststore.set(iter,col,val,col,val,...,...) for multiple columns and values. To remove a single row do liststore.remove(iter), and liststore.clear() to remove them all.

The model (ListStore) is essentially a database. To actually see the contents of it in a widget you'll need a TreeView, TreeViewColumns, and CellRenderers. view=gtk.TreeView(model=None) does the job of creating a TreeView, optionally setting the model off of which it's based. Otherwise it can be set and retrieved with view.set_model(model) model=view.get_model().

TreeViewColumns hold the CellRenderers and are managed by the TreeView. You add a TreeViewColumn to a TreeView like this: view.append_column(column). You make one thus col=gtk.TreeViewColumn('Heading').

CellRenderers do the work of displaying a particular column in the store. CellRenderers get packed into the TreeViewColumns (which manage the column headers), therefore you can have multiple CellRenderers under one column heading. It is a common practice to put an image and text together under the same column heading. Different Cellrenderers exist for different types namely pixmaps, text, and toggle buttons. (And, of course, you can always make your own.) It is easy to create a Cellrenderer: cell=gtk.CellRendererText() cell=gtk.CellRendererPixbuf() cell=gtk.CellRendererToggle(). You can then pack it into the the desired TreeViewColumn with column.pack_start(cell,expand=True) and column.pack_end(cell,expand=False).

Here is the part I have the hardest time remembering... How do you associate data in the store with what gets displayed in the CellRenderers? The answer is column attributes. So, to associate the text of column 0 in the model with the text in a particular CellRenderer cell you do col.add_attribute(cell, 'text', 0) or equivalently col.set_attributes(cell, text=0, prop1=modelcol1, prop2=modelcol2, ...). There are many other attributes besides 'text' that can be tied to your data model. Some include 'markup' (simple HTML), 'font', 'background' (color), 'foreground' (color).

I've written it to be as simple as possible, (hence no classes, functions, etc.), so that the associations between the ListStore, TreeView, TreeViewColumns, and CellRenderers are obvious. The result is ugly (both the code and the screenshot!), but effective at achieving the desired result. Note that the hex and dec CellRenderers are both under the 'Strings' heading and how you can associate the model with more than just the text, in this case the background color of the cell.
For more details including sorting, additional attributes, and signals see chapter 14 of the PyGTK tutorial http://www.pygtk.org/pygtk2tutorial/sec-TreeModelInterface.html upon which this post is heavily based.

Wednesday, July 25, 2007

gcc (well cpp, the preprocessor) provides several predefined macros. Many of these are platform independent, for instance __FILE__ and __LINE__. Others are platform dependent (or "system-specific" as the gcc manual puts it). So how do you know what your particular platform defines? Try this: cpp -dM /dev/null
I stumbled across this trick the other day on Apple's mailing lists archives.

Friday, July 20, 2007

Ok, first things first... If you're a control freak like I am and want to actually use "real" HTML (tables, div, etc.) in your posts to blogger, the first thing you should do is disable the automatic conversion of line breaks to the <br /> tag.

Of course, then you'll have to explicitly add <br /> tags to format your posts, but at least then you won't waste an hour trying to figure out why your tables look atrocious. (Not that I did that or anything ;)

To do this, log in, hit the "Settings" tab, then the "Formatting" tab, then set "Convert Line Breaks" to "No".

In other words "Settings"->"Formatting"->"Convert Line Breaks" = "No".