add font to QAction (#307)

* add font to QAction

* lint
This commit is contained in:
slidinghotdog 2019-12-28 15:34:10 -03:00 committed by Atul R
parent 8521efbe32
commit 96e7799988
4 changed files with 35 additions and 75 deletions

View File

@ -31,6 +31,7 @@ class QActionWrap : public Napi::ObjectWrap<QActionWrap> {
Napi::Value setChecked(const Napi::CallbackInfo& info);
Napi::Value isSeparator(const Napi::CallbackInfo& info);
Napi::Value setSeparator(const Napi::CallbackInfo& info);
Napi::Value setFont(const Napi::CallbackInfo& info);
QOBJECT_WRAPPED_METHODS_DECLARATION
};

View File

@ -1,5 +1,6 @@
#include "QtWidgets/QAction/qaction_wrap.h"
#include <QFont>
#include <QWidget>
#include "Extras/Utils/nutils.h"
@ -26,6 +27,7 @@ Napi::Object QActionWrap::init(Napi::Env env, Napi::Object exports) {
InstanceMethod("setChecked", &QActionWrap::setChecked),
InstanceMethod("isSeparator", &QActionWrap::isSeparator),
InstanceMethod("setSeparator", &QActionWrap::setSeparator),
InstanceMethod("setFont", &QActionWrap::setFont),
QOBJECT_WRAPPED_METHODS_EXPORT_DEFINE(QActionWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
@ -190,3 +192,15 @@ Napi::Value QActionWrap::setSeparator(const Napi::CallbackInfo& info) {
return env.Null();
}
Napi::Value QActionWrap::setFont(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
std::string family = info[0].As<Napi::String>().Utf8Value();
int pointSize = info[1].As<Napi::Number>().Int32Value();
int weight = info[2].As<Napi::Number>().Int32Value();
bool italic = info[3].As<Napi::Boolean>().Value();
QFont f(QString::fromStdString(family.c_str()), pointSize, weight, italic);
this->instance->setFont(f);
return env.Null();
}

View File

@ -1,87 +1,29 @@
import {
FlexLayout,
PenStyle,
WidgetEventTypes,
QColor,
QMainWindow,
QPainter,
QPoint,
QWidget,
RenderHint,
} from './index';
import { FlexLayout, QMainWindow, QWidget } from './index';
import { QLabel } from './lib/QtWidgets/QLabel';
import { QSystemTrayIcon } from './lib/QtWidgets/QSystemTrayIcon';
import { QMenu } from './lib/QtWidgets/QMenu';
import { QAction } from './lib/QtWidgets/QAction';
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 label = new QLabel();
const tray = new QSystemTrayIcon();
const menu = new QMenu();
const action = new QAction();
center.setLayout(layout);
win.setWindowTitle('Analog Clock');
action.setText('action');
action.setFont('Mono', 20);
menu.addAction(action);
tray.setContextMenu(menu);
tray.show();
const side = Math.min(win.geometry().width(), win.geometry().height());
function repaint(): void {
win.repaint();
setTimeout(repaint, 1000);
}
setTimeout(repaint, 1000);
win.addEventListener(WidgetEventTypes.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();
});
label.setText('Hello!');
layout.addWidget(label);
win.setCentralWidget(center);
win.resize(400, 400);
win.show();
(global as any).win = win;
(global as any).tray = tray;

View File

@ -72,4 +72,7 @@ export class QAction extends NodeObject<QActionSignals> {
setSeparator(isSeparator: boolean): void {
this.native.setSeparator(isSeparator);
}
setFont(family: string, pointSize = -1, weight = -1, italic = false): void {
this.native.setFont(family, pointSize, weight, italic);
}
}