App/Chart/Gtk2/IntradayModeComboBox.pm untested

filter: Statementstmt 0.0% 0 / 108 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 163 CC 10 SCAR 35.3
CC
10
Cov
0%
SET_PROPERTY
90.0
_do_changed
6.0
INIT_INSTANCE
2.0
CRAP 34.0
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::IntradayModeComboBox;
18 use 5.008;
19 use strict;
20 use warnings;
21 use Carp;
22 use Gtk2;
23 use Locale::TextDomain ('App-Chart');
24  
25 use App::Chart::Glib::Ex::MoreUtils;
26 use App::Chart::Intraday;
27  
28 # uncomment this to run the ### lines
29 #use Smart::Comments;
30  
31 use Glib::Object::Subclass
32 'Gtk2::ComboBox',
33 signals => { changed => \&_do_changed },
34 properties => [ Glib::ParamSpec->string
35 ('symbol',
36 __('Symbol'),
37 'The symbol for which to display modes.',
38 '',
39 Glib::G_PARAM_READWRITE),
40  
41 Glib::ParamSpec->string
42 ('mode',
43 'mode',
44 'The currently selected mode, or empty when no modes at all.',
45 '',
46 Glib::G_PARAM_READWRITE)];
47  
48 use constant { COL_MODE => 0,
49 COL_NAME => 1,
50 NUM_COLUMNS => 2 };
51  
52 sub INIT_INSTANCE {
53 my ($self) = @_;
54 $self->{'symbol'} = '';
55 $self->{'mode'} = '';
56  
57 my $model = Gtk2::ListStore->new (('Glib::String') x NUM_COLUMNS);
58 $self->set_model ($model);
59  
60 my $renderer = Gtk2::CellRendererText->new;
61 $renderer->set (ypad => 0);
62 $self->pack_start ($renderer, 1);
63 $self->set_attributes ($renderer, markup => COL_NAME);
64 }
65  
66 sub SET_PROPERTY {
67 my ($self, $pspec, $newval) = @_;
68 my $pname = $pspec->get_name;
69 my $model = $self->get_model;
70  
71 my $oldval = $self->{$pname};
72 if ($oldval eq $newval) { return; }
73  
74 if ($pname eq 'mode') {
75 my $mode = $newval;
76  
77 my $found = 0;
78 $model->foreach (sub {
79 my ($model, $path, $iter) = @_;
80 if ($mode eq $model->get_value ($iter, COL_MODE)) {
81 $found = 1;
82 $self->{'mode'} = $mode;
83 $self->set_active_iter ($iter);
84 return 1; # stop
85 }
86 return 0; # continue
87 });
88 if (! $found) {
89 Glib->warning (undef, "IntradayModeComboBox: no such mode \"$mode\"");
90 }
91 return;
92 }
93  
94 $self->{$pname} = $newval; # per default GET_PROPERTY
95  
96 if ($pname eq 'symbol') {
97 my $symbol = $newval;
98 $model->clear;
99 my $old_mode = $self->{'mode'};
100  
101 # create and fill the accelgroup always for now, since the intraday
102 # dialog always wants it
103 my $accelgroup = $self->accelgroup;
104 my $ref_weak_self = App::Chart::Glib::Ex::MoreUtils::ref_weak($self);
105  
106 # remove old entries
107 my $accelgroup_keyvals = $self->{'accelgroup_keyvals'};
108 while (my $keyval = pop @$accelgroup_keyvals) {
109 $accelgroup->disconnect_key ($keyval, ['mod1-mask']);
110 }
111  
112 require App::Chart::IntradayHandler;
113 my @handler_list
114 = $symbol ? App::Chart::IntradayHandler->handlers_for_symbol ($symbol)
115 : ();
116 ### symbol handler count: $symbol." ".scalar(@handler_list)
117  
118 my $active_pos = 0;
119 foreach my $i (0 .. $#handler_list) {
120 my $h = $handler_list[$i];
121 my $name = $h->name_as_markup;
122 my $mode = $h->{'mode'};
123 if ($mode eq $old_mode) {
124 $active_pos = $i;
125 }
126 $model->set ($model->append,
127 COL_MODE, $mode,
128 COL_NAME, $name);
129  
130 if ($accelgroup && defined (my $key = $h->name_mnemonic_key)) {
131 my $keyval = Gtk2::Gdk->keyval_from_name($key);
132 push @$accelgroup_keyvals, $keyval;
133 $accelgroup->connect ($keyval, ['mod1-mask'], [],
134 sub { _do_accelgroup ($ref_weak_self, $mode) });
135  
136 }
137 }
138 $self->set_active ($active_pos);
139 $self->set_sensitive (@handler_list != 0);
140 }
141 }
142  
143 # 'changed' class closure
144 sub _do_changed {
145 my ($self) = @_;
146 my $model = $self->get_model;
147 my $iter = $self->get_active_iter || return; # possibly nothing
148 my $mode = $model->get_value ($iter, COL_MODE);
149 if ($self->{'mode'} ne $mode) {
150 $self->{'mode'} = $mode;
151 $self->notify ('mode');
152 }
153 }
154  
155 sub accelgroup {
156 my ($self) = @_;
157 return ($self->{'accelgroup'} ||= Gtk2::AccelGroup->new);
158 }
159  
160 sub _do_accelgroup {
161 my ($ref_weak_self, $mode) = @_;
162 my $self = $$ref_weak_self || return;
163 $self->set (mode => $mode);
164 }
165  
166 1;
167 __END__