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
|
package DropArea;
use strict;
use warnings;
use QtCore4;
use QtGui4;
# [DropArea header part1]
use QtCore4::isa qw( Qt::Label );
use QtCore4::slots
clear => [];
use QtCore4::signals
changed => ['const QMimeData *'];
# [DropArea header part1]
use DropArea;
# [DropArea header part2]
sub label() {
return this->{label};
}
# [DropArea header part2]
# [DropArea constructor]
sub NEW
{
my ($class, $parent) = @_;
$class->SUPER::NEW($parent);
this->setMinimumSize(200, 200);
this->setFrameStyle(Qt::Frame::Sunken() | Qt::Frame::StyledPanel());
this->setAlignment(Qt::AlignCenter());
this->setAcceptDrops(1);
this->setAutoFillBackground(1);
this->clear();
}
# [DropArea constructor]
# [dragEnterEvent() function]
sub dragEnterEvent
{
my ($event) = @_;
this->setText(this->tr('<drop content>'));
this->setBackgroundRole(Qt::Palette::Highlight());
$event->acceptProposedAction();
emit this->changed($event->mimeData());
}
# [dragEnterEvent() function]
# [dragMoveEvent() function]
sub dragMoveEvent
{
my ($event) = @_;
$event->acceptProposedAction();
}
# [dragMoveEvent() function]
# [dropEvent() function part1]
sub dropEvent
{
my ($event) = @_;
my $mimeData = $event->mimeData();
# [dropEvent() function part1]
# [dropEvent() function part2]
if ($mimeData->hasImage()) {
this->setPixmap(Qt::Pixmap::fromImage($mimeData->imageData()->value()));
} elsif ($mimeData->hasHtml()) {
this->setText($mimeData->html());
this->setTextFormat(Qt::RichText());
} elsif ($mimeData->hasText()) {
this->setText($mimeData->text());
this->setTextFormat(Qt::PlainText());
} elsif ($mimeData->hasUrls()) {
my $urlList = $mimeData->urls();
my $text;
for (my $i = 0; $i < scalar @{$urlList} && $i < 32; ++$i) {
my $url = $urlList->[$i]->path();
$text += $url . "\n";
}
this->setText($text);
} else {
this->setText(this->tr('Cannot display data'));
}
# [dropEvent() function part2]
# [dropEvent() function part3]
this->setBackgroundRole(Qt::Palette::Dark());
$event->acceptProposedAction();
}
# [dropEvent() function part3]
# [dragLeaveEvent() function]
sub dragLeaveEvent
{
my ($event) = @_;
this->clear();
$event->accept();
}
# [dragLeaveEvent() function]
# [clear() function]
sub clear
{
this->setText(this->tr('<drop content>'));
this->setBackgroundRole(Qt::Palette::Dark());
emit this->changed(Qt::MimeData());
}
# [clear() function]
1;
|