App-SourcePlot

 view release on metacpan or  search on metacpan

lib/App/SourcePlot/Plotter/Tk.pm  view on Meta::CPAN

=back

=head2 Setup tools

=over 4

=item width

Returns the width of a canvas.

    $w = $plotter->width();

=cut

sub width {
    my $self = shift;
    return $self->getCanvas->width;
}

=item height

Returns the width of a canvas.

    $w = $plotter->height();

=cut

sub height {
    my $self = shift;
    return $self->getCanvas->height;
}

=item setBackground

Sets the background color

    $plotter->setBackground('black');

=cut

sub setBackground {
    my $self = shift;
    my $color = shift;
    $self->getCanvas()->configure(-background => $color);
}

=item setForeground

Sets the foreground color.

    $plotter->setForeground('black');

=cut

sub setForeground {
    my $self = shift;
    my $color = shift;
    $self->getCanvas()->configure(-foreground => $color);
}

=item font

Sets and returns the font currently being used.

    $f = $plotter->font();
    $plotter->font($f);

=cut

sub font {
    my $self = shift;
    if (@_) {
        $self->{FONT} = shift;
    }
    return $self->{FONT};
}

=item fontColor

Sets and returns the font color.

    $plotter->fontColor('black');

=cut

sub fontColor {
    my $self = shift;
    if (@_) {
        $self->{FONT_COLOR} = shift;
    }
    return $self->{FONT_COLOR};
}

=item fontSize

Sets and returns the font size.

    $plotter->fontSize(5);
    $size = $plotter->fontSize();

=cut

sub fontSize {
    my $self = shift;
    my $font = $self->font();
    $font =~ /\d+/;
    my $before = $`;
    my $num = $&;
    my $end = $';
    if (@_) {
        my $size = shift;
        $self->font($before . $size . '0' . $end);
        $self->{FONTSIZE} = $size;
    }
    else {
        $self->{FONT_SIZE} = $num / 10 if (!defined $self->{FONT_SIZE});
    }
    return $self->{FONT_SIZE};
}


=item drawColor

Sets and returns the drawing color, used for filling as well.

    $plotter->drawColor('Black');
    $c = $plotter->drawColor();

=cut

sub drawColor {
    my $self = shift;
    if (@_) {
        $self->{DRAW_COLOR} = shift;
    }
    return $self->{DRAW_COLOR};
}

=item penWidth

Sets and returns the width lines, circles, etc will be drawn in.

    $plotter->penWidth(3);
    $w = $plotter->penWidth();

=cut

sub penWidth {
    my $self = shift;
    if (@_) {
        $self->{PEN_WIDTH} = shift;
    }
    return $self->{PEN_WIDTH};
}

=item pack

Packs the canvas into a certain position within a main window
or frame.

    $plotter->pack(-side => 'left');

=cut

sub pack {
    my $self = shift;
    $self->getCanvas()->pack(@_);
}

=back

=head2 Conversion from world to pixels

lib/App/SourcePlot/Plotter/Tk.pm  view on Meta::CPAN

=cut

sub bindTag {
    my $self = shift;
    my $tag = shift;
    my @con = @_;
    $self->getCanvas()->bind($tag, @con);
}

=item existTag

Returns the number of items associated with that tag

    $exists = $plotter->existTag('oval');

=cut

sub existTag {
    my $self = shift;
    my $tag = shift;
    my @items = $self->getCanvas()->find('withtag', $tag);
    my $len = @items;
    return $len;
}

=item raiseAbove

Raise the objects with the first tag above the objects with the second
tag.

    $plotter->raiseAbove('oval', 'square');

=cut

sub raiseAbove {
    my $self = shift;
    my $tag = shift;
    my $tag2 = shift;
    $self->getCanvas()->raise($tag, $tag2);
}

=item lowerBelow

Lowers the objects with the first tag below the objects with the
second tag.

    $plotter->lowerBelow('oval', 'square');

=cut

sub lowerBelow {
    my $self = shift;
    my $tag = shift;
    my $tag2 = shift;
    $self->getCanvas()->lower($tag, $tag2);
}

=item drawTextVert

Draws text on the canvas in the given (x, y) coordinates using
the current font, font size, and font Color. Returns the text item
number.  Adds a tag name if one is given.  Draws it vertically

    $plotter->drawTextVert(5, 5, "hello");
    $id = $plotter->drawTextVert(5, 5, "hello", 'text');

=cut

sub drawTextVert {
    my $self = shift;
    my $x = shift;
    my $y = shift;
    my $text = shift;
    my $font = $self->font();
    if ($self->usingWorld()) {
        $font =~ /\d+/;
        my $before = $`;
        my $num = $&;
        my $end = $';
        $num = ($num / 10) / $self->worldToPixRatio();
        $num = s/\..+//;
        $font = $before . $num . '0' . $end;
    }
    my $t = $self->getCanvas()->create(
        'text',
        $self->toP($x, $y),
        -text => $text,
        -font => $font,
        -width => 1,
        -fill => $self->fontColor()
    );
    if (@_) {
        my $tag = shift;
        $self->getCanvas()->addtag($tag, 'withtag', $t);
    }
    return $t;
}


