aboutsummaryrefslogtreecommitdiffstats
path: root/examples/declarative/dragtarget/launcher/IconDelegate.qml
blob: 360ca8a9463c24376b1b0bb2771030c3831b99d7 (plain)
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
import QtQuick 2.0

Item {
    id: iconDelegate

    property alias drag: dragArea.drag

    width: 64; height: 64

    Rectangle {
        anchors.fill: parent
        opacity: dragArea.drag.active ? 0.5 : 0.0
        color: "lightgrey"
        radius: 12

        Behavior on opacity { NumberAnimation { duration: 300 } }
    }

    MouseArea {
        id: dragArea

        property real rootX
        property real rootY

        width: iconImage.implicitWidth; height:  iconImage.implicitHeight
        anchors { horizontalCenter: parent.horizontalCenter; verticalCenter: parent.verticalCenter }

        drag.target: dragArea
        drag.data: index

        Binding {
            target: dragArea
            property: "rootX"
            value: dragArea.mapToItem(root, x, y).x
            when: !dragArea.drag.active
        }
        Binding {
            target: dragArea
            property: "rootY"
            value: dragArea.mapToItem(root, x, y).y
            when: !dragArea.drag.active
        }

        Image {
            id: iconImage

            anchors { horizontalCenter: parent.horizontalCenter; verticalCenter: parent.verticalCenter }

            source: icon
        }

        states: State {
            when: dragArea.drag.active

            AnchorChanges {
                target: dragArea;
                anchors { horizontalCenter: undefined; verticalCenter: undefined }
            }
            ParentChange { target: dragArea; parent: root; x: dragArea.rootX; y:dragArea.rootY }
            AnchorChanges {
                target: iconImage;
                anchors { verticalCenter: undefined; bottom: dragArea.verticalCenter }
            }
        }

        transitions: Transition {
            AnchorAnimation { targets: iconImage; duration: 100 }
        }
    }
}