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
RevLine 
[4779]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# +-----------------------------------------------------------------------+
[4467]20package Uploader::GUI::DlgCommon;
21use strict;
[5041]22use Wx qw/wxDP_ALLOWNONE wxDefaultPosition wxDefaultSize/;
[4477]23use Wx::Calendar;
[4569]24use Wx::Locale qw/:default/;
[5041]25use Data::Dumper;
[4469]26use base qw/
27               Class::Accessor::Fast
28           /; 
29
30__PACKAGE__->mk_accessors(     
31    qw/
32        properties
[4589]33        frame_callback
[4469]34      /
35);
36
[4475]37sub InitHandler {
38    my ( $self ) = @_;
[4469]39
[4569]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(@_) } ); },
[4779]45        'Wx::RadioBox' => sub { my ( $ctrl ) = @_; Wx::Event::EVT_RADIOBOX( $ctrl, $ctrl, sub { $self->OnRadioBox(@_) } ); },
[4569]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(@_) } ); },
[5184]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                          },
[4569]52    };
53   
54    map {
[4475]55        my $ctrl =$self->FindWindow($_);
56        if(defined $ctrl){
[4569]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        }
[4475]63    }
[4569]64    keys %{$self->properties};
65   
[4475]66
67}
68
69# what does happen when the text changes
[4569]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
[4779]86# what does happen when selection changes
[4569]87
[4779]88
[5184]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
[4569]110# what does happen when the text changes
[4475]111sub OnTextCtrl {
112    my ( $self, $ctrl, $event ) = @_;
[4569]113   
[4475]114    my $id = $ctrl->GetId;
115    # change the property value
[4569]116    $self->properties->{$id}->{value}->(
[4475]117        $event->GetString
118    ) if exists $self->properties->{$id}->{value};
119
120    # exec the callback
[4569]121    $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists
122        $self->properties->{$id}->{frame_callback};   
123   
[4475]124}
125
[4569]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
[4779]142sub OnRadioBox {
143    my ( $self, $ctrl, $event ) = @_;
144   
145    my $id = $ctrl->GetId;
[4569]146
[4779]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
[4476]164sub OnChoice {
165    my ( $self, $ctrl, $event ) = @_;
[4569]166   
[4476]167    my $id = $ctrl->GetId;
[4569]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
[4476]176    ) if exists $self->properties->{$id}->{selection};
177
[4569]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
[4476]183    # exec the callback
[4569]184    $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists
185        $self->properties->{$id}->{frame_callback};   
186   
187    $event->Skip;
[4476]188}
189
[5184]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
[4477]221sub OnDatePicker {
222    my ( $self, $ctrl, $event ) = @_;
[4569]223   
[4477]224    my $id = $ctrl->GetId;
[4476]225
[4477]226    # change the property value
[4569]227    $self->properties->{$id}->{value}->(
[4477]228        $event->GetDate->FormatISODate
229    ) if exists $self->properties->{$id}->{value};
230    # exec the callback
[4569]231    $self->properties->{$id}->{frame_callback}->($self, $ctrl, $event) if exists
232        $self->properties->{$id}->{frame_callback};   
233   
234    $event->Skip;
[4477]235}
236
237
[4475]238my $change_value = {
[4569]239    'Wx::SpinCtrl' => sub { my ($ctrl, $value ) = @_; $ctrl->SetValue($value)},
[4475]240    'Wx::TextCtrl' => sub { my ($ctrl, $value ) = @_; $ctrl->ChangeValue($value)},
241    'Wx::CheckBox' => sub { my ($ctrl, $value ) = @_; $ctrl->SetValue($value)},
[4477]242    'Wx::DatePickerCtrl' => sub { my ($ctrl, $value ) = @_;
[4569]243                                  my $date = Wx::DateTime->new;   
[4477]244                                  my ($yyyy, $mm, $dd, $hh, $mi, $ss ) = split(/[:\/\\\-\.\s]/, $value);
[4569]245                                  $date->ParseDate(
246                                    sprintf("%s/%s/%s", $yyyy, $mm, $dd)
247                                  );
[5041]248                                  if(-1 eq $value){
249                                      $date->SetToCurrent;
250                                  }
[4569]251                                  $ctrl->SetValue($date);
[5041]252                            },
[5184]253    'Wx::ComboBox' => sub { 
254                          my ($ctrl, $value ) = @_; 
255                          $ctrl->SetValue($value) ; 
256                      },
[4569]257};
[4475]258
[4478]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)},
[4569]263};   
[4478]264
[4467]265sub SetProperties {
[4469]266    my ( $self ) = @_;
[4569]267   
268    map {
[4469]269        my $ctrl =$self->FindWindow($_);
[4467]270        if(defined $ctrl){
[5184]271            #printf("%s\n", $ctrl);
[4569]272            # checkbox, static text
[4475]273            $change_value->{ref $ctrl}->(
[4569]274                $ctrl,
[4475]275                $self->properties->{$_}->{value}->()
[4569]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        }
[4475]288    }
[4569]289    keys %{$self->properties};
[4475]290}
[4467]291
[4475]292sub InitLabels {
293    my ( $self ) = @_;
[4569]294   
295    map {
296        my $ctrl =$self->FindWindow($_)||$self->{$_};
297        #printf("ctrl %s : %s\n", $_, $ctrl);       
[4475]298        if(defined $ctrl){
[4569]299            # checkbox, static text
300            $ctrl->SetLabel(
[4475]301               $self->properties->{$_}->{label}
[4569]302            ) if exists $self->properties->{$_}->{label};
303
304            $ctrl->GetStaticBox->SetLabel(
305                $self->properties->{$_}->{staticbox_label}
306            ) if exists $self->properties->{$_}->{staticbox_label};
[4784]307
[4475]308            # radiobox
309            my $labels =$self->properties->{$_}->{labels};
[4569]310            $labels||=[];
311            for(my $i=0; $i < scalar @$labels ; $i++){
312                $ctrl->SetItemLabel($i, $labels->[$i]);
313            }
[4784]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
[4569]322        }
[4467]323    }
[4569]324    keys %{$self->properties};
[4475]325
[4467]326}
327
[4476]328sub InitChoices {
329    my ( $self ) = @_;
330
[5184]331    map {
[4476]332        my $ctrl =$self->FindWindow($_);
[4569]333        #printf("ctrl %s : %s\n", $_, $ctrl);       
[4476]334        if(defined $ctrl){
335            # choice
336            my $choices =$self->properties->{$_}->{choices};
[4569]337            $choices||=[];
338            map{
339                $ctrl->Append($_);
340            }@$choices;
341        }
[4476]342    }
[4569]343    keys %{$self->properties};
[4476]344
345}
346
[4467]347sub GetProperties {
[4469]348    my ( $self ) = @_;
[4467]349
[4569]350    map {
[4469]351        my $ctrl = $self->FindWindow($_);
[4569]352        #printf("ctrl %s : %s\n", $_, $ctrl);       
[4467]353        if(defined $ctrl){
[4569]354        # checkbox, static text
[4477]355            $self->properties->{$_}->{value}->(
356                $ctrl->GetValue()
[4569]357            ) if exists $self->properties->{$_}->{value};
[4467]358
[4477]359            $self->properties->{$_}->{selection}->(
[4569]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        }
[4467]367    }
[4569]368    keys %{$self->properties};
[4467]369}
[4475]370
[4478]371sub ClearProperties {
372    my ( $self ) = @_;
[4569]373   
374    map {
[4478]375        my $ctrl =$self->FindWindow($_);
376        if(defined $ctrl){
[4569]377             # checkbox, static text
[4478]378            $clear_value->{ref $ctrl}->(
[4569]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};
[4478]390    }
[4569]391    keys %{$self->properties};
[4478]392
393}
[4467]3941;
Note: See TracBrowser for help on using the repository browser.