sample_images_thumbnail.pl
author Peter Gervai <grin@grin.hu>
Thu, 16 Oct 2008 15:29:16 +0200
changeset 4 e1efc7395f84
permissions -rwxr-xr-x
Adding sample: image thumbnail viewer
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     1
#!/usr/bin/perl -w
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     2
#
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     3
# image thumbnails and liststore
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     4
#
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     5
use strict;
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     6
use Gtk2 -init;
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     7
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     8
my $window = Gtk2::Window->new;
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
     9
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    10
$window->set_default_size( 650, 650 );
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    11
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    12
# the ListStore model that actually holds the 
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    13
#  data ..a picture and a filename
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    14
my $model = Gtk2::ListStore->new ('Gtk2::Gdk::Pixbuf', 'Glib::String');
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    15
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    16
my @files = <*.jpg *.png *.gif>;
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    17
#print "@files\n";
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    18
foreach my $file (@files){
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    19
    print "Processing $file...\n";
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    20
    my $pixbuf = Gtk2::Gdk::Pixbuf->new_from_file_at_scale($file,100,100,1);
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    21
    my $iter = $model->append;
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    22
    $model->set($iter,
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    23
                0, $pixbuf,
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    24
                1, $file,
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    25
               );
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    26
}
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    27
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    28
# now make the actual treeview renderer
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    29
my $treeview = Gtk2::TreeView->new ($model);
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    30
$treeview->set_headers_visible (0);
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    31
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    32
$treeview->insert_column_with_attributes(
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    33
                                         -1, # append
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    34
                                         "", # this won't be visible because we turned off headings
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    35
                                         Gtk2::CellRendererPixbuf->new,  # the renderer
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    36
                                         pixbuf => 0);  # get the pixbuf property from model col 0
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    37
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    38
$treeview->insert_column_with_attributes(
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    39
                                         -1, # append
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    40
                                         "", # this won't be visible because we turned off headings
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    41
                                         Gtk2::CellRendererText->new(),  # the renderer
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    42
                                         text => 1);  # get the property from model col 1
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    43
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    44
$treeview->get_selection->signal_connect (changed => sub {
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    45
                                             
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    46
                                             my $treeselection = $treeview->get_selection;
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    47
                                             #print "$treeselection\n";
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    48
                                             
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    49
                                             $treeselection->selected_foreach (sub{ 
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    50
#this sub wil receive the following:
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    51
my ($model,$path,$iter) =@_;
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    52
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    53
#we want data at the model's column 1  
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    54
#where the iter is pointing
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    55
my $value = $model->get($iter,1);
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    56
#print $value."\n";
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    57
load_image( "./$value" );
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    58
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    59
});    
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    60
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    61
                                         });
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    62
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    63
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    64
my $hbox = Gtk2::HBox->new (0, 4);
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    65
my $scwin = Gtk2::ScrolledWindow->new();
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    66
$scwin->set_size_request( 250, 250 );
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    67
$scwin->add_with_viewport($treeview);
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    68
$scwin->set_policy('always', 'always');
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    69
$hbox->pack_start($scwin,0,1,1);
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    70
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    71
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    72
my $vp = Gtk2::Viewport->new( undef, undef );
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    73
my $scwin1 = Gtk2::ScrolledWindow->new( undef, undef );
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    74
$scwin1->set_policy( 'automatic', 'automatic' );
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    75
$scwin1->add( $vp );
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    76
$hbox->pack_start($scwin1,1,1,1);
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    77
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    78
my $image = Gtk2::Image->new();
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    79
$vp->add($image);
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    80
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    81
$window->add ($hbox);
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    82
$window->show_all;
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    83
$window->signal_connect (destroy => sub {Gtk2->main_quit});
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    84
Gtk2->main;
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    85
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    86
###################################
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    87
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    88
sub load_image {
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    89
    my $file = shift  or warn "$!\n";
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    90
    $image->set_from_file ($file);
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    91
    $window->show_all();
e1efc7395f84 Adding sample: image thumbnail viewer
Peter Gervai <grin@grin.hu>
parents:
diff changeset
    92
}