#
# PerlQt Example Application: drawdemo
#
# Demonstrates the painter and the printer.
#

use QColor;
use QFont;
use QPainter;
use QPrinter;
use QPushButton;
use QRadioButton;
use POSIX 'math_h';  # No atan() in Perl.

#
# This function draws a color wheel.
# The coordinate system x=(0..500), y=(0..500) spans the paint device.
#

sub drawColorWheel {
    my $p = shift;
    my $f = new QFont('Times', 18, $Weight{Bold});
    $p->setFont($f);
    $p->setPen($black);
    $p->setWindow(0, 0, 500, 500);

    for(my $i = 0; $i < 36; $i++) {
	my $matrix = new QWMatrix;
	$matrix->translate(250.0, 250.0);
	$matrix->shear(0.0, 0.3);
	$matrix->rotate($i*10);
	$p->setWorldMatrix($matrix);

	$c = new QColor;
	$c->setHsv($i*10, 255, 255);
	$p->setBrush($c);
	$p->drawRect(70, -10, 80, 10);

	$p->drawText(80+70+5, 0, "H=" . $i*10);
    }
}

#
# This function draws a few lines of text using different fonts.
#

{
    my @fonts = ('Helvetica', 'Courier', 'Times');
    my @sizes = (10, 12, 18, 24);

    sub drawFonts {
	my $p = shift;
	my($y) = (0, 0, 0);
	for my $f (@fonts) {
	    for my $s (@sizes) {
		my $font = new QFont($f, $s);
		$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;
    my $b1 = new QBrush($blue);
    my $b2 = new QBrush($green, $BrushStyle{Dense6});
    my $b3 = new QBrush($BrushStyle{NoBrush});
    my $b4 = new QBrush($BrushStyle{Cross});

    $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);
}

#
# This function draws a text that follows a (Bezier) curve.
# Notice that this function does not support the general case.
# It should be rewritten to calculate the real dx/dy.
#

sub drawPathText {
    my $p = new QPainter;
    my $a = new QPointArray(4);
    $a->setPoint(0, 100,200);
    $a->setPoint(1, 150,75);
    $a->setPoint(2, 250,75);
    $a->setPoint(3, 300,200);
    $a = $a->quadBezier();

    $p->setPen($lightGray);
    $p->drawPolyline($a);

    $p->setFont(new QFont('Times', 24));
    $p->setPen($black);

    my $text = "PerlQt rocks!";

    my $len = length($text);
    return unless $len;
    my $ipos = $a->size()/$len;
    my $cpos = $ipos;

    for(my $i = 0; $i < $len; $i++) {
	my $p1 = $a->point($cpos-1);
	my $p2 = $a->point($cpos+1);
	my $pt = $a->point($cpos);
	my $dx = $p2->x() - $p1->x();
	my $dy = $p2->y() - $p1->y();
	my $angle = atan($dy/$dx);
	$angle *= 180.0/3.14;
	my $m = new QWMatrix;
	$m->translate($pt->x(), $pt->y());
	$m->rotate($angle);
	$p->setWorldMatrix($m);
	$p->drawText(0, 0, substr($text, $i, 1), 1);
	$cpos += $ipos;
    }
}

package DrawView;

use slots 'updateIt(int)', 'printIt()';

@ourDrawFunctions = (
    { f => \&drawColorWheel, name => 'Draw color wheel' },
    { f => \&drawFonts,      name => 'Draw fonts'       },
    { f => \&drawShapes,     name => 'Draw shapes'      },
    { f => \&drawPathText,   name => 'Draw path text'   }
);

sub new {}
    
