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

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

Feature 1332 added : new list and thumbnail display mode for photo selection.

  • Property svn:eol-style set to LF
File size: 9.7 KB
Line 
1# +-----------------------------------------------------------------------+
2# | pLoader - a Perl photo uploader for Piwigo                            |
3# +-----------------------------------------------------------------------+
4# | Copyright(C) 2008      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 qw/
26             wxDefaultPosition
27             wxDefaultSize
28             wxLC_LIST
29             wxNO_BORDER
30             wxLC_EDIT_LABELS
31             wxLC_ICON
32                         wxLC_REPORT
33             wxLC_SMALL_ICON
34             wxIMAGE_LIST_NORMAL
35             wxBITMAP_TYPE_JPEG
36             wxBITMAP_TYPE_PNG
37             wxBLACK
38             wxLC_VIRTUAL
39             wxLC_REPORT
40                         wxLC_AUTOARRANGE
41             wxBORDER_THEME
42             wxLIST_HITTEST_ONITEM
43             wxLIST_NEXT_ALL
44             wxLIST_STATE_SELECTED
45             wxLIST_STATE_DONTCARE
46             wxLIST_STATE_FOCUSED
47                         wxIMAGE_LIST_SMALL
48         /;
49use Wx::Event qw/
50                    EVT_LIST_BEGIN_DRAG
51                    EVT_LIST_ITEM_SELECTED
52                                        EVT_SIZE
53                /;
54         
55use base qw/Wx::ListCtrl Class::Accessor::Fast/;
56__PACKAGE__->mk_accessors( 
57    qw/
58          prevItemCount
59          imagenames
60          imagelist
61          wx_imagelist
62                  image_size
63                  arrange_items
64                  styles
65                  insert_items
66                  initialize
67                  columns
68                  change_display_mode
69                  wx_img
70                  item_refresh
71      / 
72);
73use Data::Dumper;
74
75
76sub new {
77    my( $class, $params ) = @_;
78
79 
80    my( $self ) = $class->SUPER::new(
81        $params->{parentwnd},
82        -1,
83        wxDefaultPosition,
84        wxDefaultSize,
85                wxNO_BORDER
86    );
87       
88        $self->wx_img(
89        Wx::ImageList->new( 16, 16, 1 )
90        );
91        $self->wx_img->Add(
92        Wx::Bitmap->new( '../res/image.png', wxBITMAP_TYPE_PNG )       
93        );
94
95    $self->imagelist(
96        $params->{imagelist}
97    );
98    # bitmap container for photo selection
99    $self->wx_imagelist(
100        $params->{wx_imagelist}||$self->imagelist->wx_thumb_imglist
101    );
102
103    $self->image_size(
104        $params->{image_size}
105    );
106
107        $self->columns(
108            $params->{columns}
109        );
110        print Dumper $self->columns;
111    $self->prevItemCount(-1);
112
113    EVT_LIST_BEGIN_DRAG( $self, $self, \&OnBeginDrag);
114
115    EVT_SIZE($self, sub {
116            my ( $this, $event ) = @_;
117   
118                        $this->arrange_items(1);
119            $event->Skip();
120        }
121    );
122       
123        Wx::Event::EVT_IDLE(
124            $self,
125                sub {
126                    my ($self, $event)=@_;
127
128                        if($self->arrange_items){
129                            $self->Refresh; 
130                            $self->arrange_items(0);
131                        }
132                        if($self->change_display_mode){
133                            my $dm = $self->imagelist->display_mode;
134                    $self->initialize->{$dm}->($self) if exists $self->initialize->{$dm};
135                            $self->Refresh; 
136                $self->change_display_mode(0);
137                        }
138                }
139        );
140       
141        $self->initialize(
142            {
143
144            'Thumbnail' => sub {  my ( $self ) = @_;
145                                                                $self->Freeze;
146                                                                $self->ClearAll;
147                                                                $self->SetSingleStyle(wxLC_ICON);
148                                                                $self->SetSingleStyle(wxLC_EDIT_LABELS, 0);
149                                $self->SetImageList( $self->wx_imagelist, wxIMAGE_LIST_NORMAL ) ;
150                                                                $self->Thaw;
151                     },                 
152            'Thumbnail and caption' => sub {  my ( $self ) = @_;
153                                                                $self->Freeze;
154                                                                $self->ClearAll;
155                                                                $self->SetSingleStyle(wxLC_ICON);
156                                                                $self->SetSingleStyle(wxLC_EDIT_LABELS);
157                                $self->SetImageList( $self->wx_imagelist, wxIMAGE_LIST_NORMAL ) ;
158                                                                $self->Thaw;
159                     },                 
160                'Property list' => sub { my ( $self ) = @_;
161                                            my $i=0;
162                                                               
163                                                                $self->Freeze;
164                                                                $self->ClearAll;
165                                                                $self->SetSingleStyle(wxLC_REPORT);
166                                                                $self->SetSingleStyle(wxLC_EDIT_LABELS);
167                                $self->SetImageList( $self->wx_img, wxIMAGE_LIST_SMALL ) ;
168                                map     {
169                                                                    $self->InsertColumn($i, $_->{label});
170                                                                        $i++;
171                                }@{$self->columns};                                                             
172                                                                $self->Thaw;
173                                        },
174                }
175        );
176
177        $self->item_refresh(
178            {
179                    'Thumbnail' => sub {},
180                    'Thumbnail and caption' => sub { my ( $self, $index ) = @_;
181                                        my $image = $self->imagelist->GetImage($index);
182                                        $self->SetItem(
183                                                    $index,
184                                                        0,
185                                                        $image->site_name,
186                                        ) if defined $image;
187                                                },
188            'Property list' => sub { my ( $self, $index ) = @_;
189                                my $col = 0;
190                                my $image = $self->imagelist->GetImage($index);
191                                map{
192                                    $self->SetItem(
193                                        $index,
194                                        $col,
195                                        $image->$_
196                                    ) if defined $image;
197                                    $col++;
198                                }
199                                qw/site_name site_comment site_author file create_date/;
200                            },
201                }
202        );
203       
204        $self->insert_items(
205            {
206                    'Thumbnail' => sub { my ( $self, $index ) = @_; $self->InsertImageItem($index, $index);},
207                    'Thumbnail and caption' => sub { my ( $self, $index ) = @_;
208                                                       $self->InsertImageStringItem(
209                                               $index,
210                                               $self->imagelist->GetImage($index)->site_name,
211                                               $index,
212                                           );
213                                     },
214            'Property list' => sub { my ( $self, $index ) = @_;
215                                                   
216                                                            $self->InsertImageStringItem(
217                                    $index,
218                                    "",
219                                                                        0
220                                                                );
221                                                                $self->ItemRefresh($index);
222
223                                        },
224                }
225        );
226
227       
228        $self->initialize->{$self->imagelist->display_mode}->($self) if exists $self->initialize->{$self->imagelist->display_mode};
229
230    $self;
231}
232
233sub ItemRefresh {
234    my ( $self, $index ) = @_;
235       
236        $self->item_refresh->{$self->imagelist->display_mode}->($self, $index) if exists $self->item_refresh->{$self->imagelist->display_mode};
237}
238
239sub OnBeginDrag {
240  my( $self, $event ) = @_;
241
242
243  my $data = Wx::TextDataObject->new( 
244      Dumper $self->GetSelectedItems
245  );
246  my $source = Wx::DropSource->new( $self );
247  $source->SetData( $data );
248
249  $source->DoDragDrop( 1 );
250}
251
252
253sub GetSelectedItems {
254    my ( $self ) = @_; 
255
256  # find selected items
257  my $item = -1;
258  my $items = [];
259  while(1) {
260      $item = $self->GetNextItem(
261          $item,     
262          wxLIST_NEXT_ALL,
263          wxLIST_STATE_SELECTED
264      );
265      last if(-1 == $item);
266     
267      # item is selected       
268      push @$items, $item
269  }
270 
271  $items;
272}
273
274sub GetAllItems {
275    my ( $self ) = @_; 
276
277  # find selected items
278  my $item = -1;
279  my $items = [];
280  while(1) {
281      $item = $self->GetNextItem(
282          $item,     
283          wxLIST_NEXT_ALL,
284          wxLIST_STATE_DONTCARE
285      );
286      last if(-1 == $item);
287     
288      # item is selected       
289      push @$items, $item
290  }
291 
292  $items;
293       
294}
295
296
297sub Refresh {
298    my ( $self ) = @_; 
299
300   $self->Freeze();
301   $self->DeleteAllItems;
302   eval {
303       my $y0 = 0;
304       my $x0 = 0;
305
306       my $x_spacing = $self->image_size + 2;
307       my $y_spacing = $self->image_size + 2;
308       my $ipr = floor(($self->GetClientSize->GetWidth - 0.20*$x_spacing)/$x_spacing)||1;
309       for( my $i = 0 ; $i < $self->imagelist->count ; $i++){
310                # call the method corresponding to the display mode
311            my $indx = $self->insert_items->{$self->imagelist->display_mode}->($self, $i);
312                        if( 'Thumbnail' eq $self->imagelist->display_mode){
313                my $row = floor($i/$ipr);
314                my $col = $i%$ipr;
315                $self->SetItemPosition(
316                    $i,
317                    Wx::Point->new(
318                        $x0 + $x_spacing*$col,
319                        $y0 + $y_spacing*$row,
320                    ),
321                );
322                        }
323       }   
324   };
325
326   map{
327       $self->SelectItem(
328           $_
329       );
330           
331           $self->EnsureVisible($_);
332   } @{$self->imagelist->image_selection||[]};
333
334   $self->Thaw();
335   $self->SetFocus;
336}
337
338sub SelectItem {
339    my ( $self, $index ) = @_;
340
341    $self->SetItemState(
342        $index,
343        wxLIST_STATE_SELECTED|wxLIST_STATE_FOCUSED,
344        wxLIST_STATE_SELECTED|wxLIST_STATE_FOCUSED
345    ); 
346}
347
3481;
349
Note: See TracBrowser for help on using the repository browser.