source: extensions/pLoader/trunk/src/Uploader/GUI/wxChoiceGridPanel.pm @ 3472

Last change on this file since 3472 was 3472, checked in by ronosman, 15 years ago

Feature 993 added : set Piwigo tags. Only existing tags are supported.

  • Property svn:eol-style set to LF
File size: 5.2 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::wxChoiceGridPanel;
21use strict;
22use Wx qw/
23             wxDefaultSize
24             wxDefaultPosition
25         /;
26
27use base qw/Wx::Grid Uploader::Object2/;
28
29use Wx::Event qw/
30   EVT_GRID_CMD_CELL_CHANGE
31   EVT_GRID_CELL_LEFT_CLICK
32/;
33use Data::Dumper;
34
35__PACKAGE__->mk_accessors(     
36    qw/
37        frame_callbacks
38        renderer
39        editor
40        choices
41        selection
42        id_lookup
43        row_lookup
44      /
45);
46
47
48
49sub new {
50    my ($this, $params) = @_;
51    #on recupere le nom de la classe en fonction du type d'appel de la méthode.
52    my $class = ref($this) || $this;
53
54
55    my $self = $class->SUPER::new( $params->{parentwnd}, -1, wxDefaultPosition, wxDefaultSize );
56
57    $self->id_lookup(
58        {}
59    );
60
61    $self->row_lookup(
62        {}
63    );
64
65    $self->choices(
66        $params->{choices}
67    );
68
69    $self->selection(
70        $params->{selection}
71    );
72
73   
74    # a frame callback for each property
75    $self->frame_callbacks(
76        $params->{frame_callbacks}
77    );
78   
79    $self->Initialize;
80     
81    EVT_GRID_CMD_CELL_CHANGE( $self, $self, \&OnCellChanged );
82    EVT_GRID_CELL_LEFT_CLICK( $self, \&OnCellLeftClick );
83    $self;   
84}
85
86sub Initialize {
87    my ( $self ) = @_; 
88
89
90  $self->CreateGrid( scalar @{$self->choices}, 2 );
91 
92
93  $self->SetRowLabelSize(0);
94  $self->SetColLabelSize(0);
95  $self->SetColSize( 0, 200 );
96  $self->SetColSize( 1, 20 );
97 
98  $self->_init_choices;
99
100}
101
102sub Refresh {
103    my ( $self ) = @_;
104   
105    $self->ClearGrid;
106    $self->id_lookup({});
107    $self->row_lookup({});
108
109    my $row = 0;
110    map {
111        $self->SetCellValue(
112            $row,
113            0,
114            $_->{name},
115        ) if defined $_;       
116
117        $self->id_lookup->{$row} = $_->{id};
118        $self->row_lookup->{$_->{id}} = $row;
119        $row++;
120    }@{$self->choices};
121
122    $self->SetSelection;
123       
124}
125
126sub _init_choices {
127    my ( $self ) = @_;
128
129    my $row = 0;
130    #print Dumper $self->properties;
131    map {
132        eval {
133            $self->SetReadOnly(
134                $row,
135                0,
136            );
137
138            $self->SetCellValue(
139                $row,
140                0,
141                $_->{name},
142            ) if defined $_;   
143
144            $self->SetCellEditor(
145                $row,
146                1,
147                &{
148                    sub { Wx::GridCellBoolEditor->new },
149                },         
150            );
151
152            $self->SetCellRenderer(
153                $row,
154                1,
155                &{
156                    sub { Wx::GridCellBoolRenderer->new }
157                },         
158            );
159
160        };
161        $self->id_lookup->{$row} = $_->{id};
162        $self->row_lookup->{$_->{id}} = $row;
163        $row++;
164    }@{$self->choices};
165
166
167}
168
169sub OnCellChanged {
170    my ( $self, $event ) = @_;
171
172    $self->selection->(
173        $self->GetSelection
174    );   
175}
176
177sub OnCellLeftClick {
178    my ( $self, $event ) = @_;
179
180    my ( $row, $col ) = ( $event->GetRow, $event->GetCol );
181   
182    # only col 0
183    if(!$event->GetCol){
184        $self->SetCellValue(
185            $event->GetRow,
186            1,
187            !$self->GetCellValue(
188                $event->GetRow,
189                1,
190            ),
191        );
192    }
193
194    $self->selection->(
195        $self->GetSelection
196    );   
197    $event->Skip;
198}
199
200sub SetSelection {
201    my ( $self ) = @_;
202
203    my $selection = $self->selection->()||[];
204
205    map {
206        $self->SetCellValue(
207            $self->row_lookup->{$_},
208            1,
209            1,
210        ) if exists $self->row_lookup->{$_} ;   
211    }
212    @{$selection};     
213}
214
215
216
217sub GetSelection {
218    my ( $self ) = @_;
219
220    my $row = 0;
221    my $selection = [];
222    map {
223        my $selected = $self->GetCellValue(
224            $row,
225            1,
226        );
227           
228        if($selected){
229            push @$selection, $self->id_lookup->{$row}; 
230        }
231        $row++;
232    }@{$self->choices};
233   
234    $selection; 
235}
2361;
Note: See TracBrowser for help on using the repository browser.