-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
177 lines (123 loc) · 3.35 KB
/
main.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
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
package main
import (
"bufio"
"errors"
"fmt"
"log"
"runtime"
"strings"
"os"
"github.com/monirz/gotri"
"github.com/therecipe/qt/core"
"github.com/therecipe/qt/gui"
"github.com/therecipe/qt/qml"
"github.com/therecipe/qt/quickcontrols2"
"github.com/therecipe/qt/widgets"
)
var (
qmlObjects = make(map[string]*core.QObject)
qmlBridge *QmlBridge
manipulatedFromQml *widgets.QWidget
)
var t *gotri.Trie
func main() {
var (
f *os.File
err error
)
if runtime.GOOS == "linux" || runtime.GOOS == "darwin" {
filePath := "/usr/share/wordgo/dictionary.txt"
_, err := os.Stat(filePath)
if os.IsNotExist(err) {
filePath = "dictionary.txt"
}
f, err = os.Open(filePath)
if err != nil {
err = errors.New("dictionary.txt file is not found in /usr/share or in the current directory")
log.Fatal(err)
}
} else {
f, err = os.Open("dictionary.txt")
if err != nil {
err = errors.New("dictionary.txt file is not found in the current directory")
log.Fatal(err)
}
}
defer f.Close()
scanner := bufio.NewScanner(f)
count := 0
t = gotri.New()
for scanner.Scan() {
splitted := strings.Split(scanner.Text(), "|")
count++
// if count == 5000 {
// break
// }
t.Add(splitted[0], splitted[1])
}
core.QCoreApplication_SetAttribute(core.Qt__AA_EnableHighDpiScaling, true)
gui.NewQGuiApplication(len(os.Args), os.Args)
quickcontrols2.QQuickStyle_SetStyle("Material")
bridge := initQmlBridge()
// create the qml application engine
engine := qml.NewQQmlApplicationEngine(nil)
engine.RootContext().SetContextProperty("qmlBridge", bridge)
engine.Load(core.NewQUrl3("qrc:/qml/main.qml", 0))
gui.QGuiApplication_Exec()
}
type QmlBridge struct {
core.QObject
core.QAbstractListModel
// _ func() `constructor:"init"`
Wr []string `property:"wrds"`
_ string `property:"word"`
_ func(source, action, data string) `signal:"sendToQml"`
_ func(data string) []string `slot:"sendToGo"`
_ func(id string) []string `slot:"find"`
_ func(obj []*core.QVariant) `signal:"add,auto"`
}
func (wm *QmlBridge) find(key string) string {
if len(qmlBridge.Wr) > 0 {
return qmlBridge.Wr[0]
}
return "No value"
}
func initQmlBridge() *QmlBridge {
qmlBridge = NewQmlBridge(nil)
qmlBridge.ConnectSendToGo(func(data string) []string {
wordList := t.GetSuggestion(data, 10)
qmlBridge.Wr = wordList
qmlBridge.SetWrds(wordList)
// qmlBridge.SetWord(data)
return wordList
})
qmlBridge.ConnectRowCount(qmlBridge.rowCount)
qmlBridge.ConnectData(qmlBridge.data)
qmlBridge.ConnectFind(func(data string) []string {
word, ok := t.Search(data)
wordList := strings.Split(word, ",")
if !ok {
return wordList
}
return wordList
})
return qmlBridge
}
func (qm *QmlBridge) init() {
}
func (qm *QmlBridge) rowCount(*core.QModelIndex) int {
return len(qm.Wr)
}
func (qm *QmlBridge) data(index *core.QModelIndex, role int) *core.QVariant {
if role != int(core.Qt__DisplayRole) {
return core.NewQVariant()
}
item := qm.Wr[index.Row()]
return core.NewQVariant1(fmt.Sprintf("%v", item))
}
//---------Add
func (qm *QmlBridge) add(item []*core.QVariant) {
// qm.BeginInsertRows(core.NewQModelIndex(), len(qm.modelData), len(qm.modelData))
// qm.modelData = append(qm.modelData, ListItem{item[0].ToString(), item[1].ToString()})
qm.EndInsertRows()
}