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
|
package DragWidget;
use strict;
use warnings;
use QtCore4;
use QtGui4;
use QtCore4::isa qw( Qt::Widget );
use DragLabel;
use List::Util qw(max);
sub NEW
{
my ($class, $parent) = @_;
$class->SUPER::NEW($parent);
my $dictionaryFile = Qt::File('words.txt');
$dictionaryFile->open(Qt::IODevice::ReadOnly());
my $inputStream = Qt::TextStream($dictionaryFile);
my $x = 5;
my $y = 5;
while (!$inputStream->atEnd()) {
my $word;
no warnings qw(void);
$inputStream >> Qt::String($word);
use warnings;
if ($word) {
my $wordLabel = DragLabel($word, this);
$wordLabel->move($x, $y);
$wordLabel->show();
$wordLabel->setAttribute(Qt::WA_DeleteOnClose());
$x += $wordLabel->width() + 2;
if ($x >= 195) {
$x = 5;
$y += $wordLabel->height() + 2;
}
}
}
my $newPalette = this->palette();
$newPalette->setColor(Qt::Palette::Window(), Qt::Color(Qt::white()));
this->setPalette($newPalette);
this->setAcceptDrops(1);
this->setMinimumSize(400, max(200, $y));
this->setWindowTitle(this->tr('Draggable Text'));
}
sub dragEnterEvent
{
my ($event) = @_;
if ($event->mimeData()->hasText()) {
my $children = this->children();
if ($children && grep{ $_ eq $event->source } @{$children}) {
$event->setDropAction(Qt::MoveAction());
$event->accept();
} else {
$event->acceptProposedAction();
}
} else {
$event->ignore();
}
}
sub dropEvent
{
my ($event) = @_;
if ($event->mimeData()->hasText()) {
my $mime = $event->mimeData();
my @pieces = split /\s+/, $mime->text();
my $position = $event->pos();
my $hotSpot = Qt::Point();
my @hotSpotPos = split / /, $mime->data('application/x-hotspot')->data();
if (scalar @hotSpotPos == 2) {
$hotSpot->setX($hotSpotPos[0]);
$hotSpot->setY($hotSpotPos[1]);
}
foreach my $piece ( @pieces ) {
my $newLabel = DragLabel($piece, this);
$newLabel->move($position - $hotSpot);
$newLabel->show();
$newLabel->setAttribute(Qt::WA_DeleteOnClose());
$position += Qt::Point($newLabel->width(), 0);
}
if ($event->source() == this) {
$event->setDropAction(Qt::MoveAction());
$event->accept();
} else {
$event->acceptProposedAction();
}
} else {
$event->ignore();
}
foreach my $child ( @{this->children()} ) {
if ($child->inherits('Qt::Widget')) {
if (!$child->isVisible()) {
$child->deleteLater();
}
}
}
}
sub mousePressEvent
{
my ($event) = @_;
my $child = this->childAt($event->pos());
if (!$child) {
return;
}
my $hotSpot = $event->pos() - $child->pos();
my $mimeData = Qt::MimeData();
$mimeData->setText($child->text());
$mimeData->setData('application/x-hotspot',
Qt::ByteArray( $hotSpot->x()
. ' ' . $hotSpot->y()) );
my $pixmap = Qt::Pixmap($child->size());
$child->render($pixmap);
my $drag = Qt::Drag(this);
$drag->setMimeData($mimeData);
$drag->setPixmap($pixmap);
$drag->setHotSpot($hotSpot);
my $dropAction = $drag->exec(Qt::CopyAction() | Qt::MoveAction(), Qt::CopyAction());
if ($dropAction == Qt::MoveAction()) {
$child->close();
}
}
1;
|