source: extensions/pLoader/trunk/src/Uploader/GUI/DlgCommon.pm

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

Bug 1710 fixed : pLoader lack of concurrency support causes data inconsistency.

File size: 14.7 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::DlgCommon;
21use strict;
22use Wx qw/wxDP_ALLOWNONE wxDefaultPosition wxDefaultSize/;
23use Wx::Calendar;
24use Wx::Locale qw/:default/;
25use Data::Dumper;
26use base qw/
27               Class::Accessor::Fast
28           /; 
29
30__PACKAGE__->mk_accessors(     
31    qw/
32        properties
33        frame_callback
34      /
35);
36
37sub InitHandler {
38    my ( $self ) = @_;
39
40    # to connect the right event handler to each control
41    my $ctrl_handlers = {
42        'Wx::TextCtrl' => sub { my ( $ctrl ) = @_; Wx::Event::EVT_TEXT( $ctrl, $ctrl, sub { $self->OnTextCtrl(@_) } ); },
43        'Wx::Choice'   => sub { my ( $ctrl ) = @_; Wx::Event::EVT_CHOICE( $ctrl, $ctrl, sub { $self->OnChoice(@_) } ); },
44        'Wx::RadioBox' => sub { my ( $ctrl ) = @_; Wx::Event::EVT_RADIOBOX( $ctrl, $ctrl, sub { $self->OnRadioBox(@_) } ); },
45        'Wx::CheckBox' => sub { my ( $ctrl ) = @_; Wx::Event::EVT_CHECKBOX( $ctrl, $ctrl, sub { $self->OnCheckBox(@_) } ); },
46        'Wx::SpinCtrl' => sub { my ( $ctrl ) = @_; Wx::Event::EVT_SPINCTRL( $ctrl, $ctrl, sub { $self->OnSpinCtrl(@_) } ); },
47        'Wx::ComboBox' => sub { my ( $ctrl ) = @_;
48                              Wx::Event::EVT_COMBOBOX( $ctrl, $ctrl, sub { $self->OnComboBox(@_) } );
49                              Wx::Event::EVT_TEXT( $ctrl, $ctrl, sub { $self->OnComboBoxText(@_) } );
50                          },
51        'Wx::DatePickerCtrl' => sub { my ( $ctrl ) = @_; Wx::Event::EVT_DATE_CHANGED( $ctrl, $ctrl, sub { $self->OnDatePicker(@_) } ); },
52        'Uploader::GUI::wxCategoryTreeCtrl' => sub { my ( $ctrl ) = @_;
53                                                   Wx::Event::EVT_TREE_SEL_CHANGED(
54                                                       $ctrl,
55                                                       $ctrl,
56                                                       sub { 
57                                                           $self->OnCategoryTreeSelChanged(@_); 
58                                                       }
59                                                   );
60                                               },
61    };
62   
63    map {
64        my $ctrl =$self->FindWindow($_);
65
66        if(defined $ctrl){
67            $ctrl_handlers->{ ref $ctrl}->($ctrl) if exists $ctrl_handlers->{ ref $ctrl};
68
69            $ctrl->SetValidator(
70                $self->properties->{$_}->{validator}
71            ) if exists $self->properties->{$_}->{validator};
72        }
73    }
74    keys %{$self->properties};
75   
76
77}
78
79# what does happen when the text changes
80sub OnCheckBox {
81    my ( $self, $ctrl, $event ) = @_;
82   
83    my $id = $ctrl->GetId;
84    # change the property value
85    $self->properties->{$id}->{value}->(
86        $event->IsChecked
87    ) if exists $self->properties->{$id}->{value};
88
89    # exec the callback
90    $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists
91        $self->properties->{$id}->{frame_callback};   
92
93    $event->Skip;
94}
95
96# what does happen when selection changes
97
98
99sub OnComboBoxText {
100    my ( $self, $ctrl, $event ) = @_;
101
102    # text changed by user, not selection
103    if( $ctrl->GetValue ne $ctrl->GetStringSelection){
104       my $id = $ctrl->GetId;
105        # change the property value
106        $self->properties->{$id}->{value}->(
107            $ctrl->GetValue
108        ) if exists $self->properties->{$id}->{value};
109
110        # exec the callback
111        $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists
112            $self->properties->{$id}->{frame_callback};   
113    }
114
115}
116
117
118sub OnComboBox {
119    my ( $self, $ctrl, $event ) = @_;
120     
121    my $id = $ctrl->GetId;
122
123    $ctrl->{_multi_selection_mode} = $self->properties->{$id}->{multi_selection_mode}->()
124        if exists $self->properties->{$id}->{multi_selection_mode};
125
126    my $selection = exists $self->properties->{$id}->{string_selection} ? $ctrl->GetValue : $ctrl->GetSelection;
127
128    my $value = $self->properties->{$id}->{pre_process}->( $selection ) if exists
129        $self->properties->{$id}->{pre_process};   
130
131    if( exists $self->properties->{$id}->{value} ){
132        $value = $self->properties->{$id}->{value}->(
133            $value,
134            $selection,
135        );
136        # the item selected in the list is not the real value
137        # we place here the actual value
138        # to use in EVT_IDLE event, and overwrite the text field with it
139        $ctrl->{_value} = $value;
140    }
141
142    # exec the callback
143    $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists
144        $self->properties->{$id}->{frame_callback};   
145}
146
147
148# what does happen when the text changes
149sub OnTextCtrl {
150    my ( $self, $ctrl, $event ) = @_;
151   
152    my $id = $ctrl->GetId;
153    # change the property value
154    $self->properties->{$id}->{value}->(
155        $event->GetString
156    ) if exists $self->properties->{$id}->{value};
157
158    # exec the callback
159    $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists
160        $self->properties->{$id}->{frame_callback};   
161   
162}
163
164sub OnSpinCtrl {
165    my ( $self, $ctrl, $event ) = @_;
166   
167    my $id = $ctrl->GetId;
168    # change the property value
169    $self->properties->{$id}->{value}->(
170        $event->GetInt
171    ) if exists $self->properties->{$id}->{value};
172
173    # exec the callback
174    $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists
175        $self->properties->{$id}->{frame_callback};   
176   
177    $event->Skip;
178}
179
180sub OnRadioBox {
181    my ( $self, $ctrl, $event ) = @_;
182   
183    my $id = $ctrl->GetId;
184
185    # if a preprocess value is required
186    my $value = $event->GetSelection;
187    $value = $self->properties->{$id}->{pre_process}->( $value ) if exists
188        $self->properties->{$id}->{pre_process};   
189    # change the property value. use the index selection
190    $self->properties->{$id}->{selection}->(
191        $value
192    ) if exists $self->properties->{$id}->{selection};
193
194    # exec the callback
195    $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists
196        $self->properties->{$id}->{frame_callback};   
197   
198    $event->Skip;
199
200}
201
202sub OnChoice {
203    my ( $self, $ctrl, $event ) = @_;
204   
205    my $id = $ctrl->GetId;
206
207    # if a preprocess value is required
208    my $value = exists $self->properties->{$id}->{string_selection} ? $event->GetString : $event->GetSelection;
209    $value = $self->properties->{$id}->{pre_process}->( $value ) if exists
210        $self->properties->{$id}->{pre_process};   
211    # change the property value. use the index selection
212    $self->properties->{$id}->{selection}->(
213        $value
214    ) if exists $self->properties->{$id}->{selection};
215
216    # change the property value. use the string
217    $self->properties->{$id}->{string_selection}->(
218        $value
219    ) if exists $self->properties->{$id}->{string_selection};
220
221    # exec the callback
222    $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists
223        $self->properties->{$id}->{frame_callback};   
224   
225    $event->Skip;
226}
227
228
229sub OnDatePicker {
230    my ( $self, $ctrl, $event ) = @_;
231   
232    my $id = $ctrl->GetId;
233
234    # change the property value
235    $self->properties->{$id}->{value}->(
236        $event->GetDate->FormatISODate
237    ) if exists $self->properties->{$id}->{value};
238    # exec the callback
239    $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists
240        $self->properties->{$id}->{frame_callback};   
241   
242    $event->Skip;
243}
244
245
246my $change_value = {
247    'Wx::SpinCtrl' => sub { my ($ctrl, $value ) = @_; $ctrl->SetValue($value)},
248    'Wx::TextCtrl' => sub { my ($ctrl, $value ) = @_; $ctrl->ChangeValue($value)},
249    'Wx::CheckBox' => sub { my ($ctrl, $value ) = @_; $ctrl->SetValue($value)},
250    'Wx::DatePickerCtrl' => sub { my ($ctrl, $value ) = @_;
251                                  my $date = Wx::DateTime->new;   
252                                  my ($yyyy, $mm, $dd, $hh, $mi, $ss ) = split(/[:\/\\\-\.\s]/, $value);
253                                  $date->ParseDate(
254                                    sprintf("%s/%s/%s", $yyyy, $mm, $dd)
255                                  );
256                                  if(-1 eq $value){
257                                      $date->SetToCurrent;
258                                  }
259                                  $ctrl->SetValue($date);
260                            },
261    'Wx::ComboBox' => sub { 
262                          my ($ctrl, $value ) = @_; 
263                          $ctrl->SetValue($value) ; 
264                      },
265};
266
267
268sub OnCategoryTreeSelChanged {
269    my ( $self, $ctrl, $event ) = @_;
270
271    my $id = $ctrl->GetId;
272
273    # change the property value.
274    $self->properties->{$id}->{id_selection}->(
275        $event->GetEventObject->selection_ids
276    ) if exists $self->properties->{$id}->{id_selection};
277
278    #printf("OnCategoryTreeSelChanged %s\n", Dumper $self->properties->{$id}->{id_selection}->());
279
280    # exec the callback
281    $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists
282        $self->properties->{$id}->{frame_callback};   
283   
284    $event->Skip;
285}
286
287
288my $clear_value = {
289    'Wx::TextCtrl' => sub { my ($ctrl) = @_; $ctrl->Clear},
290    'Wx::CheckBox' => sub { my ($ctrl) = @_; $ctrl->SetValue(0)},
291    'Wx::DatePickerCtrl' => sub { my ($ctrl) = @_; $ctrl->SetValue(Wx::DateTime->new->SetToCurrent)},
292    'Uploader::GUI::wxCategoryTreeCtrl' => sub { my ( $ctrl ) = @_;
293                                               $ctrl->SelectItem($ctrl->GetRootItem);
294                                           },
295};   
296
297sub SetProperties {
298    my ( $self ) = @_;
299   
300    map {
301        my $ctrl =$self->FindWindow($_);
302        if(defined $ctrl){
303            #printf("%s\n", $ctrl);
304            # checkbox, static text
305            $change_value->{ref $ctrl}->(
306                $ctrl,
307                $self->properties->{$_}->{value}->()
308            ) if exists $self->properties->{$_}->{value};
309            # only works for control with items
310            $ctrl->SetSelection(
311                $self->properties->{$_}->{selection}->()
312            ) if exists $self->properties->{$_}->{selection};
313
314            $ctrl->SetStringSelection(
315                gettext(
316                    $self->properties->{$_}->{string_selection}->()
317                )
318            ) if exists $self->properties->{$_}->{string_selection};
319        }
320    }
321    keys %{$self->properties};
322}
323
324sub InitLabels {
325    my ( $self ) = @_;
326   
327    map {
328        my $ctrl =$self->FindWindow($_)||$self->{$_};
329        if(defined $ctrl){
330            # checkbox, static text
331            $ctrl->SetLabel(
332               $self->properties->{$_}->{label}
333            ) if exists $self->properties->{$_}->{label};
334
335            $ctrl->GetStaticBox->SetLabel(
336                $self->properties->{$_}->{staticbox_label}
337            ) if exists $self->properties->{$_}->{staticbox_label};
338
339            # radiobox
340            my $labels =$self->properties->{$_}->{labels};
341            $labels||=[];
342            for(my $i=0; $i < scalar @$labels ; $i++){
343                $ctrl->SetItemLabel($i, $labels->[$i]);
344            }
345
346            # notebook pages
347            my $texts =$self->properties->{$_}->{texts};
348            $texts||=[];
349            for(my $i=0; $i < scalar @$texts ; $i++){
350                $ctrl->SetPageText($i, $texts->[$i]);
351            }
352
353        }
354    }
355    keys %{$self->properties};
356
357}
358
359sub InitChoices {
360    my ( $self ) = @_;
361
362    map {
363        my $ctrl =$self->FindWindow($_);
364        #printf("ctrl %s : %s\n", $_, $ctrl);       
365        if(defined $ctrl){
366            # choice
367            my $choices =$self->properties->{$_}->{choices};
368            $choices||=[];
369            map{
370                $ctrl->Append($_);
371            }@$choices;
372        }
373    }
374    keys %{$self->properties};
375
376}
377
378sub GetProperties {
379    my ( $self ) = @_;
380
381    map {
382        my $ctrl = $self->FindWindow($_);
383        #printf("ctrl %s : %s\n", $_, $ctrl);       
384        if(defined $ctrl){
385        # checkbox, static text
386            $self->properties->{$_}->{value}->(
387                $ctrl->GetValue()
388            ) if exists $self->properties->{$_}->{value};
389
390            $self->properties->{$_}->{selection}->(
391                $ctrl->GetSelection()           
392            ) if exists $self->properties->{$_}->{selection};
393
394            $self->properties->{$_}->{string_selection}->(
395                $ctrl->GetStringSelection()           
396            ) if exists $self->properties->{$_}->{string_selection};
397        }
398    }
399    keys %{$self->properties};
400}
401
402sub ClearProperties {
403    my ( $self ) = @_;
404   
405    map {
406        my $ctrl =$self->FindWindow($_);
407        if(defined $ctrl){
408             # checkbox, static text
409            $clear_value->{ref $ctrl}->(
410                $ctrl
411            ) if exists  $clear_value->{ref $ctrl};
412            # only works for control with items
413            $ctrl->SetSelection(
414                -1           
415            ) if exists $self->properties->{$_}->{selection};
416        }
417
418            $ctrl->SetStringSelection(
419                -1           
420            ) if exists $self->properties->{$_}->{string_selection};
421    }
422    keys %{$self->properties};
423
424}
4251;
Note: See TracBrowser for help on using the repository browser.