Commit 00f0e26a authored by Loïck Bonniot's avatar Loïck Bonniot

[d] Add import/export feature

parent a1cf888b
Pipeline #455 passed with stage
package gui
import (
"encoding/json"
"io/ioutil"
"strconv"
)
func (w *Window) Save(filename string) {
data, err := json.Marshal(w.scene)
if err != nil {
w.StatusBar().ShowMessage(err.Error())
return
}
err = ioutil.WriteFile(filename, data, 0600)
if err != nil {
w.StatusBar().ShowMessage(err.Error())
return
}
w.StatusBar().ShowMessage("Saved file as " + filename)
}
func (w *Window) Open(filename string) {
data, err := ioutil.ReadFile(filename)
if err != nil {
w.StatusBar().ShowMessage(err.Error())
return
}
newScene := &Scene{}
err = json.Unmarshal(data, newScene)
if err != nil {
w.StatusBar().ShowMessage(err.Error())
return
}
w.scene = newScene
w.StatusBar().ShowMessage("Imported file from " + filename + " (" + strconv.Itoa(len(w.scene.Events)) + " events)")
w.initScene()
}
......@@ -70,10 +70,22 @@ func (w *Window) addActions() {
openAct := ui.NewActionWithTextParent("&Open", w)
openAct.SetShortcuts(ui.QKeySequence_Open)
openAct.SetStatusTip("Open a demonstration file")
openAct.OnTriggered(func() {
filename := ui.QFileDialogGetOpenFileName()
if filename != "" {
w.Open(filename)
}
})
saveAct := ui.NewActionWithTextParent("&Save", w)
saveAct.SetShortcuts(ui.QKeySequence_Save)
saveAct.SetStatusTip("Save a demonstration file")
saveAct.OnTriggered(func() {
filename := ui.QFileDialogGetSaveFileName()
if filename != "" {
w.Save(filename)
}
})
w.MenuBar().AddAction(openAct)
w.MenuBar().AddAction(saveAct)
......
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