App/Chart/Gtk2/WeblinkMenu.pm untested

filter: Statementstmt 0.0% 0 / 77 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 119 CC 4 SCAR 11.5
CC
4
Cov
0%
_do_notify
6.0
_update_model_contents
6.0
_do_item_create_or_update
6.0
CRAP 3.1
1 # Copyright 2007, 2008, 2009, 2010, 2011, 2013 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::WeblinkMenu;
18 use 5.010;
19 use strict;
20 use warnings;
21 use Glib;
22 use Gtk2;
23 use Locale::TextDomain ('App-Chart');
24  
25 use Glib::Ex::ObjectBits 'set_property_maybe';;
26 use Glib::Ex::ConnectProperties 13; # v.13 for model-rows
27 use Gtk2::Ex::MenuView;
28  
29 use App::Chart::Weblink;
30  
31 # uncomment this to run the ### lines
32 #use Smart::Comments;
33  
34 use Glib::Object::Subclass
35 'Gtk2::Ex::MenuView',
36 signals => { activate => \&_do_activate,
37 item_create_or_update => \&_do_item_create_or_update,
38 notify => \&_do_notify },
39 properties => [Glib::ParamSpec->string
40 ('symbol',
41 __('Symbol'),
42 'The stock or commodity symbol to display weblinks for.',
43 '', # default
44 Glib::G_PARAM_READWRITE)];
45  
46  
47 sub INIT_INSTANCE {
48 my ($self) = @_;
49 ### WeblinkMenu INIT_INSTANCE()
50 $self->set (model => Gtk2::ListStore->new ('Glib::Scalar'));
51 }
52  
53 sub SET_PROPERTY {
54 my ($self, $pspec, $newval) = @_;
55 my $pname = $pspec->get_name;
56 ### WeblinkMenu SET_PROPERTY(): $pname
57 $self->{$pname} = $newval; # per default GET_PROPERTY
58  
59 # $pname eq 'symbol'
60 _update_model_contents ($self);
61 _update_parent_sensitive ($self);
62 }
63  
64 sub _do_notify {
65 my ($self, $pspec) = @_;
66 ### WeblinkMenu _do_notify: $pspec->get_name
67 if ($pspec->get_name eq 'attach_widget') {
68 my $parent = $self->get_attach_widget;
69 ### attached: "$parent"
70 ### model: $self->get('model')
71 $self->{'connp'} = Glib::Ex::ConnectProperties->dynamic
72 ([$self->get('model'), 'model-rows#not-empty'],
73 [$parent, 'sensitive']);
74 }
75 # return shift->signal_chain_from_overridden(@_);
76 }
77  
78 sub _update_model_contents {
79 my ($self) = @_;
80 ### WeblinkMenu _update_model_contents()
81 my $model = $self->get('model');
82 my $symbol = $self->get('symbol');
83 my $i = 0;
84 if (defined $symbol) {
85 foreach my $weblink (App::Chart::Weblink->links_for_symbol ($symbol)) {
86 my $iter = $model->iter_nth_child(undef,$i++) || $model->append;
87 $model->set ($iter, 0 => $weblink);
88 }
89 }
90 while (my $iter = $model->iter_nth_child(undef,$i)) {
91 $model->remove ($iter);
92 }
93 }
94 sub _update_parent_sensitive {
95 my ($self) = @_;
96 return;
97  
98 ### WeblinkMenu _update_parent_sensitive: $self->get_attach_widget
99 my $parent_item = $self->get_attach_widget || return;
100 my $model = $self->get('model');
101 $parent_item->set_sensitive ($model->get_iter_first);
102 }
103  
104 # 'item-create-or-update' class closure handler
105 sub _do_item_create_or_update {
106 my ($self, $item, $model, $path, $iter) = @_;
107 my $weblink = $model->get_value ($iter, 0);
108 if (! $item) {
109 $item = Gtk2::MenuItem->new_with_label ('');
110 $item->show;
111 }
112 $item->get_child->set_text_with_mnemonic ($weblink->name);
113 set_property_maybe
114 ($item,
115 sensitive => $weblink->sensitive($self->get('symbol')),
116 # tooltip-markup new in 2.12
117 tooltip_markup => Glib::Markup::escape_text($weblink->{'desc'} || ''));
118 return $item;
119 }
120  
121 # 'activate' class closure
122 sub _do_activate {
123 my ($self, $item, $model, $path, $iter) = @_;
124 my $weblink = $model->get_value ($iter, 0);
125 my $symbol = $self->get('symbol');
126 $weblink->open ($symbol);
127 }
128  
129 1;
130 __END__