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

Item {
    id: iconDelegate

    property alias drag: dragArea.drag

    width: 64; height: 64

    Shadow {
        anchors.centerIn: parent
        sourceItem: iconImage
        width: iconImage.width
        height: iconImage.height
    }

    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: iconDelegate.VisualItemModel.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 }
        }

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