source: extensions/pLoader/trunk/src/Uploader/GUI/wxChoiceFilteredPanel.pm @ 4779

Last change on this file since 4779 was 4779, checked in by ronosman, 14 years ago

Change copyright notice to add 2010.

  • Property svn:eol-style set to LF
File size: 10.3 KB
Line 
1# +-----------------------------------------------------------------------+
2# | pLoader - a Perl photo uploader for Piwigo                            |
3# +-----------------------------------------------------------------------+
4# | Copyright(C) 2008-2010 Piwigo Team                  http://piwigo.org |
5# +-----------------------------------------------------------------------+
6# | This program is free software; you can redistribute it and/or modify  |
7# | it under the terms of the GNU General Public License as published by  |
8# | the Free Software Foundation                                          |
9# |                                                                       |
10# | This program is distributed in the hope that it will be useful, but   |
11# | WITHOUT ANY WARRANTY; without even the implied warranty of            |
12# | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
13# | General Public License for more details.                              |
14# |                                                                       |
15# | You should have received a copy of the GNU General Public License     |
16# | along with this program; if not, write to the Free Software           |
17# | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
18# | USA.                                                                  |
19# +-----------------------------------------------------------------------+
20package Uploader::GUI::wxChoiceFilteredPanel;
21use strict;
22use Wx qw/
23             wxDefaultSize
24             wxDefaultPosition
25             wxVERTICAL
26             wxALIGN_CENTER_VERTICAL
27             wxALL
28             wxSHAPED
29             wxALIGN_CENTER
30             wxTE_PROCESS_ENTER
31             wxRA_SPECIFY_ROWS
32         /;
33
34use base qw/Wx::Panel Uploader::Object2/;
35use Wx::Locale qw/:default/;
36
37use Data::Dumper;
38__PACKAGE__->mk_accessors(     
39    qw/
40        frame_callbacks
41        choices
42        selection
43        id_lookup
44        row_lookup
45        id_from_name
46        search
47        listchoices
48        btn_show
49        show_method
50        is_selected
51        creation_callback
52        search_result
53        previous_autocompleted
54                autocomplete_from
55                autocomplete_to
56                autocomplete_set_selection
57      /
58);
59
60use Wx::Event qw(EVT_CHECKLISTBOX EVT_LISTBOX EVT_SEARCHCTRL_SEARCH_BTN EVT_SEARCHCTRL_CANCEL_BTN
61                 EVT_TEXT EVT_TEXT_ENTER EVT_IDLE EVT_RADIOBOX);
62
63
64sub new {
65    my ($this, $params) = @_;
66    #on recupere le nom de la classe en fonction du type d'appel de la méthode.
67    my $class = ref($this) || $this;
68
69
70    my $self = $class->SUPER::new( $params->{parentwnd}, $params->{id}||-1, wxDefaultPosition, wxDefaultSize );
71    $self->_init_panel;
72    $self->_init_properties($params);
73
74    $self->Refresh;
75
76    EVT_CHECKLISTBOX( $self, $self->listchoices, \&OnCheck );
77    EVT_LISTBOX( $self, $self->listchoices, \&OnSelected );
78    EVT_SEARCHCTRL_SEARCH_BTN( $self, $self->search, \&OnSearch );
79    EVT_SEARCHCTRL_CANCEL_BTN( $self, $self->search, \&OnCancel );
80    EVT_TEXT( $self, $self->search, \&OnSearch );
81    EVT_TEXT_ENTER( $self, $self->search, \&OnSearchEnter );
82    EVT_RADIOBOX( $self, $self->btn_show, \&OnShow );
83        # fix for linux : can not call SetSelection before the current event is processed.
84        # call when idle
85        EVT_IDLE(
86            $self,
87                sub {
88                    my ($self, $event)=@_;
89                        $self->search->SetSelection(
90                            $self->autocomplete_from,
91                            $self->autocomplete_to
92                        ) if $self->autocomplete_set_selection;
93                        $self->autocomplete_set_selection(0);
94                }
95        );
96
97    $self;
98}
99
100sub _init_properties {
101    my ( $self, $params ) = @_; 
102       
103    $self->choices(
104        $params->{choices}|| sub { [] }
105    );
106
107    $self->selection(
108        $params->{selection}|| sub{ [] }
109    );
110
111    my $choices = $self->choices->();
112    $self->search_result(
113        []
114    );
115   
116   
117   
118    $self->is_selected({});
119   
120    $self->creation_callback(
121        $params->{creation_callback}
122    ) if 'CODE' eq ref($params->{creation_callback});
123
124    $self->show_method(
125        {
126            gettext("All") => sub { $self->_show_all(@_) },
127            gettext("Selected") => sub { $self->_show_selected(@_) },
128            gettext("Not selected") => sub { $self->_show_notselected(@_) },
129        }
130    );
131}
132
133sub _init_panel {
134    my ( $self ) = @_; 
135
136    my( $vsizer ) = Wx::BoxSizer->new( wxVERTICAL );
137   
138    $self->search(
139        Wx::SearchCtrl->new( $self, -1, "", wxDefaultPosition, [320,-1],wxTE_PROCESS_ENTER )
140    );
141
142    $self->search->ShowCancelButton( 1 );
143
144    $vsizer->AddWindow( $self->search, 0, wxALIGN_CENTER_VERTICAL|wxALL, 2 );
145
146    $self->btn_show(
147        Wx::RadioBox->new( $self, -1, sprintf("%s :", gettext("Show")), wxDefaultPosition, [320, -1], 
148            [gettext("All"),gettext("Selected"),gettext("Not selected")] , 1, wxRA_SPECIFY_ROWS )
149    );
150    $vsizer->AddWindow( $self->btn_show, 0, wxALIGN_CENTER_VERTICAL|wxALL, 2 );
151
152
153    $self->listchoices(
154        Wx::CheckListBox->new( $self, -1, wxDefaultPosition, [320,200], [], 0 )
155    );
156    $vsizer->AddWindow( $self->listchoices, 0, wxALIGN_CENTER_VERTICAL|wxALL, 2 );
157
158    $self->SetSizer( $vsizer );
159         
160    $vsizer->SetSizeHints( $self );
161}
162
163sub _init_choices {
164    my ( $self, $choices ) = @_;
165
166        $self->listchoices->Freeze;
167    $self->listchoices->Clear;
168    $self->id_lookup({});
169    $self->row_lookup({});
170    $self->id_from_name({});
171    $self->is_selected({});
172
173    my $row = 0;
174    #print Dumper $self->properties;
175    map {
176        $self->listchoices->Append(
177            $_->{name},
178        );
179        $self->id_lookup->{$row} = $_->{id};
180        $self->row_lookup->{$_->{id}} = $row;
181        $self->id_from_name->{$_->{name}} = $_->{id};
182        $row++; 
183    }@{$choices};
184        $self->listchoices->Thaw;
185
186}
187
188sub Refresh {
189    my ( $self, $choices, $selected ) = @_;
190
191   
192
193    if (!defined $choices){
194        $self->btn_show->SetStringSelection(gettext("All"));
195        $choices = $self->choices->();
196    }
197
198    $self->_init_choices($choices);
199    $self->SetSelection($selected);
200       
201}
202
203sub ClearAllSelection {
204    my ( $self ) = @_;
205
206    my $row = 0;
207   
208    my $choices = $self->choices->();
209    $self->listchoices->Freeze;
210    map {
211        $self->listchoices->Check($row, 0) ;   
212        $row++;
213    }@$choices;
214    $self->listchoices->Thaw;
215}
216
217sub SetSelection {
218    my ( $self, $selected ) = @_;
219
220    my $selection = $self->selection->()||[];
221
222    push @$selection, $self->id_from_name->{$selected} if defined $selected;
223
224    $self->listchoices->Freeze;
225    map {
226        $self->listchoices->Check(
227            $self->row_lookup->{$_},
228            exists $self->row_lookup->{$_},
229        ) if defined $self->row_lookup->{$_};
230        $self->is_selected->{$_} = 1;   
231    }
232    @{$selection};     
233    $self->listchoices->Thaw;
234}
235
236sub GetSelection {
237    my ( $self ) = @_;
238
239    my $row = 0;
240    my $selection = [];
241   
242    my $choices = $self->choices->();
243    map {
244           
245        my $id = $self->id_lookup->{$row};
246        $self->is_selected->{$id} = $self->listchoices->IsChecked($row) ;       
247        $row++;
248    }@$choices;
249
250    [
251        grep { $self->is_selected->{$_} } keys %{$self->is_selected}
252    ]; 
253}
254
255sub OnCheck {
256    my ( $self ) = @_;
257   
258    $self->selection->(
259        $self->GetSelection
260    ); 
261}
262
263sub OnSelected {
264    my ( $self, $event ) = @_;
265
266    my $row = $event->GetInt;
267    $self->listchoices->Check(
268        $row,
269        !$self->listchoices->IsChecked($row)   
270    );
271
272    $self->OnCheck;
273
274    $event->Skip;   
275}
276
277sub OnShow {
278    my( $self, $event ) = @_;
279
280    my $show = $event->GetString();
281   
282    $self->show_method->{$show}->();                               
283}
284
285sub OnSearchEnter {
286    my( $self, $event ) = @_;
287
288    my $searched = $self->search->GetValue;
289    $searched =~ s/\s+$//;
290    $searched = $searched eq "" ? undef : $searched ;
291
292    my $cleanup;
293    if(defined($searched)){
294   
295        my $busy = Wx::BusyCursor->new();
296   
297        if(!scalar @{$self->search_result}){
298            $self->creation_callback->(
299                $searched   
300            );
301   
302            $self->_refresh_selected_searched($searched);
303            $cleanup = 1;
304        }
305   
306        if( 1 == scalar @{$self->search_result}){
307            $self->_refresh_selected_searched($searched);
308            $cleanup = 1;
309        }
310   
311    }
312    else{
313        $cleanup = 1;
314    }
315
316    if($cleanup){   
317        $self->search->ChangeValue("");
318        $self->search->SetFocus;
319    }
320}
321
322sub _refresh_selected_searched {
323    my ( $self, $searched ) = @_;       
324
325    $self->Refresh;
326    $self->SetSelection($searched);
327    $self->_show_selected;
328    $self->btn_show->SetStringSelection(gettext("Selected"));
329}
330
331sub OnSearch {
332    my( $self, $event ) = @_;
333
334    $self->btn_show->SetStringSelection(gettext("All"));
335
336    my $searched = $self->search->GetValue;
337
338    $self->search_result(
339        $self->_filter_choices(
340            $searched
341        )
342    );
343
344    $self->Refresh(
345        $self->search_result
346    );
347   
348    # autocompletion
349    if(1== scalar @{$self->search_result}){
350        my $value = $self->search_result->[0]->{name};
351        unless(  $value eq $self->previous_autocompleted ){
352            $self->autocomplete_from(
353                            $self->search->GetLastPosition
354                    );
355            $self->search->ChangeValue($value);
356            $self->autocomplete_to(
357                            $self->search->GetLastPosition
358                    );
359            $self->autocomplete_set_selection(1);
360            $self->previous_autocompleted($value);
361        }       
362    }
363    else{
364        $self->previous_autocompleted(undef);
365    }
366       
367        $event->Skip;
368}
369
370sub OnCancel {
371    my( $self, $event ) = @_;
372
373    $self->Refresh(
374        $self->choices->()
375    );
376   
377    $self->btn_show->SetStringSelection(gettext("All"));
378}
379
380
381sub _filter_choices {
382    my ( $self, $searched ) = @_;
383   
384    my $choices = $self->choices->()||[];
385    [
386       grep { $_->{name} =~ /^$searched/} @$choices
387    ]
388}
389
390sub _show_notselected {
391    my ( $self ) = @_; 
392
393    $self->Refresh(
394        $self->_filter_notselected()
395    );
396}
397
398sub _show_selected {
399    my ( $self ) = @_; 
400
401    $self->Refresh(
402        $self->_filter_selected()
403    );
404}
405
406sub _show_all {
407    my ( $self ) = @_; 
408
409    $self->Refresh(
410        $self->choices->()
411    );
412}
413
414sub _filter_selected {
415    my ( $self ) = @_;
416
417    my $choices = $self->choices->()||[];
418    [
419        grep { $self->is_selected->{$_->{id}} } @$choices
420    ]   
421}
422
423sub _filter_notselected {
424    my ( $self ) = @_;
425
426    my $choices = $self->choices->()||[];
427    [
428        grep { !$self->is_selected->{$_->{id}} } @$choices
429    ]   
430}
431
432
4331;
Note: See TracBrowser for help on using the repository browser.