Re: lasso tool



On Mon, 11 Aug 2008 11:57:00 -0700
"Matt Kowalczyk" <matt kowalczyk gmail com> wrote:


>with this one. Is GnomeCanvas something I should look at?
>
>The solution to only clear the pixels representing the selection clearly
>improves performance.
>
>I'm just a newbie UI developer and wanted to solicit some suggestions as
>this is my first day working with GTK and I would hate to overlook a
>possibly simple solution to this problem.
>
>Thanks!

I would go with a canvas type widget, because you can make a single
rectangle and just redefine it's points as you drag the mouse.

I'm sorry I don't have an example for Gnome2::Canvas or the Goo::Canvas,
which would be preferrable.

I did this in Perl/Tk at
http://perlmonks.org?node_id=692998

It scans your directory, makes a thumbnail selection bar, then clicking on
a thumbnail, loads the image on a Tk::Canvas. You can then drag-right-click
on the image, and it's selected area is automatically copied to another canvas,
where it is draggable for you to position. It is sort of a custom montage maker.


The whole idea should work in Gnome2::Canvas or Goo::Canvas(my favorite now.).

Here is a quick Perl script to do the selection, but copying off the sub-pixbuf is left out.
One thing I noticed, is you need to give a transparent fill color, to prevent dragging
artifacts being left as unwanted random lines. That seems like a bug.
This shouldn't be too hard to convert to c.

#!/usr/bin/perl
use warnings;
use strict;
use Gtk2 '-init';
use Gnome2::Canvas;
use Glib qw/TRUE FALSE/;

my $file = shift || die "Need an image file $!\n";

my ( $dragging, $last_x, $last_y ); # item_move globals
my $rect;

my $window = Gtk2::Window->new();
$window->set_size_request(450,350);
$window->signal_connect('destroy'=>\&delete_event );

my $vbox= Gtk2::VBox->new(FALSE, 1 );
$window->add($vbox);

my $scroller = Gtk2::ScrolledWindow->new();

my $canvas = Gnome2::Canvas->new_aa();
$canvas->set_center_scroll_region (FALSE);

$scroller->add($canvas);

my $hbox= Gtk2::HBox->new(TRUE, 1 );
$vbox->pack_start($hbox,TRUE,TRUE,0);
$hbox->set_border_width(2);

$hbox->add($scroller);

$vbox->pack_start(Gtk2::HSeparator->new, FALSE, FALSE, 0);

my $hbox1= Gtk2::HBox->new(FALSE, 1 );
$vbox->pack_end($hbox1,FALSE,FALSE,0);
$hbox1->set_border_width(2);

my $button = Gtk2::Button->new_from_stock('gtk-quit');
$hbox1->pack_end( $button, FALSE, FALSE, 0 );
$button->signal_connect( clicked => \&delete_event );

my $root   = $canvas->root;
my $im = Gtk2::Gdk::Pixbuf->new_from_file( $file );
my $image = Gnome2::Canvas::Item->new ($root,
			       'Gnome2::Canvas::Pixbuf',
  			       pixbuf => $im,
			       x      => 10,
           		       y      => 10,
			       width  => $im->get_width,
			       height => $im->get_height,
			       anchor => 'nw',
		       );

$canvas->set_scroll_region(0,0,$im->get_width,$im->get_height);
$image->lower_to_bottom();

$canvas->signal_connect( "event", \&drag_rect );

$window->show_all();

Gtk2->main();
################################################
sub delete_event {
    Gtk2->main_quit;
    return FALSE;
}  
#############################################
sub drag_rect {
    my ( $canvas, $event ) = @_;
#       print "$item $event->type\n";

    if ( $event->type eq "button-press" ) {
        
        #$canvas->window->set_cursor( Gtk2::Gdk::Cursor->new('fleur') );
        my $startx   = $event->x;
        my $starty   = $event->y;
        $dragging = 1;
        print "start\n";    
       $rect = Gnome2::Canvas::Item->new ($root,
				   'Gnome2::Canvas::Rect',
				   x1 => $startx,
				   y1 => $starty,
				   x2 => $startx,
				   y2 => $starty,
				   fill_color_rgba => 0x00000000,
				   outline_color => 'black',
				   width_units => 1.0);

    }
    elsif ( $event->type eq "motion-notify" ) {
        if ($dragging) {
            my $new_x = $event->x;
            my $new_y = $event->y;
          print "dragging\n";
            $rect->set(x2 => $new_x, y2 => $new_y);        
        }
    }
    elsif ( $event->type eq "button-release" ) {
         $dragging = 0;
         print "release\n";
         #$rect->destroy;
        # $canvas->window->set_cursor (undef);

    }

return 0;
}

__END__


zentara












-- 
I'm not really a human, but I play one on earth.
http://zentara.net/Remember_How_Lucky_You_Are.html 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]