-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectionArea.qml
144 lines (131 loc) · 5.15 KB
/
SelectionArea.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
import QtQuick 2.15
Rectangle {
id: selectionRect
property bool squared: false
property int rulerSize: 12
property color rulerColor: "steelblue"
property int minimumSize: 50
property int maximumSize: Math.min(parent.width, parent.height)
color: "#354682B4"
border {
width: 2
color: rulerColor
}
MouseArea {
id: dragMouseArea
anchors.fill: parent
drag{
target: parent
minimumX: 0
minimumY: 0
maximumX: parent.parent.width - parent.width
maximumY: parent.parent.height - parent.height
smoothed: true
}
onDoubleClicked: {
parent.destroy() // destroy component
}
}
Repeater {
id: repeater
//TODO: optimize this code
readonly property var actions: {
"left" : function(x, y) {
selectionRect.width = selectionRect.width - x
selectionRect.x = selectionRect.x + x
if(selectionRect.width < minimumSize)
selectionRect.width = minimumSize
if(selectionRect.width > maximumSize)
selectionRect.width = maximumSize
if(squared) {
selectionRect.height = selectionRect.width
}
},
"right" : function(x, y) {
selectionRect.width = selectionRect.width + x
if(selectionRect.width < minimumSize)
selectionRect.width = minimumSize
if(selectionRect.width > maximumSize)
selectionRect.width = maximumSize
if(squared) {
selectionRect.height = selectionRect.width
}
},
"top" : function(x, y) {
selectionRect.height = selectionRect.height - y
selectionRect.y = selectionRect.y + y
if(selectionRect.height < minimumSize)
selectionRect.height = minimumSize
if(selectionRect.height > maximumSize)
selectionRect.height = maximumSize
if(squared) {
selectionRect.width = selectionRect.height
}
},
"bottom" : function(x, y) {
selectionRect.height = selectionRect.height + y
if(selectionRect.height < minimumSize)
selectionRect.height = minimumSize
if(selectionRect.height > maximumSize)
selectionRect.height = maximumSize
if(squared) {
selectionRect.width = selectionRect.height
}
},
"lt" : function(x, y) {
repeater.actions["left"](x, y);
if(!squared) {
repeater.actions["top"](x, y);
}
},
"lb" : function(x, y) {
repeater.actions["left"](x, y);
if(!squared) {
repeater.actions["bottom"](x, y);
}
},
"rt" : function(x, y) {
repeater.actions["right"](x, y);
if(!squared) {
repeater.actions["top"](x, y);
}
},
"rb" : function(x, y) {
repeater.actions["right"](x, y);
if(!squared) {
repeater.actions["bottom"](x, y);
}
},
}
model: [
// edge rulers
{ horizontal: parent.left, vertical: parent.verticalCenter, axis: Drag.XAxis, callback: "left" },
{ horizontal: parent.right, vertical: parent.verticalCenter, axis: Drag.XAxis, callback: "right" },
{ horizontal: parent.horizontalCenter, vertical: parent.top, axis: Drag.YAxis, callback: "top" },
{ horizontal: parent.horizontalCenter, vertical: parent.bottom, axis: Drag.YAxis, callback: "bottom" },
// corner rulers
{ horizontal: parent.left, vertical: parent.top, axis: Drag.YAxis | Drag.XAxis, callback: "lt" },
{ horizontal: parent.left, vertical: parent.bottom, axis: Drag.YAxis | Drag.XAxis, callback: "lb" },
{ horizontal: parent.right, vertical: parent.top, axis: Drag.YAxis | Drag.XAxis, callback: "rt" },
{ horizontal: parent.right, vertical: parent.bottom, axis: Drag.YAxis | Drag.XAxis, callback: "rb" },
]
delegate: Rectangle {
width: rulerSize
height: rulerSize
radius: rulerSize
color: rulerColor
anchors.horizontalCenter: modelData.horizontal
anchors.verticalCenter: modelData.vertical
MouseArea {
anchors.fill: parent
drag{ target: parent; axis: modelData.axis }
onPositionChanged: {
if(drag.active) {
if(typeof repeater.actions[modelData.callback] === "function")
repeater.actions[modelData.callback](mouseX, mouseY)
}
}
}
}
}// Repeater
}