ImageView.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« ImageView/Tool.pm ↑ Summary
1 package Gtk3::ImageView;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶no if $] >= 5.018, warnings => 'experimental::smartmatch';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use feature 'switch';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Cairo;
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use Glib qw(TRUE FALSE); # To get TRUE and FALSE
9 use Gtk3;
10 use Gtk3::ImageView::Tool;
11 use Gtk3::ImageView::Tool::Dragger;
12 use Gtk3::ImageView::Tool::Selector;
13 use List::Util qw(min);
14 use Scalar::Util qw(blessed);
15 use Carp;
16 use Readonly;
17 Readonly my $HALF => 0.5;
18 Readonly my $MAX_ZOOM => 100;
19  
20 our $VERSION = 8;
21  
22 use Glib::Object::Subclass Gtk3::DrawingArea::, signals => {
23 'zoom-changed' => {
24 param_types => ['Glib::Float'], # new zoom
25 },
26 'offset-changed' => {
27 param_types => [ 'Glib::Int', 'Glib::Int' ], # new offset
28 },
29 'selection-changed' => {
30 param_types => ['Glib::Scalar'], # Gdk::Rectangle of selection area
31 },
32 'tool-changed' => {
33 param_types => ['Glib::Scalar'], # new Gtk3::ImageView::Tool
34 },
35 'dnd-start' => {
36 param_types => [
37 'Glib::Float', # x
38 'Glib::Float', # y
39 'Glib::UInt', # button
40 ],
41 return_type => 'Glib::Boolean',
42 flags => ['run-last'],
43 }
44 },
45 properties => [
46 Glib::ParamSpec->object(
47 'pixbuf', # name
48 'pixbuf', # nickname
49 'Gtk3::Gdk::Pixbuf to be shown', # blurb
50 'Gtk3::Gdk::Pixbuf',
51 [qw/readable writable/] # flags
52 ),
53 Glib::ParamSpec->scalar(
54 'offset', # name
55 'Image offset', # nick
56 'Gdk::Rectangle hash of x, y', # blurb
57 [qw/readable writable/] # flags
58 ),
59 Glib::ParamSpec->float(
60 'zoom', # name
61 'zoom', # nick
62 'zoom level', # blurb
63 0.001, # minimum
64 100.0, # maximum
65 1.0, # default_value
66 [qw/readable writable/] # flags
67 ),
68 Glib::ParamSpec->float(
69 'resolution-ratio', # name
70 'resolution-ratio', # nick
71 'Ratio of x-resolution/y-resolution', # blurb
72 0.0001, # minimum
73 1000.0, # maximum
74 1.0, # default_value
75 [qw/readable writable/] # flags
76 ),
77 Glib::ParamSpec->scalar(
78 'tool', # name
79 'tool', # nickname
80 'Active Gtk3::ImageView::Tool', # blurb
81 [qw/readable writable/] # flags
82 ),
83 Glib::ParamSpec->scalar(
84 'selection', # name
85 'Selection', # nick
86 'Gdk::Rectangle hash of selected region', # blurb
87 [qw/readable writable/] # flags
88 ),
89 Glib::ParamSpec->boolean(
90 'zoom-to-fit', # name
91 'Zoom to fit', # nickname
92 'Whether the zoom factor is automatically calculated to fit the window'
93 , # blurb
94 TRUE, # default
95 [qw/readable writable/] # flags
96 ),
97 Glib::ParamSpec->float(
98 'zoom-to-fit-limit', # name
99 'Zoom to fit limit', # nickname
100 'When zooming automatically, don\'t zoom more than this', # blurb
101 0.0001, # minimum
102 100.0, # maximum
103 100.0, # default_value
104 [qw/readable writable/] # flags
105 ),
106 Glib::ParamSpec->string(
107 'interpolation', # name
108 'interpolation', # nick
109 'Interpolation method to use, from Cairo::Filter', # blurb
110 'good', # default
111 [qw/readable writable/], # flags
112 ),
113 ];
114  
115 sub INIT_INSTANCE {
116 my $self = shift;
117 $self->signal_connect( draw => \&_draw );
118 $self->signal_connect( 'button-press-event' => \&_button_pressed );
119 $self->signal_connect( 'button-release-event' => \&_button_released );
120 $self->signal_connect( 'motion-notify-event' => \&_motion );
121 $self->signal_connect( 'scroll-event' => \&_scroll );
122 $self->signal_connect( configure_event => \&_configure_event );
123 $self->set_app_paintable(TRUE);
124  
125 if (
126 $Glib::Object::Introspection::VERSION <
127 0.043 ## no critic (ProhibitMagicNumbers)
128 )
129 {
130 $self->add_events(
131 ${ Gtk3::Gdk::EventMask->new(qw/exposure-mask/) } |
132 ${ Gtk3::Gdk::EventMask->new(qw/button-press-mask/) } |
133 ${ Gtk3::Gdk::EventMask->new(qw/button-release-mask/) } |
134 ${ Gtk3::Gdk::EventMask->new(qw/pointer-motion-mask/) } |
135 ${ Gtk3::Gdk::EventMask->new(qw/scroll-mask/) } );
136 }
137 else {
138 $self->add_events(
139 Glib::Object::Introspection->convert_sv_to_flags(
140 'Gtk3::Gdk::EventMask', 'exposure-mask' ) |
141 Glib::Object::Introspection->convert_sv_to_flags(
142 'Gtk3::Gdk::EventMask', 'button-press-mask' ) |
143 Glib::Object::Introspection->convert_sv_to_flags(
144 'Gtk3::Gdk::EventMask', 'button-release-mask' ) |
145 Glib::Object::Introspection->convert_sv_to_flags(
146 'Gtk3::Gdk::EventMask', 'pointer-motion-mask' ) |
147 Glib::Object::Introspection->convert_sv_to_flags(
148 'Gtk3::Gdk::EventMask', 'scroll-mask'
149 )
150 );
151 }
152 $self->set_tool( Gtk3::ImageView::Tool::Dragger->new($self) );
153 $self->set_redraw_on_allocate(FALSE);
154 return $self;
155 }
156  
157 sub SET_PROPERTY {
158 my ( $self, $pspec, $newval ) = @_;
159 my $name = $pspec->get_name;
160 my $oldval = $self->get($name);
161 my $invalidate = FALSE;
162 if ( ( defined $newval and defined $oldval and $newval ne $oldval )
163 or ( defined $newval xor defined $oldval ) )
164 {
165 given ($name) {
166 when ('pixbuf') {
167 $self->{$name} = $newval;
168 $invalidate = TRUE;
169 }
170 when ('zoom') {
171 $self->{$name} = $newval;
172 $self->signal_emit( 'zoom-changed', $newval );
173 $invalidate = TRUE;
174 }
175 when ('offset') {
176 if ( ( defined $newval xor defined $oldval )
177 or $oldval->{x} != $newval->{x}
178 or $oldval->{y} != $newval->{y} )
179 {
180 $self->{$name} = $newval;
181 $self->signal_emit( 'offset-changed', $newval->{x},
182 $newval->{y} );
183 $invalidate = TRUE;
184 }
185 }
186 when ('resolution-ratio') {
187 $self->{$name} = $newval;
188 $invalidate = TRUE;
189 }
190 when ('interpolation') {
191 $self->{$name} = $newval;
192 $invalidate = TRUE;
193 }
194 when ('selection') {
195 if ( ( defined $newval xor defined $oldval )
196 or $oldval->{x} != $newval->{x}
197 or $oldval->{y} != $newval->{y}
198 or $oldval->{width} != $newval->{width}
199 or $oldval->{height} != $newval->{height} )
200 {
201 $self->{$name} = $newval;
202 $invalidate = TRUE;
203 $self->signal_emit( 'selection-changed', $newval );
204 }
205 }
206 when ('tool') {
207 $self->{$name} = $newval;
208 if ( defined $self->get_selection ) {
209 $invalidate = TRUE;
210 }
211 $self->signal_emit( 'tool-changed', $newval );
212 }
213 default {
214 $self->{$name} = $newval;
215  
216 # $self->SUPER::SET_PROPERTY( $pspec, $newval );
217 }
218 }
219 if ($invalidate) {
220 $self->queue_draw();
221 }
222 }
223 return;
224 }
225  
226 sub set_pixbuf {
227 my ( $self, $pixbuf, $zoom_to_fit ) = @_;
228 $self->set( 'pixbuf', $pixbuf );
229 $self->set_zoom_to_fit($zoom_to_fit);
230 if ( not $zoom_to_fit ) {
231 $self->set_offset( 0, 0 );
232 }
233 return;
234 }
235  
236 sub get_pixbuf {
237 my ($self) = @_;
238 return $self->get('pixbuf');
239 }
240  
241 sub get_pixbuf_size {
242 my ($self) = @_;
243 my $pixbuf = $self->get_pixbuf;
244 if ( defined $pixbuf ) {
245 return { width => $pixbuf->get_width, height => $pixbuf->get_height };
246 }
247 return;
248 }
249  
250 sub _button_pressed {
251 my ( $self, $event ) = @_;
252 return $self->get_tool->button_pressed($event);
253 }
254  
255 sub _button_released {
256 my ( $self, $event ) = @_;
257 $self->get_tool->button_released($event);
258 return;
259 }
260  
261 sub _motion {
262 my ( $self, $event ) = @_;
263 $self->update_cursor( $event->x, $event->y );
264 $self->get_tool->motion($event);
265 return;
266 }
267  
268 sub _scroll {
269 my ( $self, $event ) = @_;
270 my ( $center_x, $center_y ) =
271 $self->to_image_coords( $event->x, $event->y );
272 my $zoom;
273 $self->set_zoom_to_fit(FALSE);
274 if ( $event->direction eq 'up' ) {
275 $zoom = $self->get_zoom * 2;
276 }
277 else {
278 $zoom = $self->get_zoom / 2;
279 }
280 $self->_set_zoom_with_center( $zoom, $center_x, $center_y );
281 return;
282 }
283  
284 sub _draw {
285 my ( $self, $context ) = @_;
286 my $allocation = $self->get_allocation;
287 my $style = $self->get_style_context;
288 my $pixbuf = $self->get_pixbuf;
289 my $ratio = $self->get_resolution_ratio;
290 my $viewport = $self->get_viewport;
291 $style->add_class('imageview');
292  
293 $style->save;
294 $style->add_class(Gtk3::STYLE_CLASS_BACKGROUND);
295 Gtk3::render_background( $style, $context, $allocation->{x},
296 $allocation->{y}, $allocation->{width}, $allocation->{height} );
297 $style->restore;
298  
299 if ( defined $pixbuf ) {
300 if ( $pixbuf->get_has_alpha ) {
301 $style->save;
302  
303 # '.imageview' affects also area outside of the image. But only
304 # when background-image is specified. background-color seems to
305 # have no effect there. Probably a bug in Gtk? Either way, this is
306 # why need a special class 'transparent' to match the correct area
307 # inside the image where both image and color work.
308 $style->add_class('transparent');
309 my ( $x1, $y1 ) = $self->to_widget_coords( 0, 0 );
310 my ( $x2, $y2 ) =
311 $self->to_widget_coords( $pixbuf->get_width,
312 $pixbuf->get_height );
313 Gtk3::render_background( $style, $context, $x1, $y1, $x2 - $x1,
314 $y2 - $y1 );
315 $style->restore;
316 }
317  
318 my $zoom = $self->get_zoom / $self->get('scale-factor');
319 $context->scale( $zoom / $ratio, $zoom );
320 my $offset = $self->get_offset;
321 $context->translate( $offset->{x}, $offset->{y} );
322 Gtk3::Gdk::cairo_set_source_pixbuf( $context, $pixbuf, 0, 0 );
323 $context->get_source->set_filter( $self->get_interpolation );
324 }
325 else {
326 my $bgcol = $style->get( 'normal', 'background-color' );
327 Gtk3::Gdk::cairo_set_source_rgba( $context, $bgcol );
328 }
329 $context->paint;
330  
331 my $selection = $self->get_selection;
332 if ( defined $pixbuf and defined $selection ) {
333 my ( $x, $y, $w, $h, ) = (
334 $selection->{x}, $selection->{y},
335 $selection->{width}, $selection->{height},
336 );
337 if ( $w <= 0 or $h <= 0 ) { return TRUE }
338  
339 $style->save;
340 $style->add_class(Gtk3::STYLE_CLASS_RUBBERBAND);
341 Gtk3::render_background( $style, $context, $x, $y, $w, $h );
342 Gtk3::render_frame( $style, $context, $x, $y, $w, $h );
343 $style->restore;
344 }
345 return TRUE;
346 }
347  
348 sub _configure_event {
349 my ( $self, $event ) = @_;
350 if ( $self->get_zoom_to_fit ) {
351 $self->zoom_to_box( $self->get_pixbuf_size );
352 }
353 return;
354 }
355  
356 # setting the zoom via the public API disables zoom-to-fit
357  
358 sub set_zoom {
359 my ( $self, $zoom ) = @_;
360 $self->set_zoom_to_fit(FALSE);
361 $self->_set_zoom_no_center($zoom);
362 return;
363 }
364  
365 sub _set_zoom {
366 my ( $self, $zoom ) = @_;
367 if ( $zoom > $MAX_ZOOM ) { $zoom = $MAX_ZOOM }
368 $self->set( 'zoom', $zoom );
369 return;
370 }
371  
372 sub get_zoom {
373 my ($self) = @_;
374 return $self->get('zoom');
375 }
376  
377 # convert x, y in image coords to widget coords
378 sub to_widget_coords {
379 my ( $self, $x, $y ) = @_;
380 my $zoom = $self->get_zoom;
381 my $ratio = $self->get_resolution_ratio;
382 my $offset = $self->get_offset;
383 my $factor = $self->get('scale-factor');
384 return ( $x + $offset->{x} ) * $zoom / $factor / $ratio,
385 ( $y + $offset->{y} ) * $zoom / $factor;
386 }
387  
388 # convert x, y in widget coords to image coords
389 sub to_image_coords {
390 my ( $self, $x, $y ) = @_;
391 my $zoom = $self->get_zoom;
392 my $ratio = $self->get_resolution_ratio;
393 my $offset = $self->get_offset;
394 my $factor = $self->get('scale-factor');
395 return $x * $factor / $zoom * $ratio - $offset->{x},
396 $y * $factor / $zoom - $offset->{y};
397 }
398  
399 # convert x, y in widget distance to image distance
400 sub to_image_distance {
401 my ( $self, $x, $y ) = @_;
402 my $zoom = $self->get_zoom;
403 my $ratio = $self->get_resolution_ratio;
404 my $factor = $self->get('scale-factor');
405 return $x * $factor / $zoom * $ratio, $y * $factor / $zoom;
406 }
407  
408 # set zoom with centre in image coordinates
409 sub _set_zoom_with_center {
410 my ( $self, $zoom, $center_x, $center_y ) = @_;
411 my $allocation = $self->get_allocation;
412 my $ratio = $self->get_resolution_ratio;
413 my $factor = $self->get('scale-factor');
414 my $offset_x =
415 $allocation->{width} * $factor / 2 / $zoom * $ratio - $center_x;
416 my $offset_y = $allocation->{height} * $factor / 2 / $zoom - $center_y;
417 $self->_set_zoom($zoom);
418 $self->set_offset( $offset_x, $offset_y );
419 return;
420 }
421  
422 # sets zoom, centred on the viewport
423 sub _set_zoom_no_center {
424 my ( $self, $zoom ) = @_;
425 my $allocation = $self->get_allocation;
426 my ( $center_x, $center_y ) =
427 $self->to_image_coords( $allocation->{width} / 2,
428 $allocation->{height} / 2 );
429 $self->_set_zoom_with_center( $zoom, $center_x, $center_y );
430 return;
431 }
432  
433 sub set_zoom_to_fit {
434 my ( $self, $zoom_to_fit, $limit ) = @_;
435 $self->set( 'zoom-to-fit', $zoom_to_fit );
436 if ( defined $limit ) {
437 $self->set( 'zoom-to-fit-limit', $limit );
438 }
439 if ( not $zoom_to_fit ) { return }
440 $self->zoom_to_box( $self->get_pixbuf_size );
441 return;
442 }
443  
444 sub zoom_to_box {
445 my ( $self, $box, $additional_factor ) = @_;
446 if ( not defined $box ) { return }
447 if ( not defined $box->{x} ) { $box->{x} = 0 }
448 if ( not defined $box->{y} ) { $box->{y} = 0 }
449 if ( not defined $additional_factor ) { $additional_factor = 1 }
450 my $allocation = $self->get_allocation;
451 my $ratio = $self->get_resolution_ratio;
452 my $limit = $self->get('zoom-to-fit-limit');
453 my $sc_factor_w =
454 min( $limit, $allocation->{width} / $box->{width} ) * $ratio;
455 my $sc_factor_h =
456 min( $limit, $allocation->{height} / $box->{height} );
457 $self->_set_zoom_with_center(
458 min( $sc_factor_w, $sc_factor_h ) *
459 $additional_factor *
460 $self->get('scale-factor'),
461 ( $box->{x} + $box->{width} / 2 ) / $ratio,
462 $box->{y} + $box->{height} / 2
463 );
464 return;
465 }
466  
467 sub zoom_to_selection {
468 my ( $self, $context_factor ) = @_;
469 $self->zoom_to_box( $self->get_selection, $context_factor );
470 return;
471 }
472  
473 sub get_zoom_to_fit {
474 my ($self) = @_;
475 return $self->get('zoom-to-fit');
476 }
477  
478 sub zoom_in {
479 my ($self) = @_;
480 $self->set_zoom_to_fit(FALSE);
481 $self->_set_zoom_no_center( $self->get_zoom * 2 );
482 return;
483 }
484  
485 sub zoom_out {
486 my ($self) = @_;
487 $self->set_zoom_to_fit(FALSE);
488 $self->_set_zoom_no_center( $self->get_zoom / 2 );
489 return;
490 }
491  
492 sub zoom_to_fit {
493 my ($self) = @_;
494 $self->set_zoom_to_fit(TRUE);
495 return;
496 }
497  
498 sub set_fitting {
499 my ( $self, $value ) = @_;
500 $self->set_zoom_to_fit( $value, 1 );
501 return;
502 }
503  
504 sub _clamp_direction {
505 my ( $offset, $allocation, $pixbuf_size ) = @_;
506  
507 # Centre the image if it is smaller than the widget
508 if ( $allocation > $pixbuf_size ) {
509 $offset = ( $allocation - $pixbuf_size ) / 2;
510 }
511  
512 # Otherwise don't allow the LH/top edge of the image to be visible
513 elsif ( $offset > 0 ) {
514 $offset = 0;
515 }
516  
517 # Otherwise don't allow the RH/bottom edge of the image to be visible
518 elsif ( $offset < $allocation - $pixbuf_size ) {
519 $offset = $allocation - $pixbuf_size;
520 }
521 return $offset;
522 }
523  
524 sub set_offset {
525 my ( $self, $offset_x, $offset_y ) = @_;
526 if ( not defined $self->get_pixbuf ) { return }
527  
528 # Convert the widget size to image scale to make the comparisons easier
529 my $allocation = $self->get_allocation;
530 ( $allocation->{width}, $allocation->{height} ) =
531 $self->to_image_distance( $allocation->{width}, $allocation->{height} );
532 my $pixbuf_size = $self->get_pixbuf_size;
533  
534 $offset_x = _clamp_direction( $offset_x, $allocation->{width},
535 $pixbuf_size->{width} );
536 $offset_y = _clamp_direction( $offset_y, $allocation->{height},
537 $pixbuf_size->{height} );
538  
539 $self->set( 'offset', { x => $offset_x, y => $offset_y } );
540 return;
541 }
542  
543 sub get_offset {
544 my ($self) = @_;
545 return $self->get('offset');
546 }
547  
548 sub get_viewport {
549 my ($self) = @_;
550 my $allocation = $self->get_allocation;
551 my $pixbuf = $self->get_pixbuf;
552 my ( $x, $y, $w, $h );
553 if ( defined $pixbuf ) {
554 ( $x, $y, $w, $h ) = (
555 $self->to_image_coords( 0, 0 ),
556 $self->to_image_distance(
557 $allocation->{width}, $allocation->{height}
558 )
559 );
560 }
561 else {
562 ( $x, $y, $w, $h ) =
563 ( 0, 0, $allocation->{width}, $allocation->{height} );
564 }
565 return { x => $x, y => $y, width => $w, height => $h };
566 }
567  
568 sub set_tool {
569 my ( $self, $tool ) = @_;
570 if ( not( blessed $tool and $tool->isa('Gtk3::ImageView::Tool') ) ) {
571  
572 # TODO remove this fallback, only accept Tool directly
573 given ($tool) {
574 when ('dragger') {
575 $tool = Gtk3::ImageView::Tool::Dragger->new($self);
576 }
577 when ('selector') {
578 $tool = Gtk3::ImageView::Tool::Selector->new($self);
579 }
580 default {
581 croak 'invalid set_tool call';
582 }
583 }
584 }
585 $self->set( 'tool', $tool );
586 return;
587 }
588  
589 sub get_tool {
590 my ($self) = @_;
591 return $self->get('tool');
592 }
593  
594 sub set_selection {
595 my ( $self, $selection ) = @_;
596 my $pixbuf_size = $self->get_pixbuf_size;
597 if ( not defined $pixbuf_size ) { return }
598 if ( $selection->{x} < 0 ) {
599 $selection->{width} += $selection->{x};
600 $selection->{x} = 0;
601 }
602 if ( $selection->{y} < 0 ) {
603 $selection->{height} += $selection->{y};
604 $selection->{y} = 0;
605 }
606 if ( $selection->{x} + $selection->{width} > $pixbuf_size->{width} ) {
607 $selection->{width} = $pixbuf_size->{width} - $selection->{x};
608 }
609 if ( $selection->{y} + $selection->{height} > $pixbuf_size->{height} ) {
610 $selection->{height} = $pixbuf_size->{height} - $selection->{y};
611 }
612 $self->set( 'selection', $selection );
613 return;
614 }
615  
616 sub get_selection {
617 my ($self) = @_;
618 return $self->get('selection');
619 }
620  
621 sub set_resolution_ratio {
622 my ( $self, $ratio ) = @_;
623 $self->set( 'resolution-ratio', $ratio );
624 if ( $self->get_zoom_to_fit ) {
625 $self->zoom_to_box( $self->get_pixbuf_size );
626 }
627 return;
628 }
629  
630 sub get_resolution_ratio {
631 my ($self) = @_;
632 return $self->get('resolution-ratio');
633 }
634  
635 sub update_cursor {
636 my ( $self, $x, $y ) = @_;
637 my $pixbuf_size = $self->get_pixbuf_size;
638 if ( not defined $pixbuf_size ) { return }
639 my $win = $self->get_window;
640 my $cursor = $self->get_tool->cursor_at_point( $x, $y );
641 if ( defined $cursor ) {
642 $win->set_cursor($cursor);
643 }
644  
645 return;
646 }
647  
648 sub set_interpolation {
649 my ( $self, $interpolation ) = @_;
650 $self->set( 'interpolation', $interpolation );
651 return;
652 }
653  
654 sub get_interpolation {
655 my ($self) = @_;
656 return $self->get('interpolation');
657 }
658  
659 1;
660  
661 __END__
« ImageView/Tool.pm ↑ Summary