import QtQuick 1.0Item { id: root width: 800 height: 600 property double x1: 0 property double y1: 0 property bool isSecondPoint: false function createSceneItem(x,y) { var component = Qt.createComponent("Item.qml") var object = component.createObject(root) object.x =x object.y =y object.myclick.connect(click) } function click(xPos, yPos){ console.log("click",xPos,yPos) if(isSecondPoint && (xPos!=x1 && yPos!=y1)) { drawLine(x1,y1,xPos,yPos) } else { x1 = xPos; y1 = yPos } isSecondPoint = !isSecondPoint } function drawLine(x1,y1,x2,y2) { var component = Qt.createComponent("Line.qml") var object = component.createObject(root) object.x1 = x1 object.y1 = y1 object.x2 = x2 object.y2 = y2 var alpha = Math.atan( (y2-y1)/(x2-x1)) * 180/ Math.PI alpha = (x2<x1) ? alpha+180 : alpha object.alpha = alpha } MouseArea{ anchors.fill: root acceptedButtons: Qt.RightButton | Qt.LeftButton onClicked: { createSceneItem( mouseX, mouseY) } }}
import QtQuick 1.0Item { id: item signal myclick(double xPos, double yPos) Rectangle{ id: back color: "lime" width: 100 height: 50 anchors.centerIn: item } MouseArea { id: mousearea anchors.fill: back drag.target: item drag.minimumX: 0; drag.maximumX: 800-back.width drag.minimumY: 0; drag.maximumY: 600-back.height onClicked: { click(item.x,item.y) } }}
import QtQuick 1.0Item { id: line property double x1: 0 property double y1: 0 property double x2: 0 property double y2: 0 property double alpha: 0 Rectangle { x: x1 y: y1 width: Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)) height: 3 color: "tomato" transform: Rotation { origin.x: x; origin.y: y; angle: alpha} }}
onXChanged: posChanged(item.num,item.x,item.y) onYChanged: posChanged(item.num,item.x,item.y)
import QtQuick 1.0Item { id: root width: 800 height: 600 signal newPos(int num, double x, double y) property int maxNum : 0 property int lastNum : 0 property double lastX : 0 property double lastY : 0 property bool isSecondPoint : false function createSceneItem(x,y) { var component = Qt.createComponent("Item.qml") var object = component.createObject(root) object.x =x object.y =y object.num = maxNum maxNum++ } function click(itemNum, xPos, yPos){ console.log("click",itemNum,xPos,yPos, isSecondPoint) if(isSecondPoint && (xPos!=lastX && yPos!=lastY)) { drawLine(lastX,lastY,lastNum, xPos,yPos, itemNum) } else { lastX = xPos; lastY = yPos; lastNum = itemNum } isSecondPoint = (!isSecondPoint) } function drawLine(x1,y1,num1,x2,y2,num2) { var component = Qt.createComponent("Line.qml") var object = component.createObject(root) object.x1 = x1 object.y1 = y1 object.num1 = num1 object.x2 = x2 object.y2 = y2 object.num2 = num2 newPos.connect(object.repaint) } function posChanged(itemNum,newX,newY) { newPos(itemNum,newX,newY) } MouseArea{ anchors.fill: root acceptedButtons: Qt.RightButton | Qt.LeftButton onClicked: { createSceneItem( mouseX, mouseY) } }}
import QtQuick 1.0Item { id: line property int num1: 0 property int num2: 0 property double x1: 0 property double y1: 0 property double x2: 0 property double y2: 0 Rectangle { x: x1 y: y1 width: Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)) height: 3 color: "tomato" transform: Rotation { origin.x: x; origin.y: y; angle: calcAlpha(x1,y1,x2,y2)} } function calcAlpha(x1,y1,x2,y2){ var alpha = Math.atan( (y2-y1)/(x2-x1)) * 180/ Math.PI alpha = (x2<x1) ? alpha+180 : alpha return alpha } function repaint(num,x,y){ if (num == num1) { x1 = x; y1 = y} else if (num == num2) { x2 = x; y2 = y} }}