3 Star 27 Fork 3

rocket049 / secret-diary

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
editor.go 21.71 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
package main
import (
"bufio"
"fmt"
"io/ioutil"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
"github.com/therecipe/qt/core"
"github.com/therecipe/qt/gui"
"github.com/therecipe/qt/widgets"
)
func (s *myWindow) setHeader(level int) {
var cfmt = gui.NewQTextCharFormat()
cfmt.SetFont2(s.app.Font())
switch level {
case 1:
cfmt.SetFontPointSize(18)
case 2:
cfmt.SetFontPointSize(16)
case 3:
cfmt.SetFontPointSize(14)
default:
cfmt.SetFontPointSize(12)
}
cfmt.SetForeground(gui.NewQBrush3(gui.NewQColor2(core.Qt__blue), core.Qt__SolidPattern))
s.mergeFormatOnLineOrSelection(cfmt)
}
func (s *myWindow) setStandard() {
var cfmt = gui.NewQTextCharFormat()
cfmt.SetFont2(s.app.Font())
cfmt.SetFontPointSize(14)
cfmt.SetForeground(gui.NewQBrush3(gui.NewQColor2(core.Qt__black), core.Qt__SolidPattern))
s.mergeFormatOnLineOrSelection(cfmt)
}
func (s *myWindow) textStyle(styleIndex int) {
var cursor = s.editor.TextCursor()
if styleIndex != 0 {
var style = gui.QTextListFormat__ListDisc
switch styleIndex {
case 1:
{
style = gui.QTextListFormat__ListDisc
}
case 2:
{
style = gui.QTextListFormat__ListCircle
}
case 3:
{
style = gui.QTextListFormat__ListSquare
}
case 4:
{
style = gui.QTextListFormat__ListDecimal
}
case 5:
{
style = gui.QTextListFormat__ListLowerAlpha
}
case 6:
{
style = gui.QTextListFormat__ListUpperAlpha
}
case 7:
{
style = gui.QTextListFormat__ListLowerRoman
}
case 8:
{
style = gui.QTextListFormat__ListUpperRoman
}
}
cursor.BeginEditBlock()
var (
blockFmt = cursor.BlockFormat()
listFmt = gui.NewQTextListFormat()
)
if cursor.CurrentList().Pointer() != nil {
listFmt = gui.NewQTextListFormatFromPointer(cursor.CurrentList().Format().Pointer())
} else {
listFmt.SetIndent(blockFmt.Indent() + 1)
blockFmt.SetIndent(0)
cursor.SetBlockFormat(blockFmt)
}
listFmt.SetStyle(style)
cursor.CreateList(listFmt)
cursor.EndEditBlock()
} else {
var bfmt = gui.NewQTextBlockFormat()
bfmt.SetObjectIndex(-1)
cursor.SetBlockFormat(bfmt)
}
}
// addIndent 增加缩进量,n=1(增加1) or n=-1(减少1)
func (s *myWindow) addIndent(n int) {
cursor := s.editor.TextCursor()
cursor.BeginEditBlock()
var (
blockFmt = cursor.BlockFormat()
listFmt = gui.NewQTextListFormat()
)
if cursor.CurrentList().Pointer() != nil {
listFmt = gui.NewQTextListFormatFromPointer(cursor.CurrentList().Format().Pointer())
if listFmt.Indent()+n >= 0 {
listFmt.SetIndent(listFmt.Indent() + n)
cursor.CreateList(listFmt)
}
} else {
if blockFmt.Indent()+n >= 0 {
blockFmt.SetIndent(blockFmt.Indent() + n)
cursor.SetBlockFormat(blockFmt)
}
}
cursor.EndEditBlock()
}
func (s *myWindow) textColor() {
var col = widgets.QColorDialog_GetColor(s.editor.TextColor(), s.editor, "", 0)
if !col.IsValid() {
return
}
var cfmt = gui.NewQTextCharFormat()
cfmt.SetForeground(gui.NewQBrush3(col, core.Qt__SolidPattern))
s.mergeFormatOnLineOrSelection(cfmt)
}
func (s *myWindow) textBgColor() {
var col = widgets.QColorDialog_GetColor(s.editor.TextColor(), s.editor, "", 0)
if !col.IsValid() {
return
}
var cfmt = gui.NewQTextCharFormat()
cfmt.SetBackground(gui.NewQBrush3(col, core.Qt__SolidPattern))
s.mergeFormatOnLineOrSelection(cfmt)
}
func (s *myWindow) textBold() {
var afmt = gui.NewQTextCharFormat()
var fw = gui.QFont__Normal
if s.actionTextBold.IsChecked() {
fw = gui.QFont__Bold
}
afmt.SetFontWeight(int(fw))
s.mergeFormatOnLineOrSelection(afmt)
}
func (s *myWindow) textUnderline() {
var afmt = gui.NewQTextCharFormat()
afmt.SetFontUnderline(s.actionTextUnderline.IsChecked())
s.mergeFormatOnLineOrSelection(afmt)
}
func (s *myWindow) textStrikeOut() {
var afmt = gui.NewQTextCharFormat()
afmt.SetFontStrikeOut(s.actionStrikeOut.IsChecked())
s.mergeFormatOnLineOrSelection(afmt)
}
func (s *myWindow) textItalic() {
var afmt = gui.NewQTextCharFormat()
afmt.SetFontItalic(s.actionTextItalic.IsChecked())
s.mergeFormatOnLineOrSelection(afmt)
}
func (s *myWindow) insertImage() {
filename := widgets.QFileDialog_GetOpenFileName(s.window, "select a file", ".", "Image (*.png *.jpg)", "Image (*.png *.jpg)", widgets.QFileDialog__ReadOnly)
data, err := ioutil.ReadFile(filename)
if err != nil {
return
}
img := gui.NewQImage()
ok := img.LoadFromData(data, len(data), "")
if !ok {
return
}
uri := core.NewQUrl3("rc://"+filename, core.QUrl__TolerantMode)
img = s.scaleImage(img)
s.editor.Document().AddResource(int(gui.QTextDocument__ImageResource), uri, img.ToVariant())
url := uri.Url(core.QUrl__None)
cursor := s.editor.TextCursor()
cursor.InsertImage4(img, url)
ba := core.NewQByteArray()
iod := core.NewQBuffer2(ba, nil)
iod.Open(core.QIODevice__WriteOnly)
ok = img.Save2(iod, filepath.Ext(filename)[1:], -1)
//fmt.Println(filepath.Ext(filename))
if ok {
s.document.Images[url] = []byte(ba.Data())
}
//fmt.Println("save image:", ok)
}
func (s *myWindow) scaleImage(src *gui.QImage) (res *gui.QImage) {
dlg := widgets.NewQDialog(s.window, core.Qt__Dialog)
dlg.SetWindowTitle(T("Scale Image Size"))
grid := newGridLayout(dlg)
width := widgets.NewQLabel2(fmt.Sprintf("%s : %d =>", T("Width"), src.Width()), dlg, core.Qt__Widget)
grid.AddWidget2(width, 0, 0, 0)
scaledW := src.Width()
scaledH := src.Height()
delta := 30
if scaledW > s.editor.Width()-delta {
scaledW = s.editor.Geometry().Width() - delta
scaledH = int(float64(src.Height()) * float64(scaledW) / float64(src.Width()))
}
wValidor := gui.NewQIntValidator(dlg)
wValidor.SetRange(10, scaledW)
hValidor := gui.NewQIntValidator(dlg)
hValidor.SetRange(10, scaledH)
widthInput := widgets.NewQLineEdit(dlg)
widthInput.SetText(strconv.Itoa(scaledW))
widthInput.SetValidator(wValidor)
grid.AddWidget2(widthInput, 0, 1, 0)
height := widgets.NewQLabel2(fmt.Sprintf("%s : %d =>", T("Height"), src.Height()), dlg, core.Qt__Widget)
grid.AddWidget2(height, 1, 0, 0)
heightInput := widgets.NewQLineEdit(dlg)
heightInput.SetText(strconv.Itoa(scaledH))
heightInput.SetValidator(hValidor)
grid.AddWidget2(heightInput, 1, 1, 0)
btb := newGridLayout2()
okBtn := widgets.NewQPushButton2(T("OK"), dlg)
btb.AddWidget2(okBtn, 0, 0, 0)
cancelBtn := widgets.NewQPushButton2(T("Cancel"), dlg)
btb.AddWidget2(cancelBtn, 0, 1, 0)
grid.AddLayout2(btb, 2, 0, 1, 2, 0)
dlg.SetLayout(grid)
widthInput.ConnectKeyReleaseEvent(func(e *gui.QKeyEvent) {
w, err := strconv.Atoi(widthInput.Text())
if err != nil {
return
}
w0 := float64(src.Width())
h0 := float64(src.Height())
h := float64(w) * h0 / w0
heightInput.SetText(strconv.Itoa(int(h)))
})
heightInput.ConnectKeyReleaseEvent(func(e *gui.QKeyEvent) {
h, err := strconv.Atoi(heightInput.Text())
if err != nil {
return
}
w0 := float64(src.Width())
h0 := float64(src.Height())
w := float64(h) * w0 / h0
widthInput.SetText(strconv.Itoa(int(w)))
})
okBtn.ConnectClicked(func(b bool) {
w, err := strconv.Atoi(widthInput.Text())
if err != nil {
res = src
}
h, err := strconv.Atoi(heightInput.Text())
if err != nil {
res = src
}
res = src.Scaled2(w, h, core.Qt__KeepAspectRatioByExpanding, core.Qt__SmoothTransformation)
dlg.Hide()
dlg.Destroy(true, true)
})
cancelBtn.ConnectClicked(func(b bool) {
res = src
dlg.Hide()
dlg.Destroy(true, true)
})
dlg.Exec()
if res == nil {
res = src
}
return
}
func (s *myWindow) getImageList(html string) []string {
r := strings.NewReader(html)
bufr := bufio.NewReader(r)
reg1, err := regexp.Compile(`<img[^><]+/>`)
if err != nil {
//fmt.Println(err)
return nil
}
reg2, err := regexp.Compile(`src="([^"]+)"`)
if err != nil {
//fmt.Println(err)
return nil
}
imgs := []string{}
for line, _, err := bufr.ReadLine(); err == nil; line, _, err = bufr.ReadLine() {
line1 := string(line)
res1 := reg1.FindAllString(line1, -1)
imgs = append(imgs, res1...)
}
res := []string{}
for _, img := range imgs {
res2 := reg2.FindStringSubmatch(img)
res = append(res, res2[1])
}
//fmt.Println(res)
return res
}
func (s *myWindow) insertTable() {
dlg := widgets.NewQDialog(s.window, core.Qt__Dialog)
dlg.SetWindowTitle(T("Table Rows and Columns"))
dlg.SetFixedWidth(s.charWidth() * 13)
grid := newGridLayout(dlg)
row := widgets.NewQLabel2(T("Rows:"), dlg, core.Qt__Widget)
grid.AddWidget2(row, 0, 0, 0)
rowInput := widgets.NewQLineEdit(dlg)
rowInput.SetText("3")
rowInput.SetValidator(gui.NewQIntValidator(dlg))
grid.AddWidget2(rowInput, 0, 1, 0)
col := widgets.NewQLabel2(T("Columns:"), dlg, core.Qt__Widget)
grid.AddWidget2(col, 1, 0, 0)
colInput := widgets.NewQLineEdit(dlg)
colInput.SetText("3")
colInput.SetValidator(gui.NewQIntValidator(dlg))
grid.AddWidget2(colInput, 1, 1, 0)
btb := newGridLayout2()
okBtn := widgets.NewQPushButton2(T("OK"), dlg)
btb.AddWidget2(okBtn, 0, 0, 0)
cancelBtn := widgets.NewQPushButton2(T("Cancel"), dlg)
btb.AddWidget2(cancelBtn, 0, 1, 0)
grid.AddLayout2(btb, 2, 0, 1, 2, 0)
dlg.SetLayout(grid)
okBtn.ConnectClicked(func(b bool) {
cursor := s.editor.TextCursor()
r, err := strconv.Atoi(rowInput.Text())
if err != nil {
return
}
c, err := strconv.Atoi(colInput.Text())
if err != nil {
return
}
tbl := cursor.InsertTable2(r, c)
tbl.Format().SetBorderBrush(gui.NewQBrush2(core.Qt__SolidPattern))
A := 'A'
for i := 0; i < c; i++ {
tbl.CellAt(0, i).FirstCursorPosition().InsertText(fmt.Sprintf(" %c ", A+rune(i)))
}
dlg.Hide()
dlg.Destroy(true, true)
})
cancelBtn.ConnectClicked(func(b bool) {
dlg.Hide()
dlg.Destroy(true, true)
})
dlg.SetModal(true)
dlg.Show()
}
func (s *myWindow) addJustifyActions(tb *widgets.QToolBar) {
rsrcPath := ":/qml/icons"
var leftIcon = gui.QIcon_FromTheme2("format-justify-left", gui.NewQIcon5(rsrcPath+"/textleft.png"))
actionAlignLeft := tb.AddAction2(leftIcon, "&Left")
actionAlignLeft.SetPriority(widgets.QAction__LowPriority)
actionAlignLeft.ConnectTriggered(func(b bool) {
s.textAlign(1)
})
var centerIcon = gui.QIcon_FromTheme2("format-justify-center", gui.NewQIcon5(rsrcPath+"/textcenter.png"))
actionAlignCenter := tb.AddAction2(centerIcon, "C&enter")
actionAlignCenter.SetPriority(widgets.QAction__LowPriority)
actionAlignCenter.ConnectTriggered(func(b bool) {
s.textAlign(2)
})
var rightIcon = gui.QIcon_FromTheme2("format-justify-right", gui.NewQIcon5(rsrcPath+"/textright.png"))
actionAlignRight := tb.AddAction2(rightIcon, "&Right")
actionAlignRight.SetPriority(widgets.QAction__LowPriority)
actionAlignRight.ConnectTriggered(func(b bool) {
s.textAlign(3)
})
var fillIcon = gui.QIcon_FromTheme2("format-justify-fill", gui.NewQIcon5(rsrcPath+"/textjustify.png"))
actionAlignJustify := tb.AddAction2(fillIcon, "&Justify")
actionAlignJustify.SetPriority(widgets.QAction__LowPriority)
actionAlignJustify.ConnectTriggered(func(b bool) {
s.textAlign(4)
})
}
func (s *myWindow) textAlign(n int) {
switch n {
case 1:
s.editor.SetAlignment(core.Qt__AlignLeft | core.Qt__AlignAbsolute)
case 2:
s.editor.SetAlignment(core.Qt__AlignHCenter)
case 3:
s.editor.SetAlignment(core.Qt__AlignRight | core.Qt__AlignAbsolute)
case 4:
s.editor.SetAlignment(core.Qt__AlignJustify)
}
}
func (s *myWindow) getTable() (t *gui.QTextTable, cell *gui.QTextTableCell) {
cursor := s.editor.TextCursor()
blk := s.editor.Document().FindBlock(cursor.Position())
for _, frame := range blk.Document().RootFrame().ChildFrames() {
//fmt.Println("table cell:", frame.FrameFormat().IsTableCellFormat(), "table:", frame.FrameFormat().IsTableFormat())
if frame.FrameFormat().IsTableFormat() {
table := gui.NewQTextTableFromPointer(frame.Pointer())
cell := table.CellAt2(cursor.Position())
//fmt.Println(cell.Row(), cell.Column())
return table, cell
}
}
return nil, nil
}
func (s *myWindow) clearFormatAtCursor() {
cursor := s.editor.TextCursor()
block := cursor.Block()
cfmt := block.CharFormat()
cfmt.ClearBackground()
cfmt.ClearForeground()
cfmt.SetFontUnderline(false)
cfmt.SetFontWeight(int(gui.QFont__Normal))
cfmt.SetFontPointSize(14)
cfmt.SetFontItalic(false)
cfmt.SetFontStrikeOut(false)
var bfmt = gui.NewQTextBlockFormat()
bfmt.SetObjectIndex(-1)
if !cursor.HasSelection() {
cursor.Select(gui.QTextCursor__LineUnderCursor)
}
cursor.SetCharFormat(cfmt)
cursor.SetBlockFormat(bfmt)
s.editor.SetCurrentCharFormat(cfmt)
}
func OpenDiaryNewWindow(parent *myWindow, id int) *myWindow {
win := new(myWindow)
win.OpenNewWindow(parent, id)
parent.LockEditor()
return win
}
func (s *myWindow) charWidth() int {
// font := gui.NewQFont()
// font.SetFamily("Serif")
// if runtime.GOOS == "windows" {
// font.SetPointSize(16)
// } else {
// //font.SetFamily("Serif Regular")
// font.SetPointSize(12)
// }
// fm := gui.NewQFontMetrics(font)
// return fm.BoundingRect2("宽W").Width()
return 24
}
func (s *myWindow) OpenNewWindow(parent *myWindow, id int) {
s.key = parent.key
s.curDiary = new(diaryPointer)
s.curDiary.Id = id
s.db = parent.db
s.window = widgets.NewQMainWindow(parent.window, core.Qt__Window)
s.window.SetWindowTitle(parent.user)
s.window.SetMinimumSize2(800, 600)
s.window.SetWindowIcon(gui.NewQIcon5(":/qml/icons/Sd.png"))
grid := newGridLayout2()
frame := widgets.NewQFrame(s.window, core.Qt__Widget)
s.window.SetCentralWidget(frame)
charW := s.charWidth()
editor := s.createEditor(charW)
s.window.SetMinimumWidth(s.editor.Width() + 100)
grid.AddWidget3(editor, 0, 0, 1, 1, 0)
//grid.SetAlign(core.Qt__AlignTop)
s.setToolBar()
s.setMenuBar()
s.exportEnc.SetEnabled(true)
s.exportPdf.SetEnabled(true)
s.exportOdt.SetEnabled(true)
s.importEnc.SetDisabled(true)
s.newDiary.SetDisabled(true)
s.renDiary.SetDisabled(true)
s.modifyPwd.SetDisabled(true)
//s.setStandaloneFuncs()
s.setEditorFuncs()
frame.SetLayout(grid)
once := sync.Once{}
s.window.ConnectShowEvent(func(e *gui.QShowEvent) {
once.Do(func() {
s.loadDiary()
})
})
s.window.ConnectCloseEvent(func(e *gui.QCloseEvent) {
if s.editor.Document().IsModified() {
ret := widgets.QMessageBox_Question(s.window, T("Close"), T("Do you want to save the document?"), widgets.QMessageBox__Yes|widgets.QMessageBox__No, widgets.QMessageBox__Yes)
if ret == widgets.QMessageBox__Yes {
s.saveCurDiary()
}
}
})
s.window.Show()
}
func (s *myWindow) loadDiary() {
filename := strconv.Itoa(s.curDiary.Id) + ".dat"
data, err := decodeFromFile(filename, s.key)
if err != nil {
s.setStatusBar(err.Error())
} else {
s.getQText(data)
s.editor.Document().SetHtml(s.document.Html)
s.showAttachList()
s.editor.Document().SetModified(false)
s.editor.SetReadOnly(false)
}
}
func (s *myWindow) saveDiaryAlone() {
if s.editor.Document().IsModified() == false {
s.setStatusBar(T("No Diary Saved"))
return
}
filename := strconv.Itoa(s.curDiary.Id) + ".dat"
encodeToFile(s.getRichText(), filename, s.key)
title := strings.TrimSpace(s.editor.Document().FirstBlock().Text())
s.db.UpdateDiaryTitle(s.curDiary.Id, title)
s.setStatusBar(T("Save Diary") + fmt.Sprintf(" %s(%s)", title, filename))
s.editor.Document().SetModified(false)
}
func (s *myWindow) searchFromDb(kw string) {
if len(kw) == 0 {
s.setStatusBar(T("Must Input Search Keyword!"))
return
}
s.setStatusBar(T("Loading Diary List..."))
diaryList, err := s.db.SearchTitle(kw)
if err != nil {
s.setStatusBar(err.Error())
return
}
s.clearModelFind()
var ym string
var r, c int
for _, diary := range diaryList {
if diary.Day[:7] != ym {
ym = diary.Day[:7]
item := s.addFindYM(ym)
idx := item.Index()
r, c = idx.Row(), idx.Column()
s.treeFind.Expand(idx)
}
item := s.modelFind.Item(r, c)
child := gui.NewQStandardItem2(fmt.Sprintf("%s-%s", diary.Day[8:], diary.Title))
child.SetEditable(false)
child.SetAccessibleText(strconv.Itoa(diary.Id))
child.SetAccessibleDescription("0")
child.SetToolTip(T("Double Click to Open. ") + T("Last Modified:") + diary.MTime)
item.AppendRow2(child)
}
s.treeFind.ResizeColumnToContents(0)
return
}
func (s *myWindow) addFindYM(yearMonth string) *gui.QStandardItem {
idx := core.NewQModelIndex()
p := s.modelFind.Parent(idx)
n := s.modelFind.RowCount(p)
for i := 0; i < n; i++ {
item := s.modelFind.Item(i, 0)
if item.Text() == yearMonth {
return item
}
}
item := gui.NewQStandardItem2(yearMonth)
item.SetColumnCount(1)
item.SetEditable(false)
item.SetAccessibleText("1")
item.SetAccessibleDescription("1")
s.modelFind.AppendRow2(item)
return item
}
func (s *myWindow) setTreeFindFuncs() {
s.treeFind.ConnectActivated(func(idx *core.QModelIndex) {
item := s.modelFind.ItemFromIndex(idx)
if item.AccessibleDescription() == "1" {
return
}
id, err := strconv.Atoi(item.AccessibleText())
if err != nil {
s.setStatusBar(err.Error())
}
OpenDiaryNewWindow(s, id)
})
}
func (s *myWindow) findText() {
if s.searchInput != nil {
cursor := s.editor.TextCursor()
s.searchInput.SetText(cursor.Selection().ToPlainText())
return
}
dlg := widgets.NewQDialog(s.window, core.Qt__Dialog)
dlg.SetMinimumWidth(s.editor.Width() / 2)
dlg.SetWindowTitle(T("Text Search"))
grid := newGridLayout(dlg)
word := widgets.NewQLineEdit(dlg)
word.SetPlaceholderText(T("Words to search."))
grid.AddWidget3(word, 0, 0, 1, 2, 0)
s.searchInput = word
cursor := s.editor.TextCursor()
s.searchInput.SetText(cursor.Selection().ToPlainText())
backBtn := widgets.NewQPushButton2(T("Last"), dlg)
grid.AddWidget2(backBtn, 1, 0, 0)
nextBtn := widgets.NewQPushButton2(T("Next"), dlg)
grid.AddWidget2(nextBtn, 1, 1, 0)
dlg.SetLayout(grid)
backBtn.ConnectClicked(func(b bool) {
kw := strings.TrimSpace(word.Text())
if len(kw) == 0 {
return
}
cursor := s.editor.TextCursor()
if len(cursor.Selection().ToPlainText()) > 0 {
cursor.SetPosition(cursor.SelectionStart(), gui.QTextCursor__MoveAnchor)
s.editor.SetTextCursor(cursor)
}
s.editor.Find(kw, gui.QTextDocument__FindBackward)
})
nextBtn.ConnectClicked(func(b bool) {
kw := strings.TrimSpace(word.Text())
if len(kw) == 0 {
return
}
cursor := s.editor.TextCursor()
if len(cursor.Selection().ToPlainText()) > 0 {
cursor.SetPosition(cursor.SelectionEnd(), gui.QTextCursor__MoveAnchor)
s.editor.SetTextCursor(cursor)
}
s.editor.Find(kw, 0)
})
dlg.ConnectCloseEvent(func(e *gui.QCloseEvent) {
dlg.Destroy(true, true)
s.searchInput = nil
})
dlg.Show()
}
func (s *myWindow) replaceText() {
if s.replaceInput != nil {
cursor := s.editor.TextCursor()
s.replaceInput.SetText(cursor.Selection().ToPlainText())
return
}
dlg := widgets.NewQDialog(s.window, core.Qt__Dialog)
dlg.SetMinimumWidth(s.editor.Width() / 2)
dlg.SetWindowTitle(T("Text Replace"))
grid := newGridLayout(dlg)
wordOld := widgets.NewQLineEdit(dlg)
wordOld.SetPlaceholderText(T("Old Text."))
wordOld.SetToolTip(T("Old Text."))
grid.AddWidget3(wordOld, 0, 0, 1, 3, 0)
s.replaceInput = wordOld
cursor := s.editor.TextCursor()
s.replaceInput.SetText(cursor.Selection().ToPlainText())
wordNew := widgets.NewQLineEdit(dlg)
wordNew.SetPlaceholderText(T("New Text."))
wordNew.SetToolTip(T("New Text."))
grid.AddWidget3(wordNew, 1, 0, 1, 3, 0)
backBtn := widgets.NewQPushButton2(T("Last"), dlg)
grid.AddWidget2(backBtn, 2, 0, 0)
nextBtn := widgets.NewQPushButton2(T("Next"), dlg)
grid.AddWidget2(nextBtn, 2, 1, 0)
allBtn := widgets.NewQPushButton2(T("All"), dlg)
grid.AddWidget2(allBtn, 2, 2, 0)
dlg.SetLayout(grid)
backBtn.ConnectClicked(func(b bool) {
word0 := wordOld.Text()
word1 := wordNew.Text()
if len(word0) == 0 {
return
}
cursor := s.editor.TextCursor()
if cursor.Selection().ToPlainText() == word0 {
cursor.RemoveSelectedText()
cursor.InsertText(word1)
cursor.SetPosition(cursor.Position()-len(word1), gui.QTextCursor__MoveAnchor)
s.editor.SetTextCursor(cursor)
}
s.editor.Find(word0, gui.QTextDocument__FindBackward)
})
nextBtn.ConnectClicked(func(b bool) {
word0 := wordOld.Text()
word1 := wordNew.Text()
if len(word0) == 0 {
return
}
cursor := s.editor.TextCursor()
if cursor.Selection().ToPlainText() == word0 {
cursor.RemoveSelectedText()
cursor.InsertText(word1)
}
s.editor.Find(word0, 0)
})
allBtn.ConnectClicked(func(b bool) {
word0 := wordOld.Text()
if len(word0) == 0 {
return
}
text := s.editor.ToPlainText()
n := strings.Count(strings.ToLower(text), strings.ToLower(word0))
cursor := s.editor.TextCursor()
cursor.SetPosition(0, gui.QTextCursor__MoveAnchor)
s.editor.SetTextCursor(cursor)
for i := 0; i < n+1; i++ {
nextBtn.Clicked(true)
}
})
dlg.ConnectCloseEvent(func(e *gui.QCloseEvent) {
dlg.Destroy(true, true)
s.replaceInput = nil
})
dlg.Show()
}
func (s *myWindow) pasteText() {
board := gui.QGuiApplication_Clipboard()
text := board.Text(gui.QClipboard__Clipboard)
cursor := s.editor.TextCursor()
cursor.InsertText(text)
}
func (s *myWindow) changeLineMargin() {
cursor := s.editor.TextCursor()
if !cursor.HasSelection() {
cursor.Select(gui.QTextCursor__LineUnderCursor)
}
bfmt := cursor.BlockFormat()
margin := bfmt.TopMargin()
dlg := widgets.NewQDialog(s.window, core.Qt__Dialog)
dlg.SetWindowTitle(T("Top Margin"))
dlg.SetMinimumWidth(s.charWidth() * 10)
layout := widgets.NewQHBoxLayout()
label := widgets.NewQLabel2(T("Top Margin")+fmt.Sprintf(":%.0f", margin), dlg, core.Qt__Widget)
layout.AddWidget(label, 1, 0)
addBtn := widgets.NewQPushButton2("+", dlg)
addBtn.SetFixedWidth(s.charWidth())
layout.AddWidget(addBtn, 1, 0)
addBtn.ConnectClicked(func(b bool) {
margin += 1.0
bfmt.SetTopMargin(margin)
cursor.SetBlockFormat(bfmt)
label.SetText(T("Top Margin") + fmt.Sprintf(":%.0f", margin))
})
degreeBtn := widgets.NewQPushButton2("-", dlg)
degreeBtn.SetFixedWidth(s.charWidth())
layout.AddWidget(degreeBtn, 1, 0)
degreeBtn.ConnectClicked(func(b bool) {
margin -= 1.0
bfmt.SetTopMargin(margin)
cursor.SetBlockFormat(bfmt)
label.SetText(T("Top Margin") + fmt.Sprintf(":%.0f", margin))
})
dlg.SetLayout(layout)
dlg.Show()
}
Go
1
https://gitee.com/rocket049/secret-diary.git
git@gitee.com:rocket049/secret-diary.git
rocket049
secret-diary
secret-diary
master

搜索帮助