Skip to content

Commit

Permalink
Support color values and properties as color.RGBA
Browse files Browse the repository at this point in the history
  • Loading branch information
niemeyer committed Oct 2, 2013
1 parent 63e7320 commit 82679ec
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 0 deletions.
10 changes: 10 additions & 0 deletions all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"github.com/niemeyer/qml"
"image/color"
"io/ioutil"
. "launchpad.net/gocheck"
"os"
Expand Down Expand Up @@ -336,6 +337,15 @@ var tests = []struct {
d.Check(d.root.String("url"), Equals, url)
},
},
{
Summary: "Reading and setting of a QColor property",
QML: `Text{ color: Qt.rgba(1/16, 1/8, 1/4, 1/2); function hasColor(c) { return Qt.colorEqual(color, c) }}`,
Done: func(d *TestData) {
d.Assert(d.root.Color("color"), Equals, color.RGBA{256/16, 256/8, 256/4, 256/2})
d.root.Set("color", color.RGBA{256/2, 256/4, 256/8, 256/16})
d.Assert(d.root.Call("hasColor", color.RGBA{256/2, 256/4, 256/8, 256/16}), Equals, true)
},
},
{
Summary: "Identical values remain identical when possible",
Init: func(d *TestData) {
Expand Down
7 changes: 7 additions & 0 deletions cpp/capi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,9 @@ void unpackDataValue(DataValue *value, QVariant_ *var)
case DTFloat32:
*qvar = *(float*)(value->data);
break;
case DTColor:
*qvar = QColor(*(QRgb*)(value->data));
break;
case DTList:
*qvar = **(QVariantList**)(value->data);
delete *(QVariantList**)(value->data);
Expand Down Expand Up @@ -544,6 +547,10 @@ void packDataValue(QVariant_ *var, DataValue *value)
value->dataType = DTFloat32;
*(float*)(value->data) = qvar->toFloat();
break;
case QMetaType::QColor:
value->dataType = DTColor;
*(unsigned int*)(value->data) = qvar->value<QColor>().rgba();
break;
default:
if (qvar->type() == (int)QMetaType::QObjectStar || qvar->canConvert<QObject *>()) {
QObject *qobject = qvar->value<QObject *>();
Expand Down
1 change: 1 addition & 0 deletions cpp/capi.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ typedef enum {
DTInt32 = 13,
DTFloat64 = 14,
DTFloat32 = 15,
DTColor = 16,

DTGoAddr = 100,
DTObject = 101,
Expand Down
7 changes: 7 additions & 0 deletions datatype.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import "C"
import (
"bytes"
"fmt"
"image/color"
"reflect"
"unicode"
"unsafe"
Expand Down Expand Up @@ -85,6 +86,9 @@ func packDataValue(value interface{}, dvalue *C.DataValue, engine *Engine, owner
case *Common:
dvalue.dataType = C.DTObject
*(*unsafe.Pointer)(datap) = value.addr
case color.RGBA:
dvalue.dataType = C.DTColor
*(*uint32)(datap) = uint32(value.A) << 24 | uint32(value.R) << 16 | uint32(value.G) << 8 | uint32(value.B)
default:
dvalue.dataType = C.DTObject
if obj, ok := value.(Object); ok {
Expand Down Expand Up @@ -120,6 +124,9 @@ func unpackDataValue(dvalue *C.DataValue, engine *Engine) interface{} {
return *(*float64)(datap)
case C.DTFloat32:
return *(*float32)(datap)
case C.DTColor:
var c uint32 = *(*uint32)(datap)
return color.RGBA{byte(c >> 16), byte(c >> 8), byte(c), byte(c >> 24)}
case C.DTGoAddr:
return (*(**valueFold)(datap)).gvalue
case C.DTInvalid:
Expand Down
13 changes: 13 additions & 0 deletions qml.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"errors"
"fmt"
"image"
"image/color"
"io"
"io/ioutil"
"os"
Expand Down Expand Up @@ -265,6 +266,7 @@ type Object interface {
Float64(property string) float64
Bool(property string) bool
String(property string) string
Color(property string) color.RGBA
Object(property string) Object
ObjectByName(objectName string) Object
Call(method string, params ...interface{}) interface{}
Expand Down Expand Up @@ -409,6 +411,17 @@ func (obj *Common) String(property string) string {
return s
}

// Color returns the RGBA value of the given color property.
// Color panics if the property value is not a color.
func (obj *Common) Color(property string) color.RGBA {
value := obj.Property(property)
c, ok := value.(color.RGBA)
if !ok {
panic(fmt.Sprintf("value of property %q is not a string: %#v", property, value))
}
return c
}

// TODO Consider getting rid of int32 and float32 results. Always returning 64-bit
// results will make it easier on clients that want to handle arbitrary typing.

Expand Down

0 comments on commit 82679ec

Please sign in to comment.