App/Chart/Gtk2/WatchlistSymbolMenu.pm untested

filter: Statementstmt 0.0% 0 / 105 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 2 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 138 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__
151  
152 =for stopwords watchlist Watchlist WatchlistSymbolMenu Popup undef clickable symlist
153  
154 =head1 NAME
155  
156 App::Chart::Gtk2::WatchlistSymbolMenu -- menu for watchlist symbol
157  
158 =for test_synopsis my ($event, $treeview)
159  
160 =head1 SYNOPSIS
161  
162 use App::Chart::Gtk2::WatchlistSymbolMenu;
163 App::Chart::Gtk2::WatchlistSymbolMenu->popup_from_treeview ($event, $treeview);
164  
165 =head1 WIDGET HIERARCHY
166  
167 C<App::Chart::Gtk2::WatchlistSymbolMenu> is a subclass of C<Gtk::Menu>,
168  
169 Gtk2::Widget
170 Gtk2::Container
171 Gtk2::MenuShell
172 Gtk2::Menu
173 App::Chart::Gtk2::WatchlistSymbolMenu
174  
175 =head1 DESCRIPTION
176  
177 A C<App::Chart::Gtk2::WatchlistSymbolMenu> shows the following things which can be
178 done from a symbol in the Watchlist. It's split out from the main Watchlist
179 code to keep the size there down and since it may not be needed at all if
180 not used.
181  
182 +----------+
183 | <SYMBOL> |
184 +----------+
185 | Chart | view the full chart
186 | Refresh | get a new quote
187 | Intraday | view an intraday graph
188 | Web > | web links from App::Chart::Gtk2::WeblinkMenu
189 +----------+
190  
191 =head1 FUNCTIONS
192  
193 =over 4
194  
195 =item C<< App::Chart::Gtk2::WatchlistSymbolMenu->new (key=>value,...) >>
196  
197 Create and return a new C<App::Chart::Gtk2::WatchlistSymbolMenu> object. Optional
198 key/value pairs set initial properties as per C<< Glib::Object->new >>.
199  
200 =item C<< App::Chart::Gtk2::WatchlistSymbolMenu->popup_from_treeview ($event, $treeview) >>
201  
202 =item C<< $wsmenu->popup_from_treeview ($event, $treeview) >>
203  
204 Popup the given C<$wsmenu>, or as a class method create and popup a new
205 menu, in both cases for the symbol under the mouse pointer in C<$treeview>.
206  
207 C<$event> should be a C<Gtk2::Gdk::Event> giving the mouse position and the
208 model in C<$treeview> is expected to be a C<App::Chart::Gtk2::WatchlistModel>.
209  
210 =back
211  
212 =head1 PROPERTIES
213  
214 =over 4
215  
216 =item C<symbol> (C<App::Chart::Gtk2::Job>, default undef)
217  
218 The stock or commodity symbol string to be acted on by the menu. This is
219 shown in a non-clickable entry in the menu.
220  
221 =item C<symlist> (C<App::Chart::Gtk2::Symlist> object, default undef)
222  
223 The symlist the C<symbol> is from. This is only used for the "Chart" menu
224 item to give a symlist to the main view so Next/Prev will go from within
225 that symlist.
226  
227 =back
228  
229 =head1 SEE ALSO
230  
231 L<App::Chart::Gtk2::WatchlistDialog>, L<App::Chart::Gtk2::Symlist>
232  
233 =cut