blib/lib/Video/DVDRip/GUI/FormFactory/ClipImage.pm untested

filter: Statementstmt 0.0% 0 / 296 Branchbran 0.0% 0 / 66 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 55 Podpod 0.0% 0 / 52 totaltotal 0.0% 0 / 509 CC 34 SCAR 28.1
CC
34
Cov
0%
move_dragged_line
56.0
draw
42.0
motion_notify
42.0
CRAP 16.6
1 # $Id: ClipImage.pm 2187 2006-08-16 19:34:38Z joern $
2  
3 #-----------------------------------------------------------------------
4 # Copyright (C) 2001-2006 Jörn Reder <joern AT zyn.de>.
5 # All Rights Reserved. See file COPYRIGHT for details.
6 #
7 # This module is part of Video::DVDRip, which is free software; you can
8 # redistribute it and/or modify it under the same terms as Perl itself.
9 #-----------------------------------------------------------------------
10  
11 package Video::DVDRip::GUI::FormFactory::ClipImage;
12  
13 use strict;
14  
15 use base qw( Gtk2::Ex::FormFactory::Widget );
16  
17 use Locale::TextDomain qw (video.dvdrip);
18  
19 (
20 top => __"Top",
21 bottom => __"Bottom",
22 left => __"Left",
23 right => __"Right",
24 );
25  
26 __"Conformance check:";
27  
28 sub get_type { "dvdrip_clip_image" }
29  
30 sub has_additional_attrs { shift->{get_no_clip} ? [] : [qw( left right top bottom )] }
31  
32 sub get_image_type { shift->{image_type} }
33 sub get_attr_left { shift->{attr_left} }
34 sub get_attr_right { shift->{attr_right} }
35 sub get_attr_top { shift->{attr_top} }
36 sub get_attr_bottom { shift->{attr_bottom} }
37 sub get_no_clip { shift->{no_clip} }
38 sub get_file_error { shift->{file_error} }
39  
40 sub set_image_type { shift->{image_type} = $_[1] }
41 sub set_attr_left { shift->{attr_left} = $_[1] }
42 sub set_attr_right { shift->{attr_right} = $_[1] }
43 sub set_attr_top { shift->{attr_top} = $_[1] }
44 sub set_attr_bottom { shift->{attr_bottom} = $_[1] }
45 sub set_no_clip { shift->{no_clip} = $_[1] }
46 sub set_file_error { shift->{file_error} = $_[1] }
47  
48 sub get_gtk_pixbuf { shift->{gtk_pixbuf} }
49 sub get_gtk_v_cursor { shift->{gtk_v_cursor} }
50 sub get_gtk_h_cursor { shift->{gtk_h_cursor} }
51 sub get_gtk_n_cursor { shift->{gtk_n_cursor} }
52 sub get_clip_lines { shift->{clip_lines} }
53 sub get_line_under_cursor { shift->{line_under_cursor} }
54 sub get_dragged_line { shift->{dragged_line} }
55 sub get_clipping_changed { shift->{clipping_changed} }
56  
57 sub set_gtk_pixbuf { shift->{gtk_pixbuf} = $_[1] }
58 sub set_gtk_v_cursor { shift->{gtk_v_cursor} = $_[1] }
59 sub set_gtk_h_cursor { shift->{gtk_h_cursor} = $_[1] }
60 sub set_gtk_n_cursor { shift->{gtk_n_cursor} = $_[1] }
61 sub set_clip_lines { shift->{clip_lines} = $_[1] }
62 sub set_line_under_cursor { shift->{line_under_cursor} = $_[1] }
63 sub set_dragged_line { shift->{dragged_line} = $_[1] }
64 sub set_clipping_changed { shift->{clipping_changed} = $_[1] }
65  
66 sub get_gtk_entries { shift->{gtk_entries} }
67 sub get_gtk_status_label { shift->{gtk_status_label} }
68  
69 sub get_in_entry_update { shift->{in_entry_update} }
70 sub set_gtk_status_label { shift->{gtk_status_label} = $_[1] }
71  
72 sub set_gtk_entries { shift->{gtk_entries} = $_[1] }
73 sub set_in_entry_update { shift->{in_entry_update} = $_[1] }
74  
75 sub new {
76 my $class = shift;
77 my %par = @_;
78 my ($image_type, $attr_left, $attr_right, $attr_top) =
79 @par{'image_type','attr_left','attr_right','attr_top'};
80 my ($attr_bottom, $no_clip) =
81 @par{'attr_bottom','no_clip'};
82  
83 my $self = $class->SUPER::new(%par);
84  
85 $self->set_image_type($image_type);
86 $self->set_attr_left($attr_left);
87 $self->set_attr_right($attr_right);
88 $self->set_attr_top($attr_top);
89 $self->set_attr_bottom($attr_bottom);
90 $self->set_no_clip($no_clip);
91  
92 return $self;
93 }
94  
95 sub build_widget {
96 my $self = shift;
97  
98 my $gtk_drawing_area = Gtk2::DrawingArea->new;
99 my $gtk_event_box = Gtk2::EventBox->new;
100  
101 $gtk_event_box->add($gtk_drawing_area);
102 $gtk_event_box->modify_bg( "normal", Gtk2::Gdk::Color->parse("#ffffff") );
103  
104 $gtk_drawing_area->signal_connect(
105 expose_event => sub { $self->draw( $_[1] ) }, );
106  
107 $self->set_gtk_n_cursor( Gtk2::Gdk::Cursor->new('GDK_CROSSHAIR') );
108 $self->set_gtk_v_cursor(
109 Gtk2::Gdk::Cursor->new('GDK_SB_V_DOUBLE_ARROW') );
110 $self->set_gtk_h_cursor(
111 Gtk2::Gdk::Cursor->new('GDK_SB_H_DOUBLE_ARROW') );
112  
113 if ( $self->get_no_clip ) {
114 $self->set_gtk_widget($gtk_drawing_area);
115 $self->set_gtk_parent_widget($gtk_event_box);
116 return;
117 }
118  
119 $gtk_event_box->set_events(
120 [ 'button_press_mask', 'pointer-motion-mask' ] );
121  
122 $gtk_event_box->signal_connect(
123 button_press_event => sub { $self->button_press( $_[1] ) }, );
124 $gtk_event_box->signal_connect(
125 button_release_event => sub { $self->button_release() }, );
126 $gtk_event_box->signal_connect(
127 motion_notify_event => sub { $self->motion_notify( $_[1] ) }, );
128  
129 my $gtk_vbox = Gtk2::VBox->new;
130 $gtk_vbox->pack_start($gtk_event_box, 0, 1, 0);
131  
132 my $gtk_hbox = Gtk2::HBox->new;
133 $gtk_vbox->pack_start($gtk_hbox, 0, 1, 0);
134  
135 my %fields = (
136 top => __"Top",
137 bottom => __"Bottom",
138 left => __"Left",
139 right => __"Right",
140 );
141  
142 my %gtk_entries;
143  
144 foreach my $field ( qw(top bottom left right ) ) {
145 my $gtk_label = Gtk2::Label->new($fields{$field});
146 $gtk_hbox->pack_start($gtk_label, 0, 1, 0);
147 my $gtk_entry = Gtk2::Entry->new();
148 $gtk_entry->set_width_chars(5);
149 $gtk_hbox->pack_start($gtk_entry, 0, 1, 0);
150 $gtk_entries{$field} = $gtk_entry;
151 $gtk_entry->signal_connect (
152 changed => sub {
153 return if $self->get_in_entry_update;
154 my $attr_method = "get_attr_".$field;
155 $self->set_object_value( $self->$attr_method, $gtk_entry->get_text );
156 $self->set_clipping_changed(1);
157 1;
158 },
159 );
160 }
161  
162 my $gtk_label = Gtk2::Label->new(__"Conformance check:");
163 $gtk_hbox->pack_start($gtk_label, 0, 1, 0);
164  
165 my $gtk_status_label = Gtk2::Label->new("");
166 $gtk_hbox->pack_start($gtk_status_label, 0, 1, 0);
167  
168 $self->set_gtk_entries(\%gtk_entries);
169 $gtk_vbox->show_all;
170
171 $self->set_gtk_status_label($gtk_status_label);
172 $self->set_gtk_widget($gtk_drawing_area);
173 $self->set_gtk_parent_widget($gtk_vbox);
174  
175 1;
176 }
177  
178 sub cleanup_xxx {
179 my $self = shift;
180
181 $self->set_gtk_status_label(undef);
182 $self->set_gtk_entries(undef);
183 $self->set_gtk_pixbuf(undef);
184 $self->set_gtk_v_cursor(undef);
185 $self->set_gtk_h_cursor(undef);
186 $self->set_gtk_n_cursor(undef);
187
188 1;
189 }
190  
191 sub object_to_widget {
192 my $self = shift;
193  
194 my $filename = $self->get_object_value;
195  
196 if ( !-f $filename ) {
197 $self->set_file_error(1);
198 $self->empty_widget;
199 return 1;
200 }
201  
202 $self->set_file_error(0);
203  
204 my $gtk_pixbuf = Gtk2::Gdk::Pixbuf->new_from_file($filename);
205 $self->set_gtk_pixbuf($gtk_pixbuf);
206  
207 my $image_width = $gtk_pixbuf->get_width;
208 my $image_height = $gtk_pixbuf->get_height;
209  
210 $self->get_gtk_widget->set_size_request( $image_width + 1,
211 $image_height + 1 );
212  
213 $self->calc_clip_lines;
214  
215 if ( $self->get_dragged_line ) {
216 $self->draw_clip_lines;
217 }
218 else {
219 $self->draw;
220 }
221  
222 1;
223 }
224  
225 sub empty_widget {
226 my $self = shift;
227  
228 my $gtk_drawing_area = $self->get_gtk_widget;
229 my $gtk_pixbuf = Gtk2::Gdk::Pixbuf->new( 'rgb', 0, 8, 2048, 2048 );
230 $self->set_gtk_pixbuf($gtk_pixbuf);
231 $self->draw;
232  
233 1;
234 }
235  
236 sub draw {
237 my $self = shift;
238 my ($event) = @_;
239  
240 my $gtk_drawing_area = $self->get_gtk_widget;
241 my $gtk_pixbuf = $self->get_gtk_pixbuf;
242  
243 my $drawable = $gtk_drawing_area->window;
244 my $black_gc = $gtk_drawing_area->style->black_gc;
245 my $white_gc = $gtk_drawing_area->style->white_gc;
246  
247 my ( $x, $y, $width, $height );
248  
249 if ($event) {
250 $x = $event->area->x;
251 $y = $event->area->y;
252 $width = $event->area->width;
253 $height = $event->area->height;
254 }
255 else {
256 $x = $y = 0;
257 $width = $gtk_pixbuf->get_width;
258 $height = $gtk_pixbuf->get_height;
259 }
260  
261 if ( $x + $width > $gtk_pixbuf->get_width ) {
262 $width = $gtk_pixbuf->get_width - $x;
263 }
264  
265 if ( $y + $height > $gtk_pixbuf->get_height ) {
266 $height = $gtk_pixbuf->get_height - $y;
267 }
268  
269 $gtk_pixbuf->render_to_drawable( $drawable, $black_gc, $x, $y, $x, $y,
270 $width, $height, "none", 0, 0 );
271  
272 return if $self->get_file_error;
273  
274 $self->draw_clip_lines unless $self->get_no_clip;
275  
276 1;
277 }
278  
279 sub draw_clip_lines {
280 my $self = shift;
281  
282 my $gtk_drawing_area = $self->get_gtk_widget;
283 my $gtk_pixbuf = $self->get_gtk_pixbuf;
284  
285 my $drawable = $gtk_drawing_area->window;
286 my $white_gc = $gtk_drawing_area->style->white_gc;
287  
288 my $clip_lines = $self->get_clip_lines;
289 my $gtk_entries = $self->get_gtk_entries;
290  
291 my $proxy = $self->get_proxy;
292  
293 my $line;
294 foreach my $type ( keys %{$clip_lines} ) {
295 $line = $clip_lines->{$type};
296 $drawable->draw_line( $white_gc, @{$line} );
297 my $value = $self->calc_clip_value($type);
298 $self->set_in_entry_update(1);
299 $gtk_entries->{$type}->set_text($value);
300 my $attr_method = "get_attr_".$type;
301 $proxy->set_attr($self->$attr_method(), $value, 1);
302 $self->set_in_entry_update(0);
303 }
304  
305 my $title = $proxy->get_object;
306  
307 my $text = $title->preview_label(
308 type => $self->get_image_type,
309 details => 1,
310 );
311  
312 $self->get_gtk_status_label->set_markup($text);
313  
314 1;
315 }
316  
317 sub clear_clip_line {
318 my $self = shift;
319 my ($type) = @_;
320  
321 my $gtk_drawing_area = $self->get_gtk_widget;
322 my $gtk_pixbuf = $self->get_gtk_pixbuf;
323  
324 my $drawable = $gtk_drawing_area->window;
325 my $black_gc = $gtk_drawing_area->style->black_gc;
326  
327 my $clip_lines = $self->get_clip_lines;
328  
329 my $line = $clip_lines->{$type};
330  
331 my ( $src_x, $src_y, $dst_x, $dst_y, $width, $height );
332  
333 if ( $type eq 'top' or $type eq 'bottom' ) {
334 $src_x = $dst_x = 0;
335 $src_y = $dst_y = $line->[1];
336 $width = $gtk_pixbuf->get_width;
337 $height = 1;
338 return if $src_y >= $gtk_pixbuf->get_height;
339 }
340 else {
341 $src_x = $dst_x = $line->[0];
342 $src_y = $dst_y = 0;
343 $width = 1;
344 $height = $gtk_pixbuf->get_height;
345 return if $src_x >= $gtk_pixbuf->get_width;
346 }
347  
348 $gtk_pixbuf->render_to_drawable(
349 $drawable, $black_gc, $src_x, $src_y, $src_x, $src_y,
350 $width, $height, "none", 0, 0
351 );
352  
353 1;
354 }
355  
356 sub calc_clip_lines {
357 my $self = shift;
358  
359 my $top = $self->get_object_value( $self->get_attr_top );
360 my $bottom = $self->get_object_value( $self->get_attr_bottom );
361 my $left = $self->get_object_value( $self->get_attr_left );
362 my $right = $self->get_object_value( $self->get_attr_right );
363  
364 my $gtk_pixbuf = $self->get_gtk_pixbuf;
365 my $width = $gtk_pixbuf->get_width;
366 my $height = $gtk_pixbuf->get_height;
367  
368 $self->set_clip_lines(
369 { top => [ 0, $top, $width - 1, $top ],
370 bottom => [ 0, $height - $bottom, $width - 1, $height - $bottom ],
371 left => [ $left, 0, $left, $height - 1 ],
372 right => [ $width - $right, 0, $width - $right, $height - 1 ],
373 }
374 );
375  
376 1;
377 }
378  
379 sub motion_notify {
380 my $self = shift;
381 my ($event) = @_;
382  
383 return if $self->get_file_error;
384  
385 if ( $self->get_dragged_line ) {
386 $self->move_dragged_line($event);
387 return 1;
388 }
389  
390 my $threshold = 5;
391  
392 my $x = $event->x;
393 my $y = $event->y;
394  
395 my $clip_lines = $self->get_clip_lines;
396  
397 my $line_under_cursor = "";
398  
399 foreach my $type ( "top", "bottom" ) {
400 my $line = $clip_lines->{$type};
401 if ( $y >= $line->[1] - $threshold
402 && $y <= $line->[1] + $threshold ) {
403 $self->get_gtk_widget->window->set_cursor(
404 $self->get_gtk_v_cursor, );
405 $line_under_cursor = $type;
406 last;
407 }
408 }
409  
410 foreach my $type ( "left", "right" ) {
411 my $line = $clip_lines->{$type};
412 if ( $x >= $line->[0] - $threshold
413 && $x <= $line->[0] + $threshold ) {
414 $self->get_gtk_widget->window->set_cursor(
415 $self->get_gtk_h_cursor, );
416 $line_under_cursor = $type;
417 last;
418 }
419 }
420  
421 if ( not $line_under_cursor
422 and $line_under_cursor ne $self->get_line_under_cursor ) {
423 $self->get_gtk_widget->window->set_cursor( $self->get_gtk_n_cursor, );
424 }
425  
426 $self->set_line_under_cursor($line_under_cursor);
427  
428 1;
429 }
430  
431 sub button_press {
432 my $self = shift;
433 my ($event) = @_;
434  
435 my $line_under_cursor = $self->get_line_under_cursor;
436 return if not $line_under_cursor;
437  
438 $self->set_dragged_line($line_under_cursor);
439  
440 1;
441 }
442  
443 sub move_dragged_line {
444 my $self = shift;
445 my ($event) = @_;
446  
447 my $type = $self->get_dragged_line;
448 my $x = $event->x;
449 my $y = $event->y;
450  
451 my $gtk_pixbuf = $self->get_gtk_pixbuf;
452 my $width = $gtk_pixbuf->get_width;
453 my $height = $gtk_pixbuf->get_height;
454  
455 $x = $width if $x > $width;
456 $y = $height if $y > $height;
457  
458 $x = 0 if $x < 0;
459 $y = 0 if $y < 0;
460  
461 my $clip_lines = $self->get_clip_lines;
462  
463 $self->clear_clip_line($type);
464  
465 $x = int( $x / 2 ) * 2;
466 $y = int( $y / 2 ) * 2;
467  
468 if ( $type eq 'top' or $type eq 'bottom' ) {
469 $clip_lines->{$type}->[1] = $y;
470 $clip_lines->{$type}->[3] = $y;
471  
472 }
473 elsif ( $type eq 'left' or $type eq 'right' ) {
474 $clip_lines->{$type}->[0] = $x;
475 $clip_lines->{$type}->[2] = $x;
476 }
477  
478 $self->draw_clip_lines;
479  
480 $self->set_clipping_changed(1);
481  
482 1;
483 }
484  
485 sub button_release {
486 my $self = shift;
487 my ($type) = @_;
488  
489 $type ||= $self->get_dragged_line;
490  
491 my $value = $self->calc_clip_value($type);
492  
493 if ( $type eq 'top' ) {
494 $self->set_object_value( $self->get_attr_top, $value );
495 }
496 elsif ( $type eq 'bottom' ) {
497 $self->set_object_value( $self->get_attr_bottom, $value );
498 }
499 elsif ( $type eq 'left' ) {
500 $self->set_object_value( $self->get_attr_left, $value );
501 }
502 elsif ( $type eq 'right' ) {
503 $self->set_object_value( $self->get_attr_right, $value );
504 }
505  
506 $self->set_dragged_line(undef);
507  
508 1;
509 }
510  
511 sub calc_clip_value {
512 my $self = shift;
513 my ($type) = @_;
514  
515 $type ||= $self->get_dragged_line;
516  
517 my $clip_lines = $self->get_clip_lines;
518  
519 my $value;
520 if ( $type eq 'top' ) {
521 $value = $clip_lines->{$type}->[1];
522 }
523 elsif ( $type eq 'bottom' ) {
524 $value = $self->get_gtk_pixbuf->get_height - $clip_lines->{$type}->[1];
525 }
526 elsif ( $type eq 'left' ) {
527 $value = $clip_lines->{$type}->[0];
528 }
529 elsif ( $type eq 'right' ) {
530 $value = $self->get_gtk_pixbuf->get_width - $clip_lines->{$type}->[0];
531 }
532
533 return $value;
534 }
535  
536  
537 1;