-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.dart
164 lines (136 loc) · 5.1 KB
/
main.dart
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import 'package:flutter/material.dart';
import 'dart:io';
/*
this is needed atm to let qtdeploy/qtminimal know that this file needs to be analysed and the following modules need to be generated
import (
"github.com/therecipe/qt/core"
"github.com/therecipe/qt/quick"
"github.com/therecipe/qt/gui"
"github.com/therecipe/qt/widgets"
"github.com/therecipe/qt/quickcontrols2"
)
*/
import 'qt/interop.dart' as interop;
import 'qt/core.dart' as core;
import 'qt/gui.dart' as gui;
import 'qt/quick.dart' as quick;
import 'qt/widgets.dart' as widgets;
import 'qt/quickcontrols2.dart' as quickcontrols2;
final interopEngine = (!Platform.isWindows) ? interop.PseudoQJSEngine_qjsEngine(null) : null;
final quickWidget = (!Platform.isWindows) ? quick.NewQQuickWidget(null) : null;
void main() {
if (!Platform.isWindows) {
final dartFunction = (String s) {
print(s);
};
interopEngine.GlobalObject().SetProperty("dartFunction", interopEngine.NewGoType(dartFunction));
// setup the qml widget
quickcontrols2.QQuickStyle_SetStyle("Material");
quickWidget.Engine().GlobalObject().SetProperty("dartFunction", quickWidget.Engine().NewGoType(dartFunction));
quickWidget.Engine().GlobalObject().SetProperty("qml", quickWidget.Engine().NewObject()); // create an empty object here, so we can write to it later from within Qml (since we can't directly add properties to the globalObject from within Qml)
quickWidget.SetMinimumSize(core.NewQSize2(400, 300));
quickWidget.SetResizeMode(1); //TODO: quick.QQuickWidget__SizeRootObjectToView
quickWidget.SetSource(core.NewQUrl3("qrc:/qml/main.qml", 0));
interopEngine.GlobalObject().SetProperty("quickWidget", interopEngine.NewGoType(quickWidget));
// call into go and let it finish the layout setup
interopEngine.GlobalObject().Property("goSetupFunction").Call(null);
}
//
// simplified port of the pixel editor example: https://github.com/therecipe/qt/blob/master/internal/examples/widgets/pixel_editor/pixel_editor.go
//
if (true) {
final view = widgets.NewQGraphicsView(null);
final scene = widgets.NewQGraphicsScene(view);
view.SetScene(scene);
view.ConnectResizeEvent((core.QEvent event) {
view.FitInView(scene.ItemsBoundingRect(), 1);
});
final img = gui.NewQImage3(16, 32, 5);
for (var x = 0; x < img.Width(); x++) {
for (var y = 0; y < img.Height(); y++) {
img.SetPixelColor2(x, y, gui.NewQColor3(x * 3, y * 6, x * 9, 255));
}
}
final item = widgets.NewQGraphicsPixmapItem2(gui.QPixmap_FromImage(img, 0), null);
scene.AddItem(item);
final color = gui.NewQColor3(255, 255, 255, 255);
final drawPixel = (num xF, num yF) {
final x = xF.toInt();
final y = yF.toInt();
final pixmap = item.Pixmap();
if (x >= 1 && x < pixmap.Width() - 1 && y >= 1 && y < pixmap.Height() - 1) {
final img = item.Pixmap().ToImage();
img.SetPixelColor2(x, y, color);
item.SetPixmap(gui.QPixmap_FromImage(img, 0));
}
};
item.ConnectMouseMoveEvent((widgets.QGraphicsSceneMouseEvent event) {
final mousePosition = event.Pos();
drawPixel(mousePosition.X(), mousePosition.Y());
});
item.ConnectMousePressEvent((widgets.QGraphicsSceneMouseEvent event) {
final mousePosition = event.Pos();
drawPixel(mousePosition.X(), mousePosition.Y());
});
view.Resize2(400, 800);
view.Show();
}
//
// test stack depth
//
if (true) {
final o = core.NewQObject(null);
o.ConnectObjectNameChanged((String) {
if (o.ObjectName().length < 300) {
o.SetObjectName(o.ObjectName() + "x");
}
});
o.SetObjectName("x");
}
runApp(MyApp());
}
/// This Widget is the main application widget.
class MyApp extends StatelessWidget {
static const String _title = 'Dart-Go-Qml Interop';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: Scaffold(
appBar: AppBar(title: const Text(_title)),
body: MyStatelessWidget(),
),
);
}
}
/// This is the stateless widget that the main application instantiates.
class MyStatelessWidget extends StatelessWidget {
MyStatelessWidget({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
RaisedButton(
onPressed: () {
if (!Platform.isWindows) {
interopEngine.GlobalObject().Property("goFunction").Call([interopEngine.NewGoType("Hello from Dart")]);
}
},
child: const Text('Dart calls Go', style: TextStyle(fontSize: 20)),
),
const SizedBox(height: 30),
RaisedButton(
onPressed: () {
if (!Platform.isWindows) {
quickWidget.Engine().GlobalObject().Property("qml").Property("qmlFunction").Call([quickWidget.Engine().NewGoType("Hello from Dart")]);
}
},
child: const Text('Dart calls Qml', style: TextStyle(fontSize: 20)),
),
],
),
);
}
}