From 2cb1ac06e7768b21219cfc195648f05e6d4e122b Mon Sep 17 00:00:00 2001 From: lordwelch Date: Mon, 7 Mar 2016 08:54:14 -0800 Subject: [PATCH] add a preview image to the qml form --- imgprovider.go | 21 ++++++++++++ main.go | 81 +++++++++++++++++++++++++++++++-------------- main.qml | 6 ++-- qml/AboutDialog.qml | 2 +- qml/cell.qml | 4 +-- qml/img.qml | 16 +++++++++ 6 files changed, 100 insertions(+), 30 deletions(-) create mode 100644 imgprovider.go create mode 100644 qml/img.qml diff --git a/imgprovider.go b/imgprovider.go new file mode 100644 index 0000000..445b638 --- /dev/null +++ b/imgprovider.go @@ -0,0 +1,21 @@ +// imgprovider.go +package main + +import ( + "fmt" + "image" + "strconv" +) + +/*var imgproviderstr = `import QtQuick 2.4 + +Image { + source: "image://images/`*/ + +func imgProvider(id string, width, height int) image.Image { + var i int + fmt.Println("id: ", id) + i, _ = strconv.Atoi(id) + fmt.Println("haha: ", i) + return slides[i].getImage(width, height) +} diff --git a/main.go b/main.go index 2919e96..3990d74 100644 --- a/main.go +++ b/main.go @@ -18,14 +18,16 @@ import ( type cell struct { text string img imagick.Image + qmlimg qml.Object qmlcell qml.Object index int } type slide []*cell var ( - x, y int + x0, y0 int path string + qimg qml.Object textEdit qml.Object cellQml qml.Object window *qml.Window @@ -62,6 +64,8 @@ func run() error { } engine := qml.NewEngine() + engine.AddImageProvider("images", imgProvider) + path, err = osext.ExecutableFolder() path = filepath.Clean(path + "/../src/github.com/lordwelch/PresentationApp/") @@ -75,6 +79,11 @@ func run() error { return err } + qimg, err = engine.LoadFile(path + "/qml/img.qml") + if err != nil { + return err + } + window = mainQml.CreateWindow(nil) textEdit = window.ObjectByName("textEdit") @@ -94,9 +103,9 @@ func run() error { func setupScene() { gl.ClearColor(0.1, 0.5, 0.9, 0.0) - mw1 = resizeImage(mw1, x, y, true, true) + mw1 = resizeImage(mw1, x0, y0, true, true) - tex1 = newTexture(*mw1) + tex1 = newTexture(*slides[0].getImage(x0, y0)) gl.MatrixMode(gl.PROJECTION) gl.LoadIdentity() @@ -130,16 +139,7 @@ func drawSlide() { } -func newTexture(mw imagick.MagickWand) uint32 { - x1 := mw.GetImageWidth() - y1 := mw.GetImageHeight() - rgba := image.NewRGBA(image.Rect(0, 0, int(x1), int(y1))) - if rgba.Stride != rgba.Rect.Size().X*4 { - panic("unsupported stride") - } - TPix, _ := mw.ExportImagePixels(0, 0, x1, y1, "RGBA", imagick.PIXEL_CHAR) - rgba.Pix = TPix.([]uint8) - +func newTexture(rgba image.RGBA) uint32 { var texture1 uint32 gl.Enable(gl.TEXTURE_2D) gl.GenTextures(1, &texture1) @@ -164,27 +164,30 @@ func checkMon() { glfw.WindowHint(glfw.ContextVersionMinor, 1) glfw.WindowHint(glfw.AutoIconify, glfw.False) glfw.WindowHint(glfw.Decorated, glfw.False) + if i := len(monitors); i < 2 { fmt.Println("You only have 1 monitor!!!!!!!!!!! :-P") - x = 800 - y = 600 - win, err = glfw.CreateWindow(x, y, "Cube", nil, nil) + x0 = 800 + y0 = 600 + + win, err = glfw.CreateWindow(x0, y0, "Cube", nil, nil) if err != nil { panic(err) } projMonitor = monitors[0] } else { fmt.Printf("You have %d monitors\n", i) - x = monitors[1].GetVideoMode().Width - y = monitors[1].GetVideoMode().Height - win, err = glfw.CreateWindow(x, y, "Cube", nil, nil) + x0 = monitors[1].GetVideoMode().Width + y0 = monitors[1].GetVideoMode().Height + win, err = glfw.CreateWindow(x0, y0, "Cube", nil, nil) fmt.Println(win.GetPos()) win.SetPos(monitors[1].GetPos()) - fmt.Println(x, y) + fmt.Println(x0, y0) if err != nil { panic(err) } projMonitor = monitors[1] + } monitorInfo() @@ -259,6 +262,25 @@ func setSignals() { } +func (cl cell) getImage(x, y int) (img *image.RGBA) { + mw := imagick.NewMagickWandFromImage(&cl.img) + if (x == 0) || (y == 0) { + x = int(mw.GetImageWidth()) + y = int(mw.GetImageHeight()) + } + + mw = resizeImage(mw, x, y, true, true) + img = image.NewRGBA(image.Rect(0, 0, int(x), int(y))) + if img.Stride != img.Rect.Size().X*4 { + panic("unsupported stride") + } + // TPix, _ := + Tpix, _ := mw.ExportImagePixels(0, 0, uint(x), uint(y), "RGBA", imagick.PIXEL_CHAR) + img.Pix = Tpix.([]uint8) + defer mw.Destroy() + return +} + func (cl *cell) setSignal() { cl.qmlcell.ObjectByName("cellMouse").On("doubleClicked", func() { @@ -280,17 +302,28 @@ func (cl *cell) setSignal() { func (sl *slide) addCell( /*cl *cell*/ ) { var cl cell - cl.qmlcell = cellQml.Create(nil) - cl.qmlcell.Set("objectName", fmt.Sprintf("cellRect%d", cl.index)) - cl.qmlcell.Set("parent", window.ObjectByName("data1")) - cl.index = len(*sl) + cl.qmlcell = cellQml.Create(nil) + cl.qmlcell.Set("objectName", fmt.Sprintf("cellRect%d", len(*sl))) + cl.qmlcell.Set("parent", window.ObjectByName("data1")) + cl.qmlcell.Set("index", cl.index) + + mw := imagick.NewMagickWand() + mw.ReadImage("logo:") + cl.img = *mw.GetImageFromMagickWand() + cl.text = "testing 1... 2... 3..." cl.qmlcell.ObjectByName("cellText").Set("text", cl.text) *sl = append(*sl, &cl) cl.setSignal() + cl.qmlimg = qimg.Create(nil) + cl.qmlimg.Set("objectName", fmt.Sprintf("cellImg%d", cl.index)) + cl.qmlimg.Set("source", fmt.Sprintf("image://images/%d", cl.index)) + cl.qmlimg.Set("parent", window.ObjectByName("data2")) + cl.qmlimg.Set("index", cl.index) + } func (cl cell) String() string { diff --git a/main.qml b/main.qml index a9e34c4..c110d57 100644 --- a/main.qml +++ b/main.qml @@ -3,6 +3,7 @@ import QtQuick.Dialogs 1.2 import QtQuick.Controls 1.3 import QtQuick.Window 2.0 import "qml" +import QtQuick.Layouts 1.2 ApplicationWindow { id: applicationWindow1 @@ -91,7 +92,7 @@ ApplicationWindow { transformOrigin: Item.TopLeft border.width: 0 - Column { + ColumnLayout { id: data1 objectName: "data1" spacing: 1 @@ -108,11 +109,12 @@ ApplicationWindow { color: "#4f90e2" border.width: 0 - Column { + ColumnLayout { id: data2 spacing: 1 objectName: "data2" anchors.fill: parent + } } } diff --git a/qml/AboutDialog.qml b/qml/AboutDialog.qml index 396fea7..7a126d1 100644 --- a/qml/AboutDialog.qml +++ b/qml/AboutDialog.qml @@ -1,4 +1,4 @@ -import QtQuick 2.2 +import QtQuick 2.4 import QtQuick.Dialogs 1.1 MessageDialog { diff --git a/qml/cell.qml b/qml/cell.qml index ba2fb8d..67f012e 100644 --- a/qml/cell.qml +++ b/qml/cell.qml @@ -1,10 +1,8 @@ -import QtQuick 2.0 +import QtQuick 2.4 Rectangle { - enabled: true objectName: "cellRect" property int index: 0 - width: 100 height: 100 border.width: 2 border.color: "black" diff --git a/qml/img.qml b/qml/img.qml new file mode 100644 index 0000000..e1a6bdc --- /dev/null +++ b/qml/img.qml @@ -0,0 +1,16 @@ +import QtQuick 2.4 + +Image { + id: img + antialiasing: true + source: "image://images/" + objectName: "cellImg" + property int index: 0 + height: 150 + transformOrigin: Item.TopLeft + fillMode: Image.PreserveAspectFit + anchors.right: parent.right + anchors.left: parent.left + Layout.minimumHeight: 100 + Layout.maximumHeight: 200 +}