示例#1
0
文件: qml.go 项目: pmeido/Arianrhod
// ObjectByName returns the Object value of the descendant object that
// was defined with the objectName property set to the provided value.
// ObjectByName panics if the object is not found.
func (obj *Common) ObjectByName(objectName string) Object {
	cname, cnamelen := unsafeStringData(objectName)
	var dvalue C.DataValue
	var object Object
	RunMain(func() {
		qname := C.newString(cname, cnamelen)
		defer C.delString(qname)
		C.objectFindChild(obj.addr, qname, &dvalue)
		// unpackDataValue will also initialize the Go type, if necessary.
		value := unpackDataValue(&dvalue, obj.engine)
		if dvalue.dataType == C.DTGoAddr {
			datap := unsafe.Pointer(&dvalue.data)
			fold := (*(**valueFold)(datap))
			if fold.init.IsValid() {
				panic("internal error: custom Go type not initialized")
			}
			object = &Common{fold.cvalue, fold.engine, newConnections()}
		} else {
			object, _ = value.(Object)
		}
	})
	// if object == nil {
	//  panic(fmt.Sprintf("cannot find descendant with objectName == %q", objectName))
	// }
	return object
}
示例#2
0
文件: qml.go 项目: reedobrien/qml
// Var returns the context variable with the given name.
func (ctx *Context) Var(name string) interface{} {
	cname, cnamelen := unsafeStringData(name)

	var dvalue C.DataValue
	gui(func() {
		qname := C.newString(cname, cnamelen)
		defer C.delString(qname)

		C.contextGetProperty(ctx.addr, qname, &dvalue)
	})
	return unpackDataValue(&dvalue, ctx.engine)
}
示例#3
0
文件: qml.go 项目: reedobrien/qml
// SetVar makes the provided value available as a variable with the
// given name for QML code executed within the c context.
//
// If value is a struct, its exported fields are also made accessible to
// QML code as attributes of the named object. The attribute name in the
// object has the same name of the Go field name, except for the first
// letter which is lowercased. This is conventional and enforced by
// the QML implementation.
//
// The engine will hold a reference to the provided value, so it will
// not be garbage collected until the engine is destroyed, even if the
// value is unused or changed.
func (ctx *Context) SetVar(name string, value interface{}) {
	cname, cnamelen := unsafeStringData(name)
	gui(func() {
		var dvalue C.DataValue
		packDataValue(value, &dvalue, ctx.engine, cppOwner)

		qname := C.newString(cname, cnamelen)
		defer C.delString(qname)

		C.contextSetProperty(ctx.addr, qname, &dvalue)
	})
}
示例#4
0
文件: qml.go 项目: reedobrien/qml
// AddImageProvider registers f to be called when an image is requested by QML code
// with the specified provider identifier. It is a runtime error to register the same
// provider identifier multiple times.
//
// The imgId provided to f is the requested image source, with the "image:" scheme
// and provider identifier removed. For example, with an image image source of
// "image://myprovider/icons/home.ext", the respective imgId would be "icons/home.ext".
//
// If either the width or the height parameters provided to f are zero, no specific
// size for the image was requested. If non-zero, the returned image should have the
// the provided size, and will be resized if the returned image has a different size.
//
// See the documentation for more details on image providers:
//
//   http://qt-project.org/doc/qt-5.0/qtquick/qquickimageprovider.html
//
func (e *Engine) AddImageProvider(prvId string, f func(imgId string, width, height int) image.Image) {
	if _, ok := e.imageProviders[prvId]; ok {
		panic(fmt.Sprintf("engine already has an image provider with id %q", prvId))
	}
	e.imageProviders[prvId] = &f
	cprvId, cprvIdLen := unsafeStringData(prvId)
	gui(func() {
		qprvId := C.newString(cprvId, cprvIdLen)
		defer C.delString(qprvId)
		C.engineAddImageProvider(e.addr, qprvId, unsafe.Pointer(&f))
	})
}
示例#5
0
文件: qml.go 项目: reedobrien/qml
// ObjectByName returns the Object value of the descendant object that
// was defined with the objectName property set to the provided value.
// ObjectByName panics if the object is not found.
func (obj *Common) ObjectByName(objectName string) Object {
	cname, cnamelen := unsafeStringData(objectName)
	var dvalue C.DataValue
	gui(func() {
		qname := C.newString(cname, cnamelen)
		defer C.delString(qname)
		C.objectFindChild(obj.addr, qname, &dvalue)
	})
	object, ok := unpackDataValue(&dvalue, obj.engine).(Object)
	if !ok {
		panic(fmt.Sprintf("cannot find descendant with objectName == %q", objectName))
	}
	return object
}