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
|
package PixelDelegate;
use strict;
use warnings;
use QtCore4;
use QtGui4;
use List::Util qw( min );
use constant ItemSize => 256;
# [0]
use QtCore4::isa qw( Qt::AbstractItemDelegate );
use QtCore4::slots
setPixelSize => ['int'];
sub pixelSize() {
return this->{pixelSize};
}
sub NEW
{
my ($class, $parent) = @_;
$class->SUPER::NEW($parent);
this->{pixelSize} = 12;
}
# [0]
our @MYSTYLES = qw(State_None State_Active State_AutoRaise State_Children State_DownArrow State_Editing State_Enabled State_HasFocus State_Horizontal State_KeyboardFocusChange State_MouseOver State_NoChange State_Off State_On State_Raised State_ReadOnly State_Selected State_Item State_Open State_Sibling State_Sunken State_UpArrow State_Mini State_Small);
# [1]
sub paint
{
my ($painter, $option, $index) = @_;
# [2]
#print join "\n",
#map{ $MYSTYLES[$_] } grep{ $_ } map{
#my $opt = $MYSTYLES[$_];
#my $state = $option->state();
#my $eval = eval("Qt::Style::$opt()");
#$_ if ${$state & $eval}
#} 0..@MYSTYLES-1;
if (${$option->state & Qt::Style::State_Selected()}) {
$painter->fillRect($option->rect, $option->palette->highlight());
}
# [1]
# [3]
my $size = min($option->rect->width(), $option->rect->height());
# [3] //! [4]
my $brightness = $index->model()->data($index, Qt::DisplayRole())->toInt();
my $radius = ($size/2.0) - ($brightness/255.0 * $size/2.0);
if ($radius == 0.0) {
return;
}
# [4]
# [5]
$painter->save();
# [5] //! [6]
$painter->setRenderHint(Qt::Painter::Antialiasing(), 1);
# [6] //! [7]
$painter->setPen(Qt::NoPen());
# [7] //! [8]
if (${$option->state & Qt::Style::State_Selected()}) {
# [8] //! [9]
$painter->setBrush($option->palette->highlightedText());
}
else {
# [2]
$painter->setBrush(Qt::Brush(Qt::Color(Qt::black())));
}
# [9]
# [10]
$painter->drawEllipse(Qt::RectF($option->rect->x() + $option->rect->width()/2 - $radius,
$option->rect->y() + $option->rect->height()/2 - $radius,
2*$radius, 2*$radius));
$painter->restore();
}
# [10]
# [11]
sub sizeHint
{
return Qt::Size(pixelSize, pixelSize);
}
# [11]
# [12]
sub setPixelSize
{
my ($size) = @_;
this->{pixelSize} = $size;
}
# [12]
1;
|