| 1 | package Gtk2::Ex::TreeMap; | ||
| 2 | |||
| 3 | our $VERSION = '0.02'; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use Data::Dumper; | ||
| 8 | use Glib qw /TRUE FALSE/; | ||
| 9 | use GD; | ||
| 10 | use Gtk2 -init; | ||
| 11 | |||
| 12 | sub new { | ||
| 13 | my ($class, $size) = @_; | ||
| 14 | my $self = {}; | ||
| 15 | $self->{tree} = undef; | ||
| 16 | $self->{rectangles} = undef; | ||
| 17 | $self->{eventbox} = undef; | ||
| 18 | $self->{selected} = undef; | ||
| 19 | $self->{size} = $size; | ||
| 20 | $self->{image} = new GD::Image(@$size); | ||
| 21 | $self->{signals} = {}; | ||
| 22 | bless ($self, $class); | ||
| 23 | return $self; | ||
| 24 | } | ||
| 25 | |||
| 26 | sub signal_connect { | ||
| 27 | my ($self, $event, $callback) = @_; | ||
| 28 | $self->{signals}->{$event} = $callback; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub draw_map_simple { | ||
| 32 | my ($self, $list) = @_; | ||
| 33 | my $tree = {}; | ||
| 34 | $tree->{Node} = []; | ||
| 35 | my $color = 120; | ||
| 36 | my $count = 0; | ||
| 37 | my $color_incr = (255-120)/($#{@$list}+1); | ||
| 38 | foreach my $element (@$list) { | ||
| 39 | push @{$tree->{Node}}, { size => $element, color => "0,0,$color", description => $count++ }; | ||
| 40 | $color += $color_incr; | ||
| 41 | } | ||
| 42 | $self->draw_map($tree); | ||
| 43 | } | ||
| 44 | |||
| 45 | sub draw_map { | ||
| 46 | my ($self, $tree) = @_; | ||
| 47 | _purify_tree($tree); | ||
| 48 | $self->{tree} = $tree; | ||
| 49 | my $rectangle = [0, 0, $self->{size}->[0], $self->{size}->[1]]; | ||
| 50 | $self->_build_rectangles($rectangle, $tree); | ||
| 51 | $self->_paint_rectangles($tree); | ||
| 52 | $self->_build_event_box; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub get_image { | ||
| 56 | my ($self) = @_; | ||
| 57 | return $self->{eventbox}; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub _purify_tree { | ||
| 61 | my ($data) = @_; | ||
| 62 | if (!$data->{Node}) { | ||
| 63 | # This is the child node | ||
| 64 | # Fill in the size, color information if missing. | ||
| 65 | $data->{size} = 0 unless $data->{size}; | ||
| 66 | $data->{color} = '0,0,0' unless $data->{color}; | ||
| 67 | return $data; | ||
| 68 | } else { | ||
| 69 | my @list; | ||
| 70 | foreach my $child (@{$data->{Node}}) { | ||
| 71 | push @list, _purify_tree($child); | ||
| 72 | } | ||
| 73 | my $temp = _aggregate_size(\@list); | ||
| 74 | $data->{size} = $temp->{size}; | ||
| 75 | $data->{color} = $temp->{color}; | ||
| 76 | @{$data->{Node}} = reverse sort { $a->{size} <=> $b->{size} } @{$data->{Node}}; | ||
| 77 | return _aggregate_size(\@list); | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | sub _aggregate_size { | ||
| 82 | my ($data) = @_; | ||
| 83 | my $size = 0; | ||
| 84 | foreach my $element (@$data) { | ||
| 85 | $size += $element->{size}; | ||
| 86 | } | ||
| 87 | return { size => $size, color => '0,0,0'}; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub _build_rectangles { | ||
| 91 | my ($self, $size, $tree) = @_; | ||
| 92 | return unless exists($tree->{Node}); | ||
| 93 | my $values = []; | ||
| 94 | foreach my $element (@{$tree->{Node}}) { | ||
| 95 | push @$values, $element->{size} if ($element->{size}); | ||
| 96 | } | ||
| 97 | my $rectangles = []; | ||
| 98 | my $width = abs($size->[2] - $size->[0]); | ||
| 99 | my $height = abs($size->[3] - $size->[1]); | ||
| 100 | if ($width > $height) { | ||
| 101 | $self->_draw_squarified($size, $values, $rectangles, 'horizontal'); | ||
| 102 | } else { | ||
| 103 | $self->_draw_squarified($size, $values, $rectangles, 'vertical'); | ||
| 104 | } | ||
| 105 | if ($#{@$rectangles} > 0) { | ||
| 106 | $tree->{rectangles} = $rectangles; | ||
| 107 | my $count = 0; | ||
| 108 | foreach my $child (@{$tree->{Node}}) { | ||
| 109 | $self->_build_rectangles($rectangles->[$count++], $child); | ||
| 110 | } | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | sub _paint_rectangles { | ||
| 115 | my ($self, $tree) = @_; | ||
| 116 | return unless exists($tree->{rectangles}); | ||
| 117 | my $image = $self->{image}; | ||
| 118 | my $black = $image->colorAllocate(0,0,0); | ||
| 119 | my $count = 0; | ||
| 120 | foreach my $rect(@{$tree->{rectangles}}) { | ||
| 121 | $image->rectangle(@$rect, $black); | ||
| 122 | my $middle_x = ($rect->[0] + $rect->[2])/2; | ||
| 123 | my $middle_y = ($rect->[1] + $rect->[3])/2; | ||
| 124 | #my $colorlist = [int(rand()*255),int(rand()*255),int(rand()*255)]; | ||
| 125 | my $colorstring = $tree->{Node}->[$count++]->{color}; | ||
| 126 | my @colorlist = split /,/, $colorstring; | ||
| 127 | my $color = $image->colorResolve(@colorlist); | ||
| 128 | $image->fill($middle_x, $middle_y, $color); | ||
| 129 | } | ||
| 130 | foreach my $child (@{$tree->{Node}}) { | ||
| 131 | $self->_paint_rectangles($child); | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | sub _build_event_box { | ||
| 136 | my ($self) = @_; | ||
| 137 | my $loader = Gtk2::Gdk::PixbufLoader->new; | ||
| 138 | $loader->write ($self->{image}->png); | ||
| 139 | $loader->close; | ||
| 140 | my $image = Gtk2::Image->new_from_pixbuf($loader->get_pixbuf); | ||
| 141 | my $eventbox = Gtk2::EventBox->new; | ||
| 142 | $eventbox->add ($image); | ||
| 143 | $eventbox->add_events (['pointer-motion-mask', 'pointer-motion-hint-mask']); | ||
| 144 | $eventbox->signal_connect ('size-allocate' => | ||
| 145 | sub { | ||
| 146 | my ($widget, $event) = @_; | ||
| 147 | my @imageallocatedsize = $eventbox->allocation->values; | ||
| 148 | $self->{imageallocatedsize} = \@imageallocatedsize; | ||
| 149 | return 0; | ||
| 150 | } | ||
| 151 | ); | ||
| 152 | $eventbox->signal_connect ('motion-notify-event' => | ||
| 153 | sub { | ||
| 154 | my ($widget, $event) = @_; | ||
| 155 | my ($x, $y) = ($event->x, $event->y); | ||
| 156 | $x -= ($self->{imageallocatedsize}->[2] - $self->{size}->[0])/2; | ||
| 157 | $y -= ($self->{imageallocatedsize}->[3] - $self->{size}->[1])/2; | ||
| 158 | my $path = []; | ||
| 159 | $self->_get_path_at_pos($self->{tree}, $path, $x, $y); | ||
| 160 | if ($#{@$path} >= 0) { | ||
| 161 | my @clonepath = @$path; | ||
| 162 | my $chosen_rectangle = $self->_get_rectangle_at_path(\@clonepath, $self->{tree}); | ||
| 163 | $self->_highlight_rectangle($chosen_rectangle); | ||
| 164 | @clonepath = @$path; | ||
| 165 | my $chosen_node = $self->_get_node_at_path(\@clonepath, $self->{tree}); | ||
| 166 | if ($self->{signals}->{'mouse-over'}) { | ||
| 167 | my @clonepath = @$path; | ||
| 168 | &{$self->{signals}->{'mouse-over'}}($x, $y, \@clonepath, $chosen_node) | ||
| 169 | } | ||
| 170 | } | ||
| 171 | } | ||
| 172 | ); | ||
| 173 | $self->{eventbox} = $eventbox; | ||
| 174 | } | ||
| 175 | |||
| 176 | sub _highlight_rectangle { | ||
| 177 | my ($self, $rect) = @_; | ||
| 178 | my $eventbox = $self->{eventbox}; | ||
| 179 | my @children = $eventbox->get_children; | ||
| 180 | foreach my $child (@children) { | ||
| 181 | $eventbox->remove($child); | ||
| 182 | } | ||
| 183 | my $im = $self->{image}->clone; | ||
| 184 | my $white = $im->colorAllocate(255,255,255); | ||
| 185 | $im->rectangle(@$rect, $white); | ||
| 186 | my $loader = Gtk2::Gdk::PixbufLoader->new; | ||
| 187 | $loader->write ($im->png); | ||
| 188 | $loader->close; | ||
| 189 | my $image = Gtk2::Image->new_from_pixbuf($loader->get_pixbuf); | ||
| 190 | $eventbox->add($image); | ||
| 191 | $eventbox->show_all; | ||
| 192 | } | ||
| 193 | |||
| 194 | |||
| 195 | |||
| 196 | sub _get_path_at_pos { | ||
| 197 | my ($self, $tree, $path, $x, $y) = @_; | ||
| 198 | my $count = 0; | ||
| 199 | foreach my $rect(@{$tree->{rectangles}}) { | ||
| 200 | if ($x > $rect->[0] and $x < $rect->[2] and $y > $rect->[1] and $y < $rect->[3]) { | ||
| 201 | push @$path, $count; | ||
| 202 | $self->_get_path_at_pos($tree->{Node}->[$count], $path, $x, $y); | ||
| 203 | } | ||
| 204 | $count++; | ||
| 205 | } | ||
| 206 | } | ||
| 207 | |||
| 208 | sub _get_node_at_path { | ||
| 209 | my ($self, $path, $tree) = @_; | ||
| 210 | if ($#{@$path} == 0) { | ||
| 211 | return $tree->{Node}->[$path->[0]]; | ||
| 212 | } | ||
| 213 | my $child = $tree->{Node}->[$path->[0]]; | ||
| 214 | shift @$path; | ||
| 215 | $self->_get_node_at_path($path, $child); | ||
| 216 | } | ||
| 217 | |||
| 218 | sub _get_rectangle_at_path { | ||
| 219 | my ($self, $path, $tree) = @_; | ||
| 220 | if ($#{@$path} == 0) { | ||
| 221 | return $tree->{rectangles}->[$path->[0]]; | ||
| 222 | } | ||
| 223 | my $child = $tree->{Node}->[$path->[0]]; | ||
| 224 | shift @$path; | ||
| 225 | $self->_get_rectangle_at_path($path, $child); | ||
| 226 | } | ||
| 227 | |||
| 228 | sub _draw_squarified { | ||
| 229 | my ($self, $rect, $values, $rectangles, $direction) = @_; | ||
| 230 | if ($#{@$values} == 0) { | ||
| 231 | push @$rectangles, $rect; | ||
| 232 | return; | ||
| 233 | } | ||
| 234 | my $sum = 0; | ||
| 235 | foreach my $x (@$values) { | ||
| 236 | $sum += $x; | ||
| 237 | } | ||
| 238 | my $best_aspect_ratio = 0; | ||
| 239 | my $best_rectangles; | ||
| 240 | my $width = abs($rect->[2] - $rect->[0]); | ||
| 241 | my $height = abs($rect->[3] - $rect->[1]); | ||
| 242 | my ($x1, $y1, $x2, $y2); | ||
| 243 | if ($direction eq 'horizontal') { | ||
| 244 | for (my $i=0; $i<=$#{@$values}; $i++) { | ||
| 245 | ($x1, $y1) = ($rect->[0], $rect->[1]); | ||
| 246 | my @temp; | ||
| 247 | my $localsum = 0; | ||
| 248 | for (my $j=0; $j<=$i; $j++) { | ||
| 249 | $localsum += $values->[$j]; | ||
| 250 | } | ||
| 251 | $x2 = int($x1 + $width*$localsum/$sum); | ||
| 252 | for (my $j=0; $j<=$i; $j++) { | ||
| 253 | $y2 = int($y1 + $height*$values->[$j]/$localsum); | ||
| 254 | push @temp, [$x1, $y1, $x2, $y2]; | ||
| 255 | $y1 = $y2; | ||
| 256 | } | ||
| 257 | my $aspect_ratio = _calc_best_aspect_ratio(\@temp); | ||
| 258 | if ($aspect_ratio >= $best_aspect_ratio) { | ||
| 259 | $best_aspect_ratio = $aspect_ratio; | ||
| 260 | $best_rectangles = \@temp; | ||
| 261 | } else { | ||
| 262 | foreach my $rect(@$best_rectangles) { | ||
| 263 | push @$rectangles, $rect; | ||
| 264 | } | ||
| 265 | my ($x1, $y1, $x2, $y2) = ($best_rectangles->[$i-1]->[2], $rect->[1], $rect->[2], $rect->[3]); | ||
| 266 | for (my $j=0; $j<$i; $j++) { | ||
| 267 | shift @$values; | ||
| 268 | } | ||
| 269 | $self->_draw_squarified([$x1, $y1, $x2, $y2], $values, $rectangles, 'vertical'); | ||
| 270 | return; | ||
| 271 | } | ||
| 272 | } | ||
| 273 | foreach my $rect(@$best_rectangles) { | ||
| 274 | push @$rectangles, $rect; | ||
| 275 | } | ||
| 276 | } elsif ($direction eq 'vertical') { | ||
| 277 | for (my $i=0; $i<=$#{@$values}; $i++) { | ||
| 278 | ($x1, $y1) = ($rect->[0], $rect->[1]); | ||
| 279 | my @temp; | ||
| 280 | my $localsum = 0; | ||
| 281 | for (my $j=0; $j<=$i; $j++) { | ||
| 282 | $localsum += $values->[$j]; | ||
| 283 | } | ||
| 284 | $y2 = int($y1 + $height*$localsum/$sum); | ||
| 285 | for (my $j=0; $j<=$i; $j++) { | ||
| 286 | $x2 = int($x1 + $width*$values->[$j]/$localsum); | ||
| 287 | push @temp, [$x1, $y1, $x2, $y2]; | ||
| 288 | $x1 = $x2; | ||
| 289 | } | ||
| 290 | my $aspect_ratio = _calc_best_aspect_ratio(\@temp); | ||
| 291 | if ($aspect_ratio >= $best_aspect_ratio) { | ||
| 292 | $best_aspect_ratio = $aspect_ratio; | ||
| 293 | $best_rectangles = \@temp; | ||
| 294 | } else { | ||
| 295 | foreach my $rect(@$best_rectangles) { | ||
| 296 | push @$rectangles, $rect; | ||
| 297 | } | ||
| 298 | my ($x1, $y1, $x2, $y2) = ($rect->[0], $best_rectangles->[$i-1]->[3], $rect->[2], $rect->[3]); | ||
| 299 | for (my $j=0; $j<$i; $j++) { | ||
| 300 | shift @$values; | ||
| 301 | } | ||
| 302 | $self->_draw_squarified([$x1, $y1, $x2, $y2], $values, $rectangles, 'horizontal'); | ||
| 303 | return; | ||
| 304 | } | ||
| 305 | } | ||
| 306 | foreach my $rect(@$best_rectangles) { | ||
| 307 | push @$rectangles, $rect; | ||
| 308 | } | ||
| 309 | } | ||
| 310 | } | ||
| 311 | |||
| 312 | sub _calc_best_aspect_ratio { | ||
| 313 | my ($rectangles) = @_; | ||
| 314 | my @aspect; | ||
| 315 | foreach my $r (@$rectangles) { | ||
| 316 | if ($r->[1] == $r->[3] or $r->[0] == $r->[2]) { | ||
| 317 | return 0; | ||
| 318 | } | ||
| 319 | my $l = abs ( ($r->[0] - $r->[2])/($r->[1] - $r->[3]) ); | ||
| 320 | my $h = abs ( ($r->[1] - $r->[3])/($r->[0] - $r->[2]) ); | ||
| 321 | push @aspect, $l < $h ? $l : $h; | ||
| 322 | } | ||
| 323 | return min(@aspect); | ||
| 324 | } | ||
| 325 | |||
| 326 | sub min { | ||
| 327 | my (@values) = @_; | ||
| 328 | my $min = $values[0]; | ||
| 329 | foreach my $x (@values) { | ||
| 330 | $min = $x if ($x < $min); | ||
| 331 | } | ||
| 332 | return $min; | ||
| 333 | } | ||
| 334 | |||
| 335 | 1; | ||
| 336 | |||
| 337 | __END__ |