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

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

Feature 1496 added : display a message in the photo selection when it is empty.

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