Re: hideing elements of the gtktreemodel



On Tue, 26 Aug 2003 14:37:17 +0200, Domsodi Gergely wrote:

What are the possibilities of showing just a part of the model, not the
whole one?

I use the following to achieve something similiar: hide rows of a list
store if they don't match a given pattern. I'm sure you can modify it to
work on trees.

sub filter {
  my ($filter_name, $filter_home, $filter_mobile) = @_;

  # first, re-add all hidden rows so that they're processed again.
  # automatic sorting is assumed.
  foreach (@{$model -> { _hidden }}) {
    $model -> set(
      $model -> append(),
      COLUMN_NAME, $_ -> [0],
      COLUMN_HOME, $_ -> [1] || "",
      COLUMN_MOBILE, $_ -> [2] || "");
  }

  $model -> { _hidden } = [];
  my @victims = ();

  # at least one filter has to be defined.
  if ($filter_name ne "" || $filter_home ne "" || $filter_mobile ne "") {
    # iterate over every row.
    $model -> foreach(sub {
      my ($model, $path, $iterator) = @_;
      my ($value_name,
          $value_home,
          $value_mobile) = ($model -> get($iterator, COLUMN_NAME),
                            $model -> get($iterator, COLUMN_HOME),
                            $model -> get($iterator, COLUMN_MOBILE));

      # see if the values match the patterns; a simple substring match  
      # in this case.
      unless (index($value_name, $filter_name) > -1 &&
              index($value_home, $filter_home) > -1 &&             
              index($value_mobile, $filter_mobile) > -1) {
        # if the row doesn't match all criteria, add its values to the
        # list of hidden rows.
        push(@{$model -> { _hidden }}, [$value_name, $value_home, $value_mobile]);

        # and store a row reference to it in the victims array for later
        # removal. you can't just use the path or an iterator here     
        # because the store may change further. row references listen to 
        # all relevant signals and adjust themselves accordingly so that  
        # they're still valid when we need them.
        # you also can't just remove the row at this point as this would
        # totally confuse $model -> foreach() and make all your iterators
        # invalid.
        push(@victims, Gtk2::TreeRowReference -> new($model, $path));
      }

      # make sure to return zero here to make $model -> foreach() go on.
      return 0;
    });

    # all that's left now is to remove the matching rows.
    foreach (@victims) {
      $model -> remove($model -> get_iter($_ -> get_path()));
    }
  }

  return 1;
}

I thought about submitting this as a FAQ or a recipe but am unsure
whether it's relevant.

HTH,
-Torsten



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