GeniusTrader/Graphics/Zone.pm untested

filter: Statementstmt 0.0% 0 / 174 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 46 Podpod 0.0% 0 / 39 totaltotal 0.0% 0 / 331 CC 19 SCAR 30.9
CC
19
Cov
0%
display
110.0
new
30.0
zone_coordinate
12.0
CRAP 21.9
1 package Finance::GeniusTrader::Graphics::Zone;
2  
3 # Copyright 2000-2002 Raphaël Hertzog, Fabien Fulhaber
4 # This file is distributed under the terms of the General Public License
5 # version 2 or (at your option) any later version.
6  
7 use strict;
8 use Finance::GeniusTrader::Graphics::Graphic;
9 use Finance::GeniusTrader::Graphics::Driver;
10 use Finance::GeniusTrader::Graphics::Axis;
11 use Finance::GeniusTrader::Tools qw(:math);
12 use Finance::GeniusTrader::Graphics::Tools qw(:color);
13 use Finance::GeniusTrader::Conf;
14  
15 =head1 Finance::GeniusTrader::Graphics::Zone
16  
17 A zone is a part of the graphic. It has an external size and an internal
18 size. The internal size may be split in several sub zones.
19  
20 ________________
21 | ____________ |
22 | | | |
23 | | Int. zone | |
24 | |____________| |
25 |________________|
26  
27 A zone can also be considered like a « drawable » object and as such
28 it implements the display(...) method. It will call the display methods
29 for the axis, draw the border if needed and draw the title.
30
31 =head2 Finance::GeniusTrader::Graphics::Zone->new($width, $height, $left, $right, $top, $bottom);
32  
33 Creates a new zone with the given internal size. $left, $right, $top, $bottom
34 is the free space to keep around the internal zone.
35  
36 =cut
37 sub new {
38 my $self = shift;
39 my $type = ref($self) || $self;
40 my ($w, $h, $l, $r, $t, $b) = @_;
41  
42 my $zone = { "childs" => [],
43 "parent" => undef,
44 "x" => 0, "y" => 0,
45 "width" => $w,
46 "height" => $h,
47 "left" => defined($l) ? $l : 0,
48 "right" => defined($r) ? $r : 0,
49 "top" => defined($t) ? $t : 0,
50 "bottom" => defined($b) ? $b : 0,
51 "border_width" => 0,
52 "border_color" =>
53 get_color(Finance::GeniusTrader::Conf::get_first(
54 "Graphic::Zone::BorderColor",
55 "Graphic::ForegroundColor"
56 )),
57 "font_name" => $FONT_ARIAL,
58 "font_size" => $FONT_SIZE_MEDIUM,
59 "font_color" =>
60 get_color(Finance::GeniusTrader::Conf::get_first(
61 "Graphic::Zone::TitleColor",
62 "Graphic::ForegroundColor"
63 ))
64 };
65 return bless $zone, $type;
66 }
67  
68 =head2 $z->add_subzone($zx, $zy, $zone)
69  
70 Add $zone as a subzone of the current zone and place it at position ($zx,$zy).
71 $zx and $zy are positive integers.
72  
73 =cut
74 sub add_subzone {
75 my ($self, $zx, $zy, $child) = @_;
76  
77 $self->{'childs'}[$zx][$zy] = $child;
78 $child->set_parent($self);
79 $child->set_position($zx, $zy);
80 }
81  
82 =head2 ($ax, $ay) = $z->absolute_coordinate($x, $y)
83  
84 Returns the absolute coordinate of the given point. ($x, $y) is
85 the coordinate in the zone $z. ($ax, $ay) are the coordinate of the
86 same point but in the root zone.
87  
88 =cut
89 sub absolute_coordinate {
90 my ($self, $x, $y) = @_;
91  
92 if ($self->{'parent'}) {
93 my ($z_x, $z_y) = $self->{'parent'}->zone_coordinate($self->{'x'},
94 $self->{'y'});
95 return $self->{'parent'}->absolute_coordinate(
96 $self->{'left'} + $x + $z_x,
97 $self->{'bottom'} + $y + $z_y);
98 } else {
99 return ($x + $self->{'left'}, $y + $self->{'bottom'});
100 }
101 }
102  
103 =head2 ($x, $y) = $z->zone_coordinate($zx, $zy)
104  
105 Returns the bottom left coordinate of the zone identified by
106 ($zx, $zy). This function will only give good results once all
107 zones have been created and linked together.
108  
109 =cut
110 sub zone_coordinate {
111 my ($self, $zx, $zy) = @_;
112  
113 my ($x, $y) = (0, 0);
114
115 for (my $i = 0; $i < $zx; $i++) {
116 if (defined($self->{'childs'}[$i][$zy])) {
117 $x += $self->{'childs'}[$i][$zy]->external_width();
118 }
119 }
120 my $nby = scalar(@{$self->{'childs'}[$zx]});
121 for (my $i = $nby; $i > $zy; $i--) {
122 if (defined($self->{'childs'}[$zx][$i])) {
123 $y += $self->{'childs'}[$zx][$i]->external_height();
124 }
125 }
126 return ($x, $y);
127 }
128  
129 =head2 $z->update_size()
130  
131 Update the size of the zone according to the size of the childs.
132  
133 =cut
134 sub update_size {
135 my ($self) = @_;
136 my ($w, $h) = (0, 0);
137 my $nb = 0;
138
139 # Look for the height in the columns
140 foreach my $column (grep { defined($_) } @{$self->{'childs'}}) {
141 my $h_col = 0;
142 $nb = max($nb, scalar @{$column});
143 foreach (grep { defined($_) } @{$column}) {
144 $h_col += $_->external_height();
145 }
146 $h = max($h, $h_col);
147 }
148  
149 # Look for the width in the rows
150 for(my $i = 0; $i < $nb; $i++) {
151 my $w_row = 0;
152 for(my $j = 0; $j < scalar @{$self->{'childs'}}; $j++) {
153 next if (! defined($self->{'childs'}[$j][$i]));
154 $w_row += $self->{'childs'}[$j][$i]->external_width();
155 }
156 $w = max($w, $w_row);
157 }
158  
159 $self->{'width'} = $w;
160 $self->{'height'} = $h;
161 }
162  
163 =head2 $z->get_subzone($zx, $zy)
164  
165 Return the subzone indicated by the coordinate.
166  
167 =cut
168 sub get_subzone {
169 my ($self, $zx, $zy) = @_;
170  
171 return $self->{'childs'}[$zx][$zy];
172 }
173  
174 =head2 $z->set_axis_{left,right,top,bottom}($axis)
175  
176 Put an axis on the indicated side.
177  
178 =cut
179 sub set_axis_left { $_[0]->{'axis_left'} = $_[1] }
180 sub set_axis_right { $_[0]->{'axis_right'} = $_[1] }
181 sub set_axis_top { $_[0]->{'axis_top'} = $_[1] }
182 sub set_axis_bottom { $_[0]->{'axis_bottom'} = $_[1] }
183  
184 =head2 $z->get_axis_{left,right,top,bottom}()
185  
186 Get the axis of the indicated side.
187  
188 =cut
189 sub get_axis_left { $_[0]->{'axis_left'} }
190 sub get_axis_right { $_[0]->{'axis_right'} }
191 sub get_axis_top { $_[0]->{'axis_top'} }
192 sub get_axis_bottom { $_[0]->{'axis_bottom'} }
193  
194 =head2 $z->set_title_{left,right,top,bottom}($title)
195  
196 Put a title on the indicated side.
197  
198 =cut
199 sub set_title_left { $_[0]->{'title_left'} = $_[1] }
200 sub set_title_right { $_[0]->{'title_right'} = $_[1] }
201 sub set_title_top { $_[0]->{'title_top'} = $_[1] }
202 sub set_title_bottom { $_[0]->{'title_bottom'} = $_[1] }
203  
204 =head2 $z->get_title_{left,right,top,bottom}()
205  
206 Get the title of the indicated side.
207  
208 =cut
209 sub get_title_left { $_[0]->{'title_left'} }
210 sub get_title_right { $_[0]->{'title_right'} }
211 sub get_title_top { $_[0]->{'title_top'} }
212 sub get_title_bottom { $_[0]->{'title_bottom'} }
213  
214 =head2 $z->set_title_font_name($name)
215  
216 =head2 $z->set_title_font_size($size)
217  
218 =head2 $z->set_title_font_color($size)
219  
220 Those 3 methods are used to change the font for the title.
221  
222 =cut
223 sub set_title_font_name {
224 my ($self, $name) = @_;
225 $self->{'font_name'} = $name;
226 }
227 sub set_title_font_size {
228 my ($self, $size) = @_;
229 $self->{'font_size'} = $size;
230 }
231 sub set_title_font_color {
232 my ($self, $color) = @_;
233 $self->{'font_color'} = $color;
234 }
235  
236 =head2 $z->set_default_scale($scale)
237  
238 Use $scale as the default scale for this zone.
239  
240 =cut
241 sub set_default_scale { $_[0]->{'default_scale'} = $_[1] }
242 sub get_default_scale { $_[0]->{'default_scale'} }
243  
244 =head2 $z->set_border_width($width)
245  
246 Add a border of the given width around the zone. It will not sharp the zone
247 but extend/decrease the space around it.
248  
249 =cut
250 sub set_border_width {
251 my ($self, $width) = @_;
252 my $change = $width - $self->{'border_width'};
253 $self->{'left'} += $change;
254 $self->{'right'} += $change;
255 $self->{'top'} += $change;
256 $self->{'bottom'} += $change;
257 $self->{'border_width'} = $width;
258 }
259  
260 =head2 $z->set_border_color([$R,$G,$B])
261  
262 Change the color of the border.
263  
264 =cut
265 sub set_border_color {
266 my ($self, $color) = @_;
267 $self->{'border_color'} = $color;
268 }
269  
270 =head2 $z->includes_point($x, $y, [$extented])
271  
272 Returns true if the point is within the zone, false otherwise.
273 If $extended is true, the border of the zone is considered as part
274 of the zone.
275  
276 =cut
277 sub includes_point {
278 my ($self, $x, $y, $extended) = @_;
279 if (defined($extended) && $extended) {
280 return (($x >= - $self->{'left'}) &&
281 ($x < $self->{'width'} + $self->{'right'}) &&
282 ($y >= - $self->{'bottom'}) &&
283 ($y < $self->{'height'} + $self->{'top'}));
284 } else {
285 return (($x >= 0) && ($x < $self->{'width'}) &&
286 ($y >= 0) && ($y < $self->{'height'}));
287 }
288 }
289  
290 =head2 $z->display($driver, $graphic)
291  
292 Display the zone with its axis, its borders and its titles.
293  
294 =cut
295 sub display {
296 my ($self, $driver, $picture) = @_;
297 my $offset = ($self->{'border_width'} > 0) ? 1 : 0;
298 if (defined($self->{'axis_left'})) {
299 $self->{'axis_left'}->set_left_side();
300 $self->{'axis_left'}->set_zone($self);
301 $self->{'axis_left'}->set_rectangle(- $self->{'left'}, 0,
302 - $self->{'border_width'} - 1 + $offset,
303 $self->{'height'});
304 $self->{'axis_left'}->display($driver, $picture);
305 }
306 if (defined($self->{'axis_right'})) {
307 $self->{'axis_right'}->set_right_side();
308 $self->{'axis_right'}->set_zone($self);
309 $self->{'axis_right'}->set_rectangle($self->{'width'} +
310 $self->{'border_width'} - $offset, 0,
311 $self->{'width'} + $self->{'right'},
312 $self->{'height'});
313 $self->{'axis_right'}->display($driver, $picture);
314 }
315 if (defined($self->{'axis_top'})) {
316 $self->{'axis_top'}->set_top_side();
317 $self->{'axis_top'}->set_zone($self);
318 $self->{'axis_top'}->set_rectangle(0, $self->{'height'} +
319 $self->{'border_width'} - $offset,
320 $self->{'width'}, $self->{'height'} +
321 $self->{'top'});
322 $self->{'axis_top'}->display($driver, $picture);
323 }
324 if (defined($self->{'axis_bottom'})) {
325 $self->{'axis_bottom'}->set_bottom_side();
326 $self->{'axis_bottom'}->set_zone($self);
327 $self->{'axis_bottom'}->set_rectangle(0, - $self->{'bottom'},
328 $self->{'width'},
329 - $self->{'border_width'} - 1 + $offset);
330 $self->{'axis_bottom'}->display($driver, $picture);
331 }
332  
333 for (my $i = 1; $i <= $self->{'border_width'}; $i++) {
334 $driver->rectangle($picture, $self->absolute_coordinate(- $i, - $i),
335 $self->absolute_coordinate($self->{'width'} - 1 + $i,
336 $self->{'height'} - 1 + $i),
337 $self->{'border_color'});
338 }
339  
340 if ($self->{'title_left'}) {
341 $driver->string($picture, $self->{'font_name'},
342 $self->{'font_size'}, $self->{'font_color'},
343 $self->absolute_coordinate(
344 - $self->{'left'}, int($self->{'height'} / 2)),
345 $self->{'title_left'}, $ALIGN_LEFT, $ALIGN_CENTER,
346 $ORIENTATION_UP);
347 }
348 if ($self->{'title_right'}) {
349 $driver->string($picture, $self->{'font_name'},
350 $self->{'font_size'}, $self->{'font_color'},
351 $self->absolute_coordinate(
352 $self->{'right'} + $self->{'width'} - 1,
353 int($self->{'height'} / 2)),
354 $self->{'title_right'}, $ALIGN_RIGHT, $ALIGN_CENTER,
355 $ORIENTATION_DOWN);
356 }
357 if ($self->{'title_top'}) {
358 $driver->string($picture, $self->{'font_name'},
359 $self->{'font_size'}, $self->{'font_color'},
360 $self->absolute_coordinate(
361 int($self->{'width'} / 2),
362 $self->{'height'} + $self->{'top'} - 1),
363 $self->{'title_top'}, $ALIGN_CENTER, $ALIGN_TOP);
364 }
365 if ($self->{'title_bottom'}) {
366 $driver->string($picture, $self->{'font_name'},
367 $self->{'font_size'}, $self->{'font_color'},
368 $self->absolute_coordinate(
369 int($self->{'width'} / 2),
370 - $self->{'bottom'}),
371 $self->{'title_bottom'}, $ALIGN_CENTER, $ALIGN_BOTTOM);
372 }
373  
374 foreach my $array (grep { defined($_) } @{$self->{'childs'}}) {
375 foreach my $subzone (grep { defined($_) } @{$array}) {
376 $subzone->display($driver, $picture);
377 }
378 }
379 }
380  
381 =head1 SIMPLE FUNCTIONS
382  
383 =head2 $z->width() && $z->height()
384  
385 =head2 $z->external_width() && $z->external_height()
386  
387 =cut
388 sub width { $_[0]->{'width'} }
389 sub height { $_[0]->{'height'} }
390 sub external_width { $_[0]->{'width'} + $_[0]->{'left'} + $_[0]->{'right'} }
391 sub external_height { $_[0]->{'height'} + $_[0]->{'top'} + $_[0]->{'bottom'} }
392  
393 =head2 ($x, $y) = $z->get_position()
394  
395 =head2 $z->get_parent()
396  
397 =cut
398 sub get_position { return ($_[0]->{'x'}, $_[0]->{'y'}) }
399 sub get_parent { $_[0]->{'parent'} }
400  
401 =head1 INTERNAL FUNCTIONS
402  
403 =head2 $z->set_parent($parent)
404  
405 =head2 $z->set_position($zx, $zy)
406  
407 =cut
408 sub set_parent { $_[0]->{'parent'} = $_[1] }
409 sub set_position { $_[0]->{'x'} = $_[1]; $_[0]->{'y'} = $_[2]; }
410  
411 1;