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
|
package StarRating;
use strict;
use warnings;
use QtCore4;
use QtGui4;
use constant {
Editable => 0,
ReadOnly => 1
};
use constant PaintingScaleFactor => 20;
sub new
{
my ($starCount, $maxStarCount) = (1, 5);
my ($class) = @_;
$starCount = $_[1] if defined $_[1];
$maxStarCount = $_[2] if defined $_[2];
my $self = bless {}, $class;
$self->{myStarCount} = $starCount;
$self->{myMaxStarCount} = $maxStarCount;
push @{$self->{starPolygon}}, Qt::PointF(1.0, 0.5);
foreach my $i ( 1..4 ) {
push @{$self->{starPolygon}}, Qt::PointF(0.5 + 0.5 * cos(0.8 * $i * 3.14),
0.5 + 0.5 * sin(0.8 * $i * 3.14));
}
push @{$self->{diamondPolygon}},
Qt::PointF(0.4, 0.5),
Qt::PointF(0.5, 0.4),
Qt::PointF(0.6, 0.5),
Qt::PointF(0.5, 0.6),
Qt::PointF(0.4, 0.5);
return $self;
}
# [0]
sub starCount
{
my $self = shift;
return $self->{myStarCount};
}
sub maxStarCount
{
my $self = shift;
return $self->{myMaxStarCount};
}
sub setStarCount
{
my ($self, $starCount) = @_;
$self->{myStarCount} = $starCount;
}
sub setMaxStarCount
{
my ($self, $maxStarCount) = @_;
$self->{myMaxStarCount} = $maxStarCount;
}
# [1]
sub sizeHint
{
my $self = shift;
return Qt::Size(
$self->{myMaxStarCount} * PaintingScaleFactor,
1 * PaintingScaleFactor
);
}
# [1]
# [2]
sub paint
{
my ($self, $painter, $rect, $palette, $mode) = @_;
$painter->save();
$painter->setRenderHint(Qt::Painter::Antialiasing(), 1);
$painter->setPen(Qt::NoPen());
if ($mode == Editable) {
$painter->setBrush($palette->highlight());
} else {
$painter->setBrush($palette->foreground());
}
my $yOffset = ($rect->height() - PaintingScaleFactor) / 2;
$painter->translate($rect->x(), $rect->y() + $yOffset);
$painter->scale(PaintingScaleFactor, PaintingScaleFactor);
foreach my $i ( 0..$self->{myMaxStarCount}-1 ) {
if ($i < $self->{myStarCount}) {
$painter->drawPolygon(Qt::PolygonF($self->{starPolygon}), Qt::WindingFill());
} elsif ($mode == Editable) {
$painter->drawPolygon(Qt::PolygonF($self->{diamondPolygon}), Qt::WindingFill());
}
$painter->translate(1.0, 0.0);
}
$painter->restore();
}
# [2]
1;
|