File: DragWidget.pm

package info (click to toggle)
qt4-perl 4.8.4-1.2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,636 kB
  • ctags: 8,100
  • sloc: perl: 42,963; cpp: 28,039; makefile: 160; xml: 98; sh: 4
file content (151 lines) | stat: -rw-r--r-- 4,086 bytes parent folder | download | duplicates (3)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
package DragWidget;

use strict;
use warnings;

use QtCore4;
use QtGui4;
use QtCore4::isa qw( Qt::Frame );

# [0]
sub NEW {
    my ( $class, $parent ) = @_;
    $class->SUPER::NEW( $parent );

    this->setMinimumSize(200, 200);
    this->setFrameStyle(Qt::Frame::Sunken() | Qt::Frame::StyledPanel());
    this->setAcceptDrops(1);

    my $boatIcon = Qt::Label(this);
    $boatIcon->setPixmap(Qt::Pixmap('images/boat.png'));
    $boatIcon->move(20, 20);
    $boatIcon->show();
    $boatIcon->setAttribute(Qt::WA_DeleteOnClose());

    my $carIcon = Qt::Label(this);
    $carIcon->setPixmap(Qt::Pixmap('images/car.png'));
    $carIcon->move(120, 20);
    $carIcon->show();
    $carIcon->setAttribute(Qt::WA_DeleteOnClose());

    my $houseIcon = Qt::Label(this);
    $houseIcon->setPixmap(Qt::Pixmap('images/house.png'));
    $houseIcon->move(20, 120);
    $houseIcon->show();
    $houseIcon->setAttribute(Qt::WA_DeleteOnClose());
}
# [0]

sub dragEnterEvent {
    my ($event) = @_;
    if ($event->mimeData()->hasFormat('application/x-dnditemdata')) {
        my $source = $event->source();
        if (defined $source && $source == this) {
            $event->setDropAction(Qt::MoveAction());
            $event->accept();
        } else {
            $event->acceptProposedAction();
        }
    } else {
        $event->ignore();
    }
}

sub dragMoveEvent {
    my ($event) = @_;
    if ($event->mimeData()->hasFormat('application/x-dnditemdata')) {
        my $source = $event->source();
        if (defined $source && $source == this) {
            $event->setDropAction(Qt::MoveAction());
            $event->accept();
        } else {
            $event->acceptProposedAction();
        }
    } else {
        $event->ignore();
    }
}

sub dropEvent {
    my ($event) = @_;
    if ($event->mimeData()->hasFormat('application/x-dnditemdata')) {
        my $itemData = $event->mimeData()->data('application/x-dnditemdata');
        my $dataStream = Qt::DataStream($itemData, Qt::IODevice::ReadOnly());
        
        my $pixmap = Qt::Pixmap();
        my $offset = Qt::Point();
        {
            no warnings qw(void); # For bitshift warning
            $dataStream >> $pixmap >> $offset;
        }

        my $newIcon = Qt::Label(this);
        $newIcon->setPixmap($pixmap);
        $newIcon->move($event->pos() - $offset);
        $newIcon->show();
        $newIcon->setAttribute(Qt::WA_DeleteOnClose());

        my $source = $event->source();
        if (defined $source && $source == this) {
            $event->setDropAction(Qt::MoveAction());
            $event->accept();
        } else {
            $event->acceptProposedAction();
        }
    } else {
        $event->ignore();
    }
}

# [1]
sub mousePressEvent {
    my ($event) = @_;
    my $child = this->childAt($event->pos());
    if (!$child) {
        return;
    }

    my $pixmap = $child->pixmap();

    my $itemData = Qt::ByteArray();
    my $dataStream = Qt::DataStream($itemData, Qt::IODevice::WriteOnly());
    {
        no warnings qw(void); # For bitshift warning
        $dataStream << $pixmap << Qt::Point($event->pos() - $child->pos());
    }
# [1]

# [2]
    my $mimeData = Qt::MimeData();
    $mimeData->setData('application/x-dnditemdata', $itemData);
# [2]
        
# [3]
    my $drag = Qt::Drag(this);
    $drag->setMimeData($mimeData);
    $drag->setPixmap($pixmap);
    $drag->setHotSpot($event->pos() - $child->pos());
# [3]

    # XXX Fix this.  Shared memory on the Pixmap causes $tempPixmap and $pixmap
    # to point to the same data.  The C++ code paints on the tempPixmap instead
    # of the pixmap.
    my $tempPixmap = Qt::Pixmap($pixmap);
    my $painter = Qt::Painter();
    $painter->begin($pixmap);
    $painter->fillRect($tempPixmap->rect(), Qt::Color(127,127,127,127));
    $painter->end();

    $child->setPixmap($pixmap);

    my $result = $drag->exec(Qt::CopyAction() | Qt::MoveAction(), Qt::CopyAction());
    if ($result == Qt::MoveAction()) {
        $child->close();
    }
    else {
        $child->show();
        $child->setPixmap($tempPixmap);
    }
}

1;