Re: TreeViews, IconViews, and images



On Mon, May 29, 2006 at 04:19:00PM -0400, muppet wrote:
On May 29, 2006, at 11:57 AM, Wouter Verhelst wrote:

I've been looking over the C API docs and the Perl examples, but I  
can't
seem to figure out how one is supposed to set up a TreeStore (or a
ListStore) so that it would be possible to show an image in a TreeView
or an IconView. Could someone point me towards a simple example? I did
figure out that it must involve a CellRendererPixbuf somehow, but I
haven't been able to figure out how the pieces fit together. The  
rest of
the treeview works somewhat (I got that far, though not by  
understaning
everything), but this is really puzzling me

Depends on whether you need generic images or can use stock items  
(even custom stock items).  The custom stock items are very easy, the  
generic images are also easy, but you do it a different way.  For  
full flexibility, you insert the column with a cell data func and do  
whatever you want --- the stock browser in Gtk2/examples does that.

But, to cut to the chase with a simple example:


#!/usr/bin/perl -w

use strict;
use Glib ':constants';
use Gtk2 -init;

my $window = Gtk2::Window->new;

#
# Using stock pixbufs is a lot easier than this, because you just store
# the stock id in the model and set up the TreeViewColumn with an  
attribute
# that sets stock_id on the renderer from that column.
#
# Here, we'll assume you have actual pixbufs in the model, which is the
# generic case.  We will, however, render stock icons for the pixbufs,
# because i'm too lazy to do all the work to set up something that scans
# a directory and thumbnails all the images in it.
#

# model just contains references to actual images.
my $model = Gtk2::ListStore->new ('Gtk2::Gdk::Pixbuf');

Okay, so *that* was what I was missing. I thought the TreeModels could
only hold simple data -- strings, integers, those kinds of things. And
then it was rather unclear to me how I could tell the CellRendererPixbuf
how to turn a string into a filename containing the actual image data.
Obviously, If it can hold "complex" data as well, then that explains.
And, as it happens, it's obviously also the only sane way to implement
this.

Sometimes it's right there, but you gotta see it :-)

Thanks.

[...]
Also, while I'm at it, I would like to set up a TreeView so that
optionally, the user can choose to not display the 'leaf' items in a
TreeStore; those would be shown in the IconView that is going to be
right next to the TreeView, with the icons in the iconview being
changed when the selection on the TreeView changes. Is that possible
without building two TreeStore objects? If so, how?

I would be surprised if you could actually get IconView to use a  
TreeStore.  I think it wants list data (depth=1).

Darn.

At any rate, the first solution to come to mind would be to store  
only the directories in the TreeStore attached to the TreeView, and  
each time the selected node of the tree is changed, populate and  
attach a new ListStore to the IconView to display the contents of  
that tree node.  This also involves storing less data in memory since  
you don't have to populate the whole tree.

I don't expect there to be a whole lot of data in this particular case
(not even considering the pixbufs), so memory is not an issue.

However, rebuilding a liststore on every click is going to be a bit
messy due to the way my data is currently organized. Guess I'll have to
revisit that then.

-- 
Wouter Verhelst
NixSys BVBA
Louizastraat 14, 2800 Mechelen
T: +32 15 27 69 50 / F: +32 15 27 69 51 / M: +32 486 836 198



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]