Gtk2-Ex-SearchBox
view release on metacpan or search on metacpan
lib/Gtk2/Ex/SearchBox.pm view on Meta::CPAN
use constant NAME_____COLUMN => 3;
sub new {
my ($class, $type, $operatorlist) = @_;
my $self = {};
my $default_operatorlist = [
'contains',
'doesn\'t contain',
'not equal to',
'equals',
];
$self->{operatorlist} = $operatorlist || $default_operatorlist;
$self->{type} = $type || 'multiple';
bless ($self, $class);
$self->{widget} = $self->_create_widget();
return $self;
}
sub signal_connect {
my ($self, $signal, $callback) = @_;
$self->{signals}->{$signal} = $callback;
}
sub get_model {
my ($self) = @_;
my @temp;
@temp = @{$self->{datamodel}} if $self->{datamodel};
push @temp, {
'operator' => $self->{operatorlist}->[$self->{operatorcombo}->get_active],
'field' => $self->{entry}->get_text,
} if $self->{entry}->get_text;
return \@temp;
}
sub set_model {
my ($self, $datamodel) = @_;
$self->{datamodel} = $datamodel;
$self->_populate;
$self->_check_list_visibility();
}
sub to_sql_condition {
my ($self, $fieldname, $datamodel) = @_;
my @condition;
foreach my $x (@$datamodel) {
next unless $x;
if ($x->{operator} eq 'equals') {
push @condition, $fieldname.' = '.'\''.$x->{field}.'\'';
} elsif ($x->{operator} eq 'not equal to') {
push @condition, $fieldname.' <> '.'\''.$x->{field}.'\'';
} elsif ($x->{operator} eq 'contains') {
push @condition, $fieldname.' like '.'\'%'.$x->{field}.'%\'';
} elsif ($x->{operator} eq 'doesn\'t contain') {
push @condition, $fieldname.' not like '.'\'%'.$x->{field}.'%\'';
}
}
my $str = join ' or ', @condition;
return $str;
}
sub attach_popup_to {
my ($self, $parent) = @_;
my $popupwindow = Gtk2::Ex::PopupWindow->new($parent);
$popupwindow->set_move_with_parent(TRUE);
my $frame = Gtk2::Frame->new;
$frame->add($self->{widget});
$self->{popup} = $popupwindow;
$popupwindow->{window}->add($frame);
return $popupwindow;
}
sub _create_widget {
my ($self) = @_;
my @column_types;
$column_types[OR_______COLUMN] = 'Glib::String';
$column_types[OPERATOR_COLUMN] = 'Glib::String';
$column_types[MODEL____COLUMN] = 'Gtk2::ListStore';
$column_types[NAME_____COLUMN] = 'Glib::String';
my $table = Gtk2::Table->new(2,4,FALSE);
my $operatorcombo = Gtk2::ComboBox->new_text;
my $entry = Gtk2::Entry->new;
my $ok_button = Gtk2::Button->new_from_stock('gtk-ok');
my $add_another_button = Gtk2::Button->new('_Another Pattern');
my $treemodel = Gtk2::ListStore->new (@column_types);
my $treeview= Gtk2::TreeView->new($treemodel);
my $scrolledwindow = Gtk2::ScrolledWindow->new;
$self->{treeview} = $treeview;
$self->{treemodel} = $treemodel;
$self->{entry} = $entry;
$self->{operatorcombo} = $operatorcombo;
$self->{add_another_button} = $add_another_button;
$self->{ok_button} = $ok_button;
$self->_add_combo;
$self->_populate;
$treeview->set_headers_visible(FALSE);
$treeview->get_selection->set_mode('multiple');
$treeview->signal_connect ('key-press-event' =>
sub {
my ($widget, $event) = @_;
if ($event->keyval == $Gtk2::Gdk::Keysyms{'Delete'}) {
my @paths = $treeview->get_selection->get_selected_rows;
return if $#paths < 0;
my @sel = map { $_->to_string } @paths;
my $data = $self->{datamodel};
foreach my $i (reverse sort @sel) {
splice (@$data, $i, 1);
}
$self->{datamodel} = $data;
$self->_populate;
$self->_check_list_visibility();
&{ $self->{signals}->{'changed'} } if $self->{signals}->{'changed'};
}
}
);
$operatorcombo->set_wrap_width(1);
my $operatorlist = $self->{operatorlist};
foreach my $x (@$operatorlist) {
$operatorcombo->append_text($x);
}
$operatorcombo->set_active(0);
$operatorcombo->signal_connect('realize' =>
sub {
$treeview->get_column(1)->set_min_width(
lib/Gtk2/Ex/SearchBox.pm view on Meta::CPAN
$window->add($searchbox->{widget});
=head1 METHODS
=head2 new;
The constructor.
my $searchbox = Gtk2::Ex::SearchBox->new;
If you want to allow only one pattern at a time, then you can call the constructor
with C<'single'> as an argument.
my $searchbox = Gtk2::Ex::SearchBox->new('single');
By default, the dropdown combobox contains the following choices.
[
'contains',
'doesn\'t contain',
'not equal to',
'equals',
]
But if you want to specify your own set, then you can call the constructor with
two arguments.
my $operatorlist = [
'starts with',
'ends with',
'has in the middle'
];
my $searchbox = Gtk2::Ex::SearchBox->new('multiple', $operatorlist);
If you do not want even that combobox to the left, then send undef as the operatorlist.
my $searchbox = Gtk2::Ex::SearchBox->new('single', undef);
Now this is just an C<$enty> with an C<$ok_button> !!
=head2 set_model($model);
Sets the C<$model>. For example,
$model = [
{'operator' => 'contains', 'field' => 'this pattern'},
{'operator' => 'equals', 'field' => 'that exact pattern'},
]
=head2 get_model;
Returns the C<$model>
For example,
$model = [
{'operator' => 'contains', 'field' => 'this pattern'},
{'operator' => 'equals', 'field' => 'that exact pattern'},
]
=head2 attach_popup_to($parent);
This method returns a C<Gtk2::Ex::PopupWindow>. The popup window will contain
a C<Gtk2::Ex::SearchBox> widget.
=head2 to_sql_condition($datefieldname, $model);
Converts the C<$model> into an SQL condition so that it can be used directly in
and SQL statement. C<$fieldname> is the fieldname that will be used inside
the SQL condition.
=head2 signal_connect($signal, $callback);
See the SIGNALS section to see the supported signals.
=head1 SIGNALS
=head2 changed;
=head2 closed;
=head1 SEE ALSO
Gtk2::Ex::PopupWindow
=head1 COPYRIGHT & LICENSE
Copyright 2005 Ofey Aikon, All Rights Reserved.
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Library General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option) any
later version.
This library is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU Library General Public License for more
details.
You should have received a copy of the GNU Library General Public License along
with this library; if not, write to the Free Software Foundation, Inc., 59
Temple Place - Suite 330, Boston, MA 02111-1307 USA.
=cut
( run in 1.904 second using v1.01-cache-2.11-cpan-364913b4093 )