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

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

Feature 1520 added : ability to set photo default caption after the photo is added.

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