File Coverage

blib/lib/App/Chart/Gtk2/JobStopMenu.pm
Criterion Covered Total %
statement 9 11 81.8
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 13 15 86.6


line stmt bran cond sub pod time code
1             # Copyright 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::JobStopMenu;
18 1     1   379 use 5.008;
  1         3  
19 1     1   4 use strict;
  1         2  
  1         15  
20 1     1   4 use warnings;
  1         2  
  1         35  
21 1     1   142 use Gtk2;
  0            
  0            
22             # use Locale::TextDomain ('App-Chart');
23              
24             use App::Chart::Glib::Ex::MoreUtils;
25             use Glib::Ex::SignalIds;
26             use App::Chart;
27             use App::Chart::Gtk2::Job;
28              
29              
30             use Glib::Object::Subclass
31             'Gtk2::Menu',
32             properties => [ Glib::ParamSpec->object
33             ('job',
34             'job',
35             'App::Chart::Gtk2::Job object to act on, or undef.',
36             'App::Chart::Gtk2::Job',
37             Glib::G_PARAM_READWRITE),
38             ];
39              
40             sub INIT_INSTANCE {
41             my ($self) = @_;
42              
43             # avoid selecting Stop too easily
44             $self->append (Gtk2::SeparatorMenuItem->new);
45             {
46             my $item = $self->{'stop'}
47             = Gtk2::ImageMenuItem->new_from_stock ('gtk-stop');
48             $self->append ($item);
49             $item->signal_connect (activate => \&_do_stop);
50             $item->show;
51             }
52             {
53             my $item = $self->{'delete'}
54             = Gtk2::ImageMenuItem->new_from_stock ('gtk-delete');
55             $self->append ($item);
56             $item->signal_connect (activate => \&_do_delete);
57             $item->show;
58             }
59             }
60              
61             sub SET_PROPERTY {
62             my ($self, $pspec, $newval) = @_;
63             my $pname = $pspec->get_name;
64             $self->{$pname} = $newval; # per default GET_PROPERTY
65              
66             if ($pname eq 'job') {
67             my $job = $newval;
68             $self->{'job_ids'} = $job && Glib::Ex::SignalIds->new
69             ($job,
70             $job->signal_connect ('notify::status' => \&_do_job_status_change,
71             App::Chart::Glib::Ex::MoreUtils::ref_weak($self)));
72             _update_sensitive ($self);
73             }
74             }
75              
76             # "Stop" item activate
77             sub _do_stop {
78             my ($item) = @_;
79             my $self = $item->get_toplevel;
80             if (my $job = $self->{'job'}) {
81             $job->stop;
82             }
83             }
84              
85             # "Delete" item activate
86             sub _do_delete {
87             my ($item) = @_;
88             my $self = $item->get_toplevel;
89             if (my $job = $self->{'job'}) {
90             $job->delete;
91             }
92             }
93              
94             # 'notify::status' on current job (if any)
95             sub _do_job_status_change {
96             my ($job, $pspec, $ref_weak_self) = @_;
97             my $self = $$ref_weak_self || return;
98             _update_sensitive ($self);
99             }
100              
101             sub _update_sensitive {
102             my ($self) = @_;
103             my $job = $self->{'job'};
104              
105             my $stop = $self->{'stop'};
106             $stop->set_sensitive ($job && $job->is_stoppable);
107              
108             my $delete = $self->{'delete'};
109             $delete->set_sensitive ($job && $job->is_done);
110             }
111              
112             sub popup_from_treeview {
113             my ($self, $event, $treeview) = @_;
114              
115             my ($path) = $treeview->get_path_at_pos ($event->x, $event->y);
116             if (! $path) { return; }
117              
118             my $model = $treeview->get_model; # App::Chart::Gtk2::JobQueue
119             my $iter = $model->get_iter ($path);
120             my $job = $model->get_value ($iter, 0);
121             $self->set (job => $job);
122             $self->set_screen ($treeview->get_screen);
123             $self->popup (undef, undef, undef, undef, $event->button, $event->time);
124             }
125              
126             1;
127             __END__
128              
129             =for stopwords undef
130              
131             =head1 NAME
132              
133             App::Chart::Gtk2::JobStopMenu -- menu to stop or delete a Job
134              
135             =for test_synopsis my ($event, $treeview)
136              
137             =head1 SYNOPSIS
138              
139             use App::Chart::Gtk2::JobStopMenu;
140             my $menu = App::Chart::Gtk2::JobStopMenu->new;
141              
142             $menu->popup_from_treeview ($event, $treeview);
143              
144             =head1 WIDGET HIERARCHY
145              
146             C<App::Chart::Gtk2::JobStopMenu> is a subclass of C<Gtk::Menu>,
147              
148             Gtk2::Widget
149             Gtk2::Container
150             Gtk2::MenuShell
151             Gtk2::Menu
152             App::Chart::Gtk2::JobStopMenu
153              
154             =head1 DESCRIPTION
155              
156             A C<App::Chart::Gtk2::JobStopMenu> displays a little menu to stop or delete a given
157             C<App::Chart::Gtk2::Job>.
158              
159             +--------+
160             +--------+
161             | Stop |
162             +--------+
163             | Delete |
164             +--------+
165              
166             This is used by C<App::Chart::Gtk2::DownloadDialog> and has been split out to
167             reduce the amount of code there.
168              
169             =head1 FUNCTIONS
170              
171             =over 4
172              
173             =item C<< App::Chart::Gtk2::JobStopMenu->new (key=>value,...) >>
174              
175             Create and return a new C<App::Chart::Gtk2::JobStopMenu> object. Optional
176             key/value pairs set initial properties as per C<< Glib::Object->new >>.
177              
178             =back
179              
180             =head1 PROPERTIES
181              
182             =over 4
183              
184             =item C<job> (C<App::Chart::Gtk2::Job>, default undef)
185              
186             The job to be acted on by the menu. Normally this is set at the time the
187             menu is popped up. Changing it while popped up works, but could confuse the
188             user.
189              
190             =back
191              
192             =head1 SEE ALSO
193              
194             L<App::Chart::Gtk2::Job>, L<Gtk2::Menu>, L<App::Chart::Gtk2::DownloadDialog>
195              
196             =cut