You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
199 lines
4.4 KiB
199 lines
4.4 KiB
13 years ago
|
#!/usr/bin/perl -w
|
||
|
use strict;
|
||
|
package DrawView;
|
||
13 years ago
|
use TQt;
|
||
|
use TQt::isa qw(TQt::Widget);
|
||
|
use TQt::slots
|
||
13 years ago
|
updateIt => ['int'],
|
||
|
printIt => [];
|
||
13 years ago
|
use TQt::attributes qw(
|
||
13 years ago
|
printer
|
||
|
bgroup
|
||
|
_print
|
||
|
drawindex
|
||
|
maxindex
|
||
|
);
|
||
|
|
||
|
#
|
||
|
# First we define the functionality our demo should present
|
||
|
# to the user. You might add different demo-modes if you wish so
|
||
|
#
|
||
|
|
||
|
#
|
||
|
# This function draws a color wheel.
|
||
|
# The coordinate system x=(0..500), y=(0..500) spans the paint device.
|
||
|
#
|
||
|
|
||
|
sub drawColorWheel {
|
||
|
my $p = shift;
|
||
13 years ago
|
my $f = TQt::Font("times", 18, &TQt::Font::Bold);
|
||
13 years ago
|
$p->setFont($f);
|
||
|
$p->setPen(&black);
|
||
|
$p->setWindow(0, 0, 500, 500); # defines coordinate system
|
||
|
|
||
|
for my $i (0..35) {
|
||
13 years ago
|
my $matrix = TQt::WMatrix;
|
||
13 years ago
|
$matrix->translate(250.0, 250.0); # move to center
|
||
|
$matrix->shear(0.0, 0.3); # twist it
|
||
|
$matrix->rotate($i*10.0); # rotate 0,10,20,.. degrees
|
||
|
$p->setWorldMatrix($matrix); # use this world matrix
|
||
|
|
||
13 years ago
|
my $c = TQt::Color;
|
||
13 years ago
|
$c->setHsv($i*10, 255, 255); # rainbow effect
|
||
|
$p->setBrush($c); # solid fill with color $c
|
||
|
$p->drawRect(70, -10, 80, 10); # draw the rectangle
|
||
|
|
||
|
my $n = sprintf "H=%d", $i*10;
|
||
|
$p->drawText(80+70+5, 0, $n); # draw the hue number
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#
|
||
|
# This function draws a few lines of text using different fonts.
|
||
|
#
|
||
|
|
||
|
sub drawFonts {
|
||
|
my $p = shift;
|
||
|
my @fonts = qw(Helvetica Courier Times);
|
||
|
my @sizes = (10, 12, 18, 24, 36);
|
||
|
my $y = 0;
|
||
|
for my $f (@fonts) {
|
||
|
for my $s (@sizes) {
|
||
13 years ago
|
my $font = TQt::Font($f, $s);
|
||
13 years ago
|
$p->setFont($font);
|
||
|
my $fm = $p->fontMetrics;
|
||
|
$y += $fm->ascent;
|
||
|
$p->drawText(10, $y, "Quartz Glyph Job Vex'd Cwm Finks");
|
||
|
$y += $fm->descent;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#
|
||
|
# This function draws some shapes
|
||
|
#
|
||
|
|
||
|
sub drawShapes {
|
||
|
my $p = shift;
|
||
13 years ago
|
my $b1 = TQt::Brush(&blue);
|
||
|
my $b2 = TQt::Brush(&green, &Dense6Pattern); # green 12% fill
|
||
|
my $b3 = TQt::Brush(&NoBrush); # void brush
|
||
|
my $b4 = TQt::Brush(&CrossPattern); # black cross pattern
|
||
13 years ago
|
|
||
|
$p->setPen(&red);
|
||
|
$p->setBrush($b1);
|
||
|
$p->drawRect(10, 10, 200, 100);
|
||
|
$p->setBrush($b2);
|
||
|
$p->drawRoundRect(10, 150, 200, 100, 20, 20);
|
||
|
$p->setBrush($b3);
|
||
|
$p->drawEllipse(250, 10, 200, 100);
|
||
|
$p->setBrush($b4);
|
||
|
$p->drawPie(250, 150, 200, 100, 45*16, 90*16);
|
||
|
}
|
||
|
|
||
|
our @drawFunctions = (
|
||
|
# title presented to user, reference to the function
|
||
|
{ name => "Draw color wheel", f => \&drawColorWheel },
|
||
|
{ name => "Draw fonts" , f => \&drawFonts },
|
||
|
{ name => "Draw shapes" , f => \&drawShapes },
|
||
|
);
|
||
|
|
||
|
#
|
||
|
# Construct the DrawView with buttons.
|
||
|
#
|
||
|
|
||
|
sub NEW {
|
||
|
shift->SUPER::NEW(@_);
|
||
|
|
||
13 years ago
|
setCaption("PerlTQt Draw Demo Application");
|
||
13 years ago
|
setBackgroundColor(&white);
|
||
|
|
||
|
# Create a button group to contain all buttons
|
||
13 years ago
|
bgroup = TQt::ButtonGroup(this);
|
||
13 years ago
|
bgroup->resize(200, 200);
|
||
13 years ago
|
this->connect(bgroup, TQT_SIGNAL('clicked(int)'), TQT_SLOT('updateIt(int)'));
|
||
13 years ago
|
|
||
|
# Calculate the size for the radio buttons
|
||
|
my $maxwidth = 80;
|
||
|
my $maxheight = 10;
|
||
|
my $fm = bgroup->fontMetrics;
|
||
|
|
||
|
for my $i (0 .. $#drawFunctions) {
|
||
|
my $n = $drawFunctions[$i]{name};
|
||
|
my $w = $fm->width($n);
|
||
|
$maxwidth = max($w, $maxwidth);
|
||
|
}
|
||
|
|
||
|
$maxwidth += 30;
|
||
|
|
||
|
for my $i (0 .. $#drawFunctions) {
|
||
|
my $n = $drawFunctions[$i]{name};
|
||
13 years ago
|
my $rb = TQt::RadioButton($n, bgroup);
|
||
13 years ago
|
$rb->setGeometry(10, $i*30+10, $maxwidth, 30);
|
||
|
|
||
|
$maxheight += 30;
|
||
|
|
||
|
$rb->setChecked(1) unless $i;
|
||
|
$i++;
|
||
|
}
|
||
|
|
||
|
$maxheight += 10;
|
||
|
|
||
|
drawindex = 0;
|
||
|
maxindex = scalar @drawFunctions;
|
||
|
$maxwidth += 20;
|
||
|
|
||
|
bgroup->resize($maxwidth, $maxheight);
|
||
|
|
||
13 years ago
|
printer = TQt::Printer;
|
||
13 years ago
|
|
||
13 years ago
|
_print = TQt::PushButton("Print...", bgroup);
|
||
13 years ago
|
_print->resize(80, 30);
|
||
|
_print->move($maxwidth/2 - _print->width/2, maxindex*30+20);
|
||
13 years ago
|
this->connect(_print, TQT_SIGNAL('clicked()'), TQT_SLOT('printIt()'));
|
||
13 years ago
|
|
||
|
bgroup->resize($maxwidth, _print->y+_print->height+10);
|
||
|
|
||
|
resize(640,300);
|
||
|
}
|
||
|
|
||
|
sub updateIt {
|
||
|
my $index = shift;
|
||
|
if($index < maxindex) {
|
||
|
drawindex = $index;
|
||
|
update();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
sub drawIt {
|
||
|
my $p = shift;
|
||
|
$drawFunctions[drawindex]{f}->($p);
|
||
|
}
|
||
|
|
||
|
sub printIt {
|
||
|
if(printer->setup(this)) {
|
||
13 years ago
|
my $paint = TQt::Painter(printer);
|
||
13 years ago
|
drawIt($paint);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
sub paintEvent {
|
||
13 years ago
|
my $paint = TQt::Painter(this);
|
||
13 years ago
|
drawIt($paint);
|
||
|
}
|
||
|
|
||
|
sub resizeEvent {
|
||
|
bgroup->move(int(width() - bgroup->width), int(0));
|
||
|
}
|
||
|
|
||
|
package main;
|
||
13 years ago
|
use TQt;
|
||
13 years ago
|
use DrawView;
|
||
|
|
||
13 years ago
|
my $app = TQt::Application(\@ARGV);
|
||
13 years ago
|
my $draw = DrawView;
|
||
|
$app->setMainWidget($draw);
|
||
13 years ago
|
$draw->setCaption("PerlTQt Example - Drawdemo");
|
||
13 years ago
|
$draw->show;
|
||
|
exit $app->exec;
|