-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.qml
197 lines (132 loc) · 7.03 KB
/
main.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
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
Window {
id: mainWindow
visible: true
width: 1000
height: 600
title: qsTr("Simple ER Diagram with QML")
// Dynamic properties
property int counter : 0
property var entityRectangleMap : new Map() // Holds the created EntityRectangle
property var entityDeleteMap : new Map() // Holds the created EntityDelete
property var entityLineMap : new Map() // Holds the created EntityLine
property var frontEntityCoordinatesMap : new Map() // Holds the top-left corner coordinates of the EntityRectangle
property var rearEntityCoordinatesMap : new Map() // Holds the top-right corner coordinates of the EntityRectangle
property var entityRectangle : Qt.createComponent("EntityRectangle.qml");
property var entityDelete : Qt.createComponent("EntityDelete.qml")
property var entityLine : Qt.createComponent("EntityLine.qml")
// Static properties
property int entityRectangleWidth: 50
property int entityRectangleHeight: 30
property int initialDistance: 50
property int initialEntityRectangleX : 5
property int initialEntityRectangleY : 50
// Dynamically add entity to the window
// and join to the previous entity
function addEntity(){
let frontCoordinates = {x: 0, y: 0}
let rearCoordinates = {x: 0, y: 0}
let prevEntityRectangleFrontCoordinates = {x: 0, y: 0}
let prevEntityRectangleRearCoordinates = {x: 0, y: 0}
let x1 = 0 // entityLine start point
let x2 = 0 // entityLine end point
let y1 = 0
let y2 = 0
let prevCounter = counter
counter++
if(counter === 1){
// Create first entityRectangle
entityRectangleMap.set(counter, entityRectangle.createObject(mainWindow, {x:initialEntityRectangleX, y: initialEntityRectangleY, height: entityRectangleHeight, width: entityRectangleWidth, objectName : counter}))
// Enter front and rear coordinates
// Save in map
frontCoordinates = {x: initialEntityRectangleX, y: initialEntityRectangleY}
rearCoordinates = {x: initialEntityRectangleX + entityRectangleWidth, y: initialEntityRectangleY}
frontEntityCoordinatesMap.set(counter, frontCoordinates)
rearEntityCoordinatesMap.set(counter, rearCoordinates)
} else{
prevEntityRectangleFrontCoordinates = frontEntityCoordinatesMap.get(prevCounter)
prevEntityRectangleRearCoordinates = rearEntityCoordinatesMap.get(prevCounter)
// Enter front and rear coordinates
// Save in map
frontCoordinates = {x: prevEntityRectangleRearCoordinates.x + initialDistance, y: prevEntityRectangleRearCoordinates.y}
rearCoordinates = {x: prevEntityRectangleRearCoordinates.x + initialDistance + entityRectangleWidth, y: prevEntityRectangleRearCoordinates.y}
frontEntityCoordinatesMap.set(counter, frontCoordinates)
rearEntityCoordinatesMap.set(counter, rearCoordinates)
entityRectangleMap.set(counter, entityRectangle.createObject(mainWindow, {x: frontEntityCoordinatesMap.get(counter).x, y: frontEntityCoordinatesMap.get(counter).y, height: entityRectangleHeight, width: entityRectangleWidth, objectName : counter}))
entityDeleteMap.set(counter, entityDelete.createObject(mainWindow, {x: frontEntityCoordinatesMap.get(counter).x, y: frontEntityCoordinatesMap.get(counter).y, objectName : counter}))
x1 = prevEntityRectangleRearCoordinates.x
x2 = frontEntityCoordinatesMap.get(counter).x
y1 = frontEntityCoordinatesMap.get(counter).y + entityRectangleHeight / 2
y2 = frontEntityCoordinatesMap.get(counter).y + entityRectangleHeight / 2
entityLineMap.set(counter, entityLine.createObject(mainWindow, {x1: x1, y1: y1, x2: x2, y2: y2, objectName : counter}))
// Dynamically connect `deleteEntitySignal` with `deleteEntitySlot()`
// This will listen for delete entity signals
entityDeleteMap.get(counter).deleteEntitySignal.connect(mainWindow.deleteEntitySlot)
}
// Dynamically connect `entityRectangleMovedSignal` with `entityRectangleMovedSlot()`
// This will listen for move entity signals
entityRectangleMap.get(counter).entityRectangleMovedSignal.connect(mainWindow.entityRectangleMovedSlot)
}
// Delete an entity relationship
// when a delete signal is received by deleteEntity() slot
function deleteEntitySlot(refObj){
// Delete objects
entityRectangleMap.get(refObj).destroy()
entityDeleteMap.get(refObj).destroy()
entityLineMap.get(refObj).destroy()
// Delete all relevant data
entityRectangleMap.delete(refObj)
entityDeleteMap.delete(refObj)
entityLineMap.delete(refObj)
frontEntityCoordinatesMap.delete(refObj)
rearEntityCoordinatesMap.delete(refObj)
// Check if the refObj is not the last element
// If not, then delete the entities behind
if(refObj < counter){
for(var i = refObj + 1; i <= counter ; i++){
entityRectangleMap.get(i).destroy()
entityDeleteMap.get(i).destroy()
entityLineMap.get(i).destroy()
entityRectangleMap.delete(i)
entityDeleteMap.delete(i)
entityLineMap.delete(i)
frontEntityCoordinatesMap.delete(i)
rearEntityCoordinatesMap.delete(i)
}
}
// Set counter to the new value
const newValue = refObj - 1
counter = newValue <= 1 ? 1 : newValue
}
// Update entityLines and entityDelete on receiving signal
function entityRectangleMovedSlot(refObj, newFrontXCoord, newFrontYCoord){
// Ignore the front lineEntity & deleteEntity for the first entityRectangle
if(refObj > 1){
entityDeleteMap.get(refObj).x = newFrontXCoord
entityDeleteMap.get(refObj).y = newFrontYCoord
entityLineMap.get(refObj).x2 = newFrontXCoord
entityLineMap.get(refObj).y2 = newFrontYCoord + entityRectangleHeight/2
}
// Ignore the rear lineEntity for the last entityRectangle
if(refObj < counter){
entityLineMap.get(refObj + 1).x1 = newFrontXCoord + entityRectangleWidth
entityLineMap.get(refObj + 1).y1 = newFrontYCoord + entityRectangleHeight/2
}
// Update front and rear coordinates of rectangleEntity
frontEntityCoordinatesMap.get(refObj).x = newFrontXCoord
frontEntityCoordinatesMap.get(refObj).y = newFrontYCoord
rearEntityCoordinatesMap.get(refObj).x = newFrontXCoord + entityRectangleWidth
rearEntityCoordinatesMap.get(refObj).y = newFrontYCoord
}
Button{
id: addBtn
text: qsTr("Add New Entity")
anchors.left: parent.left
anchors.top: parent.top
anchors.topMargin: 5
anchors.leftMargin: 5
onClicked: addEntity()
}
}