Skip to content

Commit 1f81d93

Browse files
committed
- Implement save/load
- Remove temporary files and using buffer
1 parent c817bd9 commit 1f81d93

3 files changed

Lines changed: 156 additions & 90 deletions

File tree

controlFunctions.go

Lines changed: 124 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,47 @@
11
package main
22

33
import (
4+
"bytes"
5+
"encoding/gob"
46
"errors"
57
"fmt"
68
"fyne.io/fyne/v2"
79
"fyne.io/fyne/v2/canvas"
810
"fyne.io/fyne/v2/dialog"
911
"image/color"
12+
"io"
1013
"log"
1114
"os"
1215
"path/filepath"
1316
"strconv"
1417
"time"
1518
)
1619

20+
var SavedProject struct {
21+
Options struct {
22+
FlatMatrix bool
23+
MatlabSaveFormat bool
24+
MatrixCol int
25+
MatrixRow int
26+
SettingsSaved bool
27+
OneHotEncodingSave bool
28+
}
29+
TempData struct {
30+
Saved bool
31+
buffer bytes.Buffer
32+
TempMatrix [][]int8
33+
TempTarget []string
34+
}
35+
OneHotDictionary struct {
36+
Dictionary map[string]interface{}
37+
Values []string
38+
}
39+
CounterValue string
40+
DataFilePath string
41+
TargetFilePath string
42+
Buffer []byte
43+
}
44+
1745
func addLabelAnimation(obj *canvas.Text) {
1846
green := color.NRGBA{G: 0xff, A: 0xff}
1947
canvas.NewColorRGBAAnimation(green, color.Black, time.Second*2, func(c color.Color) {
@@ -22,11 +50,15 @@ func addLabelAnimation(obj *canvas.Text) {
2250
}).Start()
2351
}
2452

25-
func saveOperation() {
53+
func exportFileOperation() {
2654
if !Options.SettingsSaved {
2755
dialog.ShowError(fmt.Errorf("please first save settings"), Application.mainWindow)
2856
return
2957
}
58+
if counterLabel.Text == "0" {
59+
dialog.ShowError(fmt.Errorf("Please first add at least 1 label"), Application.mainWindow)
60+
return
61+
}
3062

3163
// Validate save path
3264
path := savePath.Text
@@ -60,15 +92,15 @@ func saveOperation() {
6092
// Handle CSV format saving
6193
filePath := filepath.Join(path, dataFileName+".csv")
6294
if _, err := os.Stat(filePath); os.IsNotExist(err) {
63-
// File doesn't exist, save directly
95+
// file doesn't exist, save directly
6496
if err = SaveFile(path, dataFileName+".csv"); err != nil {
6597
statusLabel.Text = "Not Saved!"
6698
return
6799
}
68100
statusLabel.Text = "Saved!"
69101
} else {
70-
// File exists, ask for confirmation
71-
dialog.ShowConfirm("Warning", "File exists. Do you want to replace it?", func(b bool) {
102+
// file exists, ask for confirmation
103+
dialog.ShowConfirm("Warning", "file exists. Do you want to replace it?", func(b bool) {
72104
if b {
73105
if err = SaveFile(path, "data.csv"); err != nil {
74106
statusLabel.Text = "Not Saved!"
@@ -115,7 +147,7 @@ func matlabSaveCheckBoxFunction(b bool) {
115147
Application.mainWindow.Canvas().Refresh(Application.mainWindow.Content())
116148
}
117149
}
118-
func expertOperation() {
150+
func expertPNGOperation() {
119151
filename := "draw.png"
120152
if input.Text != "" {
121153
filename = input.Text + ".png"
@@ -126,7 +158,7 @@ func expertOperation() {
126158
}
127159
}
128160
func oneHotEncodingCheckBoxFunction(b bool) {
129-
Options.oneHotEncodingSave = b
161+
Options.OneHotEncodingSave = b
130162
if b {
131163
flatMatrixCheck.Disable()
132164
flatMatrixCheck.SetChecked(false)
@@ -169,7 +201,7 @@ func addButtonFunction() {
169201
}
170202
dialog.ShowError(fmt.Errorf("please enter valid label"), Application.mainWindow)
171203
}
172-
func saveProjectButtonFunction() {
204+
func applyProjectSetting() {
173205
rowInput.Disable()
174206
colInput.Disable()
175207
flatMatrixCheck.Disable()
@@ -178,7 +210,7 @@ func saveProjectButtonFunction() {
178210
Options.SettingsSaved = true
179211
InitializeTemps()
180212
}
181-
func resetProjectButtonFunction() {
213+
func resetProjectSetting() {
182214
dialog.ShowConfirm("Warning", "Are you sure you want to do that?\nthis is delete your added matrix if you dont saves it. ",
183215
func(choice bool) {
184216
rowInput.Enable()
@@ -188,27 +220,14 @@ func resetProjectButtonFunction() {
188220
oneHotEncodingSaveCheck.Enable()
189221
counterLabel.SetText("0")
190222
Options.SettingsSaved = false
191-
TempData.tempTarget = nil
192-
TempData.tempMatrix = nil
193-
OneHotDictionary.dictionary = nil
194-
OneHotDictionary.values = nil
195-
if TempData.file != nil {
196-
err := os.Remove(TempData.file.Name())
197-
if err != nil {
198-
fmt.Println(err)
199-
}
200-
}
201-
if TempData.targetFile != nil {
202-
err := os.Remove(TempData.targetFile.Name())
203-
if err != nil {
204-
fmt.Println(err)
205-
}
206-
}
207-
err := os.RemoveAll(TempData.dir)
208-
if err != nil {
209-
fmt.Println(err)
223+
TempData.TempTarget = nil
224+
TempData.TempMatrix = nil
225+
OneHotDictionary.Dictionary = nil
226+
OneHotDictionary.Values = nil
227+
if &TempData.buffer != nil {
228+
TempData.buffer = bytes.Buffer{}
229+
TempData.buffer.Reset()
210230
}
211-
TempData.file.Close()
212231

213232
}, Application.mainWindow,
214233
)
@@ -242,22 +261,88 @@ func onStartedApplication() {
242261
Application.paintObject.PrintMatrix(Application.paintWindow, Options.FlatMatrix)
243262
os.Stdout = oldStdOut
244263
}
245-
func onStoppedApplication() {
246-
if TempData.file != nil {
247-
err := TempData.file.Close()
264+
265+
func prepareSaveProjectObj() {
266+
SavedProject.Options = Options
267+
SavedProject.TempData = TempData
268+
SavedProject.OneHotDictionary = OneHotDictionary
269+
SavedProject.CounterValue = counterLabel.Text
270+
SavedProject.Buffer = TempData.buffer.Bytes()
271+
272+
}
273+
274+
func loadProjectFile(reader io.ReadCloser) error {
275+
decoder := gob.NewDecoder(reader)
276+
err := decoder.Decode(&SavedProject)
277+
if err != nil {
278+
return err
279+
}
280+
Options = SavedProject.Options
281+
TempData = SavedProject.TempData
282+
TempData.buffer.Write(SavedProject.Buffer)
283+
OneHotDictionary = SavedProject.OneHotDictionary
284+
err = countValue.Set(SavedProject.CounterValue)
285+
if err != nil {
286+
log.Println(err)
287+
return err
288+
}
289+
rowInput.Text = strconv.Itoa(Options.MatrixRow - 1)
290+
colInput.Text = strconv.Itoa(Options.MatrixCol - 1)
291+
oneHotEncodingSaveCheck.SetChecked(Options.OneHotEncodingSave)
292+
matlabSaveCheck.SetChecked(Options.MatlabSaveFormat)
293+
flatMatrixCheck.SetChecked(Options.FlatMatrix)
294+
Application.mainWindow.Content().Refresh()
295+
return nil
296+
}
297+
298+
func saveProjectFileFunction() {
299+
dialog.ShowFileSave(func(writer fyne.URIWriteCloser, err error) {
300+
if !Options.SettingsSaved {
301+
dialog.ShowError(fmt.Errorf("Please first save project settings."), Application.mainWindow)
302+
return
303+
}
304+
prepareSaveProjectObj()
305+
encoder := gob.NewEncoder(writer)
306+
err = encoder.Encode(SavedProject)
248307
if err != nil {
249308
log.Println(err)
250309
return
251310
}
252-
}
253-
if TempData.targetFile != nil {
254-
err := TempData.targetFile.Close()
311+
312+
err = writer.Close()
255313
if err != nil {
256-
log.Println(err)
257314
return
258315
}
259-
}
260-
if err := os.RemoveAll(TempData.dir); err != nil {
261-
log.Println("Error removing temp directory:", err)
262-
}
316+
317+
}, Application.mainWindow)
318+
319+
}
320+
321+
func loadProjectFileFunction() {
322+
dialog.ShowFileOpen(func(reader fyne.URIReadCloser, err error) {
323+
324+
if Options.SettingsSaved {
325+
dialog.ShowConfirm("Warning", "Are you sure to load project? Your current session is removed if you dont saved it.", func(b bool) {
326+
if b {
327+
loadErr := loadProjectFile(reader)
328+
if loadErr != nil {
329+
dialog.ShowError(fmt.Errorf("error loading project file"), Application.mainWindow)
330+
return
331+
}
332+
applyProjectSetting()
333+
}
334+
335+
}, Application.mainWindow)
336+
} else {
337+
loadErr := loadProjectFile(reader)
338+
if loadErr != nil {
339+
dialog.ShowError(fmt.Errorf("error loading project file"), Application.mainWindow)
340+
return
341+
}
342+
applyProjectSetting()
343+
statusLabel.Text = "Project loaded!"
344+
addLabelAnimation(statusLabel)
345+
}
346+
347+
}, Application.mainWindow)
263348
}

0 commit comments

Comments
 (0)