ImageView/Tool/Selector.pm untested

filter: Statementstmt 0.0% 0 / 128 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 243 CC 29 SCAR 46.6
CC
29
Cov
0%
_update_selection
182.0
cursor_type_at_point
156.0
button_pressed
12.0
CRAP 105.8
1 package Gtk3::ImageView::Tool::Selector;
2  
3 use warnings;
4 use strict;
5 use base 'Gtk3::ImageView::Tool';
6 use Glib qw(TRUE FALSE); # To get TRUE and FALSE
7 use List::Util qw(min);
8 use Readonly;
9 Readonly my $RIGHT_BUTTON => 3;
10 Readonly my $EDGE_WIDTH => 5;
11  
12 our $VERSION = '12';
13  
14 sub button_pressed {
15 my ( $self, $event ) = @_;
16 if ( $event->button == $RIGHT_BUTTON ) {
17 return FALSE;
18 }
19 my $type = $self->cursor_type_at_point( $event->x, $event->y );
20 if ( $type eq 'grab' ) {
21 $type = 'grabbing';
22 $self->{drag_start_x} = int( $event->x + 0.5 );
23 $self->{drag_start_y} = int( $event->y + 0.5 );
24 }
25 $self->{dragging} = $type;
26 $self->_update_selection( $event->x, $event->y );
27 return FALSE;
28 }
29  
30 sub button_released {
31 my ( $self, $event ) = @_;
32 if ( $event->button == $RIGHT_BUTTON ) {
33 return FALSE;
34 }
35 if ( $self->{dragging} ) {
36 $self->_update_selection( $event->x, $event->y );
37 }
38 $self->{dragging} = undef;
39 $self->view->update_cursor( $event->x, $event->y );
40 return FALSE;
41 }
42  
43 sub motion {
44 my ( $self, $event ) = @_;
45 if ( $self->{dragging} ) {
46 $self->_update_selection( $event->x, $event->y );
47 }
48 return FALSE;
49 }
50  
51 sub cursor_type_at_point { ## no critic (ProhibitExcessComplexity);
52 my ( $self, $x, $y ) = @_;
53 if ( $self->{dragging} ) {
54 return $self->{dragging};
55 }
56 my $selection = $self->view->get_selection;
57 if ( !defined $selection ) {
58 return 'crosshair';
59 }
60 my $edge_width = $EDGE_WIDTH * $self->view->get('scale-factor');
61 my ( $sx1, $sy1 ) =
62 $self->view->to_widget_coords( $selection->{x}, $selection->{y} );
63 my ( $sx2, $sy2 ) = $self->view->to_widget_coords(
64 $selection->{x} + $selection->{width},
65 $selection->{y} + $selection->{height}
66 );
67 if ( $x < $sx1 - $edge_width
68 || $x > $sx2 + $edge_width
69 || $y < $sy1 - $edge_width
70 || $y > $sy2 + $edge_width )
71 {
72 return 'crosshair';
73 }
74 if ( $x > $sx1 + $edge_width
75 && $x < $sx2 - $edge_width
76 && $y > $sy1 + $edge_width
77 && $y < $sy2 - $edge_width )
78 {
79 return 'grab';
80 }
81  
82 # This makes it possible for the selection to be smaller than edge_width and still be resizeable in all directions
83 my $leftish = $x < ( $sx1 + $sx2 ) / 2;
84 my $topish = $y < ( $sy1 + $sy2 ) / 2;
85 if ( $y > $sy1 + $edge_width && $y < $sy2 - $edge_width ) {
86 if ($leftish) {
87 return 'w-resize';
88 }
89 else {
90 return 'e-resize';
91 }
92 }
93 if ( $x > $sx1 + $edge_width && $x < $sx2 - $edge_width ) {
94 if ($topish) {
95 return 'n-resize';
96 }
97 else {
98 return 's-resize';
99 }
100 }
101 if ($leftish) {
102 if ($topish) {
103 return 'nw-resize';
104 }
105 else {
106 return 'sw-resize';
107 }
108 }
109 else {
110 if ($topish) {
111 return 'ne-resize';
112 }
113 else {
114 return 'se-resize';
115 }
116 }
117 }
118  
119 sub _update_selection {
120 my ( $self, $x, $y ) = @_;
121 my $selection = $self->view->get('selection-float') // {
122 x => 0,
123 y => 0,
124 width => 0,
125 height => 0,
126 };
127 my ( $sel_x1, $sel_y1 ) =
128 $self->view->to_widget_coords( $selection->{x}, $selection->{y} );
129 my ( $sel_x2, $sel_y2 ) = $self->view->to_widget_coords(
130 $selection->{x} + $selection->{width},
131 $selection->{y} + $selection->{height}
132 );
133 my $type = $self->{dragging};
134 if ( $type eq 'grabbing' ) {
135 my $off_x = $x - $self->{drag_start_x};
136 my $off_y = $y - $self->{drag_start_y};
137 $sel_x1 += $off_x;
138 $sel_x2 += $off_x;
139 $sel_y1 += $off_y;
140 $sel_y2 += $off_y;
141 $self->{drag_start_x} = $x;
142 $self->{drag_start_y} = $y;
143 }
144 if ( $type eq 'crosshair' ) {
145 $sel_x1 = $x;
146 $sel_x2 = $x;
147 $sel_y1 = $y;
148 $sel_y2 = $y;
149 $type = 'se-resize';
150 }
151 my $flip_we = 0;
152 my $flip_ns = 0;
153 if ( $type =~ /w-resize/smx ) {
154 $sel_x1 = $x;
155 if ( $x > $sel_x2 ) { $flip_we = 'e' }
156 }
157 if ( $type =~ /e-resize/smx ) {
158 $sel_x2 = $x;
159 if ( $x < $sel_x1 ) { $flip_we = 'w' }
160 }
161 if ( $type =~ /n.?-resize/smx ) {
162 $sel_y1 = $y;
163 if ( $y > $sel_y2 ) { $flip_ns = 's' }
164 }
165 if ( $type =~ /s.?-resize/smx ) {
166 $sel_y2 = $y;
167 if ( $y < $sel_y1 ) { $flip_ns = 'n' }
168 }
169 my ( $w, $h ) = $self->view->to_image_distance( abs( $sel_x2 - $sel_x1 ),
170 abs( $sel_y2 - $sel_y1 ) );
171 my ( $img_x, $img_y ) =
172 $self->view->to_image_coords( min( $sel_x1, $sel_x2 ),
173 min( $sel_y1, $sel_y2 ) );
174 $self->view->set_selection(
175 {
176 x => $img_x,
177 y => $img_y,
178 width => $w,
179 height => $h,
180 }
181 );
182  
183 # Prepare for next mouse event
184 # If we are dragging, a corner cursor must stay as a corner cursor,
185 # a left/right cursor must stay as left/right,
186 # and a top/bottom cursor must stay as top/bottom
187 if ($flip_we) {
188 $type =~ s/[we]-/$flip_we-/smx;
189 }
190 if ($flip_ns) {
191 $type =~ s/^[ns]/$flip_ns/smx;
192 }
193 $self->{dragging} = $type;
194 $self->view->update_cursor( $x, $y );
195 return;
196 }
197  
198 # compatibility layer
199  
200 sub get_selection {
201 my $self = shift;
202 return $self->view->get_selection;
203 }
204  
205 sub set_selection {
206 my ( $self, @args ) = @_;
207 $self->view->set_selection(@args);
208 return;
209 }
210  
211 1;