/
searchmodel.go
130 lines (101 loc) · 2.56 KB
/
searchmodel.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
package main
import (
"github.com/therecipe/qt/core"
)
const (
Date = int(core.Qt__UserRole) + 1<<iota
Description
ID
)
type SearchModel struct {
core.QAbstractListModel
_ func() `constructor:"init"`
_ map[int]*core.QByteArray `property:"roles"`
_ []*Search `property:"rows"`
_ func(*Search) `slot:"addSearch"`
_ func(row int, date, description, id string) `slot:"editSearch"`
_ func(row int) `slot:"removeSearch"`
}
type Search struct {
core.QObject
_ string `property:"description"`
_ string `property:"date"`
_ string `property:"id"`
}
func init() {
Search_QRegisterMetaType()
}
func (m *SearchModel) init() {
m.SetRoles(map[int]*core.QByteArray{
Description: core.NewQByteArray2("description", len("description")),
Date: core.NewQByteArray2("date", len("date")),
ID: core.NewQByteArray2("id", len("id")),
})
m.ConnectData(m.data)
m.ConnectRowCount(m.rowCount)
m.ConnectColumnCount(m.columnCount)
m.ConnectRoleNames(m.roleNames)
m.ConnectAddSearch(m.addSearch)
m.ConnectEditSearch(m.editSearch)
m.ConnectRemoveSearch(m.removeSearch)
}
func (m *SearchModel) data(index *core.QModelIndex, role int) *core.QVariant {
if !index.IsValid() {
return core.NewQVariant()
}
if index.Row() >= len(m.Rows()) {
return core.NewQVariant()
}
var p = m.Rows()[index.Row()]
switch role {
case Description:
{
return core.NewQVariant14(p.Description())
}
case Date:
{
return core.NewQVariant14(p.Date())
}
case ID:
{
return core.NewQVariant14(p.Id())
}
default:
{
return core.NewQVariant()
}
}
}
func (m *SearchModel) rowCount(parent *core.QModelIndex) int {
return len(m.Rows())
}
func (m *SearchModel) columnCount(parent *core.QModelIndex) int {
return 3
}
func (m *SearchModel) roleNames() map[int]*core.QByteArray {
return m.Roles()
}
func (m *SearchModel) addSearch(p *Search) {
m.BeginInsertRows(core.NewQModelIndex(), len(m.Rows()), len(m.Rows()))
m.SetRows(append(m.Rows(), p))
m.EndInsertRows()
}
func (m *SearchModel) editSearch(row int, date string, description string, id string) {
var p = m.Rows()[row]
if date != "" {
p.SetDate(date)
}
if description != "" {
p.SetDescription(description)
}
if id != "" {
p.SetId(id)
}
var pIndex = m.Index(row, 0, core.NewQModelIndex())
m.DataChanged(pIndex, pIndex, []int{Description, Date, ID})
}
func (m *SearchModel) removeSearch(row int) {
m.BeginRemoveRows(core.NewQModelIndex(), row, row)
m.SetRows(append(m.Rows()[:row], m.Rows()[row+1:]...))
m.EndRemoveRows()
}