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
|
package SourceWidget;
use strict;
use warnings;
use QtCore4;
use QtGui4;
use QtCore4::isa qw( Qt::Widget );
#[0]
use QtCore4::slots
createData => ['QString'],
startDrag => [];
#[0]
use MimeData;
sub NEW {
shift->SUPER::NEW();
my $imageFile = Qt::File('images/example.svg');
$imageFile->open(Qt::IODevice::ReadOnly());
my $imageData = $imageFile->readAll();
this->{imageData} = $imageData;
$imageFile->close();
my $imageArea = Qt::ScrollArea();
my $imageLabel = Qt::SvgWidget();
this->{imageLabel} = $imageLabel;
$imageLabel->renderer()->load($imageData);
$imageArea->setWidget($imageLabel);
#imageLabel->setMinimumSize(imageLabel->renderer()->viewBox()->size());
my $instructTopLabel = Qt::Label(this->tr('This is an SVG drawing:'));
my $instructBottomLabel = Qt::Label(
this->tr('Drag the icon to copy the drawing as a PNG file:'));
my $dragIcon = Qt::PushButton(this->tr('Export'));
$dragIcon->setIcon(Qt::Icon('images/drag.png'));
this->connect($dragIcon, SIGNAL 'pressed()', this, SLOT 'startDrag()');
my $layout = Qt::GridLayout();
$layout->addWidget($instructTopLabel, 0, 0, 1, 2);
$layout->addWidget($imageArea, 1, 0, 2, 2);
$layout->addWidget($instructBottomLabel, 3, 0);
$layout->addWidget($dragIcon, 3, 1);
this->setLayout($layout);
this->setWindowTitle(this->tr('Delayed Encoding'));
}
#[1]
sub createData {
my ($mimeType) = @_;
if ($mimeType ne 'image/png') {
return;
}
my $imageLabel = this->{imageLabel};
my $image = Qt::Image($imageLabel->size(), Qt::Image::Format_RGB32());
my $painter = Qt::Painter();
$painter->begin($image);
$imageLabel->renderer()->render($painter);
$painter->end();
my $data = Qt::ByteArray();
my $buffer = Qt::Buffer($data);
$buffer->open(Qt::IODevice::WriteOnly());
$image->save($buffer, 'PNG');
$buffer->close();
my $mimeData = this->{mimeData};
$mimeData->setData('image/png', $data);
}
#[1]
#[0]
sub startDrag {
my $mimeData = MimeData();
this->{mimeData} = $mimeData;
this->connect($mimeData, SIGNAL 'dataRequested(QString)',
this, SLOT 'createData(QString)', Qt::DirectConnection());
my $drag = Qt::Drag(this);
$drag->setMimeData($mimeData);
$drag->setPixmap(Qt::Pixmap('images/drag.png'));
$drag->exec(Qt::CopyAction());
}
#[0]
1;
|