-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDragArrow.qml
133 lines (113 loc) · 3.18 KB
/
DragArrow.qml
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import QtQuick 2.12
import QtQuick.Controls 1.4
DragItem {
id: arrow
property var originCoord: null
property var endCoord: null
name: "arrow"
action: "Fold"
Canvas {
id: canvas
anchors.fill: parent
antialiasing: true
z:20
property var path: []
property double angle: 0
property int arrowHeadLength: 75 //px
property int offset: 80
onPaint: {
var i = 0;
var p1 = {x: origin.x+origin.width/2,y:origin.y+origin.width/2}
var p2 = {x: end.x+origin.width/2,y:end.y+origin.width/2}
angle = -Math.atan2(p2.x-p1.x,p2.y-p1.y)+Math.PI/2
p2.x -= arrowHeadLength * Math.cos(angle);
p2.y -= arrowHeadLength * Math.sin(angle);
var ctx = canvas.getContext('2d');
ctx.reset();
ctx.lineJoin = "round"
ctx.lineCap="round";
ctx.lineWidth = 10;
ctx.strokeStyle = arrow.objColor;
ctx.fillStyle = arrow.objColor;
ctx.beginPath();
ctx.moveTo(p1.x, p1.y);
ctx.lineTo(p2.x, p2.y);
ctx.stroke();
ctx.beginPath();
ctx.translate(p2.x, p2.y);
ctx.rotate(angle);
ctx.lineTo(0, 20);
ctx.lineTo(arrowHeadLength, 0);
ctx.lineTo(0, - 20);
ctx.closePath();
ctx.fill();
}
}
function paint(){
checkSnap()
canvas.requestPaint()
}
function getPoints(){
return end.getCoord()//+'_'+origin.getCoord()
}
DragAnchor{
id: origin
objColor:"transparent"
center: originCoord
onUpdated: paint();
}
DragAnchor{
id: end
center:endCoord
onUpdated: {
paint()
}
onReleasedChanged: {
doSnap()
}
}
Component.onCompleted: {
paint()
doSnap()
doSnap()
}
function checkSnap(){
var dMin=Math.pow(end.x-origin.x,2)+Math.pow(end.y-origin.y,2)
for (var i=0;i<pois.children.length;i++){
var d = Math.pow(pois.children[i].x-origin.x,2)+Math.pow(pois.children[i].y-origin.y,2)
if(d < dMin){
dMin=d
snapTo(pois.children[i].x,pois.children[i].y)
snappedPoi = pois.children[i]
}
}
if(dMin === Math.pow(end.x-origin.x,2)+Math.pow(end.y-origin.y,2)){
snapTo(origin.x,origin.y)
snappedPoi=null
}
}
function doSnap(){
var d_x = snap.x-origin.x
var d_y = snap.y - origin.y
origin.x = snap.x
origin.y = snap.y
end.x += d_x
end.y += d_y
}
function selected(val){
currentItem = val
}
onCurrentItemChanged: {
if(currentItem){
objColor = Qt.lighter(objColor,1.3)
if(figures.currentItem !== null && figures.currentItem !== arrow)
figures.currentItem.selected(false)
figures.currentItem = arrow
paint()
}
else{
objColor = figures.colors[index]
paint()
}
}
}