source: extensions/pLoader/trunk/src/Uploader/GUI/wxImageListCtrl.pm @ 5261

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

Remove useless constants

  • Property svn:eol-style set to LF
File size: 13.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::wxImageListCtrl;
21use strict;
22use Wx;
23use Wx::DND;
24use POSIX qw(ceil floor);
25use Wx::Locale qw/:default/;
26use Wx qw/
27             wxDefaultPosition
28             wxDefaultSize
29             wxLC_LIST
30             wxNO_BORDER
31             wxLC_EDIT_LABELS
32             wxLC_ICON
33             wxLC_REPORT
34             wxLC_SMALL_ICON
35             wxIMAGE_LIST_NORMAL
36             wxBITMAP_TYPE_JPEG
37             wxBITMAP_TYPE_PNG
38             wxBLACK
39             wxLC_REPORT
40             wxLIST_NEXT_ALL
41             wxLIST_STATE_SELECTED
42             wxLIST_STATE_DONTCARE
43             wxLIST_STATE_FOCUSED
44             wxIMAGE_LIST_SMALL
45             wxTheApp
46             wxVERTICAL
47             wxALIGN_CENTER
48             wxHORIZONTAL
49             wxALL
50             wxGROW
51             wxWHITE
52             wxSWISS
53             wxNORMAL
54             wxBOLD
55             wxBITMAP_TYPE_PNG
56         /;
57use Wx::Event qw/
58                    EVT_LIST_BEGIN_DRAG
59                    EVT_LIST_ITEM_SELECTED
60                    EVT_SIZE
61                /;
62         
63use base qw/Wx::ListCtrl Class::Accessor::Fast/;
64__PACKAGE__->mk_accessors( 
65    qw/
66          prevItemCount
67          imagenames
68          imagelist
69          wx_imagelist
70          image_size
71          arrange_items
72          styles
73          insert_items
74          initialize
75          columns
76          change_display_mode
77          wx_img
78          item_refresh
79      / 
80);
81use Data::Dumper;
82
83
84sub new {
85    my( $class, $params ) = @_;
86
87 
88    my( $self ) = $class->SUPER::new(
89        $params->{parentwnd},
90        -1,
91        wxDefaultPosition,
92        [ -1, -1],
93    );
94   
95    $self->wx_img(
96        Wx::ImageList->new( 16, 16, 1 )
97    );
98    $self->wx_img->Add(
99        Wx::Bitmap->new( wxTheApp->resource_path('image.png'), wxBITMAP_TYPE_PNG )   
100    );
101
102    $self->imagelist(
103        $params->{imagelist}
104    );
105    # bitmap container for photo selection
106    $self->wx_imagelist(
107        $params->{wx_imagelist}||$self->imagelist->wx_thumb_imglist
108    );
109
110    $self->image_size(
111        $params->{image_size}
112    );
113
114    $self->columns(
115        $params->{columns}
116    );
117
118    $self->prevItemCount(-1);
119
120
121    EVT_LIST_BEGIN_DRAG( $self, $self, \&OnBeginDrag);
122
123    EVT_SIZE($self, sub {
124            my ( $this, $event ) = @_;
125   
126            $this->arrange_items(1);
127            $event->Skip();
128        }
129    );
130   
131    Wx::Event::EVT_IDLE(
132        $self,
133        sub {
134            my ($self, $event)=@_;
135
136            if($self->arrange_items){
137                $self->Refresh; 
138                $self->arrange_items(0);
139            }
140            if($self->change_display_mode){
141                my $dm = $self->imagelist->display_mode;
142                   $self->initialize->{$dm}->($self) if exists $self->initialize->{$dm};
143                $self->Refresh; 
144                $self->change_display_mode(0);
145            }
146            $event->Skip;
147        }
148    );
149
150    # manages empty panel position
151    Wx::Event::EVT_PAINT( $self, sub {
152            my ( $self, $event ) = @_;
153            if(exists $self->{_empty_panel}){
154                my ($w, $h) = $self->GetSizeWH;
155                my ($x, $y) = $self->GetPositionXY;
156                my ($wp, $hp) = $self->{_empty_panel}->GetSizeWH;
157                my $x_off = ($w-$wp)/3 > 0 ? ($w-$wp)/3 : 0;
158                my $y_off = ($h-$hp)/3 > 0 ? ($h-$hp)/3 : 0;
159                $self->{_empty_panel}->Move(
160                    [ $x+$x_off, $y+$y_off ]
161                );
162            }
163            $event->Skip;
164        }
165    );
166
167   
168    $self->initialize(
169        {
170
171            'Thumbnail' => sub {  my ( $self ) = @_;
172                                $self->Freeze;
173                                $self->ClearAll;
174                                $self->SetSingleStyle(wxLC_ICON);
175                                $self->SetSingleStyle(wxLC_EDIT_LABELS, 0);
176                                $self->SetImageList( $self->wx_imagelist, wxIMAGE_LIST_NORMAL ) ;
177                                $self->Thaw;
178                     },           
179            'Thumbnail and caption' => sub {  my ( $self ) = @_;
180                                $self->Freeze;
181                                $self->ClearAll;
182                                $self->SetSingleStyle(wxLC_ICON);
183                                $self->SetSingleStyle(wxLC_EDIT_LABELS);
184                                $self->SetImageList( $self->wx_imagelist, wxIMAGE_LIST_NORMAL ) ;
185                                $self->Thaw;
186                     },           
187             'Property list' => sub { my ( $self ) = @_;
188                                my $i=0;
189                               
190                                $self->Freeze;
191                                $self->ClearAll;
192                                $self->SetSingleStyle(wxLC_REPORT);
193                                $self->SetSingleStyle(wxLC_EDIT_LABELS);
194                                $self->SetImageList( $self->wx_img, wxIMAGE_LIST_SMALL ) ;
195                                map    {
196                                    $self->InsertColumn($i, $_->{label});
197                                    $i++;
198                                }@{$self->columns};                               
199                                $self->Thaw;
200                            },
201        }
202    );
203
204    $self->item_refresh(
205        {
206            'Thumbnail' => sub {},
207            'Thumbnail and caption' => sub { my ( $self, $index ) = @_;
208                                        my $image = $self->imagelist->GetImage($index);
209                                        $self->SetItem(
210                                            $index,
211                                            0,
212                                            $image->site_name,
213                                        ) if defined $image;
214                                    },
215            'Property list' => sub { my ( $self, $index ) = @_;
216                                my $col = 0;
217                                my $image = $self->imagelist->GetImage($index);
218                                map{
219                                    $self->SetItem(
220                                        $index,
221                                        $col,
222                                        $image->$_
223                                    ) if defined $image;
224                                    $self->SetColumnWidth($col, -1);
225                                    $col++;
226                                }
227                                qw/site_name site_comment site_author file create_date/;
228                            },
229        }
230    );
231   
232    $self->insert_items(
233        {
234            'Thumbnail' => sub { my ( $self, $index ) = @_; $self->InsertImageItem($index, $index);},
235            'Thumbnail and caption' => sub { my ( $self, $index ) = @_;
236                                           $self->InsertImageStringItem(
237                                               $index,
238                                               $self->imagelist->GetImage($index)->site_name,
239                                               $index,
240                                           );
241                                     },
242            'Property list' => sub { my ( $self, $index ) = @_;
243                                       
244                                $self->InsertImageStringItem(
245                                    $index,
246                                    "",
247                                    0
248                                );
249                                $self->ItemRefresh($index);
250
251                            },
252        }
253    );
254
255   
256    $self->initialize->{$self->imagelist->display_mode}->($self) if exists $self->initialize->{$self->imagelist->display_mode};
257
258    $self;
259}
260
261sub ItemRefresh {
262    my ( $self, $index ) = @_;
263   
264    $self->item_refresh->{$self->imagelist->display_mode}->($self, $index) if exists $self->item_refresh->{$self->imagelist->display_mode};
265}
266
267sub OnBeginDrag {
268  my( $self, $event ) = @_;
269
270
271  my $data = Wx::TextDataObject->new( 
272      Dumper $self->GetSelectedItems
273  );
274  my $source = Wx::DropSource->new( $self );
275  $source->SetData( $data );
276
277  $source->DoDragDrop( 1 );
278}
279
280sub GetSelectectItemsCount {
281    my ( $self ) = @_;   
282
283    scalar @{$self->GetSelectedItems};
284}
285
286sub GetSelectedItems {
287    my ( $self ) = @_;   
288
289  # find selected items
290  my $item = -1;
291  my $items = [];
292  while(1) {
293      $item = $self->GetNextItem(
294          $item,     
295          wxLIST_NEXT_ALL,
296          wxLIST_STATE_SELECTED
297      );
298      last if(-1 == $item);
299     
300      # item is selected   
301      push @$items, $item
302  }
303
304  $items;
305}
306
307sub GetAllItems {
308    my ( $self ) = @_;   
309
310  # find selected items
311  my $item = -1;
312  my $items = [];
313  while(1) {
314      $item = $self->GetNextItem(
315          $item,     
316          wxLIST_NEXT_ALL,
317          wxLIST_STATE_DONTCARE
318      );
319      last if(-1 == $item);
320     
321      # item is selected   
322      push @$items, $item
323  }
324 
325  $items;
326   
327}
328
329
330sub Refresh {
331    my ( $self ) = @_;   
332
333   $self->Freeze();
334   $self->DeleteAllItems;
335   eval {
336       my $y0 = 0;
337       my $x0 = 0;
338
339       my $x_spacing = $self->image_size + 2;
340       my $y_spacing = $self->image_size + 2;
341       my $ipr = floor(($self->GetClientSize->GetWidth - 0.20*$x_spacing)/$x_spacing)||1;
342       for( my $i = 0 ; $i < $self->wx_imagelist->GetImageCount ; $i++){
343            # call the method corresponding to the display mode
344            my $indx = $self->insert_items->{$self->imagelist->display_mode}->($self, $i);
345            if( 'Thumbnail' eq $self->imagelist->display_mode){
346                my $row = floor($i/$ipr);
347                my $col = $i%$ipr;
348                $self->SetItemPosition(
349                    $i,
350                    Wx::Point->new(
351                        $x0 + $x_spacing*$col,
352                        $y0 + $y_spacing*$row,
353                    ),
354                );
355            }
356       }   
357   };
358
359   map{
360       $self->SelectItem(
361           $_
362       );
363       
364       $self->EnsureVisible($_);
365   } @{$self->imagelist->image_selection||[]};
366
367   $self->Thaw();
368   $self->SetFocus;
369}
370
371sub SelectItem {
372    my ( $self, $index ) = @_;
373
374    $self->SetItemState(
375        $index,
376        wxLIST_STATE_SELECTED|wxLIST_STATE_FOCUSED,
377        wxLIST_STATE_SELECTED|wxLIST_STATE_FOCUSED
378    );   
379}
380
381sub OnEmpty {
382    my ( $self ) = @_;
383
384    $self->_OnEmptyLayout;
385
386}
387
388sub OnNotEmpty {
389    my ( $self ) = @_;
390
391    $self->{_empty_panel}->Show(0) if exists $self->{_empty_panel};
392}
393
394sub _OnEmptyLayout {
395
396    my( $self ) = @_;
397    $self->{_empty_panel}->Show(1);
398
399}
400
401sub add_button {
402    my ( $self ) = @_;
403
404    $self->{_add_bt};
405}
406
407sub InitEmptyMsg {
408
409    my ( $self ) = @_;
410    # add a panel
411    my $empty_panel = Wx::Panel->new($self, -1, wxDefaultPosition, wxDefaultSize);
412    my( $item0 ) = Wx::BoxSizer->new( wxVERTICAL );
413   
414    my $fb = Wx::Font->new( 12, wxSWISS, wxNORMAL, wxBOLD );
415    my $fn = Wx::Font->new( 12, wxSWISS, wxNORMAL, wxNORMAL );
416
417    my( $item1 ) = Wx::StaticText->new( $empty_panel, -1, gettext("Drag photos here to get started"), wxDefaultPosition, wxDefaultSize, 0 );
418    $item1->SetFont($fb);
419    $item0->AddWindow( $item1, 0, wxALIGN_CENTER|wxALL, 5 );
420
421    my( $item2 ) = Wx::BoxSizer->new( wxHORIZONTAL );
422   
423    my( $item3 ) = Wx::StaticText->new( $empty_panel, -1, gettext("Or click"), wxDefaultPosition, wxDefaultSize, 0 );
424    $item3->SetFont($fn);
425    $item2->AddWindow( $item3, 0, wxALIGN_CENTER|wxALL, 5 );
426
427    my $icon1 = Wx::Icon->new();
428    eval {
429        $icon1->LoadFile(
430            wxTheApp->resource_path('tb_add.png'),
431            wxBITMAP_TYPE_PNG
432        );
433    };
434    my $bmp = Wx::Bitmap->new( $icon1 );
435
436    my( $item4 ) = Wx::BitmapButton->new( $empty_panel, -1, $bmp, wxDefaultPosition, wxDefaultSize );
437    $item2->AddWindow( $item4, 0, wxALIGN_CENTER|wxALL, 5 );
438
439    my( $item5 ) = Wx::StaticText->new( $empty_panel, -1, gettext("to choose files on your computer"), wxDefaultPosition, wxDefaultSize, 0 );
440    $item5->SetFont($fn);
441    $item2->AddWindow( $item5, 0, wxALIGN_CENTER|wxALL, 5 );
442
443    $item0->Add( $item2, 0, wxALIGN_CENTER|wxALL, 0 );
444
445    $empty_panel->SetSizer( $item0 );
446    $item0->SetSizeHints( $empty_panel );
447
448    $self->{_empty_panel} = $empty_panel;
449    $self->{_add_bt} = $item4;
450    $self->{_empty_panel}->SetBackgroundColour(wxWHITE);
451
452    my ($wp, $hp) = $self->{_empty_panel}->GetSizeWH;
453
454    $self->SetMinSize( [20+$wp, -1 ]);
455
456}
457
458
459
4601;
461
Note: See TracBrowser for help on using the repository browser.