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
|
package PiecesList;
use strict;
use warnings;
use QtCore4;
use QtGui4;
use QtCore4::isa qw( Qt::ListWidget );
sub NEW
{
my ($class, $parent) = @_;
$class->SUPER::NEW($parent);
setDragEnabled(1);
setViewMode(Qt::ListView::IconMode());
setIconSize(Qt::Size(60, 60));
setSpacing(10);
setAcceptDrops(1);
setDropIndicatorShown(1);
}
sub dragEnterEvent
{
my ($event) = @_;
if ($event->mimeData()->hasFormat('image/x-puzzle-piece')) {
$event->accept();
}
else {
$event->ignore();
}
}
sub dragMoveEvent
{
my ($event) = @_;
if ($event->mimeData()->hasFormat('image/x-puzzle-piece')) {
$event->setDropAction(Qt::MoveAction());
$event->accept();
} else {
$event->ignore();
}
}
sub dropEvent
{
my ($event) = @_;
if ($event->mimeData()->hasFormat('image/x-puzzle-piece')) {
my $pieceData = $event->mimeData()->data('image/x-puzzle-piece');
my $dataStream = Qt::DataStream($pieceData, Qt::IODevice::ReadOnly());
my $pixmap = Qt::Pixmap();
my $location = Qt::Point();
no warnings qw(void);
$dataStream >> $pixmap >> $location;
use warnings;
addPiece($pixmap, $location);
$event->setDropAction(Qt::MoveAction());
$event->accept();
} else {
$event->ignore();
}
}
sub addPiece
{
my ($pixmap, $location) = @_;
my $pieceItem = Qt::ListWidgetItem(this);
$pieceItem->setIcon(Qt::Icon($pixmap));
$pieceItem->setData(Qt::UserRole(), Qt::qVariantFromValue($pixmap));
$pieceItem->setData(Qt::UserRole()+1, Qt::Variant($location));
$pieceItem->setFlags(Qt::ItemIsEnabled() | Qt::ItemIsSelectable()
| Qt::ItemIsDragEnabled());
}
sub startDrag
{
my $item = currentItem();
my $itemData = Qt::ByteArray();
my $dataStream = Qt::DataStream($itemData, Qt::IODevice::WriteOnly());
my $pixmap = $item->data(Qt::UserRole())->value();
my $location = $item->data(Qt::UserRole()+1)->toPoint();
no warnings qw(void);
$dataStream << $pixmap << $location;
use warnings;
my $mimeData = Qt::MimeData();
$mimeData->setData('image/x-puzzle-piece', $itemData);
my $drag = Qt::Drag(this);
$drag->setMimeData($mimeData);
$drag->setHotSpot(Qt::Point($pixmap->width()/2, $pixmap->height()/2));
$drag->setPixmap($pixmap);
if ($drag->exec(Qt::MoveAction()) == Qt::MoveAction()) {
takeItem(row($item));
}
}
1;
|