Re: dumping an entire treeview into a HoH



Hello.

The only method that comes to my mind is to use "foreach" method on
your tree model. But I'm no expert when it comes to low-level
gtk2-perl hacks, so there might be a faster solution that I'm not
aware of.


2008/12/25 zentara <zentara1 sbcglobal net>:
Hi,

I'm hoping someone can save me some hacking by showing me
a generic code snippet for walking an entire tree and getting
all it's values.

This is code from the Novell Study Guide, and I added a button,
with a callback called "clicked", in which I want to automatically
walk the tree and collect all values without selecting them all.

Is this possible? Or is it better to create a data structure first,
then populate the tree from the data structure?

#! /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 {
 print "@_\n";
 # I want to dump the tree into a HoH structure, or arrays

}


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__

Thanks,
zentara

--
I'm not really a human, but I play one on earth.
http://zentara.net/Remember_How_Lucky_You_Are.html
_______________________________________________
gtk-perl-list mailing list
gtk-perl-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-perl-list




-- 
Tadej BorovÅak
00386 (0)40 613 131
tadeboro gmail com
tadej borovsak gmail com


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