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

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

Remove debug code.

  • Property svn:eol-style set to LF
File size: 10.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
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                                    $col++;
199                                }
200                                qw/site_name site_comment site_author file create_date/;
201                            },
202        }
203    );
204   
205    $self->insert_items(
206        {
207            'Thumbnail' => sub { my ( $self, $index ) = @_; $self->InsertImageItem($index, $index);},
208            'Thumbnail and caption' => sub { my ( $self, $index ) = @_;
209                                           $self->InsertImageStringItem(
210                                               $index,
211                                               $self->imagelist->GetImage($index)->site_name,
212                                               $index,
213                                           );
214                                     },
215            'Property list' => sub { my ( $self, $index ) = @_;
216                                       
217                                $self->InsertImageStringItem(
218                                    $index,
219                                    "",
220                                    0
221                                );
222                                $self->ItemRefresh($index);
223
224                            },
225        }
226    );
227
228   
229    $self->initialize->{$self->imagelist->display_mode}->($self) if exists $self->initialize->{$self->imagelist->display_mode};
230
231    $self;
232}
233
234sub ItemRefresh {
235    my ( $self, $index ) = @_;
236   
237    $self->item_refresh->{$self->imagelist->display_mode}->($self, $index) if exists $self->item_refresh->{$self->imagelist->display_mode};
238}
239
240sub OnBeginDrag {
241  my( $self, $event ) = @_;
242
243
244  my $data = Wx::TextDataObject->new( 
245      Dumper $self->GetSelectedItems
246  );
247  my $source = Wx::DropSource->new( $self );
248  $source->SetData( $data );
249
250  $source->DoDragDrop( 1 );
251}
252
253
254sub GetSelectedItems {
255    my ( $self ) = @_;   
256
257  # find selected items
258  my $item = -1;
259  my $items = [];
260  while(1) {
261      $item = $self->GetNextItem(
262          $item,     
263          wxLIST_NEXT_ALL,
264          wxLIST_STATE_SELECTED
265      );
266      last if(-1 == $item);
267     
268      # item is selected   
269      push @$items, $item
270  }
271 
272  $items;
273}
274
275sub GetAllItems {
276    my ( $self ) = @_;   
277
278  # find selected items
279  my $item = -1;
280  my $items = [];
281  while(1) {
282      $item = $self->GetNextItem(
283          $item,     
284          wxLIST_NEXT_ALL,
285          wxLIST_STATE_DONTCARE
286      );
287      last if(-1 == $item);
288     
289      # item is selected   
290      push @$items, $item
291  }
292 
293  $items;
294   
295}
296
297
298sub Refresh {
299    my ( $self ) = @_;   
300
301   $self->Freeze();
302   $self->DeleteAllItems;
303   eval {
304       my $y0 = 0;
305       my $x0 = 0;
306
307       my $x_spacing = $self->image_size + 2;
308       my $y_spacing = $self->image_size + 2;
309       my $ipr = floor(($self->GetClientSize->GetWidth - 0.20*$x_spacing)/$x_spacing)||1;
310       for( my $i = 0 ; $i < $self->wx_imagelist->GetImageCount ; $i++){
311            # call the method corresponding to the display mode
312            my $indx = $self->insert_items->{$self->imagelist->display_mode}->($self, $i);
313            if( 'Thumbnail' eq $self->imagelist->display_mode){
314                my $row = floor($i/$ipr);
315                my $col = $i%$ipr;
316                $self->SetItemPosition(
317                    $i,
318                    Wx::Point->new(
319                        $x0 + $x_spacing*$col,
320                        $y0 + $y_spacing*$row,
321                    ),
322                );
323            }
324       }   
325   };
326
327   map{
328       $self->SelectItem(
329           $_
330       );
331       
332       $self->EnsureVisible($_);
333   } @{$self->imagelist->image_selection||[]};
334
335   $self->Thaw();
336   $self->SetFocus;
337}
338
339sub SelectItem {
340    my ( $self, $index ) = @_;
341
342    $self->SetItemState(
343        $index,
344        wxLIST_STATE_SELECTED|wxLIST_STATE_FOCUSED,
345        wxLIST_STATE_SELECTED|wxLIST_STATE_FOCUSED
346    );   
347}
348
3491;
350
Note: See TracBrowser for help on using the repository browser.