App/Chart/Gtk2/AnnDrag.pm untested

filter: Statementstmt 0.0% 0 / 222 Branchbran 0.0% 0 / 68 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 33 Podpod 0.0% 0 / 20 totaltotal 0.0% 0 / 367 CC 35 SCAR 30.3
CC
35
Cov
0%
move
56.0
_do_key_snooper
56.0
find
42.0
CRAP 20.7
1 # Copyright 2008, 2009, 2010, 2011 Kevin Ryde
2  
3 # This file is part of Chart.
4 #
5 # Chart is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3, or (at your option) any later version.
8 #
9 # Chart is distributed in the hope that it will be useful, but WITHOUT ANY
10 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12 # details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Chart. If not, see <http://www.gnu.org/licenses/>.
16  
17 package App::Chart::Gtk2::AnnDrag;
18 use 5.008;
19 use strict;
20 use warnings;
21 use Gtk2 1.220;
22  
23 use Gtk2::Ex::WidgetBits;
24 use App::Chart::Gtk2::Graph::Plugin::Alerts;
25  
26 Gtk2->CHECK_VERSION(2,12,0)
27 or die "Need Gtk 2.12 or higher"; # for ->error_bell
28  
29 use constant DEBUG => 0;
30  
31 use Glib::Object::Subclass
32 'Gtk2::Object',
33 signals => { abort => { param_types => [],
34 return_type => undef,
35 flags => ['run-last','action'],
36 class_closure => \&_do_abort,
37 },
38 toggle_alert => { param_types => [],
39 return_type => undef,
40 flags => ['run-last','action'],
41 class_closure => \&_do_toggle_alert,
42 },
43 swap_ends => { param_types => [],
44 return_type => undef,
45 flags => ['run-last','action'],
46 class_closure => \&_do_swap_ends,
47 },
48 delete_element => { param_types => [],
49 return_type => undef,
50 flags => ['run-last','action'],
51 class_closure => \&_do_delete_element,
52 },
53 toggle_horizontal => { param_types => [],
54 return_type => undef,
55 flags => ['run-last','action'],
56 class_closure => \&_do_toggle_horizontal,
57 },
58 };
59  
60 # priority level "gtk" treating this as widget level default, for overriding
61 # by application or user RC
62 Gtk2::Rc->parse_string (<<'HERE');
63 binding "App__Chart__Gtk2__AnnDrag_keys" {
64 bind "Escape" { "abort" () }
65 bind "a" { "toggle-alert" () }
66 bind "space" { "swap-ends" () }
67 bind "d" { "delete-element" () }
68 bind "h" { "toggle-horizontal" () }
69 }
70 class "App__Chart__Gtk2__AnnDrag" binding:gtk "App__Chart__Gtk2__AnnDrag_keys"
71 HERE
72  
73  
74 my $active = 0;
75 my $graph;
76 my $cursor;
77 my $drawn = 0;
78 my $wevents;
79 my $snooper;
80  
81 my $orig_elem; # from database, or undef for new line
82 my $edit_elem;
83  
84 use constant::defer init => sub {
85 $graph->signal_connect (motion_notify_event => \&_do_motion_notify_event);
86 $graph->signal_connect (button_release_event=> \&_do_button_release_event);
87  
88 require Gtk2::Ex::WidgetCursor;
89 $cursor = Gtk2::Ex::WidgetCursor->new (widget => $graph,
90 cursor => 'left_ptr');
91  
92 require Gtk2::Ex::WidgetEvents;
93 $wevents = Gtk2::Ex::WidgetEvents->new ($graph, ['key-press-mask',
94 'button-motion-mask',
95 'button-release-mask']);
96 require Gtk2::Ex::KeySnooper;
97 $snooper = Gtk2::Ex::KeySnooper->new;
98 return; # nothing to memoize
99 };
100  
101 # start dragging in $graph of object nearest $event
102 sub start {
103 ($graph, my $event) = @_; # global $graph
104 if ($active) { return; }
105  
106 init ();
107 my $x = $event->x;
108 my $y = $event->y;
109 $orig_elem = find ($x, $y);
110 if ($orig_elem) {
111 $edit_elem = $orig_elem->clone;
112 $drawn = 1;
113 } else {
114 $edit_elem = App::Chart::Annotation::Line->new_for_graph ($graph, $x, $y);
115 $drawn = 0;
116 draw(1);
117 }
118  
119 $active = 1;
120 $cursor->active (1);
121 $snooper->install (\&_do_key_snooper);
122 }
123  
124 sub _stop {
125 $active = 0;
126 $cursor->active (0);
127 $snooper->remove;
128 }
129  
130 sub done {
131 if (! $active) { return; }
132 if (DEBUG) { print "done\n"; }
133 _stop ();
134 draw (1);
135 }
136  
137 sub abort {
138 goto &_do_abort;
139 }
140 sub _do_abort {
141 my ($self) = @_;
142 if (! $active) { return; }
143 _stop ();
144 draw (0);
145 $edit_elem = $orig_elem;
146 if ($edit_elem) {
147 draw(1);
148 }
149 }
150  
151 sub delete_elem {
152 goto &_do_delete_element;
153 }
154 sub _do_delete_element {
155 my ($self) = @_;
156 if (! $active) { return; }
157 _stop();
158 draw (0);
159 if ($orig_elem) {
160 $orig_elem->delete;
161 }
162 }
163  
164 sub _do_expose_event {
165 my ($graph, $event) = @_;
166 $drawn = 0;
167 draw (1, $event->region);
168 return Gtk2::EVENT_PROPAGATE;
169 }
170  
171 sub _do_motion_notify_event {
172 my ($graph, $event) = @_;
173 my $x = $event->x;
174 my $y = $event->y;
175 if ($event->is_hint) {
176 ($x, $y) = $event->window->get_pointer;
177 }
178 move ($x, $y);
179 return Gtk2::EVENT_PROPAGATE;
180 }
181  
182 sub _do_button_release_event {
183 my ($graph, $event) = @_;
184 if (! $active) { # button release after Esc or D keyboard stop
185 return Gtk2::EVENT_PROPAGATE;
186 }
187 move ($event->x, $event->y);
188 done();
189 $edit_elem->write;
190 return Gtk2::EVENT_PROPAGATE;
191 }
192  
193 sub draw {
194 my ($want, $region) = @_;
195 if (DEBUG >= 2) { print " draw drawn=$drawn want=$want\n"; }
196 if ($drawn != $want) {
197 $edit_elem->draw ($graph, $region);
198 $drawn = $want;
199 }
200 }
201  
202 sub move {
203 my ($x, $y) = @_;
204 if (! $active) { return; }
205 if (DEBUG) { print "AnnDrag move $x, $y\n"; }
206  
207 my $t = $graph->x_to_date ($x);
208 my $price = $graph->y_to_value ($y);
209 my $scale_y = $graph->scale_y_proc;
210  
211 if (DEBUG) {
212 print " $t, $price was ",$edit_elem->t,",",$edit_elem->price," (",
213 $graph->scale_y ($edit_elem->price),")\n";
214 }
215  
216 # redraw only if changed
217 if ($edit_elem->t != $t
218 || $scale_y->($edit_elem->price) != $scale_y->($price)) {
219 if (DEBUG >= 2) { print " redraw\n"; }
220 draw (0);
221 $edit_elem->t ($t);
222 $edit_elem->price ($price);
223 draw (1);
224 } else {
225 if (DEBUG >= 2) { print " no pixel position change\n"; }
226 # but store floating point price always
227 $edit_elem->price ($price);
228 }
229 }
230  
231 sub _do_key_snooper {
232 my ($widget, $event) = @_;
233  
234 # ignore key releases
235 $event->type eq 'key-press' or return Gtk2::EVENT_PROPAGATE;
236  
237 # needing an actual self Gtk2::Object
238 # require Gtk2::Ex::BindingBits;
239 # Gtk2::Ex::BindingBits::activate_event
240 # if (App::Chart::Gtk2::GUI::bindings_activate_event ('App__Chart__Gtk2__AnnDrag_keys',
241 # $event, $self)) {
242 # return Gtk2::EVENT_STOP; # since handled
243 # }
244  
245 my $keyval = $event->keyval;
246 my $modifiers = $event->state;
247 $modifiers = $modifiers & Gtk2::Accelerator->get_default_mod_mask;
248 my $key = lc (Gtk2::Gdk->keyval_name ($keyval));
249 if (DEBUG) { print "keypress $key\n"; }
250  
251 if ($key eq 'escape') {
252 abort ();
253  
254 } elsif ($key eq 'a') {
255 toggle_alert ();
256  
257 } elsif ($key eq 'space') {
258 swap_ends ();
259  
260 } elsif ($key eq 'd') {
261 delete_elem ();
262  
263 } elsif ($key eq 'h') {
264 toggle_horizontal ();
265  
266 } else {
267 # some key not for us
268 return Gtk2::EVENT_PROPAGATE;
269 }
270  
271 # any of the handled keys
272 return Gtk2::EVENT_STOP;
273 }
274  
275 sub toggle_alert {
276 goto &_do_toggle_alert;
277 }
278 sub _do_toggle_alert {
279 my ($self) = @_;
280 if ($edit_elem->isa('App::Chart::Annotation::Line')) {
281 if ($edit_elem->{'id'}) {
282 $graph->error_bell;
283 return;
284 }
285 draw (0);
286 $edit_elem = App::Chart::Annotation::Alert->new
287 (symbol => $edit_elem->{'symbol'},
288 price => $edit_elem->{'price1'},
289 above => 1);
290  
291 } else {
292 draw (0);
293 $edit_elem->{'above'} = ($edit_elem->{'above'} ? 0 : 1);
294 }
295 draw (1);
296 }
297  
298 sub toggle_horizontal {
299 goto &_do_toggle_horizontal;
300 }
301 sub _do_toggle_horizontal {
302 my ($self) = @_;
303 draw (0);
304 $edit_elem->{'horizontal'} = ($edit_elem->{'horizontal'} ? 0 : 1);
305 draw (1);
306 }
307  
308 sub swap_ends {
309 goto &_do_swap_ends;
310 }
311 sub _do_swap_ends {
312 if (DEBUG) { print "swap_ends\n"; }
313 draw (0);
314 $edit_elem->swap_ends;
315 draw (1);
316 Gtk2::Ex::WidgetBits::warp_pointer ($graph,
317 $graph->scale_x ($edit_elem->t),
318 $graph->scale_y ($edit_elem->price));
319 }
320  
321 # return a line from notes, or undef
322 # the line ends are swapped if necessary to make t2,p2 the point near x,y
323 #
324 sub find {
325 my ($x, $y) = @_;
326 if (DEBUG) { print "find\n"; }
327  
328 my $series = $graph->get('series_list')->[0] || return undef;
329 my $scale_x = $graph->scale_x_proc;
330 my $scale_y = $graph->scale_y_proc;
331  
332 # distance in millimetres from tdate/price to X,Y
333 my $tp_distance = sub {
334 my ($t, $p) = @_;
335 return Gtk2::Ex::WidgetBits::xy_distance_mm
336 ($graph, $x,$y, $scale_x->($t), $scale_y->($p));
337 };
338  
339 my $min_elem;
340 my $min_dist;
341 my $minproc = sub {
342 my ($elem, $t, $p) = @_;
343 my $dist = $tp_distance->($t, $p);
344 if (DEBUG) { print " $elem dist $dist\n"; }
345 if ($dist > 5) {
346 return; # no more than 5 millimetres
347 }
348 if (! defined $min_dist || $dist < $min_dist) {
349 $min_dist = $dist;
350 $min_elem = $elem;
351 }
352 };
353  
354 require App::Chart::Annotation;
355 foreach my $elem (@{$series->AnnLines_arrayref}) {
356 $minproc->($elem, $elem->t, $elem->price);
357 $elem = $elem->clone ($elem);
358 $elem->swap_ends;
359 $minproc->($elem, $elem->t, $elem->price);
360 }
361 {
362 my $t = App::Chart::Gtk2::Graph::Plugin::Alerts::draw_t ($graph);
363 foreach my $elem (@{$series->Alerts_arrayref}) {
364 $minproc->($elem, $t, $elem->price);
365 }
366 }
367  
368 if (DEBUG) { print " result ",$min_elem||'undef',
369 " dist ",$min_dist||'undef',"\n"; }
370 return $min_elem;
371 }
372  
373 1;
374 __END__