akiyosi.goneovim/util/utils.go

285 lines
5.7 KiB
Go
Raw Normal View History

2019-03-05 23:09:22 +09:00
package util
import (
2019-10-22 12:04:36 +09:00
"strings"
"os/user"
"path/filepath"
2019-10-22 12:04:36 +09:00
2019-03-05 23:09:22 +09:00
"github.com/therecipe/qt/core"
2019-11-21 12:10:16 +09:00
"github.com/therecipe/qt/gui"
2019-03-05 23:09:22 +09:00
"github.com/therecipe/qt/widgets"
)
func ReflectToInt(iface interface{}) int {
i, ok := iface.(int64)
if ok {
return int(i)
}
2020-02-28 00:59:45 +09:00
j, ok := iface.(uint64)
2019-03-05 23:09:22 +09:00
if ok {
2020-02-28 00:59:45 +09:00
return int(j)
}
k, ok := iface.(int)
if ok {
return int(k)
}
l, ok := iface.(uint)
if ok {
return int(l)
2019-03-05 23:09:22 +09:00
}
return 0
}
2019-04-08 01:01:16 +09:00
func ReflectToFloat(iface interface{}) float64 {
i, ok := iface.(float64)
if ok {
2019-11-21 12:10:16 +09:00
return i
2019-04-08 01:01:16 +09:00
}
u, ok := iface.(float32)
if ok {
return float64(u)
}
return 0
}
2019-03-05 23:09:22 +09:00
func IsZero(d interface{}) bool {
if d == nil {
return false
}
switch a := d.(type) {
case int64:
if a == 0 {
return true
}
case uint64:
if a == 0 {
return true
}
}
return false
}
func IsTrue(d interface{}) bool {
if d == nil {
return false
}
switch a := d.(type) {
case int64:
if a == 1 {
return true
}
case uint64:
if a == 1 {
return true
}
}
return false
}
2019-10-22 12:04:36 +09:00
func SplitVimscript(s string) string {
if string(s[0]) == "\n" {
s = strings.TrimPrefix(s, string("\n"))
}
listLines := "["
lines := strings.Split(s, "\n")
for i, line := range lines {
listLines = listLines + `'` + line + `'`
if i == len(lines)-1 {
listLines = listLines + "]"
} else {
listLines = listLines + ","
}
}
return listLines
}
2019-11-21 12:10:16 +09:00
func DropShadow(x, y, radius float64, alpha int) *widgets.QGraphicsDropShadowEffect {
2019-09-11 16:11:02 +09:00
shadow := widgets.NewQGraphicsDropShadowEffect(nil)
2019-10-18 00:08:56 +09:00
shadow.SetBlurRadius(radius)
shadow.SetColor(gui.NewQColor3(0, 0, 0, alpha))
shadow.SetOffset2(x, y)
2019-09-11 16:11:02 +09:00
return shadow
}
2019-03-05 23:09:22 +09:00
func NewHFlowLayout(spacing int, padding int, paddingTop int, rightIdex int, width int) *widgets.QLayout {
layout := widgets.NewQLayout2()
items := []*widgets.QLayoutItem{}
rect := core.NewQRect()
layout.ConnectSizeHint(func() *core.QSize {
size := core.NewQSize()
for _, item := range items {
size = size.ExpandedTo(item.MinimumSize())
}
return size
})
if width > 0 {
layout.ConnectMinimumSize(func() *core.QSize {
size := core.NewQSize()
for _, item := range items {
size = size.ExpandedTo(item.MinimumSize())
}
if size.Width() > width {
size.SetWidth(width)
}
// size.SetWidth(0)
return size
})
layout.ConnectMaximumSize(func() *core.QSize {
size := core.NewQSize()
for _, item := range items {
size = size.ExpandedTo(item.MinimumSize())
}
// size.SetWidth(width)
return size
})
}
layout.ConnectAddItem(func(item *widgets.QLayoutItem) {
items = append(items, item)
})
layout.ConnectSetGeometry(func(r *core.QRect) {
sizes := [][]int{}
maxWidth := 0
for _, item := range items {
sizeHint := item.SizeHint()
width := sizeHint.Width()
height := sizeHint.Height()
size := []int{width, height}
sizes = append(sizes, size)
if width > maxWidth {
maxWidth = width
}
}
y := 0
for i, item := range items {
size := sizes[i]
height := size[1]
rect.SetRect(0, y, maxWidth, height)
item.SetGeometry(rect)
y += height
}
})
layout.ConnectItemAt(func(index int) *widgets.QLayoutItem {
if index < len(items) {
return items[index]
}
return nil
})
layout.ConnectTakeAt(func(index int) *widgets.QLayoutItem {
if index < len(items) {
return items[index]
}
return nil
})
return layout
}
func NewVFlowLayout(spacing int, padding int, paddingTop int, rightIdex int, width int) *widgets.QLayout {
layout := widgets.NewQLayout2()
items := []*widgets.QLayoutItem{}
rect := core.NewQRect()
layout.ConnectSizeHint(func() *core.QSize {
size := core.NewQSize()
for _, item := range items {
size = size.ExpandedTo(item.MinimumSize())
}
return size
})
if width > 0 {
layout.ConnectMinimumSize(func() *core.QSize {
size := core.NewQSize()
for _, item := range items {
size = size.ExpandedTo(item.MinimumSize())
}
if size.Width() > width {
size.SetWidth(width)
}
size.SetWidth(0)
return size
})
layout.ConnectMaximumSize(func() *core.QSize {
size := core.NewQSize()
for _, item := range items {
size = size.ExpandedTo(item.MinimumSize())
}
size.SetWidth(width)
return size
})
}
layout.ConnectAddItem(func(item *widgets.QLayoutItem) {
items = append(items, item)
})
layout.ConnectSetGeometry(func(r *core.QRect) {
x := padding
right := padding
sizes := [][]int{}
maxHeight := 0
totalWidth := r.Width()
for _, item := range items {
sizeHint := item.SizeHint()
width := sizeHint.Width()
height := sizeHint.Height()
size := []int{width, height}
sizes = append(sizes, size)
if height > maxHeight {
maxHeight = height
}
}
for i, item := range items {
size := sizes[i]
width := size[0]
height := size[1]
y := paddingTop
if height != maxHeight {
y = (maxHeight-height)/2 + paddingTop
}
if rightIdex > 0 && i >= rightIdex {
rect.SetRect(totalWidth-width-right, y, width, height)
item.SetGeometry(rect)
if width > 0 {
right += width + spacing
}
} else {
if x+width+padding > totalWidth {
width = totalWidth - x - padding
rect.SetRect(x, y, width, height)
item.SetGeometry(rect)
break
}
rect.SetRect(x, y, width, height)
item.SetGeometry(rect)
if width > 0 {
x += width + spacing
}
}
}
})
layout.ConnectItemAt(func(index int) *widgets.QLayoutItem {
if index < len(items) {
return items[index]
}
return nil
})
layout.ConnectTakeAt(func(index int) *widgets.QLayoutItem {
if index < len(items) {
return items[index]
}
return nil
})
return layout
}
func ExpandTildeToHomeDirectory(path string) (string, error) {
if len(path) == 0 || path[0] != '~' {
return path, nil
}
usr, err := user.Current()
if err != nil {
return "", err
}
return filepath.Join(usr.HomeDir, path[1:]), nil
}