This repository has been archived by the owner on Apr 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tabMakeSort.go
137 lines (119 loc) · 3.87 KB
/
tabMakeSort.go
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
// tabMakeSort.go
package main
import (
"fmt"
"github.com/andlabs/ui"
"github.com/hfmrow/csveditor/genLib"
)
func tabMakeSort(vbox *ui.Box) *ui.Box {
if CsvProfileList.Initialised { // If there is nothing to display, skip it ...
vbox.Enable() // Used to know if control has been already initialised
// Create options Form
entryForm := ui.NewForm()
entryForm.SetPadded(true)
vbox.Append(entryForm, false)
// First part in a group
gridSort := ui.NewGrid()
gridSort.SetPadded(true)
groupSort := ui.NewGroup(" C.S.V sorting: " + genLib.TruncateString(CsvProfileList.FileName, "...", 80, 1))
groupSort.SetMargined(true)
groupSort.SetChild(gridSort)
vbox.Append(groupSort, false)
// Set COLUMN sort storage
sortFields = make([]int, 0)
// Add controls
addColBtn := ui.NewButton("Add field")
clrColBtn := ui.NewButton("Reset all")
caseSvChk := ui.NewCheckbox("Case sensitive")
doSortBtn := ui.NewButton("Sort")
addColCbx := ui.NewCombobox()
for _, col := range CsvProfileList.FieldNames {
addColCbx.Append(col)
}
addColCbx.SetSelected(0)
// Add OnClicked
addColBtn.OnClicked(func(*ui.Button) { // Add button OnClicked
var addOk bool = true
for _, fieldIdx := range sortFields {
if fieldIdx == addColCbx.Selected() {
addOk = false
break
}
}
if addOk {
sortFields = append(sortFields, addColCbx.Selected())
refreshSortFields()
}
})
// Clear OnClicked
clrColBtn.OnClicked(func(*ui.Button) { // Clear button OnClicked
resetSortFields()
})
// Case sensitive OnToggled
caseSvChk.OnToggled(func(*ui.Checkbox) { caseSensitiveSortFlag = caseSvChk.Checked() })
// Sort OnClicked
doSortBtn.OnClicked(func(*ui.Button) { // Sort button OnClicked
if len(sortFields) != 0 {
// Get column names for the new table
var tmpDatas [][]string
tmpDatas = append(tmpDatas, CsvProfileList.FieldNames)
// Duplicate table to make independant one
sortedDatas := make([][]string, len(tableDatas))
copy(sortedDatas, tableDatas)
// Create temp filename to store result ...
filename := tempDir + genLib.GenFileName() + ".tmp"
for _, fieldIdx := range sortFields {
switch CsvProfileList.FieldType[fieldIdx] {
case TypE[0]: // Date
sortedDatas = genLib.SliceSortDate(sortedDatas, CsvProfileList.DateFormat+" %H:%M:%S", fieldIdx, -1, sortAscDscCheckbox[fieldIdx].fChkbox.Checked())
case TypE[1]: // String
genLib.SliceSortString(sortedDatas, fieldIdx, sortAscDscCheckbox[fieldIdx].fChkbox.Checked(), caseSensitiveSortFlag)
case TypE[2]: // Numeric
genLib.SliceSortFloat(sortedDatas, fieldIdx, sortAscDscCheckbox[fieldIdx].fChkbox.Checked(), CsvProfileList.DecimalSep)
default:
fmt.Println("Sort Error: Bad field type !")
}
}
CsvProfileList.Modified = true // Something changed, store information
sortedDatas = append(tmpDatas, sortedDatas...)
// Write it
genLib.WriteCsv(filename, ",", sortedDatas)
// Load, Store and set CSV
addAndSetCsv(filename, "Sort")
ChkRow.Reset() // Reset stored checked rows history
// Refresh tabs
refreshTable()
}
})
// Display controls
gridSort.Append(addColBtn,
0, 0, 1, 1,
true, ui.AlignEnd, false, ui.AlignCenter)
gridSort.Append(addColCbx,
1, 0, 1, 1,
true, ui.AlignCenter, false, ui.AlignCenter)
gridSort.Append(clrColBtn,
2, 0, 1, 1,
true, ui.AlignStart, false, ui.AlignCenter)
gridSort.Append(caseSvChk,
3, 0, 1, 1,
true, ui.AlignStart, false, ui.AlignCenter)
gridSort.Append(doSortBtn,
4, 0, 1, 1,
true, ui.AlignStart, false, ui.AlignCenter)
sortBox = tabMakeSortFields(vbox)
}
return vbox
}
func resetSortFields() {
if sortFieldsFlag {
sortBox.Delete(3)
sortFieldsFlag = false
sortFields = sortFields[:0]
sortAscDscCheckbox = sortAscDscCheckbox[:0]
}
}
func refreshSort() {
mainTab.Delete(2)
mainTab.InsertAt("Sort", 2, tabSort())
}