App/Chart/Gtk2/WatchlistSymbolMenu.pm untested

filter: Statementstmt 0.0% 0 / 105 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 139 CC 4 SCAR 13.2
CC
4
Cov
0%
popup_from_treeview
12.0
SET_PROPERTY
6.0
INIT_INSTANCE
2.0
CRAP 3.7
1 # Copyright 2007, 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::WatchlistSymbolMenu;
18 use 5.010;
19 use strict;
20 use warnings;
21 use Gtk2;
22 use Locale::TextDomain ('App-Chart');
23  
24 use App::Chart;
25 use App::Chart::Gtk2::Symlist;
26 use App::Chart::Gtk2::Ex::ToplevelBits;
27  
28 use Glib::Object::Subclass
29 'Gtk2::Menu',
30 properties => [ Glib::ParamSpec->string
31 ('symbol',
32 __('Symbol'),
33 'Stock or commodity symbol string.',
34 '', # default
35 Glib::G_PARAM_READWRITE),
36  
37 Glib::ParamSpec->object
38 ('symlist',
39 'symlist',
40 'The symlist the symbol is from.',
41 'App::Chart::Gtk2::Symlist',
42 Glib::G_PARAM_READWRITE),
43  
44 ];
45  
46 sub INIT_INSTANCE {
47 my ($self) = @_;
48 ### WatchlistSymbolMenu INIT_INSTANCE()
49 {
50 my $menu_title = $self->{'menu_title'} = Gtk2::Label->new ("X");
51 $menu_title->set_alignment (0.5, 0); # centre horizontally
52 my $item = Gtk2::MenuItem->new;
53 $item->add ($menu_title);
54 $self->append ($item);
55 }
56 $self->append (Gtk2::SeparatorMenuItem->new);
57  
58 {
59 my $item = Gtk2::ImageMenuItem->new_from_stock ('gtk-refresh');
60 $item->set_tooltip_text (__('Download a new quote for this symbol'));
61 $item->signal_connect (activate => \&_do_refresh);
62 $self->append ($item);
63 }
64 {
65 my $item = Gtk2::MenuItem->new_with_mnemonic (__('_Chart'));
66 $item->set_tooltip_text (__('Go to this symbol in the main Chart window'));
67 $item->signal_connect (activate => \&_do_chart);
68 $self->append ($item);
69 }
70 {
71 my $item = $self->{'menu_intraday'}
72 = Gtk2::MenuItem->new_with_mnemonic (__('_Intraday'));
73 $item->set_tooltip_text (__('View intraday graphs for this symbol'));
74 $item->signal_connect (activate => \&_do_intraday);
75 $self->append ($item);
76 }
77 {
78 my $item = Gtk2::MenuItem->new_with_mnemonic (__('_Web'));
79 $self->append ($item);
80 require App::Chart::Gtk2::WeblinkMenu;
81 my $weblinkmenu = $self->{'weblinkmenu'} = App::Chart::Gtk2::WeblinkMenu->new;
82 $item->set_submenu ($weblinkmenu);
83 }
84 $self->show_all;
85 }
86  
87 sub SET_PROPERTY {
88 my ($self, $pspec, $newval) = @_;
89 my $pname = $pspec->get_name;
90 $self->{$pname} = $newval; # per default GET_PROPERTY
91  
92 if ($pname eq 'symbol') {
93 my $symbol = $newval;
94 $self->{'menu_title'}->set_text ($symbol);
95 $self->{'menu_intraday'}->set_sensitive
96 (App::Chart::Gtk2::WatchlistDialog::symbol_intraday_sensitive($symbol));
97 $self->{'weblinkmenu'}->set (symbol => $symbol);
98 }
99 }
100  
101 # open the main chart display on the current symbol
102 sub _do_chart {
103 my ($menuitem) = @_;
104 my $self = $menuitem->get_ancestor (__PACKAGE__);
105 require App::Chart::Gtk2::Main;
106 my $main = App::Chart::Gtk2::Main->find_for_dialog ($self->{'watchlist'});
107 $main->goto_symbol ($self->{'symbol'}, $self->{'symlist'});
108 $main->present;
109 }
110  
111 sub _do_refresh {
112 my ($menuitem) = @_;
113 my $self = $menuitem->get_ancestor (__PACKAGE__);
114 require App::Chart::Gtk2::Job::Latest;
115 App::Chart::Gtk2::Job::Latest->start ([$self->{'symbol'}]);
116 }
117  
118 sub _do_intraday {
119 my ($menuitem) = @_;
120 my $self = $menuitem->get_ancestor(__PACKAGE__) || return;
121 ### WatchlistSymbolMenu intraday: $self->{'symbol'}
122 App::Chart::Gtk2::Ex::ToplevelBits::popup
123 ('App::Chart::Gtk2::IntradayDialog',
124 properties => { symbol => $self->get('symbol') },
125 screen => $self);
126 }
127  
128 sub popup_from_treeview {
129 my ($class_or_self, $event, $treeview) = @_;
130 my $self = ref $class_or_self ? $class_or_self : $class_or_self->new;
131  
132 my $watchlist = $treeview->get_toplevel;
133 require Scalar::Util;
134 Scalar::Util::weaken ($self->{'watchlist'} = $watchlist);
135  
136 my ($path) = $treeview->get_path_at_pos ($event->x, $event->y);
137 if (! $path) { return; }
138  
139 my $model = $treeview->get_model; # App::Chart::Gtk2::WatchlistModel
140 my $symlist = $model->get_symlist;
141 my $iter = $symlist->get_iter ($path);
142 my $symbol = $symlist->get_value ($iter, $model->COL_SYMBOL);
143 $self->set (symbol => $symbol,
144 symlist => $symlist);
145 $self->set_screen ($treeview->get_screen);
146 $self->popup (undef, undef, undef, undef, $event->button, $event->time);
147 }
148  
149 1;
150 __END__