itemCreation.js Example File
declarative/toys/dynamicscene/qml/dynamicscene/itemCreation.js
var itemComponent = null;
var draggedItem = null;
var startingMouse;
var posnInWindow;
function startDrag(mouse)
{
posnInWindow = paletteItem.mapToItem(window, 0, 0);
startingMouse = { x: mouse.x, y: mouse.y }
loadComponent();
}
function loadComponent() {
if (itemComponent != null) {
createItem();
return;
}
itemComponent = Qt.createComponent(paletteItem.componentFile);
if (itemComponent.status == Component.Loading)
component.statusChanged.connect(createItem);
else
createItem();
}
function createItem() {
if (itemComponent.status == Component.Ready && draggedItem == null) {
draggedItem = itemComponent.createObject(window, {"image": paletteItem.image, "x": posnInWindow.x, "y": posnInWindow.y, "z": 3});
} else if (itemComponent.status == Component.Error) {
draggedItem = null;
console.log("error creating component");
console.log(itemComponent.errorString());
}
}
function continueDrag(mouse)
{
if (draggedItem == null)
return;
draggedItem.x = mouse.x + posnInWindow.x - startingMouse.x;
draggedItem.y = mouse.y + posnInWindow.y - startingMouse.y;
}
function endDrag(mouse)
{
if (draggedItem == null)
return;
if (draggedItem.x + draggedItem.width > toolbox.x) {
draggedItem.destroy();
draggedItem = null;
} else {
draggedItem.created = true;
draggedItem = null;
}
}