Commit a1cf888b authored by Loïck Bonniot's avatar Loïck Bonniot

[d] Add basic draw features

- Draw and place clients
- Draw arrows
- Draw servers
- Add icon
parent ae7eeac3
Pipeline #454 passed with stage
<RCC>
<qresource prefix="/">
<file>widget.ui</file>
<file>images/server_key.png</file>
<file>images/server_connect.png</file>
<file>images/node_magnifier.png</file>
</qresource>
</RCC>
package gui
import (
"math"
"github.com/visualfc/goqt/ui"
)
const ARROW_T = math.Pi / 6
const ARROW_L = 15
func (w *Window) DrawClients() {
scene := w.graphics.Scene()
for i, c := range w.scene.Clients {
x, y := w.GetClientPosition(i)
// Add ellipse
scene.AddEllipseFWithXYWidthHeightPenBrush(x-10, y-10, 20, 20, pen_black, brush_black)
// Add text
t := scene.AddSimpleText(c.Name)
r := t.BoundingRect()
t.SetX(x - r.Width()/2)
t.SetY(y + 10)
}
}
func (w *Window) GetClientPosition(i int) (x, y float64) {
nbClients := float64(len(w.scene.Clients))
angle := 2 * math.Pi * float64(i) / nbClients
return math.Cos(angle) * (w.circleSize / 2), math.Sin(angle) * (w.circleSize / 2)
}
func (w *Window) GetServerPosition(platform bool) (x, y float64) {
x = w.circleSize/2 + 150
y = -16
if !platform {
x *= -1
}
return
}
func (w *Window) DrawServers() {
scene := w.graphics.Scene()
ttp := scene.AddPixmap(w.pixmaps["ttp"])
x, y := w.GetServerPosition(false)
ttp.SetPosFWithXY(x, y)
ttp.SetToolTip("TTP")
platform := scene.AddPixmap(w.pixmaps["platform"])
platform.SetX(w.circleSize/2 + 150)
platform.SetY(-16)
platform.SetToolTip("Platform")
}
func (w *Window) DrawArrow(xa, ya, xb, yb float64, rgb uint32) {
scene := w.graphics.Scene()
path := ui.NewPainterPath()
path.MoveToFWithXY(xa, ya)
path.LineToFWithXY(xb, yb)
v := ui.NewVector2DWithXposYpos(xa-xb, ya-yb)
l := v.Length()
// from http://math.stackexchange.com/a/1314050
xc := xb + ARROW_L/l*(v.X()*math.Cos(ARROW_T)+v.Y()*math.Sin(ARROW_T))
yc := yb + ARROW_L/l*(v.Y()*math.Cos(ARROW_T)-v.X()*math.Sin(ARROW_T))
xd := xb + ARROW_L/l*(v.X()*math.Cos(ARROW_T)-v.Y()*math.Sin(ARROW_T))
yd := yb + ARROW_L/l*(v.Y()*math.Cos(ARROW_T)+v.X()*math.Sin(ARROW_T))
path.LineToFWithXY(xc, yc)
path.LineToFWithXY(xd, yd)
path.LineToFWithXY(xb, yb)
path.SetFillRule(ui.Qt_WindingFill)
color := ui.NewColorWithRgb(rgb)
color.SetAlpha(200)
pen := ui.NewPenWithColor(color)
pen.SetWidth(3)
pen.SetJoinStyle(ui.Qt_RoundJoin)
brush := ui.NewBrush()
brush.SetColor(color)
brush.SetStyle(ui.Qt_SolidPattern)
scene.AddPathWithPathPenBrush(path, pen, brush)
}
package gui
import "github.com/visualfc/goqt/ui"
var colors = map[string]uint32{
"red": 0x00ff0000,
"blue": 0x0000ff00,
"green": 0x000000ff,
"black": 0x00000000,
}
var pen_black = ui.NewPenWithColor(ui.NewColorWithGlobalcolor(ui.Qt_black))
var pen_gray = ui.NewPenWithColor(ui.NewColorWithGlobalcolor(ui.Qt_gray))
var brush_none = ui.NewBrushWithGlobalcolorBrushstyle(ui.Qt_transparent, ui.Qt_SolidPattern)
var brush_black = ui.NewBrushWithGlobalcolorBrushstyle(ui.Qt_black, ui.Qt_SolidPattern)
package gui
import (
"time"
)
// Client represents a DFSSC instance
type Client struct {
Name string
}
// EventType is used as an enum for event types, to differenciate promises, signatures...
type EventType int
const (
PROMISE EventType = iota
SIGNATURE
OTHER
)
// Event represents a single signature event
type Event struct {
Type EventType
Sender int
Receiver int
Date *time.Time
Duration *time.Duration
}
// Scene holds the global scene for registered clients and signature events
type Scene struct {
Clients []Client
Events []Event
currentEvent int
}
......@@ -39,6 +39,15 @@
<height>300</height>
</size>
</property>
<property name="backgroundBrush">
<brush brushstyle="SolidPattern">
<color alpha="255">
<red>255</red>
<green>255</green>
<blue>255</blue>
</color>
</brush>
</property>
</widget>
</item>
<item>
......
package gui
import (
"math"
"dfss"
"github.com/visualfc/goqt/ui"
)
......@@ -8,7 +10,11 @@ import (
type Window struct {
*ui.QMainWindow
logField *ui.QTextEdit
logField *ui.QTextEdit
graphics *ui.QGraphicsView
scene *Scene
circleSize float64
pixmaps map[string]*ui.QPixmap
}
func NewWindow() *Window {
......@@ -23,13 +29,33 @@ func NewWindow() *Window {
w := &Window{
QMainWindow: window,
scene: &Scene{},
}
w.InstallEventFilter(w)
// Load dynamic elements from driver
w.logField = ui.NewTextEditFromDriver(widget.FindChild("logField"))
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"))
// Add actions
w.addActions()
w.initScene()
// TEST ONLY
w.scene.Clients = []Client{
Client{"signer1@lesterpig.com"},
Client{"signer2@insa-rennes.fr"},
Client{"signer3@dfss.com"},
}
w.StatusBar().ShowMessage("Ready")
return w
......@@ -52,3 +78,37 @@ func (w *Window) addActions() {
w.MenuBar().AddAction(openAct)
w.MenuBar().AddAction(saveAct)
}
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()
}
}
......@@ -8,8 +8,8 @@ import (
"strconv"
"dfss"
"dfss/dfssd/server"
"dfss/dfssd/gui"
"dfss/dfssd/server"
"github.com/visualfc/goqt/ui"
)
......@@ -54,7 +54,7 @@ func main() {
lfn := func(str string) {
fmt.Println(str)
}
err := server.Listen("0.0.0.0:" + strconv.Itoa(port), lfn)
err := server.Listen("0.0.0.0:"+strconv.Itoa(port), lfn)
if err != nil {
os.Exit(1)
}
......@@ -62,7 +62,7 @@ func main() {
ui.Run(func() {
window := gui.NewWindow()
go func() {
err := server.Listen("0.0.0.0:" + strconv.Itoa(port), window.Log)
err := server.Listen("0.0.0.0:"+strconv.Itoa(port), window.Log)
if err != nil {
window.Log("!! " + err.Error())
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment