| 1 | # Copyright 2008, 2009, 2010, 2011 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Chart. | ||
| 4 | # | ||
| 5 | # Chart is free software; you can redistribute it and/or modify it under the | ||
| 6 | # terms of the GNU General Public License as published by the Free Software | ||
| 7 | # Foundation; either version 3, or (at your option) any later version. | ||
| 8 | # | ||
| 9 | # Chart is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| 10 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 12 | # details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU General Public License along | ||
| 15 | # with Chart. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | |||
| 17 | |||
| 18 | package App::Chart::Gtk2::Symlist::Glob; | ||
| 19 | use 5.008; | ||
| 20 | use strict; | ||
| 21 | use warnings; | ||
| 22 | use Gtk2; | ||
| 23 | use Carp; | ||
| 24 | |||
| 25 | use App::Chart::Glib::Ex::MoreUtils; | ||
| 26 | use App::Chart; | ||
| 27 | |||
| 28 | use constant DEBUG => 0; | ||
| 29 | |||
| 30 | use Gtk2::Ex::TreeModelFilter::Draggable; | ||
| 31 | use Glib::Object::Subclass | ||
| 32 | 'Gtk2::Ex::TreeModelFilter::Draggable', | ||
| 33 | properties => [Glib::ParamSpec->string | ||
| 34 | ('pattern', | ||
| 35 | 'pattern', | ||
| 36 | '', | ||
| 37 | 'Glob style pattern of symbols.', | ||
| 38 | Glib::G_PARAM_READWRITE), | ||
| 39 | |||
| 40 | Glib::ParamSpec->string | ||
| 41 | ('key', | ||
| 42 | 'key', | ||
| 43 | 'The symlist database key.', | ||
| 44 | '', | ||
| 45 | Glib::G_PARAM_READWRITE), | ||
| 46 | |||
| 47 | Glib::ParamSpec->string | ||
| 48 | ('name', | ||
| 49 | 'name', | ||
| 50 | 'The symlist name.', | ||
| 51 | '', # default | ||
| 52 | Glib::G_PARAM_READWRITE) ]; | ||
| 53 | |||
| 54 | use App::Chart::Gtk2::Symlist; | ||
| 55 | use base 'App::Chart::Gtk2::Symlist'; | ||
| 56 | |||
| 57 | sub can_edit { return 0; } | ||
| 58 | sub can_destroy { return 0; } | ||
| 59 | |||
| 60 | sub new { | ||
| 61 | my ($class, $symlist, $pattern) = @_; | ||
| 62 | return $class->Glib::Object::new (child_model => $symlist, | ||
| 63 | pattern => $pattern); | ||
| 64 | } | ||
| 65 | |||
| 66 | my $counter = 0; | ||
| 67 | |||
| 68 | sub INIT_INSTANCE { | ||
| 69 | my ($self) = @_; | ||
| 70 | $self->{'key'} = '_glob_' . $counter++; | ||
| 71 | $self->{'regexp'} = qr//; | ||
| 72 | $self->set_visible_func (\&_visible_func, | ||
| 73 | App::Chart::Glib::Ex::MoreUtils::ref_weak($self)); | ||
| 74 | } | ||
| 75 | |||
| 76 | sub SET_PROPERTY { | ||
| 77 | my ($self, $pspec, $newval) = @_; | ||
| 78 | my $pname = $pspec->get_name; | ||
| 79 | $self->{$pname} = $newval; | ||
| 80 | |||
| 81 | if ($pname eq 'pattern') { | ||
| 82 | require Text::Glob; | ||
| 83 | $self->{'name'} = $newval; | ||
| 84 | $self->{'regexp'} = Text::Glob::glob_to_regex ($newval); | ||
| 85 | if (DEBUG) { print "regexp ",$self->{'regexp'},"\n"; } | ||
| 86 | $self->refilter; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | sub _visible_func { | ||
| 91 | my ($child_model, $child_iter, $ref_weak_self) = @_; | ||
| 92 | if (DEBUG) { print "test ",$child_model->get_value($child_iter,0),"\n"; } | ||
| 93 | my $self = $$ref_weak_self || return; | ||
| 94 | return ($child_model->get_value($child_iter,0) =~ $self->{'regexp'}); | ||
| 95 | } | ||
| 96 | |||
| 97 | 1; | ||
| 98 | __END__ | ||
| 99 | |||
| 100 | =for stopwords symlist globbed | ||
| 101 | |||
| 102 | =head1 NAME | ||
| 103 | |||
| 104 | App::Chart::Gtk2::Symlist::Glob -- pattern match symlist | ||
| 105 | |||
| 106 | =for test_synopsis my ($parentlist) | ||
| 107 | |||
| 108 | =head1 SYNOPSIS | ||
| 109 | |||
| 110 | use App::Chart::Gtk2::Symlist::Glob; | ||
| 111 | my $symlist = App::Chart::Gtk2::Symlist::Glob->new ($parentlist, '*.NZ'); | ||
| 112 | |||
| 113 | =head1 OBJECT HIERARCHY | ||
| 114 | |||
| 115 | C<App::Chart::Gtk2::Symlist::Glob> is a subclass of | ||
| 116 | C<Gtk2::Ex::TreeModelFilter::Draggable>, | ||
| 117 | |||
| 118 | Glib::Object | ||
| 119 | Gtk2::TreeModelFilter | ||
| 120 | Gtk2::Ex::TreeModelFilter::Draggable | ||
| 121 | App::Chart::Gtk2::Symlist::Glob | ||
| 122 | |||
| 123 | =head1 DESCRIPTION | ||
| 124 | |||
| 125 | A C<App::Chart::Gtk2::Symlist::Glob> object filters a given child symlist according | ||
| 126 | to a glob style pattern like "C<*.NZ>". The globbed list updates with the | ||
| 127 | child symlist, but is otherwise read-only and exists only in the current | ||
| 128 | process (it doesn't go into the database). | ||
| 129 | |||
| 130 | =head1 FUNCTIONS | ||
| 131 | |||
| 132 | =over 4 | ||
| 133 | |||
| 134 | =item C<< App::Chart::Gtk2::Symlist::Glob->new ($child_symlist, $pattern) >> | ||
| 135 | |||
| 136 | Create and return a C<App::Chart::Gtk2::Symlist::Glob> which is C<$parent_symlist> | ||
| 137 | filtered by C<$pattern>. | ||
| 138 | |||
| 139 | =back | ||
| 140 | |||
| 141 | =head1 PROPERTIES | ||
| 142 | |||
| 143 | =over 4 | ||
| 144 | |||
| 145 | =item C<pattern> (string) | ||
| 146 | |||
| 147 | A glob style pattern like "C<*.NZ>". | ||
| 148 | |||
| 149 | =back | ||
| 150 | |||
| 151 | =head1 SEE ALSO | ||
| 152 | |||
| 153 | L<App::Chart::Gtk2::Symlist> | ||
| 154 | |||
| 155 | =cut |