[Date Prev][Date Next] [Thread Prev][Thread Next]
[Thread Index]
[Date Index]
[Author Index]
Re: GtkTreeModelSort + GtkTreeModelFilter
- From: muppet <scott asofyet org>
- To: Patrick Fritzsch <lugpaj anet ne jp>
- Cc: gtk-perl-list gnome org
- Subject: Re: GtkTreeModelSort + GtkTreeModelFilter
- Date: Mon, 22 Aug 2005 22:15:54 -0400
On Aug 22, 2005, at 7:38 PM, Patrick Fritzsch wrote:
i understand that the GtkTreeModelSort and GtkTreeModelFilter
Interfaces are just a
GtkTreeModel with more functionality.
Well, technically, TreeSortable is an interface that you can
implement, but TreeModelSort and TreeModelFilter are actual objects
that act as proxy models. The TreeModelSort or TreeModelFilter is a
real object, but doesn't hold any data of its own.
Actually for the programm i want to code those functionalities
would be prefect. But not GtkTreeModelSort or GtkTreeModelFilter, i
would like to use both.
But well, i cannot use both, i can use one or the other i think.
Sure you can. See attached example, modelmodelmodel.pl... a sorted
and filtered list. That's probably a rather heavyweight way to do
it, but it is at least possible.
#!/usr/bin/perl -w
use strict;
use Glib qw(TRUE FALSE);
use Gtk2 -init;
my $index_threshold = 10;
# a simple ListStore...
my $model = Gtk2::ListStore->new ('Glib::Int', 'Glib::String');
foreach (1..20) {
my $str = join "", map { chr (rand(26)+ord("a")) } 1..6;
$model->set ($model->append, 0, $_, 1, $str);
}
# filtered...
my $filter = Gtk2::TreeModelFilter->new ($model);
$filter->set_visible_func (\&is_visible);
# and sorted.
my $sort = Gtk2::TreeModelSort->new_with_model ($filter);
$sort->set_sort_column_id (1, 'ascending');
# and now, a view with two click-to-sort columns.
my $treeview = Gtk2::TreeView->new ($sort);
foreach (["Index", 0], ["Junk", 1]) {
my $column = Gtk2::TreeViewColumn->new_with_attributes
($_->[0], Gtk2::CellRendererText->new, text => $_->[1]);
$column->set_sort_column_id ($_->[1]);
$treeview->append_column ($column);
}
$treeview->set_headers_clickable (TRUE);
# boilerplate to show the treeview...
my $window = Gtk2::Window->new;
my $hbox = Gtk2::HBox->new;
$window->add ($hbox);
$hbox->add ($treeview);
my $vbox = Gtk2::VBox->new;
$hbox->pack_start ($vbox, FALSE, FALSE, 0);
# a primitive way to modify the filter.
$vbox->pack_start (left_label ('Index Threshold'), FALSE, FALSE, 0);
our $index_spinner = Gtk2::SpinButton->new_with_range (0, 20, 1);
$index_spinner->set_digits (0);
$index_spinner->set_value ($index_threshold);
$index_spinner->signal_connect (changed => sub {
$index_threshold = $index_spinner->get_value;
$filter->refilter; # update for changed threshold!
});
$vbox->pack_start ($index_spinner, FALSE, FALSE, 0);
$window->signal_connect (destroy => sub {Gtk2->main_quit});
$window->show_all;
Gtk2->main;
sub is_visible {
my ($liststore, $iter) = @_;
my $index = $liststore->get ($iter, 0);
return $index < $index_threshold;
}
sub left_label {
my $label = Gtk2::Label->new (shift);
$label->set_alignment (0.0, 0.5);
return $label;
}
--
Jolt is my co-pilot.
-- Slogan on a giant paper airplane hung in Lobby 7 at MIT.
http://hacks.mit.edu/
[Date Prev][Date Next] [Thread Prev][Thread Next]
[Thread Index]
[Date Index]
[Author Index]