App/Chart/Gtk2/IntradayModeComboBox.pm untested

filter: Statementstmt 0.0% 0 / 108 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 10 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 157 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__
168  
169 =for stopwords combobox intraday ComboBox programmatically
170  
171 =head1 NAME
172  
173 App::Chart::Gtk2::IntradayModeComboBox -- combobox intraday mode selector
174  
175 =head1 SYNOPSIS
176  
177 use App::Chart::Gtk2::IntradayModeComboBox;
178 my $combobox = App::Chart::IntradayModeCombo->new;
179  
180 =head1 WIDGET HIERARCHY
181  
182 C<App::Chart::Gtk2::IntradayModeComboBox> is a subclass of C<Gtk2::ComboBox>,
183  
184 Gtk2::Widget
185 Gtk2::Container
186 Gtk2::Bin
187 Gtk2::ComboBox
188 App::Chart::Gtk2::IntradayModeComboBox
189  
190 =head1 DESCRIPTION
191  
192 C<App::Chart::Gtk2::IntradayModeComboBox> presents available intraday data
193 modes, like "1 Day" or "5 Days", for a given stock symbol. The C<mode>
194 property changes according to the mode selected.
195  
196 =head1 FUNCTIONS
197  
198 =over 4
199  
200 =item C<< App::Chart::IntradayModeCombo->new (key=>value,...) >>
201  
202 Create and return a new C<App::Chart::IntradayModeCombo> object. Optional
203 key/value pairs set initial properties as per C<< Glib::Object->new >>.
204  
205 =back
206  
207 =head1 PROPERTIES
208  
209 =over 4
210  
211 =item C<symbol> (string, default empty "")
212  
213 The symbol for which to display intraday modes. If the symbol has no
214 intraday handlers at all then the ComboBox is empty and insensitive.
215  
216 When changing the symbol if the current C<mode> is not available in the new
217 symbol then it changes to the first mode of that new symbol.
218  
219 =item C<mode> (string, default empty "")
220  
221 The selected intraday mode key, such as C<"1d">. This changes with the user
222 selection, and can be set programmatically to change the ComboBox display.
223  
224 =back
225  
226 =head1 SEE ALSO
227  
228 L<App::Chart::Gtk2::IntradayDialog>
229  
230 =head1 HOME PAGE
231  
232 L<http://user42.tuxfamily.org/chart/index.html>
233  
234 =head1 LICENCE
235  
236 Copyright 2008, 2009, 2010, 2011 Kevin Ryde
237  
238 Chart is free software; you can redistribute it and/or modify it under the
239 terms of the GNU General Public License as published by the Free Software
240 Foundation; either version 3, or (at your option) any later version.
241  
242 Chart is distributed in the hope that it will be useful, but WITHOUT ANY
243 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
244 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
245 details.
246  
247 You should have received a copy of the GNU General Public License along with
248 Chart; see the file F<COPYING>. Failing that, see
249 L<http://www.gnu.org/licenses/>.
250  
251 =cut