summaryrefslogtreecommitdiffstats
path: root/examples/declarative/dynamic/qml/itemCreation.js
blob: ccc03aa37e30df3d3c81275b3abf871b0039e2fc (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
71
72
73
74
75
76
77
78
79
80
81
82
var itemComponent = null;
var draggedItem = null;
var startingMouse;
var startingZ;
//Until QT-2385 is resolved we need to convert to scene coordinates manually
var xOffset;
var yOffset;
function setSceneOffset()
{
    xOffset = 0;
    yOffset = 0;
    var p = itemButton;
    while(p != window){
        xOffset += p.x;
        yOffset += p.y;
        p = p.parent;
    }
}

function startDrag(mouse)
{
    setSceneOffset();
    startingMouse = { x: mouse.x, y: mouse.y }
    loadComponent();
}

//Creation is split into two functions due to an asyncronous wait while
//possible external files are loaded.

function loadComponent() {
    if (itemComponent != null) //Already loaded the component
        createItem();

    itemComponent = createComponent(itemButton.file);
    //print(itemButton.file)
    if(itemComponent.isLoading){
        component.statusChanged.connect(finishCreation);
    }else{//Depending on the content, it can be ready or error immediately
        createItem();
    }
}

function createItem() {
    if (itemComponent.isReady && draggedItem == null) {
        draggedItem = itemComponent.createObject();
        draggedItem.parent = window;
        draggedItem.image = itemButton.image;
        draggedItem.x = xOffset;
        draggedItem.y = yOffset;
        startingZ = draggedItem.z;
        draggedItem.z = 4;//On top
    } else if (itemComponent.isError) {
        draggedItem = null;
        print("error creating component");
        print(component.errorsString());
    }
}

function moveDrag(mouse)
{
    if(draggedItem == null)
        return;

    draggedItem.x = mouse.x + xOffset - startingMouse.x;
    draggedItem.y = mouse.y + yOffset - startingMouse.y;
}

function endDrag(mouse)
{
    if(draggedItem == null)
        return;

    if(draggedItem.x + draggedItem.width > toolbox.x){ //Don't drop it in the toolbox
        draggedItem.destroy();
        draggedItem = null;
    }else{
        draggedItem.z = startingZ;
        draggedItem.created = true;
        draggedItem = null;
    }
}