summaryrefslogtreecommitdiffstats
path: root/examples/declarative/dynamic/qml/itemCreation.js
diff options
context:
space:
mode:
Diffstat (limited to 'examples/declarative/dynamic/qml/itemCreation.js')
-rw-r--r--examples/declarative/dynamic/qml/itemCreation.js65
1 files changed, 24 insertions, 41 deletions
diff --git a/examples/declarative/dynamic/qml/itemCreation.js b/examples/declarative/dynamic/qml/itemCreation.js
index 98d48a8..59750f3 100644
--- a/examples/declarative/dynamic/qml/itemCreation.js
+++ b/examples/declarative/dynamic/qml/itemCreation.js
@@ -1,80 +1,63 @@
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;
- }
-}
+var posnInWindow;
function startDrag(mouse)
{
- setSceneOffset();
+ posnInWindow = paletteItem.mapToItem(null, 0, 0);
startingMouse = { x: mouse.x, y: mouse.y }
loadComponent();
}
-//Creation is split into two functions due to an asyncronous wait while
+//Creation is split into two functions due to an asynchronous wait while
//possible external files are loaded.
function loadComponent() {
- if (itemComponent != null) //Already loaded the component
+ if (itemComponent != null) { // component has been previously loaded
createItem();
+ return;
+ }
- itemComponent = Qt.createComponent(itemButton.file);
- //console.log(itemButton.file)
- if(itemComponent.isLoading){
- component.statusChanged.connect(finishCreation);
- }else{//Depending on the content, it can be ready or error immediately
+ itemComponent = Qt.createComponent(paletteItem.componentFile);
+ if (itemComponent.status == Component.Loading) //Depending on the content, it can be ready or error immediately
+ component.statusChanged.connect(createItem);
+ else
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) {
+ if (itemComponent.status == Component.Ready && draggedItem == null) {
+ draggedItem = itemComponent.createObject(window);
+ draggedItem.image = paletteItem.image;
+ draggedItem.x = posnInWindow.x;
+ draggedItem.y = posnInWindow.y;
+ draggedItem.z = 3; // make sure created item is above the ground layer
+ } else if (itemComponent.status == Component.Error) {
draggedItem = null;
console.log("error creating component");
console.log(component.errorsString());
}
}
-function moveDrag(mouse)
+function continueDrag(mouse)
{
- if(draggedItem == null)
+ if (draggedItem == null)
return;
- draggedItem.x = mouse.x + xOffset - startingMouse.x;
- draggedItem.y = mouse.y + yOffset - startingMouse.y;
+ draggedItem.x = mouse.x + posnInWindow.x - startingMouse.x;
+ draggedItem.y = mouse.y + posnInWindow.y - startingMouse.y;
}
function endDrag(mouse)
{
- if(draggedItem == null)
+ if (draggedItem == null)
return;
- if(draggedItem.x + draggedItem.width > toolbox.x){ //Don't drop it in the toolbox
+ if (draggedItem.x + draggedItem.width > toolbox.x) { //Don't drop it in the toolbox
draggedItem.destroy();
draggedItem = null;
- }else{
- draggedItem.z = startingZ;
+ } else {
draggedItem.created = true;
draggedItem = null;
}