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

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

Feature 1478 added : ability to set properties for a photo selection.

  • Property svn:eol-style set to LF
File size: 10.8 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 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             wxTheApp
49         /;
50use Wx::Event qw/
51                    EVT_LIST_BEGIN_DRAG
52                    EVT_LIST_ITEM_SELECTED
53                    EVT_SIZE
54                /;
55         
56use base qw/Wx::ListCtrl Class::Accessor::Fast/;
57__PACKAGE__->mk_accessors( 
58    qw/
59          prevItemCount
60          imagenames
61          imagelist
62          wx_imagelist
63          image_size
64          arrange_items
65          styles
66          insert_items
67          initialize
68          columns
69          change_display_mode
70          wx_img
71          item_refresh
72      / 
73);
74use Data::Dumper;
75
76
77sub new {
78    my( $class, $params ) = @_;
79
80 
81    my( $self ) = $class->SUPER::new(
82        $params->{parentwnd},
83        -1,
84        wxDefaultPosition,
85        [ 180, -1],
86    );
87   
88    $self->wx_img(
89        Wx::ImageList->new( 16, 16, 1 )
90    );
91    $self->wx_img->Add(
92        Wx::Bitmap->new( wxTheApp->resource_path('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
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        $event->Skip;
139        }
140    );
141   
142    $self->initialize(
143        {
144
145            'Thumbnail' => sub {  my ( $self ) = @_;
146                                $self->Freeze;
147                                $self->ClearAll;
148                                $self->SetSingleStyle(wxLC_ICON);
149                                $self->SetSingleStyle(wxLC_EDIT_LABELS, 0);
150                                $self->SetImageList( $self->wx_imagelist, wxIMAGE_LIST_NORMAL ) ;
151                                $self->Thaw;
152                     },           
153            'Thumbnail and caption' => sub {  my ( $self ) = @_;
154                                $self->Freeze;
155                                $self->ClearAll;
156                                $self->SetSingleStyle(wxLC_ICON);
157                                $self->SetSingleStyle(wxLC_EDIT_LABELS);
158                                $self->SetImageList( $self->wx_imagelist, wxIMAGE_LIST_NORMAL ) ;
159                                $self->Thaw;
160                     },           
161             'Property list' => sub { my ( $self ) = @_;
162                                my $i=0;
163                               
164                                $self->Freeze;
165                                $self->ClearAll;
166                                $self->SetSingleStyle(wxLC_REPORT);
167                                $self->SetSingleStyle(wxLC_EDIT_LABELS);
168                                $self->SetImageList( $self->wx_img, wxIMAGE_LIST_SMALL ) ;
169                                map    {
170                                    $self->InsertColumn($i, $_->{label});
171                                    $i++;
172                                }@{$self->columns};                               
173                                $self->Thaw;
174                            },
175        }
176    );
177
178    $self->item_refresh(
179        {
180            'Thumbnail' => sub {},
181            'Thumbnail and caption' => sub { my ( $self, $index ) = @_;
182                                        my $image = $self->imagelist->GetImage($index);
183                                        $self->SetItem(
184                                            $index,
185                                            0,
186                                            $image->site_name,
187                                        ) if defined $image;
188                                    },
189            'Property list' => sub { my ( $self, $index ) = @_;
190                                my $col = 0;
191                                my $image = $self->imagelist->GetImage($index);
192                                map{
193                                    $self->SetItem(
194                                        $index,
195                                        $col,
196                                        $image->$_
197                                    ) if defined $image;
198                                    $self->SetColumnWidth($col, -1);
199                                    $col++;
200                                }
201                                qw/site_name site_comment site_author file create_date/;
202                            },
203        }
204    );
205   
206    $self->insert_items(
207        {
208            'Thumbnail' => sub { my ( $self, $index ) = @_; $self->InsertImageItem($index, $index);},
209            'Thumbnail and caption' => sub { my ( $self, $index ) = @_;
210                                           $self->InsertImageStringItem(
211                                               $index,
212                                               $self->imagelist->GetImage($index)->site_name,
213                                               $index,
214                                           );
215                                     },
216            'Property list' => sub { my ( $self, $index ) = @_;
217                                       
218                                $self->InsertImageStringItem(
219                                    $index,
220                                    "",
221                                    0
222                                );
223                                $self->ItemRefresh($index);
224
225                            },
226        }
227    );
228
229   
230    $self->initialize->{$self->imagelist->display_mode}->($self) if exists $self->initialize->{$self->imagelist->display_mode};
231
232    $self;
233}
234
235sub ItemRefresh {
236    my ( $self, $index ) = @_;
237   
238    $self->item_refresh->{$self->imagelist->display_mode}->($self, $index) if exists $self->item_refresh->{$self->imagelist->display_mode};
239}
240
241sub OnBeginDrag {
242  my( $self, $event ) = @_;
243
244
245  my $data = Wx::TextDataObject->new( 
246      Dumper $self->GetSelectedItems
247  );
248  my $source = Wx::DropSource->new( $self );
249  $source->SetData( $data );
250
251  $source->DoDragDrop( 1 );
252}
253
254sub GetSelectectItemsCount {
255    my ( $self ) = @_;   
256
257    scalar @{$self->GetSelectedItems};
258}
259
260sub GetSelectedItems {
261    my ( $self ) = @_;   
262
263  # find selected items
264  my $item = -1;
265  my $items = [];
266  while(1) {
267      $item = $self->GetNextItem(
268          $item,     
269          wxLIST_NEXT_ALL,
270          wxLIST_STATE_SELECTED
271      );
272      last if(-1 == $item);
273     
274      # item is selected   
275      push @$items, $item
276  }
277
278  $items;
279}
280
281sub GetAllItems {
282    my ( $self ) = @_;   
283
284  # find selected items
285  my $item = -1;
286  my $items = [];
287  while(1) {
288      $item = $self->GetNextItem(
289          $item,     
290          wxLIST_NEXT_ALL,
291          wxLIST_STATE_DONTCARE
292      );
293      last if(-1 == $item);
294     
295      # item is selected   
296      push @$items, $item
297  }
298 
299  $items;
300   
301}
302
303
304sub Refresh {
305    my ( $self ) = @_;   
306
307   $self->Freeze();
308   $self->DeleteAllItems;
309   eval {
310       my $y0 = 0;
311       my $x0 = 0;
312
313       my $x_spacing = $self->image_size + 2;
314       my $y_spacing = $self->image_size + 2;
315       my $ipr = floor(($self->GetClientSize->GetWidth - 0.20*$x_spacing)/$x_spacing)||1;
316       for( my $i = 0 ; $i < $self->wx_imagelist->GetImageCount ; $i++){
317            # call the method corresponding to the display mode
318            my $indx = $self->insert_items->{$self->imagelist->display_mode}->($self, $i);
319            if( 'Thumbnail' eq $self->imagelist->display_mode){
320                my $row = floor($i/$ipr);
321                my $col = $i%$ipr;
322                $self->SetItemPosition(
323                    $i,
324                    Wx::Point->new(
325                        $x0 + $x_spacing*$col,
326                        $y0 + $y_spacing*$row,
327                    ),
328                );
329            }
330       }   
331   };
332
333   map{
334       $self->SelectItem(
335           $_
336       );
337       
338       $self->EnsureVisible($_);
339   } @{$self->imagelist->image_selection||[]};
340
341   $self->Thaw();
342   $self->SetFocus;
343}
344
345sub SelectItem {
346    my ( $self, $index ) = @_;
347
348    $self->SetItemState(
349        $index,
350        wxLIST_STATE_SELECTED|wxLIST_STATE_FOCUSED,
351        wxLIST_STATE_SELECTED|wxLIST_STATE_FOCUSED
352    );   
353}
354
3551;
356
Note: See TracBrowser for help on using the repository browser.