-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrolFunctions.go
More file actions
385 lines (356 loc) · 9.98 KB
/
controlFunctions.go
File metadata and controls
385 lines (356 loc) · 9.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package main
import (
"bytes"
"encoding/gob"
"errors"
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/widget"
"image/color"
"io"
"log"
url2 "net/url"
"os"
"path/filepath"
"strconv"
"time"
)
var SavedProject struct {
Options struct {
FlatMatrix bool
MatlabSaveFormat bool
DotMFileWithVariable bool
MatrixCol int
MatrixRow int
SettingsSaved bool
OneHotEncodingSave bool
}
TempData struct {
Saved bool
buffer bytes.Buffer
TempMatrix [][]int8
TempTarget []string
}
OneHotDictionary struct {
Dictionary map[string]interface{}
Values []string
}
CounterValue string
DataFilePath string
TargetFilePath string
Buffer []byte
}
func addLabelAnimation(obj *canvas.Text) {
green := color.NRGBA{G: 0xff, A: 0xff}
canvas.NewColorRGBAAnimation(green, color.Black, time.Second*2, func(c color.Color) {
obj.Color = c
obj.Refresh()
}).Start()
}
func exportFileOperation() {
if !Options.SettingsSaved {
dialog.ShowError(fmt.Errorf("please first save settings"), Application.mainWindow)
return
}
if counterLabel.Text == "0" {
dialog.ShowError(fmt.Errorf("Please first add at least 1 label"), Application.mainWindow)
return
}
// Validate save path
path := savePath.Text
if path == "" {
dialog.ShowError(errors.New("path is empty"), Application.mainWindow)
return
}
// Validate data filename
dataFileName := dataFileEntry.Text
if dataFileName == "" {
dialog.ShowError(errors.New("data file name is empty"), Application.mainWindow)
return
}
// Handle MATLAB format saving
if Options.MatlabSaveFormat {
targetFileName := targetFileEntry.Text
if targetFileName == "" {
dialog.ShowError(errors.New("target file name is empty"), Application.mainWindow)
return
}
if err := SaveFileForMatlab(path, dataFileName, targetFileName); err != nil {
statusLabel.Text = "Not Saved!"
return
}
statusLabel.Text = "Saved!"
return
}
// Handle CSV format saving
filePath := filepath.Join(path, dataFileName+".csv")
if _, err := os.Stat(filePath); os.IsNotExist(err) {
// file doesn't exist, save directly
if err = SaveFile(path, dataFileName+".csv"); err != nil {
statusLabel.Text = "Not Saved!"
return
}
statusLabel.Text = "Saved!"
} else {
// file exists, ask for confirmation
dialog.ShowConfirm("Warning", "file exists. Do you want to replace it?", func(b bool) {
if b {
if err = SaveFile(path, "data.csv"); err != nil {
statusLabel.Text = "Not Saved!"
return
}
statusLabel.Text = "Saved!"
} else {
statusLabel.Text = "Not Saved!"
}
}, Application.mainWindow)
}
}
func browseOperation() {
dialog.ShowFolderOpen(func(uc fyne.ListableURI, err error) {
if err != nil {
dialog.ShowError(err, Application.mainWindow)
return
}
if uc == nil {
return
}
DirPath := uc.Path()
savePath.SetText(DirPath)
}, Application.mainWindow)
}
func openPaintWindowOperation() {
if Application.paintWindow != nil {
Application.paintWindow.Close()
}
Application.paintWindow = NewPaintWindow(mainApp, Application.paintObject)
Application.paintWindow.Show()
}
func matlabSaveCheckBoxFunction(b bool) {
Options.MatlabSaveFormat = b
if b {
targetFileEntry.Enable()
dotMFileWithVariableCheck.Enable()
flatMatrixCheck.Disable()
flatMatrixCheck.SetChecked(false)
Options.FlatMatrix = false
Application.mainWindow.Canvas().Refresh(Application.mainWindow.Content())
} else {
dotMFileWithVariableCheck.Disable()
targetFileEntry.Disable()
flatMatrixCheck.Enable()
Application.mainWindow.Canvas().Refresh(Application.mainWindow.Content())
}
}
func DotMFileWithVariableCheck(b bool) {
Options.DotMFileWithVariable = b
}
func expertPNGOperation() {
filename := "draw.png"
if input.Text != "" {
filename = input.Text + ".png"
}
err := Application.paintObject.ExportToPNG(Application.paintWindow, filename)
if err != nil {
fmt.Printf("Export error: %s", err)
}
}
func oneHotEncodingCheckBoxFunction(b bool) {
Options.OneHotEncodingSave = b
if b {
flatMatrixCheck.Disable()
flatMatrixCheck.SetChecked(false)
matlabSaveCheck.SetChecked(true)
} else {
flatMatrixCheck.Enable()
}
}
func addButtonFunction() {
if !Options.SettingsSaved {
dialog.ShowError(fmt.Errorf("please first save settings"), Application.mainWindow)
return
}
if input.Text != "" {
if Options.MatlabSaveFormat {
AddToFileForMatlab(Application.paintObject.GetMatrix(Application.paintWindow), input.Text)
count, err := strconv.Atoi(counterLabel.Text)
if err != nil {
return
}
counterLabel.SetText(strconv.Itoa(count + 1))
addLabelAnimation(statusLabel)
statusLabel.Text = "Added!"
return
}
err := AddToFile(Application.paintObject.GetMatrix(Application.paintWindow), input.Text)
if err != nil {
dialog.ShowError(fmt.Errorf("error to add matrix"), Application.mainWindow)
return
}
count, err := strconv.Atoi(counterLabel.Text)
if err != nil {
return
}
counterLabel.SetText(strconv.Itoa(count + 1))
addLabelAnimation(statusLabel)
statusLabel.Text = "Added!"
return
}
dialog.ShowError(fmt.Errorf("please enter valid label"), Application.mainWindow)
}
func applyProjectSetting(withInitial bool) {
rowInput.Disable()
colInput.Disable()
flatMatrixCheck.Disable()
matlabSaveCheck.Disable()
dotMFileWithVariableCheck.Disable()
oneHotEncodingSaveCheck.Disable()
Options.SettingsSaved = true
if withInitial {
InitializeTemps()
}
}
func resetProjectSetting() {
dialog.ShowConfirm("Warning", "Are you sure you want to do that?\nthis is delete your added matrix if you dont saves it. ",
func(choice bool) {
rowInput.Enable()
colInput.Enable()
flatMatrixCheck.Enable()
matlabSaveCheck.Enable()
dotMFileWithVariableCheck.Enable()
oneHotEncodingSaveCheck.Enable()
counterLabel.SetText("0")
Options.SettingsSaved = false
TempData.TempTarget = nil
TempData.TempMatrix = nil
OneHotDictionary.Dictionary = nil
OneHotDictionary.Values = nil
if &TempData.buffer != nil {
TempData.buffer = bytes.Buffer{}
TempData.buffer.Reset()
}
}, Application.mainWindow,
)
}
func aboutBtn() {
url, _ := url2.Parse("https://github.com/ehsan-torabi")
repoURL, _ := url2.Parse("https://github.com/ehsan-torabi/Draw2Matrix")
richText := widget.NewRichText(
&widget.TextSegment{Text: "Programmed by : Ehsan Torabi Farsani", Style: widget.RichTextStyle{}},
&widget.TextSegment{Text: "\nFor more details, visit our ", Style: widget.RichTextStyle{}},
&widget.HyperlinkSegment{Text: "Github", URL: url},
&widget.TextSegment{Text: "\t", Style: widget.RichTextStyle{}},
&widget.HyperlinkSegment{Text: "Draw2Matrix Repository", URL: repoURL},
&widget.TextSegment{Text: "\n", Style: widget.RichTextStyle{}},
)
dialog.NewCustom("About", "exit", richText, Application.mainWindow).Show()
}
func labelValidator(s string) error {
if len(s) > 20 {
return fmt.Errorf("label too long")
}
return nil
}
func rowValidator(s string) error {
val, err := strconv.Atoi(s)
if err != nil || val <= 0 {
return fmt.Errorf("enter number ")
}
Options.MatrixRow = val + 1
return nil
}
func colValidator(s string) error {
val, err := strconv.Atoi(s)
if err != nil || val <= 0 {
return fmt.Errorf("enter number")
}
Options.MatrixCol = val + 1
return nil
}
func onStartedApplication() {
// Temporarily disable stdout to prevent matrix printing
oldStdOut := os.Stdout
os.Stdout = nil
Application.paintObject.PrintMatrix(Application.paintWindow, Options.FlatMatrix)
os.Stdout = oldStdOut
}
func prepareSaveProjectObj() {
SavedProject.Options = Options
SavedProject.TempData = TempData
SavedProject.OneHotDictionary = OneHotDictionary
SavedProject.CounterValue = counterLabel.Text
SavedProject.Buffer = TempData.buffer.Bytes()
}
func loadProjectFile(reader io.ReadCloser) error {
decoder := gob.NewDecoder(reader)
err := decoder.Decode(&SavedProject)
if err != nil {
return err
}
Options = SavedProject.Options
TempData = SavedProject.TempData
copy(TempData.TempMatrix, SavedProject.TempData.TempMatrix)
copy(TempData.TempTarget, SavedProject.TempData.TempTarget)
TempData.buffer.Write(SavedProject.Buffer)
OneHotDictionary = SavedProject.OneHotDictionary
copy(OneHotDictionary.Values, SavedProject.OneHotDictionary.Values)
err = countValue.Set(SavedProject.CounterValue)
if err != nil {
log.Println(err)
return err
}
rowInput.Text = strconv.Itoa(Options.MatrixRow - 1)
colInput.Text = strconv.Itoa(Options.MatrixCol - 1)
oneHotEncodingSaveCheck.SetChecked(Options.OneHotEncodingSave)
matlabSaveCheck.SetChecked(Options.MatlabSaveFormat)
dotMFileWithVariableCheck.SetChecked(Options.DotMFileWithVariable)
flatMatrixCheck.SetChecked(Options.FlatMatrix)
Application.mainWindow.Content().Refresh()
return nil
}
func saveProjectFileFunction() {
dialog.ShowFileSave(func(writer fyne.URIWriteCloser, err error) {
if !Options.SettingsSaved {
dialog.ShowError(fmt.Errorf("Please first save project settings."), Application.mainWindow)
return
}
prepareSaveProjectObj()
encoder := gob.NewEncoder(writer)
err = encoder.Encode(SavedProject)
if err != nil {
log.Println(err)
return
}
err = writer.Close()
if err != nil {
return
}
}, Application.mainWindow)
}
func loadProjectFileFunction() {
dialog.ShowFileOpen(func(reader fyne.URIReadCloser, err error) {
if Options.SettingsSaved {
dialog.ShowConfirm("Warning", "Are you sure to load project? Your current session is removed if you dont saved it.", func(b bool) {
if b {
loadErr := loadProjectFile(reader)
if loadErr != nil {
dialog.ShowError(fmt.Errorf("error loading project file"), Application.mainWindow)
return
}
applyProjectSetting(false)
}
}, Application.mainWindow)
} else {
loadErr := loadProjectFile(reader)
if loadErr != nil {
dialog.ShowError(fmt.Errorf("error loading project file"), Application.mainWindow)
return
}
applyProjectSetting(false)
statusLabel.Text = "Project loaded!"
addLabelAnimation(statusLabel)
}
}, Application.mainWindow)
}