App/financeta/gui/security_wizard.pm

filter: Statementstmt 90.0% 18 / 20 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.5% 25 / 27 CC 1 SCAR 0.4
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« App/financeta.pm ↑ Summary App/financeta/gui/editor.pm »
1 package App::financeta::gui::security_wizard;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use App::financeta::mo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use App::financeta::utils qw(dumper log_filter);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Log::Any '$log', filter => \&App::financeta::utils::log_filter;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 0 ▶use Prima qw(
Subroutine BEGIN: called CC=1 SCAR=2.6
10 Application Buttons MsgBox Calendar Label InputLine ComboBox
11 sys::GUIException Utils
12 0 );
13 use Try::Tiny;
14 use DateTime;
15 use Browser::Open ();
16  
17 $|=1;
18  
19 has owner => undef;
20 has gui => (required => 1);
21 has wizard => ( builder => '_build_wizard' );
22  
23 sub run {
24 my $self = shift;
25 my $w = $self->wizard;
26 my $res = $w->execute();
27 $w->end_modal;
28 return $res;
29 }
30  
31 sub _build_wizard {
32 my $self = shift;
33 my $gui = $self->gui;
34 $log->debug("GUI reference in security_wizard: " . ref($gui));
35 my $w_x0 = 50;
36 my $w_y0 = 50;
37 my $sz_x = 800;
38 my $sz_y = 600;
39 my $w = Prima::Dialog->new(
40 name => 'sec_wizard',
41 centered => 1,
42 origin => [$w_x0, $w_y0],
43 size => [$sz_x, $sz_y],
44 text => 'Security Wizard',
45 icon => $gui->icon,
46 visible => 1,
47 taskListed => 0,
48 onExecute => sub {
49 my $dlg = shift;
50 $dlg->input_source->focusedItem($gui->current->{source_index} // 0);
51 my $sec = $gui->current->{symbol} || '';
52 $dlg->input_symbol->text($sec);
53 $dlg->btn_ok->enabled(length($sec) ? 1 : 0);
54 $dlg->btn_cancel->enabled(1);
55 if ($gui->current->{start_date}) {
56 my $dt = $gui->current->{start_date};
57 $dlg->cal_start->date($dt->day, $dt->month - 1, $dt->year - 1900);
58 } else {
59 $dlg->cal_start->date_from_time(gmtime);
60 # reduce 1 year
61 my $yr = $dlg->cal_start->year;
62 $dlg->cal_start->year($yr - 1);
63 }
64 if ($gui->current->{end_date}) {
65 my $dt = $gui->current->{end_date};
66 $dlg->cal_end->date($dt->day, $dt->month - 1, $dt->year - 1900);
67 } else {
68 $dlg->cal_end->date_from_time(gmtime);
69 }
70 $dlg->chk_force_download->checked(0);
71 $gui->current->{force_download} = 0;
72 },
73 );
74 my $pwin = $self->owner;
75 $w->owner($pwin) if defined $pwin;
76 $w->insert(
77 Label => text => 'Select Source',
78 name => 'label_source',
79 alignment => ta::Left,
80 autoHeight => 1,
81 origin => [ 20, $sz_y - 40],
82 autoWidth => 1,
83 font => { height => 14, style => fs::Bold },
84 hint => 'Stock symbols are available at Yahoo! Finance',
85 );
86 $w->insert(
87 ComboBox => name => 'input_source',
88 style => cs::DropDownList,
89 multiSelect => 0,
90 alignment => ta::Left,
91 width => 200,
92 height => 30,
93 autoTab => 1,
94 origin => [ 200, $sz_y - 40],
95 font => { height => 16 },
96 items => $gui->list_sources_pretty,
97 selectedItems => [0],
98 onChange => sub {
99 my $inp = shift;
100 my $idx = $inp->focusedItem;
101 $gui->current->{source_index} = $idx;
102 $log->info("Selected input source index: $idx");
103 },
104 );
105 $w->insert(
106 Label => text => 'Enter Security Symbol',
107 name => 'label_symbol',
108 alignment => ta::Left,
109 autoHeight => 1,
110 origin => [ 20, $sz_y - 80],
111 autoWidth => 1,
112 font => { height => 14, style => fs::Bold },
113 hint => 'Stock symbols are available at Yahoo! Finance',
114 );
115 $w->insert(
116 InputLine => name => 'input_symbol',
117 alignment => ta::Left,
118 autoHeight => 1,
119 width => 160,
120 autoTab => 1,
121 maxLen => 10,
122 origin => [ 200, $sz_y - 80],
123 font => { height => 16 },
124 onChange => sub {
125 my $inp = shift;
126 my $owner = $inp->owner;
127 unless (length $inp->text) {
128 $owner->btn_ok->enabled(0);
129 } else {
130 $owner->btn_ok->enabled(1);
131 }
132 },
133 );
134 $w->insert(
135 Button => name => 'btn_help',
136 text => 'Symbol Search',
137 height => 20,
138 autoWidth => 1,
139 origin => [$sz_x - 100, $sz_y - 80],
140 default => 0,
141 enabled => 1,
142 font => { height => 12, style => fs::Bold },
143 onClick => sub {
144 my $owner = shift->owner;
145 my $arr = $gui->list_sources_urls;
146 my $idx = $owner->input_source->focusedItem;
147 my $url = $arr->[$idx // 0];
148 $log->info("Opening URL $url");
149 my $ok = Browser::Open::open_browser($url, 1);
150 if (not defined $ok) {
151 message("Error finding a browser to open $url");
152 } elsif ($ok != 0) {
153 message("Error opening $url");
154 }
155 },
156 );
157 $w->insert(
158 Label => text => 'Select Start Date',
159 name => 'label_enddate',
160 alignment => ta::Center,
161 autoHeight => 1,
162 autoWidth => 1,
163 origin => [ 20, $sz_y - 220 ],
164 font => { height => 14, style => fs::Bold },
165 );
166 $w->insert(
167 Calendar => name => 'cal_start',
168 useLocale => 1,
169 size => [ 220, 200 ],
170 origin => [ 20, $sz_y - 440 ],
171 font => { height => 16 },
172 onChange => sub {
173 my $cal = shift;
174 $gui->current->{start_date} = DateTime->new(
175 year => 1900 + $cal->year(),
176 month => 1 + $cal->month(),
177 day => $cal->day(),
178 time_zone => $gui->timezone,
179 );
180 },
181 );
182 $w->insert(
183 Label => text => 'Select End Date',
184 name => 'label_enddate',
185 alignment => ta::Center,
186 autoHeight => 1,
187 autoWidth => 1,
188 origin => [ $sz_x / 2, $sz_y - 220 ],
189 font => { height => 14, style => fs::Bold },
190 );
191 $w->insert(
192 Calendar => name => 'cal_end',
193 useLocale => 1,
194 size => [ 220, 200 ],
195 origin => [ $sz_x / 2, $sz_y - 440 ],
196 font => { height => 16 },
197 onChange => sub {
198 my $cal = shift;
199 $gui->current->{end_date} = DateTime->new(
200 year => 1900 + $cal->year(),
201 month => 1 + $cal->month(),
202 day => $cal->day(),
203 time_zone => $gui->timezone,
204 );
205 },
206 );
207 $w->insert(
208 CheckBox => name => 'chk_force_download',
209 text => 'Force Download',
210 origin => [ 20, $sz_y - 500 ],
211 font => { height => 14, style => fs::Bold },
212 onCheck => sub {
213 my $chk = shift;
214 my $owner = $chk->owner;
215 if ($chk->checked) {
216 $gui->current->{force_download} = 1;
217 } else {
218 $gui->current->{force_download} = 0;
219 }
220 },
221 );
222 $w->insert(
223 Button => name => 'btn_csv',
224 text => 'Load from CSV',
225 autoHeight => 1,
226 autoWidth => 1,
227 origin => [ 200, $sz_y - 500 ],
228 default => 0,
229 enabled => 1,
230 font => { height => 16, style => fs::Bold },
231 onClick => sub {
232 my $btn = shift;
233 my $owner = $btn->owner;
234 $owner->hide;
235 my $dlg = Prima::Dialog::OpenDialog->new(
236 filter => [
237 ['CSV files' => '*.csv'],
238 ['All files' => '*'],
239 ],
240 filterIndex => 0,
241 fileMustExist => 1,
242 multiSelect => 0,
243 directory => $gui->tmpdir,
244 );
245 my $csv = $dlg->fileName if $dlg->execute;
246 if (defined $csv and -e $csv) {
247 $log->info("You have selected $csv to load");
248 if ($owner->label_csv) {
249 $owner->label_csv->text($csv);
250 }
251 $gui->current->{csv} = $csv;
252 }
253 $owner->show;
254 },
255 );
256 $w->insert(
257 Label => text => '',
258 name => 'label_csv',
259 alignment => ta::Left,
260 autoHeight => 1,
261 autoWidth => 1,
262 origin => [ 360, $sz_y - 500 ],
263 font => { height => 13, style => fs::Bold },
264 );
265 $w->insert(
266 Button => name => 'btn_cancel',
267 text => 'Cancel',
268 autoHeight => 1,
269 autoWidth => 1,
270 origin => [ 20, $sz_y - 550 ],
271 modalResult => mb::Cancel,
272 default => 0,
273 enabled => 1,
274 font => { height => 16, style => fs::Bold },
275 onClick => sub {
276 delete $gui->current->{symbol};
277 delete $gui->current->{start_date};
278 delete $gui->current->{end_date};
279 delete $gui->current->{force_download};
280 delete $gui->current->{csv};
281 },
282 );
283 $w->insert(
284 Button => name => 'btn_ok',
285 text => 'OK',
286 autoHeight => 1,
287 autoWidth => 1,
288 origin => [ 150, $sz_y - 550 ],
289 modalResult => mb::Ok,
290 default => 1,
291 enabled => 0,
292 font => { height => 16, style => fs::Bold },
293 onClick => sub {
294 my $btn = shift;
295 my $owner = $btn->owner;
296 $gui->current->{source_index} = $owner->input_source->focusedItem;
297 $log->info("Selected input source index: " . $gui->current->{source_index});
298 $gui->current->{symbol} = $owner->input_symbol->text;
299 $log->info("Selected input symbol: " . $gui->current->{symbol});
300 unless (defined $gui->current->{start_date}) {
301 my $cal = $owner->cal_start;
302 $gui->current->{start_date} = DateTime->new(
303 year => 1900 + $cal->year(),
304 month => 1 + $cal->month(),
305 day => $cal->day(),
306 time_zone => $gui->timezone,
307 );
308 }
309 unless (defined $gui->current->{end_date}) {
310 my $cal = $owner->cal_end;
311 $gui->current->{end_date} = DateTime->new(
312 year => 1900 + $cal->year(),
313 month => 1 + $cal->month(),
314 day => $cal->day(),
315 time_zone => $gui->timezone,
316 );
317 }
318 },
319 );
320 return $w;
321 }
322  
323  
324 1;
325 __END__
« App/financeta.pm ↑ Summary App/financeta/gui/editor.pm »