Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / examples / declarative / toys / dynamicscene / qml / itemCreation.js
1 var itemComponent = null;
2 var draggedItem = null;
3 var startingMouse;
4 var posnInWindow;
5
6 function startDrag(mouse)
7 {
8     posnInWindow = paletteItem.mapToItem(window, 0, 0);
9     startingMouse = { x: mouse.x, y: mouse.y }
10     loadComponent();
11 }
12
13 //Creation is split into two functions due to an asynchronous wait while
14 //possible external files are loaded.
15
16 function loadComponent() {
17     if (itemComponent != null) { // component has been previously loaded
18         createItem();
19         return;
20     }
21
22     itemComponent = Qt.createComponent(paletteItem.componentFile);
23     if (itemComponent.status == Component.Loading)  //Depending on the content, it can be ready or error immediately
24         component.statusChanged.connect(createItem);
25     else    
26         createItem();
27 }
28
29 function createItem() {
30     if (itemComponent.status == Component.Ready && draggedItem == null) {
31         draggedItem = itemComponent.createObject(window, {"image": paletteItem.image, "x": posnInWindow.x, "y": posnInWindow.y, "z": 3});
32         // make sure created item is above the ground layer
33     } else if (itemComponent.status == Component.Error) {
34         draggedItem = null;
35         console.log("error creating component");
36         console.log(itemComponent.errorString());
37     }
38 }
39
40 function continueDrag(mouse)
41 {
42     if (draggedItem == null)
43         return;
44
45     draggedItem.x = mouse.x + posnInWindow.x - startingMouse.x;
46     draggedItem.y = mouse.y + posnInWindow.y - startingMouse.y;
47 }
48
49 function endDrag(mouse)
50 {
51     if (draggedItem == null)
52         return;
53
54     if (draggedItem.x + draggedItem.width > toolbox.x) { //Don't drop it in the toolbox
55         draggedItem.destroy();
56         draggedItem = null;
57     } else {
58         draggedItem.created = true;
59         draggedItem = null;
60     }
61 }
62