Re: call for FAQ topics and content



On 14 Aug 2003 23:25:17 -0400, Ross McFarland wrote:

think back to what it took you forever to figure out, to things you
couldn't find information on no matter how hard you looked, or things
you figured out that you want the rest of the world to know.

I want my tree to be reorderable /and/ sortable. For long, I didn't find
any solution to this. Until yesterday, when I stumbled across
gtk_tree_store_swap. I was instantly reminded of the most beautiful and
most slowest way to sort: BUBBLE SORT! :)

Here you go:

sub node_sort {
  my ($view) = @_;

  # get the selected row; single selection mode is assumed
  my ($model, $iterator) = $view -> get_selection() -> get_selected();

  if (defined($model) && defined($iterator)) {
    # get the number of children the selected row has
    my $children = $model -> iter_n_children($iterator);

    foreach my $i (1 .. $children) {
      foreach my $j (1 .. ($children - $i)) {
        # get the iterators for the two neighboring rows
        my ($a, $b) = ($model -> iter_nth_child($iterator, $j - 1),
                       $model -> iter_nth_child($iterator, $j));

        # compare the values of the task column of the two rows and swap
        # them if the lower one sorts before the upper one
        if (($model -> get_value($a, COLUMN_TASK) cmp
             $model -> get_value($b, COLUMN_TASK)) == 1) {         
          $model -> swap($a, $b);
        }
      }
    }
  }
}

I call node_sort($view) from a rightclick popup menu entry labelled
"_Sort".

Bye,
-Torsten



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