Re: dumping an entire treeview into a HoH

On Thu, 25 Dec 2008 08:15:05 -0500
zentara <zentara1 sbcglobal net> wrote:

Thanks to Emmanuele for pointing out the foreach method of the Treemodel.
In case someone is looking for it, here is the code. I didn't convert
to HoH, but just printed them out. You can get hash keys from the PathString.

#! /usr/bin/perl -w

use strict;
use Gtk2 '-init';
use Glib qw/TRUE FALSE/;

#Create a constant to differentiate columns in the model
#(With more than once column this will become more clear)
use constant MODEL_FIRST_COLUMN       => 0;

#Create a constant to differentiate different renderers;
#This makes things easy to discover which renderer in the view
#was changed
use constant RENDERER_FIRST_TEXT       => 0;

#standard window creation, placement, and signal connecting
my $window = Gtk2::Window->new('toplevel');
$window->signal_connect('delete_event' => sub { Gtk2->main_quit; });

#this vbox will geturn the bulk of the gui
my $vbox = &ret_vbox();

#add and show the vbox

#our main event-loop

sub ret_vbox {

my $vbox = Gtk2::VBox->new(FALSE,5);
$vbox->set_size_request (300, 300);

        #this is one of the provided base Gtk2::TreeModel classes.
        my $tree_store = Gtk2::TreeStore->new(qw/Glib::String/);

        #fill it with arbitry data
        foreach (1..3) {
                my $parent_nr = $_;
                my $iter = $tree_store->append(undef);
                $tree_store->set ($iter,MODEL_FIRST_COLUMN, "Parent $parent_nr");
                 foreach (1..3){
                        my $iter_child = $tree_store->append($iter);
                        $tree_store->set ($iter_child,MODEL_FIRST_COLUMN, "Child $_ of Parent $parent_nr");
        #this will create a treeview, specify $tree_store as its model
        my $tree_view = Gtk2::TreeView->new($tree_store);

                #create a Gtk2::TreeViewColumn to add to $tree_view
                my $tree_column = Gtk2::TreeViewColumn->new();
                $tree_column->set_title ("Click to sort");
                        my $renderer = Gtk2::CellRendererText->new;
                        $renderer->set_property('editable' => TRUE);
            #Attach a 'renderer_number' value to the renderer.
            #This can be used to differentiate between renderers
            #when we have a few renderers which can be edited
            $renderer->{'renderer_number'} = RENDERER_FIRST_TEXT;
                        $renderer->signal_connect (edited => \&cell_edited, $tree_store);
                $tree_column->pack_start ($renderer, FALSE);
                $tree_column->add_attribute($renderer, text => MODEL_FIRST_COLUMN);
        #add $tree_column to the treeview
        $tree_view->append_column ($tree_column);

my $button = Gtk2::Button->new ('Get all values');
$button->signal_connect (clicked => \&clicked, $tree_view );

return $vbox;

sub clicked {
   my (undef,$treeview) = @_;
  # I want to dump the tree into a HoH structure, or arrays
  my $treemodel = $treeview->get_model;
  my $rc = $treemodel->foreach(\&walk);
  if ($rc){ print "$rc\n"};
  return 0;

sub walk{
         my($treemodel,$path,$iter)= @_;
         my $value = $treemodel->get($iter,0);
         my $list = $path->get_indices;
         my $pstr = $path->to_string;
         print "$value\tIndices: $list  PathString: $pstr \n";
   return 0;

sub cell_edited {
  my ($cell, $path_string, $new_text, $model) = @_;
  my $path = Gtk2::TreePath->new_from_string ($path_string);

    #Get the renderer number;
        my $column_of_model;
    if ($cell->{'renderer_number'} == RENDERER_FIRST_TEXT) {

        $column_of_model = MODEL_FIRST_COLUMN;

        my $iter = $model->get_iter ($path);
  $model->set_value ($iter, $column_of_model, $new_text); 


I'm not really a human, but I play one on earth. 

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