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

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

Remove empty event EVT_KEY_DOWN processing.

  • Property svn:eol-style set to LF
File size: 10.8 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             wxGROW
33             wxEXPAND
34         /;
35
36use base qw/Wx::Panel Uploader::Object2/;
37use Wx::Locale qw/:default/;
38
39use Data::Dumper;
40__PACKAGE__->mk_accessors(     
41    qw/
42        frame_callbacks
43        choices
44        selection
45        id_lookup
46        row_lookup
47        id_from_name
48        search
49        choices_list
50        btn_show
51        show_method
52        is_selected
53        creation_callback
54        search_result
55        previous_autocompleted
56        autocomplete_from
57        autocomplete_to
58        autocomplete_set_selection
59        current_show_mode
60      /
61);
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    my $self = $class->SUPER::new( $params->{parentwnd}, $params->{id}||-1, wxDefaultPosition, wxDefaultSize );
70
71    $self->init_panel;
72    $self->init_properties($params);
73    $self->init_event_handlers;
74
75    $self;
76}
77
78
79sub init_panel {
80    my ( $self ) = @_;   
81
82    my( $vsizer ) = Wx::BoxSizer->new( wxVERTICAL );
83   
84    $self->search(
85        Wx::SearchCtrl->new( $self, -1, "", wxDefaultPosition, [-1,-1],wxTE_PROCESS_ENTER )
86    );
87
88    $self->search->ShowCancelButton( 1 );
89    $vsizer->Add( $self->search, 0, wxEXPAND|wxALL, 2 );
90
91    $self->choices_list(
92        Wx::CheckListBox->new( $self, -1, wxDefaultPosition, [-1,-1], [], 0 )
93    );
94    $vsizer->Add( $self->choices_list, 1, wxEXPAND|wxALL, 2 );
95
96    $self->btn_show(
97        Wx::RadioBox->new( $self, -1, sprintf("%s :", gettext("Show")), wxDefaultPosition, [-1, -1], 
98            [gettext("All"),gettext("Selection")] , 1, wxRA_SPECIFY_ROWS )
99    );
100    $vsizer->Add( $self->btn_show, 0, wxEXPAND|wxALL, 2 );
101
102    $self->SetSizer( $vsizer );
103    $vsizer->Fit( $self );
104}
105
106
107sub init_properties {
108    my ( $self, $params ) = @_;   
109   
110    $self->choices(
111        $params->{choices}|| sub { [] }
112    );
113
114    $self->selection(
115        $params->{selection}|| sub{ [] }
116    );
117
118    $self->search_result(
119        []
120    );
121   
122    $self->is_selected({});
123   
124    $self->creation_callback(
125        $params->{creation_callback}
126    ) if 'CODE' eq ref($params->{creation_callback});
127
128    $self->show_method(
129        {
130            gettext("All") => sub { $self->show_all(@_) },
131            gettext("Selection") => sub { $self->show_selected(@_) },
132        }
133    );
134}
135
136
137sub RefreshChoices {
138    my ( $self ) = @_;
139
140    $self->refresh_lookups(
141        $self->choices->()
142    );
143    $self->refresh_is_selected;
144    my $show_mode = $self->current_show_mode||gettext("All");
145    $self->show_method->{$show_mode}->();
146}
147
148
149sub refresh_lookups {
150    my ( $self, $choices ) = @_;
151
152    $self->id_lookup({});
153    $self->row_lookup({});
154    $self->id_from_name({});
155
156    my $row = 0;
157    #print Dumper $self->properties;
158    map {
159        $self->id_lookup->{$row} = $_->{id};
160        $self->row_lookup->{$_->{id}} = $row;
161        $self->id_from_name->{$_->{name}} = $_->{id};
162        $row++;   
163    }@{$choices};
164
165}
166
167
168sub refresh_is_selected {
169    my ( $self ) = @_;
170
171    my $selection = $self->selection->();
172    $self->is_selected({});
173
174    map {
175        $self->is_selected->{$_} = 1;
176    } @$selection;
177
178}
179
180
181sub show_all {
182    my ( $self ) = @_;
183
184    my $choices = $self->choices->();
185    $self->current_show_mode(
186        gettext("All")
187    );
188    $self->refresh_choices_list(
189        $self->choices->()
190    );
191}
192
193
194sub show_selected {
195    my ( $self ) = @_;   
196
197    $self->current_show_mode(
198        gettext("Selection")
199    );
200
201    $self->refresh_choices_list(
202        $self->get_selected_choices()
203    );
204}
205
206
207sub refresh_choices_list {
208    my ( $self, $choices ) = @_;
209
210    $self->choices_list->Freeze;
211    $self->choices_list->Clear;
212
213    #print Dumper $self->properties;
214    my $i=0;
215    map {
216        $self->choices_list->Append(
217            $_->{name},
218        );
219        $self->choices_list->Check(
220            $i,
221            $self->is_selected->{$_->{id}}
222        );
223        $i++;
224    }@{$choices};
225    $self->choices_list->Thaw;
226}
227
228
229sub get_selected_choices {
230    my ( $self ) = @_;
231
232    my $choices = $self->choices->()||[];
233    # each choice item is { id => $id,  name => $name, url_nam => $url_name }
234    my $selected = [
235        grep { $self->is_selected->{$_->{id}} } @$choices
236    ];
237
238
239    $selected;
240}
241
242
243sub ClearAllSelection {
244    my ( $self ) = @_;
245
246    my $row = 0;
247   
248    my $choices = $self->choices->();
249    $self->choices_list->Freeze;
250    map {
251        $self->choices_list->Check($row, 0) ;   
252        $row++;
253    }@$choices;
254    $self->choices_list->Thaw;
255}
256
257
258sub init_event_handlers {
259    my ( $self ) = @_;
260
261    Wx::Event::EVT_CHECKLISTBOX( $self, $self->choices_list, \&OnCheck );
262    Wx::Event::EVT_LISTBOX( $self, $self->choices_list, \&OnSelected );
263    Wx::Event::EVT_SEARCHCTRL_SEARCH_BTN( $self, $self->search, \&OnSearch );
264    Wx::Event::EVT_SEARCHCTRL_CANCEL_BTN( $self, $self->search, \&OnCancel );
265    Wx::Event::EVT_TEXT( $self, $self->search, \&OnSearch );
266    Wx::Event::EVT_TEXT_ENTER( $self, $self->search, \&OnSearchEnter );
267    Wx::Event::EVT_RADIOBOX( $self, $self->btn_show, \&OnShow );
268    # fix for linux : can not call SetSelection before the current event is processed.
269    # call when idle
270    Wx::Event::EVT_IDLE(
271        $self,
272        sub {
273            my ($self, $event)=@_;
274            $self->search->SetSelection(
275                $self->autocomplete_from,
276                $self->autocomplete_to
277            ) if $self->autocomplete_set_selection;
278            $self->autocomplete_set_selection(0);
279        }
280    );
281
282}
283
284
285sub OnSelected {
286    my ( $self, $event ) = @_;
287
288    my $list = $event->GetEventObject;
289    my $row = $event->GetInt;
290
291    $list->Check(
292        $row,
293        !$list->IsChecked($row)   
294    );
295
296    $self->OnCheck($event);
297
298}
299
300
301sub OnCheck {
302    my ( $self, $event ) = @_;
303
304    my $list = $event->GetEventObject;
305    my $indx = $event->GetInt;
306    my $item = $list->GetString($indx);
307    if($list->IsChecked($indx)){
308       $self->add_to_selection($item);
309    }
310    else{
311        $self->remove_from_selection($item);
312    }
313   
314}
315
316
317sub add_to_selection {
318    my ( $self, $item ) = @_;
319
320    return if !defined $item;
321
322    my $id = $self->id_from_name->{$item};
323    return if !defined $id;
324
325    $self->is_selected->{$id} = 1;
326    my $selection = $self->selection->();
327
328    $self->selection->(
329        [
330            $id,
331            @$selection
332        ]
333    );
334}
335
336
337sub remove_from_selection {
338    my ( $self, $item ) = @_;
339
340    my $id = $self->id_from_name->{$item};
341    my $selection = $self->selection->();
342    $self->selection->(
343        [
344            grep { $_ ne $id } @$selection
345        ]
346    );
347    $self->refresh_is_selected;
348}
349
350
351sub OnShow {
352    my( $self, $event ) = @_;
353
354    my $show_mode = $event->GetString();
355    $self->show_method->{$show_mode}->();
356}
357
358
359sub OnSearchEnter {
360    my( $self, $event ) = @_;
361
362    my $searched = $self->search->GetValue;
363    eval { $searched =~ s/\s+$//; };
364    $searched = $searched eq "" ? undef : $searched ;
365
366    my $cleanup;
367    if(defined($searched)){
368   
369        my $busy = Wx::BusyCursor->new();
370   
371        if(!scalar @{$self->search_result}){
372            $self->creation_callback->(
373                $searched   
374            );
375   
376            $self->refresh_selected_searched($searched);
377            $cleanup = 1;
378        }
379   
380        if( 1 == scalar @{$self->search_result}){
381            $self->refresh_selected_searched($searched);
382            $cleanup = 1;
383        }
384   
385    }
386    else{
387        $cleanup = 1;
388    }
389
390    if($cleanup){   
391        $self->search->ChangeValue("");
392        $self->search->SetFocus;
393    }
394}
395
396sub refresh_selected_searched {
397    my ( $self, $searched ) = @_;       
398
399    $self->refresh_lookups(
400        $self->choices->()
401    );
402    $self->add_to_selection($searched);
403    $self->show_selected;
404    $self->btn_show->SetStringSelection(gettext("Selection"));
405}
406
407sub OnSearch {
408    my( $self, $event ) = @_;
409
410    $self->btn_show->SetStringSelection(gettext("All"));
411
412    my $searched = $self->search->GetValue;
413
414    $self->search_result(
415        $self->search_in_choices(
416            $searched
417        )
418    );
419
420    $self->refresh_choices_list(
421        $self->search_result
422    );
423   
424    # autocompletion
425    if(1== scalar @{$self->search_result}){
426        my $value = $self->search_result->[0]->{name};
427        unless(  $value eq $self->previous_autocompleted ){
428            $self->autocomplete_from(
429                $self->search->GetLastPosition
430            );
431            $self->search->ChangeValue($value);
432            $self->autocomplete_to(
433                $self->search->GetLastPosition
434            );
435            $self->autocomplete_set_selection(1);
436            $self->previous_autocompleted($value);
437        }   
438    }
439    else{
440        $self->previous_autocompleted(undef);
441    }
442   
443    $event->Skip;
444}
445
446
447sub search_in_choices {
448    my ( $self, $searched ) = @_;
449   
450    my $choices = $self->choices->()||[];
451   
452    [
453       grep { $_->{name} =~ /\b$searched/i} @$choices
454    ];
455}
456
457
458sub OnCancel {
459    my( $self, $event ) = @_;
460
461    $self->init_choices_list(
462        $self->choices->()
463    );
464   
465    $self->btn_show->SetStringSelection(gettext("All"));
466}
467
468
4691;
Note: See TracBrowser for help on using the repository browser.