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; });
$window->set_border_width(5);
$window->set_position('center_always');

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

#add and show the vbox
$window->add($vbox);
$window->show();

#our main event-loop
Gtk2->main();


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);
                
$vbox->pack_start($tree_view,TRUE,TRUE,0);

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

$vbox->show_all();
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); 
}

__END__

-- 
I'm not really a human, but I play one on earth.
http://zentara.net/Remember_How_Lucky_You_Are.html 



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