1 | #!/opt/bin/perl |
1 | #!/opt/bin/perl |
2 | |
2 | |
3 | use Gtk2; |
3 | use Gtk2 -init; |
4 | use Gtk2::Gdk::Keysyms; |
4 | use Gtk2::Gdk::Keysyms; |
5 | |
5 | |
6 | use Gtk2::CV::ImageWindow; |
6 | use Gtk2::CV::ImageWindow; |
|
|
7 | use Gtk2::CV::Schnauzer; |
7 | |
8 | |
8 | $VERSION = 0.1; |
9 | use Gtk2::CV; |
9 | |
10 | |
|
|
11 | Gtk2::Rc->parse (Gtk2::CV::find_rcfile "gtkrc"); |
|
|
12 | |
|
|
13 | use File::Spec; |
|
|
14 | |
|
|
15 | my $mainwin; |
10 | my $viewer; |
16 | my $viewer; |
|
|
17 | my $schnauzer; |
|
|
18 | my $info; |
|
|
19 | my $help; |
11 | |
20 | |
12 | package schnauzer; |
21 | sub new_schnauzer { |
|
|
22 | my $s = new Gtk2::CV::Schnauzer; |
13 | |
23 | |
14 | use Glib::Object::Subclass Gtk2::Frame; |
24 | $s->signal_connect_after (key_press_event => \&std_keys); |
15 | |
25 | $s->signal_connect (activate => sub { |
16 | sub INIT_INSTANCE { |
26 | my $label = sprintf "%s (%d)", |
17 | my ($self) = @_; |
27 | (File::Spec->splitpath ($_[1]))[2], |
18 | |
28 | -s $_[1]; |
19 | $self->add ( |
29 | $info->set_label ($label); |
20 | $self->{list} = new Gtk2::SimpleList "Name" => "text" |
|
|
21 | ); |
|
|
22 | |
|
|
23 | $self->{list}->signal_connect (cursor_changed => sub { |
|
|
24 | my $row = scalar +($_[0]->get_selection->get_selected_rows)[0]->get_indices; |
|
|
25 | $viewer->load_image ($self->{files}[$row]); |
30 | $viewer->load_image ($_[1]); |
26 | }); |
31 | }); |
27 | } |
|
|
28 | |
32 | |
29 | sub set_files { |
33 | $s; |
30 | my ($self, $files) = @_; |
34 | } |
31 | |
35 | |
32 | $self->{files} = $files; |
36 | sub std_keys { |
|
|
37 | my $key = $_[1]->keyval; |
|
|
38 | my $state = $_[1]->state; |
33 | |
39 | |
34 | @{$self->{list}{data}} = (); |
40 | my $ctrl = $state * "control-mask"; |
35 | |
41 | |
36 | my ($dir, $file); |
42 | if ($key == $Gtk2::Gdk::Keysyms{q}) { |
|
|
43 | main_quit Gtk2; |
|
|
44 | } elsif ($ctrl && $key == $Gtk2::Gdk::Keysyms{v}) { |
|
|
45 | my $w = new Gtk2::Window; |
|
|
46 | $w->add (my $s = new_schnauzer); |
|
|
47 | $s->set_dir (File::Spec->curdir); |
|
|
48 | $w->show_all; |
|
|
49 | } elsif ($ctrl && $key == $Gtk2::Gdk::Keysyms{h}) { |
|
|
50 | unless ($help) { |
|
|
51 | require Gtk2::PodViewer; |
37 | |
52 | |
38 | for (@$files) { |
53 | $help = new Gtk2::Window; |
39 | if (/^(.*)\/([^\/]*)$/s) { |
54 | $help->set_title ("CV Help"); |
40 | ($dir, $file) = ($1, $2); |
55 | $help->set_default_size (500, 300); |
41 | } else { |
56 | $help->signal_connect (delete_event => sub { $help->hide; 1 }); |
42 | ($dir, $file) = (".", $_); |
57 | |
|
|
58 | $help->add (my $sw = new Gtk2::ScrolledWindow); |
|
|
59 | $sw->add (my $h = new Gtk2::PodViewer); |
|
|
60 | |
|
|
61 | #binmode DATA, ":utf8"; |
|
|
62 | $h->load_string (do { local $/; <DATA> }); |
43 | } |
63 | } |
44 | |
64 | |
45 | if (0) { |
65 | $help->show_all; |
46 | if ($file eq "." or $file eq "..") { |
|
|
47 | } elsif (-f "$dir/.xvpics/$file") { |
|
|
48 | print new_from_file Gtk2::Gdk::Pixbuf "$dir/.xvpics/$file"; |
|
|
49 | } else { |
66 | } else { |
50 | } |
67 | #$mainwin->show_all; |
51 | } |
68 | $schnauzer->handle_key ($key, $state); |
52 | |
|
|
53 | push @{$self->{list}{data}}, [$file]; |
|
|
54 | } |
69 | } |
55 | } |
|
|
56 | |
70 | |
57 | sub set_dir { |
|
|
58 | my ($self, $dir) = @_; |
|
|
59 | |
|
|
60 | my @files; |
|
|
61 | |
|
|
62 | opendir my $fh, $dir |
|
|
63 | or die "$dir: $!"; |
|
|
64 | |
|
|
65 | $self->set_files ([map "$dir/$_", readdir $fh]); |
|
|
66 | } |
|
|
67 | |
|
|
68 | package cluster; |
|
|
69 | |
|
|
70 | use Glib::Object::Subclass Gtk2::Window; |
|
|
71 | |
|
|
72 | use Gtk2::SimpleList; |
|
|
73 | |
|
|
74 | sub INIT_INSTANCE { |
|
|
75 | my ($self) = @_; |
|
|
76 | |
|
|
77 | $self->set_default_size (600, 400); |
|
|
78 | |
|
|
79 | my $vbox = new Gtk2::VBox; |
|
|
80 | $self->add ($vbox); |
|
|
81 | |
|
|
82 | my $box = new Gtk2::HBox; |
|
|
83 | $vbox->add ($box); |
|
|
84 | $box->add (my $sw = new Gtk2::ScrolledWindow); |
|
|
85 | $sw->add ( |
|
|
86 | $self->{list} = new Gtk2::SimpleList |
|
|
87 | "#" => "int", |
|
|
88 | "Name" => "text", |
|
|
89 | ); |
71 | 1; |
|
|
72 | } |
90 | |
73 | |
91 | $self->{schnauzer} = new schnauzer; |
74 | { |
92 | $box->add (my $sw = new Gtk2::ScrolledWindow); |
75 | $viewer = new Gtk2::CV::ImageWindow; |
93 | $sw->add_with_viewport ($self->{schnauzer} = new schnauzer); |
|
|
94 | |
76 | |
95 | $self->{list}->get_column(0)->set_sort_column_id(0); |
77 | $viewer->set_title ("CV: Image"); |
96 | $self->{list}->get_column(1)->set_sort_column_id(1); |
|
|
97 | |
78 | |
98 | $self->{list}->signal_connect (cursor_changed => sub { |
79 | $viewer->signal_connect (key_press_event => \&std_keys); |
99 | my $row = scalar +($_[0]->get_selection->get_selected_rows)[0]->get_indices; |
80 | $viewer->signal_connect (delete_event => sub { main_quit Gtk2 }); |
100 | |
81 | |
101 | my $k = $_[0]{data}[$row][1]; |
82 | $viewer->signal_connect (button3_press_event => sub { |
102 | $k = $self->{cluster}{$k}; |
83 | $mainwin->visible |
103 | |
84 | ? $mainwin->hide |
104 | $self->{schnauzer}->set_files ( |
85 | : $mainwin->show_all; |
105 | [sort map "$self->{path}/$_", @{$k->[1]}], |
|
|
106 | ); |
|
|
107 | |
|
|
108 | 1; |
86 | 1; |
109 | |
|
|
110 | }); |
87 | }); |
111 | } |
|
|
112 | |
88 | |
113 | sub analyse { |
89 | $schnauzer = new_schnauzer; |
114 | my ($self, $path) = @_; |
|
|
115 | |
90 | |
116 | opendir my $dir, $path |
91 | $mainwin = new Gtk2::Window; |
117 | or die "$path: $!"; |
92 | $mainwin->set_title ("CV"); |
|
|
93 | $mainwin->add (my $vbox = new Gtk2::VBox); |
|
|
94 | $mainwin->signal_connect (delete_event => sub { $mainwin->hide; 1; }); |
118 | |
95 | |
119 | $self->{path} = $path; |
96 | $vbox->add ($schnauzer); |
120 | |
97 | $vbox->pack_end (my $frame = new Gtk2::Frame, 0, 0, 0); |
121 | my @files = map { |
98 | $frame->add (my $hbox = new Gtk2::HBox 0, 0); |
122 | my $str = $_; |
99 | $hbox->pack_start ((new Gtk2::Label "Info"), 0, 0, 0); |
123 | $str =~ s/[\-_ ]+/ /g; |
100 | $hbox->pack_start (($info = new Gtk2::Label), 1, 1, 0); |
124 | $str =~ s/\.[^\.]+//g; |
|
|
125 | [$str, $_] |
|
|
126 | } |
|
|
127 | grep !/\.(sfv|crc|par|par2)$/, readdir $dir; |
|
|
128 | |
|
|
129 | my %cluster; |
|
|
130 | |
|
|
131 | my @regexps = ( |
|
|
132 | [qr<\d+$>, qr<^(.+?)\ *(\d+)$>], |
|
|
133 | [qr<\ \d+$>, qr<^(.+?)(\d+)\ +(\d+)$>], |
|
|
134 | ); |
|
|
135 | |
|
|
136 | for my $info (@regexps) { |
|
|
137 | my $re = $info->[0]; |
|
|
138 | for (@files) { |
|
|
139 | if ($_->[0] =~ $re && $_->[0] =~ $info->[1]) { |
|
|
140 | push @{ ($cluster{$1} ||= [ $info, []])->[1] }, $_->[1]; |
|
|
141 | } |
|
|
142 | } |
|
|
143 | } |
|
|
144 | |
|
|
145 | $self->{cluster} = \%cluster; |
|
|
146 | |
|
|
147 | while (my ($k, $v) = each %cluster) { |
|
|
148 | my $n = scalar @{$v->[1]}; |
|
|
149 | push @{$self->{list}{data}}, [$n, $k] if $n > 2; |
|
|
150 | } |
|
|
151 | } |
101 | } |
152 | |
102 | |
153 | package main; |
103 | if (@ARGV) { |
154 | |
104 | $schnauzer->set_paths (\@ARGV); |
155 | init Gtk2; |
105 | $schnauzer->handle_key ($Gtk2::Gdk::Keysyms{space}, []); |
156 | |
106 | } else { |
157 | $viewer = new Gtk2::CV::ImageWindow; |
107 | $schnauzer->set_dir (File::Spec->curdir); |
|
|
108 | $mainwin->show_all; |
|
|
109 | } |
158 | |
110 | |
159 | $viewer->show_all; |
111 | $viewer->show_all; |
160 | |
112 | |
161 | my $cluster = new cluster; |
|
|
162 | |
|
|
163 | 0 and do { |
|
|
164 | my $w = new Gtk2::Window; |
|
|
165 | $w->add (my $s = new schnauzer); |
|
|
166 | $s->set_dir ("/root/pix"); |
|
|
167 | $w->show_all; |
|
|
168 | }; |
|
|
169 | |
|
|
170 | $cluster->analyse ("/fs/samsung/store-anime-done"); |
|
|
171 | $cluster->show_all; |
|
|
172 | |
|
|
173 | #$viewer->set (path => "/fs/samsung/store-anime-done/yuumi_kazuaki_-_love_to_hajieki_to_sayonara_to_-_150.jpg"); |
|
|
174 | #$viewer->set (path => "/root/pix/kvvz.jpg"); |
|
|
175 | |
|
|
176 | main Gtk2; |
113 | main Gtk2; |
177 | |
114 | |
|
|
115 | __DATA__ |
178 | |
116 | |
|
|
117 | =head1 NAME |
|
|
118 | |
|
|
119 | cv - a fast gtk+ image viewer modeled after xv |
|
|
120 | |
|
|
121 | =head1 SYNOPSIS |
|
|
122 | |
|
|
123 | cv [file...] |
|
|
124 | |
|
|
125 | =head1 DESCRIPTION |
|
|
126 | |
|
|
127 | None yet. |
|
|
128 | |
|
|
129 | =head2 THE IMAGE WINDOW |
|
|
130 | |
|
|
131 | You can use the following keys in the image window: |
|
|
132 | |
|
|
133 | q quit the program |
|
|
134 | < half the image size |
|
|
135 | > double the image size |
|
|
136 | , shrink the image by 10% |
|
|
137 | . enlarge the image by 10% |
|
|
138 | n reset to normal size |
|
|
139 | m maximize to screensize |
|
|
140 | M maxime to screensize, respecting image aspect |
|
|
141 | u uncrop |
|
|
142 | r set scaling mode to 'nearest' (fastest) |
|
|
143 | s set scaling mode to 'bilinear' (default) |
|
|
144 | S set scaling mode to 'hyper' (slowest) |
|
|
145 | t rotate clockwise 90° |
|
|
146 | T rotate counterclockwise° |
|
|
147 | ctrl-v open a new visual schnauzer window for the current dir |
|
|
148 | |
|
|
149 | The following keys are redirected to the default visual schnauzer window: |
|
|
150 | |
|
|
151 | space next image |
|
|
152 | backspace last image |
|
|
153 | |
|
|
154 | =head2 THE VISUAL SCHNAUZER |
|
|
155 | |
|
|
156 | You can use the following keys in the schnauzer window: |
|
|
157 | |
|
|
158 | space move to and display next image |
|
|
159 | backspace move to and display previous image |
|
|
160 | return display selected picture |
|
|
161 | |
|
|
162 | cursor keys move selection |
|
|
163 | page-up move one page up |
|
|
164 | page-down move one page down |
|
|
165 | home move to first file |
|
|
166 | end move to last file |
|
|
167 | |
|
|
168 | ctrl-d delete selected files WITHOUT ASKING AGAIN |
|
|
169 | ctrl-g generate icons for the selected files |
|
|
170 | ctrl-u update selected (or all) icons if neccessary |
|
|
171 | |
|
|
172 | =head1 SECURITY CONSIDERATIONS |
|
|
173 | |
|
|
174 | CV uses Pixbuf to load images. Pixbuf is not considered safe for this |
|
|
175 | purpose, though (from the gtk-2.2 release notes): |
|
|
176 | |
|
|
177 | "While efforts have been made to make gdk-pixbuf robust against invalid |
|
|
178 | images, using gdk-pixbuf to load untrusted data is not recommended, due to |
|
|
179 | the likelyhood that there are additional problems where an invalid image |
|
|
180 | could cause gdk-pixbuf to crash or worse." |
|
|
181 | |
|
|
182 | =head1 BUGS/TODO |
|
|
183 | |
|
|
184 | Pixbuf doesn't honor G_BROKEN_FILENAMES, so accessing files with names |
|
|
185 | incompatible with utf-8 fails. |
|
|
186 | |
|
|
187 | rotate on disk |
|
|
188 | print |
|
|
189 | lots of ui issues |
|
|
190 | save(?) |
|
|
191 | preferences |
|
|
192 | ctrl-u in schnauzer |
|
|
193 | shift-cursor in schnauzer |
|
|
194 | |
|
|
195 | =head1 AUTHOR |
|
|
196 | |
|
|
197 | Marc Lehmann <cv@plan9.de>. |
|
|
198 | |
|
|
199 | =cut |
|
|
200 | |