1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
package DisplayWidget;
use strict;
use warnings;
use QtCore4;
use QtGui4;
# [DisplayWidget class definition]
use QtCore4::isa qw( Qt::Widget );
use QtCore4::slots
setBackground => ['int'],
setColor => ['const Qt::Color &'],
setShape => ['int'];
use constant { House => 0, Car => 1 };
use constant { Sky => 0, Trees => 1, Road => 2 };
sub background() {
return this->{background};
}
sub shapeColor() {
return this->{shapeColor};
}
sub shape() {
return this->{shape};
}
sub shapeMap() {
return this->{shapeMap};
}
sub moon() {
return this->{moon};
}
sub tree() {
return this->{tree};
}
# [DisplayWidget class definition]
sub NEW
{
my ($class, $parent) = @_;
$class->SUPER::NEW($parent);
my $car = Qt::PainterPath();
my $house = Qt::PainterPath();
this->{tree} = Qt::PainterPath();
this->{moon} = Qt::PainterPath();
my $file = Qt::File('resources/shapes.dat');
$file->open(Qt::File::ReadOnly());
my $stream = Qt::DataStream($file);
no warnings qw(void);
$stream >> $car >> $house >> this->{tree} >> this->{moon};
use warnings;
$file->close();
this->{shapeMap} = {
Car() => $car,
House() => $house
};
this->{background} = Sky;
this->{shapeColor} = Qt::Color(Qt::darkYellow());
this->{shape} = House;
}
# [paint event]
sub paintEvent
{
my ($event) = @_;
my $painter = Qt::Painter();
$painter->begin(this);
$painter->setRenderHint(Qt::Painter::Antialiasing());
this->paint($painter);
$painter->end();
}
# [paint event]
# [paint function]
sub paint
{
my ($painter) = @_;
#[paint picture]
$painter->setClipRect(Qt::Rect(0, 0, 200, 200));
$painter->setPen(Qt::NoPen());
if (this->background == Trees)
{
$painter->fillRect(Qt::Rect(0, 0, 200, 200), Qt::Color(Qt::darkGreen()));
$painter->setBrush(Qt::Brush(Qt::Color(Qt::green())));
$painter->setPen(Qt::black());
for (my $y = -55, my $row = 0; $y < 200; $y += 50, ++$row) {
my $xs;
if ($row == 2 || $row == 3) {
$xs = 150;
}
else {
$xs = 50;
}
for (my $x = 0; $x < 200; $x += $xs) {
$painter->save();
$painter->translate($x, $y);
$painter->drawPath(this->tree);
$painter->restore();
}
}
}
elsif (this->background == Road) {
$painter->fillRect(Qt::Rect(0, 0, 200, 200), Qt::Color(Qt::gray()));
$painter->setPen(Qt::Pen(Qt::Brush(Qt::Color(Qt::white())), 4, Qt::DashLine()));
$painter->drawLine(Qt::Line(0, 35, 200, 35));
$painter->drawLine(Qt::Line(0, 165, 200, 165));
}
else {
$painter->fillRect(Qt::Rect(0, 0, 200, 200), Qt::Color(Qt::darkBlue()));
$painter->translate(145, 10);
$painter->setBrush(Qt::Brush(Qt::Color(Qt::white())));
$painter->drawPath(this->moon);
$painter->translate(-145, -10);
}
$painter->setBrush(Qt::Brush(this->shapeColor));
$painter->setPen(Qt::black());
$painter->translate(100, 100);
$painter->drawPath(this->shapeMap->{this->shape});
#[paint picture]
}
# [paint function]
sub color
{
return this->shapeColor;
}
sub setBackground
{
my ($background) = @_;
this->{background} = $background;
this->update();
}
sub setColor
{
my ($color) = @_;
this->{shapeColor} = $color;
this->update();
}
sub setShape
{
my ($shape) = @_;
this->{shape} = $shape;
this->update();
}
1;
|