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

[d] Improve timer management and add controls

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