-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEditor.qml
474 lines (441 loc) · 14.7 KB
/
Editor.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
import QtQuick 2.9
import Org.Syrja.Symbol.Operation 1.0
Rectangle
{
enum Tool
{
Select = 1,
Line = 11,
RectCenter = 21,
RectCorner = 22,
CircleCorner = 31,
CircleDiameter = 32,
CircleCenter = 33,
ArcSemi = 41,
ArcQuarter = 42,
TextHorizontal = 51,
TextRotated = 52
}
property int units: 100
property int max: units / 2
property int offsetx: 100
property int offsety: 50
property int totalx: units + offsetx * 2 // 300
property int totaly: units + offsety * 2 // 200
property bool horizontal: (height / 2 < width / 3)
property real scalexy: (horizontal ? height / totaly : width / totalx) * (preview ? zoommin : zoomscale)
property bool down: false
property int startx: 0
property int starty: 0
property int endx: 0
property int endy: 0
property string paintcolor: "black"
property string editcolor: "brown"
property string gridcolor: "gray"
property string backcolor: "white"
property string graycolor: "lightgray"
anchors.fill: parent
color: graycolor
MouseArea
{
anchors.fill: parent
hoverEnabled: true
onPositionChanged:
{
if ( tool === Editor.Tool.Select )
{
mousex = Math.round((mouse.x - canvas.x) / scalexy) - max - offsetx
mousey = max - Math.round((mouse.y - canvas.y) / scalexy) + offsety
}
else // snap and clip
{
mousex = Math.round((mouse.x - canvas.x) / scalexy / snapgrid) * snapgrid - max - offsetx
mousey = max - Math.round((mouse.y - canvas.y) / scalexy / snapgrid) * snapgrid + offsety
if ( mousex < -units )
mousex = -units
else if ( mousex > units )
mousex = units
if ( mousey < -units )
mousey = -units
else if ( mousey > units )
mousey = units
if ( down )
canvas.requestPaint()
}
}
onPressed:
{
startx = mousex
starty = mousey
manager.setActiveIndex(-1);
focus = true
down = true
}
onReleased:
{
down = false
endx = mousex
endy = mousey
if ( tool === Editor.Tool.Select )
{
manager.selectItem(Qt.point(endx, endy))
symbol = manager.getSymbol(true)
canvas.requestPaint()
}
else if ( !preview )
{
if ( tool === Editor.Tool.TextHorizontal )
{
if ( manager.addTextItem(Operation.Text, Qt.point(endx, endy), Qt.point(endx, endy), textvalue, textsize, sizeunit, colorindex, alignment) )
{
symbol = manager.getSymbol(true)
canvas.requestPaint()
}
}
else if ( startx != endx || starty != endy )
{
if ( tool === Editor.Tool.Line )
{
manager.addLineItem(Operation.Line, Qt.point(startx, starty), Qt.point(endx, endy), 0, colorindex, 0)
symbol = manager.getSymbol(true)
}
else if ( tool === Editor.Tool.RectCenter || tool === Editor.Tool.RectCorner )
{
if ( tool === Editor.Tool.RectCenter )
{
startx -= (endx - startx)
starty -= (endy - starty)
}
if ( manager.addLineItem(Operation.Rectangle, Qt.point(startx, starty), Qt.point(endx, endy), 0, colorindex, fillitem) )
symbol = manager.getSymbol(true)
}
else if ( tool > 30 && tool < 50 ) // circle or arc
{
var deltax = Math.abs(mousex - startx)
var deltay = Math.abs(mousey - starty)
var centerx, centery, radius
if ( tool === Editor.Tool.CircleCorner )
{
radius = (deltax < deltay ? deltax : deltay) / 2
centerx = (startx + endx) / 2
centery = (starty + endy) / 2
if ( manager.addPointItem(Operation.Circle, Qt.point(centerx, centery), radius, colorindex, fillitem) )
symbol = manager.getSymbol(true)
}
else if ( tool === Editor.Tool.CircleDiameter )
{
radius = Math.sqrt(deltax * deltax + deltay * deltay) / 2
centerx = (startx + endx) / 2
centery = (starty + endy) / 2
if ( manager.addPointItem(Operation.Circle, Qt.point(centerx, centery), radius, colorindex, fillitem) )
symbol = manager.getSymbol(true)
}
else if ( tool === Editor.Tool.CircleCenter )
{
radius = Math.sqrt(deltax * deltax + deltay * deltay)
if ( manager.addPointItem(Operation.Circle, Qt.point(startx, starty), radius, colorindex, fillitem) )
symbol = manager.getSymbol(true)
}
else if ( tool === Editor.Tool.ArcSemi )
{
radius = Math.sqrt(deltax * deltax + deltay * deltay) / 2
if ( manager.addLineItem(Operation.Arc, Qt.point(startx, starty), Qt.point(endx, endy), radius, colorindex, fillitem) )
symbol = manager.getSymbol(true)
}
else if ( tool === Editor.Tool.ArcQuarter )
{
radius = Math.sqrt(deltax * deltax + deltay * deltay)
//##
}
}
else if ( tool === Editor.Tool.TextRotated )
{
if ( manager.addTextItem(Operation.Text, Qt.point(startx, starty), Qt.point(endx, endy), textvalue, textsize, sizeunit, colorindex, alignment) )
symbol = manager.getSymbol(true)
}
}
else // activate last
{
manager.setActiveIndex(manager.getItemCount() - 1);
canvas.requestPaint()
}
}
}
}
Canvas
{
id: canvas
width: totalx * scalexy; height: totaly * scalexy
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
function paintline(context, start, end)
{
context.beginPath().moveTo((start.x + max + offsetx) * scalexy, (max - start.y + offsety) * scalexy)
context.lineTo((end.x + max + offsetx) * scalexy, (max - end.y + offsety) * scalexy).stroke()
}
function paintrect(context, start, size, fill)
{
context.beginPath().rect((start.x + max + offsetx) * scalexy, (max - start.y + offsety) * scalexy,
size.width * scalexy, size.height * scalexy)
if ( fill )
{
context.fillStyle = (fill === 3 ? graycolor : (fill === 2 ? paintcolor : backcolor))
context.fill()
}
else
context.stroke()
}
function paintcircle(context, center, radius, fill)
{
context.beginPath().ellipse((center.x + max - radius + offsetx) * scalexy,
(max - center.y - radius + offsety) * scalexy, radius * 2 * scalexy, radius * 2 * scalexy)
if ( fill )
{
context.fillStyle = (fill === 2 ? paintcolor : backcolor)
context.fill()
}
else
context.stroke()
}
function paintsemicircle(context, start, end, radius, fill)
{
// context.beginPath().moveTo((start.x + max + offsetx) * scalexy, (max - start.y + offsety) * scalexy)
context.beginPath().moveTo((0 + max + offsetx) * scalexy, (max - 0 + offsety) * scalexy)
context.arcTo((start.x + max + offsetx) * scalexy, (max - start.y + offsety) * scalexy,
(end.x + max + offsetx) * scalexy, (max - end.y + offsety) * scalexy, radius)
if ( fill )
{
context.fillStyle = (fill === 2 ? paintcolor : backcolor)
context.fill()
}
else
context.stroke()
}
function setalignment(context, align)
{
switch ( align )
{
case 1: context.textAlign = "right"; context.textBaseline = "top"; break
case 2: context.textAlign = "center"; context.textBaseline = "top"; break
case 3: context.textAlign = "left"; context.textBaseline = "top"; break
case 4: context.textAlign = "right"; context.textBaseline = "alphabetic"; break
case 5: context.textAlign = "center"; context.textBaseline = "alphabetic"; break
case 6: context.textAlign = "left"; context.textBaseline = "alphabetic"; break
case 7: context.textAlign = "right"; context.textBaseline = "bottom"; break
case 8: context.textAlign = "center"; context.textBaseline = "bottom"; break
case 9: context.textAlign = "left"; context.textBaseline = "bottom"; break
case 10: context.textAlign = "right"; context.textBaseline = "middle"; break
case 11: context.textAlign = "center"; context.textBaseline = "middle"; break
case 12: context.textAlign = "left"; context.textBaseline = "middle"; break
}
}
function setcolorindex(context, color)
{
switch ( color )
{
case 0: paintcolor = "white"; break
case 1: paintcolor = "black"; break
case 2: paintcolor = "blue"; break
case 3: paintcolor = "red"; break
case 4: paintcolor = "green"; break
case 5: paintcolor = "yellow"; break
default: paintcolor = "black"; break
}
}
function painttext(context, string, point, end, size, unit, active)
{
var currentwidth = context.lineWidth
size = (unit === 2 ? symbolsize * size / 10.0 : (size ? size : 2.5)) * (preview ? zoommin : zoomscale) / symbolsize
context.lineWidth = size * 10
var fontsize = 300 * size
context.font = fontsize.toString() + "px sans-serif"
var position = Qt.point((point.x + max + offsetx) * scalexy, (max - point.y + offsety) * scalexy)
context.translate(position.x, position.y)
if ( point.x !== end.x || point.y !== end.y ) // rotate
context.rotate(Math.atan2(point.y - end.y, end.x - point.x))
context.fillText(string, 0, 0)
context.strokeText(string, 0, 0)
if ( !preview ) // attach point
{
if ( active )
context.fillStyle = editcolor
context.beginPath().ellipse(zoomscale * -5 , zoomscale * -5, zoomscale * 10, zoomscale * 10).fill()
if ( active )
context.fillStyle = paintcolor
}
context.resetTransform()
context.lineWidth = currentwidth
}
function paintgrid(context)
{
context.lineWidth = 0.4
context.strokeStyle = gridcolor
if ( viewgrid && !preview )
{
var row, col;
for ( row = -max; row <= max; row += snapgrid )
paintline(context, Qt.point(-max, row), Qt.point(max, row))
for ( col = -max; col <= max; col += snapgrid )
paintline(context, Qt.point(col, -max), Qt.point(col, max))
context.lineWidth = 1
paintline(context, Qt.point(0, -max), Qt.point(0, max))
paintline(context, Qt.point(-max, 0), Qt.point(max, 0))
paintrect(context, Qt.point(-max, max), Qt.size(units, units), 0)
}
}
function paintsymbol(context)
{
setcolorindex(context, 1)
context.lineWidth = linewidth * (preview ? zoommin * 2 : zoomscale) * 2
var currentcolor = 1
var active = manager.getActiveIndex()
var count = manager.getItemCount()
var operation, index, radius, fill, color
var point, position
for ( index = 0; index < count; index++ )
{
operation = manager.getItemOperation(index)
position = manager.getItemPosition(index)
fill = manager.getItemFill(index)
color = manager.getItemColor(index)
if ( color !== currentcolor )
{
setcolorindex(context, color)
currentcolor = color
}
context.strokeStyle = (index === active && !preview ? editcolor : paintcolor)
context.fillStyle = paintcolor
if ( operation === Operation.Line )
{
point = manager.getItemPoint(index)
paintline(context, position, point)
}
else if ( operation === Operation.Rectangle )
{
point = manager.getItemPoint(index)
var deltax = point.x - position.x
var deltay = position.y - point.y
paintrect(context, position, Qt.size(deltax, deltay), fill)
if ( !preview && fill && index === active )
paintrect(context, position, Qt.size(deltax, deltay), 0)
}
else if ( operation === Operation.Circle )
{
radius = manager.getItemValue(index)
paintcircle(context, position, radius, fill)
if ( !preview && fill && index === active )
paintcircle(context, position, radius, false)
}
else if ( operation === Operation.Arc )
{
point = manager.getItemPoint(index)
radius = manager.getItemValue(index)
paintsemicircle(context, position, point, radius, fill)
if ( !preview && fill && index === active )
paintsemicircle(context, position, point, radius, false)
}
else if ( operation === Operation.Text )
{
var size = manager.getItemSize(index);
var unit = manager.getItemUnit(index);
point = manager.getItemPoint(index)
setalignment(context, manager.getItemAlign(index))
painttext(context, manager.getItemText(index), position, point, size, unit, index === active)
}
}
}
onPaint:
{
var context = getContext("2d")
context.lineCap = "round"
paintrect(context, Qt.point(-max - offsetx, max + offsety), Qt.size(totalx, totaly), 3)
paintsymbol(context)
if ( down )
{
setcolorindex(context, colorindex)
context.strokeStyle = editcolor
context.fillStyle = paintcolor
var cornerx = (mousex < startx ? mousex : startx)
var cornery = (mousey > starty ? mousey : starty)
var deltax = Math.abs(mousex - startx)
var deltay = Math.abs(mousey - starty)
var start = Qt.point(startx, starty)
var end = Qt.point(mousex, mousey)
if ( tool > 10 && tool < 20 ) // line
{
if ( tool === Editor.Tool.Line )
paintline(context, start, end)
}
else if ( tool > 20 && tool < 30 ) // rectangle
{
if ( tool === Editor.Tool.RectCenter )
{
cornerx = startx + (mousex < startx ? -deltax : -deltax)
cornery = starty + (mousey < starty ? deltay : deltay)
deltax *= 2; deltay *= 2
}
paintrect(context, Qt.point(cornerx, cornery), Qt.size(deltax, deltay), fillitem)
if ( fillitem )
paintrect(context, Qt.point(cornerx, cornery), Qt.size(deltax, deltay), 0)
}
else if ( tool > 30 && tool < 50 ) // circle or arc
{
var centerx, centery, radius
if ( tool === Editor.Tool.CircleCorner )
{
radius = (deltax < deltay ? deltax : deltay) / 2
centerx = (startx + mousex) / 2
centery = (starty + mousey) / 2
}
else if ( tool === Editor.Tool.CircleDiameter )
{
radius = Math.sqrt(deltax * deltax + deltay * deltay) / 2
centerx = (startx + mousex) / 2
centery = (starty + mousey) / 2
}
else if ( tool === Editor.Tool.CircleCenter )
{
radius = Math.sqrt(deltax * deltax + deltay * deltay)
centerx = startx
centery = starty
}
else if ( tool === Editor.Tool.ArcSemi )
{
radius = Math.sqrt(deltax * deltax + deltay * deltay) / 2
centerx = (startx + mousex) / 2
centery = (starty + mousey) / 2
}
if ( radius )
{
var center = Qt.point(centerx, centery)
if ( tool > 40 )
{
paintsemicircle(context, center, start, end, radius, fillitem)
// if ( fillitem )
// paintsemicircle(context, center, radius, false)
}
else
{
paintcircle(context, center, radius, fillitem)
if ( fillitem )
paintcircle(context, center, radius, false)
}
}
}
else if ( tool > 50 && tool < 60 ) // text
{
setalignment(context, alignment)
if ( tool === Editor.Tool.TextHorizontal )
painttext(context, textvalue, Qt.point(mousex, mousey), Qt.point(mousex, mousey), textsize, sizeunit, true)
else if ( tool === Editor.Tool.TextRotated )
painttext(context, textvalue, Qt.point(startx, starty), Qt.point(mousex, mousey), textsize, sizeunit, true)
}
}
paintgrid(context)
}
}
function update()
{
canvas.requestPaint()
}
}