App/Chart/Gtk2/ViewStyleDialog.pm untested

filter: Statementstmt 0.0% 0 / 465 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 59 MC/DCmcdc 0.0% 0 / 32 Subroutinesub 0.0% 0 / 86 Podpod 0.0% 0 / 38 totaltotal 0.0% 0 / 736 CC 29 SCAR 22.8
CC
29
Cov
0%
SET_PROPERTY
72.0
_do_response
30.0
_do_notify
12.0
CRAP 9.8
1 # Copyright 2007, 2008, 2009, 2010, 2011, 2013, 2014 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::ViewStyleDialog;
18 use 5.010;
19 use strict;
20 use warnings;
21 use Glib::Ex::ConnectProperties;
22 use Gtk2;
23 use Locale::TextDomain ('App-Chart');
24  
25 use App::Chart::Glib::Ex::MoreUtils;
26 use Gtk2::Ex::Units;
27 use App::Chart;
28 use App::Chart::Gtk2::GUI;
29 use App::Chart::Gtk2::View;
30  
31 # uncomment this to run the ### lines
32 #use Smart::Comments;
33  
34 use base 'App::Chart::Gtk2::Ex::ToplevelSingleton';
35 use Glib::Object::Subclass
36 'Gtk2::Dialog',
37 signals => { notify => \&_do_notify,
38 show => \&_do_show },
39 properties => [Glib::ParamSpec->scalar
40 ('viewstyle',
41 'viewstyle',
42 'Blurb.',
43 Glib::G_PARAM_READWRITE),
44  
45 Glib::ParamSpec->object
46 ('view',
47 'view',
48 'Blurb.',
49 'App::Chart::Gtk2::View',
50 Glib::G_PARAM_READWRITE),
51 ];
52  
53 use constant RESPONSE_SAVE => 0;
54  
55 sub INIT_INSTANCE {
56 my ($self) = @_;
57  
58 $self->set_title (__('Chart: View Style'));
59 $self->add_buttons ('gtk-ok' => 'ok',
60 'gtk-save' => RESPONSE_SAVE,
61 'gtk-cancel' => 'cancel',
62 'gtk-help' => 'help');
63 $self->signal_connect (response => \&_do_response);
64 my $vbox = $self->vbox;
65  
66 $vbox->pack_start (Gtk2::Label->new
67 ("Warning, this dialog is not quite right."),
68 0,0,0);
69  
70 my $hbox = Gtk2::HBox->new (0, 0);
71 $vbox->pack_start ($hbox, 0, 0, 0);
72  
73 my $adjust_splits = $self->{'adjust_splits'}
74 = Gtk2::CheckButton->new_with_label (__('Adj splits'));
75 $adjust_splits->set_tooltip_text (__('Whether to adjust past prices to the current share capital basis'));
76 $hbox->pack_start ($adjust_splits, 0, 0, Gtk2::Ex::Units::em($hbox));
77  
78 my $adjust_dividends = $self->{'adjust_dividends'}
79 = Gtk2::CheckButton->new_with_label (__('Adj dividends'));
80 $adjust_dividends->set_tooltip_text (__('Whether to adjust past prices down for dividends paid out'));
81 $adjust_dividends->signal_connect (toggled => \&_do_button_toggled);
82 $hbox->pack_start ($adjust_dividends, 0, 0, Gtk2::Ex::Units::em($hbox));
83  
84 my $adjust_imputation = $self->{'adjust_imputation'}
85 = Gtk2::CheckButton->new_with_label (__('Adj imputation'));
86 $adjust_imputation->set_tooltip_text (__('Whether to adjust past prices down for any imputation credits attached to dividends (only applicable when dividend adjustments selected)'));
87 $adjust_imputation->signal_connect (toggled => \&_do_button_toggled);
88 # only applicable when divs enabled
89 Glib::Ex::ConnectProperties->new ([$adjust_dividends,'active'],
90 [$adjust_imputation,'sensitive']);
91 $hbox->pack_start ($adjust_imputation, 0, 0, Gtk2::Ex::Units::em($hbox));
92  
93 my $adjust_rollovers = $self->{'adjust_rollovers'}
94 = Gtk2::CheckButton->new_with_label (__('Adj rollovers'));
95 $adjust_rollovers->set_tooltip_text (__('Whether to adjust past prices up for front month futures contract rollovers'));
96 $hbox->pack_start ($adjust_rollovers, 0, 0, Gtk2::Ex::Units::em($hbox));
97 $adjust_rollovers->signal_connect (toggled => \&_do_button_toggled);
98  
99 my $lower_hbox = Gtk2::HBox->new;
100 $vbox->pack_end ($lower_hbox, 0,0,0);
101 $vbox->pack_end (Gtk2::HSeparator->new, 0,0,0);
102  
103 my $add_graph = $self->{'add_graph'}
104 = Gtk2::Button->new_with_label (__('Add Graph'));
105 $add_graph->signal_connect (clicked => \&_do_add_graph);
106 $lower_hbox->pack_start ($add_graph, 0,0,0);
107  
108 $self->set_viewstyle (App::Chart::Gtk2::View::DEFAULT_VIEWSTYLE);
109 $vbox->show_all;
110 }
111  
112 sub GET_PROPERTY {
113 my ($self, $pspec) = @_;
114 my $pname = $pspec->get_name;
115 if ($pspec->get_name eq 'viewstyle') {
116 return $self->get_viewstyle;
117 } else {
118 return $self->{$pname};
119 }
120 }
121  
122 sub SET_PROPERTY {
123 my ($self, $pspec, $newval) = @_;
124 my $pname = $pspec->get_name;
125 if ($pname eq 'viewstyle') {
126 $self->set_viewstyle ($newval);
127 } else {
128 $self->{$pname} = $newval;
129 }
130 }
131  
132 sub get_viewstyle {
133 my ($self) = @_;
134 my $vbox = $self->vbox;
135 return { adjust_splits => $self->{'adjust_splits'}->get_active,
136 adjust_dividends => $self->{'adjust_dividends'}->get_active,
137 adjust_imputation => $self->{'adjust_imputation'}->get_active,
138 adjust_rollovers => $self->{'adjust_rollovers'}->get_active,
139 graphs => [ map { $_->isa('App::Chart::Gtk2::GraphStyleWidget')
140 ? ($_->get_graphstyle)
141 : () }
142 $vbox->get_children ]
143 };
144 }
145  
146 sub set_viewstyle {
147 my ($self, $viewstyle) = @_;
148 ### ViewStyleDialog set_viewstyle(): $viewstyle
149  
150 require Glib::Ex::FreezeNotify;
151 my $freezer = Glib::Ex::FreezeNotify->new ($self);
152  
153 foreach my $field (qw(adjust_splits adjust_dividends adjust_imputation
154 adjust_rollovers)) {
155 $self->{$field}->set_active ($viewstyle->{$field})
156 }
157  
158 my $vbox = $self->vbox;
159 my @graphs = grep {$_->isa('App::Chart::Gtk2::GraphStyleWidget')}
160 $vbox->get_children;
161 my $graphstyles = ($viewstyle->{'graphs'} ||= []);
162 while (@graphs > @$graphstyles) {
163 $vbox->remove (pop @graphs);
164 }
165 foreach my $i (0 .. $#$graphstyles) {
166 if (my $graph = $graphs[$i]) {
167 $graph->set_graphstyle ($graphstyles->[$i]);
168 } else {
169 my $graph = $graphs[$i] = App::Chart::Gtk2::GraphStyleWidget->new
170 (graphstyle => $graphstyles->[$i]);
171 $graph->signal_connect (notify => \&_do_graphstyle_notify);
172 $graph->signal_connect (delete_graph => \&_do_graphstyle_delete);
173 $vbox->pack_start ($graph, 0,0,
174 0.25 * Gtk2::Ex::Units::line_height($self));
175 }
176 $graphs[$i]->{'i'} = $i;
177 }
178 $vbox->show_all;
179 $self->notify ('viewstyle');
180 }
181  
182 sub _do_button_toggled {
183 my ($origin) = @_;
184 my $self = $origin->get_ancestor(__PACKAGE__);
185 ### ViewStyleDialog _do_button_toggled(): $origin
186 $self->notify ('viewstyle');
187 }
188  
189 sub _do_response {
190 my ($self, $response) = @_;
191  
192 if ($response eq RESPONSE_SAVE) {
193 $self->save;
194  
195 } elsif ($response eq 'ok') {
196 $self->save;
197 $self->hide;
198  
199 } elsif ($response eq 'cancel') {
200 $self->hide;
201  
202 } elsif ($response eq 'help') {
203 require App::Chart::Manual;
204 App::Chart::Manual->open(__p('manual-node','View Style'), $self);
205 }
206 }
207  
208 # 'show' class closure
209 sub _do_show {
210 my ($self) = @_;
211 $self->load;
212 return shift->signal_chain_from_overridden(@_);
213 }
214  
215 sub load {
216 my ($self) = @_;
217 require App::Chart::Gtk2::View;
218 $self->set_viewstyle (App::Chart::Gtk2::View::viewstyle_read());
219 }
220 sub save {
221 my ($self) = @_;
222 require App::Chart::Gtk2::View;
223 App::Chart::Gtk2::View::viewstyle_write ($self->get_viewstyle);
224 }
225  
226 # 'notify' signal class closure
227 sub _do_notify {
228 my ($self, $pspec) = @_;
229 $self->signal_chain_from_overridden ($pspec);
230  
231 if ($pspec->get_name eq 'viewstyle') {
232 if (my $view = $self->{'view'}) {
233 $view->set (viewstyle => $self->get_viewstyle);
234 }
235 }
236 }
237  
238 sub _do_graphstyle_notify {
239 my ($graphstylewidget, $pspec) = @_;
240 my $self = $graphstylewidget->get_ancestor(__PACKAGE__) || return;
241 if ($pspec->get_name eq 'graphstyle') {
242 ### ViewStyleDialog _do_graphstyle_notify() change
243 $self->notify ('viewstyle');
244 }
245 }
246  
247 sub _do_add_graph {
248 my ($button) = @_;
249 my $self = $button->get_ancestor(__PACKAGE__);
250 my $viewstyle = $self->get_viewstyle;
251 push @{$viewstyle->{'graphs'}}, { size => 1,
252 indicators => [{ key => 'None' }],
253 };
254 $self->set_viewstyle ($viewstyle);
255 }
256  
257 sub _do_graphstyle_delete {
258 my ($graphstylewidget) = @_;
259 my $self = $graphstylewidget->get_ancestor(__PACKAGE__) || return;
260 my $i = $graphstylewidget->{'i'};
261  
262 my $viewstyle = $self->get_viewstyle;
263 my $graphstyles = $viewstyle->{'graphs'};
264 splice @$graphstyles, $i,1;
265 $self->set_viewstyle ($viewstyle);
266 }
267  
268  
269 package App::Chart::Gtk2::GraphStyleWidget;
270 use strict;
271 use warnings;
272 use Gtk2;
273 use Locale::TextDomain ('App-Chart');
274  
275 use App::Chart;
276 use App::Chart::Gtk2::GUI;
277  
278 use Glib::Object::Subclass
279 'Gtk2::VBox',
280 properties => [Glib::ParamSpec->scalar
281 ('graphstyle',
282 'graphstyle',
283 'Blurb.',
284 Glib::G_PARAM_READWRITE),
285 ],
286 signals => { delete_graph => { param_types => [],
287 return_type => undef,
288 flags => ['action'] },
289 };
290  
291 sub INIT_INSTANCE {
292 my ($self) = @_;
293  
294 $self->pack_start (Gtk2::HSeparator->new, 0,0,0);
295  
296 my $hbox = $self->{'hbox'} = Gtk2::HBox->new (0, 0);
297 $self->pack_start ($hbox, 0,0,0);
298  
299 my $label = Gtk2::Label->new (__('Graph') . ' ');
300 $hbox->pack_start ($label, 0,0,0);
301  
302 my $spin_label = Gtk2::Label->new (__('Size'));
303 $hbox->pack_start ($spin_label, 0,0,0);
304  
305 my $size_adj = $self->{'size_adj'}
306 = Gtk2::Adjustment->new (1, # initial
307 1, 999, # min,max
308 1,10, # steps
309 0); # page_size
310 $size_adj->signal_connect (value_changed => \&_do_size_adj_changed,
311 App::Chart::Glib::Ex::MoreUtils::ref_weak($self));
312 my $size_spin = Gtk2::SpinButton->new ($size_adj, 10, 0);
313 $size_spin->set_tooltip_text
314 (__('The size of this graph, relative to the others'));
315 $hbox->pack_start ($size_spin, 0,0,0);
316  
317 my $delete_button = Gtk2::Button->new_with_label (__('Delete Graph'));
318 $hbox->pack_end ($delete_button, 0,0,0);
319 $delete_button->signal_connect (clicked => \&_do_delete_graph);
320  
321 my $add_button = Gtk2::Button->new_with_label (__('Add Indicator'));
322 $hbox->pack_end ($add_button, 0,0,0);
323 $add_button->signal_connect (clicked => \&_do_add_indicator);
324 }
325  
326 sub GET_PROPERTY {
327 my ($self, $pspec) = @_;
328 my $pname = $pspec->get_name;
329 if ($pspec->get_name eq 'graphstyle') {
330 return $self->get_graphstyle;
331 } else {
332 return $self->{$pname};
333 }
334 }
335  
336 sub SET_PROPERTY {
337 my ($self, $pspec, $newval) = @_;
338 my $pname = $pspec->get_name;
339 if ($pspec->get_name eq 'graphstyle') {
340 $self->set_graphstyle ($newval);
341 } else {
342 $self->{$pname} = $newval;
343 }
344 }
345  
346 sub get_graphstyle {
347 my ($self) = @_;
348 my $graphstyle = { size => $self->{'size_adj'}->value,
349 indicators => [ map {$_->get('indicatorstyle')}
350 $self->indicatorstylewidgets ],
351 };
352  
353 my $linestyle = ($self->{'linestylecombo'}
354 && $self->{'linestylecombo'}->get('linestyle'));
355 if (defined $linestyle) {
356 $graphstyle->{'linestyle'} = $linestyle;
357 }
358 return $graphstyle;
359 }
360  
361 sub set_graphstyle {
362 my ($self, $graphstyle) = @_;
363 $self->{'size_adj'}->set_value ($graphstyle->{'size'} // 1);
364  
365 my $linestyle = $graphstyle->{'linestyle'};
366 if (defined $linestyle) {
367 require App::Chart::Gtk2::LineStyleComboBox;
368 my $linestylecombo = ($self->{'linestylecombo'} ||= do {
369 my $combo = App::Chart::Gtk2::LineStyleComboBox->new (visible => 1);
370 $combo->signal_connect (changed => \&_do_linestyle_combo_changed);
371 my $hbox = $self->{'hbox'};
372 $hbox->pack_start ($combo, 0,0, Gtk2::Ex::Units::em($combo));
373 $hbox->reorder_child ($combo, 3);
374 $combo;
375 });
376 $linestylecombo->set(linestyle => $linestyle);
377 } else {
378 delete $self->{'linestylecombo'};
379 }
380  
381 my $indicators = $graphstyle->{'indicators'};
382 my @indicatorwidgets = $self->indicatorstylewidgets;
383 while (@indicatorwidgets > @$indicators) {
384 $self->remove (pop @indicatorwidgets);
385 }
386 for (my $i = 0; $i < @$indicators; $i++) {
387 my $indicatorstylewidget = $indicatorwidgets[$i] || do {
388 my $w = App::Chart::IndicatorStyleWidget->new
389 (visible => 1,
390 type => (defined $linestyle ? 'average' : 'indicator'));
391 $w->signal_connect ('notify::indicatorstyle',
392 \&_do_indicatorstyle_notify, $i);
393 $self->pack_start ($w, 0,0,0);
394 $w;
395 };
396 $indicatorstylewidget->set (indicatorstyle => $indicators->[$i])
397 }
398 ### indicatorstylewidgets: join(' ',$self->indicatorstylewidgets)
399 }
400  
401 sub _do_size_adj_changed {
402 my ($adj, $ref_weak_self) = @_;
403 my $self = $$ref_weak_self || return;
404 ### GraphStyleWidget _do_size_adj_changed()
405 $self->notify ('graphstyle');
406 }
407  
408 sub _do_linestyle_combo_changed {
409 my ($combo) = @_;
410 my $self = $combo->get_ancestor (__PACKAGE__);
411 ### GraphStyleWidget _do_linestyle_combo_changed(): "$combo"
412 $self->notify ('graphstyle');
413 }
414  
415 sub _do_add_indicator {
416 my ($button) = @_;
417 my $self = $button->get_ancestor(__PACKAGE__) || return;
418 ### GraphStyleWidget _do_add_indicator()
419  
420 my $graphstyle = $self->get_graphstyle;
421 push @{$graphstyle->{'indicators'}}, { key => 'None',
422 ma_days => 20,
423 };
424 ### GraphStyleWidget _do_add_indicator(): $graphstyle
425 $self->set_graphstyle ($graphstyle);
426 }
427 sub _do_delete_graph {
428 my ($button) = @_;
429 my $self = $button->get_ancestor(__PACKAGE__) || return;
430 ### GraphStyleWidget _do_delete_graph()
431 $self->signal_emit('delete_graph');
432 }
433  
434 sub indicatorstylewidgets {
435 my ($self) = @_;
436 return grep {$_->isa('App::Chart::IndicatorStyleWidget')} $self->get_children;
437 }
438  
439 sub _do_indicatorstyle_notify {
440 my ($indicatorstylewidget, $pspec, $n) = @_;
441 my $self = $indicatorstylewidget->get_ancestor(__PACKAGE__) || return;
442 ### GraphStyleWidget _do_indicatorstyle_notify()
443  
444 my $graphstyle = $self->get_graphstyle;
445 $graphstyle->{'indicators'}->[$n]
446 = $indicatorstylewidget->get('indicatorstyle');
447 $self->notify ('graphstyle');
448 }
449  
450  
451 package App::Chart::IndicatorStyleWidget;
452 use strict;
453 use warnings;
454 use Gtk2;
455 use Locale::TextDomain ('App-Chart');
456  
457 use App::Chart;
458 use App::Chart::Gtk2::GUI;
459 use App::Chart::IndicatorInfo;
460  
461 use Glib::Object::Subclass
462 'Gtk2::VBox',
463 properties => [ Glib::ParamSpec->scalar
464 ('indicatorstyle',
465 'indicatorstyle',
466 'Blurb.',
467 Glib::G_PARAM_READWRITE),
468  
469 Glib::ParamSpec->string
470 ('type',
471 'type',
472 'Indicator type either "indicator" or "average".',
473 '',
474 Glib::G_PARAM_READWRITE),
475 ];
476  
477 sub INIT_INSTANCE {
478 my ($self) = @_;
479  
480 my $hbox = Gtk2::HBox->new;
481 $self->pack_start ($hbox, 0,0,0);
482  
483 require App::Chart::Gtk2::IndicatorComboBox;
484 my $combo = $self->{'combo'} = App::Chart::Gtk2::IndicatorComboBox->new;
485 $combo->signal_connect (changed => \&_do_combo_changed);
486 $hbox->pack_start ($combo, 0,0,0);
487  
488 my $info_button = $self->{'info_button'}
489 = Gtk2::Button->new_from_stock ('gtk-info');
490 $info_button->signal_connect (clicked => \&_do_info_button);
491 $hbox->pack_end ($info_button, 0,0,0);
492  
493 # my $delete_button = $self->{'delete_button'}
494 # = Gtk2::Button->new_from_stock ('gtk-delete');
495 # $delete_button->signal_connect (clicked => \&_do_delete_button);
496 # $hbox->pack_end ($delete_button, 0,0,0);
497  
498 my $parambox = $self->{'parambox'} = Gtk2::HBox->new;
499 $parambox->show;
500 $self->pack_start ($parambox, 0,0,0);
501 $parambox->pack_start (Gtk2::Label->new(' '), 0,0,0);
502  
503 $hbox->show_all;
504 $parambox->show_all;
505 }
506  
507 sub SET_PROPERTY {
508 my ($self, $pspec, $newval) = @_;
509 my $pname = $pspec->get_name;
510 $self->{$pname} = $newval;
511  
512 if ($pname eq 'indicatorstyle') {
513 my $indicatorstyle = $newval;
514 $self->{'combo'}->set_key ($indicatorstyle->{'key'});
515 $self->{'indicatorstyle'} = { %$newval }; # copy
516  
517 } elsif ($pname eq 'type') {
518 $self->{'combo'}->set (type => $newval);
519 }
520 }
521  
522 sub _do_info_button {
523 my ($info_button) = @_;
524 # supposed to be insensitive when no manual, but check just in case
525 my $manual = $info_button->{'manual'} || return;
526 my $self = $info_button->get_ancestor(__PACKAGE__) || return;
527 require App::Chart::Manual;
528 App::Chart::Manual->open ($manual, $self);
529 }
530 sub _update_info_tooltip {
531 my ($self) = @_;
532 my $info_button = $self->{'info_button'};
533 my $manual = $info_button->{'manual'} = $self->indicator_info->manual;
534 $info_button->set_tooltip_text
535 ($manual ? __x('Open the manual at the section "{section}"',
536 section => $manual)
537 : undef);
538 $info_button->set_sensitive ($manual);
539 }
540  
541 sub _do_delete_button {
542 # my ($delete_button) = @_;
543 # my $self = $delete_button->get_ancestor(__PACKAGE__) || return;
544 print "Can't delete indicator yet\n";
545 }
546  
547 sub _update_params {
548 my ($self) = @_;
549 my $indicatorstyle = $self->{'indicatorstyle'};
550 my $parameter_info = $self->indicator_info->parameter_info;
551  
552 my $parambox = $self->{'parambox'};
553 my @paramwidgets = $parambox->get_children;
554 shift @paramwidgets; # not the spacer
555 while (@paramwidgets > @$parameter_info) {
556 $parambox->remove (pop @paramwidgets);
557 }
558  
559 local $self->{'update_in_progress'} = 1;
560 for (my $i = 0; $i < @$parameter_info; $i++) {
561 my $paramwidget = ($paramwidgets[$i] || do {
562 my $pp = App::Chart::IndicatorParamWidget->new (visible => 1);
563 $pp->signal_connect ('notify::paramvalue' => \&_do_paramvalue_changed, $i);
564 $parambox->pack_start ($pp, 0,0,0);
565 $pp;
566 });
567 my $paramkey = $parameter_info->[$i]->{'key'};
568 my $paramvalue = ($indicatorstyle->{$paramkey}
569 // $parameter_info->[$i]->{'default'});
570 require Glib::Ex::FreezeNotify;
571 my $freezer = Glib::Ex::FreezeNotify->new ($paramwidget);
572 $paramwidget->set (paraminfo => $parameter_info->[$i],
573 paramvalue => $paramvalue);
574 }
575 }
576  
577 # return a App::Chart::IndicatorInfo
578 sub indicator_info {
579 my ($self) = @_;
580 my $indicatorstyle = $self->{'indicatorstyle'};
581 return App::Chart::IndicatorInfo->new ($indicatorstyle->{'key'});
582 }
583  
584 sub _do_combo_changed {
585 my ($combo) = @_;
586 my $self = $combo->get_ancestor(__PACKAGE__) || return;
587 my $indicatorstyle = $self->{'indicatorstyle'};
588 $indicatorstyle->{'key'} = $self->{'combo'}->get('key');
589 _update_info_tooltip ($self);
590 _update_params ($self);
591 $self->notify ('indicatorstyle');
592 }
593 sub _do_paramvalue_changed {
594 my ($paramwidget, $pspec, $n) = @_;
595 ### IndicatorStyleWidget _do_paramvalue_changed()
596 my $self = $paramwidget->get_ancestor(__PACKAGE__) || return;
597  
598 my $parameter_info = $self->indicator_info->parameter_info;
599 my $indicatorstyle = $self->{'indicatorstyle'};
600  
601 my $param_key = $parameter_info->[$n]->{'key'};
602 my $paramvalue = $paramwidget->get('paramvalue');
603 if ($paramvalue == $parameter_info->[$n]->{'default'}) {
604 delete $indicatorstyle->{$param_key};
605 } else {
606 $indicatorstyle->{$param_key} = $paramwidget->get('paramvalue');
607 }
608  
609 ### indicatorstyle: $self->get('indicatorstyle')
610 if (! $self->{'update_in_progress'}) {
611 $self->notify('indicatorstyle');
612 }
613 }
614  
615  
616 package App::Chart::IndicatorParamWidget;
617 use 5.010;
618 use strict;
619 use warnings;
620 use Carp;
621 use Gtk2;
622 use POSIX ();
623 use Locale::TextDomain ('App-Chart');
624  
625 use App::Chart;
626 use App::Chart::Gtk2::GUI;
627 use App::Chart::IndicatorInfo;
628  
629 use Glib::Object::Subclass
630 'Gtk2::HBox',
631 properties => [ Glib::ParamSpec->scalar
632 ('paraminfo',
633 'paraminfo',
634 'Blurb.',
635 Glib::G_PARAM_READWRITE),
636  
637 Glib::ParamSpec->scalar
638 ('paramvalue',
639 'paramvalue',
640 'Blurb.',
641 Glib::G_PARAM_READWRITE),
642 ];
643  
644 sub INIT_INSTANCE {
645 my ($self) = @_;
646  
647 my $label = $self->{'label'} = Gtk2::Label->new ('');
648 $self->pack_start ($label, 0,0,0);
649  
650 my $adj = $self->{'adj'}
651 = Gtk2::Adjustment->new (1, # initial
652 1, 9999, # min,max
653 1,10, # steps
654 0); # page_size
655 $adj->signal_connect (value_changed => \&_do_adj_value_changed,
656 App::Chart::Glib::Ex::MoreUtils::ref_weak($self));
657 }
658  
659 sub SET_PROPERTY {
660 my ($self, $pspec, $newval) = @_;
661 my $pname = $pspec->get_name;
662 $self->{$pname} = $newval;
663 ### IndicatorParamWidget SET_PROPERTY: $pname, $newval
664  
665 if ($pname eq 'paramvalue') {
666 my $paraminfo = $self->{'paraminfo'} // return;
667 my $type = $paraminfo->{'type'} // 'integer';
668 if ($type eq 'boolean') {
669 $self->{'child'}->set_active ($newval);
670 } else {
671 $self->{'child'}->set_value ($newval);
672 }
673  
674 } elsif ($pname eq 'paraminfo') {
675 my $paraminfo = $self->{'paraminfo'};
676 if ($paraminfo) {
677 if (my $child = delete $self->{'child'}) {
678 $self->remove ($child);
679 }
680  
681 my $type = $paraminfo->{'type'} // 'integer';
682 if ($type eq 'boolean') {
683 my $check = $self->{'child'} = Gtk2::CheckButton->new;
684 $check->signal_connect ('notify::active',
685 \&_do_checkbutton_changed);
686 $self->pack_start ($check, 0,0,0);
687  
688 } else {
689 # 'integer' or 'float'
690 my $step = $paraminfo->{'step'} // 1;
691 $self->{'adj'}->set
692 (lower => $paraminfo->{'minimum'} // 0,
693 upper => $paraminfo->{'maximum'} // POSIX::FLT_MAX(),
694 page_increment => $step,
695 step_increment => $step,
696 page_size => 0);
697 $self->{'adj'}->changed;
698  
699 my $spin = $self->{'child'}
700 = Glib::Object::new ('Gtk2::SpinButton',
701 adjustment => $self->{'adj'},
702 climb_rate => $step,
703 digits => ($paraminfo->{'decimals'}
704 // ($type eq 'float' ? 1 : 0)),
705 xalign => 1.0);
706 $self->pack_start ($spin, 0,0,0);
707 }
708 $self->{'child'}->show;
709  
710 $self->{'label'}->set_text ($paraminfo->{'name'} . ' ');
711 }
712 }
713 }
714  
715 sub _do_adj_value_changed {
716 my ($adj, $ref_weak_self) = @_;
717 my $self = $$ref_weak_self || return;
718 $self->{'paramvalue'} = $adj->value;
719 $self->notify('paramvalue');
720 }
721  
722 sub _do_checkbutton_changed {
723 my ($check) = @_;
724 my $self = $check->get_ancestor(__PACKAGE__) || return;
725 $self->{'paramvalue'} = $check->get_active;
726 $self->notify('paramvalue');
727 }
728  
729  
730 1;
731 __END__