App/Chart/Gtk2/Ex/ListModelPos.pm

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« App/Chart/Gtk2/Diagnostics.pm ↑ Summary App/Chart/Suffix/ATH.pm »
1 # Copyright 2008, 2009, 2010, 2011 Kevin Ryde
2  
3 # This file is part of Chart.
4 #
5 # Chart is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 3, or (at your option) any later
8 # version.
9 #
10 # Chart is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Chart. If not, see <http://www.gnu.org/licenses/>.
17  
18 package App::Chart::Gtk2::Ex::ListModelPos;
19 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use List::Util qw(min max);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 0 ▶use Gtk2;
Subroutine BEGIN: called CC=1 SCAR=2.6
25 use POSIX ();
26  
27 use App::Chart::Glib::Ex::MoreUtils;
28 use Glib::Ex::SignalIds;
29  
30 use constant DEBUG => 0;
31  
32 BEGIN {
33 Glib::Type->register_enum ('App::Chart::Gtk2::Ex::ListModelPos::Type',
34 at => 0,
35 before => 1,
36 after => 2,
37 start => 3,
38 end => 4);
39 }
40 use Glib::Object::Subclass
41 'Glib::Object',
42 properties => [ Glib::ParamSpec->object
43 ('model',
44 'model',
45 'TreeModel to operate on.',
46 'Gtk2::TreeModel',
47 Glib::G_PARAM_READWRITE),
48  
49 Glib::ParamSpec->int
50 ('index',
51 'index',
52 'Current position as an integer.',
53 0, POSIX::INT_MAX(),
54 0,
55 Glib::G_PARAM_READWRITE),
56  
57 Glib::ParamSpec->enum
58 ('type',
59 'type',
60 'Position type.',
61 'App::Chart::Gtk2::Ex::ListModelPos::Type',
62 'start',
63 Glib::G_PARAM_READWRITE),
64  
65 Glib::ParamSpec->int
66 ('key-column',
67 'key-column',
68 'Column number in the model which is a unique key to identify a row.',
69 -1, POSIX::INT_MAX(),
70 -1,
71 Glib::G_PARAM_READWRITE),
72  
73 Glib::ParamSpec->scalar
74 ('key-func',
75 'key-func',
76 'Function returning a key (a string) uniquely identifying a row.',
77 Glib::G_PARAM_READWRITE),
78  
79 Glib::ParamSpec->scalar
80 ('key-equal',
81 'key-equal',
82 'Function testing equality of key strings.',
83 Glib::G_PARAM_READWRITE),
84  
85 ];
86  
87 sub INIT_INSTANCE {
88 my ($self) = @_;
89 $self->{'type'} = 'start'; # defaults
90 $self->{'index'} = 0;
91 $self->{'key_column'} = -1;
92 }
93  
94 sub SET_PROPERTY {
95 my ($self, $pspec, $newval) = @_;
96 my $pname = $pspec->get_name;
97 $self->{$pname} = $newval;
98  
99 if ($pname eq 'model') {
100 my $model = $newval;
101 my $ref_weak_self = App::Chart::Glib::Ex::MoreUtils::ref_weak($self);
102  
103 $self->{'ids'} = $model && Glib::Ex::SignalIds->new
104 ($model,
105 $model->signal_connect (row_changed => \&_do_row_changed,
106 $ref_weak_self),
107 $model->signal_connect (row_deleted => \&_do_row_deleted,
108 $ref_weak_self),
109 $model->signal_connect (row_inserted => \&_do_row_inserted,
110 $ref_weak_self),
111 $model->signal_connect (rows_reordered => \&_do_rows_reordered,
112 $ref_weak_self));
113 }
114 if ($pname eq 'type' || $pname eq 'index'
115 || $pname eq 'key_column' || $pname eq 'key_func') {
116 if ($self->{'type'} eq 'at') {
117 _at_key ($self, $self->{'index'}); # record key
118 }
119 }
120 }
121  
122 # the types which have 'index' referring to a particular row
123 my %_on_row_offsets = (before => 0,
124 at => 1,
125 after => 1);
126  
127 sub _do_row_changed {
128 my ($model, $chg_path, $chg_iter, $ref_weak_self) = @_;
129 my $self = $$ref_weak_self || return;
130 if (DEBUG) { print "ListModelPos changed '", $chg_path->to_string, "'\n"; }
131  
132 $chg_path->get_depth == 1 or return;
133 my ($chg_index) = $chg_path->get_indices;
134  
135 # when current row changes remember its possibly changed key
136 if ($chg_index == $self->{'index'} && $self->{'type'} eq 'at') {
137 my $key = _get_key ($self, $chg_index, $chg_iter);
138 if (defined $key) { $self->{'at_key'} = $key; }
139 return;
140 }
141  
142 # when seeking 'want_key' see if newly changed row matches
143 if (exists $self->{'want_key'}) {
144 my $this_key = _get_key ($self, $chg_index, $chg_iter);
145 if (_match_key ($self, $self->{'want_key'}, $this_key)) {
146 _at_key ($self, $chg_index, $this_key);
147 }
148 }
149 }
150  
151 sub _do_row_inserted {
152 my ($model, $ins_path, $ins_iter, $ref_weak_self) = @_;
153 my $self = $$ref_weak_self || return;
154 if (DEBUG) { print "ListModelPos inserted ", $ins_path->to_string, "\n"; }
155  
156 $ins_path->get_depth == 1 or return;
157 my ($ins_index) = $ins_path->get_indices;
158  
159 # when seeking 'want_key' see if new row matches
160 if (exists $self->{'want_key'}) {
161 my $this_key = _get_key ($self, $ins_index, $ins_iter);
162 if (_match_key ($self, $self->{'want_key'}, $this_key)) {
163 _at_key ($self, $ins_index, $this_key);
164 return;
165 }
166 }
167  
168 # when "at", "before" or "after", adjust index if we're after the insertion
169 my $offset = $_on_row_offsets{$self->{'type'}};
170 if (defined $offset) {
171 if ($ins_index < $self->{'index'} + $offset) {
172 $self->{'index'} ++;
173 }
174 }
175 }
176  
177 sub _do_row_deleted {
178 my ($model, $del_path, $ref_weak_self) = @_;
179 my $self = $$ref_weak_self || return;
180 if (DEBUG) { print "ListModelPos deleted ", $del_path->to_string, "\n"; }
181  
182 $del_path->get_depth == 1 or return;
183 my ($del_index) = $del_path->get_indices;
184  
185 my $type = $self->{'type'};
186  
187 # when current row deleted, become "before" what was the following row
188 if ($type eq 'at' && $del_index == $self->{'index'}) {
189 $self->{'type'} = 'before';
190 $self->notify ('type');
191 if (exists $self->{'at_key'}) {
192 $self->{'want_key'} = delete $self->{'at_key'};
193 }
194 return;
195 }
196  
197 # when "at", "before" or "after", adjust index if we're after the deletion
198 my $offset = $_on_row_offsets{$type};
199 if (defined $offset) {
200 if ($del_index < $self->{'index'} + $offset) {
201 if (-- $self->{'index'} < 0) {
202 $self->{'type'} = 'before';
203 $self->{'index'} = 0;
204 $self->notify ('type');
205 $self->notify ('index');
206 }
207 }
208 }
209 }
210  
211 sub _do_rows_reordered {
212 my ($model, $reorder_path, $reorder_iter, $aref, $ref_weak_self) = @_;
213 my $self = $$ref_weak_self || return;
214 if (DEBUG) { print "ListModelPos reorder ", join(' ',@$aref), "\n"; }
215  
216 $reorder_path->get_depth == 0 or return;
217 exists $_on_row_offsets{$self->{'type'}} or return;
218  
219 # when "at", "before" or "after" move $self->{'index'} old value to new,
220 # searching through $old_index == $aref->[$new_index]
221 my $index = $self->{'index'};
222 foreach my $new_index (0 .. $#$aref) {
223 if ($aref->[$new_index] == $index) {
224 $self->{'index'} = $new_index;
225 $self->notify ('index');
226 last;
227 }
228 }
229 }
230  
231 # optional $iter is the $index row
232 sub _get_key {
233 my ($self, $index, $iter) = @_;
234  
235 my $key_func = $self->{'key_func'};
236 my $key_column = $self->{'key_column'};
237 defined $key_func or $key_column>=0 or return;
238  
239 my $model = $self->{'model'};
240 $iter ||= $model->iter_nth_child (undef, $index);
241 if (! $iter) { return; }
242  
243 if (defined $key_func) {
244 return $key_func->($model, $iter);
245 } else {
246 return $model->get_value ($iter, $key_column);
247 }
248 }
249 sub _match_key {
250 my ($self, $want, $got) = @_;
251 if (DEBUG) {
252 print " _match_key ",
253 (defined $want ? $want : 'undef'),
254 " ",(defined $got ? $got : 'undef'),"\n";
255 }
256 if (! defined $got) { return 0; }
257 if (my $key_equal = $self->{'key_equal'}) {
258 return $key_equal->($want, $got);
259 } else {
260 return $want eq $got;
261 }
262 }
263  
264 sub model {
265 my ($self) = @_;
266 return $self->{'model'};
267 }
268 sub index {
269 my ($self) = @_;
270 return $self->{'index'};
271 }
272 sub iter {
273 my ($self) = @_;
274 if ($self->{'type'} eq 'at') {
275 my $model = $self->{'model'};
276 return $model->iter_nth_child ($self->{'index'});
277 } else {
278 return undef;
279 }
280 }
281  
282 sub goto {
283 my ($self, $index, $type) = @_;
284 $type ||= 'at';
285 if (DEBUG) { print "ListModelPos goto $index, $type\n"; }
286 $self->{'index'} = $index;
287 $self->{'type'} = $type;
288 if ($type eq 'at') {
289 _at_key ($self, $index); # record key
290 }
291 $self->notify ('index');
292 $self->notify ('type');
293 }
294  
295 sub goto_start {
296 my ($self) = @_;
297 $self->{'type'} = 'start';
298 $self->notify ('type');
299 }
300 sub goto_end {
301 my ($self) = @_;
302 $self->{'type'} = 'end';
303 $self->notify ('type');
304 }
305  
306 sub next_index {
307 my ($self) = @_;
308 if (DEBUG) { print "ListModelPos next ",$self->{'model'},"\n"; }
309  
310 if ($self->{'type'} eq 'end') {
311 return undef;
312 }
313  
314 my $index = $self->{'index'};
315 if ($self->{'type'} eq 'start') {
316 $index = 0;
317 } elsif ($self->{'type'} ne 'before') {
318 $index++;
319 }
320  
321 my $model = $self->{'model'};
322 my $mlen = $model->iter_n_children(undef);
323 if ($index >= $mlen) {
324 return undef;
325 }
326  
327 _at_key ($self, $index);
328 return $self->{'index'};
329 }
330  
331 sub prev_index {
332 my ($self) = @_;
333 if (DEBUG) { print "ListModelPos prev $self->{'model'}\n"; }
334  
335 my $type = $self->{'type'};
336 if ($type eq 'start') {
337 return undef;
338 }
339  
340 my $model = $self->{'model'};
341 my $mlen = $model->iter_n_children(undef);
342 if ($mlen == 0) {
343 return undef;
344 }
345  
346 my $index = $self->{'index'};
347 if ($type eq 'end') {
348 $index = $mlen - 1;
349 } elsif ($type eq 'at' || $type eq 'before') {
350 $index--;
351 }
352 $index = min ($index, $mlen-1);
353  
354 if ($index < 0) {
355 return undef;
356 }
357  
358 _at_key ($self, $index);
359 return $self->{'index'};
360 }
361  
362 # optional $key is row key data for $index, if not given then _get_key() runs
363 sub _at_key {
364 my ($self, $index, $key) = @_;
365 if (DEBUG) { print " at $index\n"; }
366 $self->{'type'} = 'at';
367 $self->{'index'} = $index;
368 $self->notify ('type');
369 $self->notify ('index');
370  
371 delete $self->{'want_key'};
372 if (@_ < 3) {
373 $key = _get_key ($self, $index);
374 }
375 if (defined $key) {
376 $self->{'at_key'} = $key;
377 }
378 }
379  
380 sub next_iter {
381 my ($self) = @_;
382 my $index = $self->next_index;
383 if (! defined $index) { return undef; }
384 return $self->{'model'}->iter_nth_child (undef, $index);
385 }
386 sub prev_iter {
387 my ($self) = @_;
388 my $index = $self->prev_index;
389 if (! defined $index) { return undef; }
390 return $self->{'model'}->iter_nth_child (undef, $index);
391 }
392  
393 1;
394 __END__
« App/Chart/Gtk2/Diagnostics.pm ↑ Summary App/Chart/Suffix/ATH.pm »