lpOD/Table.pm untested

filter: Statementstmt 0.0% 0 / 1262 Branchbran 0.0% 0 / 416 Conditioncond 0.0% 0 / 350 MC/DCmcdc 0.0% 0 / 212 Subroutinesub 0.0% 0 / 163 Podpod 0.0% 0 / 129 totaltotal 0.0% 0 / 2532 CC 209 SCAR 40.8
CC
209
Cov
0%
_get_cell_values
342.0
add_column
306.0
add_row
240.0
CRAP 59.3
1 #=============================================================================
2 #
3 # Copyright (c) 2010 Ars Aperta, Itaapy, Pierlis, Talend.
4 # Copyright (c) 2014 Jean-Marie Gouarné.
5 # Author: Jean-Marie Gouarné <jean.marie.gouarne@online.fr>
6 #
7 #=============================================================================
8 use 5.010_001;
9 use strict;
10 #=============================================================================
11 # Tables and table components (columns, rows, cells, row/col groups)
12 #=============================================================================
13 package ODF::lpOD::Matrix;
14 use base 'ODF::lpOD::Element';
15 our $VERSION = '1.002';
16 use constant PACKAGE_DATE => '2011-06-06T08:38:04';
17 use ODF::lpOD::Common;
18 use feature ':5.10';
19 #-----------------------------------------------------------------------------
20  
21 use constant ROW_FILTER => 'table:table-row';
22 use constant COLUMN_FILTER => 'table:table-column';
23 use constant CELL_FILTER => qr'table:(covered-|)table-cell';
24 use constant TABLE_FILTER => 'table:table';
25  
26 #-----------------------------------------------------------------------------
27  
28 sub set_group
29 {
30 my $self = shift;
31 my $type = shift;
32 my $start = shift;
33 my $end = shift;
34 unless ($start && $end)
35 {
36 alert "Range not valid"; return FALSE;
37 }
38 if ($start->after($end))
39 {
40 alert "Start element is not before end element";
41 return FALSE;
42 }
43 unless ($start->is_child($self) && $end->is_child($self))
44 {
45 alert "Grouping not allowed"; return FALSE;
46 }
47 my $tag = ($type eq 'column' || $type eq 'row') ?
48 'table:table-' . $type . '-group' :
49 'table:table-' . $type;
50 my $group = ODF::lpOD::Element->create($tag);
51 $group->paste_before($start);
52 my @elts = (); my $e = $start;
53 do {
54 push @elts, $e;
55 $e = $e->next_sibling;
56 }
57 while ($e && ! $e->after($end));
58 $group->group(@elts);
59 my %opt = @_;
60 $group->set_attribute('display', odf_boolean($opt{display}));
61 return $group;
62 }
63  
64 sub get_group
65 {
66 my $self = shift;
67 my $type = shift;
68 my $position = shift;
69 return $self->child($position, 'table:table-' . $type . '-group');
70 }
71  
72 #-----------------------------------------------------------------------------
73  
74 sub get_size
75 {
76 my $self = shift;
77 my $height = 0;
78 my $width = 0;
79 my $row = $self->first_row;
80 my $max_h = $self->att('#lpod:h');
81 my $max_w = $self->att('#lpod:w');
82 while ($row)
83 {
84 $height += $row->get_repeated;
85 if (wantarray)
86 {
87 my $row_width = $row->get_width;
88 $width = $row_width if $row_width > $width;
89 }
90 last if ((defined $max_h) and ($height >= $max_h));
91 $row = $row->next($self);
92 }
93 $height = $max_h if defined $max_h and $max_h < $height;
94 return wantarray ? ($height, $width) : $height;
95 }
96  
97 sub contains
98 {
99 my $self = shift;
100 my $expr = shift;
101 my $segment = $self->first_descendant(TEXT_SEGMENT);
102 while ($segment)
103 {
104 my %r = ();
105 my $t = $segment->get_text;
106 return $segment if $t =~ /$expr/;
107 $segment = $segment->next_elt($self, TEXT_SEGMENT);
108 }
109 return FALSE;
110 }
111  
112 sub table
113 {
114 my $self = shift;
115 return $self->is(TABLE_FILTER) ? $self : $self->parent(TABLE_FILTER);
116 }
117  
118 #=============================================================================
119 package ODF::lpOD::ColumnGroup;
120 use base 'ODF::lpOD::Matrix';
121 our $VERSION = '1.003';
122 use constant PACKAGE_DATE => '2011-06-06T08:32:22';
123 use ODF::lpOD::Common;
124 #-----------------------------------------------------------------------------
125  
126 sub _create { ODF::lpOD::ColumnGroup->create(@_) }
127  
128 #-----------------------------------------------------------------------------
129  
130 sub create
131 {
132 my $caller = shift;
133 return ODF::lpOD::Element->create('table:table-column-group', @_);
134 }
135  
136 #-----------------------------------------------------------------------------
137  
138 sub get_size
139 {
140 my $self = shift;
141 return $self->SUPER::get_size if $self->isa('ODF::lpOD::Table');
142 my $width = $self->get_column_count;
143 my $t = $self->table;
144 my $height = $t ? $t->get_length : undef;
145 return wantarray ? ($height, $width) : $height;
146 }
147  
148 sub all_columns
149 {
150 my $self = shift;
151 return $self->descendants(ODF::lpOD::Matrix->COLUMN_FILTER);
152 }
153  
154 sub first_column
155 {
156 my $self = shift;
157 my $elt = $self->first_child(qr'(column$|column-group)')
158 or return undef;
159 if ($elt->isa('ODF::lpOD::Column'))
160 { return $elt; }
161 elsif ($elt->isa('ODF::lpOD::ColumnGroup'))
162 { return $elt->first_column; }
163 else
164 { return undef; }
165 }
166  
167 sub last_column
168 {
169 my $self = shift;
170 my $elt = $self->last_child(qr'(column$|column-group)')
171 or return undef;
172 if ($elt->isa('ODF::lpOD::Column'))
173 { return $elt; }
174 elsif ($elt->isa('ODF::lpOD::ColumnGroup'))
175 { return $elt->last_column; }
176 else
177 { return undef; }
178 }
179  
180 sub get_column_count
181 {
182 my $self = shift;
183 my $count = 0;
184 my $col = $self->first_column;
185 my $max_w = $self->att('#lpod:w');
186 while ($col)
187 {
188 $count += $col->get_repeated;
189 $col = $col->next($self);
190 }
191 return (defined $max_w and $max_w < $count) ? $max_w : $count;
192 }
193  
194 sub get_position
195 {
196 my $self = shift;
197 my $start = $self->first_column;
198 return $start ? $start->get_position : undef;
199 }
200  
201 sub _get_column
202 {
203 my $self = shift;
204 my $position = shift;
205 my $col = $self->first_column or return undef;
206 for (my $i = 0 ; $i < $position ; $i++)
207 {
208 $col = $col->next($self) or return undef;
209 }
210 return $col;
211 }
212  
213 #-----------------------------------------------------------------------------
214  
215 sub get_column
216 {
217 my $self = shift;
218 my $position = alpha_to_num(shift) || 0;
219 my $width = $self->get_column_count;
220 my $max_w = $self->get_attribute('#lpod:w');
221 my $filter = ODF::lpOD::Matrix->COLUMN_FILTER;
222 if ($position < 0)
223 {
224 $position += $width;
225 }
226 if (($position >= $width) || ($position < 0))
227 {
228 alert "Column position $position out of range";
229 return undef;
230 }
231 my $col = $self->first_column or return undef;
232  
233 my $p = $position;
234 my $r = $col->get_repeated;
235 while ($p >= $r)
236 {
237 $p -= $r;
238 $col = $col->next($self);
239 $r = $col->get_repeated;
240 }
241 if ($self->rw and $col->repeat($r, $p))
242 {
243 $col = $self->get_column($position);
244 }
245  
246 return $col;
247 }
248  
249 sub get_columns
250 {
251 my $self = shift;
252 my $arg = shift;
253 my ($start, $end);
254 if ($arg)
255 {
256 ($start, $end) = translate_range($arg, shift);
257 }
258 $start //= 0; $end //= $self->get_column_count() - 1;
259 my @list = ();
260  
261 if ($self->ro)
262 {
263 my $col = $self->get_column($start);
264 my $n = $end - $start;
265 while ($n >= 0)
266 {
267 my $r = $col->get_repeated;
268 while ($r > 0 && $n >= 0)
269 {
270 push @list, $col;
271 $r--; $n--;
272 }
273 $col = $col->next($self);
274 }
275 }
276 else
277 {
278 for (my $i = $start ; $i <= $end ; $i++)
279 {
280 push @list, $self->get_column($i);
281 }
282 }
283 return @list;
284 }
285  
286 sub add_column
287 {
288 my $self = shift;
289 my %opt = process_options
290 (
291 number => 1,
292 propagate => TRUE,
293 @_
294 );
295 my $expand = $opt{expand};
296 my $propagate = $opt{propagate};
297 my $empty = $opt{empty};
298 my $cell_style = $opt{cell_style};
299 my $style = $opt{style};
300 my $col_filter = ODF::lpOD::Matrix->COLUMN_FILTER;
301 my $row_filter = ODF::lpOD::Matrix->ROW_FILTER;
302 my $position = undef;
303 my $ref_elt = $opt{before} // $opt{after};
304 my $set_style = exists $opt{style};
305 my $set_cell_style = exists $opt{cell_style};
306 unless (defined $ref_elt)
307 {
308 $position = 'after';
309 }
310 else
311 {
312 if (defined $opt{before} && defined $opt{after})
313 {
314 alert "'before' and 'after' are mutually exclusive";
315 return FALSE;
316 }
317 $position = defined $opt{before} ? 'before' : 'after';
318 $ref_elt = $self->get_column($ref_elt) unless ref $ref_elt;
319 unless (
320 $ref_elt->isa('ODF::lpOD::Column')
321 &&
322 $ref_elt->parent() == $self
323 )
324 {
325 alert "Wrong $position reference";
326 return FALSE;
327 }
328 }
329 my $number = $opt{number};
330 return undef unless $number && ($number > 0);
331 delete @opt
332 {qw
333 (number before after expand propagate empty style cell_style)
334 };
335 my $elt;
336 unless ($ref_elt)
337 {
338 my $proto = $self->last_child($col_filter);
339 if ($proto)
340 {
341 $elt = $proto->clone;
342 $elt->paste_after($proto);
343 }
344 else
345 {
346 $elt = ODF::lpOD::Column->create(%opt);
347 $elt->paste_first_child($self);
348 }
349 }
350 else
351 {
352 $elt = $ref_elt->clone;
353 $elt->paste($position, $ref_elt);
354 }
355 $elt->set_style($style) if $set_style;
356 if ($number && $number > 1)
357 {
358 if (is_true($expand))
359 {
360 $elt->set_repeated(undef);
361 $elt->repeat($number);
362 }
363 else
364 {
365 $elt->set_repeated($number);
366 }
367 }
368 else
369 {
370 $elt->set_repeated(undef);
371 }
372 if (is_true($propagate))
373 {
374 my $context = $self;
375 my %opt =
376 (
377 number => $number,
378 expand => $expand,
379 empty => $empty
380 );
381 $opt{style} = $cell_style if $set_cell_style;
382 my $hz_pos = $elt->get_position;
383 $hz_pos-- if $position eq 'after';
384 unless ($self->isa('ODF::lpOD::Table'))
385 {
386 $context = $self->parent('table:table');
387 }
388 foreach my $row ($context->descendants($row_filter))
389 {
390 if ($position && ($hz_pos < $row->get_width))
391 {
392 $opt{$position} = $row->get_cell($hz_pos);
393 }
394 else
395 {
396 delete $opt{$position};
397 }
398 $row->add_cell(%opt);
399 }
400 }
401 return $elt;
402 }
403  
404 sub delete_column
405 {
406 my $self = shift;
407 my $position = shift;
408 my %opt =
409 (
410 propagate => TRUE,
411 @_
412 );
413 my $column;
414 unless (ref $position)
415 {
416 $column = $self->get_column($position);
417 }
418 else
419 {
420 $column = $position;
421 unless (
422 $column->isa('ODF::lpOD::Column')
423 &&
424 $column->parent() == $self
425 )
426 {
427 alert "Column can't be deleted in this context";
428 return FALSE;
429 }
430 $position = $column->get_position;
431 }
432 unless ($column && defined $position)
433 {
434 alert "Wrong column position"; return FALSE;
435 }
436 $column->ODF::lpOD::Element::delete;
437 if ($opt{propagate})
438 {
439 my $row_filter = ODF::lpOD::Matrix->ROW_FILTER;
440 my $context = $self;
441 unless ($self->isa('ODF::lpOD::Table'))
442 {
443 $context = $self->parent('table:table');
444 }
445 foreach my $row ($context->descendants($row_filter))
446 {
447 my $cell = $row->get_cell($position);
448 $cell && $cell->delete;
449 }
450 }
451 return TRUE;
452 }
453  
454 sub set_column_group
455 {
456 my $self = shift;
457 my ($start, $end) = translate_range(shift, shift);
458 my $e1 = $self->get_column($start);
459 my $e2 = $self->get_column($end);
460 return $self->set_group('column', $e1, $e2, @_);
461 }
462  
463 sub get_column_group
464 {
465 my $self = shift;
466 return $self->get_group('column', @_);
467 }
468  
469 sub get_cell
470 {
471 my $self = shift;
472 my ($r, $c) = translate_coordinates(@_);
473 my $col = $self->get_column($c) or return undef;
474 return $col->get_cell($r);
475 }
476  
477 sub get_cells
478 {
479 my $self = shift;
480 my ($r1, $c1, $r2, $c2) = translate_range(@_);
481 my @cells = (); my $i = 0;
482  
483 foreach my $col ($self->get_columns($c1, $c2))
484 {
485 @{$cells[$i]} = $col->get_cells($c1, $c2); $i++;
486 }
487 return @cells;
488 }
489  
490 sub collapse
491 {
492 my $self = shift;
493 $_->set_visibility('collapse') for $self->get_columns;
494 }
495  
496 sub uncollapse
497 {
498 my $self = shift;
499 $_->set_visibility(undef) for $self->get_columns;
500 }
501  
502 sub set_default_cell_style
503 {
504 my $self = shift;
505 my $style = shift;
506 $_->set_default_cell_style($style) for $self->all_columns;
507 }
508  
509 #-----------------------------------------------------------------------------
510  
511 sub clear
512 {
513 my $self = shift;
514 my %opt = @_;
515 $_->clear for $self->get_columns($opt{start}, $opt{end});
516 }
517  
518 #=============================================================================
519 package ODF::lpOD::RowGroup;
520 use base 'ODF::lpOD::Matrix';
521 our $VERSION = '1.005';
522 use constant PACKAGE_DATE => '2011-06-06T08:37:31';
523 use ODF::lpOD::Common;
524 #-----------------------------------------------------------------------------
525  
526 sub _create { ODF::lpOD::RowGroup->create(@_) }
527  
528 #-----------------------------------------------------------------------------
529  
530 sub create
531 {
532 my $caller = shift;
533 return ODF::lpOD::Element->create('table:table-row-group', @_);
534 }
535  
536 #-----------------------------------------------------------------------------
537  
538 sub read_optimize
539 {
540 my $self = shift;
541 return $self->ro(shift);
542 }
543  
544 sub all_rows
545 {
546 my $self = shift;
547 return $self->descendants(ODF::lpOD::Matrix->ROW_FILTER);
548 }
549  
550 sub all_cells
551 {
552 my $self = shift;
553 return $self->descendants(ODF::lpOD::Matrix->CELL_FILTER);
554 }
555  
556 sub empty_cells
557 {
558 my $self = shift;
559 $_->empty_cells for $self->all_rows;
560 }
561  
562 sub clean
563 {
564 my $self = shift;
565 $_->clean() for $self->descendants(ODF::lpOD::Matrix->ROW_FILTER);
566 }
567  
568 sub first_row
569 {
570 my $self = shift;
571 my $elt = $self->first_child(qr'(row$|row-group)') or return undef;
572 if ($elt->isa('ODF::lpOD::Row'))
573 { return $elt; }
574 elsif ($elt->isa('ODF::lpOD::RowGroup'))
575 { return $elt->first_row; }
576 else
577 { return undef; }
578 }
579  
580 sub last_row
581 {
582 my $self = shift;
583 my $elt = $self->last_child(qr'(row$|row-group)') or return undef;
584 if ($elt->isa('ODF::lpOD::Row'))
585 { return $elt; }
586 elsif ($elt->isa('ODF::lpOD::RowGroup'))
587 { return $elt->last_row; }
588 else
589 { return undef; }
590 }
591  
592 sub get_height
593 {
594 my $self = shift;
595 my $height = 0;
596 my $row = $self->first_row;
597 my $max_h = $self->att('#lpod:h');
598 while ($row)
599 {
600 $height += $row->get_repeated;
601 $row = $row->next($self);
602 }
603 return (defined $max_h and $max_h < $height) ? $max_h : $height;
604 }
605  
606 sub get_position
607 {
608 my $self = shift;
609 my $start = $self->first_row;
610 return $start ? $start->get_position : undef;
611 }
612  
613 #-----------------------------------------------------------------------------
614  
615 sub _get_row
616 {
617 my $self = shift;
618 my $position = shift;
619 my $row = $self->first_row or return undef;
620 for (my $i = 0 ; $i < $position ; $i++)
621 {
622 $row = $row->next($self) or return undef;
623 }
624 return $row;
625 }
626  
627 sub get_row
628 {
629 my $self = shift;
630 my $position = alpha_to_num(shift) || 0;
631 my $height = $self->get_height;
632 my $max_h = $self->att('#lpod:h');
633 unless (is_numeric($position))
634 {
635 $position = alpha_to_num($position);
636 }
637 if ($position < 0)
638 {
639 $position += $height;
640 }
641 if (($position >= $height) || ($position < 0))
642 {
643 alert "Row position $position out of range";
644 return undef;
645 }
646 my $row = $self->first_row or return undef;
647 my $p = $position;
648 my $r = $row->get_repeated;
649 while ($p >= $r)
650 {
651 $p -= $r;
652 $row = $row->next($self);
653 $r = $row->get_repeated;
654 }
655 if ($self->rw and $row->repeat($r, $p))
656 {
657 $row = $self->get_row($position);
658 }
659  
660 return $row;
661 }
662  
663 sub get_rows
664 {
665 my $self = shift;
666 my $arg = shift;
667 my ($start, $end);
668 if ($arg)
669 {
670 ($start, $end) = translate_range($arg, shift);
671 }
672 $start //= 0; $end //= $self->get_size() - 1;
673 my @list = ();
674  
675 if ($self->ro)
676 {
677 my $row = $self->get_row($start);
678 my $n = $end - $start;
679 while ($n >= 0 && $row)
680 {
681 my $r = $row->get_repeated;
682 while ($r > 0 && $n >= 0)
683 {
684 push @list, $row;
685 $r--; $n--;
686 }
687 $row = $row->next($self);
688 }
689 }
690 else
691 {
692 for (my $i = $start ; $i <= $end ; $i++)
693 {
694 push @list, $self->get_row($i);
695 }
696 }
697 return @list;
698 }
699  
700 sub get_rows_by_index
701 {
702 my $self = shift;
703 my $index = shift;
704 my $filter = shift;
705 my @rownums = ();
706 unless (defined $index)
707 {
708 alert "Missing arguments"; return undef;
709 }
710 my $pos = 0;
711 my $match = FALSE;
712 my $old_status = $self->read_optimize;
713 $self->read_optimize(TRUE);
714 for my $row ($self->get_rows)
715 {
716 my $cell = $row->get_cell($index) or next;
717 my $v = $cell->get_value;
718 unless (defined $v)
719 {
720 $match = TRUE unless (defined $filter);
721 }
722 else
723 {
724 #$match = TRUE if $v ~~ $filter;
725 $match = TRUE if fake_smartmatch($v, $filter);
726 }
727 if ($match)
728 {
729 if (wantarray)
730 {
731 push @rownums, $pos;
732 $match = FALSE;
733 }
734 else
735 {
736 $self->read_optimize($old_status);
737 return $self->get_row($pos);
738 }
739 }
740 $pos++;
741 }
742 $self->read_optimize($old_status);
743 my @rows = ();
744 push @rows, $self->get_row($_) for @rownums;
745 return @rows;
746 }
747  
748 sub get_row_by_index
749 {
750 my $self = shift;
751 return scalar $self->get_rows_by_index(@_);
752 }
753  
754 sub add_row
755 {
756 my $self = shift;
757 my %opt = process_options
758 (
759 number => 1,
760 expand => TRUE,
761 @_
762 );
763 my $empty = $opt{empty};
764 my $cell_style = $opt{cell_style};
765 my $style = $opt{style};
766 my $ref_elt = $opt{before} // $opt{after};
767 my $expand = $opt{expand};
768 my $set_style = exists $opt{style};
769 my $set_cell_style = exists $opt{cell_style};
770 my $position = undef;
771 unless (defined $ref_elt)
772 {
773 $position = 'after';
774 }
775 else
776 {
777 if (defined $opt{before} && defined $opt{after})
778 {
779 alert "'before' and 'after' are mutually exclusive";
780 return FALSE;
781 }
782 $position = defined $opt{before} ? 'before' : 'after';
783 $ref_elt = $self->get_row($ref_elt) unless ref $ref_elt;
784 unless (
785 $ref_elt->isa('ODF::lpOD::Row')
786 &&
787 $ref_elt->parent() == $self
788 )
789 {
790 alert "Wrong $position reference";
791 return FALSE;
792 }
793 }
794 my $number = $opt{number};
795 return undef unless $number && ($number > 0);
796 delete @opt{qw(number before after expand empty style cell_style)};
797 my $elt;
798 unless ($ref_elt)
799 {
800 my $proto = $self->last_child(ODF::lpOD::Matrix->ROW_FILTER);
801 $elt = $proto ?
802 $proto->clone() : ODF::lpOD::Row->create(%opt);
803 }
804 else
805 {
806 $elt = $ref_elt->clone;
807 }
808 $elt->empty_cells if is_true($empty);
809 $elt->set_style($style) if $set_style;
810 if ($set_cell_style)
811 {
812 $_->set_style($cell_style) for $elt->all_cells;
813 }
814 if ($ref_elt)
815 {
816 $elt->paste($position, $ref_elt);
817 }
818 else
819 {
820 $elt->paste_last_child($self);
821 }
822 if ($number && $number > 1)
823 {
824 if (is_true($expand))
825 {
826 $elt->set_repeated(undef);
827 $elt->repeat($number);
828 }
829 else
830 {
831 $elt->set_repeated($number);
832 }
833 }
834 else
835 {
836 $elt->set_repeated(undef);
837 }
838  
839 return $elt;
840 }
841  
842 sub delete_row
843 {
844 my $self = shift;
845 my $position = shift;
846 my $row;
847 if (ref $position)
848 {
849 $row = $position;
850 unless (
851 $row->isa('ODF::lpOD::Row')
852 &&
853 $row->parent() == $self
854 )
855 {
856 alert "Row can't be deleted in this context";
857 return FALSE;
858 }
859 }
860 else
861 {
862 $row = $self->get_row($position);
863 }
864 $row->delete;
865 }
866  
867 sub set_row_group
868 {
869 my $self = shift;
870 my ($start, $end) = translate_range(shift, shift);
871 my $e1 = $self->get_row($start);
872 my $e2 = $self->get_row($end);
873 return $self->set_group('row', $e1, $e2, @_);
874 }
875  
876 sub get_row_group
877 {
878 my $self = shift;
879 return $self->get_group('row', @_);
880 }
881  
882 sub get_cell
883 {
884 my $self = shift;
885 my ($r, $c) = translate_coordinates(@_);
886 my $row = $self->get_row($r) or return undef;
887 return $row->get_cell($c);
888 }
889  
890 sub get_cells
891 {
892 my $self = shift;
893 my ($r1, $c1, $r2, $c2) = translate_range(@_);
894 my @cells = (); my $i = 0;
895 foreach my $row ($self->get_rows($r1, $r2))
896 {
897 @{$cells[$i]} = $row->get_cells($c1, $c2); $i++;
898 }
899 return @cells;
900 }
901  
902 sub get_cell_values
903 {
904 my $self = shift;
905 my $type = shift;
906 unless ($type)
907 {
908 alert "Missing cell data type"; return undef;
909 }
910 my ($r1, $c1, $r2, $c2) = translate_range(@_);
911 my $old_status = $self->read_optimize;
912 $self->read_optimize(TRUE);
913 my $row;
914 if (wantarray)
915 {
916 my @values = ();
917 my $i = 0;
918 foreach $row ($self->get_rows($r1, $r2))
919 {
920 foreach my $cell ($row->get_cells($c1, $c2))
921 {
922 my ($v, $t) = $cell->get_value;
923 $v = undef unless
924 $t eq $type or $type eq 'all';
925 push @{$values[$i]}, $v;
926 }
927 $i++;
928 }
929 $self->read_optimize($old_status);
930 return @values;
931 }
932 else
933 {
934 my @cells = ();
935 foreach $row ($self->get_rows($r1, $r2))
936 {
937 push @cells, $row->get_cells($c1, $c2);
938 }
939 $self->read_optimize($old_status);
940 return scalar
941 $self->ODF::lpOD::TableElement::_get_cell_values
942 ($type, @cells);
943 }
944 }
945  
946 sub get_text
947 {
948 my $self = shift;
949 my %opt = @_;
950 return $self->ODF::lpOD::Element::get_text(%opt)
951 if is_true($opt{recursive});
952 my $text;
953 my $old_status = $self->read_optimize;
954 $self->read_optimize(TRUE);
955 for my $row ($self->get_rows)
956 {
957 for ($row->get_cells)
958 {
959 my $t = $_->get_value;
960 $text .= $t if defined $t;
961 }
962 }
963 $self->read_optimize($old_status);
964 return $text;
965 }
966  
967 sub collapse
968 {
969 my $self = shift;
970 $_->set_visibility('collapse') for $self->get_rows;
971 }
972  
973 sub uncollapse
974 {
975 my $self = shift;
976 $_->set_visibility('visible') for $self->get_rows;
977 }
978  
979 sub set_default_cell_style
980 {
981 my $self = shift;
982 my $style = shift;
983 $_->set_default_cell_style($style) for $self->all_rows;
984 }
985  
986 #-----------------------------------------------------------------------------
987  
988 sub clear
989 {
990 my $self = shift;
991 my %opt = @_;
992 if (is_true($opt{compact}))
993 {
994 my ($height, $width) = $self->get_size;
995 $self->ODF::lpOD::Element::clear;
996 my $row = ODF::lpOD::Row->create;
997 $row->set_repeated($height);
998 $row->paste_first_child($self);
999 my $cell = ODF::lpOD::Cell->create;
1000 $cell->set_repeated($width);
1001 $cell->paste_first_child($row);
1002 }
1003 else
1004 {
1005 $_->clear(%opt) for $self->all_rows;
1006 }
1007 }
1008  
1009 #=============================================================================
1010 # Tables
1011 #-----------------------------------------------------------------------------
1012 package ODF::lpOD::Table;
1013 use base ('ODF::lpOD::RowGroup', 'ODF::lpOD::ColumnGroup');
1014 our $VERSION = '1.002';
1015 use constant PACKAGE_DATE => '2011-06-06T08:46:58';
1016 use ODF::lpOD::Common;
1017 #=============================================================================
1018 #--- constructor -------------------------------------------------------------
1019  
1020 sub _create { ODF::lpOD::Table->create(@_) }
1021  
1022 #-----------------------------------------------------------------------------
1023  
1024 sub create
1025 {
1026 my $caller = shift;
1027 my $name = shift;
1028 unless ($name)
1029 {
1030 alert "Missing table name";
1031 return FALSE;
1032 }
1033  
1034 my %opt = process_options
1035 (
1036 style => undef,
1037 display => undef,
1038 protected => undef,
1039 key => undef,
1040 @_
1041 );
1042  
1043 my $width = $opt{width};
1044 my $height = $opt{length} // $opt{height};
1045 unless (defined $width && defined $height)
1046 {
1047 ($height, $width) = input_2d_value($opt{size}, "");
1048 }
1049 $width // 0; $height // 0;
1050 if ($width < 0 || $height < 0)
1051 {
1052 alert "Wrong table size ($height x $width)";
1053 return FALSE;
1054 }
1055  
1056 my $t = ODF::lpOD::Element->create('table:table');
1057 $t->set_attribute('name', $name);
1058 $t->set_attribute('style name', $opt{style});
1059 $t->set_attribute('protected', odf_boolean($opt{protected}));
1060 $t->set_attribute('protection key', $opt{key});
1061 $t->set_attribute('display', odf_boolean($opt{display}));
1062 $t->set_attribute('print', odf_boolean($opt{print}));
1063 $t->set_attribute('print ranges', $opt{print_ranges});
1064  
1065 $t->add_column(
1066 number => $width,
1067 expand => $opt{expand},
1068 propagate => FALSE
1069 );
1070 my $r = $t->add_row;
1071 unless (is_true($opt{expand}))
1072 {
1073 $r->add_cell()->set_repeated($width);
1074 $r->set_repeated($height);
1075 }
1076 else
1077 {
1078 $r->add_cell(number => $width, expand => TRUE);
1079 $r->repeat($height);
1080 }
1081  
1082 $t->set_default_cell_style($opt{cell_style}) if ($opt{cell_style});
1083  
1084 return $t;
1085 }
1086  
1087 #--- special optimization ----------------------------------------------------
1088  
1089 sub set_working_area
1090 {
1091 my $self = shift;
1092 my ($h, $w) = @_;
1093 $self->set_attribute('#lpod:h' => $h);
1094 $self->set_attribute('#lpod:w' => $w);
1095 }
1096  
1097 #-----------------------------------------------------------------------------
1098  
1099 sub set_column_header
1100 {
1101 my $self = shift;
1102 if ($self->get_column_header)
1103 {
1104 alert "Column header already defined for this table";
1105 return FALSE;
1106 }
1107 my $number = shift || 1;
1108 my $start = $self->get_row(0);
1109 my $end = $self->get_row($number > 1 ? $number-1 : 0);
1110 return $self->set_group('header-rows', $start, $end);
1111 }
1112  
1113 sub get_column_header
1114 {
1115 my $self = shift;
1116 return $self->first_child('table:table-header-rows');
1117 }
1118  
1119 sub set_row_header
1120 {
1121 my $self = shift;
1122 if ($self->get_row_header)
1123 {
1124 alert "Row header already defined for this table";
1125 return FALSE;
1126 }
1127 my $number = shift || 1;
1128 my $start = $self->get_column(0);
1129 my $end = $self->get_column($number > 1 ? $number-1 : 0);
1130 return $self->set_group('header-columns', $start, $end);
1131 }
1132  
1133 sub get_row_header
1134 {
1135 my $self = shift;
1136 return $self->first_child('table:table-header-columns');
1137 }
1138  
1139 sub set_default_cell_style
1140 {
1141 my $self = shift;
1142 my $style = shift;
1143 $_->set_default_cell_style($style)
1144 for ($self->all_rows, $self->all_columns);
1145 }
1146  
1147 #-----------------------------------------------------------------------------
1148  
1149 sub get_cell_value
1150 {
1151 my $self = shift;
1152 my $old_status = $self->read_optimize;
1153 my $cell = $self->get_cell(@_);
1154 $self->read_optimize($old_status);
1155 return wantarray ?
1156 ($cell->get_value(), $cell->get_type()) :
1157 $cell->get_value;
1158 }
1159  
1160 #=============================================================================
1161 package ODF::lpOD::TableElement;
1162 use base 'ODF::lpOD::Element';
1163 our $VERSION = '1.004';
1164 use constant PACKAGE_DATE => '2011-06-06T08:49:11';
1165 use ODF::lpOD::Common;
1166 #-----------------------------------------------------------------------------
1167  
1168 sub table
1169 {
1170 my $self = shift;
1171 return $self->get_ancestor('table:table');
1172 }
1173  
1174 sub tro
1175 {
1176 my $self = shift;
1177 my $table = $self->table or return FALSE;
1178 return is_true($table->ro);
1179 }
1180  
1181 sub trw
1182 {
1183 my $self = shift;
1184 my $table = $self->table or return TRUE;
1185 return is_false($table->ro);
1186 }
1187  
1188 sub repeat
1189 {
1190 my $self = shift;
1191 my $r = shift // $self->get_repeated;
1192 return undef unless $r > 1;
1193 my $p = shift;
1194 unless (defined $p)
1195 {
1196 $self->set_repeated(undef);
1197 return $self->SUPER::repeat($r);
1198 }
1199 else
1200 {
1201 if (($p < 0) or ($p >= $r))
1202 {
1203 return undef;
1204 }
1205 elsif ($p == 0)
1206 {
1207 $self->set_repeated(undef);
1208 my $c1 = $self->clone; $c1->paste_after($self);
1209 $c1->set_repeated($r - 1);
1210 }
1211 else
1212 {
1213 $self->set_repeated($p);
1214 my $c1 = $self->clone; $c1->paste_after($self);
1215 $c1->set_repeated(undef); $p++;
1216 if ($p < $r)
1217 {
1218 my $c2 = $c1->clone; $c2->paste_after($c1);
1219 $c2->set_repeated($r - $p);
1220 }
1221 }
1222  
1223 return TRUE;
1224 }
1225 }
1226  
1227 sub get_cell_value
1228 {
1229 my $self = shift;
1230 my $table = $self->table;
1231 unless ($table)
1232 {
1233 alert "Not in table"; return undef;
1234 }
1235 my $old_status = $table->read_optimize;
1236 my $cell = $self->get_cell(@_);
1237 $table->read_optimize($old_status);
1238 return wantarray ?
1239 ($cell->get_value(), $cell->get_type()) :
1240 $cell->get_value;
1241 }
1242  
1243 sub _get_cell_values
1244 {
1245 my $self = shift;
1246 my $type = shift;
1247 my $col = $self->isa('ODF::lpOD::Column') ? TRUE : FALSE;
1248 my ($sum, $min, $max);
1249 my $count = 0;
1250 my @values = ();
1251 my $cf = $ODF::lpOD::Common::COMPARE;
1252 for my $cell (@_)
1253 {
1254 my ($v, $t) = $cell->get_value;
1255 next unless defined $v;
1256 next unless (($t eq $type) || ($type eq 'all'));
1257 my $rep = $col ? 1 : $cell->get_repeated;
1258 $count += $rep;
1259 if (wantarray)
1260 {
1261 push @values, $v;
1262 }
1263 else
1264 {
1265 $min //= $v; $max //= $v;
1266 if ($type eq 'string' || $type eq 'all')
1267 {
1268 $min = $v if &$cf($min, $v) > 0;
1269 $max = $v if &$cf($max, $v) < 0;
1270 }
1271 elsif ($type eq 'date' || $type eq 'time')
1272 {
1273 $min = $v if $min gt $v;
1274 $max = $v if $max lt $v;
1275 }
1276 elsif ($type eq 'float' || $type eq 'currency' || $type eq 'percentage')
1277 {
1278 $min = $v if $min > $v;
1279 $max = $v if $max < $v;
1280 $sum += ($v * $rep);
1281 }
1282 elsif ($type eq 'boolean')
1283 {
1284 if (is_true($v)) { $min++ }
1285 else { $max++ }
1286 }
1287 }
1288 }
1289 return wantarray ? @values : [ $count, $min, $max, $sum ];
1290 }
1291  
1292 sub get_cell_values
1293 {
1294 my $self = shift;
1295 my $table = $self->table;
1296 unless ($table)
1297 {
1298 alert "Not in table"; return undef;
1299 }
1300 my $type = shift;
1301 unless ($type)
1302 {
1303 alert "Missing cell data type"; return undef;
1304 }
1305 my $old_status = $table->read_optimize;
1306 $table->read_optimize(TRUE);
1307 my @cells = $self->get_cells(@_);
1308 $table->read_optimize($old_status);
1309 return $self->_get_cell_values($type, @cells);
1310 }
1311  
1312 sub set_default_cell_style
1313 {
1314 my $self = shift;
1315 $self->set_attribute('table:default-cell-style-name' => shift);
1316 }
1317  
1318 #-----------------------------------------------------------------------------
1319  
1320 sub get_position
1321 {
1322 my $self = shift;
1323 my $parent = $self->table;
1324 unless ($parent)
1325 {
1326 alert "Missing or wrong table attachment";
1327 return undef;
1328 }
1329 my $position = 0;
1330 my $elt = $self->previous($parent);
1331 while ($elt)
1332 {
1333 $position += ($elt->get_repeated() // 1);
1334 $elt = $elt->previous($parent);
1335 }
1336 return wantarray ? ($parent->get_name, $position) : $position;
1337 }
1338  
1339 #-----------------------------------------------------------------------------
1340  
1341 sub clear
1342 {
1343 my $self = shift;
1344 my %opt = @_;
1345 my $rep = $self->get_repeated;
1346 my $style = $self->get_style;
1347 $self->del_attributes;
1348 $self->set_repeated($rep);
1349 $self->set_style($style);
1350 }
1351  
1352 #=============================================================================
1353 # Table columns
1354 #-----------------------------------------------------------------------------
1355 package ODF::lpOD::Column;
1356 use base 'ODF::lpOD::TableElement';
1357 our $VERSION = '1.004';
1358 use constant PACKAGE_DATE => '2011-06-06T08:53:28';
1359 use ODF::lpOD::Common;
1360 #-----------------------------------------------------------------------------
1361  
1362 sub _create { ODF::lpOD::Column->create(@_) }
1363  
1364 #-----------------------------------------------------------------------------
1365  
1366 sub create
1367 {
1368 my $caller = shift;
1369 my %opt = process_options
1370 (
1371 style => undef,
1372 @_
1373 );
1374  
1375 my $col = ODF::lpOD::Element->create('table:table-column')
1376 or return undef;
1377 $col->set_attribute('style name', $opt{style})
1378 if defined $opt{style};
1379 $col->set_default_cell_style($opt{cell_style})
1380 if defined $opt{cell_style};
1381 delete @opt{qw(style cell_style)};
1382 foreach my $a (keys %opt)
1383 {
1384 $col->set_attribute($a, $opt{$a});
1385 }
1386 return $col;
1387 }
1388  
1389 #-----------------------------------------------------------------------------
1390  
1391 sub delete
1392 {
1393 my $self = shift;
1394 my $parent = $self->parent;
1395 if ($parent && $parent->isa('ODF::lpOD::ColumnGroup'))
1396 {
1397 return $parent->delete_column($self, @_);
1398 }
1399 else
1400 {
1401 return $self->SUPER::delete;
1402 }
1403 }
1404  
1405 sub clear
1406 {
1407 my $self = shift;
1408 $_->clear for $self->get_cells;
1409 $self->SUPER::clear;
1410 }
1411  
1412 #-----------------------------------------------------------------------------
1413  
1414 sub get_length
1415 {
1416 my $self = shift;
1417 my $parent = $self->table;
1418 unless ($parent)
1419 {
1420 alert "No defined length for a non attached column";
1421 return undef;
1422 }
1423 return scalar $parent->get_height;
1424 }
1425  
1426 sub get_repeated
1427 {
1428 my $self = shift;
1429 return $self->get_attribute('table:number-columns-repeated') // 1;
1430 }
1431  
1432 sub set_repeated
1433 {
1434 my $self = shift;
1435 my $rep = shift;
1436 $rep = undef unless $rep && $rep > 1;
1437 return $self->set_attribute('table:number-columns-repeated' => $rep);
1438 }
1439  
1440 sub next
1441 {
1442 my $self = shift;
1443 my $context = shift || $self->table;
1444 my $filter = shift || qr'column';
1445 my $elt = $self->next_elt($context, $filter);
1446 while ($elt)
1447 {
1448 if ($elt->isa('ODF::lpOD::Column'))
1449 {
1450 return $elt;
1451 }
1452 elsif ($elt->isa('ODF::lpOD::ColumnGroup'))
1453 {
1454 my $n = $elt->first_column;
1455 return $n if $n;
1456 }
1457 $elt = $elt->next_elt($context, $filter);
1458 }
1459 return undef;
1460 }
1461  
1462 sub previous
1463 {
1464 my $self = shift;
1465 my $context = shift || $self->table;
1466 my $filter = shift || ODF::lpOD::Matrix->COLUMN_FILTER;
1467 my $elt = $self->prev_elt($context, $filter);
1468 while ($elt)
1469 {
1470 if ($elt->isa('ODF::lpOD::Column'))
1471 {
1472 return $elt;
1473 }
1474 elsif ($elt->isa('ODF::lpOD::ColumnGroup'))
1475 {
1476 my $n = $elt->last_column();
1477 return $n if $n;
1478 }
1479 $elt = $elt->prev_elt($context, $filter);
1480 }
1481 return undef;
1482 }
1483  
1484 sub set_cell_style
1485 {
1486 my $self = shift;
1487 my $style = shift;
1488 $_->set_style($style) for $self->get_cells;
1489 }
1490  
1491 #-----------------------------------------------------------------------------
1492  
1493 sub get_cell
1494 {
1495 my $self = shift;
1496 my $table = $self->table;
1497 unless ($table)
1498 {
1499 alert "Not in table"; return undef;
1500 }
1501 if ($self->get_repeated() > 1)
1502 {
1503 alert "Not supported in this mode"; return undef;
1504 }
1505 my $col_num = $self->get_position;
1506 my $row_num = alpha_to_num(shift) // 0;
1507 return $table->get_cell($row_num, $col_num);
1508 }
1509  
1510 sub get_cells
1511 {
1512 my $self = shift;
1513 my $table = $self->table;
1514 unless ($table)
1515 {
1516 alert "Not in table"; return undef;
1517 }
1518 if ($self->get_repeated() > 1)
1519 {
1520 alert "Not supported in this mode"; return undef;
1521 }
1522 my $arg = shift;
1523 my ($start, $end);
1524 unless ($arg)
1525 {
1526 $start = 0; $end = $self->get_length() - 1;
1527 }
1528 else
1529 {
1530 ($start, $end) = translate_range($arg, shift);
1531 }
1532 $start //= 0; $end //= $self->get_length() - 1;
1533 my @cells = ();
1534 for (my $i = $start ; $i <= $end ; $i++)
1535 {
1536 my $c = $self->get_cell($i) or last;
1537 push @cells, $c;
1538 }
1539 return @cells;
1540 }
1541  
1542 #=============================================================================
1543 # Table rows
1544 #-----------------------------------------------------------------------------
1545 package ODF::lpOD::Row;
1546 use base 'ODF::lpOD::TableElement';
1547 our $VERSION = '1.005';
1548 use constant PACKAGE_DATE => '2011-06-06T08:57:37';
1549 use ODF::lpOD::Common;
1550 #-----------------------------------------------------------------------------
1551  
1552 sub _create { ODF::lpOD::Row->create(@_) }
1553  
1554 #-----------------------------------------------------------------------------
1555  
1556 sub create
1557 {
1558 my $caller = shift;
1559 my %opt = process_options
1560 (
1561 style => undef,
1562 @_
1563 );
1564  
1565 my $row = ODF::lpOD::Element->create('table:table-row')
1566 or return undef;
1567 $row->set_attribute('style name', $opt{style})
1568 if defined $opt{style};
1569 $row->set_default_cell_style($opt{cell_style})
1570 if defined $opt{cell_style};
1571 delete @opt{qw(style cell_style)};
1572  
1573 foreach my $a (keys %opt)
1574 {
1575 $row->set_attribute($a, $opt{$a});
1576 }
1577 return $row;
1578 }
1579  
1580 #-----------------------------------------------------------------------------
1581  
1582 sub clean
1583 {
1584 my $self = shift;
1585 my $cell = $self->last_child(ODF::lpOD::Matrix->CELL_FILTER)
1586 or return undef;
1587 $cell->set_repeated(undef);
1588 }
1589  
1590 sub all_cells
1591 {
1592 my $self = shift;
1593 return $self->descendants(ODF::lpOD::Matrix->CELL_FILTER);
1594 }
1595  
1596 sub set_cell_style
1597 {
1598 my $self = shift;
1599 my $style = shift;
1600 $_->set_style($style) for $self->all_cells;
1601 }
1602  
1603 sub empty_cells
1604 {
1605 my $self = shift;
1606 foreach my $cell ($self->all_cells)
1607 {
1608 $cell->set_value(undef);
1609 $cell->set_type(undef);
1610 $cell->set_text(undef);
1611 }
1612 }
1613  
1614 #-----------------------------------------------------------------------------
1615  
1616 sub get_cell
1617 {
1618 my $self = shift;
1619 my $position = alpha_to_num(shift) || 0;
1620 my $width = $self->get_width;
1621 if ($position < 0)
1622 {
1623 $position += $width;
1624 }
1625 if (($position >= $width) || ($position < 0))
1626 {
1627 alert "Cell position $position out of range";
1628 return undef;
1629 }
1630 my $cell = $self->first_child(ODF::lpOD::Matrix->CELL_FILTER)
1631 or return undef;
1632  
1633 my $p = $position;
1634 my $r = $cell->get_repeated;
1635 while ($p >= $r)
1636 {
1637 $p -= $r;
1638 $cell = $cell->next($self);
1639 $r = $cell->get_repeated;
1640 }
1641 if ($self->trw and $cell->repeat($r, $p))
1642 {
1643 $cell = $self->get_cell($position);
1644 }
1645  
1646 return $cell;
1647 }
1648  
1649 sub get_cells
1650 {
1651 my $self = shift;
1652 my $arg = shift;
1653 my ($start, $end);
1654 unless ($arg)
1655 {
1656 $start = 0; $end = $self->get_width() - 1;
1657 }
1658 else
1659 {
1660 ($start, $end) = translate_range($arg, shift);
1661 }
1662 $start //= 0; $end //= $self->get_width() - 1;
1663 my @list = ();
1664 if ($self->tro)
1665 {
1666 my $cell = $self->get_cell($start);
1667 my $n = $end - $start;
1668 my $sp = $start - $cell->get_position;
1669 while ($n >= 0 && $cell)
1670 {
1671 my $r = $cell->get_repeated() - $sp;
1672 while ($r > 0 && $n >= 0)
1673 {
1674 push @list, $cell;
1675 $r--; $n--;
1676 }
1677 $cell = $cell->next
1678 (
1679 $self,
1680 ODF::lpOD::Matrix->CELL_FILTER
1681 );
1682 $sp = 0;
1683 }
1684 }
1685 else
1686 {
1687 for (my $i = $start ; $i <= $end ; $i++)
1688 {
1689 push @list, $self->get_cell($i);
1690 }
1691 }
1692  
1693 return @list;
1694 }
1695  
1696 sub get_text
1697 {
1698 my $self = shift;
1699 my %opt = @_;
1700 return $self->ODF::lpOD::Element::get_text(%opt)
1701 if is_true($opt{recursive});
1702 my $text;
1703 for ($self->get_cells)
1704 {
1705 my $t = $_->get_value;
1706 $text .= $t if defined $t;
1707 }
1708 return $text;
1709 }
1710  
1711 sub get_width
1712 {
1713 my $self = shift;
1714 my $width = 0;
1715 my $cell = $self->first_child(ODF::lpOD::Matrix->CELL_FILTER);
1716 my $tbl = $self->table;
1717 my $max_w = $tbl ? $tbl->att('#lpod:w') : undef;
1718 while ($cell)
1719 {
1720 $width += $cell->get_repeated;
1721 $cell = $cell->next;
1722 }
1723 return (defined $max_w and $max_w < $width) ? $max_w : $width;
1724 }
1725  
1726 sub clear
1727 {
1728 my $self = shift;
1729 my %opt = @_;
1730 if (is_true($opt{compact}))
1731 {
1732 my $width = $self->get_width;
1733 $self->ODF::lpOD::Element::clear;
1734 my $cell = ODF::lpOD::Cell->create;
1735 $cell->set_repeated($width);
1736 $cell->paste_first_child($self);
1737 }
1738 else
1739 {
1740 $_->clear for $self->all_cells;
1741 }
1742 return $self->SUPER::clear;
1743 }
1744  
1745 sub add_cell
1746 {
1747 my $self = shift;
1748 my %opt =
1749 (
1750 number => 1,
1751 @_
1752 );
1753 my $expand = $opt{expand};
1754 my $empty = $opt{empty};
1755 my $set_style = exists $opt{style};
1756 my $style = $opt{style};
1757 my $position = undef;
1758 my $ref_elt = $opt{before} // $opt{after};
1759 unless (defined $ref_elt)
1760 {
1761 $position = 'after';
1762 }
1763 else
1764 {
1765 if ($opt{before} && $opt{after})
1766 {
1767 alert "'before' and 'after' are mutually exclusive";
1768 return FALSE;
1769 }
1770 $position = defined $opt{before} ? 'before' : 'after';
1771 unless (
1772 $ref_elt->isa('ODF::lpOD::Cell')
1773 &&
1774 $ref_elt->parent() == $self
1775 )
1776 {
1777 alert "Wrong $position reference";
1778 return FALSE;
1779 }
1780 }
1781 my $number = $opt{number};
1782 return undef unless $number && ($number > 0);
1783 delete @opt{qw(number before after expand empty style)};
1784 my $elt;
1785 unless ($ref_elt)
1786 {
1787 my $proto = $self->last_child(ODF::lpOD::Matrix->CELL_FILTER);
1788 $elt = $proto ?
1789 $proto->clone() : ODF::lpOD::Cell->create(%opt);
1790 }
1791 else
1792 {
1793 $elt = $ref_elt->clone;
1794 }
1795 if ($ref_elt)
1796 {
1797 $elt->paste($position, $ref_elt);
1798 }
1799 else
1800 {
1801 $elt->paste_last_child($self);
1802 }
1803 $elt->set_style($style) if $set_style;
1804 $elt->set_attributes
1805 (
1806 'number columns spanned' => undef,
1807 'number rows spanned' => undef
1808 );
1809 if (is_true($empty))
1810 {
1811 $elt->set_value(undef);
1812 $elt->set_type(undef);
1813 $elt->set_text(undef);
1814 }
1815 if ($number && $number > 1)
1816 {
1817 if (is_true($expand))
1818 {
1819 $elt->repeat($number);
1820 }
1821 else
1822 {
1823 $elt->set_repeated($number);
1824 }
1825 }
1826 return $elt;
1827 }
1828  
1829 sub get_repeated
1830 {
1831 my $self = shift;
1832 return $self->get_attribute('table:number-rows-repeated') // 1;
1833 }
1834  
1835 sub set_repeated
1836 {
1837 my $self = shift;
1838 my $rep = shift;
1839 $rep = undef unless $rep && $rep > 1;
1840 return $self->set_attribute('table:number-rows-repeated' => $rep);
1841 }
1842  
1843 sub next
1844 {
1845 my $self = shift;
1846 my $context = shift || $self->table;
1847 my $filter = shift || qr'row';
1848 my $elt = $self->next_elt($context, $filter);
1849 while ($elt)
1850 {
1851 if ($elt->isa('ODF::lpOD::Row'))
1852 {
1853 return $elt;
1854 }
1855 elsif ($elt->isa('ODF::lpOD::RowGroup'))
1856 {
1857 my $n = $elt->first_row;
1858 return $n if $n;
1859 }
1860 $elt = $elt->next_elt($context, $filter);
1861 }
1862 }
1863  
1864 sub previous
1865 {
1866 my $self = shift;
1867 my $context = shift || $self->table;
1868 my $filter = shift || ODF::lpOD::Matrix->ROW_FILTER;
1869 my $elt = $self->prev_elt($context, $filter);
1870 while ($elt)
1871 {
1872 if ($elt->isa('ODF::lpOD::Row'))
1873 {
1874 if ($elt->parent->is('table:table-header-rows'))
1875 {
1876 return undef unless $self
1877 ->parent
1878 ->is('table:table-header-rows');
1879 }
1880 return $elt;
1881 }
1882 elsif ($elt->isa('ODF::lpOD::RowGroup'))
1883 {
1884 my $n = $elt->last_row();
1885 return $n if $n;
1886 }
1887 $elt = $elt->prev_elt($context, $filter);
1888 }
1889 return undef;
1890 }
1891  
1892 #=============================================================================
1893 # Table cells
1894 #-----------------------------------------------------------------------------
1895 package ODF::lpOD::Cell;
1896 use base ('ODF::lpOD::Field', 'ODF::lpOD::TableElement');
1897 our $VERSION = '1.005';
1898 use constant PACKAGE_DATE => '2012-01-24T08:09:35';
1899 use ODF::lpOD::Common;
1900 #-----------------------------------------------------------------------------
1901  
1902 BEGIN {
1903 *repeat = *ODF::lpOD::TableElement::repeat;
1904 *get_parent_row = *row;
1905 }
1906  
1907 #-----------------------------------------------------------------------------
1908 our %ATTRIBUTE;
1909 #-----------------------------------------------------------------------------
1910  
1911 sub _create { ODF::lpOD::Cell->create(@_) }
1912  
1913 #-----------------------------------------------------------------------------
1914  
1915 sub create
1916 {
1917 my $caller = shift;
1918 my $cell = ODF::lpOD::Field->create('table:table-cell', @_);
1919 return $cell ? bless($cell, __PACKAGE__) : undef;
1920 }
1921  
1922 #-----------------------------------------------------------------------------
1923  
1924 sub row
1925 {
1926 my $self = shift;
1927 return $self->parent('table:table-row');
1928 }
1929  
1930 sub column
1931 {
1932 my $self = shift;
1933 my $t = $self->table;
1934 unless ($t)
1935 {
1936 alert "Not in table"; return undef;
1937 }
1938 my $pos = $self->get_position or return undef;
1939 return $t->get_column($pos);
1940 }
1941  
1942 #-----------------------------------------------------------------------------
1943  
1944 sub insert_element
1945 {
1946 my $context = shift;
1947 my $e = shift;
1948 my %opt = @_;
1949 my $position = lc $opt{position} || 'first_child';
1950 if (UNIVERSAL::isa($e, "ODF::lpOD::Frame"))
1951 {
1952 if (my $doc = $context->document)
1953 {
1954 if ($doc->get_type() eq 'spreadsheet')
1955 {
1956 $e->paste($position => $context);
1957 }
1958 else
1959 {
1960 my $p = ODF::lpOD::Paragraph->create;
1961 $p->paste($position => $context);
1962 $e->paste_first_child($p);
1963 }
1964 return $e;
1965 }
1966 }
1967 return $context->SUPER::insert_element($e, %opt);
1968 }
1969  
1970 #-----------------------------------------------------------------------------
1971  
1972 sub get_repeated
1973 {
1974 my $self = shift;
1975 return $self->get_attribute('table:number-columns-repeated') // 1;
1976 }
1977  
1978 sub set_repeated
1979 {
1980 my $self = shift;
1981 my $rep = shift;
1982 $rep = undef unless $rep && $rep > 1;
1983 return $self->set_attribute('table:number-columns-repeated' => $rep);
1984 }
1985  
1986 sub is_covered
1987 {
1988 my $self = shift;
1989 my $tag = $self->get_tag;
1990 return $tag =~ /covered/ ? TRUE : FALSE;
1991 }
1992  
1993 sub next
1994 {
1995 my $self = shift;
1996 my $row = $self->row;
1997 unless ($row)
1998 {
1999 alert "Wrong context"; return FALSE;
2000 }
2001 return $self->next_elt($row, ODF::lpOD::Matrix->CELL_FILTER);
2002 }
2003  
2004 sub previous
2005 {
2006 my $self = shift;
2007 my $row = $self->row;
2008 unless ($row)
2009 {
2010 alert "Wrong context"; return FALSE;
2011 }
2012 return $self->prev_elt($row, ODF::lpOD::Matrix->CELL_FILTER);
2013 }
2014  
2015 sub get_position
2016 {
2017 my $self = shift;
2018 my $row = $self->row;
2019 unless ($row)
2020 {
2021 alert "Missing or wrong attachment";
2022 return FALSE;
2023 }
2024 my $position = 0;
2025 my $elt = $self->previous;
2026 while ($elt)
2027 {
2028 $position += $elt->get_repeated // 1;
2029 $elt = $elt->previous;
2030 }
2031 if (wantarray)
2032 {
2033 return (
2034 $row->get_position(),
2035 $position
2036 );
2037 }
2038 return $position;
2039 }
2040  
2041 #-----------------------------------------------------------------------------
2042  
2043 sub set_text
2044 {
2045 my $self = shift;
2046 my $text = shift;
2047 my %opt =
2048 (
2049 style => undef,
2050 @_
2051 );
2052 $self->cut_children(qr'^text|^table');
2053 return undef unless defined $text;
2054 $self->append_element
2055 (
2056 ODF::lpOD::Paragraph->create
2057 (text => $text, style => $opt{style})
2058 );
2059 }
2060  
2061 sub set_value
2062 {
2063 my $self = shift;
2064 return $self->get_type() ne 'string' ?
2065 $self->SUPER::set_value(@_) : $self->set_text(@_);
2066 }
2067  
2068 sub get_text
2069 {
2070 my $self = shift;
2071 return $self->ODF::lpOD::TextElement::get_text(@_);
2072 }
2073  
2074 sub get_content
2075 {
2076 my $self = shift;
2077 return $self->get_children_elements;
2078 }
2079  
2080 sub set_content
2081 {
2082 my $self = shift;
2083 $self->set_text;
2084 foreach my $elt (@_)
2085 {
2086 if (ref $elt && $elt->isa('ODF::lpOD::Element'))
2087 {
2088 $self->append_element($elt);
2089 }
2090 }
2091 }
2092  
2093 sub remove_span
2094 {
2095 my $self = shift;
2096 my $hspan = $self->get_attribute('number columns spanned') || 1;
2097 my $vspan = $self->get_attribute('number rows spanned') || 1;
2098 $self->del_attribute('number columns spanned');
2099 $self->del_attribute('number rows spanned');
2100 my $row = $self->parent(ODF::lpOD::Matrix->ROW_FILTER);
2101 my $table = $self->parent(ODF::lpOD::Matrix->TABLE_FILTER);
2102 my $vpos = $row->get_position;
2103 my $hpos = $self->get_position;
2104 my $vend = $vpos + $vspan - 1;
2105 my $hend = $hpos + $hspan - 1;
2106 ROW: for (my $i = $vpos ; $i <= $vend ; $i++)
2107 {
2108 my $cr = $table->get_row($i) or last ROW;
2109 CELL: for (my $j = $hpos ; $j <= $hend ; $j++)
2110 {
2111 my $covered = $cr->get_cell($j) or last CELL;
2112 next CELL if $covered == $self;
2113 $covered->set_tag('table:table-cell');
2114 $covered->set_atts($self->atts);
2115 }
2116 }
2117 return ($hspan, $vspan);
2118 }
2119  
2120 sub set_span
2121 {
2122 my $self = shift;
2123 if ($self->is_covered)
2124 {
2125 alert "Span expansion is not allowed for covered cells";
2126 return FALSE;
2127 }
2128 my %opt = @_;
2129 my $hspan = $opt{columns} // 1;
2130 my $vspan = $opt{rows} // 1;
2131 my $old_hspan = $self->get_attribute('number columns spanned') || 1;
2132 my $old_vspan = $self->get_attribute('number rows spanned') || 1;
2133 unless (($hspan > 1) || ($vspan > 1))
2134 {
2135 return $self->remove_span;
2136 }
2137 unless (($hspan != $old_hspan) || ($vspan != $old_vspan))
2138 {
2139 return ($old_vspan, $old_hspan);
2140 }
2141 $self->remove_span;
2142 $hspan = $old_hspan unless $hspan;
2143 $vspan = $old_vspan unless $vspan;
2144 my $row = $self->parent(ODF::lpOD::Matrix->ROW_FILTER);
2145 my $table = $self->parent(ODF::lpOD::Matrix->TABLE_FILTER);
2146 my $vpos = $row->get_position;
2147 my $hpos = $self->get_position;
2148 my $vend = $vpos + $vspan - 1;
2149 my $hend = $hpos + $hspan - 1;
2150 $self->set_attribute('number columns spanned', $hspan);
2151 $self->set_attribute('number rows spanned', $vspan);
2152 ROW: for (my $i = $vpos ; $i <= $vend ; $i++)
2153 {
2154 my $cr = $table->get_row($i) or last ROW;
2155 CELL: for (my $j = $hpos ; $j <= $hend ; $j++)
2156 {
2157 my $covered = $cr->get_cell($j) or last CELL;
2158 next CELL if $covered == $self;
2159 $_->move(last_child => $self)
2160 for $covered->get_content;
2161 $covered->remove_span;
2162 $covered->set_tag('table:covered-table-cell');
2163 }
2164 }
2165 return ($hspan, $vspan);
2166 }
2167  
2168 sub get_span
2169 {
2170 my $self = shift;
2171 return (
2172 $self->get_attribute('number rows spanned') // 1,
2173 $self->get_attribute('number columns spanned') // 1
2174 );
2175 }
2176  
2177 sub clear
2178 {
2179 my $self = shift;
2180 $self->remove_span if $self->table;
2181 my $rep = $self->get_repeated;
2182 my $style = $self->get_style;
2183 $self->del_attributes;
2184 $self->set_repeated($rep);
2185 $self->set_style($style);
2186 $self->set_text;
2187 }
2188  
2189 #=============================================================================
2190 # Named ranges
2191 #-----------------------------------------------------------------------------
2192 package ODF::lpOD::NamedRange;
2193 use base 'ODF::lpOD::Element';
2194 our $VERSION = '1.001';
2195 use constant PACKAGE_DATE => '2012-03-29T08:06:56';
2196 use ODF::lpOD::Common;
2197 #-----------------------------------------------------------------------------
2198  
2199 sub create
2200 {
2201 my $caller = shift;
2202 my $nr = ODF::lpOD::Element->create('table:named-range');
2203 $nr->set_attribute('name' => shift);
2204 $nr->set_properties(@_);
2205 return $nr;
2206 }
2207  
2208 #-----------------------------------------------------------------------------
2209  
2210 sub set_name
2211 {
2212 my $self = shift;
2213 my $name = shift or return undef;
2214 my $old = $self->get_attribute('name');
2215 my $doc = $self->document;
2216 if ($doc and ($name ne $old) and $doc->get_named_range($name))
2217 {
2218 alert "Named range $name already exists";
2219 return undef;
2220 }
2221 return $self->set_attribute('name' => $name);
2222 }
2223  
2224 sub get_properties
2225 {
2226 my $self = shift;
2227 my (%p, $t, $b, $s, $e);
2228 my $range = $self->get_attribute('table:cell-range-address');
2229 my $base = $self->get_attribute('table:base-cell-address');
2230 for ($range, $base)
2231 {
2232 if ($_) { $_ =~ s/\$//g; $_ =~ s/://g; }
2233 }
2234 ($p{table}, $b) = split(/\./, $base) if $base;
2235 ($t, $p{start}, $p{end}) = split(/\./, $range) if $range;
2236 $p{table} ||= $t;
2237 $p{range} = $p{start} . ':' . $p{end}
2238 if defined $p{start} and defined $p{end};
2239 $p{usage} = $self->get_attribute('range usable as') || 'none';
2240 return wantarray ? %p : {%p};
2241 }
2242  
2243 sub set_properties
2244 {
2245 my $self = shift;
2246 my $att = shift or return undef;
2247 my %att = ref $att ? %{$att} : ($att, @_);
2248 my $t = ref $att{table} ? $att{table}->get_name : $att{table};
2249 my %old = $self->get_properties;
2250 $att{$_} //= $old{$_} for keys %old;
2251 if ($att{range})
2252 {
2253 ($att{start}, $att{end}) = split(/:/, $att{range});
2254 delete $att{range};
2255 }
2256  
2257 $att{'base cell address'} ||=
2258 $t . '.' . $att{start};
2259 $att{'cell range address'} ||=
2260 $t . '.' . $att{start} . ':.' . $att{end};
2261 $att{'range usable as'} ||=
2262 ($att{usage} // 'none');
2263 delete @att{qw(table start end usage)};
2264  
2265 $self->set_attributes(%att);
2266 }
2267  
2268 sub _get_range_access
2269 {
2270 my $self = shift;
2271 my $doc = $self->document;
2272 unless ($doc)
2273 {
2274 alert "Not in document"; return undef;
2275 }
2276 my $context = $doc->get_body('spreadsheet');
2277 my $p = $self->get_properties;
2278 my $t = $context->get_table($p->{table});
2279 unless ($t)
2280 {
2281 alert "Unknown table"; return FALSE;
2282 }
2283 return ($t, $p->{range});
2284 }
2285  
2286 sub get_cells
2287 {
2288 my $self = shift;
2289 my ($t, $range) = $self->_get_range_access;
2290 return $t ? $t->get_cells($range, @_) : undef;
2291 }
2292  
2293 sub get_cell_values
2294 {
2295 my $self = shift;
2296 my $type = shift;
2297 my ($t, $range) = $self->_get_range_access;
2298 return $t ? $t->get_cell_values($type, $range) : undef;
2299 }
2300  
2301 #=============================================================================
2302 1;