add remove function

discovered memleak
This commit is contained in:
lordwelch 2016-03-11 14:30:04 -08:00
parent f4450e7e3d
commit 74fe888188
4 changed files with 67 additions and 18 deletions

View File

@ -6,6 +6,7 @@ import (
"strconv" "strconv"
) )
<<<<<<< Updated upstream
/*var imgproviderstr = `import QtQuick 2.4 /*var imgproviderstr = `import QtQuick 2.4
Image { Image {
@ -21,4 +22,9 @@ func imgProvider(id string, width, height int) image.Image {
var img1 image.Image = image.NewRGBA(image.Rect(0, 0, 340, 480)) var img1 image.Image = image.NewRGBA(image.Rect(0, 0, 340, 480))
return img1 return img1
} }
=======
func imgProvider(id string, width, height int) image.Image {
i, _ := strconv.Atoi(id)
return slides[i].getImage(width, height)
>>>>>>> Stashed changes
} }

View File

@ -4,7 +4,7 @@ package main
import ( import (
"math" "math"
"github.com/gographics/imagick/imagick" "gopkg.in/gographics/imagick.v2/imagick"
) )
func resizeImage(mw *imagick.MagickWand, newWidth, newHeight int, keepSpecSize, center bool) (resmw *imagick.MagickWand) { func resizeImage(mw *imagick.MagickWand, newWidth, newHeight int, keepSpecSize, center bool) (resmw *imagick.MagickWand) {

44
main.go
View File

@ -7,17 +7,18 @@ import (
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
"runtime/debug"
"github.com/go-gl/gl/v2.1/gl" "github.com/go-gl/gl/v2.1/gl"
"github.com/go-gl/glfw/v3.1/glfw" "github.com/go-gl/glfw/v3.1/glfw"
"github.com/gographics/imagick/imagick"
"github.com/kardianos/osext" "github.com/kardianos/osext"
"github.com/lordwelch/qml" "github.com/lordwelch/qml"
"gopkg.in/gographics/imagick.v2/imagick"
) )
type cell struct { type cell struct {
text string text string
img imagick.Image img *imagick.MagickWand
qmlimg qml.Object qmlimg qml.Object
qmlcell qml.Object qmlcell qml.Object
index int index int
@ -48,7 +49,6 @@ func main() {
} }
defer glfw.Terminate() defer glfw.Terminate()
fmt.Println("test")
} }
@ -241,6 +241,15 @@ func setSignals() {
slides.addCell() slides.addCell()
}) })
window.ObjectByName("btnRem").On("clicked", func() {
slides[len(slides)-1].remove()
fmt.Println("testing....")
})
window.ObjectByName("btnMem").On("clicked", func() {
debug.FreeOSMemory()
})
window.On("closing", func() { window.On("closing", func() {
fmt.Println(window.Bool("cls")) fmt.Println(window.Bool("cls"))
win.Hide() win.Hide()
@ -268,7 +277,8 @@ func setSignals() {
} }
func (cl cell) getImage(x, y int) (img *image.RGBA) { func (cl cell) getImage(x, y int) (img *image.RGBA) {
mw := imagick.NewMagickWandFromImage(&cl.img) mw := cl.img.GetImage()
//mw := imagick.NewMagickWandFromImage(cl.img)
if (x == 0) || (y == 0) { if (x == 0) || (y == 0) {
x = int(mw.GetImageWidth()) x = int(mw.GetImageWidth())
y = int(mw.GetImageHeight()) y = int(mw.GetImageHeight())
@ -315,9 +325,8 @@ func (sl *slide) addCell( /*cl *cell*/ ) {
cl.qmlcell.Set("parent", window.ObjectByName("data1")) cl.qmlcell.Set("parent", window.ObjectByName("data1"))
cl.qmlcell.Set("index", cl.index) cl.qmlcell.Set("index", cl.index)
mw := imagick.NewMagickWand() cl.img = imagick.NewMagickWand()
mw.ReadImage("logo:") cl.img.ReadImage("logo:")
cl.img = *mw.GetImageFromMagickWand()
cl.text = "testing 1... 2... 3..." cl.text = "testing 1... 2... 3..."
cl.qmlcell.ObjectByName("cellText").Set("text", cl.text) cl.qmlcell.ObjectByName("cellText").Set("text", cl.text)
@ -332,6 +341,27 @@ func (sl *slide) addCell( /*cl *cell*/ ) {
} }
func (cl *cell) remove() {
cl.text = ""
cl.qmlimg.Destroy()
cl.img.Destroy()
cl.qmlcell.Destroy()
window.ObjectByName("gridRect").Set("count", window.ObjectByName("gridRect").Int("count")-1)
slides.remove(cl.index)
cl.index = -1
}
func (sl *slide) remove(i int) {
//*sl = append((*sl)[:i], (*sl)[i+1:]...)
/*copy((*sl)[i:], (*sl)[i+1:])
(*sl)[len(*sl)-1] = nil // or the zero value of T
*sl = (*sl)[:len((*sl))-1]*/
// or, more simply:
*sl, (*sl)[len((*sl))-1] = append((*sl)[:i], (*sl)[i+1:]...), nil
}
func (cl cell) String() string { func (cl cell) String() string {
return fmt.Sprintf("Index: %d \nText: %s\n", cl.index, cl.text) return fmt.Sprintf("Index: %d \nText: %s\n", cl.index, cl.text)
} }

View File

@ -80,18 +80,20 @@ ApplicationWindow {
id: scview id: scview
anchors.fill: parent anchors.fill: parent
anchors.margins: 4 anchors.margins: 4
horizontalScrollBarPolicy: Qt.ScrollBarAlwaysOff
verticalScrollBarPolicy: Qt.ScrollBarAlwaysOn
SplitView { SplitView {
id: gridData id: gridData
objectName: "gridData" objectName: "gridData"
width: scview.width width: scview.width - 1
height: gridRect.count*100 height: gridRect.count * 101
Rectangle { Rectangle {
id: col1 id: col1
objectName: "col1" objectName: "col1"
width: gridData.width / 2 width: gridData.width / 2
color: "#e41616" color: "#00000000"
transformOrigin: Item.TopLeft transformOrigin: Item.TopLeft
border.width: 0 border.width: 0
Rectangle { Rectangle {
@ -151,19 +153,14 @@ ApplicationWindow {
id: data1 id: data1
objectName: "data1" objectName: "data1"
spacing: 1 spacing: 1
anchors.right: parent.right anchors.fill: parent
anchors.bottom: parent.bottom
anchors.top: parent.top
anchors.left: parent.left
} }
} }
Rectangle { Rectangle {
id: col2 id: col2
objectName: "col2" objectName: "col2"
color: "#4f90e2" color: "#00000000"
border.width: 0 border.width: 0
Column { Column {
@ -198,6 +195,22 @@ ApplicationWindow {
y: 8 y: 8
text: qsTr("Button") text: qsTr("Button")
} }
Button {
id: button2
x: 8
y: 39
text: qsTr("Button")
objectName: "btnRem"
}
Button {
id: button3
x: 8
y: 70
text: qsTr("Button")
objectName: "btnMem"
}
} }
} }