| 1 | package LPDS::RendererModel; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | use feature qw/say switch/; | ||
| 5 | |||
| 6 | use Glib qw/TRUE FALSE/; | ||
| 7 | use Gtk2; | ||
| 8 | use Goo::Canvas; | ||
| 9 | |||
| 10 | use LPDS::Util; | ||
| 11 | |||
| 12 | use constant { | ||
| 13 | COL_NAME => 0, | ||
| 14 | COL_CPU_NAME => 1, | ||
| 15 | COL_GPU_NAME => 2, | ||
| 16 | COL_MEM_SZ => 3, | ||
| 17 | COL_MEM_FREQ => 4, | ||
| 18 | COL_DISK_SZ => 5, | ||
| 19 | COL_DISK_ROT => 6, | ||
| 20 | COL_SCREEN_SZ => 7, | ||
| 21 | COL_PRICE => 8, | ||
| 22 | COL_COLOR => 9 | ||
| 23 | }; | ||
| 24 | |||
| 25 | has 'top_group' => ( | ||
| 26 | is => 'rw', | ||
| 27 | isa => 'Goo::Canvas::Group', | ||
| 28 | required => 1 | ||
| 29 | ); | ||
| 30 | |||
| 31 | has row => ( | ||
| 32 | is => 'ro', | ||
| 33 | isa => 'Gtk2::TreeRowReference', | ||
| 34 | required => 1 | ||
| 35 | ); | ||
| 36 | |||
| 37 | has parent => ( | ||
| 38 | is => 'ro', | ||
| 39 | isa => 'LPDS::Renderer', | ||
| 40 | weak_ref => 1, | ||
| 41 | required => 1 | ||
| 42 | ); | ||
| 43 | |||
| 44 | has axis_labels => ( | ||
| 45 | is => 'ro', | ||
| 46 | isa => 'HashRef[Goo::Canvas::Text]' | ||
| 47 | ); | ||
| 48 | |||
| 49 | has curve => ( | ||
| 50 | is => 'ro', | ||
| 51 | isa => 'Goo::Canvas::Path' | ||
| 52 | ); | ||
| 53 | |||
| 54 | has need_render => ( | ||
| 55 | is => 'rw', | ||
| 56 | isa => 'Bool', | ||
| 57 | default => 1 | ||
| 58 | ); | ||
| 59 | |||
| 60 | has selected => ( | ||
| 61 | is => 'ro', | ||
| 62 | isa => 'Bool', | ||
| 63 | default => 1, | ||
| 64 | ); | ||
| 65 | |||
| 66 | has handler => ( | ||
| 67 | is => 'rw', | ||
| 68 | ); | ||
| 69 | |||
| 70 | sub BUILD { | ||
| 71 | my $self = shift; | ||
| 72 | my $parent = $self->parent; | ||
| 73 | |||
| 74 | say "Model created"; | ||
| 75 | # create labels | ||
| 76 | my %labels; | ||
| 77 | |||
| 78 | foreach my $axis_name ( keys %{ $parent->axes } ) { | ||
| 79 | my $axis = $parent->axes->{$axis_name}; | ||
| 80 | my $x = $axis->x; | ||
| 81 | |||
| 82 | my $text = Goo::Canvas::Text->new( $self->top_group, '', $x, 0, -1, | ||
| 83 | 'south-west' ); | ||
| 84 | $labels{$axis_name} = $text; | ||
| 85 | } | ||
| 86 | $self->{axis_labels} = \%labels; | ||
| 87 | |||
| 88 | # create path | ||
| 89 | my $curve = Goo::Canvas::Path->new( $self->top_group, '' ); | ||
| 90 | $self->{curve} = $curve; | ||
| 91 | |||
| 92 | } | ||
| 93 | |||
| 94 | sub DESTROY { | ||
| 95 | my $self = shift; | ||
| 96 | my $group = $self->top_group; | ||
| 97 | |||
| 98 | foreach my $name ( keys %{ $self->axis_labels } ) { | ||
| 99 | my $label = $self->axis_labels->{$name}; | ||
| 100 | my $i = $group->find_child($label); | ||
| 101 | $group->remove_child($i); | ||
| 102 | } | ||
| 103 | |||
| 104 | my $curve = $self->curve; | ||
| 105 | my $i = $group->find_child($curve); | ||
| 106 | $group->remove_child($i); | ||
| 107 | } | ||
| 108 | |||
| 109 | sub get_name { | ||
| 110 | my $self = shift; | ||
| 111 | my $data = $self->row->get_model; | ||
| 112 | my $path = $self->row->get_path; | ||
| 113 | my $iter = $data->get_iter($path); | ||
| 114 | my $name = $data->get( $iter, COL_NAME ); | ||
| 115 | return $name; | ||
| 116 | } | ||
| 117 | |||
| 118 | sub get_color { | ||
| 119 | my $self = shift; | ||
| 120 | my $data = $self->row->get_model; | ||
| 121 | my $path = $self->row->get_path; | ||
| 122 | my $iter = $data->get_iter($path); | ||
| 123 | my $color = $data->get( $iter, COL_COLOR ); | ||
| 124 | return $color; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub get_iter { | ||
| 128 | my $self = shift; | ||
| 129 | my $data = $self->row->get_model; | ||
| 130 | my $path = $self->row->get_path; | ||
| 131 | return $data->get_iter($path); | ||
| 132 | } | ||
| 133 | |||
| 134 | sub render { | ||
| 135 | my $self = shift; | ||
| 136 | |||
| 137 | my $parent = $self->parent; | ||
| 138 | |||
| 139 | # say "render $self"; | ||
| 140 | my @path_points; | ||
| 141 | |||
| 142 | my $color = $self->get_color; | ||
| 143 | |||
| 144 | foreach my $axis_name ( @{ $parent->axes_order } ) { | ||
| 145 | my $axis = $parent->axes->{$axis_name}; | ||
| 146 | my $axis_mode = $axis->get_mode; | ||
| 147 | |||
| 148 | my $value = $self->get_data_by_axis( $axis_name, $axis_mode ); | ||
| 149 | |||
| 150 | if ( defined $value ) { | ||
| 151 | my $x = $axis->x; | ||
| 152 | my $y = $axis->calc_y($value); | ||
| 153 | # say "\t$axis_name - $axis_mode: $value at $y"; | ||
| 154 | |||
| 155 | push @path_points, [ $x, $y ]; | ||
| 156 | $self->axis_labels->{$axis_name}->set( | ||
| 157 | x => $x, | ||
| 158 | y => $y, | ||
| 159 | text => $value, | ||
| 160 | 'stroke-color-rgba' => $color, | ||
| 161 | 'fill-color-rgba' => $color | ||
| 162 | ); | ||
| 163 | } | ||
| 164 | else { | ||
| 165 | push @path_points, undef; | ||
| 166 | $self->axis_labels->{$axis_name}->set( text => '' ); | ||
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | my @cmd; | ||
| 171 | for ( my $i = 1 ; $i < @path_points ; $i++ ) { | ||
| 172 | next if !defined $path_points[$i] or !defined $path_points[ $i - 1 ]; | ||
| 173 | |||
| 174 | my @curr = @{ $path_points[$i] }; | ||
| 175 | my @prev = @{ $path_points[ $i - 1 ] }; | ||
| 176 | my $mid_x = ( $prev[0] + $curr[0] ) / 2; | ||
| 177 | my @prev_c = ( $mid_x, $prev[1] ); | ||
| 178 | my @curr_c = ( $mid_x, $curr[1] ); | ||
| 179 | |||
| 180 | push @cmd, | ||
| 181 | "M$prev[0],$prev[1]", | ||
| 182 | "C$prev_c[0],$prev_c[1] $curr_c[0],$curr_c[1] $curr[0],$curr[1]"; | ||
| 183 | |||
| 184 | } | ||
| 185 | |||
| 186 | $self->curve->set( | ||
| 187 | data => join( ' ', @cmd ), | ||
| 188 | 'stroke-color-rgba' => $color | ||
| 189 | ); | ||
| 190 | } | ||
| 191 | |||
| 192 | sub get_data_by_axis { | ||
| 193 | my $self = shift; | ||
| 194 | my $axis_name = shift; | ||
| 195 | my $mode = shift; | ||
| 196 | |||
| 197 | my $data = $self->row->get_model; | ||
| 198 | my $path = $self->row->get_path; | ||
| 199 | my $iter = $data->get_iter($path); | ||
| 200 | |||
| 201 | my $value; | ||
| 202 | given ($axis_name) { | ||
| 203 | when ('CPU') { | ||
| 204 | my %cpu = %{ $self->parent->CPU }; | ||
| 205 | my $cpu_name = $data->get( $iter, COL_CPU_NAME ); | ||
| 206 | if ( defined $cpu_name and defined $cpu{$cpu_name}{$mode} ) | ||
| 207 | { | ||
| 208 | $value = $cpu{$cpu_name}{$mode}; | ||
| 209 | } | ||
| 210 | else { | ||
| 211 | $value = undef; | ||
| 212 | } | ||
| 213 | } | ||
| 214 | when ('GPU') { | ||
| 215 | my %gpu = %{ $self->parent->GPU }; | ||
| 216 | my $gpu_name = $data->get( $iter, COL_GPU_NAME ); | ||
| 217 | if ( defined $gpu_name and defined $gpu{$gpu_name}{$mode} ) { | ||
| 218 | $value = $gpu{$gpu_name}{$mode}; | ||
| 219 | } | ||
| 220 | else { | ||
| 221 | $value = undef; | ||
| 222 | } | ||
| 223 | } | ||
| 224 | when ('Memory') { | ||
| 225 | if ( $mode eq 'size' ) { | ||
| 226 | $value = $data->get( $iter, COL_MEM_SZ ); | ||
| 227 | } | ||
| 228 | elsif ( $mode eq 'frequency' ) { | ||
| 229 | $value = $data->get( $iter, COL_MEM_FREQ ); | ||
| 230 | } | ||
| 231 | else { | ||
| 232 | confess "invalid mem axis: $axis_name - $mode"; | ||
| 233 | } | ||
| 234 | } | ||
| 235 | when ('Disk') { | ||
| 236 | if ( $mode eq 'size' ) { | ||
| 237 | $value = $data->get( $iter, COL_DISK_SZ ); | ||
| 238 | } | ||
| 239 | elsif ( $mode eq 'RPM' ) { | ||
| 240 | $value = $data->get( $iter, COL_DISK_ROT ); | ||
| 241 | } | ||
| 242 | else { | ||
| 243 | confess "invalid mem axis: $axis_name - $mode"; | ||
| 244 | } | ||
| 245 | } | ||
| 246 | when ('Screen') { | ||
| 247 | $value = $data->get( $iter, COL_SCREEN_SZ ); | ||
| 248 | } | ||
| 249 | when ('Price') { | ||
| 250 | $value = $data->get( $iter, COL_PRICE ); | ||
| 251 | } | ||
| 252 | default { | ||
| 253 | confess 'unknown axis name: $axis_name'; | ||
| 254 | } | ||
| 255 | } | ||
| 256 | |||
| 257 | return $value; | ||
| 258 | } | ||
| 259 | |||
| 260 | sub set_selected { | ||
| 261 | my $self = shift; | ||
| 262 | my $value = shift; | ||
| 263 | |||
| 264 | return undef if $self->selected == $value; | ||
| 265 | |||
| 266 | $self->{selected} = $value; | ||
| 267 | |||
| 268 | foreach my $name ( keys %{ $self->axis_labels } ) { | ||
| 269 | my $label = $self->axis_labels->{$name}; | ||
| 270 | if ($value) { | ||
| 271 | $label->set( visibility => 'visible' ); | ||
| 272 | } | ||
| 273 | else { | ||
| 274 | $label->set( visibility => 'invisible' ); | ||
| 275 | } | ||
| 276 | } | ||
| 277 | |||
| 278 | my $curve = $self->curve; | ||
| 279 | my $color = $curve->get('stroke_color_rgba'); | ||
| 280 | if ($value) { | ||
| 281 | $color |= 0x000000ff; | ||
| 282 | } | ||
| 283 | else { | ||
| 284 | $color &= 0xffffff00; | ||
| 285 | $color += 0x3f; | ||
| 286 | } | ||
| 287 | $curve->set( 'stroke_color_rgba' => $color ); | ||
| 288 | |||
| 289 | } | ||
| 290 | |||
| 291 | sub render_if_needed { | ||
| 292 | my $self = shift; | ||
| 293 | $self->render if $self->need_render; | ||
| 294 | } | ||
| 295 | |||
| 296 | 1; |