TreeIters again



I'm back to struggling with TreeIters again :(

This time, I'm trying to do something with a set of siblings. I have
some code that walks the siblings nicely:

    for (my $iter = $tree_store->iter_children($parent);
            $iter;
            $iter = $tree_store->iter_next($iter)
        )
    {
        # do something
    }

But $parent isn't where I start. I actually start from one of the
siblings, which I want to treat specially. So my code actually looks
like this:

    my $parent = $tree_store->iter_parent($selected_row_iter);

    for (my $iter = $tree_store->iter_children($parent);
            $iter;
            $iter = $tree_store->iter_next($iter)
        )
    {
        if ($iter == $selected_row_iter) {
            # do special case
        } else {
            # do normal case
        }
    }

The problem is that $selected_row_iter is never equal to $iter. $iter is
a new TreeIter for every sibling.

<rant>TreeIters seem like a very badly thought out feature!</rant>

I know I could make a TreePath from each iter and compare the paths, but
that seems slow since it involves iterated extra object creation etc.

I know I could use iter_nth_child() to iterate using an index, but I
haven't found a way to discover the index of the selected child.
Something like:

    my $selected_index =
            $tree_store->???child_index???($parent, $selected_iter);

    my $n = $tree_store->iter_n_children($parent);

    for (my $i = 0; $i < $n; $i++)
    {
        my $iter = $tree_store->iter_nth_child($parent, $i);
        if ($i == $special_index)
        ...
    }

And I know I should be able to iterate using TreePaths directly, but
when I try that, not only do I have to convert every path to an iter
(slow), but I haven't found the proper idiom. When I do this:

    my $selected_path = $tree_store->get_path($selected_row_iter);
    my $parent        = $selected_path->copy->up;

    for (my $path = $parent->copy->down;
            $path;
            $path->next
        )
    {
        my $name = lc $tree_store->get($tree_store-->get_iter($path), NAME);
        if ($path->compare($selected_path) == 0) {
            ...
        } else {
            ...
        }
    }

I get an error 'Can't call method "copy" without a package or object
reference' at the $path = $parent->copy. But $parent claims it is a
TreePath, which isa Glib::Boxed - parent $VAR1 = bless( do{\(my $o =
345348880)}, 'Gtk2::TreePath' );

So I'd welcome any suggestions as to how to iterate through the siblings
of a node in a TreeStore?

Thanks, Dave



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