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