Skip to content

Commit cc2c727

Browse files
author
Matt Peterson
committed
Move qpainter to a subpackage, add lots of QPainter methods
1 parent ed6968d commit cc2c727

File tree

10 files changed

+96
-48
lines changed

10 files changed

+96
-48
lines changed

all.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include "cpp/govaluetype.cpp"
55
#include "cpp/idletimer.cpp"
66
#include "cpp/connector.cpp"
7-
#include "cpp/painter.cpp"
87

98
#include "cpp/moc_all.cpp"
109

bridge.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"github.com/limetext/qml-go/cdata"
2323
"github.com/limetext/qml-go/internal/util"
24+
"github.com/limetext/qml-go/qpainter"
2425
)
2526

2627
type mainThreadFunc struct {
@@ -593,7 +594,7 @@ func printPaintPanic() {
593594
}
594595

595596
//export hookGoValuePaint
596-
func hookGoValuePaint(enginep, foldp unsafe.Pointer, reflectIndex C.intptr_t, qpainter unsafe.Pointer) {
597+
func hookGoValuePaint(enginep, foldp unsafe.Pointer, reflectIndex C.intptr_t, qpainterptr unsafe.Pointer) {
597598
// Besides a convenience this is a workaround for http://golang.org/issue/8588
598599
defer printPaintPanic()
599600
defer atomic.StoreUintptr(&guiPaintRef, 0)
@@ -607,14 +608,12 @@ func hookGoValuePaint(enginep, foldp unsafe.Pointer, reflectIndex C.intptr_t, qp
607608
return
608609
}
609610

610-
painter := &Painter{
611-
engine: fold.engine,
612-
obj: CommonOf(fold.cvalue, fold.engine),
613-
qpainter: qpainter,
614-
}
611+
obj := CommonOf(fold.cvalue, fold.engine)
612+
painter := qpainter.FromPtr(qpainterptr)
613+
615614
v := reflect.ValueOf(fold.gvalue)
616615
method := v.Method(int(reflectIndex))
617-
method.Call([]reflect.Value{reflect.ValueOf(painter)})
616+
method.Call([]reflect.Value{reflect.ValueOf(obj), reflect.ValueOf(painter)})
618617
}
619618

620619
func ensureEngine(enginep, foldp unsafe.Pointer) *valueFold {

datatype.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import (
1212
"strings"
1313
"unicode"
1414
"unsafe"
15+
1516
"github.com/limetext/qml-go/internal/util"
17+
"github.com/limetext/qml-go/qpainter"
1618
)
1719

1820
var (
@@ -35,7 +37,7 @@ var (
3537
typeRGBA = reflect.TypeOf(color.RGBA{})
3638
typeObjSlice = reflect.TypeOf([]Object(nil))
3739
typeObject = reflect.TypeOf([]Object(nil)).Elem()
38-
typePainter = reflect.TypeOf(&Painter{})
40+
typePainter = reflect.TypeOf(&qpainter.Painter{})
3941
typeList = reflect.TypeOf(&List{})
4042
typeMap = reflect.TypeOf(&Map{})
4143
typeGenericMap = reflect.TypeOf(map[string]interface{}(nil))
@@ -424,7 +426,7 @@ func typeInfo(v interface{}) *C.GoTypeInfo {
424426
membersi += 1
425427
mnamesi += uintptr(len(method.Name)) + 1
426428

427-
if method.Name == "Paint" && memberInfo.numIn == 1 && memberInfo.numOut == 0 && method.Type.In(1) == typePainter {
429+
if method.Name == "Paint" && memberInfo.numIn == 2 && memberInfo.numOut == 0 && method.Type.In(1) == typeObject && method.Type.In(2) == typePainter {
428430
typeInfo.paint = memberInfo
429431
}
430432
}

examples/gopher/gopher.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/limetext/qml-go"
88
"github.com/limetext/qml-go/gl/2.0"
9+
"github.com/limetext/qml-go/qpainter"
910
)
1011

1112
var filename = "gopher.qml"
@@ -61,7 +62,7 @@ func (r *Gopher) SetRotation(rotation int) {
6162
r.Call("update")
6263
}
6364

64-
func (r *Gopher) Paint(p *qml.Painter) {
65+
func (r *Gopher) Paint(obj qml.Object, p *qpainter.Painter) {
6566
gl := GL.API(p)
6667

6768
width := float32(r.Int("width"))

examples/painting-es2/painting.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/limetext/qml-go"
88
"github.com/limetext/qml-go/gl/es2"
9+
"github.com/limetext/qml-go/qpainter"
910
)
1011

1112
func main() {
@@ -56,7 +57,7 @@ void main()
5657
}
5758
`
5859

59-
func (r *GoRect) Paint(p *qml.Painter) {
60+
func (r *GoRect) Paint(obj qml.Object, p *qpainter.Painter) {
6061
gl := GL.API(p)
6162

6263
vertices := []float32{

examples/painting/painting.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/limetext/qml-go"
88
"github.com/limetext/qml-go/gl/2.0"
9+
"github.com/limetext/qml-go/qpainter"
910
)
1011

1112
func main() {
@@ -19,7 +20,7 @@ type GoRect struct {
1920
qml.Object
2021
}
2122

22-
func (r *GoRect) Paint(p *qml.Painter) {
23+
func (r *GoRect) Paint(obj qml.Object, p *qpainter.Painter) {
2324
gl := GL.API(p)
2425

2526
width := float32(r.Int("width"))

qml_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/limetext/qml-go"
2121
"github.com/limetext/qml-go/cpptest"
2222
"github.com/limetext/qml-go/gl/2.0"
23+
"github.com/limetext/qml-go/qpainter"
2324
. "gopkg.in/check.v1"
2425
)
2526

@@ -80,11 +81,9 @@ type GoRect struct {
8081
PaintCount int
8182
}
8283

83-
func (r *GoRect) Paint(p *qml.Painter) {
84+
func (r *GoRect) Paint(obj qml.Object, p *qpainter.Painter) {
8485
r.PaintCount++
8586

86-
obj := p.Object()
87-
8887
gl := GL.API(p)
8988

9089
width := float32(obj.Int("width"))

cpp/painter.cpp renamed to qpainter/painter.cpp

+23-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
#include <private/qmetaobjectbuilder_p.h>
1+
// #include <private/qmetaobjectbuilder_p.h>
22

3-
#include <QtQml/QtQml>
4-
#include <QQmlEngine>
5-
#include <QDebug>
3+
// #include <QtQml/QtQml>
4+
#include <QPainter>
5+
// #include <QQmlEngine>
6+
// #include <QDebug>
67

78
#include "painter.h"
8-
#include "capi.h"
99

1010

1111

@@ -97,21 +97,33 @@ void painterRestore(QPainter_ *painter){
9797
// void painterSetWorldMatrixEnabled(QPainter_ *painter, bool enabled);
9898
// bool painterWorldMatrixEnabled(QPainter_ *painter) const;
9999
//
100-
// void painterScale(QPainter_ *painter, qreal sx, qreal sy);
101-
// void painterShear(QPainter_ *painter, qreal sh, qreal sv);
102-
// void painterRotate(QPainter_ *painter, qreal a);
100+
void painterScale(QPainter_ *painter, qreal sx, qreal sy) {
101+
reinterpret_cast<QPainter *>(painter)->scale(sx, sy);
102+
}
103+
void painterShear(QPainter_ *painter, qreal sh, qreal sv) {
104+
reinterpret_cast<QPainter *>(painter)->shear(sh, sv);
105+
}
106+
void painterRotate(QPainter_ *painter, qreal a) {
107+
reinterpret_cast<QPainter *>(painter)->rotate(a);
108+
}
103109
//
104110
// void painterTranslate(QPainter_ *painter, const QPointF &offset);
105111
// void painterTranslate(QPainter_ *painter, const QPoint &offset);
106-
// void painterTranslate(QPainter_ *painter, qreal dx, qreal dy);
112+
void painterTranslate(QPainter_ *painter, qreal dx, qreal dy) {
113+
reinterpret_cast<QPainter *>(painter)->translate(dx, dy);
114+
}
107115
//
108116
// QRect painterWindow(QPainter_ *painter) const;
109117
// void painterSetWindow(QPainter_ *painter, const QRect &window);
110-
// void painterSetWindow(QPainter_ *painter, int x, int y, int w, int h);
118+
void painterSetWindow(QPainter_ *painter, int x, int y, int w, int h) {
119+
reinterpret_cast<QPainter *>(painter)->setWindow(x, y, w, h);
120+
}
111121
//
112122
// QRect painterViewport(QPainter_ *painter) const;
113123
// void painterSetViewport(QPainter_ *painter, const QRect &viewport);
114-
// void painterSetViewport(QPainter_ *painter, int x, int y, int w, int h);
124+
void painterSetViewport(QPainter_ *painter, int x, int y, int w, int h) {
125+
reinterpret_cast<QPainter *>(painter)->setViewport(x, y, w, h);
126+
}
115127
//
116128
// void painterSetViewTransformEnabled(QPainter_ *painter, bool enable);
117129
// bool painterViewTransformEnabled(QPainter_ *painter) const;

cpp/painter.h renamed to qpainter/painter.h

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
#ifndef PAINTER_H
22
#define PAINTER_H
33

4-
// #include <private/qmetaobject_p.h>
5-
6-
// #include <QQuickPaintedItem>
7-
// #include <QPainter>
8-
94
#include "capi.h"
105

116
#ifdef __cplusplus
127
extern "C" {
138
#endif
149

10+
#define qreal double
11+
1512

1613

1714
// Enums stolen from <QPainter>
@@ -190,21 +187,21 @@ void painterRestore(QPainter_ *painter);
190187
// void painterSetWorldMatrixEnabled(QPainter_ *painter, bool enabled);
191188
// bool painterWorldMatrixEnabled(QPainter_ *painter) const;
192189
//
193-
// void painterScale(QPainter_ *painter, qreal sx, qreal sy);
194-
// void painterShear(QPainter_ *painter, qreal sh, qreal sv);
195-
// void painterRotate(QPainter_ *painter, qreal a);
190+
void painterScale(QPainter_ *painter, qreal sx, qreal sy);
191+
void painterShear(QPainter_ *painter, qreal sh, qreal sv);
192+
void painterRotate(QPainter_ *painter, qreal a);
196193
//
197194
// void painterTranslate(QPainter_ *painter, const QPointF &offset);
198195
// void painterTranslate(QPainter_ *painter, const QPoint &offset);
199-
// void painterTranslate(QPainter_ *painter, qreal dx, qreal dy);
196+
void painterTranslate(QPainter_ *painter, qreal dx, qreal dy);
200197
//
201198
// QRect painterWindow(QPainter_ *painter) const;
202199
// void painterSetWindow(QPainter_ *painter, const QRect &window);
203-
// void painterSetWindow(QPainter_ *painter, int x, int y, int w, int h);
200+
void painterSetWindow(QPainter_ *painter, int x, int y, int w, int h);
204201
//
205202
// QRect painterViewport(QPainter_ *painter) const;
206203
// void painterSetViewport(QPainter_ *painter, const QRect &viewport);
207-
// void painterSetViewport(QPainter_ *painter, int x, int y, int w, int h);
204+
void painterSetViewport(QPainter_ *painter, int x, int y, int w, int h);
208205
//
209206
// void painterSetViewTransformEnabled(QPainter_ *painter, bool enable);
210207
// bool painterViewTransformEnabled(QPainter_ *painter) const;
@@ -406,6 +403,6 @@ void painterEndNativePainting(QPainter_ *painter);
406403
} // extern "C"
407404
#endif
408405

409-
#endif // GOVALUE_H
406+
#endif // PAINTER_H
410407

411408
// vim:ts=4:sw=4:et:ft=cpp

qmlpainter.go renamed to qpainter/qpainter.go

+46-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
package qml
1+
package qpainter
22

3-
// #include <stdlib.h>
4-
//
5-
// #include "capi.h"
3+
// #cgo CPPFLAGS: -I../cpp
4+
// #cgo CXXFLAGS: -std=c++0x -pedantic-errors -Wall -fno-strict-aliasing
5+
// #cgo LDFLAGS: -lstdc++
6+
// #cgo pkg-config: Qt5Core Qt5Widgets Qt5Quick
67
//
8+
// #include <stdlib.h>
79
// #include "painter.h"
810
//
911
import "C"
@@ -74,17 +76,21 @@ const /* CompositionMode */ (
7476

7577
// Painter is provided to Paint methods on Go types that have displayable content.
7678
type Painter struct {
77-
engine *Engine
78-
obj Object
79+
// engine *Engine
80+
// obj Object
7981
glctxt glbase.Context
8082
qpainter unsafe.Pointer
8183
}
8284

83-
// Object returns the underlying object being painted.
84-
func (p *Painter) Object() Object {
85-
return p.obj
85+
func FromPtr(ptr unsafe.Pointer) *Painter {
86+
return &Painter{qpainter: ptr}
8687
}
8788

89+
// Object returns the underlying object being painted.
90+
// func (p *Painter) Object() Object {
91+
// return p.obj
92+
// }
93+
8894
// GLContext returns the OpenGL context for this painter.
8995
func (p *Painter) GLContext() *glbase.Context {
9096
return &p.glctxt
@@ -105,6 +111,37 @@ func (p *Painter) Restore() {
105111
C.painterRestore(p.qpainter)
106112
}
107113

114+
func (p *Painter) Scale(sx float64, sy float64) {
115+
C.painterScale(p.qpainter, C.qreal(sx), C.qreal(sy))
116+
}
117+
func (p *Painter) Shear(sh float64, sv float64) {
118+
C.painterShear(p.qpainter, C.qreal(sh), C.qreal(sv))
119+
}
120+
func (p *Painter) Rotate(a float64) {
121+
C.painterRotate(p.qpainter, C.qreal(a))
122+
}
123+
124+
//
125+
// void painterTranslate(QPainter_ *painter, const QPointF &offset);
126+
// void painterTranslate(QPainter_ *painter, const QPoint &offset);
127+
func (p *Painter) Translate(dx float64, dy float64) {
128+
C.painterTranslate(p.qpainter, C.qreal(dx), C.qreal(dy))
129+
}
130+
131+
//
132+
// QRect painterWindow(QPainter_ *painter) const;
133+
// void painterSetWindow(QPainter_ *painter, const QRect &window);
134+
func (p *Painter) SetWindow(x int, y int, w int, h int) {
135+
C.painterSetWindow(p.qpainter, C.int(x), C.int(y), C.int(w), C.int(h))
136+
}
137+
138+
//
139+
// QRect painterViewport(QPainter_ *painter) const;
140+
// void painterSetViewport(QPainter_ *painter, const QRect &viewport);
141+
func (p *Painter) SetViewport(x int, y int, w int, h int) {
142+
C.painterSetViewport(p.qpainter, C.int(x), C.int(y), C.int(w), C.int(h))
143+
}
144+
108145
func (p *Painter) BeginNativePainting() {
109146
C.painterBeginNativePainting(p.qpainter)
110147
}

0 commit comments

Comments
 (0)