App/Chart/Gtk2/Ex/ListOfListsModel.pm untested

filter: Statementstmt 0.0% 0 / 507 Branchbran 0.0% 0 / 234 Conditioncond 0.0% 0 / 99 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 56 Podpod 0.0% 0 / 44 totaltotal 0.0% 0 / 940 CC 118 SCAR 39.4
CC
118
Cov
0%
_do_toplevel_row_changed
182.0
ITER_NTH_CHILD
156.0
_update_models
90.0
CRAP 51.4
1 # iter when submodel undef ...
2  
3  
4 # Copyright 2008, 2009, 2010, 2011 Kevin Ryde
5  
6 # This file is part of Chart.
7 #
8 # Chart is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as published
10 # by the Free Software Foundation; either version 3, or (at your option) any
11 # later version.
12 #
13 # Chart is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16 # Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along
19 # with Chart. If not, see <http://www.gnu.org/licenses/>.
20  
21  
22 package App::Chart::Gtk2::Ex::ListOfListsModel;
23 use 5.008;
24 use strict;
25 use warnings;
26 use Gtk2 1.201; # 1.201 for drag_data_get() stack fix
27 use Carp;
28 use List::Util qw(min max);
29 use Scalar::Util;
30  
31 use App::Chart::Glib::Ex::MoreUtils;
32 use Glib::Ex::SignalIds;
33 use Gtk2::Ex::TreeModel::ImplBits;
34  
35 use constant DEBUG => 0;
36  
37 use Glib::Object::Subclass
38 'Glib::Object',
39 interfaces => [ 'Gtk2::TreeModel',
40 'Gtk2::TreeDragSource',
41 'Gtk2::TreeDragDest',
42 ],
43 properties => [ Glib::ParamSpec->object
44 ('list-model',
45 'list-model',
46 'Model of list models to present.',
47 'Gtk2::TreeModel',
48 Glib::G_PARAM_READWRITE)
49 ];
50  
51 sub INIT_INSTANCE {
52 my ($self) = @_;
53 $self->{'n_columns'} = 0;
54 $self->{'column_types'} = [];
55 Gtk2::Ex::TreeModel::ImplBits::random_stamp ($self);
56 }
57  
58 sub SET_PROPERTY {
59 my ($self, $pspec, $newval) = @_;
60 my $pname = $pspec->get_name;
61 if (DEBUG) { print "ListOfListsModel set '$pname'\n"; }
62  
63 if ($pname eq 'list_model') {
64 if (! $newval->isa('Gtk2::TreeModel')) {
65 croak 'ListOfListsModel.list-model object must implement GtkTreeModel interface';
66 }
67 }
68 $self->{$pname} = $newval; # per default GET_PROPERTY
69  
70 if ($pname eq 'list_model') {
71 _update_models ($self);
72 }
73 }
74  
75 sub _update_models {
76 my ($self) = @_;
77 if (DEBUG) { print "ListOfListsModel _update_models\n"; }
78  
79 my $list_model = $self->{'list_model'};
80 my $ref_weak_self = App::Chart::Glib::Ex::MoreUtils::ref_weak($self);
81  
82 $self->{'list_model_ids'} = $list_model &&
83 Glib::Ex::SignalIds->new
84 ($list_model,
85 $list_model->signal_connect
86 (row_changed =>\&_do_toplevel_row_changed, $ref_weak_self),
87 $list_model->signal_connect
88 (row_deleted =>\&_do_toplevel_row_deleted, $ref_weak_self),
89 $list_model->signal_connect
90 (row_inserted =>\&_do_toplevel_row_inserted, $ref_weak_self),
91 $list_model->signal_connect
92 (rows_reordered=>\&_do_toplevel_rows_reordered, $ref_weak_self));
93  
94 my @mlist;
95 my $n_columns = 0;
96 my @column_types;
97  
98 if ($list_model) {
99 if (DEBUG) { print " list_model $list_model\n"; }
100 $n_columns = $self->{'list_model_n_columns'} = $list_model->get_n_columns;
101 @column_types = map {$list_model->get_column_type($_)} (0..$n_columns-1);
102 my $submodel;
103  
104 # for (my $iter = $list_model->get_iter_first, my $mnum = 0;
105 # $iter;
106 # $iter = $list_model->iter_next ($iter), $mnum++) {
107  
108 for (my $mnum = 0; ; $mnum++) {
109 if (DEBUG) { print " mnum $mnum\n"; }
110 my $iter = $list_model->iter_nth_child (undef, $mnum) || last;
111 my $model = $list_model->get_value ($iter, 0);
112 $submodel ||= $model;
113  
114 my $has_child = ($model && $model->iter_n_children(undef) != 0 ? 1 : 0);
115 my $minfo = { model => $model,
116 mnum => $mnum,
117 self => $self,
118 has_child => $has_child };
119 Scalar::Util::weaken ($minfo->{'self'});
120 push @mlist, $minfo;
121  
122 if ($model) {
123 $minfo->{'ids'} = Glib::Ex::SignalIds->new
124 ($model,
125 $model->signal_connect
126 (row_changed => \&_do_sublist_row_changed, $minfo),
127 $model->signal_connect
128 (row_deleted => \&_do_sublist_row_deleted, $minfo),
129 $model->signal_connect
130 (row_inserted => \&_do_sublist_row_inserted, $minfo),
131 $model->signal_connect
132 (rows_reordered => \&_do_sublist_rows_reordered, $minfo));
133 }
134 }
135  
136 if ($submodel) {
137 my $sub_columns = $submodel->get_n_columns;
138 $n_columns += $sub_columns;
139 push @column_types,
140 map {$submodel->get_column_type($_)} (0 .. $sub_columns-1);
141 }
142 }
143  
144 $self->{'mlist'} = \@mlist;
145 $self->{'n_columns'} = $n_columns;
146 $self->{'column_types'} = \@column_types;
147  
148 if (DEBUG) {
149 local $, = ' ';
150 print " column_types",@column_types,"\n";
151 }
152 }
153  
154 # gtk_tree_model_get_flags
155 #
156 sub GET_FLAGS {
157 return [];
158 }
159  
160 # gtk_tree_model_get_n_columns
161 #
162 sub GET_N_COLUMNS {
163 my ($self) = @_;
164 if (DEBUG >= 2) { print "ListOfListsModel get_n_columns\n"; }
165 return $self->{'n_columns'};
166 }
167  
168 # gtk_tree_model_get_column_type
169 #
170 sub GET_COLUMN_TYPE {
171 return $_[0]->{'column_types'}->[$_[1]];
172 }
173  
174 # gtk_tree_model_get_iter
175 #
176 sub GET_ITER {
177 my ($self, $path) = @_;
178 if (DEBUG >= 2) { print "ListOfListsModel get_iter, path='",
179 $path->to_string,"'\n"; }
180 my $depth = $path->get_depth;
181 $self->{'list_model'} || return undef; # when no models
182 my ($top_index, $sub_index) = $path->get_indices;
183  
184 if ($depth == 1) {
185 # top-level
186 return [ $self->{'stamp'}, $top_index, undef, undef ];
187 }
188 if ($depth == 2) {
189 # sub-list
190 my $minfo = $self->{'mlist'}->[$top_index] || return undef;
191 return [ $self->{'stamp'}, $sub_index, $minfo, undef ];
192 }
193 return undef;
194 }
195  
196 # gtk_tree_model_get_path
197 #
198 sub GET_PATH {
199 my ($self, $iter) = @_;
200 if (DEBUG >= 2) { print "ListOfListsModel get_path\n"; }
201 my ($index, $minfo) = _iter_validate ($self, $iter);
202 if (defined $minfo) {
203 my $mnum = $minfo->{'mnum'};
204 return Gtk2::TreePath->new_from_indices ($mnum, $index);
205 } else {
206 return Gtk2::TreePath->new_from_indices ($index);
207 }
208 }
209  
210 # gtk_tree_model_get_value
211 #
212 sub GET_VALUE {
213 my ($self, $iter, $col) = @_;
214 if (DEBUG >= 2) { print "ListOfListsModel get_value\n";
215 print " iter=",$iter->[0],",",$iter->[1],",",
216 defined $iter->[2] ? $iter->[2] : 'undef',
217 " col=$col\n"; }
218  
219 my ($index, $minfo) = _iter_validate ($self, $iter);
220 my $list_model_n_columns = $self->{'list_model_n_columns'};
221 my $model;
222  
223 if ($col < $list_model_n_columns) {
224 # columns of list_model
225 $model = $self->{'list_model'} || return undef;
226 if ($minfo) {
227 # $iter is a submodel row
228 $index = $minfo->{'mnum'};
229 }
230  
231 } else {
232 # columns of submodels
233 if (! $minfo) {
234 if (DEBUG >= 2) { print " submodel col empty in toplevel row\n"; }
235 return;
236 }
237 $col -= $list_model_n_columns;
238 $model = $minfo->{'model'} || return undef;
239 if (DEBUG >= 2) { print " submodel adj col=$col\n"; }
240 }
241  
242 my $subiter = $model->iter_nth_child(undef,$index) || do {
243 if (DEBUG >= 2) { print " cannot get subiter $model index=$index\n"; }
244 return undef;
245 };
246 return $model->get_value ($subiter, $col);
247 }
248  
249 # gtk_tree_model_iter_next
250 #
251 sub ITER_NEXT {
252 my ($self, $iter) = @_;
253 if (DEBUG >= 2) { print "ListOfListsModel iter_next\n"; }
254 my ($index, $minfo) = _iter_validate ($self, $iter);
255  
256 $index++;
257 if ($minfo) {
258 # next in submodel
259 my $model = $minfo->{'model'} || return undef;
260 if ($index < $model->iter_n_children(undef)) {
261 return [ $self->{'stamp'}, $index, $minfo, undef ];
262 }
263  
264 } else {
265 # next in toplevel
266 if ($index < scalar @{$self->{'mlist'}}) {
267 return [ $self->{'stamp'}, $index, undef, undef ];
268 }
269 }
270 return undef;
271 }
272  
273 # gtk_tree_model_iter_children
274 #
275 sub ITER_CHILDREN {
276 my ($self, $iter) = @_;
277 if (DEBUG) { print "ListOfListsModel iter_children\n"; }
278 return ITER_NTH_CHILD ($self, $iter, 0);
279 }
280  
281 # gtk_tree_model_iter_has_child
282 #
283 sub ITER_HAS_CHILD {
284 my ($self, $iter) = @_;
285 if (DEBUG) { print "ListOfListsModel has_child ",$iter->[1],"\n"; }
286  
287 # Note: prior to Gtk2-Perl 1.190 the return had to be a number, not any
288 # old boolean
289 return ITER_N_CHILDREN($self,$iter) != 0;
290 }
291  
292 # gtk_tree_model_iter_n_children
293 #
294 sub ITER_N_CHILDREN {
295 my ($self, $iter) = @_;
296 if (DEBUG) { print "ListOfListsModel iter_n_children\n"; }
297  
298 if (! defined $iter) {
299 # asking about top-levels
300 if (DEBUG) { print " toplevel has ",scalar @{$self->{'mlist'}},"\n"; }
301 return scalar @{$self->{'mlist'}};
302 }
303 my ($index, $minfo) = _iter_validate ($self, $iter);
304  
305 if ($minfo) {
306 # asking about under submodel
307 if (DEBUG) { print " nothing under submodel rows\n"; }
308 return 0;
309 } else {
310 # asking about under toplevel row
311 $minfo = $self->{'mlist'}->[$index] || return 0;
312 my $model = $minfo->{'model'} || return 0;
313 if (DEBUG) { print " model row has",$model->iter_n_children(undef),"\n"; }
314 return $model->iter_n_children(undef);
315 }
316 }
317  
318 # gtk_tree_model_iter_nth_child
319 #
320 sub ITER_NTH_CHILD {
321 my ($self, $iter, $n) = @_;
322 if (DEBUG) { print "ListOfListsModel iter_nth_child",
323 " index=",$iter?$iter->[1]:'<iter undef>',
324 " minfo=",($iter&&$iter->[2])||'undef',
325 " child n=$n\n"; }
326  
327 my $mlist = $self->{'mlist'};
328 if (defined $iter) {
329 my ($index, $minfo) = _iter_validate ($self, $iter);
330 if (! $minfo) {
331 # $n'th row of model under toplevel $index
332 if ($minfo = $mlist->[$index]) {
333 if (my $model = $minfo->{'model'}) {
334 if ($n < $model->iter_n_children(undef)) {
335 if (DEBUG) { print " yes, submodel $model row $n\n"; }
336 return [ $self->{'stamp'}, $n, $minfo, undef ];
337 }
338 }
339 }
340 }
341 } else {
342 # $n'th row of top-level
343 if ($n < @$mlist) {
344 if (DEBUG) { print " yes, toplevel row $n\n"; }
345 return [ $self->{'stamp'}, $n, undef, undef ];
346 }
347 }
348 if (DEBUG) { print " no\n"; }
349 return undef;
350 }
351  
352 # gtk_tree_model_iter_parent
353 #
354 sub ITER_PARENT {
355 my ($self, $iter) = @_;
356 if (DEBUG) { print "ListOfListsModel iter_parent\n"; }
357  
358 my ($index, $minfo) = _iter_validate ($self, $iter);
359 if (defined $minfo) {
360 if (DEBUG) { print " yes, up to toplevel ",$minfo->{'mnum'},"\n"; }
361 return [ $self->{'stamp'}, $minfo->{'mnum'}, undef, undef ];
362 }
363 if (DEBUG) { print " no\n"; }
364 return undef;
365 }
366  
367 # gtk_tree_model_ref_node
368 # gtk_tree_model_unref_node
369  
370  
371 #------------------------------------------------------------------------------
372 # our iters
373  
374 # return ($mnum, $minfo), with $minfo undef on toplevel rows
375 sub _iter_validate {
376 my ($self, $iter) = @_;
377 if ($iter->[0] != $self->{'stamp'}) {
378 croak "iter is not for this ", ref($self),
379 " (stamp ", $iter->[0], " want ", $self->{'stamp'}, ")\n";
380 }
381 return ($iter->[1], $iter->[2]);
382 }
383  
384 sub _top_index_to_iterobj {
385 my ($self, $mnum) = @_;
386 return Gtk2::TreeIter->new_from_arrayref ([ $self->{'stamp'}, $mnum, undef, undef ]);
387 }
388  
389 sub _sub_index_to_iterobj {
390 my ($self, $minfo, $index) = @_;
391 return Gtk2::TreeIter->new_from_arrayref ([ $self->{'stamp'}, $index, $minfo, undef]);
392 }
393  
394 sub _iterobj_validate {
395 my ($self, $iterobj) = @_;
396 if (! defined $iterobj) {
397 croak 'iter is undef';
398 }
399 my $iter = Gtk2::TreeIter->to_arrayref ($self->{'stamp'});
400 return ($iter->[1], $iter->[2]);
401 }
402  
403  
404 #------------------------------------------------------------------------------
405 # 'list_model' toplevel signals
406  
407 # 'row-changed' on the toplevel list-model
408 #
409 sub _do_toplevel_row_changed {
410 my ($model, $path, $subiter, $ref_weak_self) = @_;
411 my $self = $$ref_weak_self || return;
412 if (DEBUG) { print "ListOfListsModel toplevel row_changed handler\n";}
413 if ($path->get_depth != 1) { return; } # ignore non-toplevel
414  
415 my ($mnum) = $path->get_indices;
416 my $old_minfo = $self->{'mlist'}->[$mnum];
417 my $old_model = $old_minfo && $old_minfo->{'model'};
418 my $old_has_child = $old_minfo && $old_minfo->{'has_child'};
419 $old_minfo = undef;
420  
421 _update_models ($self);
422  
423 my $new_minfo = $self->{'mlist'}->[$mnum];
424 my $new_model = $new_minfo->{'model'};
425  
426 my $iterobj = _top_index_to_iterobj ($self, $mnum);
427 $self->row_changed ($path, $iterobj);
428  
429 if (! defined $old_has_child ||
430 $old_has_child != $new_minfo->{'has_child'}) {
431 $self->row_has_child_toggled ($path, $iterobj);
432 }
433  
434 if (($old_model||0) != ($new_model||0)) {
435 if (DEBUG) { print " different model at $mnum: old ",
436 $old_model||'undef', " new ",$new_model||'undef',"\n";}
437  
438 my $old_len = $old_model ? $old_model->iter_n_children(undef) : 0;
439 my $new_len = $new_model ? $new_model->iter_n_children(undef) : 0;
440  
441 my $changed_len = min ($old_len, $new_len);
442 if (DEBUG) { print " changed_len $changed_len\n"; }
443 foreach my $i (0 .. $changed_len - 1) {
444 $path = Gtk2::TreePath->new_from_indices ($mnum, $i);
445 $iterobj = _sub_index_to_iterobj ($self, $new_minfo, $i);
446 $self->row_changed ($path, $iterobj);
447 }
448  
449 if ($old_len > $changed_len) {
450 if (DEBUG) { print " deleted to $old_len\n"; }
451 $path = Gtk2::TreePath->new_from_indices ($mnum, $changed_len);
452 foreach ($changed_len .. $old_len - 1) {
453 $self->row_deleted ($path);
454 }
455 }
456  
457 if ($new_len > $changed_len) {
458 if (DEBUG) { print " inserted to $old_len\n"; }
459 foreach my $i ($changed_len .. $old_len - 1) {
460 $path = Gtk2::TreePath->new_from_indices ($mnum, $i);
461 $iterobj = _sub_index_to_iterobj ($self, $new_minfo, $i);
462 $self->row_inserted ($path, $iterobj);
463 }
464 }
465 }
466 }
467  
468 # 'row-inserted' on the toplevel list-model
469 #
470 sub _do_toplevel_row_inserted {
471 my ($model, $path, $subiter, $ref_weak_self) = @_;
472 my $self = $$ref_weak_self || return;
473 if (DEBUG) { print "ListOfListsModel toplevel row_inserted handler\n";}
474 if ($path->get_depth != 1) { return; } # ignore non-toplevel
475  
476 _update_models ($self);
477 my ($mnum) = $path->get_indices;
478 my $iterobj = _top_index_to_iterobj ($self, $mnum);
479 $self->row_inserted ($path, $iterobj);
480 }
481  
482 # 'row-deleted' on the toplevel list-model
483 #
484 sub _do_toplevel_row_deleted {
485 my ($model, $path, $ref_weak_self) = @_;
486 my $self = $$ref_weak_self || return;
487 if (DEBUG) { print "ListOfListsModel toplevel row_deleted handler\n";}
488 if ($path->get_depth != 1) { return; } # ignore non-toplevel
489  
490 _update_models ($self);
491 $self->row_deleted ($path);
492 }
493  
494 # 'rows-reordered' on the toplevel list-model
495 #
496 sub _do_toplevel_rows_reordered {
497 my ($model, $path, $iter, $aref, $ref_weak_self) = @_;
498 my $self = $$ref_weak_self || return;
499 if (DEBUG) { print "ListOfListsModel toplevel rows_reordered handler\n";}
500 if ($path->get_depth != 0) { return; } # ignore non-toplevel
501  
502 _update_models ($self);
503 $self->rows_reordered ($path, undef, @$aref);
504 }
505  
506 #------------------------------------------------------------------------------
507 # sub-model signals
508  
509 # 'row-changed' on sub-list
510 #
511 sub _do_sublist_row_changed {
512 my ($model, $subpath, $subiter, $minfo) = @_;
513 my $self = $minfo->{'self'} || return;
514  
515 if (DEBUG) { print "ListOfListsModel submodel row_changed handler\n";}
516 if ($subpath->get_depth != 1) { return; } # ignore non-toplevel
517  
518 my $mnum = $minfo->{'mnum'};
519 my ($index) = $subpath->get_indices;
520 my $path = Gtk2::TreePath->new_from_indices ($mnum, $index);
521 my $iterobj = _sub_index_to_iterobj ($self, $minfo, $index);
522 $self->row_changed ($path, $iterobj);
523 }
524  
525 # 'row-inserted' on sub-list
526 #
527 sub _do_sublist_row_inserted {
528 my ($model, $subpath, $subiter, $minfo) = @_;
529 my $self = $minfo->{'self'} || return;
530  
531 if (DEBUG) { print "ListOfListsModel submodel row_inserted handler\n";}
532 if ($subpath->get_depth != 1) { return; } # ignore non-toplevel
533  
534 my $mnum = $minfo->{'mnum'};
535 my ($index) = $subpath->get_indices;
536 my $path = Gtk2::TreePath->new_from_indices ($mnum, $index);
537 my $iterobj = _sub_index_to_iterobj ($self, $minfo, $index);
538 $self->row_inserted ($path, $iterobj);
539  
540 if (! $minfo->{'has_child'} && $model->iter_n_children(undef) != 0) {
541 # newly become with-children
542 $minfo->{'has_child'} = 1;
543 $path->up;
544 my $iterobj = _top_index_to_iterobj ($self, $mnum);
545 $self->row_has_child_toggled ($path, $iterobj);
546 }
547 }
548  
549 # 'row-deleted' on sub-list
550 #
551 sub _do_sublist_row_deleted {
552 my ($model, $subpath, $minfo) = @_;
553 my $self = $minfo->{'self'} || return;
554  
555 if (DEBUG) { print "ListOfListsModel submodel row_deleted handler\n";}
556 if ($subpath->get_depth != 1) { return; } # ignore non-toplevel
557  
558 my $mnum = $minfo->{'mnum'};
559 my $path = $subpath->copy;
560 $path->prepend_index ($mnum);
561 $self->row_deleted ($path);
562  
563 if ($minfo->{'has_child'} && $model->iter_n_children(undef) == 0) {
564 # newly become no-children
565 $minfo->{'has_child'} = 0;
566 $path->up;
567 my $iterobj = _top_index_to_iterobj ($self, $mnum);
568 $self->row_has_child_toggled ($path, $iterobj);
569 }
570 }
571  
572 # 'rows-reordered' on sub-list
573 #
574 sub _do_sublist_rows_reordered {
575 my ($model, $subpath, $subiter, $aref, $minfo) = @_;
576 my $self = $minfo->{'self'} || return;
577  
578 if (DEBUG) { print "ListOfListsModel submodel rows_reordered handler\n";}
579 if ($subpath->get_depth != 0) { return; } # ignore non-toplevel
580  
581 my $mnum = $minfo->{'mnum'};
582 my $path = Gtk2::TreePath->new_from_indices ($mnum);
583 my $iterobj = _top_index_to_iterobj ($self, $mnum);
584 $self->rows_reordered ($path, $iterobj, @$aref);
585 }
586  
587  
588 #------------------------------------------------------------------------------
589 # Gtk2::TreeStore compatible methods
590  
591 # gtk_tree_store_append
592 # gtk_tree_store_prepend
593 sub append {
594 push @_, 'append';
595 goto _Xpend;
596 }
597 sub prepend {
598 push @_, 'prepend';
599 goto _Xpend;
600 }
601 sub _Xpend {
602 my ($self, $parent_iterobj, $method) = @_;
603 my ($model, $minfo, $subiter);
604 if (! defined $parent_iterobj) {
605 # append to toplevel
606 $model = $self->{'list_model'};
607 $subiter = $model->$method;
608 } else {
609 my $index;
610 ($index, $minfo) = _iterobj_validate ($parent_iterobj);
611 if ($minfo) { croak 'cannot append under sub-row'; }
612 # append to submodel, ie. under a toplevel row
613 $minfo = $self->{'mlist'}->[$index] || croak 'no such row (bad iter)';
614 $model = $minfo->{'model'};
615 $subiter = $model->$method;
616 }
617 if (! $subiter) { return undef; }
618 my ($index) = $model->get_path($subiter)->get_indices;
619 return Gtk2::TreeIter->new_from_arrayref ([ $self->{'stamp'}, $index, $minfo, undef]);
620 }
621  
622 # gtk_tree_store_is_ancestor
623 sub is_ancestor {
624 my ($self, $parent_iterobj, $child_iterobj) = @_;
625 my ($parent_index, $parent_minfo) = _iterobj_validate ($parent_iterobj);
626 if ($parent_minfo) { return 0; } # a submodel row
627 my ($child_index, $child_minfo) = _iterobj_validate ($child_iterobj);
628 return defined $child_minfo; # only if a submodel row
629 }
630  
631 # gtk_tree_store_iter_depth
632 sub iter_depth {
633 my ($self, $iterobj) = @_;
634 my ($index, $minfo) = _iterobj_validate ($iterobj);
635 return $minfo ? 2 : 1;
636 }
637  
638 # gtk_tree_store_clear
639 sub clear {
640 my ($self) = @_;
641 my $list_model = $self->{'list_model'} || return;
642 $list_model->clear;
643 }
644  
645 # gtk_tree_store_iter_is_valid
646 # this is not quite right
647 # if $iter->[2] is bogus it probably causes a segv when treated as an RV
648 sub iter_is_valid {
649 my ($self, $iterobj) = @_;
650 my $iter = eval { $iterobj->to_arrayref($self->{'stamp'}) };
651 my $index = $iter->[1];
652 my $minfo = $iter->[2];
653 if (defined $minfo) {
654 return List::Util::first {$_ == $minfo} @{$self->{'mlist'}}
655 && $index < $minfo->{'model'}->iter_n_children(undef);
656 } else {
657 return $index < scalar @{$self->{'mlist'}};
658 }
659 }
660  
661 sub remove {
662 my ($self, $iterobj) = @_;
663 my ($index, $minfo) = _iterobj_validate ($iterobj);
664 my $model = $minfo ? $minfo->{'model'} : $self->{'list_model'};
665 my $subiter = $model->iter_nth_child (undef, $index)
666 || croak 'no such row (bad iter)';
667 if ($model->remove ($subiter)) {
668 # more rows
669 $iterobj->set([$self->{'stamp'}, $index+1, $minfo, undef]);
670 return 1;
671 } else {
672 $iterobj->set([0,0,undef,undef]); # zap
673 return 0;
674 }
675 }
676  
677 # gtk_tree_store_reorder
678 # Gtk2::TreeStore reorder() taking multiple args
679 sub reorder {
680 my ($self, $iterobj, @order) = @_;
681 if ($iterobj) {
682 my ($index, $minfo) = _iterobj_validate ($iterobj);
683 if ($minfo) {
684 croak 'nothing under sub-row';
685 }
686 $minfo = $self->{'mlist'}->[$index] || croak 'iter out of range';
687 my $model = $minfo->{'model'} || croak 'no submodel';
688 $model->reorder (@order);
689  
690 } else {
691 # toplevel
692 $self->{'list_model'}->reorder (@order);
693 }
694 }
695  
696  
697 #------------------------------------------------------------------------------
698 # other methods
699  
700 sub path_for_model {
701 my ($self, $model) = @_;
702 my $mlist = $self->{'mlist'};
703 foreach my $minfo (@$mlist) {
704 my $this_model = $minfo->{'model'} || next;
705 if ($model == $this_model) {
706 return Gtk2::TreePath->new_from_indices ($minfo->{'mnum'});
707 }
708 }
709 return undef;
710 }
711  
712  
713 #------------------------------------------------------------------------------
714 # drag source
715  
716 # gtk_tree_drag_source_row_draggable ($self, $path)
717 #
718 sub ROW_DRAGGABLE {
719 unshift @_, 'row_draggable';
720 goto &_drag_source;
721 }
722  
723 # gtk_tree_drag_source_drag_data_delete ($self, $path)
724 #
725 sub DRAG_DATA_DELETE {
726 unshift @_, 'drag_data_delete';
727 goto &_drag_source;
728 }
729  
730 # gtk_tree_drag_source_drag_data_get
731 #
732 sub DRAG_DATA_GET {
733 my ($self, $path, $sel) = @_;
734 unshift @_, 'drag_data_get'; # needing Gtk2 1.200
735 goto &_drag_source;
736 }
737  
738 sub _drag_source {
739 my ($method, $self, $path, @sel) = @_;
740 if (DEBUG) { print "ListOfLists \U$method\E path=",$path->to_string,"\n"; }
741  
742 my ($model, $subpath) = _drag_path ($self, $path)
743 or return 0; # path no good
744  
745 if (! $model->isa('Gtk2::TreeDragSource')) {
746 if (DEBUG) { print " no, model not a TreeDragSource\n"; }
747 return 0;
748 }
749 if (DEBUG) { print " submodel $model ->$method subpath=",
750 $subpath->to_string,"\n"; }
751 my $ret = $model->$method ($subpath, @sel);
752 if (DEBUG) { print " ",$ret?"yes":"no","\n"; }
753 return $ret;
754 }
755  
756 sub _drag_path {
757 my ($self, $path) = @_;
758  
759 my $depth = $path->get_depth;
760 if ($depth == 1) {
761 # toplevel row
762 return ($self->{'list_model'}, $path);
763 }
764 if ($depth == 2) {
765 my ($top_index, $sub_index) = $path->get_indices;
766 my $minfo = $self->{'mlist'}->[$top_index] || do {
767 if (DEBUG) { print " no, toplevel index out of range\n"; }
768 return;
769 };
770 my $model = $minfo->{'model'} || do {
771 if (DEBUG) { print " no, no submodel (undef) at this row\n"; }
772 return;
773 };
774 $path = Gtk2::TreePath->new_from_indices ($sub_index);
775 return ($model, $path);
776 }
777 if (DEBUG) { print " no, not depth 1 or 2\n"; }
778 return;
779 }
780  
781 #------------------------------------------------------------------------------
782 # drag destination
783  
784 # gtk_tree_drag_dest_row_drop_possible
785 #
786 sub ROW_DROP_POSSIBLE {
787 my ($self, $dst_path, $sel) = @_;
788 if (DEBUG) { print "ListOfLists ROW_DROP_POSSIBLE, to path=",
789 $dst_path->to_string," type=",$sel->type->name,"\n";
790 if ($sel->type->name eq 'GTK_TREE_MODEL_ROW') {
791 my ($src_model, $src_path) = $sel->get_row_drag_data;
792 print " src_model=$src_model src_path=",
793 $src_path->to_string,"\n";
794 }}
795  
796 my ($model, $subpath) = _drag_path ($self, $dst_path)
797 or return 0; # path no good
798  
799 # if the submodel implements DragDest and it's prepared to accept $sel
800 # directly
801 if ($model->isa('Gtk2::TreeDragDest')) {
802 if ($model->row_drop_possible ($subpath, $sel)) {
803 if (DEBUG) { print " yes, submodel row_drop_possible()\n"; }
804 return 1;
805 }
806 }
807  
808 if (DEBUG) { print " no, submodel refuses\n"; }
809 return 0;
810 }
811  
812 # gtk_tree_drag_dest_drag_data_received
813 #
814 sub DRAG_DATA_RECEIVED {
815 my ($self, $dst_path, $sel) = @_;
816 if (DEBUG) { print "ListOfLists DRAG_DATA_RECEIVED, to path=",
817 $dst_path->to_string," type=",$sel->type->name,"\n";
818 if ($sel->type->name eq 'GTK_TREE_MODEL_ROW') {
819 my ($src_model, $src_path) = $sel->get_row_drag_data;
820 print " src_model=$src_model src_path=",
821 $src_path->to_string,"\n";
822 }}
823  
824 my $dst_model;
825 ($dst_model, $dst_path) = _drag_path ($self, $dst_path)
826 or return 0; # path no good
827  
828 if ($dst_model->isa('Gtk2::TreeDragDest')) {
829 if ($dst_model->drag_data_received ($dst_path, $sel)) {
830 if (DEBUG) { print " accepted by submodel\n";}
831 return 1;
832 }
833 }
834  
835 if (DEBUG) { print " no, dest won't accept\n";}
836 return 0;
837 }
838  
839 1;
840 __END__
841  
842 =for stopwords submodel submodels ListOfLists TreeDragSource TreeDragDest toplevel Gtk ie ListofLists Eg TreeModel iter iters Ryde Chart
843  
844 =head1 NAME
845  
846 App::Chart::Gtk2::Ex::ListOfListsModel -- two-level tree model presenting lists
847  
848 =for test_synopsis my ($mmod)
849  
850 =head1 SYNOPSIS
851  
852 use App::Chart::Gtk2::Ex::ListOfListsModel;
853 my $ll = App::Chart::Gtk2::Ex::ListOfListsModel->new (list_model => $mmod);
854  
855 =head1 OBJECT HIERARCHY
856  
857 C<App::Chart::Gtk2::Ex::ListOfListsModel> is a subclass of C<Glib::Object>.
858  
859 Glib::Object
860 App::Chart::Gtk2::Ex::ListOfListsModel
861  
862 and implements the interfaces
863  
864 Gtk2::TreeModel
865 Gtk2::TreeDragSource
866 Gtk2::TreeDragDest
867  
868 =head1 DESCRIPTION
869  
870 C<App::Chart::Gtk2::Ex::ListOfListsModel> presents a set of list models as a
871 two level tree,
872  
873 Toplevel row 0
874 Submodel 0 row 0
875 Submodel 0 row 1
876 Submodel 0 row 2
877 Toplevel row 1
878 Submodel 1 row 0
879 Submodel 1 row 1
880  
881 The C<list-model> property is a model containing submodel objects. Column 0
882 in C<list-model> should be the submodel objects. Changes in that
883 C<list-model> and changes in the submodels are all reported up through the
884 ListOfLists.
885  
886 The columns in the ListOfLists are those of C<list-model> followed by those
887 of the submodels. The submodels are expected to all have the same number of
888 columns and column types. So
889  
890 +-----------+-----------+-----------+-----------+-----------+
891 | LL col 0 | LL col 1 | LL col 2 | LL col 3 | LL col 4 |
892 +-----------+-----------+-----------+-----------+-----------+
893  
894 with 2 columns in C<list-store> and 3 in the submodels would be
895  
896 +-----------+-----------+-----------+-----------+-----------+
897 | top col 0 | top col 1 | sub col 0 | sub col 1 | sub col 2 |
898 +-----------+-----------+-----------+-----------+-----------+
899  
900 =head2 Drag and Drop
901  
902 ListOfLists implements TreeDragSource and TreeDragDest, allowing rows to be
903 moved by dragging in a C<Gtk2::TreeView> or similar. Dragging is delegated
904 to the C<list-model> and the submodels. So a row can be dragged if its
905 model implements TreeDragSource, and a position can be a drop if its model
906 implements TreeDragDest. The effect is that you can drag to re-order the
907 toplevel rows, and drag within the submodels and perhaps between them.
908  
909 Dragging between models depends on the destination model being prepared to
910 accept the source row offered. For example as of Gtk 2.12 a
911 C<Gtk2::ListStore> only accepts its own rows (ie. a re-ordering of itself),
912 not rows from other models, even if the column types seem compatible. If
913 that's not enough for you the suggestion is to wrap or subclass the
914 offending models to make them accept more.
915  
916 =head1 PROPERTIES
917  
918 =over 4
919  
920 =item C<list-model> (C<Glib::Object> implementing C<Gtk2::TreeModel>)
921  
922 A model containing the submodels to present. It's expected to be a list
923 type model (ie. just one level), but currently that's not checked or
924 enforced.
925  
926 Currently when the C<list-model> property is changed there's no
927 C<row-inserted> / C<row-deleted> etc signals emitted by the ListofLists to
928 announce the new or altered data presented. Perhaps this will change. The
929 disadvantage is that changing to or from a big model could generate
930 thousands of fairly pointless signals. The suggestion is to treat
931 C<list-model> as if it were "construct-only" and make a new ListOfLists for
932 a new set of models.
933  
934 =back
935  
936 =head1 FUNCTIONS
937  
938 =over 4
939  
940 =item C<< $ll = App::Chart::Gtk2::Ex::ListOfListsModel->new (key=>value,...) >>
941  
942 Create and return a new ListOfLists object. Optional key/value pairs set
943 initial properties as per C<< Glib::Object->new >>. Eg.
944  
945 my $ll = App::Chart::Gtk2::Ex::ListOfListsModel->new (list_model => $mmod);
946  
947 =back
948  
949 =head1 SIGNALS
950  
951 The TreeModel interface as implemented by ListOfLists provides the following
952 usual signals
953  
954 row-changed ($ll, $path, $iter, $userdata)
955 row-inserted ($ll, $path, $iter, $userdata)
956 row-deleted ($ll, $path, $userdata)
957 rows-reordered ($ll, $path, $iter, $arrayref, $userdata)
958  
959 When a change occurs in a sub-model the corresponding signal is reported up
960 through the ListOfLists too. Of course the path and iter reported are in
961 the list-of-lists tree coordinates and iters, not the sub-models'.
962  
963 =head1 LIMITATIONS
964  
965 The C<ref_node> and C<unref_node> methods are no-ops. The intention is to
966 apply them down on the sub-models, but hopefully without needing lots of
967 bookkeeping in the ListOfLists as to what's currently reffed.
968  
969 It mostly works to have the same submodel appear in multiple toplevel rows,
970 it's simply presented at each of those points. The C<row-deleted> and
971 C<row-inserted> signals are emitted on the ListOfLists the right number of
972 times, but the multiple changes are all present in the data as of the first
973 emit, which could potentially confuse handler code. (The idea could be some
974 sort of temporary index mapping to make the changes seem one-at-a-time for
975 the handlers.)
976  
977 =head1 SEE ALSO
978  
979 L<Gtk2::TreeModel>, L<Gtk2::ListStore>, L<Glib::Object>
980  
981 L<Gtk2::Ex::ListModelConcat>, a similar thing as one level.
982  
983 =head1 HOME PAGE
984  
985 L<http://user42.tuxfamily.org/gtk2-ex-listoflistsmodel/index.html>
986  
987 =head1 COPYRIGHT
988  
989 Copyright 2008, 2009, 2010, 2011 Kevin Ryde
990  
991 Chart is free software; you can redistribute it and/or
992 modify it under the terms of the GNU General Public License as published by
993 the Free Software Foundation; either version 3, or (at your option) any
994 later version.
995  
996 Chart is distributed in the hope that it will be useful,
997 but WITHOUT ANY WARRANTY; without even the implied warranty of
998 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
999 Public License for more details.
1000  
1001 You should have received a copy of the GNU General Public License along with
1002 Chart. If not, see L<http://www.gnu.org/licenses/>.
1003  
1004 =cut