blib/lib/Gtk2/Ex/FormFactory/Table.pm untested

filter: Statementstmt 0.0% 0 / 171 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 312 CC 23 SCAR 43.2
CC
23
Cov
0%
calculate_layout
210.0
get_xalign
20.0
get_yalign
20.0
CRAP 75.3
1 package Gtk2::Ex::FormFactory::Table;
2  
3 use strict;
4  
5 use base qw( Gtk2::Ex::FormFactory::Container );
6  
7 sub get_type { "table" }
8  
9 sub get_layout { shift->{layout} }
10 sub get_widget_table_attach { shift->{widget_table_attach} }
11 sub get_widget_table_align { shift->{widget_table_align} }
12 sub get_rows { shift->{rows} }
13 sub get_columns { shift->{columns} }
14  
15 sub set_layout { shift->{layout} = $_[1] }
16 sub set_widget_table_attach { shift->{widget_table_attach} = $_[1] }
17 sub set_widget_table_align { shift->{widget_table_align} = $_[1] }
18 sub set_rows { shift->{rows} = $_[1] }
19 sub set_columns { shift->{columns} = $_[1] }
20  
21 sub new {
22 my $class = shift;
23 my %par = @_;
24 my ($layout) = $par{'layout'};
25  
26 my $self = $class->SUPER::new(@_);
27
28 $layout =~ s/^\s+//m;
29 $layout =~ s/\s+$//m;
30 $layout .= "\n";
31  
32 warn "Layout of table ".$self->get_name." contains tab characters"
33 if $layout =~ /\t/;
34  
35 $self->set_layout($layout);
36  
37 $self->calculate_layout;
38
39 return $self;
40 }
41  
42 =cut
43  
44 Table example (helps understanding the code beyond):
45  
46 This Column
47 expands
48  
49 +--------------+>>>>>>>>>>>>+
50 | 1 | 2 |
51 +--------------+ |
52 | 3 | |
53 +-------+------+------------+ -
54 > 4 | 5 | 6 | |
55 > | +------------+ |
56 > | | 7 | |
57 +-------+------+------------+ | All rows in this span
58 > 8 | | 9 | | expand as well
59 > | +------------+ |
60 > | | 10 | |
61 > +------+------------+ |
62 > | 11 | |
63 +-------+-------------------+ -
64  
65 * empty fields get no Widget (what's
66 inside the field, doesn't matter)
67  
68 * non-empty fields get the Widgets in
69 the order showed above, counting
70 from left-top to right-bottom
71  
72 =cut
73  
74 sub calculate_layout {
75 my $self = shift;
76
77 #-- Get layout of this table
78 my $layout = $self->get_layout;
79 my @layout_rows = map { s/^\s*//; $_ } split (/\n/, $layout);
80  
81 #-- First we calculate the raster which forms the
82 #-- basis of this table. %raster_x and %raster_y
83 #-- will contain all indicies, where a column
84 #-- border was detected.
85 my (%raster_x, %raster_y);
86 my $y = 0;
87 foreach my $row ( @layout_rows ) {
88 #-- Detect X coordinates
89 if ( $row =~ /[|^_'~]/ ) {
90 my $x = 0;
91 foreach my $field ( split(/[|^_']/, $row) ) {
92 $x+=length($field);
93 $raster_x{$x} = 1;
94 ++$x;
95 }
96 }
97 #-- Detect Y coordinates
98 if ( $y == 0 || $row =~ /[-=]/ ) {
99 $raster_y{$y} = 1;
100 }
101 #-- Count next row
102 ++$y;
103 }
104  
105 #-- Store the number of rows the layout string has
106 my $layout_rows = $y;
107
108 #-- Sorted list of row/column indices
109 my @raster_x = sort { $a <=> $b } keys %raster_x;
110 my @raster_y = sort { $a <=> $b } keys %raster_y;
111  
112 if ( $Gtk2::Ex::FormFactory::DEBUG ) {
113 require Data::Dumper;
114 print "raster_x: ", Data::Dumper::Dumper(\@raster_x);
115 print "raster_y: ", Data::Dumper::Dumper(\@raster_y);
116 }
117  
118 #-- Now walk through all lines and build the table
119 #-- attach information for each non-empty field
120 my @widget_table_attach;
121 my @widgets_table_align;
122 $y = 0;
123  
124 foreach my $row_i ( @layout_rows ) {
125 #-- $row is modified beyond
126 my $row = $row_i;
127  
128 #-- Skip lines which have no | or ' or ^ or _
129 #-- (such lines just separate rows from each other
130 #-- and are of no interest here)
131 if ( $row !~ /[|^_']/ ) {
132 ++$y;
133 next;
134 }
135  
136 $Gtk2::Ex::FormFactory::DEBUG &&
137 print "=============================\n";
138 $Gtk2::Ex::FormFactory::DEBUG &&
139 print "[$y] analyze row: '$row'\n";
140  
141 #-- Track left/right indices and attach
142 #-- column numbers
143 my $right_x = 0;
144 my $right_attach = 0;
145 my $left_x = 0;
146 my $left_attach = 0;
147  
148 #-- Remove the first | character, because we
149 #-- split at | borders (otherwise we would
150 #-- get an empty field at the first position)
151 $row =~ s/^(.)//;
152  
153 #-- And remember it, since it may be a special
154 #-- character (^ _ ') defining a vertical property.
155 my $row_first_char = $1;
156  
157 #-- split row into fields. All vertical special
158 #-- characters mark borders, plus + and |.
159 foreach my $field ( split(/[|+'_^~]/, $row) ) {
160 $Gtk2::Ex::FormFactory::DEBUG &&
161 print "[$y,$left_x] field='$field'\n";
162  
163 #-- Calculate right index of this field
164 $right_x = $left_x + length($field)+1;
165  
166 #-- Calculate the cell span of this field using
167 #-- the @raster_x array calculated above.
168 my $span_x = 0;
169 foreach my $x ( @raster_x ) {
170 ++$span_x if $x >= $left_x &&
171 $x < $right_x;
172 last if $x > $right_x;
173 }
174  
175 #-- With the cell span we know the right attach
176 $right_attach = $left_attach + $span_x;
177  
178 #-- Now we know all X values, more work needs to
179 #-- be done for the Y part
180  
181 #-- Process this field only if it doesn't
182 #-- span over several rows, because then
183 #-- it was processed already in a previous
184 #-- iteration)
185 if ( $y > 0 && substr($layout_rows[$y-1], $left_x+1, 1 ) =~ /[-=>%\[\]]/ ) {
186 #-- Seek for the bottom of that field and calculate
187 #-- top and bottom attachment
188 my $top_attach = 0;
189 my $bot_attach = 0;
190  
191 #-- Skip first line, which defines no columns
192 my $y_seek;
193 for ( $y_seek = 1; $y_seek < $layout_rows; ++$y_seek ) {
194 $Gtk2::Ex::FormFactory::DEBUG &&
195 print "top_attach: y=$y y_seek=$y_seek ".
196 "raster_y=$raster_y{$y_seek}\n";
197 if ( $y_seek < $y and $raster_y{$y_seek} ) {
198 #-- If we are above the current y position and
199 #-- hit a y raster point, increment top_
200 #-- and bot_attach
201 ++$top_attach;
202 ++$bot_attach;
203  
204 } elsif ( $raster_y{$y_seek} ) {
205 #-- If we're beyond the current y position,
206 #-- increase bot_attach if we hit a raster point
207 ++$bot_attach;
208 }
209  
210 $Gtk2::Ex::FormFactory::DEBUG &&
211 print "=> top_attach=$top_attach ".
212 "bot_attach=$bot_attach\n".
213 " '".
214 substr($layout_rows[$y_seek],
215 $left_x+1, 1)."'\n";
216  
217 #-- Out here if we hit the end of the field
218 last if $y_seek >= $y and
219 substr($layout_rows[$y_seek],
220 $left_x+1, 1) =~ /[-+=\[\]\%]/;
221  
222 $field .= "\n".substr($layout_rows[$y_seek],
223 $left_x+1, $right_x-$left_x-1)
224 if $y_seek > $y;
225 }
226  
227 #-- Push all attach values into the list,
228 #-- but only if the field is not empty
229 if ( $field =~ /\S/ ) {
230 my $hexpand = $self->get_xexpansion(\@layout_rows, $left_x+1, $y, $right_x);
231 my $vexpand = $self->get_yexpansion(\@layout_rows, $left_x, $y, $y_seek-1),
232 my $xalign = $self->get_xalign(\@layout_rows, $left_x+1, $y, $right_x);
233 my $yalign = $self->get_yalign(\@layout_rows, $left_x, $y, $y_seek-1);
234  
235 push @widget_table_attach, [
236 #-- Attachments
237 $left_attach,
238 $right_attach,
239 $top_attach,
240 $bot_attach,
241 $hexpand,
242 $vexpand,
243 ];
244  
245 push @widgets_table_align, {
246 xalign => $xalign,
247 yalign => $yalign,
248 };
249  
250 if ( $Gtk2::Ex::FormFactory::DEBUG && $field =~ /\S/ ) {
251 $hexpand = join("|",@{$hexpand});
252 $vexpand = join("|",@{$vexpand});
253 print "--------------------------\n";
254 print "y: $y\n";
255 print "x: ".($left_x+1),"\n";
256 print "left_attach: $left_attach\n";
257 print "right_attach: $right_attach\n";
258 print "top_attach: $top_attach\n";
259 print "bot_attach: $bot_attach\n";
260 print "field: '$field'\n";
261 print "hexpand: $hexpand\n";
262 print "vexpand: $vexpand\n";
263 print "xalign: $xalign\n";
264 print "yalign: $yalign\n";
265 }
266 }
267 }
268  
269 #-- Initialize left index/attach values
270 #-- for the next field
271 $left_x = $right_x;
272 $left_attach = $right_attach;
273 }
274  
275 #-- Count Y coordinate
276 ++$y;
277 }
278
279 #-- Store results in the object
280 $self->set_rows(scalar(@raster_y)-1);
281 $self->set_columns(scalar(@raster_x-1));
282 $self->set_widget_table_attach(\@widget_table_attach);
283 $self->set_widget_table_align(\@widgets_table_align);
284  
285 $Gtk2::Ex::FormFactory::DEBUG &&
286 print Data::Dumper::Dumper(\@widget_table_attach);
287  
288 1;
289 }
290  
291 sub get_xexpansion {
292 my $self = shift;
293 my ($layout_rows, $x, $y, $max_x) = @_;
294  
295 my $row = $layout_rows->[$y-1];
296 my $c;
297 while ( $x <= $max_x ) {
298 $c = substr($row, $x, 1);
299 return ["fill","expand"] if $c =~ /[>^]/;
300 ++$x;
301 }
302  
303 return ["fill"];
304 }
305  
306 sub get_yexpansion {
307 my $self = shift;
308 my ($layout_rows, $x, $y, $max_y) = @_;
309  
310 my $c;
311 while ( $y <= $max_y ) {
312 $c = substr($layout_rows->[$y], $x, 1);
313 return ["fill","expand"] if $c =~ /[>^]/;
314 ++$y;
315 }
316  
317 return ["fill"];
318 }
319  
320 sub get_xalign {
321 my $self = shift;
322 my ($layout_rows, $x, $y, $max_x) = @_;
323
324 my $row = $layout_rows->[$y-1];
325 my $c;
326 while ( $x <= $max_x ) {
327 $c = substr($row, $x, 1);
328 return 0 if $c eq '[';
329 return 1 if $c eq ']';
330 return 0.5 if $c eq '%';
331 ++$x;
332 }
333  
334 return -1;
335 }
336  
337 sub get_yalign {
338 my $self = shift;
339 my ($layout_rows, $x, $y, $max_y) = @_;
340  
341 my $c;
342 while ( $y <= $max_y ) {
343 $c = substr($layout_rows->[$y], $x, 1);
344 return 0 if $c eq "'";
345 return 1 if $c eq '_';
346 return 0.5 if $c eq '~';
347 ++$y;
348 }
349  
350 return -1;
351 }
352  
353 1;
354  
355 __END__