-
Notifications
You must be signed in to change notification settings - Fork 11
/
storge.go
108 lines (95 loc) · 2.29 KB
/
storge.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
package main
import (
"bytes"
"encoding/gob"
"reflect"
"github.com/therecipe/qt/core"
"github.com/therecipe/qt/gui"
)
type attachmentFile struct {
Name string
Data []byte
}
type qtextFormat struct {
Html string
Images map[string][]byte
Attachments []attachmentFile
}
func (s *myWindow) getRichText() []byte {
var data qtextFormat
data.Html = s.editor.ToHtml()
data.Images = s.getImagesMap(data.Html)
data.Attachments = s.document.Attachments
buf := bytes.NewBufferString("")
encoder := gob.NewEncoder(buf)
err := encoder.Encode(data)
if err != nil {
return nil
}
return buf.Bytes()
}
func (s *myWindow) getQText(data []byte) (*qtextFormat, error) {
buf := bytes.NewReader(data)
decoder := gob.NewDecoder(buf)
s.document.Images = make(map[string][]byte)
s.document.Attachments = []attachmentFile{}
err := decoder.Decode(&s.document)
if err == nil {
s.loadImgResources(s.document.Images)
}
return &s.document, err
}
func (s *myWindow) getImagesMap(html string) map[string][]byte {
urls := s.getImageList(html)
imgMap := make(map[string][]byte)
for _, url := range urls {
buf, ok := s.document.Images[url]
if ok {
imgMap[url] = buf
}
}
return imgMap
}
func (s *myWindow) loadImgResources(imgs map[string][]byte) {
for url, data := range imgs {
img := gui.NewQImage()
ok := img.LoadFromData(data, len(data), "")
if !ok {
return
}
uri := core.NewQUrl3(url, core.QUrl__TolerantMode)
s.editor.Document().AddResource(int(gui.QTextDocument__ImageResource), uri, img.ToVariant())
}
}
func (s *myWindow) removeCurAttachment() {
idx := s.comboAttachs.CurrentIndex()
if idx == 0 {
return
}
attachs := sliceValueRemoveIndex(s.document.Attachments, idx-1)
s.document.Attachments = attachs.([]attachmentFile)
s.showAttachList()
s.editor.Document().SetModified(true)
//curDiary.Modified = true
}
func sliceValueRemoveIndex(v interface{}, idx int) interface{} {
val := reflect.ValueOf(v)
if val.Type().Kind() != reflect.Slice {
return v
}
length := val.Len()
if length == 0 {
return v
}
if idx >= length || idx < 0 {
return v
}
res := reflect.MakeSlice(val.Type(), length-1, length-1)
for i := 0; i < idx; i++ {
res.Index(i).Set(val.Index(i))
}
for i := idx + 1; i < length; i++ {
res.Index(i - 1).Set(val.Index(i))
}
return res.Interface()
}