/
file_model.go
125 lines (104 loc) · 2.74 KB
/
file_model.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
package d2
import (
"github.com/therecipe/qt/core"
)
// File ...
type File struct {
core.QObject
Name string
D2Path string
RemoteCRC string
LocalCRC string
FileAction string
}
// Model Roles.
const (
Name = int(core.Qt__UserRole) + 1<<iota
D2Path
RemoteCRC
LocalCRC
FileAction
)
// FileModel represents a patch file.
type FileModel struct {
core.QAbstractListModel
_ func() `constructor:"init"`
_ map[int]*core.QByteArray `property:"roles"`
_ []*File `property:"files"`
_ func(*File) `slot:"addFile"`
_ func() `slot:"clear"`
}
func (m *FileModel) init() {
m.SetRoles(map[int]*core.QByteArray{
Name: core.NewQByteArray2("name", -1),
D2Path: core.NewQByteArray2("d2Path", -1),
RemoteCRC: core.NewQByteArray2("remoteCRC", -1),
LocalCRC: core.NewQByteArray2("localCRC", -1),
FileAction: core.NewQByteArray2("fileAction", -1),
})
m.ConnectData(m.data)
m.ConnectRowCount(m.rowCount)
m.ConnectColumnCount(m.columnCount)
m.ConnectRoleNames(m.roleNames)
m.ConnectAddFile(m.addFile)
m.ConnectClear(m.clear)
}
func (m *FileModel) rowCount(*core.QModelIndex) int {
return len(m.Files())
}
func (m *FileModel) columnCount(*core.QModelIndex) int {
return 1
}
func (m *FileModel) roleNames() map[int]*core.QByteArray {
return m.Roles()
}
func (m *FileModel) data(index *core.QModelIndex, role int) *core.QVariant {
if !index.IsValid() {
return core.NewQVariant()
}
if index.Row() >= len(m.Files()) {
return core.NewQVariant()
}
item := m.Files()[index.Row()]
switch role {
case Name:
return core.NewQVariant1(item.Name)
case D2Path:
return core.NewQVariant1(item.D2Path)
case RemoteCRC:
return core.NewQVariant1(item.RemoteCRC)
case LocalCRC:
return core.NewQVariant1(item.LocalCRC)
case FileAction:
return core.NewQVariant1(item.FileAction)
default:
return core.NewQVariant()
}
}
// addFile adds a file to the model.
func (m *FileModel) addFile(g *File) {
m.BeginInsertRows(core.NewQModelIndex(), len(m.Files()), len(m.Files()))
m.SetFiles(append(m.Files(), g))
m.EndInsertRows()
}
// updateFile will notify the UI of the updated model item.
func (m *FileModel) updateFile(index int) {
var fIndex = m.Index(0, 0, core.NewQModelIndex())
var lIndex = m.Index(index, 0, core.NewQModelIndex())
m.DataChanged(fIndex, lIndex, []int{Name, D2Path, LocalCRC, RemoteCRC, FileAction})
}
// removeFile will remove a file from the model.
func (m *FileModel) removeFile(index int) {
m.BeginRemoveRows(core.NewQModelIndex(), index, index)
m.SetFiles(append(m.Files()[:index], m.Files()[index+1:]...))
m.EndRemoveRows()
}
func (m *FileModel) clear() {
m.BeginResetModel()
m.SetFiles([]*File{})
m.EndResetModel()
}
func init() {
FileModel_QRegisterMetaType()
File_QRegisterMetaType()
}