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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
package RenderArea;
use strict;
use warnings;
use QtCore4;
use QtGui4;
# [0]
use constant {
NoTransformation => 0,
Translate => 1,
Rotate => 2,
Scale => 3
};
# [0]
# [1]
use QtCore4::isa qw( Qt::Widget );
# [1]
# [2]
sub operations() {
return this->{operations};
}
sub shape() {
return this->{shape};
}
sub xBoundingRect() {
return this->{xBoundingRect};
}
sub yBoundingRect() {
return this->{yBoundingRect};
}
# [2]
# [0]
sub NEW
{
my ($class, $parent) = @_;
$class->SUPER::NEW($parent);
my $newFont = this->font();
$newFont->setPixelSize(12);
this->setFont($newFont);
my $fontMetrics = Qt::FontMetrics($newFont);
this->{xBoundingRect} = $fontMetrics->boundingRect(this->tr('x'));
this->{yBoundingRect} = $fontMetrics->boundingRect(this->tr('y'));
this->{operations} = [];
}
# [0]
# [1]
sub setOperations
{
my ($operations) = @_;
# Make sure to copy the array
this->{operations} = [@{$operations}];
this->update();
}
# [1]
# [2]
sub setShape
{
my ($shape) = @_;
this->{shape} = $shape;
this->update();
}
# [2]
# [3]
sub minimumSizeHint
{
return Qt::Size(182, 182);
}
# [3]
# [4]
sub sizeHint
{
return Qt::Size(232, 232);
}
# [4]
# [5]
sub paintEvent
{
my ($event) = @_;
my $painter = Qt::Painter(this);
$painter->setRenderHint(Qt::Painter::Antialiasing());
$painter->fillRect($event->rect(), Qt::Brush(Qt::white()));
$painter->translate(66, 66);
# [5]
# [6]
$painter->save();
this->transformPainter($painter);
this->drawShape($painter);
$painter->restore();
# [6]
# [7]
this->drawOutline($painter);
# [7]
# [8]
this->transformPainter($painter);
this->drawCoordinates($painter);
$painter->end();
}
# [8]
# [9]
sub drawCoordinates
{
my ($painter) = @_;
$painter->setPen(Qt::Color(Qt::red()));
$painter->drawLine(0, 0, 50, 0);
$painter->drawLine(48, -2, 50, 0);
$painter->drawLine(48, 2, 50, 0);
$painter->drawText(60 - this->xBoundingRect->width() / 2,
0 + this->xBoundingRect->height() / 2, this->tr('x'));
$painter->drawLine(0, 0, 0, 50);
$painter->drawLine(-2, 48, 0, 50);
$painter->drawLine(2, 48, 0, 50);
$painter->drawText(0 - this->yBoundingRect->width() / 2,
60 + this->yBoundingRect->height() / 2, this->tr('y'));
}
# [9]
# [10]
sub drawOutline
{
my ($painter) = @_;
$painter->setPen(Qt::darkGreen());
$painter->setPen(Qt::DashLine());
$painter->setBrush(Qt::NoBrush());
$painter->drawRect(0, 0, 100, 100);
}
# [10]
# [11]
sub drawShape
{
my ($painter) = @_;
$painter->fillPath(this->shape, Qt::Brush(Qt::blue()));
}
# [11]
# [12]
sub transformPainter
{
my ($painter) = @_;
foreach my $operation( @{this->operations} ) {
if ( $operation == Translate ) {
$painter->translate(50, 50);
}
elsif ( $operation == Scale ) {
$painter->scale(0.75, 0.75);
}
elsif ( $operation == Rotate ) {
$painter->rotate(60);
}
}
}
# [12]
1;
|