| 1 | package Gtk2::Ex::TreeMaker; | ||
| 2 | |||
| 3 | our $VERSION = '0.11'; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use Glib qw(TRUE FALSE); | ||
| 8 | use Gtk2::Ex::TreeMaker::FlatInterface; | ||
| 9 | use Data::Dumper; | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | Gtk2::Ex::TreeMaker - A high level widget to represent a set of relational records in a hierarchical spreadsheet kinda display. This task is typical to most of the business application user interfaces. | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | Typically in business applications, users like to view data in a spreadsheet kind of display. (Columns represent timeline(typically) and rows represent measures like sales/inventory/blah/blah). | ||
| 18 | |||
| 19 | The data itself is typically stored internally as relational records. For example, here is some sales info (stored internally in a relational database) | ||
| 20 | |||
| 21 | ------------------------------------- | ||
| 22 | Region, City, Product, Date, Quantity | ||
| 23 | ------------------------------------- | ||
| 24 | Texas, Dallas, Fruits, Dec-2003, 300 | ||
| 25 | Texas, Dallas, Veggies, Jan-2004, 120 | ||
| 26 | Texas, Austin, Fruits, Nov-2003, 310 | ||
| 27 | Texas, Austin, Veggies, Feb-2004, 20 | ||
| 28 | ------------------------------------- | ||
| 29 | |||
| 30 | The user will typically want to view the same data in a hierarchical(/spreadsheet) kinda display. | ||
| 31 | |||
| 32 | ------------------------------------------------------ | ||
| 33 | Prod / Date Nov-2003 Dec-2003 Jan-2004 Feb-2004 | ||
| 34 | ------------------------------------------------------ | ||
| 35 | Texas | ||
| 36 | Dallas | ||
| 37 | Fruits 300 | ||
| 38 | Veggies 120 | ||
| 39 | Austin | ||
| 40 | Fruits 310 | ||
| 41 | Veggies 20 | ||
| 42 | ------------------------------------------------------ | ||
| 43 | |||
| 44 | With web-based business apps, similar views are created in the browser using lots of html/jsp coding. | ||
| 45 | |||
| 46 | The Gtk2::TreeView is an excellent widget to display a similar presentation of data in a desktop app. But creating a (hierarchical) TreeView from flat relational data can require some recursive function coding. It would be great if all this recursive code could be abstracted out and packaged separately. | ||
| 47 | |||
| 48 | This high level widget is designed with that purpose in mind. This module will accept a relational feed of records and automatically convert it into a hierarchical treeview using the Gtk2::TreeView. The process involves invoking some recursive functions to build a TreeModel and populate it. Also, since the spreadsheet itself can be rather long horizontally, the widget also has a I<FreezePane> capability. | ||
| 49 | |||
| 50 | Details on the widget including a screenshot can be found at: http://ofey.blogspot.com/2005/02/gtk2extreemaker.html | ||
| 51 | |||
| 52 | =head1 SYNOPSIS | ||
| 53 | |||
| 54 | use Gtk2 -init; | ||
| 55 | use Gtk2::Ex::TreeMaker; | ||
| 56 | |||
| 57 | # Create an array to contain the column_names. These names appear as the header for each column. | ||
| 58 | # The first entry should be the title of the left side of the FreezePane. | ||
| 59 | my $column_names = [ | ||
| 60 | 'Name', | ||
| 61 | 'Nov-2003', 'Dec-2003', 'Jan-2004', 'Feb-2004' | ||
| 62 | ]; | ||
| 63 | |||
| 64 | # This api will have to be cleaned soon... | ||
| 65 | # All the attributes of the cell in the treeview are specified here | ||
| 66 | # The value for these attributes are to be populated from the recordset | ||
| 67 | # The assumption is that the attributes are contained in the data record | ||
| 68 | # in the same order towards the **end** of the record. (the last few fields) | ||
| 69 | # Since we are using CellRendererText in the TreeView, any of the properties | ||
| 70 | # of the CellRendererText can be passed using this mechanism | ||
| 71 | # In addition to the properties of the CellRendererText, I have also added a | ||
| 72 | # custom property called 'hyperlinked'. | ||
| 73 | my $data_attributes = [ | ||
| 74 | {'text' => 'Glib::String'}, | ||
| 75 | {'editable' => 'Glib::Boolean'}, | ||
| 76 | {'hyperlinked' => 'Glib::Boolean'}, | ||
| 77 | {'background' => 'Glib::String'}, | ||
| 78 | ]; | ||
| 79 | |||
| 80 | # Here is the set of relational records to be displayed | ||
| 81 | my $recordset = [ | ||
| 82 | ['Texas','Dallas','Fruits','Dec-2003','300',0,1,'red'], | ||
| 83 | ['Texas','Dallas','Veggies','Jan-2004','120',1,0,'blue'], | ||
| 84 | ['Texas','Austin','Fruits','Nov-2003','310',1,1,'white'], | ||
| 85 | ['Texas','Austin','Veggies','Feb-2004','20',0,1,'green'] | ||
| 86 | ]; | ||
| 87 | |||
| 88 | # Initialize our new widget | ||
| 89 | # The constructor requires two attributes | ||
| 90 | # This constitutes of the $column_name and the $data_attributes as described above | ||
| 91 | my $treemaker = Gtk2::Ex::TreeMaker->new($column_names, $data_attributes); | ||
| 92 | |||
| 93 | # We will inject our relational recordset into the new widget | ||
| 94 | $treemaker->set_data_flat(\@recordset); | ||
| 95 | |||
| 96 | # Build the model | ||
| 97 | $treemaker->build_model; | ||
| 98 | |||
| 99 | # Create a root window to display the widget | ||
| 100 | my $window = Gtk2::Window->new; | ||
| 101 | $window->signal_connect(destroy => sub { Gtk2->main_quit; }); | ||
| 102 | |||
| 103 | # Add the widget to the root window | ||
| 104 | $window->add($treemaker->get_widget()); | ||
| 105 | |||
| 106 | $window->set_default_size(500, 300); | ||
| 107 | $window->show_all; | ||
| 108 | Gtk2->main; | ||
| 109 | |||
| 110 | =head1 METHODS | ||
| 111 | |||
| 112 | =head2 Gtk2::Ex::TreeMaker->new($column_names, $data_attributes) | ||
| 113 | |||
| 114 | This is the constructor. Accepts two arguments. | ||
| 115 | |||
| 116 | First argument is the column_names list. Each element of the array is a hash. The hash uses 'ColumnName' as the key. For example, | ||
| 117 | |||
| 118 | my $column_names = [ | ||
| 119 | 'Name', | ||
| 120 | 'Nov-2003', 'Dec-2003', 'Jan-2004', 'Feb-2004' | ||
| 121 | ]; | ||
| 122 | |||
| 123 | Second argument is the data_attributes list. Here you specify what attributes each record has. For example, | ||
| 124 | |||
| 125 | my $data_attributes = [ | ||
| 126 | {'text' => 'Glib::String'}, | ||
| 127 | {'editable' => 'Glib::Boolean'}, | ||
| 128 | {'hyperlinked' => 'Glib::Boolean'}, | ||
| 129 | {'background' => 'Glib::String'}, | ||
| 130 | ]; | ||
| 131 | |||
| 132 | All the attributes of the cell in the treeview are specified here. The value for these attributes are to be populated from the recordset. | ||
| 133 | The assumption is that the attributes are contained in the data record | ||
| 134 | in the same order towards the B<**end**> of the record. (the last few fields) | ||
| 135 | |||
| 136 | Since we are using C<Gtk2::CellRendererText> in the TreeView, any of the properties | ||
| 137 | of the C<Gtk2::CellRendererText> can be passed using this mechanism | ||
| 138 | In addition to the properties of the CellRendererText, I have also added a | ||
| 139 | custom property called 'hyperlinked'. | ||
| 140 | |||
| 141 | =cut | ||
| 142 | |||
| 143 | sub new { | ||
| 144 | my ($class, $column_names, $data_attributes) = @_; | ||
| 145 | my $self = {}; | ||
| 146 | $self->{data_tree} = undef; | ||
| 147 | $self->{data_attributes} = undef; | ||
| 148 | $self->{edited_data_flat} = []; | ||
| 149 | $self->{data_tree_depth} = undef; | ||
| 150 | $self->{column_names} = undef; | ||
| 151 | $self->{treeview_columns} = undef; | ||
| 152 | $self->{frozen_column} = undef; | ||
| 153 | $self->{tree_store_full} = undef; | ||
| 154 | $self->{tree_store_frozen} = undef; | ||
| 155 | $self->{tree_view_full} = undef; | ||
| 156 | $self->{tree_view_frozen} = undef; | ||
| 157 | $self->{chosen_column} = undef; | ||
| 158 | $self->{signals} = undef; | ||
| 159 | bless ($self, $class); | ||
| 160 | $self->set_meta_data($column_names, $data_attributes); | ||
| 161 | return $self; | ||
| 162 | } | ||
| 163 | |||
| 164 | =head2 Gtk2::Ex::TreeMaker->set_data_flat($data_flat) | ||
| 165 | |||
| 166 | This sub is used to inject the relational recordset into the widget. This sub accepts a set of relational records (an array of arrays) as the argument. For example, | ||
| 167 | |||
| 168 | my $recordset = [ | ||
| 169 | ['Texas','Dallas','Fruits','Dec-2003','300',0,1,'red'], | ||
| 170 | ['Texas','Dallas','Veggies','Jan-2004','120',1,0,'blue'], | ||
| 171 | ['Texas','Austin','Fruits','Nov-2003','310',1,1,'white'], | ||
| 172 | ['Texas','Austin','Veggies','Feb-2004','20',0,1,'green'] | ||
| 173 | ]; | ||
| 174 | |||
| 175 | =cut | ||
| 176 | |||
| 177 | sub set_data_flat { | ||
| 178 | my ($self, $data_flat) = @_; | ||
| 179 | my $flat_interface = Gtk2::Ex::TreeMaker::FlatInterface->new(); | ||
| 180 | my $data_tree = $flat_interface->flat_to_tree($self->{data_attributes}, $data_flat); | ||
| 181 | $self->{data_tree} = $data_tree; | ||
| 182 | } | ||
| 183 | |||
| 184 | # This method is temporarily not required. Will come back to it later | ||
| 185 | # Leave it in there for now | ||
| 186 | sub set_data_tree_depth { | ||
| 187 | my ($self, $data_tree_depth) = @_; | ||
| 188 | $self->{data_tree_depth} = $data_tree_depth; | ||
| 189 | } | ||
| 190 | |||
| 191 | =head2 Gtk2::Ex::TreeMaker->signal_connect($signal_name, $action) | ||
| 192 | |||
| 193 | Currently, four signals are suppoted | ||
| 194 | |||
| 195 | =over 4 | ||
| 196 | |||
| 197 | =item * cell-edited | ||
| 198 | |||
| 199 | Thrown only for 'editable' cells. | ||
| 200 | |||
| 201 | =item * cell-clicked | ||
| 202 | |||
| 203 | Thrown only for 'hyperlinked' cells. | ||
| 204 | |||
| 205 | =item * cell-enter | ||
| 206 | |||
| 207 | Thrown only for 'hyperlinked' cells. | ||
| 208 | |||
| 209 | =item * cell-leave | ||
| 210 | |||
| 211 | Thrown only for 'hyperlinked' cells. | ||
| 212 | |||
| 213 | =back | ||
| 214 | |||
| 215 | =cut | ||
| 216 | |||
| 217 | sub signal_connect { | ||
| 218 | my ($self, $signal, $action) = @_; | ||
| 219 | $self->{signals}->{$signal} = $action; | ||
| 220 | } | ||
| 221 | |||
| 222 | sub set_meta_data { | ||
| 223 | my ($self, $col_names, $data_attributes) = @_; | ||
| 224 | $self->{data_attributes} = $data_attributes; | ||
| 225 | my $column_names = []; | ||
| 226 | foreach my $col_name(@$col_names) { | ||
| 227 | push @$column_names, { ColumnName => $col_name}; | ||
| 228 | } | ||
| 229 | # Add an emtpy column in the end for display purposes | ||
| 230 | push @$column_names, { ColumnName => ''}; | ||
| 231 | $self->{column_names} = $column_names; | ||
| 232 | $self->{frozen_column} = [$column_names->[0]]; | ||
| 233 | my @temp; | ||
| 234 | my @column_attr; | ||
| 235 | my $count=0; | ||
| 236 | foreach my $attr (@$data_attributes) { | ||
| 237 | foreach my $key (keys %$attr) { | ||
| 238 | push @temp, $attr->{$key}; | ||
| 239 | |||
| 240 | # Here is the logic for dealing with the 'custom' properties | ||
| 241 | # All the properties that don't belong to the CellRendererText has to be handled here | ||
| 242 | # Else, the CellRendererText is gonna complain. | ||
| 243 | if ($key eq 'hyperlinked') { | ||
| 244 | $key = 'underline'; | ||
| 245 | } | ||
| 246 | |||
| 247 | push @column_attr, $key; | ||
| 248 | push @column_attr, $count++; | ||
| 249 | } | ||
| 250 | } | ||
| 251 | |||
| 252 | my @tree_store_full_types = map {@temp} @{$self->{column_names}}; | ||
| 253 | my @tree_store_frozen_types = map {@temp} @{$self->{frozen_column}}; | ||
| 254 | my $tree_store_full = Gtk2::TreeStore->new(@tree_store_full_types); | ||
| 255 | my $tree_store_frozen = Gtk2::TreeStore->new(@tree_store_frozen_types); | ||
| 256 | $self->{tree_store_full} = $tree_store_full; | ||
| 257 | $self->{tree_store_frozen} = $tree_store_frozen; | ||
| 258 | my $tree_view_full = Gtk2::TreeView->new($tree_store_full); | ||
| 259 | my $tree_view_frozen = Gtk2::TreeView->new($tree_store_frozen); | ||
| 260 | |||
| 261 | $tree_view_full->set_rules_hint(TRUE); | ||
| 262 | $tree_view_frozen->set_rules_hint(TRUE); | ||
| 263 | |||
| 264 | #$tree_view_full->get_selection->set_mode ('none'); | ||
| 265 | #$tree_view_frozen->get_selection->set_mode ('none'); | ||
| 266 | |||
| 267 | _synchronize_trees($tree_view_frozen, $tree_view_full); | ||
| 268 | $self->{tree_view_full} = $tree_view_full; | ||
| 269 | $self->{tree_view_frozen} = $tree_view_frozen; | ||
| 270 | |||
| 271 | $self->_create_columns ($self->{column_names}, $tree_store_full, $tree_view_full); | ||
| 272 | # There is only one column (the first column) in this case | ||
| 273 | my $column_name = $self->{frozen_column}->[0]->{ColumnName}; | ||
| 274 | my $column = Gtk2::TreeViewColumn->new_with_attributes( | ||
| 275 | $column_name, Gtk2::CellRendererText->new(), @column_attr); | ||
| 276 | $column->set_resizable(TRUE); | ||
| 277 | $tree_view_frozen->append_column($column); | ||
| 278 | |||
| 279 | my $treemaker_self = $self; | ||
| 280 | |||
| 281 | # If the cell is hyperlinked, then change the mouse pointer to something else | ||
| 282 | # This will give a visual feedback to the user that he should click on the cell | ||
| 283 | $tree_view_full->signal_connect('motion-notify-event' => | ||
| 284 | sub { | ||
| 285 | my ($self, $event) = @_; | ||
| 286 | my ($path, $column, $cell_x, $cell_y) = $self->get_path_at_pos ($event->x, $event->y); | ||
| 287 | my $cursor = undef; | ||
| 288 | if ($path) { | ||
| 289 | my $model = $self->get_model; | ||
| 290 | my $hyperlinked; | ||
| 291 | if (defined $column->{hyperlinked}){ | ||
| 292 | $hyperlinked = $model->get ($model->get_iter ($path), $column->{hyperlinked}); | ||
| 293 | } | ||
| 294 | if ($hyperlinked) { | ||
| 295 | $self->{cursor} = Gtk2::Gdk::Cursor->new ('hand2') | ||
| 296 | unless $self->{cursor}; | ||
| 297 | $cursor = $self->{cursor}; | ||
| 298 | |||
| 299 | # We also need to throw the cell-enter event from here | ||
| 300 | # Note: Only hyperlinked cells should throw the cell-enter and leave events | ||
| 301 | if ($treemaker_self->{signals}->{'cell-enter'}) { | ||
| 302 | &{$treemaker_self->{signals}->{'cell-enter'}} | ||
| 303 | ($treemaker_self, $path, $column->{column_number}) | ||
| 304 | unless $self->{'cell-hovered'}; | ||
| 305 | $self->{'cell-hovered'} = TRUE; | ||
| 306 | } | ||
| 307 | |||
| 308 | } else { | ||
| 309 | # Throw the cell-leave event here | ||
| 310 | if ($treemaker_self->{signals}->{'cell-leave'}) { | ||
| 311 | &{$treemaker_self->{signals}->{'cell-leave'}} | ||
| 312 | ($treemaker_self, $path, $column->{column_number}) | ||
| 313 | if $self->{'cell-hovered'}; | ||
| 314 | $self->{'cell-hovered'} = FALSE; | ||
| 315 | } | ||
| 316 | } | ||
| 317 | } | ||
| 318 | $event->window->set_cursor ($cursor); | ||
| 319 | return 0; | ||
| 320 | } | ||
| 321 | ); | ||
| 322 | |||
| 323 | |||
| 324 | $tree_view_full->signal_connect('button-press-event' => | ||
| 325 | sub { | ||
| 326 | my ($self, $event) = @_; | ||
| 327 | my ($path, $column, $cell_x, $cell_y) = $self->get_path_at_pos ($event->x, $event->y); | ||
| 328 | my $cursor = undef; | ||
| 329 | if ($path && defined $column->{hyperlinked}) { | ||
| 330 | my $model = $self->get_model; | ||
| 331 | my $hyperlinked = $model->get ($model->get_iter ($path), $column->{hyperlinked}); | ||
| 332 | if ($hyperlinked) { | ||
| 333 | if ($treemaker_self->{signals}->{'cell-clicked'}){ | ||
| 334 | &{$treemaker_self->{signals}->{'cell-clicked'}} | ||
| 335 | ($treemaker_self, $path, $column->{column_number}); | ||
| 336 | } | ||
| 337 | } | ||
| 338 | } | ||
| 339 | $event->window->set_cursor ($cursor); | ||
| 340 | return 0; | ||
| 341 | } | ||
| 342 | ); | ||
| 343 | |||
| 344 | } | ||
| 345 | |||
| 346 | sub set_selection_mode { | ||
| 347 | my ($self, $mode) = @_; | ||
| 348 | $self->{tree_view_full}->get_selection->set_mode($mode); | ||
| 349 | $self->{tree_view_frozen}->get_selection->set_mode($mode); | ||
| 350 | } | ||
| 351 | |||
| 352 | sub clear_model { | ||
| 353 | my ($self) = @_; | ||
| 354 | $self->{tree_store_full}->clear; | ||
| 355 | $self->{tree_store_frozen}->clear; | ||
| 356 | } | ||
| 357 | |||
| 358 | =head2 Gtk2::Ex::TreeMaker->build_model | ||
| 359 | |||
| 360 | This is the core recursive method that actually builds the tree. | ||
| 361 | |||
| 362 | =cut | ||
| 363 | |||
| 364 | sub build_model { | ||
| 365 | my ($self) = @_; | ||
| 366 | $self->clear_model; | ||
| 367 | foreach my $subtree (@{$self->{data_tree}->{'Node'}}) { | ||
| 368 | $self->_append_children($self->{tree_view_full}->get_model(), undef, | ||
| 369 | $subtree, $self->{column_names}); | ||
| 370 | $self->_append_children($self->{tree_view_frozen}->get_model(), undef, | ||
| 371 | $subtree, $self->{frozen_column}); | ||
| 372 | } | ||
| 373 | # Expand the tree to start with | ||
| 374 | $self->{tree_view_frozen}->expand_all; | ||
| 375 | $self->{tree_view_full}->expand_all; | ||
| 376 | } | ||
| 377 | |||
| 378 | =head2 Gtk2::Ex::TreeMaker->get_widget | ||
| 379 | |||
| 380 | Returns the widget that you can later attach to a root window or any other container. | ||
| 381 | |||
| 382 | =cut | ||
| 383 | |||
| 384 | sub get_widget { | ||
| 385 | my ($self) = @_; | ||
| 386 | # Add the frozen-tree to the left side of the pane | ||
| 387 | my $display_paned = Gtk2::HPaned->new; | ||
| 388 | $display_paned->add1 ($self->{tree_view_frozen}); | ||
| 389 | |||
| 390 | # we set the vertical size request very small, and it will fill up the | ||
| 391 | # available space when we set the default size of the window. | ||
| 392 | $self->{tree_view_frozen}->set_size_request (-1, 10); | ||
| 393 | |||
| 394 | # Add the full-tree to a scrolled window in the right pane | ||
| 395 | my $scroll = Gtk2::ScrolledWindow->new; | ||
| 396 | $scroll->add ($self->{tree_view_full}); | ||
| 397 | $display_paned->add2 ($scroll); | ||
| 398 | |||
| 399 | # Synchronize the scrolling | ||
| 400 | $self->{tree_view_frozen}->set(vadjustment => $self->{tree_view_full}->get_vadjustment); | ||
| 401 | |||
| 402 | return $display_paned; | ||
| 403 | } | ||
| 404 | |||
| 405 | # Private method to enable the freezepane. | ||
| 406 | sub _synchronize_trees { | ||
| 407 | my ($tree_view_frozen, $tree_view_full) = @_; | ||
| 408 | |||
| 409 | # First, we will synchronize the row-expansion/collapse | ||
| 410 | $tree_view_frozen->signal_connect('row-expanded' => | ||
| 411 | sub { | ||
| 412 | my ($view, $iter, $path) = @_; | ||
| 413 | $tree_view_full->expand_row($path,0); | ||
| 414 | } | ||
| 415 | ); | ||
| 416 | $tree_view_frozen->signal_connect('row-collapsed' => | ||
| 417 | sub { | ||
| 418 | my ($view, $iter, $path) = @_; | ||
| 419 | $tree_view_full->collapse_row($path); | ||
| 420 | } | ||
| 421 | ); | ||
| 422 | |||
| 423 | # Next, we will synchronize the row selection | ||
| 424 | $tree_view_frozen->get_selection->signal_connect('changed' => | ||
| 425 | sub { | ||
| 426 | my ($selection) = @_; | ||
| 427 | _synchronize_tree_selection($tree_view_frozen, $tree_view_full, $selection); | ||
| 428 | } | ||
| 429 | ); | ||
| 430 | $tree_view_full->get_selection->signal_connect('changed' => | ||
| 431 | sub { | ||
| 432 | my ($selection) = @_; | ||
| 433 | _synchronize_tree_selection($tree_view_full, $tree_view_frozen, $selection); | ||
| 434 | } | ||
| 435 | ); | ||
| 436 | } | ||
| 437 | |||
| 438 | |||
| 439 | # Synchronize the tree selections | ||
| 440 | sub _synchronize_tree_selection { | ||
| 441 | my ($thisview, $otherview, $this_selection) = @_; | ||
| 442 | return unless ($thisview and $otherview and $this_selection); | ||
| 443 | my ($selected_model, $selected_iter) = $this_selection->get_selected; | ||
| 444 | return unless ($selected_model and $selected_iter); | ||
| 445 | $otherview->get_selection->select_path ($selected_model->get_path($selected_iter)); | ||
| 446 | } | ||
| 447 | |||
| 448 | sub _append_children { | ||
| 449 | my ($self, $tree_store, $iter, $data_tree, $columns) = @_; | ||
| 450 | if ($data_tree ) { | ||
| 451 | my $count = 0; | ||
| 452 | if ($data_tree->{'Node'}) { | ||
| 453 | my $child_iter = $tree_store->append ($iter); | ||
| 454 | for my $column(@$columns) { | ||
| 455 | my $column_name = $column->{ColumnName}; | ||
| 456 | |||
| 457 | # Ignore the real name of the first column. Use the special value called 'Name' | ||
| 458 | # so that the tree traversal is correct. | ||
| 459 | $column_name = 'Name' if ($count == 0); | ||
| 460 | |||
| 461 | if ($data_tree->{$column_name}) { | ||
| 462 | $tree_store->set($child_iter, $count, $data_tree->{$column_name}); | ||
| 463 | } | ||
| 464 | $count+=$#{@{$self->{data_attributes}}}+1; | ||
| 465 | } | ||
| 466 | foreach my $child(@{$data_tree->{'Node'}}) { | ||
| 467 | $self->_append_children($tree_store, $child_iter, $child, $columns); | ||
| 468 | } | ||
| 469 | } else { | ||
| 470 | for my $column(@$columns) { | ||
| 471 | my $column_name = $column->{ColumnName}; | ||
| 472 | next unless ($column_name); | ||
| 473 | if ($data_tree->{'Name'} eq $column_name) { | ||
| 474 | foreach my $attr (@{$self->{data_attributes}}) { | ||
| 475 | foreach my $key (keys %$attr) { | ||
| 476 | $tree_store->set($iter, $count++, $data_tree->{$key}); | ||
| 477 | } | ||
| 478 | } | ||
| 479 | } | ||
| 480 | $count += $#{@{$self->{data_attributes}}}+1; | ||
| 481 | } | ||
| 482 | } | ||
| 483 | |||
| 484 | } | ||
| 485 | } | ||
| 486 | |||
| 487 | sub _create_columns { | ||
| 488 | my ($self, $all_columns, $tree_store, $tree_view )=@_; | ||
| 489 | my $column_count = 0; | ||
| 490 | my $column_number = 0; | ||
| 491 | for my $column (@$all_columns) { | ||
| 492 | my $column_name = $column->{ColumnName}; | ||
| 493 | my $cell = Gtk2::CellRendererText->new; | ||
| 494 | |||
| 495 | # Align all cells to the right | ||
| 496 | $cell->set (xalign => 1); | ||
| 497 | |||
| 498 | # Create a new variable. | ||
| 499 | # Else it will always be set to max value of column_count | ||
| 500 | # Reference issues | ||
| 501 | my $column_id = $column_count; | ||
| 502 | |||
| 503 | # Handle the edits. This is currently half baked. | ||
| 504 | $cell->signal_connect (edited => | ||
| 505 | sub { | ||
| 506 | my ($cell, $pathstring, $newtext) = @_; | ||
| 507 | my $path = Gtk2::TreePath->new_from_string ($pathstring); | ||
| 508 | my $iter = $tree_store->get_iter ($path); | ||
| 509 | $tree_store->set ($iter, $column_id, $newtext); | ||
| 510 | |||
| 511 | # Call the call-back hook specified | ||
| 512 | # Hey watch out for a division by zero !!! :) Come back later and fix it... | ||
| 513 | if ($self->{signals}->{'cell-edited'}){ | ||
| 514 | &{$self->{signals}->{'cell-edited'}} | ||
| 515 | ($self, $path, $column_id/($#{@{$self->{data_attributes}}}+1), $newtext); | ||
| 516 | } | ||
| 517 | } | ||
| 518 | ); | ||
| 519 | |||
| 520 | my @column_attr; | ||
| 521 | my $count=0; | ||
| 522 | my $attr_pos_hyperlinked; | ||
| 523 | foreach my $attr (@{$self->{data_attributes}}) { | ||
| 524 | foreach my $key (keys %$attr) { | ||
| 525 | # The custom attributes that we created should not be passed | ||
| 526 | # on to the CellRendererText. Either remove them or replace them with | ||
| 527 | # something that makes sense to the CellRendererText | ||
| 528 | if ($key eq 'hyperlinked') { | ||
| 529 | $key = 'underline'; | ||
| 530 | $attr_pos_hyperlinked = $column_count + $count; | ||
| 531 | } | ||
| 532 | push @column_attr, $key; | ||
| 533 | push @column_attr, $column_count + $count++; | ||
| 534 | } | ||
| 535 | } | ||
| 536 | |||
| 537 | my $column = Gtk2::TreeViewColumn->new_with_attributes( | ||
| 538 | $column_name, $cell, text => $column_count, @column_attr); | ||
| 539 | $column->set_title($column_name); | ||
| 540 | |||
| 541 | # Keep a ref to the $column for later use. | ||
| 542 | push @{$self->{treeview_columns}}, $column; | ||
| 543 | |||
| 544 | # Keep this for later; the TreeViewColumn doesn't allow us to | ||
| 545 | # query the attribute list, so we have to keep this information | ||
| 546 | # for ourselves. | ||
| 547 | if (defined $attr_pos_hyperlinked) { | ||
| 548 | $column->{hyperlinked} = $attr_pos_hyperlinked; | ||
| 549 | } | ||
| 550 | $column->{column_number} = $column_number++; | ||
| 551 | |||
| 552 | $column->set_resizable(TRUE); | ||
| 553 | $tree_view->append_column($column); | ||
| 554 | |||
| 555 | # Hide the first column | ||
| 556 | # Ensure that the expander is fixed to the first column | ||
| 557 | # (and hence is hidden too) | ||
| 558 | if ($column_count == 0) { | ||
| 559 | $column->set_visible(FALSE); | ||
| 560 | $tree_view->set_expander_column($column); | ||
| 561 | } | ||
| 562 | $column_count+=$#{@{$self->{data_attributes}}}+1; | ||
| 563 | } | ||
| 564 | } | ||
| 565 | |||
| 566 | =head2 Gtk2::Ex::TreeMaker->locate_record(Gtk2::TreePath, Integer, Text) | ||
| 567 | |||
| 568 | This sub maps a TreeCell location into a flat record in the original recordset that was used to create this tree. The location of the TreeCell itself is denoted using two arguments, the Gtk2::TreePath that points to the row and the Column_ID that points to the column. | ||
| 569 | |||
| 570 | Using this information, the function then traverses the internal data structure and returns a record (an array object). | ||
| 571 | |||
| 572 | =cut | ||
| 573 | |||
| 574 | sub locate_record { | ||
| 575 | my ($self, $edit_path, $column_id) = @_; | ||
| 576 | my $record; | ||
| 577 | |||
| 578 | # Drill down the $tree_path and keep adding entries into the record | ||
| 579 | my $temp = $self->{data_tree}; | ||
| 580 | my @tree_path = split /:/, $edit_path->to_string; | ||
| 581 | for (my $i=0; $i<=$#tree_path; $i++) { | ||
| 582 | my $index = $tree_path[$i]; | ||
| 583 | $temp = _get_subtree($temp, $index); | ||
| 584 | push @$record, $temp->{'Name'}; | ||
| 585 | } | ||
| 586 | my $column_name = $self->{column_names}->[$column_id]->{'ColumnName'}; | ||
| 587 | # Now the hierarchical tree elements have been added into the record | ||
| 588 | # Next we just need to add the correct column_name | ||
| 589 | push @$record, $column_name; | ||
| 590 | foreach my $node (@{$temp->{'Node'}}) { | ||
| 591 | if ($node->{'Name'} eq $column_name) { | ||
| 592 | push @$record, $node; | ||
| 593 | } | ||
| 594 | } | ||
| 595 | return $record; | ||
| 596 | } | ||
| 597 | |||
| 598 | sub _get_subtree { | ||
| 599 | my ($tree, $index) = @_; | ||
| 600 | my $count = 0; | ||
| 601 | foreach my $rec (@{$tree->{'Node'}}) { | ||
| 602 | if (exists $rec->{'Node'}) { | ||
| 603 | if ($count == $index) { | ||
| 604 | return $rec; | ||
| 605 | } | ||
| 606 | $count++ ; | ||
| 607 | } | ||
| 608 | } | ||
| 609 | } | ||
| 610 | |||
| 611 | 1; | ||
| 612 | |||
| 613 | __END__ |