blib/lib/Gtk2/Ex/FormFactory/ExecFlow.pm untested

filter: Statementstmt 0.0% 0 / 103 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 148 CC 6 SCAR 13.4
CC
6
Cov
0%
build_widget
6.0
object_to_widget
6.0
add_job_to_model
6.0
CRAP 3.8
1 package Gtk2::Ex::FormFactory::ExecFlow;
2  
3 use strict;
4  
5 use base qw( Gtk2::Ex::FormFactory::Widget );
6  
7 sub get_type {"execflow"}
8  
9 sub get_add_columns { shift->{add_columns} }
10 sub get_path_by_id_href { shift->{path_by_id_href} }
11  
12 sub set_add_columns { shift->{add_columns} = $_[1] }
13 sub set_path_by_id_href { shift->{path_by_id_href} = $_[1] }
14  
15 sub new {
16 my $class = shift;
17 my %par = @_;
18 my ($add_columns) = $par{'add_columns'};
19  
20 $add_columns ||= [];
21  
22 my $self = $class->SUPER::new(@_);
23  
24 $self->set_add_columns($add_columns);
25  
26 return $self;
27 }
28  
29 sub build_widget {
30 my $self = shift;
31
32 my $model = Gtk2::TreeStore->new(
33 "Glib::String", "Glib::String", "Glib::String",
34 ("Glib::String") x @{$self->get_add_columns}
35 );
36 my $tree_view = Gtk2::TreeView->new_with_model($model);
37  
38 for my $i ( 0..1+@{$self->get_add_columns} ) {
39 my $column = Gtk2::TreeViewColumn->new_with_attributes(
40 "col$i",
41 Gtk2::CellRendererText->new,
42 'text' => $i < 2 ? $i : $i + 1
43 );
44 $tree_view->append_column($column);
45 }
46  
47 $tree_view->set_headers_visible(0);
48 $tree_view->set_rules_hint(1);
49
50 $self->set_gtk_widget($tree_view);
51
52 1;
53 }
54  
55 sub empty_widget {
56 my $self = shift;
57  
58 $self->get_gtk_widget->get_model->clear;
59 $self->set_path_by_id_href({});
60  
61 1;
62 }
63  
64 sub object_to_widget {
65 my $self = shift;
66  
67 my $job = $self->get_object_value;
68 return unless $job;
69  
70 $self->empty_widget;
71 $self->add_job_to_model($job, undef);
72  
73 $self->get_gtk_widget->expand_all;
74  
75 1;
76 }
77  
78 sub add_job_to_model {
79 my $self = shift;
80 my ($job, $parent_iter) = @_;
81
82 my $model = $self->get_gtk_widget->get_model;
83
84 my $iter = $model->append($parent_iter);
85 $model->set($iter, 0 => $job->get_info);
86 $model->set($iter, 1 => $job->get_progress_stats);
87 $model->set($iter, 2 => $job->get_id);
88  
89 my $i = 3;
90 foreach my $add_col ( @{$self->get_add_columns} ) {
91 my $method = "get_$add_col";
92 $model->set($iter, $i => $job->$method());
93 ++$i;
94 }
95  
96 my $path = $model->get_path($iter);
97 $self->get_path_by_id_href->{$job->get_id} = $path;
98  
99 if ( $job->get_type eq 'group' ) {
100 foreach my $child_job ( @{$job->get_jobs} ) {
101 $self->add_job_to_model($child_job, $iter);
102 }
103 }
104  
105 1;
106 }
107  
108 sub update_job {
109 my $self = shift;
110 my ($job) = @_;
111  
112 my $path = $self->get_path_by_id_href->{$job->get_id};
113
114 return if !$path;
115  
116 my $model = $self->get_gtk_widget->get_model;
117 my $iter = $model->get_iter($path);
118
119 $model->set($iter, 0 => $job->get_info);
120 $model->set($iter, 1 => $job->get_progress_stats);
121
122 my $i = 3;
123 foreach my $add_col ( @{$self->get_add_columns} ) {
124 my $method = "get_$add_col";
125 $model->set($iter, $i => $job->$method());
126 ++$i;
127 }
128  
129 1;
130 }
131  
132 sub add_job {
133 my $self = shift;
134 my ($job) = @_;
135  
136 my $group = $job->get_group;
137 my $group_path = $self->get_path_by_id_href->{$group->get_id};
138  
139 return $self->add_job_to_model($job) if !$group_path;
140
141 my $model = $self->get_gtk_widget->get_model;
142 my $iter = $model->get_iter($group_path);
143
144 $self->add_job_to_model($job, $iter);
145
146 $self->get_gtk_widget->expand_row($group_path, 0);
147 $self->get_gtk_widget->expand_row(
148 $self->get_path_by_id_href->{$job->get_id},
149 1
150 );
151  
152 1;
153 }
154  
155 sub remove_job {
156 my $self = shift;
157 my ($job) = @_;
158  
159 $self->remove_job_with_id($job->get_id);
160  
161 1;
162 }
163  
164 sub remove_job_with_id {
165 my $self = shift;
166 my ($job_id) = @_;
167  
168 my $path = $self->get_path_by_id_href->{$job_id};
169 my $model = $self->get_gtk_widget->get_model;
170  
171 $model->remove($model->get_iter($path));
172  
173 $self->rebuild_job_path_index;
174  
175 1;
176 }
177  
178 sub rebuild_job_path_index {
179 my $self = shift;
180
181 my %path_by_job_id;
182 my $model = $self->get_gtk_widget->get_model;
183
184 $model->foreach(sub{
185 #-- Storing the Gtk2::TreePath doesn't work here, weird
186 #-- stuff happens. So we store the string and turn it
187 #-- into a Gtk2::TreePath after the foreach iteration.
188 $path_by_job_id{$model->get($_[2],2)} = $_[1]->to_string;
189 return 0;
190 });
191  
192 #-- Turn path strings into Gtk2::TreePath objects
193 foreach my $job_id ( sort {$a <=> $b} keys %path_by_job_id ) {
194 $path_by_job_id{$job_id} =
195 Gtk2::TreePath->new_from_string($path_by_job_id{$job_id});
196 }
197  
198 $self->set_path_by_id_href(\%path_by_job_id);
199
200 1;
201 }
202  
203 1;
204  
205 __END__