Commit 1f645003 authored by Loïck Bonniot's avatar Loïck Bonniot
Browse files

[d] Improve timer management and add controls

parent 2812ea2d
Pipeline #502 passed with stage
...@@ -4,5 +4,10 @@ ...@@ -4,5 +4,10 @@
<file>images/server_key.png</file> <file>images/server_key.png</file>
<file>images/server_connect.png</file> <file>images/server_connect.png</file>
<file>images/node_magnifier.png</file> <file>images/node_magnifier.png</file>
<file>images/control_play.png</file>
<file>images/control_play_blue.png</file>
<file>images/control_pause.png</file>
<file>images/control_pause_blue.png</file>
<file>images/control_rewind_blue.png</file>
</qresource> </qresource>
</RCC> </RCC>
...@@ -4,17 +4,14 @@ import ( ...@@ -4,17 +4,14 @@ import (
"fmt" "fmt"
"time" "time"
"math" "math"
"github.com/visualfc/goqt/ui"
) )
// TEMPORARY // TEMPORARY
const quantum = 100 // discretization argument for events (ns) const quantum = 100 // discretization argument for events (ns)
const speed = 500 // duration of a quantum (ms) const speed = 500 // duration of a quantum (ms)
func (w *Window) StartSimulation() {
w.ticker = time.NewTicker(speed * time.Millisecond)
go subroutine(w)
}
func (w *Window) DrawEvent(e *Event) { func (w *Window) DrawEvent(e *Event) {
xa, ya := w.GetClientPosition(e.Sender) xa, ya := w.GetClientPosition(e.Sender)
xb, yb := w.GetClientPosition(e.Receiver) xb, yb := w.GetClientPosition(e.Receiver)
...@@ -22,24 +19,30 @@ func (w *Window) DrawEvent(e *Event) { ...@@ -22,24 +19,30 @@ func (w *Window) DrawEvent(e *Event) {
} }
func (w *Window) PrintQuantumInformation() { func (w *Window) PrintQuantumInformation() {
if len(w.scene.Events) == 0 {
w.progress.SetText("No event")
return
}
beginning := w.scene.Events[0].Date.UnixNano() beginning := w.scene.Events[0].Date.UnixNano()
totalDuration := w.scene.Events[len(w.scene.Events) - 1].Date.UnixNano() - beginning totalDuration := w.scene.Events[len(w.scene.Events) - 1].Date.UnixNano() - beginning
nbQuantum := math.Ceil(float64(totalDuration) / quantum) nbQuantum := math.Ceil(float64(totalDuration) / quantum)
durationFromBeginning := w.scene.currentTime.UnixNano() - beginning durationFromBeginning := w.scene.currentTime.UnixNano() - beginning
currentQuantum := math.Ceil(float64(durationFromBeginning) / quantum)+1 currentQuantum := math.Ceil(float64(durationFromBeginning) / quantum)+1
if w.scene.currentEvent == 0 {
currentQuantum = 0
}
w.progress.SetText(fmt.Sprint(currentQuantum, " / ", nbQuantum)) w.progress.SetText(fmt.Sprint(currentQuantum, " / ", nbQuantum))
} }
func subroutine(w *Window) { func (w *Window) initTimer() {
for _ = range w.ticker.C { w.timer = ui.NewTimerWithParent(w)
w.timer.OnTimeout(func() {
nbEvents := len(w.scene.Events) nbEvents := len(w.scene.Events)
if w.scene.currentEvent >= nbEvents { if w.scene.currentEvent >= nbEvents {
// TODO disable looping if needed w.replayButton.Click()
w.RemoveArrows() return
w.scene.currentEvent = 0 // loop
w.Log("Restarting simulation...")
continue
} }
// Remove arrows from last tick // Remove arrows from last tick
...@@ -47,7 +50,7 @@ func subroutine(w *Window) { ...@@ -47,7 +50,7 @@ func subroutine(w *Window) {
// Check that we have a least one event to read // Check that we have a least one event to read
if nbEvents == 0 { if nbEvents == 0 {
continue return
} }
// Init first time // Init first time
...@@ -70,5 +73,5 @@ func subroutine(w *Window) { ...@@ -70,5 +73,5 @@ func subroutine(w *Window) {
w.PrintQuantumInformation() w.PrintQuantumInformation()
w.scene.currentTime = endOfQuantum w.scene.currentTime = endOfQuantum
} })
} }
...@@ -15,12 +15,15 @@ type Window struct { ...@@ -15,12 +15,15 @@ type Window struct {
logField *ui.QTextEdit logField *ui.QTextEdit
graphics *ui.QGraphicsView graphics *ui.QGraphicsView
progress *ui.QLabel progress *ui.QLabel
playButton *ui.QPushButton
stopButton *ui.QPushButton
replayButton *ui.QPushButton
scene *Scene scene *Scene
circleSize float64 circleSize float64
pixmaps map[string]*ui.QPixmap pixmaps map[string]*ui.QPixmap
currentArrows []*ui.QGraphicsPathItem currentArrows []*ui.QGraphicsPathItem
ticker *time.Ticker timer *ui.QTimer
} }
// Client represents a DFSSC instance // Client represents a DFSSC instance
......
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"> <ui version="4.0">
<class>CalculatorForm</class> <class>Demonstrator</class>
<widget class="QWidget" name="CalculatorForm"> <widget class="QWidget" name="Demonstrator">
<property name="enabled"> <property name="enabled">
<bool>true</bool> <bool>true</bool>
</property> </property>
...@@ -66,10 +66,32 @@ ...@@ -66,10 +66,32 @@
<verstretch>0</verstretch> <verstretch>0</verstretch>
</sizepolicy> </sizepolicy>
</property> </property>
<property name="text"> <property name="maximumSize">
<string>Play</string> <size>
<width>40</width>
<height>40</height>
</size>
</property> </property>
<property name="default"> <property name="focusPolicy">
<enum>Qt::NoFocus</enum>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="stopButton">
<property name="maximumSize">
<size>
<width>40</width>
<height>40</height>
</size>
</property>
<property name="focusPolicy">
<enum>Qt::NoFocus</enum>
</property>
<property name="flat">
<bool>true</bool> <bool>true</bool>
</property> </property>
</widget> </widget>
...@@ -82,8 +104,17 @@ ...@@ -82,8 +104,17 @@
<verstretch>0</verstretch> <verstretch>0</verstretch>
</sizepolicy> </sizepolicy>
</property> </property>
<property name="text"> <property name="maximumSize">
<string>Replay</string> <size>
<width>40</width>
<height>40</height>
</size>
</property>
<property name="focusPolicy">
<enum>Qt::NoFocus</enum>
</property>
<property name="flat">
<bool>true</bool>
</property> </property>
</widget> </widget>
</item> </item>
...@@ -108,6 +139,9 @@ ...@@ -108,6 +139,9 @@
<verstretch>0</verstretch> <verstretch>0</verstretch>
</sizepolicy> </sizepolicy>
</property> </property>
<property name="focusPolicy">
<enum>Qt::TabFocus</enum>
</property>
<property name="suffix"> <property name="suffix">
<string> us</string> <string> us</string>
</property> </property>
...@@ -146,6 +180,9 @@ ...@@ -146,6 +180,9 @@
<verstretch>0</verstretch> <verstretch>0</verstretch>
</sizepolicy> </sizepolicy>
</property> </property>
<property name="focusPolicy">
<enum>Qt::TabFocus</enum>
</property>
<property name="orientation"> <property name="orientation">
<enum>Qt::Horizontal</enum> <enum>Qt::Horizontal</enum>
</property> </property>
......
...@@ -29,18 +29,23 @@ func NewWindow() *Window { ...@@ -29,18 +29,23 @@ func NewWindow() *Window {
w.graphics = ui.NewGraphicsViewFromDriver(widget.FindChild("graphicsView")) w.graphics = ui.NewGraphicsViewFromDriver(widget.FindChild("graphicsView"))
w.progress = ui.NewLabelFromDriver(widget.FindChild("progressLabel")) w.progress = ui.NewLabelFromDriver(widget.FindChild("progressLabel"))
w.playButton = ui.NewPushButtonFromDriver(widget.FindChild("playButton"))
w.stopButton = ui.NewPushButtonFromDriver(widget.FindChild("stopButton"))
w.replayButton = ui.NewPushButtonFromDriver(widget.FindChild("replayButton"))
// Load pixmaps // Load pixmaps
w.pixmaps = map[string]*ui.QPixmap{ w.pixmaps = map[string]*ui.QPixmap{
"ttp": ui.NewPixmapWithFilenameFormatFlags(":/images/server_key.png", "", ui.Qt_AutoColor), "ttp": ui.NewPixmapWithFilenameFormatFlags(":/images/server_key.png", "", ui.Qt_AutoColor),
"platform": ui.NewPixmapWithFilenameFormatFlags(":/images/server_connect.png", "", ui.Qt_AutoColor), "platform": ui.NewPixmapWithFilenameFormatFlags(":/images/server_connect.png", "", ui.Qt_AutoColor),
} }
// Load icon // Load icons
w.SetWindowIcon(ui.NewIconWithFilename(":/images/node_magnifier.png")) w.addIcons()
// Add actions // Add actions
w.addActions() w.addActions()
w.initScene() w.initScene()
w.initTimer()
// TEST ONLY // TEST ONLY
w.scene.Clients = []Client{ w.scene.Clients = []Client{
...@@ -56,16 +61,38 @@ func NewWindow() *Window { ...@@ -56,16 +61,38 @@ func NewWindow() *Window {
} }
w.StatusBar().ShowMessage("Ready") w.StatusBar().ShowMessage("Ready")
w.StartSimulation() w.PrintQuantumInformation()
return w return w
} }
func (w *Window) OnResizeEvent(ev *ui.QResizeEvent) bool {
w.initScene()
return true
}
func (w *Window) Log(str string) { func (w *Window) Log(str string) {
w.logField.Append(str) w.logField.Append(str)
w.logField.EnsureCursorVisible() w.logField.EnsureCursorVisible()
} }
func (w *Window) addIcons() {
w.SetWindowIcon(ui.NewIconWithFilename(":/images/node_magnifier.png"))
var i *ui.QIcon
i = ui.NewIconWithFilename(":/images/control_play_blue.png")
i.AddFileWithFilenameSizeModeState(":/images/control_play.png", ui.NewSizeWithWidthHeight(32, 32), ui.QIcon_Disabled, ui.QIcon_Off)
w.playButton.SetIcon(i)
i = ui.NewIconWithFilename(":/images/control_pause_blue.png")
i.AddFileWithFilenameSizeModeState(":/images/control_pause.png", ui.NewSizeWithWidthHeight(32, 32), ui.QIcon_Disabled, ui.QIcon_Off)
w.stopButton.SetIcon(i)
i = ui.NewIconWithFilename(":/images/control_rewind_blue.png")
w.replayButton.SetIcon(i)
}
func (w *Window) addActions() { func (w *Window) addActions() {
// MENU BAR
openAct := ui.NewActionWithTextParent("&Open", w) openAct := ui.NewActionWithTextParent("&Open", w)
openAct.SetShortcuts(ui.QKeySequence_Open) openAct.SetShortcuts(ui.QKeySequence_Open)
openAct.SetStatusTip("Open a demonstration file") openAct.SetStatusTip("Open a demonstration file")
...@@ -88,11 +115,29 @@ func (w *Window) addActions() { ...@@ -88,11 +115,29 @@ func (w *Window) addActions() {
w.MenuBar().AddAction(openAct) w.MenuBar().AddAction(openAct)
w.MenuBar().AddAction(saveAct) w.MenuBar().AddAction(saveAct)
}
func (w *Window) OnResizeEvent(ev *ui.QResizeEvent) bool { // SIMULATION CONTROL
w.initScene() w.playButton.OnClicked(func() {
return true w.playButton.SetDisabled(true)
w.stopButton.SetDisabled(false)
w.timer.StartWithMsec(500)
w.Log("Started simulation")
})
w.stopButton.OnClicked(func() {
w.playButton.SetDisabled(false)
w.stopButton.SetDisabled(true)
w.timer.Stop()
w.Log("Paused simulation")
})
w.stopButton.SetDisabled(true)
w.replayButton.OnClicked(func() {
w.RemoveArrows()
w.scene.currentEvent = 0
w.PrintQuantumInformation()
w.Log("Restarting simulation")
})
} }
func (w *Window) initScene() { func (w *Window) initScene() {
......
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