Skip to content

Commit a48715f

Browse files
committed
- Add .m file saving
- Add about button
1 parent 6d5eac7 commit a48715f

4 files changed

Lines changed: 74 additions & 24 deletions

File tree

controlFunctions.go

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import (
88
"fyne.io/fyne/v2"
99
"fyne.io/fyne/v2/canvas"
1010
"fyne.io/fyne/v2/dialog"
11+
"fyne.io/fyne/v2/widget"
1112
"image/color"
1213
"io"
1314
"log"
15+
url2 "net/url"
1416
"os"
1517
"path/filepath"
1618
"strconv"
@@ -19,12 +21,13 @@ import (
1921

2022
var SavedProject struct {
2123
Options struct {
22-
FlatMatrix bool
23-
MatlabSaveFormat bool
24-
MatrixCol int
25-
MatrixRow int
26-
SettingsSaved bool
27-
OneHotEncodingSave bool
24+
FlatMatrix bool
25+
MatlabSaveFormat bool
26+
DotMFileWithVariable bool
27+
MatrixCol int
28+
MatrixRow int
29+
SettingsSaved bool
30+
OneHotEncodingSave bool
2831
}
2932
TempData struct {
3033
Saved bool
@@ -137,16 +140,24 @@ func matlabSaveCheckBoxFunction(b bool) {
137140
Options.MatlabSaveFormat = b
138141
if b {
139142
targetFileEntry.Enable()
143+
dotMFileWithVariableCheck.Enable()
140144
flatMatrixCheck.Disable()
141145
flatMatrixCheck.SetChecked(false)
142146
Options.FlatMatrix = false
143147
Application.mainWindow.Canvas().Refresh(Application.mainWindow.Content())
144148
} else {
149+
dotMFileWithVariableCheck.Disable()
145150
targetFileEntry.Disable()
146151
flatMatrixCheck.Enable()
147152
Application.mainWindow.Canvas().Refresh(Application.mainWindow.Content())
148153
}
149154
}
155+
156+
func DotMFileWithVariableCheck(b bool) {
157+
Options.DotMFileWithVariable = b
158+
159+
}
160+
150161
func expertPNGOperation() {
151162
filename := "draw.png"
152163
if input.Text != "" {
@@ -206,6 +217,7 @@ func applyProjectSetting(withInitial bool) {
206217
colInput.Disable()
207218
flatMatrixCheck.Disable()
208219
matlabSaveCheck.Disable()
220+
dotMFileWithVariableCheck.Disable()
209221
oneHotEncodingSaveCheck.Disable()
210222
Options.SettingsSaved = true
211223
if withInitial {
@@ -220,6 +232,7 @@ func resetProjectSetting() {
220232
colInput.Enable()
221233
flatMatrixCheck.Enable()
222234
matlabSaveCheck.Enable()
235+
dotMFileWithVariableCheck.Enable()
223236
oneHotEncodingSaveCheck.Enable()
224237
counterLabel.SetText("0")
225238
Options.SettingsSaved = false
@@ -235,12 +248,29 @@ func resetProjectSetting() {
235248
}, Application.mainWindow,
236249
)
237250
}
251+
252+
func aboutBtn() {
253+
url, _ := url2.Parse("https://github.com/ehsan-torabi")
254+
repoURL, _ := url2.Parse("https://github.com/ehsan-torabi/Draw2Matrix")
255+
richText := widget.NewRichText(
256+
&widget.TextSegment{Text: "Programmed by : Ehsan Torabi Farsani", Style: widget.RichTextStyle{}},
257+
&widget.TextSegment{Text: "\nFor more details, visit our ", Style: widget.RichTextStyle{}},
258+
&widget.HyperlinkSegment{Text: "Github", URL: url},
259+
&widget.TextSegment{Text: "\t", Style: widget.RichTextStyle{}},
260+
&widget.HyperlinkSegment{Text: "Draw2Matrix Repository", URL: repoURL},
261+
&widget.TextSegment{Text: "\n", Style: widget.RichTextStyle{}},
262+
)
263+
dialog.NewCustom("About", "exit", richText, Application.mainWindow).Show()
264+
265+
}
266+
238267
func labelValidator(s string) error {
239268
if len(s) > 20 {
240269
return fmt.Errorf("label too long")
241270
}
242271
return nil
243272
}
273+
244274
func rowValidator(s string) error {
245275
val, err := strconv.Atoi(s)
246276
if err != nil || val <= 0 {
@@ -296,6 +326,7 @@ func loadProjectFile(reader io.ReadCloser) error {
296326
colInput.Text = strconv.Itoa(Options.MatrixCol - 1)
297327
oneHotEncodingSaveCheck.SetChecked(Options.OneHotEncodingSave)
298328
matlabSaveCheck.SetChecked(Options.MatlabSaveFormat)
329+
dotMFileWithVariableCheck.SetChecked(Options.DotMFileWithVariable)
299330
flatMatrixCheck.SetChecked(Options.FlatMatrix)
300331
Application.mainWindow.Content().Refresh()
301332
return nil

dataTools.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,13 @@ func AddToFileForMatlab(inputData [][]int8, outputData string) {
155155
// SaveFileForMatlab saves the matrix data and target data to separate files
156156
// in MATLAB compatible format
157157
func SaveFileForMatlab(dirPath, dataFileName, targetFileName string) error {
158+
extension := ".txt"
159+
if Options.DotMFileWithVariable {
160+
extension = ".m"
161+
}
158162
// Prepare file paths
159-
dataPath := filepath.Join(dirPath, dataFileName+".txt")
160-
targetPath := filepath.Join(dirPath, targetFileName+".txt")
163+
dataPath := filepath.Join(dirPath, dataFileName+extension)
164+
targetPath := filepath.Join(dirPath, targetFileName+extension)
161165

162166
// Create data file
163167
dataFile, err := os.OpenFile(dataPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
@@ -175,18 +179,29 @@ func SaveFileForMatlab(dirPath, dataFileName, targetFileName string) error {
175179

176180
// Write processed matrix data
177181
finalData := processForMatlabString(transposeMatrix(TempData.TempMatrix))
182+
if Options.DotMFileWithVariable {
183+
finalData = dataFileName + " = " + finalData + ";"
184+
}
178185
if _, err = dataFile.WriteString(finalData); err != nil {
179186
return err
180187
}
181188

182189
if Options.OneHotEncodingSave {
183-
_, err = targetFile.WriteString(oneHotSaveUtil())
190+
finalTarget := oneHotSaveUtil()
191+
if Options.DotMFileWithVariable {
192+
finalTarget = targetFileName + " = " + finalTarget + ";"
193+
}
194+
_, err = targetFile.WriteString(finalTarget)
184195
if err != nil {
185196
log.Println(err)
186197
return err
187198
}
188199
} else {
189-
_, err = targetFile.WriteString(fmt.Sprintf("%v", TempData.TempTarget))
200+
finalTarget := fmt.Sprintf("%v", TempData.TempTarget)
201+
if Options.DotMFileWithVariable {
202+
finalTarget = targetFileName + " = " + finalTarget + ";"
203+
}
204+
_, err = targetFile.WriteString(finalTarget)
190205
if err != nil {
191206
log.Println(err)
192207
return err

main.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ import (
1616

1717
// Options stores the global application settings
1818
var Options struct {
19-
FlatMatrix bool // Whether to flatten the matrix when saving
20-
MatlabSaveFormat bool // Whether to save in MATLAB compatible format
21-
MatrixCol int // Number of columns in the output matrix
22-
MatrixRow int // Number of rows in the output matrix
23-
SettingsSaved bool // Whether settings have been Saved and locked
24-
OneHotEncodingSave bool // Whether to save target to one-hot-encoding format
19+
FlatMatrix bool // Whether to flatten the matrix when saving
20+
MatlabSaveFormat bool // Whether to save in MATLAB compatible format
21+
DotMFileWithVariable bool // Whether to save array in variable for matlab in .m file
22+
MatrixCol int // Number of columns in the output matrix
23+
MatrixRow int // Number of rows in the output matrix
24+
SettingsSaved bool // Whether settings have been Saved and locked
25+
OneHotEncodingSave bool // Whether to save target to one-hot-encoding format
2526
}
2627

2728
var (
@@ -68,6 +69,7 @@ func main() {
6869

6970
matlabSaveCheck.Checked = false
7071
flatMatrixCheck.Checked = false
72+
dotMFileWithVariableCheck.Disable()
7173

7274
rowInput.SetPlaceHolder("Rows")
7375
rowInput.SetText(strconv.Itoa(Options.MatrixRow))

mainWidgets.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ var (
2929
flatMatrixCheck = widget.NewCheck("Flat Matrix", func(b bool) {
3030
Options.FlatMatrix = b
3131
})
32-
matlabSaveCheck = widget.NewCheck("Matlab Save Format", matlabSaveCheckBoxFunction)
33-
oneHotEncodingSaveCheck = widget.NewCheck("One Hot Encoding Save", oneHotEncodingCheckBoxFunction)
34-
colInput = widget.NewEntry()
35-
rowInput = widget.NewEntry()
36-
addBtn = widget.NewButtonWithIcon("Add", theme.ContentAddIcon(), addButtonFunction)
37-
addAndClearPaintBtn = widget.NewButtonWithIcon("Add & Clear Paint", theme.ContentCutIcon(), func() {
32+
matlabSaveCheck = widget.NewCheck("Matlab Save Format", matlabSaveCheckBoxFunction)
33+
dotMFileWithVariableCheck = widget.NewCheck(".m file save", DotMFileWithVariableCheck)
34+
oneHotEncodingSaveCheck = widget.NewCheck("One Hot Encoding Save", oneHotEncodingCheckBoxFunction)
35+
colInput = widget.NewEntry()
36+
rowInput = widget.NewEntry()
37+
addBtn = widget.NewButtonWithIcon("Add", theme.ContentAddIcon(), addButtonFunction)
38+
addAndClearPaintBtn = widget.NewButtonWithIcon("Add & Clear Paint", theme.ContentCutIcon(), func() {
3839
addButtonFunction()
3940
Application.paintObject.Clear()
4041
})
@@ -44,7 +45,8 @@ var (
4445
resetProjectBtn = widget.NewButtonWithIcon("Reset Project", theme.ContentClearIcon(), resetProjectSetting)
4546
toolbar = widget.NewToolbar(
4647
widget.NewToolbarAction(theme.DocumentSaveIcon(), saveProjectFileFunction),
47-
widget.NewToolbarAction(theme.ContentUndoIcon(), loadProjectFileFunction))
48+
widget.NewToolbarAction(theme.ContentUndoIcon(), loadProjectFileFunction),
49+
widget.NewToolbarAction(theme.InfoIcon(), aboutBtn))
4850
)
4951

5052
// Layout containers
@@ -53,7 +55,7 @@ var (
5355
openPaint,
5456
widget.NewLabel("Matrix Settings:"),
5557
container.NewGridWithColumns(2, rowInput, colInput),
56-
container.NewGridWithColumns(3, flatMatrixCheck, matlabSaveCheck, oneHotEncodingSaveCheck),
58+
container.NewGridWithColumns(4, flatMatrixCheck, matlabSaveCheck, dotMFileWithVariableCheck, oneHotEncodingSaveCheck),
5759
container.NewGridWithColumns(2, resetProjectBtn, saveOptionsBtn),
5860
)
5961

0 commit comments

Comments
 (0)