-
Notifications
You must be signed in to change notification settings - Fork 736
/
personmodel.go
116 lines (89 loc) · 2.38 KB
/
personmodel.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
package main
import "github.com/therecipe/qt/core"
const (
FirstName = int(core.Qt__UserRole) + 1<<iota
LastName
)
type PersonModel struct {
core.QAbstractListModel
_ func() `constructor:"init"`
_ map[int]*core.QByteArray `property:"roles"`
_ []*Person `property:"people"`
_ func(*Person) `slot:"addPerson"`
_ func(row int, firstName, lastName string) `slot:"editPerson"`
_ func(row int) `slot:"removePerson"`
}
type Person struct {
core.QObject
_ string `property:"firstName"`
_ string `property:"lastName"`
}
func init() {
Person_QRegisterMetaType()
}
func (m *PersonModel) init() {
m.SetRoles(map[int]*core.QByteArray{
FirstName: core.NewQByteArray2("firstName", -1),
LastName: core.NewQByteArray2("lastName", -1),
})
m.ConnectData(m.data)
m.ConnectRowCount(m.rowCount)
m.ConnectColumnCount(m.columnCount)
m.ConnectRoleNames(m.roleNames)
m.ConnectAddPerson(m.addPerson)
m.ConnectEditPerson(m.editPerson)
m.ConnectRemovePerson(m.removePerson)
}
func (m *PersonModel) data(index *core.QModelIndex, role int) *core.QVariant {
if !index.IsValid() {
return core.NewQVariant()
}
if index.Row() >= len(m.People()) {
return core.NewQVariant()
}
var p = m.People()[index.Row()]
switch role {
case FirstName:
{
return core.NewQVariant1(p.FirstName())
}
case LastName:
{
return core.NewQVariant1(p.LastName())
}
default:
{
return core.NewQVariant()
}
}
}
func (m *PersonModel) rowCount(parent *core.QModelIndex) int {
return len(m.People())
}
func (m *PersonModel) columnCount(parent *core.QModelIndex) int {
return 1
}
func (m *PersonModel) roleNames() map[int]*core.QByteArray {
return m.Roles()
}
func (m *PersonModel) addPerson(p *Person) {
m.BeginInsertRows(core.NewQModelIndex(), len(m.People()), len(m.People()))
m.SetPeople(append(m.People(), p))
m.EndInsertRows()
}
func (m *PersonModel) editPerson(row int, firstName string, lastName string) {
var p = m.People()[row]
if firstName != "" {
p.SetFirstName(firstName)
}
if lastName != "" {
p.SetLastName(lastName)
}
var pIndex = m.Index(row, 0, core.NewQModelIndex())
m.DataChanged(pIndex, pIndex, []int{FirstName, LastName})
}
func (m *PersonModel) removePerson(row int) {
m.BeginRemoveRows(core.NewQModelIndex(), row, row)
m.SetPeople(append(m.People()[:row], m.People()[row+1:]...))
m.EndRemoveRows()
}