App/Chart/Gtk2/SymlistRadioMenu.pm untested

filter: Statementstmt 0.0% 0 / 96 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 142 CC 9 SCAR 18.4
CC
9
Cov
0%
set_symlist
20.0
INIT_INSTANCE
6.0
SET_PROPERTY
6.0
CRAP 6.3
1 # Copyright 2007, 2008, 2009, 2010, 2011, 2013, 2014 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::SymlistRadioMenu;
18 use 5.010;
19 use strict;
20 use warnings;
21 use Carp;
22 use Gtk2;
23  
24 use App::Chart::Gtk2::Symlist;
25 use App::Chart::Gtk2::SymlistListModel (qw(COL_KEY COL_NAME));
26  
27 # uncomment this to run the ### lines
28 #use Smart::Comments;
29  
30 use Gtk2::Ex::MenuView;
31 use Glib::Object::Subclass
32 'Gtk2::Ex::MenuView',
33 signals => { activate => \&_do_activate,
34 item_create_or_update => \&_do_item_create_or_update,
35 },
36 properties => [ Glib::ParamSpec->object
37 ('symlist',
38 'symlist',
39 'App::Chart::Gtk2::Symlist object selected, or undef.',
40 'App::Chart::Gtk2::Symlist',
41 Glib::G_PARAM_READWRITE),
42 ];
43  
44 sub INIT_INSTANCE {
45 my ($self) = @_;
46  
47 my $model = App::Chart::Gtk2::SymlistListModel->instance;
48  
49 # hack for extra constructed lists ...
50 #
51 my @symlists = grep {$_->isa('App::Chart::Gtk2::Symlist::Join')}
52 values %App::Chart::Gtk2::Symlist::instances;
53 if (@symlists) {
54 @symlists = sort {$a->key cmp $b->key} @symlists;
55 require Gtk2::Ex::TreeModelBits;
56 my $store = Gtk2::ListStore->new
57 (Gtk2::Ex::TreeModelBits::all_column_types ($model));
58 my $pos = 0;
59 foreach my $symlist (@symlists) {
60 ### extra: COL_KEY.' '.COL_NAME.' '.$symlist->key
61 $store->insert_with_values ($pos++,
62 COL_KEY, $symlist->key,
63 COL_NAME, $symlist->name);
64 }
65 require Gtk2::Ex::ListModelConcat;
66 $model = Gtk2::Ex::ListModelConcat->new (models => [ $store, $model ]);
67 }
68  
69 $self->set (model => $model);
70 }
71  
72 sub SET_PROPERTY {
73 my ($self, $pspec, $newval) = @_;
74 my $pname = $pspec->get_name;
75 if ($pspec->get_name eq 'symlist') {
76 $self->set_symlist ($newval);
77 } else {
78 $self->{$pname} = $newval; # per default GET_PROPERTY
79 }
80 }
81  
82 sub _do_item_create_or_update {
83 my ($self, $item, $model, $path, $iter) = @_;
84 ### SymlistRadioMenu _do_item_create_or_update: $item
85 if (! $item) {
86 my $group = ($self->get_children)[0];
87 $item = Gtk2::RadioMenuItem->new_with_label($group,'');
88 }
89 ### label: $item->get_child
90 $item->get_child->set_text ($model->get_value ($iter, COL_NAME));
91 return $item;
92 }
93  
94 # 'activate' signal handler on items
95 sub _do_activate {
96 my ($self, $item, $model, $path, $iter) = @_;
97 if ($item->get_active) {
98 my $key = $model->get_value ($iter, COL_KEY);
99 $self->{'symlist'} = App::Chart::Gtk2::Symlist->new_from_key ($key);
100 $self->notify ('symlist');
101 }
102 }
103  
104 sub set_symlist {
105 my ($self, $symlist) = @_;
106 ### SymlistRadioMenu set_symlist: $self->get_name." to ".($symlist||'[none]')
107 if (($self->{'symlist'}||0) == ($symlist||0)) { return; }
108  
109 my $key = $symlist->key;
110 my $model = $self->get('model');
111 my $pos = liststore_find_pos
112 ($model, sub { my ($model, $path, $iter) = @_;
113 return ($model->get_value($iter, COL_KEY) eq $key); });
114 if (! defined $pos) {
115 croak "SymlistRadioMenu: no such list for set_symlist: $key";
116 }
117  
118 $self->{'symlist'} = undef;
119 require Glib::Ex::FreezeNotify;
120 { my $freezer = Glib::Ex::FreezeNotify->new ($self);
121 if (my $item = $self->item_at_indices ($pos)) {
122 $item->set_active (1);
123 }
124 $self->notify ('symlist');
125 }
126 }
127  
128 sub liststore_find_pos {
129 my ($store, $subr) = @_;
130 my $pos;
131 $store->foreach (sub {
132 my ($store, $path, $iter) = @_;
133 if ($subr->($store, $path, $iter)) {
134 ($pos) = $path->get_indices;
135 return 1; # stop;
136 }
137 return 0; # continue;
138 });
139 return $pos;
140 }
141  
142 1;
143 __END__