PresentationApp/main.go

184 lines
4.0 KiB
Go
Raw Normal View History

2016-01-11 09:10:52 -08:00
// PresentationApp project main.go
package main
import (
"fmt"
"image/color"
"os"
"path/filepath"
2016-04-07 10:11:42 -07:00
"github.com/go-gl/glfw/v3.1/glfw"
"github.com/kardianos/osext"
2016-01-25 17:27:00 -08:00
"github.com/lordwelch/qml"
2016-03-11 14:30:04 -08:00
"gopkg.in/gographics/imagick.v2/imagick"
2016-01-11 09:10:52 -08:00
)
2016-03-27 13:01:51 -07:00
//Bool type i'm lazy wanted a toggle function
type Bool bool
2016-01-14 22:25:53 -08:00
type cell struct {
text string
2016-03-11 14:30:04 -08:00
img *imagick.MagickWand
2016-03-07 08:54:14 -08:00
qmlimg qml.Object
2016-01-14 22:25:53 -08:00
qmlcell qml.Object
index int
font struct {
name string
outlineSize, size, x, y float64
color color.RGBA
outlineColor color.RGBA
outline Bool
}
2016-01-14 22:25:53 -08:00
}
type slide []*cell
2016-01-14 22:25:53 -08:00
var (
2016-03-27 13:01:51 -07:00
path string
slides slide
err error
)
2016-01-11 09:10:52 -08:00
func main() {
2016-02-01 17:04:11 -08:00
if err = qml.Run(run); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
win.Destroy()
glfw.PollEvents()
glfw.Terminate()
}
func run() error {
imagick.Initialize()
findfonts()
2016-03-11 09:56:45 -08:00
engine = qml.NewEngine()
2016-03-07 08:54:14 -08:00
engine.AddImageProvider("images", imgProvider)
2016-03-23 19:52:46 -07:00
//path for qml files TODO: change to somewhere else
path, err = osext.ExecutableFolder()
path = filepath.Clean(path + "/../src/github.com/lordwelch/PresentationApp/")
2016-01-25 17:27:00 -08:00
2016-02-01 17:04:11 -08:00
mainQml, err = engine.LoadFile(path + "/main.qml")
if err != nil {
return err
}
edtQml, err = engine.LoadFile(path + "/qml/songEdit.qml")
if err != nil {
return err
}
cellQml, err = engine.LoadFile(path + "/qml/cell.qml")
if err != nil {
return err
}
2016-03-07 08:54:14 -08:00
qimg, err = engine.LoadFile(path + "/qml/img.qml")
if err != nil {
return err
}
window = mainQml.CreateWindow(engine.Context())
window2 := edtQml.CreateWindow(engine.Context())
textEdit = window.ObjectByName("textEdit")
2016-03-23 19:52:46 -07:00
//signals for whole qml
2016-02-01 17:04:11 -08:00
setSignals()
2016-03-27 13:01:51 -07:00
slides.add()
2016-03-24 09:16:15 -07:00
2016-03-23 19:52:46 -07:00
//image is ready for imageprovider
2016-03-11 09:56:45 -08:00
imgready = true
2016-01-25 17:27:00 -08:00
window.Show()
window2.Show()
edtQmlShow()
2016-03-27 13:01:51 -07:00
slides[0].clearcache()
qml.RunMain(glInit)
2016-02-01 17:04:11 -08:00
window.Wait()
2016-02-19 08:46:03 -08:00
imagick.Terminate()
return nil
2016-01-11 09:10:52 -08:00
}
2016-01-14 22:25:53 -08:00
2016-03-23 19:52:46 -07:00
//Adds a new cell
2016-03-27 13:01:51 -07:00
func (sl *slide) add( /*cl *cell*/ ) {
var cl cell
cl.Init()
2016-03-23 19:52:46 -07:00
//gets the length so that the index is valid
2016-03-07 08:54:14 -08:00
cl.index = len(*sl)
2016-03-23 19:52:46 -07:00
//increase count on parent QML element
2016-03-08 10:23:04 -08:00
window.ObjectByName("gridRect").Set("count", window.ObjectByName("gridRect").Int("count")+1)
cl.qmlcell = cellQml.Create(engine.Context())
2016-03-07 08:54:14 -08:00
cl.qmlcell.Set("objectName", fmt.Sprintf("cellRect%d", len(*sl)))
cl.qmlcell.Set("parent", window.ObjectByName("data1"))
2016-03-07 08:54:14 -08:00
cl.qmlcell.Set("index", cl.index)
2016-03-23 19:52:46 -07:00
//keep the pointer/dereference (i'm not sure which it is)
//problems occur otherwise
*sl = append(*sl, &cl)
2016-03-23 19:52:46 -07:00
//seperate image object in QML
2016-03-07 08:54:14 -08:00
cl.qmlimg.Set("objectName", fmt.Sprintf("cellImg%d", cl.index))
cl.qmlimg.Set("source", fmt.Sprintf("image://images/%d"+`;`+"0", cl.index))
2016-03-07 08:54:14 -08:00
cl.qmlimg.Set("parent", window.ObjectByName("data2"))
cl.qmlimg.Set("index", cl.index)
cl.setSignal()
//give QML the text
cl.qmlcell.ObjectByName("cellText").Set("text", cl.text)
2016-03-07 08:54:14 -08:00
}
2016-01-14 22:25:53 -08:00
func (cl *cell) Init() {
cl.text = "hello this is text\nhaha\nhdsjfklfhaskjd"
cl.index = -1
cl.font.color, cl.font.outlineColor = color.RGBA{0, 0, 0, 1}, color.RGBA{1, 1, 1, 1}
cl.font.name = "none"
cl.font.outline = false
cl.font.outlineSize = 1
cl.font.size = 35
cl.font.x, cl.font.y = 10, 30
cl.qmlcell = cellQml.Create(engine.Context())
cl.qmlimg = qimg.Create(engine.Context())
//load image
cl.img = imagick.NewMagickWand()
cl.img.ReadImage("logo:")
}
//(cell) remove() should destroy everything for this cell
2016-03-11 14:30:04 -08:00
func (cl *cell) remove() {
cl.text = ""
cl.qmlimg.Destroy()
cl.qmlcell.Destroy()
2016-03-15 10:21:59 -07:00
cl.img.Destroy()
2016-03-11 14:30:04 -08:00
window.ObjectByName("gridRect").Set("count", window.ObjectByName("gridRect").Int("count")-1)
slides.remove(cl.index)
cl.index = -1
}
2016-03-27 13:01:51 -07:00
//(slide) remove copied from github.com/golang/go/wiki/SliceTricks
2016-03-11 14:30:04 -08:00
func (sl *slide) remove(i int) {
*sl, (*sl)[len((*sl))-1] = append((*sl)[:i], (*sl)[i+1:]...), nil
}
//Toggle, lazy wanted a func for it
func (bl *Bool) Toggle() {
if *bl == false {
*bl = true
} else {
*bl = false
}
}
2016-03-23 19:52:46 -07:00
//not really needed
func (cl cell) String() string {
return fmt.Sprintf("Index: %d \nText: %s\n", cl.index, cl.text)
2016-01-14 22:25:53 -08:00
}