lib/Gapp/Gtk2/Model/List.pm

filter: Statementstmt 33.3% 1 / 3 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 1 / 1 Podpod n/a totaltotal 50.0% 2 / 4 CC 1 SCAR 2.6
CC
1
Cov
33%
BEGIN
1.3
CRAP 1.3
« blib/lib/Gapp.pm ↑ Summary lib/Gapp/Gtk2/Model/SimpleList.pm »
1 package Gapp::Gtk2::Model::List;
2 {
3 $Gapp::Gtk2::Model::List::VERSION = '0.60';
4 }
5  
6 0 ▶use Glib qw(TRUE FALSE);
Subroutine BEGIN: called CC=1 SCAR=2.6
7 use Gtk2;
8 use Carp;
9 use Data::Dumper;
10 use strict;
11 use warnings;
12  
13 use Glib::Object::Subclass
14 Glib::Object::,
15 interfaces => [ Gtk2::TreeModel:: ],
16 ;
17  
18 sub INIT_INSTANCE {
19 my $self = shift;
20 $self->{column_types} = [ 'Glib::Scalar' ];
21 $self->{rows} = [];
22 $self->{stamp} = sprintf '%d', rand (1<<31);
23 }
24  
25 sub FINALIZE_INSTANCE {
26 my $self = shift;
27 $self->{rows} = undef;
28 $self->{stamp} = undef;
29 $self->{column_types} = undef;
30 }
31  
32  
33 sub GET_FLAGS { [qw/list-only/] }
34 sub GET_N_COLUMNS { 0 }
35  
36 sub GET_COLUMN_TYPE {
37 my ($self, $index) = @_;
38 return 'Glib::Scalar';
39 }
40  
41 sub GET_ITER {
42 my ($self, $path) = @_;
43  
44 die "no path" unless $path;
45  
46 my @indices = $path->get_indices;
47 my $depth = $path->get_depth;
48  
49 # no children, this is a list
50 die "depth != 1" unless $depth == 1;
51  
52 my $n = $indices[0];
53 return undef if $n >= @{$self->{rows}} || $n < 0;
54  
55 my $record = $self->{rows}[$n];
56  
57 die "no record" unless $record;
58 die "bad record" unless $record->{pos} == $n;
59  
60 return [ $self->{stamp}, $n, $record, undef ];
61 }
62  
63  
64 sub GET_PATH {
65 my ($self, $iter) = @_;
66 die "no iter" unless $iter;
67  
68 my $record = $iter->[2];
69  
70 my $path = Gtk2::TreePath->new;
71 $path->append_index ($record->{pos});
72 return $path;
73 }
74  
75 sub GET_VALUE {
76 my ($self, $iter, $column) = @_;
77  
78 die "bad iter" unless $iter;
79  
80 my $record = $iter->[2];
81  
82 return undef unless $record;
83  
84 die "bad iter" if $record->{pos} >= @{$self->{rows}};
85  
86 return $record->{value}[$column];
87 }
88  
89 sub ITER_NEXT {
90 my ($self, $iter) = @_;
91  
92 return undef unless $iter && $iter->[2];
93  
94 my $record = $iter->[2];
95  
96 # Is this the last record in the list?
97 return undef if $record->{pos} >= @{ $self->{rows} };
98  
99 my $nextrecord = $self->{rows}[$record->{pos} + 1];
100  
101 return undef unless $nextrecord;
102 die "invalid record" unless $nextrecord->{pos} == ($record->{pos} + 1);
103  
104 return [ $self->{stamp}, $nextrecord->{pos}, $nextrecord, undef ];
105 }
106  
107 sub ITER_CHILDREN {
108 my ($self, $parent) = @_;
109 return undef if $parent; # this is a list, nodes have no children
110 return undef unless @{ $self->{rows} }; # No rows => no first row
111 return [ $self->{stamp}, 0, $self->{rows}[0] ]; # Set iter to first item in list
112 }
113  
114  
115 sub ITER_HAS_CHILD { FALSE }
116  
117  
118 sub ITER_N_CHILDREN {
119 my ($self, $iter) = @_;
120 # special case: if iter == NULL, return number of top-level rows
121 return scalar @{$self->{rows}} if ! $iter;
122  
123 return 0; # otherwise, this is easy again for a list
124 }
125  
126  
127 sub ITER_NTH_CHILD {
128 my ($self, $parent, $n) = @_;
129  
130 # a list has only top-level rows
131 return undef if $parent;
132  
133 # special case: if parent == NULL, set iter to n-th top-level row
134  
135 return undef if ! defined $self->{rows} || $n >= @{$self->{rows}};
136  
137 my $record = $self->{rows}[$n];
138  
139 die "no record" unless $record;
140 die "bad record" unless $record->{pos} == $n;
141  
142 return [ $self->{stamp}, $n, $record ];
143 }
144  
145  
146 sub ITER_PARENT { FALSE }
147  
148 sub append {
149 my ( $self, %data ) = @_;
150
151 my @record_data;
152 for my $pos ( keys %data ) {
153 $record_data[$pos] = $data{$pos};
154 }
155
156 my $record = { value => \@record_data };
157
158 push @{ $self->{rows} }, $record;
159 $record->{pos} = @{$self->{rows}} - 1;
160
161 my $path = Gtk2::TreePath->new;
162 $path->append_index( $record->{pos} );
163
164 my $iter = $self->get_iter( $path );
165 $self->row_inserted( $path, $iter );
166 return $self->get_iter( $path );
167 }
168  
169 sub append_record {
170 my ( $self, @data ) = @_;
171
172 my $record = { value => \@data };
173
174 push @{ $self->{rows} }, $record;
175 $record->{pos} = @{$self->{rows}} - 1;
176
177 my $path = Gtk2::TreePath->new;
178 $path->append_index( $record->{pos} );
179
180 my $iter = $self->get_iter( $path );
181 $self->row_inserted( $path, $iter );
182 return $self->get_iter( $path );
183 }
184  
185  
186 sub set {
187 my ( $self, $treeiter, %data ) = @_;
188  
189 my $iter = $treeiter->to_arrayref($self->{stamp});
190
191 my $record = $iter->[2];
192
193 for my $pos ( keys %data ) {
194 $record->{value}[$pos] = $data{$pos};
195 }
196  
197 $self->row_changed ($self->get_path ($treeiter), $treeiter);
198 }
199  
200 sub clear {
201 my ( $self ) = @_;
202
203 while ( @{$self->{rows}} ) {
204 my $record = pop @{ $self->{rows} };
205
206 my $path = Gtk2::TreePath->new;
207 $path->append_index( $record->{pos} );
208
209 $self->row_deleted ( $path );
210 }
211  
212 }
213  
214  
215 sub remove {
216 my ( $self, $treeiter ) = @_;
217
218 my $iter = $treeiter->to_arrayref($self->{stamp});
219 my $n = $iter->[1];
220
221 # splice the record from the rows
222 my ( $record ) = splice @{$self->{rows}}, $n, 1;
223
224 # update the position field of affected records
225 for my $i ( $n..$#{$self->{rows}} ) {
226 $self->{rows}[$i]{pos} = $i;
227 }
228
229 # find the path and notify the model of the row removal
230 my $path = Gtk2::TreePath->new;
231 $path->append_index( $record->{pos} );
232 $self->row_deleted ( $path );
233
234 # update the iter to point new row
235 $iter->[0] = $self->{stamp};
236 $iter->[2] = $self->{rows}[$n];
237
238 return Gtk2::TreeIter->new_from_arrayref( $iter );
239 }
240  
241  
242  
243  
244 1;
245  
246  
247  
248 __END__
« blib/lib/Gapp.pm ↑ Summary lib/Gapp/Gtk2/Model/SimpleList.pm »