-
Notifications
You must be signed in to change notification settings - Fork 76
/
qml_ex.jl
69 lines (55 loc) · 1.51 KB
/
qml_ex.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
ENV["QSG_RENDER_LOOP"] = "basic"
using CxxWrap # for safe_cfunction
using QML
using Observables
using GR
const qmlfile = joinpath(dirname(Base.source_path()), "qml_ex.qml")
x = randn(1000000)
y = randn(1000000)
nbins = Observable(30)
w, h = (600, 450)
zoom = Nothing
# Arguments here need to be the "reference types", hence the "Ref" suffix
function paint(p::CxxPtr{QPainter}, item::CxxPtr{JuliaPaintedItem})
global w, h
global zoom
ENV["GKSwstype"] = 381
ENV["GKSconid"] = split(repr(p.cpp_object), "@")[2]
dev = device(p[])[]
r = effectiveDevicePixelRatio(window(item[])[])
w, h = width(dev) / r, height(dev) / r
plt = kvs()
plt[:size] = (w, h)
if zoom === Nothing
xmin, xmax, ymin, ymax = (-5, 5, -5, 5)
elseif zoom != 0
xmin, xmax, ymin, ymax = panzoom(0, 0, zoom)
else
xmin, xmax, ymin, ymax = inqwindow()
end
num_bins = Int64(round(nbins[]))
hexbin(x, y, nbins=num_bins, xlim=(xmin, xmax), ylim=(ymin, ymax), title="nbins: $num_bins")
return
end
function mousePosition(eventx, eventy, deltay)
global zoom
if deltay != 0
zoom = deltay < 0 ? 1.02 : 1/1.02
else
zoom = 0
end
if w > h
xn = eventx / w
yn = (h - eventy) / w
else
xn = eventx / h
yn = (h - eventy) / h
end
x, y = ndctowc(xn, yn)
"($(round(x,digits=4)), $(round(y,digits=4)))"
end
loadqml(qmlfile,
paint_cfunction = @safe_cfunction(paint, Cvoid, (CxxPtr{QPainter}, CxxPtr{JuliaPaintedItem})),
parameters = JuliaPropertyMap("nbins" => nbins))
@qmlfunction mousePosition
exec()