blib/lib/Video/DVDRip/GUI/FormFactory/SubtitlePreviews.pm untested

filter: Statementstmt 0.0% 0 / 89 Branchbran 0.0% 0 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 141 CC 9 SCAR 31.0
CC
9
Cov
0%
object_to_widget
72.0
add_image
6.0
get_type
2.0
CRAP 22.3
1 package Video::DVDRip::GUI::FormFactory::SubtitlePreviews;
2  
3 use strict;
4  
5 use base qw( Gtk2::Ex::FormFactory::Widget );
6  
7 use Video::DVDRip::SrtxFile;
8  
9 use FileHandle;
10  
11 sub get_type { "dvdrip_subtitle_preview" }
12  
13 sub has_additional_attrs { [ "image_cnt", "start_time" ] }
14  
15 sub get_attr_image_cnt { shift->{attr_image_cnt} }
16 sub get_attr_start_time { shift->{attr_start_time} }
17 sub get_gtk_hbox { shift->{gtk_hbox} }
18 sub get_last_valid_start_time { shift->{last_valid_start_time} }
19  
20 sub set_attr_image_cnt { shift->{attr_image_cnt} = $_[1] }
21 sub set_attr_start_time { shift->{attr_start_time} = $_[1] }
22 sub set_gtk_hbox { shift->{gtk_hbox} = $_[1] }
23 sub set_last_valid_start_time { shift->{last_valid_start_time} = $_[1] }
24  
25 sub new {
26 my $class = shift;
27 my %par = @_;
28 my ( $attr_image_cnt, $attr_start_time )
29 = @par{ 'attr_image_cnt', 'attr_start_time' };
30  
31 my $self = $class->SUPER::new(@_);
32  
33 $self->set_attr_image_cnt($attr_image_cnt);
34 $self->set_attr_start_time($attr_start_time);
35  
36 return $self;
37 }
38  
39 sub cleanup {
40 my $self = shift;
41  
42 $self->SUPER::cleanup(@_);
43  
44 $self->set_gtk_hbox(undef);
45  
46 1;
47 }
48  
49 sub build_widget {
50 my $self = shift;
51  
52 $self->set_gtk_widget( Gtk2::VBox->new );
53  
54 1;
55 }
56  
57 sub object_to_widget {
58 my $self = shift;
59  
60 $self->empty_widget;
61  
62 my $sub_dir = $self->get_object_value;
63 my $image_cnt = $self->get_proxy->get_attr( $self->get_attr_image_cnt );
64 my $start_time = $self->get_proxy->get_attr( $self->get_attr_start_time );
65  
66 my $srtx = Video::DVDRip::SrtxFile->new;
67  
68 return if $image_cnt == 0;
69 return if not $srtx->set_filename_from_dir($sub_dir);
70  
71 if ( $start_time =~ /:/ ) {
72 if ( $start_time =~ /^(\d\d):(\d\d):(\d\d)/ ) {
73 $start_time = $3 + $2 * 60 + $1 * 3600;
74 }
75 else {
76 $start_time = $self->get_last_valid_start_time;
77 }
78 }
79 else {
80 $start_time = int(
81 $start_time / $self->get_proxy->get_object->title->frame_rate );
82 }
83  
84 $self->set_last_valid_start_time($start_time);
85  
86 $srtx->open;
87  
88 my $cnt = 0;
89 while ( my $entry = $srtx->read_entry ) {
90 next if $entry->get_time_sec < $start_time;
91 next if not $entry->get_image_file;
92  
93 $self->add_image($entry);
94 ++$cnt;
95  
96 last if $cnt == $image_cnt;
97 }
98  
99 $srtx->close;
100  
101 1;
102 }
103  
104 sub empty_widget {
105 my $self = shift;
106  
107 my $gtk_vbox = $self->get_gtk_widget;
108 my @children = $gtk_vbox->get_children;
109 $gtk_vbox->remove($_) for @children;
110  
111 my $gtk_scrolled_window = Gtk2::ScrolledWindow->new;
112 $gtk_scrolled_window->set(
113 hscrollbar_policy => "automatic",
114 vscrollbar_policy => "automatic",
115 );
116  
117 my $gtk_event_box = Gtk2::EventBox->new;
118 $gtk_event_box->modify_bg( "normal", Gtk2::Gdk::Color->parse("#ffffff") );
119  
120 my $gtk_hbox = Gtk2::HBox->new;
121  
122 $gtk_event_box->add($gtk_hbox);
123 $gtk_scrolled_window->add_with_viewport($gtk_event_box);
124  
125 $self->get_gtk_widget->pack_start( $gtk_scrolled_window, 1, 1, 0 );
126 $self->set_gtk_hbox($gtk_hbox);
127  
128 $gtk_scrolled_window->show_all;
129  
130 1;
131 }
132  
133 sub add_image {
134 my $self = shift;
135 my ($entry) = @_;
136  
137 my $nr = $entry->get_nr;
138 my $time = $entry->get_time;
139  
140 $nr = "[$nr] - " if defined $nr;
141  
142 my $gtk_hbox = $self->get_gtk_hbox;
143 my $gtk_image = Gtk2::Image->new_from_file( $entry->get_image_file );
144 my $gtk_vbox = Gtk2::VBox->new;
145 $gtk_vbox->set( border_width => 5 );
146 $gtk_vbox->pack_start( $gtk_image, 0, 1, 0 );
147 my $gtk_frame = Gtk2::Frame->new("$nr$time");
148 $gtk_frame->set( border_width => 5 );
149 $gtk_frame->set_label_align( 0.5, 0.5 );
150 $gtk_frame->add($gtk_vbox);
151 $gtk_frame->show_all;
152 $gtk_hbox->pack_start( $gtk_frame, 0, 1, 0 );
153  
154 1;
155 }
156  
157 1;