oops, this directory was missed...
[cview.git] / lib / CXGN / ImageObject.pm
blob5e420f10c5dcb74bae2aa035c53aff870032752a
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 return $self;
57 =head2 accessors get_name(), set_name()
59 Synopsis: $io -> set_name("foo")
60 Property: the name of the image element
61 Side effects:
62 Description:
64 =cut
66 sub set_name {
67 my $self =shift;
68 $self->{name} = shift;
71 sub get_name {
72 my $self = shift;
73 return ($self->{name});
76 =head2 accessors get_enclosing_rect(), set_enclosing_rect()
78 Synopsis: $io->set_enclosing_rect(10, 10, 100, 100)
79 Property: a list of coords, top left x,y and bottom right x,y
80 Side effects:
81 Description:
83 =cut
85 sub set_enclosing_rect {
86 my $self = shift;
87 @{$self->{enclosing_rect}} = @_;
90 sub get_enclosing_rect {
91 my $self = shift;
92 if (!exists($self->{enclosing_rect})) {
93 @{$self->{enclosing_rect}}=(0, 0, 0, 0);
95 return @{$self->{enclosing_rect}};
98 =head2 accessors set_color(), get_color()
100 Synopsis: $io->set_color(255,255,0)
101 Property: the color of the element
102 Side effects: the element will be rendered in this color
103 Description:
105 =cut
107 sub set_color {
108 my $self = shift;
109 ($self->{color}[0], $self->{color}[1], $self->{color}[2]) = @_;
112 sub get_color {
113 my $self = shift;
114 return @{$self->{color}};
117 =head2 function render()
119 Synopsis: [abstract method]
120 Arguments: an GD::Image object
121 Returns: nothing
122 Side effects: this should be implemented to draw the image object
123 on the GD::Image.
124 Description:
126 =cut
128 sub render {
129 my $self = shift;
130 # does nothing
133 =head2 function get_image_map()
135 Synopsis: $io->get_mage_map()
136 Arguments: none
137 Returns: a string representing the html image map for the object
138 Side effects:
139 Description: a default implementation is given that should work for most
140 objects, or it may be overridden in sub-classes.
142 =cut
144 sub get_image_map {
145 my $self = shift;
146 my $coords = join ",", ($self -> get_enclosing_rect());
147 my $string;
148 if ($self->get_url()) { $string = "<area name=".$self->get_name()." shape=\"rect\" coords=\"".$coords."\" href=\"".$self->get_url()."\" alt=\"".$self->get_name()."\" />";}
149 return $string;
153 =head2 accessors get_url(), set_url()
155 Synopsis: $io->set_url("http://sgn.cornell.edu/search/unigene.pl?unigene_id=449494")
156 Property: the link this object should go to when clicked
157 Side effects: this link will be imbeded in the html image map
158 Description:
160 =cut
162 sub set_url {
163 my $self = shift;
164 $self->{url}=shift;
167 sub get_url {
168 my $self = shift;
169 if (!exists($self->{url}) || !defined($self->{url})) { $self->{url}=""; }
170 return $self->{url};
173 =head2 accessors set_horizontal_offset(), get_horizontal_offset()
175 Synopsis: my $offset = $chr -> get_horizontal_offset()
176 $chr->set_horizontal_offset(57)
177 Arguments: setter: the offset in pixels [integer]
178 Returns: getter: returns the horizontal offset in pixels
179 of the image element. The
180 Side effects:
181 Description: The horizontal offset can be defined in different ways
182 depending on the image. Most often, it denotes the mid-line
183 of the object (such as a chromosome) or the left boundary.
185 =cut
187 sub get_horizontal_offset {
188 my $self=shift;
189 if (!exists($self->{horizontal_offset}) || ! defined($self->{horizontal_offset})) { $self->set_horizontal_offset(0); }
190 return $self->{horizontal_offset};
193 sub set_horizontal_offset {
194 my $self=shift;
195 $self->{horizontal_offset}=shift;
198 =head2 accessors get_X(), set_X()
200 Synopsis: same as accessors for horizontal_offset(), but shorter!
201 Arguments:
202 Returns:
203 Side effects:
204 Description:
206 =cut
208 sub get_X {
209 my $self=shift;
210 return $self->{horizontal_offset};
213 sub set_X {
214 my $self=shift;
215 $self->{horizontal_offset}=shift;
218 =head2 accessors get_vertical_offset(), set_vertical_offset()
220 Synopsis: $chr -> get_vertical_offset()
221 Arguments: setter: the vertical offset in pixels.
222 Returns: Returns the vertical offset of the image element in pixels.
223 Side effects:
224 Description: Returns the vertical offset of the image element, which
225 defines the upper limit of the image element. Certain
226 chromosome renditions will add a round edge on the top that
227 will extend the chromomsome beyond that value.
229 =cut
231 sub get_vertical_offset {
232 my $self=shift;
233 if (!exists($self->{vertical_offset}) || !defined($self->{vertical_offset})) {
234 $self->{vertical_offset}=0;
236 return $self->{vertical_offset};
239 sub set_vertical_offset {
240 my $self=shift;
241 $self->{vertical_offset}=shift;
244 =head2 accessors get_Y(), set_Y()
246 Synopsis: same as vertical_offset accessors
247 Arguments:
248 Returns:
249 Side effects:
250 Description:
252 =cut
254 sub get_Y {
255 my $self=shift;
256 return $self->{vertical_offset};
259 sub set_Y {
260 my $self=shift;
261 $self->{vertical_offset}=shift;
266 =head2 accessors get_height(), set_height()
268 Synopsis: $io->set_height(300)
269 Property: the height of the object in pixels
270 Side effects: note that changing this property does not change
271 the enclosing rect automatically. This has to be
272 done manually.
273 Description:
275 =cut
277 sub get_height {
278 my $self=shift;
279 if (!exists($self->{height})) { $self->set_height(0); }
280 return $self->{height};
283 sub set_height {
284 my $self=shift;
285 $self->{height}=shift;
288 =head2 accessors get_width(), set_width()
290 Synopsis: $io->set_width(100)
291 Property: the width of this object in pixels
292 Side effects: note that changing this will not change the
293 enclosing rect - this needs to be changed
294 separately manually
295 Description:
297 =cut
299 sub get_width {
300 my $self=shift;
301 return $self->{width};
305 sub set_width {
306 my $self=shift;
307 $self->{width}=shift;
313 =head2 accessors get_font(), set_font()
315 Synopsis: $io->set_font(GD::Font->Tiny)
316 Property: the GD font to be used when drawing this object
317 Side effects:
318 Description:
320 =cut
322 sub get_font {
323 my $self=shift;
324 return $self->{font};
327 sub set_font {
328 my $self=shift;
329 $self->{font}=shift;
332 =head2 function round()
334 Synopsis: my $rounded = round(4.3);
335 Arguments: a real number to be rounded
336 Returns: an int
337 Side effects: none
338 Description: Perl does not have a round function built in (it\'s in Math::round),
339 but we need it for rounding calculation of pixels.
340 Note: works also for negative numbers, astoninglishly
342 =cut
344 sub round {
345 my $value = shift;
346 my $int = int($value);
347 my $rest = abs($value)-abs($int);
348 if ($rest > 0.5) {
349 if ($value > 0) {
350 $int++;
352 else {
353 $int--;
356 #print STDERR "Rounded $value to $int\n";
357 return $int;