=item drawText

Draws text on the canvas in the given (x, y) coordinates using
the current font, font size, and font Color. Returns the text item
number.  Adds a tag name if one is given.

    $plotter->drawText(5, 5, "hello");
    $id = $plotter->drawText(5, 5, "hello", 'text');

=cut

sub drawText {
    my $self = shift;
    my $x = shift;
    my $y = shift;
    my $text = shift;
    my $font = $self->font();
    if ($self->usingWorld()) {
        $font =~ /\d+/;
        my $before = $`;
        my $num = $&;
        my $end = $';
        $num = ($num / 10) / $self->worldToPixRatio();
        $num = s/\..+//;
        $font = $before . $num . '0' . $end;
    }
    my $t = $self->getCanvas()->create(
        'text',
        $self->toP($x, $y),
        -text => $text,
        -font => $font,
        -fill => $self->fontColor()
    );
    if (@_) {
        my $tag = shift;
        $self->getCanvas()->addtag($tag, 'withtag', $t);
    }
    return $t;
}

=item drawTextFromLeft

Draws text on the canvas in the given (x, y) coordinates using
the current font, font size, and font Color. Returns the text item
number.  Adds a tag name if one is given.

    $plotter->drawTextFromLeft(5, 5, "hello");
    $id = $plotter->drawTextFromLeft(5, 5, "hello", 'text');

=cut

sub drawTextFromLeft {
    my $self = shift;
    my $x = shift;
    my $y = shift;
    my $text = shift;
    my $font = $self->font();
    if ($self->usingWorld()) {
        $font =~ /\d+/;
        my $before = $`;
        my $num = $&;
        my $end = $';
        $num = ($num / 10) / $self->worldToPixRatio();
        $num = s/\..+//;
        $font = $before . $num . '0' . $end;
    }
    my $t = $self->getCanvas()->create(
        'text',
        $self->toP($x, $y),
        -text => $text,
        -font => $font,
        -fill => $self->fontColor(),
        -anchor => 'w'
    );
    if (@_) {
        my $tag = shift;
        $self->getCanvas()->addtag($tag, 'withtag', $t);
    }
    return $t;
}

=item drawTextFromRight

Draws text on the canvas in the given (x, y) coordinates using
the current font, font size, and font Color.  Returns the text item
number.  Adds a tag name if one is given.

    $plotter->drawTextFromRight(5, 5, "hello");
    $id = $plotter->drawTextFromRight(5, 5, "hello", 'text');

=cut

sub drawTextFromRight {
    my $self = shift;
    my $x = shift;
    my $y = shift;
    my $text = shift;
    my $font = $self->font();
    if ($self->usingWorld()) {
        $font =~ /\d+/;
        my $before = $`;
        my $num = $&;
        my $end = $';
        $num = ($num / 10) / $self->worldToPixRatio();
        $num = s/\..+//;
        $font = $before . $num . '0' . $end;
    }
    my $t = $self->getCanvas()->create(
        'text',
        $self->toP($x, $y),
        -text => $text,
        -font => $font,
        -fill => $self->fontColor(),
        -anchor => 'e'
    );
    if (@_) {
        my $tag = shift;
        $self->getCanvas()->addtag($tag, 'withtag', $t);
    }
    return $t;
}

=item drawOval

Draws an oval on the canvas in the given set of (x, y)
coordinates Returns the ovals item number.  Adds a tag name if one is
given.

    $plotter->drawOval(5, 5, 10, 10);
    $id = $plotter->drawOval(5, 5, 10, 10, 'oval');

=cut

sub drawOval {
    my $self = shift;
    my $oval = $self->getCanvas()->create(
        'oval',
        $self->toP(shift, shift),
        $self->toP(shift, shift),
        -width => $self->penWidth(),
        -outline => $self->drawColor()
    );
    if (@_) {
        my $tag = shift;
        $self->getCanvas()->addtag($tag, 'withtag', $oval);
    }
    return $oval;
}

=item drawFillOval

Draws an oval on the canvas in the given set of (x, y)
coordinates and fills it.  Returns the ovals item number.  Adds a tag
name if one is given.

    $plotter->drawFillOval(5, 5, 10, 10);
    $id = $plotter->drawFillOval(5, 5, 10, 10, 'oval');

=cut

sub drawFillOval {
    my $self = shift;
    my $oval = $self->getCanvas()->create(
        'oval',
        $self->toP(shift, shift),
        $self->toP(shift, shift),
        -width => $self->penWidth(),
        -outline => $self->drawColor(),
        -fill => $self->drawColor()
    );
    if (@_) {
        my $tag = shift;
        $self->getCanvas()->addtag($tag, 'withtag', $oval);



( run in 0.792 second using v1.01-cache-2.11-cpan-ceb78f64989 )