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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import QtQuick.Shapes
Rectangle {
id: root
width: 1024
height: 768
color: palette.window
RowLayout {
id: topRow
x: 20
y: 10
spacing: 20
ButtonGroup {
id: toolButtons
buttons: drawingTools.children
}
RowLayout {
id: drawingTools
ToolButton {
text: qsTr("Line")
checkable: true
checked: true
property Component shapeType: Component {
ShapePath {
id: lineShapePath
strokeColor: root.palette.windowText
strokeWidth: widthSlider.value
fillColor: "transparent"
PathLine {
id: pathSegment
x: lineShapePath.startX + 1
y: lineShapePath.startY + 1
}
function finishCreation() {
createStartEndHandles(this, pathSegment);
}
}
}
}
ToolButton {
text: qsTr("Quadratic")
checkable: true
property Component shapeType: Component {
ShapePath {
id: quadShapePath
strokeColor: root.palette.windowText
strokeWidth: widthSlider.value
fillColor: fillSwitch.checked ? "green" : "transparent"
PathQuad {
id: pathSegment
x: quadShapePath.startx + 1
y: quadShapePath.startY + 1
controlX: quadShapePath.startX + 50
controlY: quadShapePath.startY + 50
}
function finishCreation() {
createStartEndHandles(this, pathSegment);
pointDragHandle.createObject(canvas, {
idleColor: "blue",
target: pathSegment, xprop: "controlX", yprop: "controlY"
});
}
}
}
}
ToolButton {
text: qsTr("Cubic")
checkable: true
property Component shapeType: Component {
ShapePath {
id: cubicShapePath
strokeColor: root.palette.windowText
strokeWidth: widthSlider.value
fillColor: fillSwitch.checked ? "green" : "transparent"
PathCubic {
id: pathSegment
x: cubicShapePath.startX + 1
y: cubicShapePath.startY + 1
control1X: cubicShapePath.startX + 50;
control1Y: cubicShapePath.startY + 50;
control2X: cubicShapePath.startX + 150;
control2Y: cubicShapePath.startY + 50;
}
function finishCreation() {
createStartEndHandles(this, pathSegment);
pointDragHandle.createObject(canvas, {
idleColor: "blue",
target: pathSegment, xprop: "control1X", yprop: "control1Y"
});
pointDragHandle.createObject(canvas, {
idleColor: "lightBlue",
target: pathSegment, xprop: "control2X", yprop: "control2Y"
});
}
}
}
}
}
Label {
text: qsTr("Width")
}
Slider {
id: widthSlider
from: 1
to: 60
value: 4
}
Switch {
id: showHandlesSwitch
text: qsTr("Handles")
}
Switch {
id: fillSwitch
text: qsTr("Fill")
}
}
Component {
id: pointDragHandle
Rectangle {
id: rect
property variant target
property string xprop
property string yprop
property color idleColor: "red"
width: 20
height: width
visible: showHandlesSwitch.checked
color: hh.hovered ? "yellow" : idleColor
border.color: "grey"
property real halfWidth: width / 2
property bool complete: false
Binding { target: rect.target; property: xprop; value: x + halfWidth; when: complete }
Binding { target: rect.target; property: yprop; value: y + halfWidth; when: complete }
DragHandler { }
HoverHandler { id: hh }
Component.onCompleted: {
x = target[xprop] - halfWidth;
y = target[yprop] - halfWidth;
complete = true;
}
}
}
function createStartEndHandles(shapePath, pathSegment) {
pointDragHandle.createObject(canvas, {
idleColor: "red",
target: shapePath, xprop: "startX", yprop: "startY"
});
pointDragHandle.createObject(canvas, {
idleColor: "red",
target: pathSegment, xprop: "x", yprop: "y"
});
}
Rectangle {
id: canvas
color: palette.base
width: root.width - 40
height: root.height - y - 20
x: 20
anchors.top: topRow.bottom
anchors.topMargin: 20
DragHandler {
target: null
grabPermissions: DragHandler.TakeOverForbidden
property ShapePath activePath: null
onActiveChanged: {
const tool = toolButtons.checkedButton;
if (active) {
activePath = tool.shapeType.createObject(root, {
startX: centroid.position.x, startY: centroid.position.y
});
shape.data.push(activePath);
} else {
activePath.finishCreation();
activePath = null;
}
}
onCentroidChanged: if (activePath) {
var pathObj = activePath.pathElements[0];
pathObj.x = centroid.position.x;
pathObj.y = centroid.position.y;
}
}
Shape {
id: shape
anchors.fill: parent
}
}
}
|