Merge pull request #5 from solgenomics/topic/fix_il_maps
[cview.git] / lib / CXGN / Cview / ImageObject.pm
bloba6c20b7d23980a8977ced5981dce5d86ba2af7b2
3 =head1 NAME
5 CXGN::Cview::ImageObject - parent class for all image objects in Cview.
7 =head1 DESCRIPTION
9 This class defines and implements a number of functions. Some functions, such as render(), need to be overridden in derived classes.
11 =head1 SEE ALSO
13 L<CXGN::Cview>
15 =head1 AUTHOR(S)
17 Lukas Mueller <lam87@cornell.edu>
19 =head1 FUNCTIONS
21 This class defines the following methods:
23 =cut
27 use strict;
29 package CXGN::Cview::ImageObject;
31 =head2 function new()
33 Synopsis: constructor
34 Arguments:
35 Returns:
36 Side effects:
37 Description:
39 =cut
41 sub new {
42 my $class = shift;
43 my $args = {};
44 my $self = bless $args, $class;
45 my $x = shift;
46 my $y = shift;
47 my $height = shift;
48 my $width = shift;
49 $self->set_horizontal_offset($x);
50 $self->set_vertical_offset($y);
51 $self->set_height($height);
52 $self->set_width($width);
53 $self->set_color(0,0,0);
54 $self->set_fill_color(255, 255, 255);
55 return $self;
58 =head2 accessors get_name(), set_name()
60 Synopsis: $io -> set_name("foo")
61 Property: the name of the image element
62 Side effects:
63 Description:
65 =cut
67 sub set_name {
68 my $self =shift;
69 $self->{name} = shift;
72 sub get_name {
73 my $self = shift;
74 return ($self->{name});
77 =head2 accessors get_enclosing_rect(), set_enclosing_rect()
79 Synopsis: $io->set_enclosing_rect(10, 10, 100, 100)
80 Property: a list of coords, top left x,y and bottom right x,y
81 Side effects:
82 Description:
84 =cut
86 sub set_enclosing_rect {
87 my $self = shift;
88 @{$self->{enclosing_rect}} = @_;
91 sub get_enclosing_rect {
92 my $self = shift;
93 if (!exists($self->{enclosing_rect})) {
94 @{$self->{enclosing_rect}}=(0, 0, 0, 0);
96 return @{$self->{enclosing_rect}};
99 =head2 accessors set_color(), get_color()
101 Synopsis: $io->set_color(255,255,0)
102 Property: the color of the element
103 Side effects: the element will be rendered in this color
104 Description:
106 =cut
108 sub set_color {
109 my $self = shift;
110 ($self->{color}[0], $self->{color}[1], $self->{color}[2]) = @_;
113 sub get_color {
114 my $self = shift;
115 return @{$self->{color}};
119 =head2 accessors set_fill_color(), get_fill_color()
121 Synopsis: $io->set_color(255,255,0)
122 Property: the fill color of the element
123 Side effects: the element will be rendered filled in this color
124 Description:
126 =cut
128 sub set_fill_color {
129 my $self = shift;
130 ($self->{fill_color}[0], $self->{fill_color}[1], $self->{fill_color}[2]) = @_;
133 sub get_fill_color {
134 my $self = shift;
135 return @{$self->{fill_color}};
138 =head2 function render()
140 Synopsis: [abstract method]
141 Arguments: an GD::Image object
142 Returns: nothing
143 Side effects: this should be implemented to draw the image object
144 on the GD::Image.
145 Description:
147 =cut
149 sub render {
150 my $self = shift;
151 # does nothing
154 =head2 function get_image_map()
156 Synopsis: $io->get_mage_map()
157 Arguments: none
158 Returns: a string representing the html image map for the object
159 Side effects:
160 Description: a default implementation is given that should work for most
161 objects, or it may be overridden in sub-classes.
163 =cut
165 sub get_image_map {
166 my $self = shift;
167 my $coords = join ",", ($self -> get_enclosing_rect());
168 my $string;
169 if ($self->get_url()) { $string = "<area name=".$self->get_name()." shape=\"rect\" coords=\"".$coords."\" href=\"".$self->get_url()."\" alt=\"".$self->get_name()."\" />";}
170 return $string;
174 =head2 accessors get_url(), set_url()
176 Synopsis: $io->set_url("http://sgn.cornell.edu/search/unigene.pl?unigene_id=449494")
177 Property: the link this object should go to when clicked
178 Side effects: this link will be imbeded in the html image map
179 Description:
181 =cut
183 sub set_url {
184 my $self = shift;
185 $self->{url}=shift;
188 sub get_url {
189 my $self = shift;
190 if (!exists($self->{url}) || !defined($self->{url})) { $self->{url}=""; }
191 return $self->{url};
194 =head2 accessors set_horizontal_offset(), get_horizontal_offset()
196 Synopsis: my $offset = $chr -> get_horizontal_offset()
197 $chr->set_horizontal_offset(57)
198 Arguments: setter: the offset in pixels [integer]
199 Returns: getter: returns the horizontal offset in pixels
200 of the image element. The
201 Side effects:
202 Description: The horizontal offset can be defined in different ways
203 depending on the image. Most often, it denotes the mid-line
204 of the object (such as a chromosome) or the left boundary.
206 =cut
208 sub get_horizontal_offset {
209 my $self=shift;
210 if (!exists($self->{horizontal_offset}) || ! defined($self->{horizontal_offset})) { $self->set_horizontal_offset(0); }
211 return $self->{horizontal_offset};
214 sub set_horizontal_offset {
215 my $self=shift;
216 $self->{horizontal_offset}=shift;
219 =head2 accessors get_X(), set_X()
221 Synopsis: same as accessors for horizontal_offset(), but shorter!
222 Arguments:
223 Returns:
224 Side effects:
225 Description:
227 =cut
229 sub get_X {
230 my $self=shift;
231 return $self->{horizontal_offset};
234 sub set_X {
235 my $self=shift;
236 $self->{horizontal_offset}=shift;
239 =head2 accessors get_vertical_offset(), set_vertical_offset()
241 Synopsis: $chr -> get_vertical_offset()
242 Arguments: setter: the vertical offset in pixels.
243 Returns: Returns the vertical offset of the image element in pixels.
244 Side effects:
245 Description: Returns the vertical offset of the image element, which
246 defines the upper limit of the image element. Certain
247 chromosome renditions will add a round edge on the top that
248 will extend the chromomsome beyond that value.
250 =cut
252 sub get_vertical_offset {
253 my $self=shift;
254 if (!exists($self->{vertical_offset}) || !defined($self->{vertical_offset})) {
255 $self->{vertical_offset}=0;
257 return $self->{vertical_offset};
260 sub set_vertical_offset {
261 my $self=shift;
262 $self->{vertical_offset}=shift;
265 =head2 accessors get_Y(), set_Y()
267 Synopsis: same as vertical_offset accessors
268 Arguments:
269 Returns:
270 Side effects:
271 Description:
273 =cut
275 sub get_Y {
276 my $self=shift;
277 return $self->{vertical_offset};
280 sub set_Y {
281 my $self=shift;
282 $self->{vertical_offset}=shift;
287 =head2 accessors get_height(), set_height()
289 Synopsis: $io->set_height(300)
290 Property: the height of the object in pixels
291 Side effects: note that changing this property does not change
292 the enclosing rect automatically. This has to be
293 done manually.
294 Description:
296 =cut
298 sub get_height {
299 my $self=shift;
300 if (!exists($self->{height})) { $self->set_height(0); }
301 return $self->{height};
304 sub set_height {
305 my $self=shift;
306 $self->{height}=shift;
309 =head2 accessors get_width(), set_width()
311 Synopsis: $io->set_width(100)
312 Property: the width of this object in pixels
313 Side effects: note that changing this will not change the
314 enclosing rect - this needs to be changed
315 separately manually
316 Description:
318 =cut
320 sub get_width {
321 my $self=shift;
322 return $self->{width};
326 sub set_width {
327 my $self=shift;
328 $self->{width}=shift;
334 =head2 accessors get_font(), set_font()
336 Synopsis: $io->set_font(GD::Font->Tiny)
337 Property: the GD font to be used when drawing this object
338 Side effects:
339 Description:
341 =cut
343 sub get_font {
344 my $self=shift;
345 return $self->{font};
348 sub set_font {
349 my $self=shift;
350 $self->{font}=shift;
353 =head2 function round()
355 Synopsis: my $rounded = round(4.3);
356 Arguments: a real number to be rounded
357 Returns: an int
358 Side effects: none
359 Description: Perl does not have a round function built in (it\'s in Math::round),
360 but we need it for rounding calculation of pixels.
361 Note: works also for negative numbers, astoninglishly
363 =cut
365 sub round {
366 my $value = shift;
367 my $int = int($value);
368 my $rest = abs($value)-abs($int);
369 if ($rest > 0.5) {
370 if ($value > 0) {
371 $int++;
373 else {
374 $int--;
377 #print STDERR "Rounded $value to $int\n";
378 return $int;