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
|
package ShapedClock;
use strict;
use warnings;
use List::Util qw( min );
use QtCore4;
use QtGui4;
use QtCore4::isa qw( Qt::Widget );
# [0]
sub dragPosition() {
return this->{dragPosition};
}
# [0]
my $hourHand = Qt::Polygon( [
Qt::Point(7, 8),
Qt::Point(-7, 8),
Qt::Point(0, -40)
] );
my $minuteHand = Qt::Polygon( [
Qt::Point(7, 8),
Qt::Point(-7, 8),
Qt::Point(0, -70)
] );
# [0]
sub NEW {
my ( $class, $parent ) = @_;
$class->SUPER::NEW( $parent, Qt::FramelessWindowHint() | Qt::WindowSystemMenuHint());
my $timer = Qt::Timer(this);
this->connect($timer, SIGNAL 'timeout()', this, SLOT 'update()');
$timer->start(1000);
my $quitAction = Qt::Action(this->tr("E&xit"), this);
$quitAction->setShortcut(Qt::KeySequence(this->tr("Ctrl+Q")));
this->connect($quitAction, SIGNAL 'triggered()', qApp, SLOT 'quit()');
this->addAction($quitAction);
this->setContextMenuPolicy(Qt::ActionsContextMenu());
this->setToolTip(this->tr("Drag the clock with the left mouse button.\n" .
"Use the right mouse button to open a context menu."));
this->setWindowTitle(this->tr("Shaped Analog Clock"));
}
# [0]
# [1]
sub mousePressEvent {
my ($event) = @_;
if ($event->button() == Qt::LeftButton()) {
this->{dragPosition} = Qt::Point($event->globalPos() - this->frameGeometry()->topLeft());
$event->accept();
}
}
# [1]
# [2]
sub mouseMoveEvent {
my ($event) = @_;
if ($event->buttons() & ${Qt::LeftButton()}) {
this->move($event->globalPos() - this->dragPosition);
$event->accept();
}
}
# [2]
# [3]
sub paintEvent {
my $hourColor = Qt::Color(127, 0, 127);
my $minuteColor = Qt::Color(0, 127, 127, 191);
my $side = min(this->width(), this->height());
my $time = Qt::Time::currentTime();
my $painter = Qt::Painter(this);
$painter->setRenderHint(Qt::Painter::Antialiasing());
$painter->translate(this->width() / 2, this->height() / 2);
$painter->scale($side / 200.0, $side / 200.0);
$painter->setPen(Qt::NoPen());
$painter->setBrush(Qt::Brush($hourColor));
$painter->save();
$painter->rotate(30.0 * (($time->hour() + $time->minute() / 60.0)));
$painter->drawConvexPolygon($hourHand);
$painter->restore();
$painter->setPen($hourColor);
for (my $i = 0; $i < 12; ++$i) {
$painter->drawLine(88, 0, 96, 0);
$painter->rotate(30.0);
}
$painter->setPen(Qt::NoPen());
$painter->setBrush(Qt::Brush($minuteColor));
$painter->save();
$painter->rotate(6.0 * ($time->minute() + $time->second() / 60.0));
$painter->drawConvexPolygon($minuteHand);
$painter->restore();
$painter->setPen($minuteColor);
for (my $j = 0; $j < 60; ++$j) {
if (($j % 5) != 0){
$painter->drawLine(92, 0, 96, 0);
}
$painter->rotate(6.0);
}
$painter->end();
}
# [3]
# [4]
sub resizeEvent {
my $side = min(this->width(), this->height());
my $maskedRegion = Qt::Region(this->width() / 2 - $side / 2, this->height() / 2 - $side / 2, $side,
$side, Qt::Region::Ellipse());
this->setMask($maskedRegion);
}
# [4]
# [5]
sub sizeHint {
return Qt::Size(100, 100);
}
# [5]
1;
|