-
Notifications
You must be signed in to change notification settings - Fork 2
/
validation.go
295 lines (279 loc) · 8.07 KB
/
validation.go
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
package main
import (
"fmt"
"time"
"github.com/therecipe/qt/core"
"github.com/therecipe/qt/gui"
"github.com/therecipe/qt/widgets"
)
func ContainsItem(items map[Item]int, target Item) bool {
_, ok := items[target]
return ok
}
func GetItemName(item Item) string {
switch GetItemType(item) {
case TypeRequirement:
return "Problem"
case TypeSolution:
return "Solution"
}
return ""
}
// Validates link to check that links are not the same type
func ValidateLinks() (items []Item) {
// Final returned splice
items = make([]Item, 0)
// Items we have already added
added := map[Item]int{}
// Loop through all links
for _, link := range links {
// Loop through all links related to that item
for _, l := range link {
// Check if child has same item type and not already added
if GetItemType(l.parent) == GetItemType(l.child) && !ContainsItem(added, l.child) {
items = append(items, l.child)
added[l.child] = 0
}
}
}
return items
}
// Validates roots to check if they have a one-to-one relation
func ValidateRoots() (items []Item) {
// Final returned items
items = make([]Item, 0)
// Items we have already added
added := map[Item]int{}
// Loop through all items in view
for _, item := range view.Items() {
// Get group and make sure it's valid
group := item.Group()
if group == nil || group.Type() == 0 {
continue
}
// Get item
groupItem := GetGroupItem(group)
// Ignore if already added
if ContainsItem(added, groupItem) {
continue
}
added[groupItem] = 0
// Get children count and if it has a parent (not a root)
children := 0
hasParent := false
for _, link := range links[groupItem] {
if link.parent == groupItem {
children++
} else if link.child == groupItem {
hasParent = true
break
}
}
// If it had a parent, it's not a root
if hasParent {
continue
}
// Otherwise, add if it had more than one child
if children > 1 {
items = append(items, groupItem)
}
}
return items
}
// Validates tree to check if there are any links to each other
func ValidateLoops() (items []Item) {
// Final returned splice
items = make([]Item, 0)
// Items we have already added
added := map[Item]int{}
// Loop through all links
for _, link := range links {
// Loop through all links related to that item
for _, l1 := range link {
// Loop through all links again checking for opposite links
for _, l2 := range link {
// They are linked to each other
if l1.parent == l2.child && l1.child == l2.parent && !ContainsItem(added, l1.child) {
items = append(items, l1.child)
added[l1.child] = 0
}
}
}
}
return items
}
func ValidateLinkErrors() (items []Item) {
// Final returned items
items = make([]Item, 0)
// Items we have already added
added := map[Item]int{}
// Loop through all items in view
for _, item := range view.Items() {
// Get group and make sure it's valid
group := item.Group()
if group == nil || group.Type() == 0 {
continue
}
// Get item
groupItem := GetGroupItem(group)
// Ignore if already added
if ContainsItem(added, groupItem) {
continue
}
// Get children count and if it has a parent (not a root)
parents := 0
for _, link := range links[groupItem] {
if link.child == groupItem {
parents++
}
}
// If it has more than one parent, something is wrong
if parents > 1 {
items = append(items, groupItem)
added[groupItem] = 0
}
}
return items
}
type ValidationOption int8
const (
SameType ValidationOption = 0
OneRoot ValidationOption = 1
LinkLoop ValidationOption = 2
LinkError ValidationOption = 3
)
type ValidationResult string
const (
ValidateOK ValidationResult = "validate-ok"
ValidateFail ValidationResult = "validate-fail"
ValidateDisabled ValidationResult = "validate-disabled"
ValidateNone ValidationResult = "validate-none"
)
func CreateValidationResult(option ValidationOption, result ValidationResult) *widgets.QListWidgetItem {
var text, info string
switch option {
case SameType:
text = "Links to same type"
info = "Linking items of the same type"
case OneRoot:
text = "One-to-one root"
info = "Roots of the current tree having an one-to-one relation to its child"
case LinkLoop:
text = "Linking loop"
info = "Items that link to each other in a loop"
case LinkError:
text = "Invalid links"
info = "Link that could not be saved to the project file"
}
item := widgets.NewQListWidgetItem3(GetIcon(string(result)), text, nil, 0)
item.SetToolTip(info)
return item
}
func GetDefaultValidationResult(enabled bool) ValidationResult {
if enabled {
return ValidateNone
}
return ValidateDisabled
}
func GetValidationResult(itemCount int) ValidationResult {
if itemCount > 0 {
return ValidateFail
}
return ValidateOK
}
// CreateValidationEngineLayout creates the validation engine window
func CreateValidationEngineLayout() *widgets.QWidget {
// Enable all validations by default
// (this should maybe be loaded/saved from database)
enabled := []bool{
true, true, true, true,
}
// Main vertical box
layout := widgets.NewQVBoxLayout()
// List of validation results
results := widgets.NewQListWidget(nil)
// List of affected items
items := widgets.NewQListWidget(nil)
// Main container for validation results
title := CreateGroupBox("Last validation: never", results)
// Option to validate
runBtn := widgets.NewQPushButton2("Run now", nil)
runBtn.ConnectReleased(func() {
// Update text and disable all options
runBtn.SetText("Running...")
runBtn.SetEnabled(false)
results.SetEnabled(false)
items.SetEnabled(false)
// Empty list of affected items
items.Clear()
// Start validation timer
start := time.Now()
// Run link validation
if enabled[SameType] {
valLinks := ValidateLinks()
for _, item := range valLinks {
items.AddItem(fmt.Sprintf("%v %v\n(links to same type)", GetItemName(item), item.ID()))
}
results.Item(int(SameType)).SetIcon(GetIcon(string(GetValidationResult(len(valLinks)))))
}
// Run root validation
if enabled[OneRoot] {
valRoots := ValidateRoots()
for _, item := range valRoots {
items.AddItem(fmt.Sprintf("%v %v\n(one-to-one root)", GetItemName(item), item.ID()))
}
results.Item(int(OneRoot)).SetIcon(GetIcon(string(GetValidationResult(len(valRoots)))))
}
// Run loop validation
if enabled[LinkLoop] {
valLoops := ValidateLoops()
for _, item := range valLoops {
items.AddItem(fmt.Sprintf("%v %v\n(linking loop)", GetItemName(item), item.ID()))
}
results.Item(int(LinkLoop)).SetIcon(GetIcon(string(GetValidationResult(len(valLoops)))))
}
// Run invalid link validation
if enabled[LinkError] {
valErrors := ValidateLinkErrors()
for _, item := range valErrors {
items.AddItem(fmt.Sprintf("%v %v\n(invalid link)", GetItemName(item), item.ID()))
}
results.Item(int(LinkError)).SetIcon(GetIcon(string(GetValidationResult(len(valErrors)))))
}
// Enable them again
runBtn.SetText("Run now")
runBtn.SetEnabled(true)
results.SetEnabled(true)
items.SetEnabled(true)
// Set last validation time
title.SetTitle(fmt.Sprintf("Last validation: %v (%v ms)", time.Now().Format("15:04"), time.Now().Sub(start).Milliseconds()))
})
layout.AddWidget(runBtn, 0, 0)
// Create initial results
for i, e := range enabled {
results.AddItem2(CreateValidationResult(ValidationOption(i), GetDefaultValidationResult(e)))
}
layout.AddWidget(title, 0, 0)
// Show menu when clicking on result item
results.ConnectItemPressed(func(item *widgets.QListWidgetItem) {
i := results.Row(item)
menu := widgets.NewQMenu(nil)
action := menu.AddAction("Enabled")
action.SetCheckable(true)
action.SetChecked(enabled[i])
action.ConnectTriggered(func(checked bool) {
enabled[i] = action.IsChecked()
item.SetIcon(GetIcon(string(GetDefaultValidationResult(enabled[i]))))
})
menu.Popup(gui.QCursor_Pos(), nil)
})
// Create list to show affected items
itemGroup := CreateGroupBox("Affected Items", items)
layout.AddWidget(itemGroup, 1, 0)
// Convert layout to widget and return it
widget := widgets.NewQWidget(nil, core.Qt__Widget)
widget.SetLayout(layout)
widget.SetMaximumWidth(250)
widget.SetMinimumWidth(175)
return widget
}