window.go 2.81 KB
Newer Older
1
2
3
package gui

import (
Loïck Bonniot's avatar
Loïck Bonniot committed
4
5
	"math"

6
7
8
9
10
11
12
	"dfss"
	"github.com/visualfc/goqt/ui"
)

type Window struct {
	*ui.QMainWindow

Loïck Bonniot's avatar
Loïck Bonniot committed
13
14
15
16
17
	logField   *ui.QTextEdit
	graphics   *ui.QGraphicsView
	scene      *Scene
	circleSize float64
	pixmaps    map[string]*ui.QPixmap
18
19
20
21
22
23
24
25
26
27
28
29
30
31
}

func NewWindow() *Window {
	file := ui.NewFileWithName(":/widget.ui")
	loader := ui.NewUiLoader()
	widget := loader.Load(file)

	// Init main window
	window := ui.NewMainWindow()
	window.SetCentralWidget(widget)
	window.SetWindowTitle("DFSS Demonstrator v" + dfss.Version)

	w := &Window{
		QMainWindow: window,
Loïck Bonniot's avatar
Loïck Bonniot committed
32
		scene:       &Scene{},
33
	}
Loïck Bonniot's avatar
Loïck Bonniot committed
34
	w.InstallEventFilter(w)
35
36
37

	// Load dynamic elements from driver
	w.logField = ui.NewTextEditFromDriver(widget.FindChild("logField"))
Loïck Bonniot's avatar
Loïck Bonniot committed
38
39
40
41
42
43
44
45
46
47
	w.graphics = ui.NewGraphicsViewFromDriver(widget.FindChild("graphicsView"))

	// Load pixmaps
	w.pixmaps = map[string]*ui.QPixmap{
		"ttp":      ui.NewPixmapWithFilenameFormatFlags(":/images/server_key.png", "", ui.Qt_AutoColor),
		"platform": ui.NewPixmapWithFilenameFormatFlags(":/images/server_connect.png", "", ui.Qt_AutoColor),
	}

	// Load icon
	w.SetWindowIcon(ui.NewIconWithFilename(":/images/node_magnifier.png"))
48
49
50

	// Add actions
	w.addActions()
Loïck Bonniot's avatar
Loïck Bonniot committed
51
52
53
54
55
56
57
58
	w.initScene()

	// TEST ONLY
	w.scene.Clients = []Client{
		Client{"signer1@lesterpig.com"},
		Client{"signer2@insa-rennes.fr"},
		Client{"signer3@dfss.com"},
	}
59
60
61
62
63
64
65
66
67
68
69
70
71
72

	w.StatusBar().ShowMessage("Ready")
	return w
}

func (w *Window) Log(str string) {
	w.logField.Append(str)
	w.logField.EnsureCursorVisible()
}

func (w *Window) addActions() {
	openAct := ui.NewActionWithTextParent("&Open", w)
	openAct.SetShortcuts(ui.QKeySequence_Open)
	openAct.SetStatusTip("Open a demonstration file")
Loïck Bonniot's avatar
Loïck Bonniot committed
73
74
75
76
77
78
	openAct.OnTriggered(func() {
		filename := ui.QFileDialogGetOpenFileName()
		if filename != "" {
			w.Open(filename)
		}
	})
79
80
81
82

	saveAct := ui.NewActionWithTextParent("&Save", w)
	saveAct.SetShortcuts(ui.QKeySequence_Save)
	saveAct.SetStatusTip("Save a demonstration file")
Loïck Bonniot's avatar
Loïck Bonniot committed
83
84
85
86
87
88
	saveAct.OnTriggered(func() {
		filename := ui.QFileDialogGetSaveFileName()
		if filename != "" {
			w.Save(filename)
		}
	})
89
90
91
92

	w.MenuBar().AddAction(openAct)
	w.MenuBar().AddAction(saveAct)
}
Loïck Bonniot's avatar
Loïck Bonniot committed
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

func (w *Window) OnResizeEvent(ev *ui.QResizeEvent) bool {
	w.initScene()
	return true
}

func (w *Window) initScene() {

	// Save old scene
	oldScene := w.graphics.Scene()

	scene := ui.NewGraphicsScene()
	w.graphics.SetScene(scene)

	// Draw base circle
	w.circleSize = math.Min(float64(w.graphics.Width()), float64(w.graphics.Height())) - 50
	r := w.circleSize / 2
	scene.AddEllipseFWithXYWidthHeightPenBrush(-r, -r, w.circleSize, w.circleSize, pen_gray, brush_none)

	// Draw clients
	w.DrawClients()
	w.DrawServers()

	// TEST
	xa, ya := w.GetClientPosition(0)
	xb, yb := w.GetClientPosition(1)
	w.DrawArrow(xa, ya, xb, yb, colors["red"])
	w.DrawArrow(xb, yb, xa, ya, colors["red"])

	// Purge
	if oldScene != nil {
		oldScene.Delete()
	}
}