-
Notifications
You must be signed in to change notification settings - Fork 2
/
mainview.go
612 lines (584 loc) · 18 KB
/
mainview.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
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
package main
import (
"fmt"
"github.com/therecipe/qt/core"
"github.com/therecipe/qt/gui"
"github.com/therecipe/qt/widgets"
"os"
"path/filepath"
"strings"
)
type Link struct {
parent Item
child Item
line *widgets.QGraphicsLineItem
dir *widgets.QGraphicsPolygonItem
}
func (link Link) SetChildItem(child Item) {
childItemID := core.NewQVariant1(child.ID())
childItemType := core.NewQVariant1(int(GetItemType(child)))
link.line.SetData(0, childItemID)
link.line.SetData(1, childItemType)
link.dir.SetData(0, childItemID)
link.dir.SetData(1, childItemType)
}
var links map[Item][]*Link
var view *widgets.QGraphicsView
var scene *widgets.QGraphicsScene
var backgroundColor *gui.QColor
// Items opened in an edit window
var openItems map[Item]*widgets.QDockWidget
func IsItemOpen(item Item) bool {
_, ok := openItems[item]
return ok
}
func CloseItem(item Item) {
delete(openItems, item)
}
func ReloadProject(window *widgets.QMainWindow) {
// Make sure view is enabled
view.SetEnabled(true)
// Delete all current items
scene.Clear()
// Clear links
links = make(map[Item][]*Link)
// Close all open items
for id := range openItems {
openItems[id].Close()
CloseItem(id)
}
// Connect to database to get new items
db := currentProject.Data()
defer db.Close()
// Load items
items, err := db.Items()
if err != nil {
fmt.Println("error: failed to get saved items:", err)
} else {
var x, y, w, h int
for item, description := range items {
x, y = item.Pos()
w, h = item.Size()
scene.AddItem(NewGraphicsItem(description, x, y, w, h, item))
}
}
// Load links
links, err := db.Links()
if err != nil {
fmt.Println("error: failed to get saved links:", err)
} else {
for child, parent := range links {
// Find parent and child
var parentItem, childItem *widgets.QGraphicsItemGroup
for _, item := range view.Items() {
group := item.Group()
if group == nil {
continue
}
groupID := group.Data(0).ToLongLong(nil)
groupType := ItemType(group.Data(1).ToInt(nil))
if groupID == 0 || groupType == 0 {
continue
}
if groupID == child.ID() && groupType == GetItemType(child) {
childItem = group
} else if groupID == parent.ID() && groupType == GetItemType(parent) {
parentItem = group
}
// Stop loop if we found everything
if parentItem != nil && childItem != nil {
break
}
}
// Create and add link
if parentItem == nil || childItem == nil {
fmt.Printf("warning: could not find parent or child, ignoring link (%3v(%v)%v -> %3v(%v)%v)\n",
parent.ID(), GetItemType(parent), parentItem != nil, child.ID(), GetItemType(child), childItem != nil)
} else {
link := CreateLink(parentItem, childItem)
scene.AddItem(link.line)
scene.AddItem(link.dir)
}
}
}
// Set window title
UpdateWindowTitle(window)
}
func UpdateWindowTitle(window *widgets.QMainWindow) {
abs, err := filepath.Abs(currentProject.path)
if err != nil {
fmt.Fprintln(os.Stderr, "warning: failed to get absolute path to project:", err)
abs = currentProject.path
}
window.SetWindowTitle(fmt.Sprintf("%v [%v] - OpenRQ", currentProject.Data().ProjectName(), abs))
// Update last used project
// (should probably not be done here)
NewSettings().SetLastProject(abs)
}
// SnapToGrid naps the specified position to the grid
func SnapToGrid(pos *core.QPoint) *core.QPoint {
// 2^5=32
const gridSize = 5
scenePos := view.MapToScene(pos).ToPoint()
return view.MapFromScene(core.NewQPointF3(
float64((scenePos.X()>>gridSize<<gridSize)-64), float64((scenePos.Y()>>gridSize<<gridSize)-32)))
}
func CreateEditWidgetFromPos(pos core.QPoint_ITF, scene *widgets.QGraphicsScene) (*widgets.QDockWidget, bool) {
// Get UID
group := view.ItemAt(pos).Group()
item := GetGroupItem(group)
// Check if already opened
if IsItemOpen(item) {
// We probably want to put it in focus here or something
return nil, false
}
// Open item
editWindow := CreateEditWidget(item, group, scene)
editWindow.ConnectCloseEvent(func(event *gui.QCloseEvent) {
CloseItem(item)
})
// Set item as being opened
openItems[item] = editWindow
// Return new window
return editWindow, true
}
func CreateView(window *widgets.QMainWindow, linkBtn *widgets.QToolButton) *widgets.QGraphicsView {
// Create scene and view
scene = widgets.NewQGraphicsScene(nil)
view = widgets.NewQGraphicsView2(scene, nil)
// Get default window background color
backgroundColor = window.Palette().Color2(window.BackgroundRole())
// Create open items map
openItems = make(map[Item]*widgets.QDockWidget)
// Check if we have a last loaded project
if project := NewSettings().LastProject(); project != "" {
NewProject(project)
ReloadProject(window)
} else {
// No recent project, show message
font := gui.NewQFont()
font.SetPointSize(18)
text := scene.AddText("No Project Loaded", font)
text.SetX(float64(view.Width() / 2) + (scene.Width() / 2.0))
text.SetY(float64(view.Height() / 2) + scene.Height())
scene.SetSceneRect2(0, 0, float64(view.Width()), float64(view.Height()))
view.SetEnabled(false)
}
// Setup drag-and-drop
view.SetAcceptDrops(true)
view.SetAlignment(core.Qt__AlignTop | core.Qt__AlignLeft)
view.ConnectDragMoveEvent(func(event *gui.QDragMoveEvent) {
if event.Source() != nil && event.Source().IsWidgetType() {
event.AcceptProposedAction()
}
})
// What item we're currently moving, if any
var movingItem *widgets.QGraphicsItemGroup
// Start position of link
var linkStart *widgets.QGraphicsItemGroup
// Temporary line shown when creating a new link
var tempLink *widgets.QGraphicsLineItem
itemSize := 64
view.ConnectDropEvent(func(event *gui.QDropEvent) {
pos := view.MapToScene(event.Pos())
// Add item to database
// For now, we assume all items are requirements
db := currentProject.Data()
defer db.Close()
uid, err := db.AddEmptyRequirement()
if err != nil {
widgets.QMessageBox_Warning(
window, "Failed to add item", err.Error(),
widgets.QMessageBox__Ok, widgets.QMessageBox__NoButton)
return
}
// Snap to grid
gridPos := SnapToGrid(pos.ToPoint())
// Set size and position
// All items are requirements by default
req := NewRequirement(uid)
req.SetPos(gridPos.Y(), gridPos.Y())
req.SetSize(itemSize*2, itemSize)
// Add item to view
scene.AddItem(NewGraphicsItem(req.Description(), gridPos.X(), gridPos.Y(), itemSize*2, itemSize, req))
if len(openItems) <= 0 {
openItems[req], _ = CreateEditWidgetFromPos(event.Pos(), scene)
window.AddDockWidget(core.Qt__RightDockWidgetArea, openItems[req])
}
})
view.ConnectMousePressEvent(func(event *gui.QMouseEvent) {
if event.Button() != core.Qt__LeftButton {
return
}
item := view.ItemAt(event.Pos())
// If an item was found
if item != nil && item.Group() != nil {
if linkBtn.IsChecked() {
// We're creating a link
linkStart = item.Group()
// Create temporary link indicator
scenePos := view.MapToScene(event.Pos())
tempLink = widgets.NewQGraphicsLineItem2(core.NewQLineF2(scenePos, scenePos), nil)
tempLink.SetPen(gui.NewQPen3(gui.NewQColor3(0, 255, 0, 128)))
scene.AddItem(tempLink)
} else {
// We're moving an item
movingItem = item.Group()
movingItem.SetOpacity(0.6)
}
}
})
view.ConnectMouseMoveEvent(func(event *gui.QMouseEvent) {
if movingItem != nil {
// Update item position
movingItem.SetPos(view.MapToScene(SnapToGrid(event.Pos())))
// Update link
UpdateLinkPos(movingItem, movingItem.Pos().X(), movingItem.Pos().Y())
}
// Update temporary link
if tempLink != nil {
tempLine := tempLink.Line()
tempLine.SetP2(view.MapToScene(event.Pos()))
tempLink.SetLine(tempLine)
}
// Show hand when trying to move item
if !linkBtn.IsChecked() && view.ItemAt(event.Pos()).Group() != nil && view.ItemAt(event.Pos()).Group().Type() != 0 {
cursor := core.Qt__OpenHandCursor
// If moving, show closed hand
if movingItem != nil {
cursor = core.Qt__ClosedHandCursor
}
view.SetCursor(gui.NewQCursor2(cursor))
} else {
// If not hovering over an item, restore cursor
view.UnsetCursor()
}
})
view.ConnectMouseReleaseEvent(func(event *gui.QMouseEvent) {
if event.Button() == core.Qt__RightButton && view.ItemAt(event.Pos()).Group() != nil {
pos := event.Pos()
menu := widgets.NewQMenu(nil)
// Check if clicking on link
item := view.ItemAt(pos)
group := item.Group()
// If type is 0, it's probably a link
if group.Type() == 0 {
// We hopefully clicked a link
menu.AddAction2(GetIcon("menu-delete"), "Delete").ConnectTriggered(func(checked bool) {
childItem := NewItem(item.Data(0).ToLongLong(nil), ItemType(item.Data(1).ToInt(nil)))
// Remove in front end
for _, itemLinks := range links {
for _, link := range itemLinks {
if link.child == childItem {
// Remove from database
childItem.SetParent(nil)
// Remove from graphics scene
scene.RemoveItem(link.line)
scene.RemoveItem(link.dir)
// Remove from links map
RemoveLink(link)
// Assume deleting one link
return
}
}
}
})
menu.Popup(view.MapToGlobal(pos), nil)
return
}
// Edit option
menu.AddAction2(GetIcon("menu-edit"), "Edit").
ConnectTriggered(func(checked bool) {
if editWidget, ok := CreateEditWidgetFromPos(pos, scene); ok {
window.AddDockWidget(core.Qt__RightDockWidgetArea, editWidget)
}
})
// Delete option
menu.AddAction2(GetIcon("menu-delete"), "Delete").
ConnectTriggered(func(checked bool) {
// Connect to database
db := currentProject.Data()
defer db.Close()
// Get the clicked item
group := view.ItemAt(pos).Group()
item := GetGroupItem(group)
// Try to get all links
link, ok := links[item]
if ok {
// Remove all links
for _, l := range link {
// It is the item we are trying to remove
if l.parent.ID() == item.ID() || l.child.ID() == item.ID() {
// Remove from scene
scene.RemoveItem(l.line)
scene.RemoveItem(l.dir)
// Remove from links map
RemoveLink(l)
}
}
}
// Remove the group from the scene
scene.RemoveItem(group)
// Remove the links and the item itself from the database
if err := db.RemoveChildrenLinks(item); err != nil {
fmt.Println("warning: failed to remove children links:", err)
}
if err := db.RemoveItem(item); err != nil {
fmt.Println("warning: failed to remove item:", err)
}
// Check if item is opened in editor
if openItem, ok := openItems[item]; ok {
openItem.Close()
delete(openItems, item)
}
})
// Show menu at cursor
menu.Popup(view.MapToGlobal(event.Pos()), nil)
return
}
// We released a button while moving an item
if movingItem != nil {
pos := movingItem.Pos()
// Update link if needed
// Error handling is already taken care of in UpdateLinkPos
UpdateLinkPos(movingItem, pos.X(), pos.Y())
// Update position in database
GetGroupItem(movingItem).SetPos(int(pos.X()), int(pos.Y()))
// Reset opacity and remove as moving
movingItem.SetOpacity(1.0)
movingItem = nil
}
// When releasing, we always want to destroy temp link
if tempLink != nil {
scene.RemoveItem(tempLink)
tempLink = nil
}
// We released while creating a link
if linkStart != nil {
linkStartItem := GetGroupItem(linkStart)
group := view.ItemAt(event.Pos()).Group()
groupItem := GetGroupItem(group)
// If we try to link to the empty void or self
if group == nil || linkStartItem == nil || (linkStartItem.ID() == groupItem.ID() &&
GetItemType(linkStartItem) == GetItemType(groupItem)) {
linkStart = nil
return
}
toPos := group.Pos()
if toPos.X() == 0 && toPos.Y() == 0 {
return
}
// Create and add link
link := CreateLink(linkStart, group)
scene.AddItem(link.line)
scene.AddItem(link.dir)
linkStart = nil
// Add link to database
db := currentProject.Data()
// Check if child already have a parent and add to db if it doesn't have one
if !GetGroupItem(group).IsPropertyNull("parent") {
fmt.Println("warning: child already has a parent")
} else if err := db.AddItemChild(linkStartItem, groupItem); err != nil {
fmt.Println("error: failed to add link to database:", err)
}
// We're done
db.Close()
}
})
return view
}
func GetGroupItem(group *widgets.QGraphicsItemGroup) Item {
// Check if group is nil
if group == nil {
fmt.Println("error: no group to create item from")
return nil
}
// Get ID and type
itemID := group.Data(0).ToLongLong(nil)
itemType := ItemType(group.Data(1).ToInt(nil))
// If either is zero, something is wrong
if itemID == 0 || itemType == 0 {
fmt.Println("error: could not create item from id", itemID, "type", itemType)
return nil
}
// Create a new type and return it
return NewItem(itemID, itemType)
}
func CreateLink(parent, child *widgets.QGraphicsItemGroup) Link {
// Check if map needs to be created
if links == nil {
links = make(map[Item][]*Link)
}
// Check if we're linking to self
parentItem := GetGroupItem(parent)
childItem := GetGroupItem(child)
// Get from (parent) and to (child)
fromPos := parent.Pos()
toPos := child.Pos()
// Create graphics line
line := widgets.NewQGraphicsLineItem3(
fromPos.X()+64, fromPos.Y()+32,
toPos.X()+64, toPos.Y()+32,
nil,
)
// Set the color of it
line.SetPen(gui.NewQPen3(gui.NewQColor3(0, 255, 0, 255)))
// Create line data
lineData := Link{
parentItem, childItem, line,
CreateTriangle(line.Line().Center(), line.Line().Angle()),
}
// Set data in line and triangle
lineData.SetChildItem(childItem)
// Save in links map
links[parentItem] = append(links[parentItem], &lineData)
links[childItem] = append(links[childItem], &lineData)
// Return the graphics line to add to scene
return lineData
}
func UpdateLinkPos(item *widgets.QGraphicsItemGroup, x, y float64) {
// Get link
itemID := GetGroupItem(item)
link, ok := links[itemID]
// Error checking
if !ok {
return
}
for _, l := range link {
// If the item is the parent
isParent := l.parent == itemID
// Update position of either parent or child
if isParent {
pos := l.line.Line().P2()
l.line.SetLine2(x+64, y+32, pos.X(), pos.Y())
} else {
pos := l.line.Line().P1()
l.line.SetLine2(pos.X(), pos.Y(), x+64, y+32)
}
// Update direction
center := l.line.Line().Center()
l.dir.SetPos2(center.X()-8, center.Y()-8)
l.dir.SetRotation((-l.line.Line().Angle()) - 90)
}
}
func RemoveLink(link *Link) {
// Remove from child
delete(links, link.child)
// Remove from parent
for i, childLink := range links[link.parent] {
if childLink.child == link.child {
last := len(links[link.parent])-1
// Replace entry to delete with last
links[link.parent][i] = links[link.parent][last]
// Cut away last element
links[link.parent] = links[link.parent][:last]
break
}
}
}
func NewGraphicsItem(text string, x, y, width, height int, item Item) *widgets.QGraphicsItemGroup {
group := widgets.NewQGraphicsItemGroup(nil)
textItem := widgets.NewQGraphicsTextItem(nil)
// Check if plain text is too long
doc := gui.NewQTextDocument(nil)
doc.SetHtml(text)
const maxLength = 46
if len(doc.ToPlainText()) > maxLength {
cursor := gui.NewQTextCursor2(doc)
// Move to end
cursor.MovePosition(gui.QTextCursor__End, gui.QTextCursor__MoveAnchor, 1)
// Keep removing when too long
for len(doc.ToPlainText()) > maxLength {
cursor.DeletePreviousChar()
}
// Remove last word to avoid weird cropping
cursor.Select(gui.QTextCursor__WordUnderCursor)
if len(cursor.Selection().ToPlainText()) != len(doc.ToPlainText()) {
cursor.InsertText("")
}
cursor.ClearSelection()
// Remove all trailing spaces
cursor.MovePosition(gui.QTextCursor__End, gui.QTextCursor__MoveAnchor, 1)
for strings.HasSuffix(doc.ToPlainText(), " ") && len(doc.ToPlainText()) > 0 {
cursor.DeletePreviousChar()
}
// Insert ... at end
cursor.InsertText("...")
}
// If no description, set text to item string
if len(doc.ToPlainText()) <= 0 {
doc.SetHtml(fmt.Sprintf("<small>(%v)</small>", item.ToString()))
}
textItem.SetHtml(doc.ToHtml(core.NewQByteArray()))
textItem.SetZValue(15)
textItem.SetTextWidth(float64(width))
shapeItem := widgets.NewQGraphicsRectItem3(0, 0, float64(width), float64(height), nil)
shapeItem.SetBrush(gui.NewQBrush3(backgroundColor, 1))
// Default purple color for requirements
var penColor uint = 10233776
// Blue color for solutions
if GetItemType(item) == TypeSolution {
penColor = 2201331
}
// Set opacity of border color
color := gui.NewQColor4(penColor)
color.SetAlpha(200)
shapeItem.SetPen(gui.NewQPen3(color))
group.AddToGroup(textItem)
group.AddToGroup(shapeItem)
group.SetPos2(float64(x), float64(y))
group.SetData(0, core.NewQVariant1(item.ID()))
group.SetData(1, core.NewQVariant1(int(GetItemType(item))))
group.SetZValue(10)
return group
}
// CreateTriangle creates a new 16x16 triangle pointing downwards
func CreateTriangle(pos *core.QPointF, angle float64) *widgets.QGraphicsPolygonItem {
// Total width/height for triangle
const size = 16
// Create each point
points := []*core.QPointF{
core.NewQPointF3(0, 0),
core.NewQPointF3(size, 0),
core.NewQPointF3(size>>1, size),
}
// Create polygon and return it
poly := widgets.NewQGraphicsPolygonItem2(gui.NewQPolygonF3(points), nil)
poly.SetPos2(pos.X()-(size>>1), pos.Y()-(size>>1))
poly.SetPen(gui.NewQPen3(gui.NewQColor3(0, 255, 0, 255)))
poly.SetTransformOriginPoint2(size>>1, size>>1)
poly.SetRotation((-angle) - 90)
return poly
}
func Roots() []Item {
// Final tree
roots := 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
isRoot := true
for _, l2 := range links[groupItem] {
if l2.child == groupItem {
isRoot = false
break
}
}
if isRoot {
roots = append(roots, groupItem)
}
}
return roots
}