From 96e77999880cffd862cd8532d394822e7398f131 Mon Sep 17 00:00:00 2001 From: slidinghotdog <33790211+slidinghotdog@users.noreply.github.com> Date: Sat, 28 Dec 2019 15:34:10 -0300 Subject: [PATCH] add font to QAction (#307) * add font to QAction * lint --- .../nodegui/QtWidgets/QAction/qaction_wrap.h | 1 + .../lib/QtWidgets/QAction/qaction_wrap.cpp | 14 +++ src/demo.ts | 92 ++++--------------- src/lib/QtWidgets/QAction.ts | 3 + 4 files changed, 35 insertions(+), 75 deletions(-) diff --git a/src/cpp/include/nodegui/QtWidgets/QAction/qaction_wrap.h b/src/cpp/include/nodegui/QtWidgets/QAction/qaction_wrap.h index 98b8dd92e..e6c0c8be3 100644 --- a/src/cpp/include/nodegui/QtWidgets/QAction/qaction_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QAction/qaction_wrap.h @@ -31,6 +31,7 @@ class QActionWrap : public Napi::ObjectWrap { 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 }; diff --git a/src/cpp/lib/QtWidgets/QAction/qaction_wrap.cpp b/src/cpp/lib/QtWidgets/QAction/qaction_wrap.cpp index 5986831dc..d90965b0b 100644 --- a/src/cpp/lib/QtWidgets/QAction/qaction_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QAction/qaction_wrap.cpp @@ -1,5 +1,6 @@ #include "QtWidgets/QAction/qaction_wrap.h" +#include #include #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().Utf8Value(); + int pointSize = info[1].As().Int32Value(); + int weight = info[2].As().Int32Value(); + bool italic = info[3].As().Value(); + QFont f(QString::fromStdString(family.c_str()), pointSize, weight, italic); + this->instance->setFont(f); + return env.Null(); +} \ No newline at end of file diff --git a/src/demo.ts b/src/demo.ts index 7974eb37b..fd3005019 100644 --- a/src/demo.ts +++ b/src/demo.ts @@ -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; diff --git a/src/lib/QtWidgets/QAction.ts b/src/lib/QtWidgets/QAction.ts index 94695e199..7aba67a63 100644 --- a/src/lib/QtWidgets/QAction.ts +++ b/src/lib/QtWidgets/QAction.ts @@ -72,4 +72,7 @@ export class QAction extends NodeObject { 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); + } }