Skip to content

Commit

Permalink
Add a background-running simulation example
Browse files Browse the repository at this point in the history
Issue #32
  • Loading branch information
barche committed Jan 8, 2017
1 parent 80bb872 commit 27c565c
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
53 changes: 53 additions & 0 deletions example/progressbar.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# This example show how to update the GUI dusing a long-running simulation

using Base.Test
using QML

qmlfile = joinpath(dirname(Base.source_path()), "qml", "progressbar.qml")

type SimulationState
progress::Float64
end

const simulation_state = SimulationState(0.0)

# Our simulation is just a busy wait, producing progress between 0.0 and 1.0
function simulate()
counter = 0.0
maxcount = 1000000000.0
while counter < maxcount
for i in 1:Int(maxcount/10)
counter += 1.0
end
produce(counter/maxcount)
end
produce(1.0)
end

simulation_task = Task(simulate)

# Run a step in the simulation, restart if already finished
function step()
global simulation_state
global simulation_task

if simulation_state.progress >= 1.0
println("Simulation was finished, restarting")
simulation_task = Task(simulate)
end

@show progress = consume(simulation_task)
@qmlset qmlcontext().simulation_state.progress = progress

return
end

# Register the stepping function function
@qmlfunction step

# The timer will run simulation steps as fast as it can while active, yielding to the GUI after each tick
timer = QTimer()

# All arguments after qmlfile are context properties:
@qmlapp qmlfile simulation_state timer
exec()
43 changes: 43 additions & 0 deletions example/qml/progressbar.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import QtQuick 2.0
import QtQuick.Controls 1.0
import QtQuick.Layouts 1.0
import org.julialang 1.0

ApplicationWindow {
title: "My Application"
width: 480
height: 200
visible: true

// Set up timer connection
Connections {
target: timer
onTimeout: Julia.step()
}

ColumnLayout {
spacing: 6
anchors.centerIn: parent

ProgressBar {
value: simulation_state.progress
onValueChanged: {
if(value >= 1.0) {
timer.stop()
}
}
}

Button {
Layout.alignment: Qt.AlignCenter
text: "Start simulation"
onClicked: timer.start()
}

Button {
Layout.alignment: Qt.AlignCenter
text: "Stop simulation"
onClicked: timer.stop()
}
}
}

0 comments on commit 27c565c

Please sign in to comment.