Add insertWidget to QBoxLayout and stretch arg (#286)
* sync upstream * Add insertWidget to QBoxLayout and stretch arg
This commit is contained in:
parent
8793f172e3
commit
d238f4f3e8
@ -26,6 +26,7 @@ class QBoxLayoutWrap : public Napi::ObjectWrap<QBoxLayoutWrap> {
|
||||
Napi::Value addStretch(const Napi::CallbackInfo& info);
|
||||
Napi::Value addStrut(const Napi::CallbackInfo& info);
|
||||
Napi::Value addWidget(const Napi::CallbackInfo& info);
|
||||
Napi::Value insertWidget(const Napi::CallbackInfo& info);
|
||||
Napi::Value direction(const Napi::CallbackInfo& info);
|
||||
Napi::Value insertLayout(const Napi::CallbackInfo& info);
|
||||
Napi::Value removeWidget(const Napi::CallbackInfo& info);
|
||||
|
||||
@ -15,6 +15,7 @@ Napi::Object QBoxLayoutWrap::init(Napi::Env env, Napi::Object exports) {
|
||||
InstanceMethod("addStretch", &QBoxLayoutWrap::addStretch),
|
||||
InstanceMethod("addStrut", &QBoxLayoutWrap::addStrut),
|
||||
InstanceMethod("addWidget", &QBoxLayoutWrap::addWidget),
|
||||
InstanceMethod("insertWidget", &QBoxLayoutWrap::insertWidget),
|
||||
InstanceMethod("direction", &QBoxLayoutWrap::direction),
|
||||
InstanceMethod("insertLayout", &QBoxLayoutWrap::insertLayout),
|
||||
InstanceMethod("removeWidget", &QBoxLayoutWrap::removeWidget),
|
||||
@ -92,7 +93,22 @@ Napi::Value QBoxLayoutWrap::addWidget(const Napi::CallbackInfo& info) {
|
||||
|
||||
Napi::Object qwidgetObject = info[0].As<Napi::Object>();
|
||||
QWidgetWrap* widget = Napi::ObjectWrap<QWidgetWrap>::Unwrap(qwidgetObject);
|
||||
this->instance->addWidget(widget->getInternalInstance());
|
||||
int stretch = info[1].As<Napi::Number>().Int32Value();
|
||||
|
||||
this->instance->addWidget(widget->getInternalInstance(), stretch);
|
||||
return env.Null();
|
||||
}
|
||||
|
||||
Napi::Value QBoxLayoutWrap::insertWidget(const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
|
||||
int index = info[0].As<Napi::Number>().Int32Value();
|
||||
Napi::Object qwidgetObject = info[1].As<Napi::Object>();
|
||||
QWidgetWrap* widget = Napi::ObjectWrap<QWidgetWrap>::Unwrap(qwidgetObject);
|
||||
int stretch = info[2].As<Napi::Number>().Int32Value();
|
||||
|
||||
this->instance->insertWidget(index, widget->getInternalInstance(), stretch);
|
||||
return env.Null();
|
||||
}
|
||||
|
||||
|
||||
93
src/demo.ts
93
src/demo.ts
@ -1,83 +1,24 @@
|
||||
import {
|
||||
FlexLayout,
|
||||
PenStyle,
|
||||
QColor,
|
||||
QMainWindow,
|
||||
QMainWindowEvents,
|
||||
QPainter,
|
||||
QPoint,
|
||||
QWidget,
|
||||
RenderHint,
|
||||
} from './index';
|
||||
import { Direction, QMainWindow, QBoxLayout, QWidget, QLabel } from './index';
|
||||
|
||||
const win = new QMainWindow();
|
||||
const center = new QWidget();
|
||||
const layout = new FlexLayout();
|
||||
const hourHand = [new QPoint(7, 8), new QPoint(-7, 8), new QPoint(0, -40)];
|
||||
const minuteHand = [new QPoint(7, 8), new QPoint(-7, 8), new QPoint(0, -70)];
|
||||
const secondHand = [new QPoint(4, 8), new QPoint(-4, 8), new QPoint(0, -70)];
|
||||
const hourColor = new QColor(127, 0, 127);
|
||||
const minuteColor = new QColor(0, 127, 127, 191);
|
||||
const secondColor = new QColor(0, 0, 0);
|
||||
const box = new QBoxLayout(Direction.TopToBottom);
|
||||
center.setLayout(box);
|
||||
|
||||
center.setLayout(layout);
|
||||
win.setWindowTitle('Analog Clock');
|
||||
win.resize(200, 200);
|
||||
const side = Math.min(win.geometry().width(), win.geometry().height());
|
||||
|
||||
function repaint(): void {
|
||||
win.repaint();
|
||||
setTimeout(repaint, 1000);
|
||||
}
|
||||
|
||||
setTimeout(repaint, 1000);
|
||||
win.addEventListener(QMainWindowEvents.Paint, () => {
|
||||
const time = new Date();
|
||||
|
||||
const painter = new QPainter(win);
|
||||
painter.setRenderHint(RenderHint.Antialiasing);
|
||||
painter.translate(win.geometry().width() / 2, win.geometry().height() / 2);
|
||||
painter.scale(side / 200.0, side / 200.0);
|
||||
|
||||
painter.setPen(PenStyle.NoPen);
|
||||
painter.setBrush(hourColor);
|
||||
|
||||
painter.save();
|
||||
painter.rotate(30.0 * (time.getHours() + time.getMinutes() / 60.0));
|
||||
painter.drawConvexPolygon(hourHand);
|
||||
painter.restore();
|
||||
|
||||
painter.setPen(hourColor);
|
||||
|
||||
for (let i = 0; i < 12; ++i) {
|
||||
painter.drawLine(88, 0, 96, 0);
|
||||
painter.rotate(30.0);
|
||||
}
|
||||
|
||||
painter.setPen(PenStyle.NoPen);
|
||||
painter.setBrush(minuteColor);
|
||||
|
||||
painter.save();
|
||||
painter.rotate(6.0 * (time.getMinutes() + time.getSeconds() / 60.0));
|
||||
painter.drawConvexPolygon(minuteHand);
|
||||
painter.restore();
|
||||
|
||||
painter.setBrush(secondColor);
|
||||
painter.setPen(PenStyle.NoPen);
|
||||
|
||||
painter.save();
|
||||
painter.rotate(360 * (time.getSeconds() / 60.0));
|
||||
painter.drawConvexPolygon(secondHand);
|
||||
painter.restore();
|
||||
|
||||
painter.setPen(minuteColor);
|
||||
for (let j = 0; j < 60; ++j) {
|
||||
if (j % 5 != 0) {
|
||||
painter.drawLine(92, 0, 96, 0);
|
||||
}
|
||||
painter.rotate(6.0);
|
||||
}
|
||||
painter.end();
|
||||
[1, 2, 3, 4].forEach(num => {
|
||||
const label = new QLabel();
|
||||
label.setText(`Hello Label ${num}`);
|
||||
const color = `#${(2 * num).toString().repeat(6)}`;
|
||||
label.setInlineStyle(`border: 2px solid ${color}`);
|
||||
box.addWidget(label, num);
|
||||
return label;
|
||||
});
|
||||
const label = new QLabel();
|
||||
label.setText('Inserted label');
|
||||
box.insertWidget(2, label, 5);
|
||||
win.setCentralWidget(center);
|
||||
win.resize(200, 400);
|
||||
win.setWindowTitle('box stretch');
|
||||
|
||||
win.show();
|
||||
(global as any).win = win;
|
||||
|
||||
@ -32,8 +32,12 @@ export class QBoxLayout extends NodeLayout {
|
||||
addStrut(size: number): void {
|
||||
this.native.addStrut(size);
|
||||
}
|
||||
addWidget(widget: NodeWidget): void {
|
||||
this.native.addWidget(widget.native);
|
||||
addWidget(widget: NodeWidget, stretch = 0): void {
|
||||
this.native.addWidget(widget.native, stretch);
|
||||
this.nodeChildren.add(widget);
|
||||
}
|
||||
insertWidget(index: number, widget: NodeWidget, stretch = 0): void {
|
||||
this.native.insertWidget(index, widget.native, stretch);
|
||||
this.nodeChildren.add(widget);
|
||||
}
|
||||
direction(): Direction {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user