diff --git a/src/cpp/include/nodegui/QtCore/QColor/qcolor_wrap.h b/src/cpp/include/nodegui/QtCore/QColor/qcolor_wrap.h index 8a67143d4..ac779af51 100644 --- a/src/cpp/include/nodegui/QtCore/QColor/qcolor_wrap.h +++ b/src/cpp/include/nodegui/QtCore/QColor/qcolor_wrap.h @@ -1,7 +1,6 @@ #pragma once #include -#include #include @@ -18,9 +17,17 @@ class QColorWrap : public Napi::ObjectWrap { ~QColorWrap(); QColor* getInternalInstance(); // Wrapped methods - + Napi::Value setRed(const Napi::CallbackInfo& info); + Napi::Value red(const Napi::CallbackInfo& info); + Napi::Value setGreen(const Napi::CallbackInfo& info); + Napi::Value green(const Napi::CallbackInfo& info); + Napi::Value setBlue(const Napi::CallbackInfo& info); + Napi::Value blue(const Napi::CallbackInfo& info); + Napi::Value setAlpha(const Napi::CallbackInfo& info); + Napi::Value alpha(const Napi::CallbackInfo& info); COMPONENT_WRAPPED_METHODS_DECLARATION }; namespace StaticQColorWrapMethods { +Napi::Value fromQVariant(const Napi::CallbackInfo& info); } // namespace StaticQColorWrapMethods diff --git a/src/cpp/include/nodegui/QtCore/QPoint/qpoint_wrap.h b/src/cpp/include/nodegui/QtCore/QPoint/qpoint_wrap.h index d0f0ef90b..c92520cc6 100644 --- a/src/cpp/include/nodegui/QtCore/QPoint/qpoint_wrap.h +++ b/src/cpp/include/nodegui/QtCore/QPoint/qpoint_wrap.h @@ -1,7 +1,6 @@ #pragma once #include -#include #include @@ -18,7 +17,10 @@ class QPointWrap : public Napi::ObjectWrap { ~QPointWrap(); QPoint* getInternalInstance(); // Wrapped methods - + Napi::Value setX(const Napi::CallbackInfo& info); + Napi::Value setY(const Napi::CallbackInfo& info); + Napi::Value x(const Napi::CallbackInfo& info); + Napi::Value y(const Napi::CallbackInfo& info); COMPONENT_WRAPPED_METHODS_DECLARATION }; diff --git a/src/cpp/lib/QtCore/QColor/qcolor_wrap.cpp b/src/cpp/lib/QtCore/QColor/qcolor_wrap.cpp index b0d007e20..627cd71ef 100644 --- a/src/cpp/lib/QtCore/QColor/qcolor_wrap.cpp +++ b/src/cpp/lib/QtCore/QColor/qcolor_wrap.cpp @@ -8,8 +8,18 @@ Napi::FunctionReference QColorWrap::constructor; Napi::Object QColorWrap::init(Napi::Env env, Napi::Object exports) { Napi::HandleScope scope(env); char CLASSNAME[] = "QColor"; - Napi::Function func = - DefineClass(env, CLASSNAME, {COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE}); + Napi::Function func = DefineClass( + env, CLASSNAME, + {InstanceMethod("setRed", &QColorWrap::setRed), + InstanceMethod("red", &QColorWrap::red), + InstanceMethod("setGreen", &QColorWrap::setGreen), + InstanceMethod("green", &QColorWrap::green), + InstanceMethod("setBlue", &QColorWrap::setBlue), + InstanceMethod("blue", &QColorWrap::blue), + InstanceMethod("setAlpha", &QColorWrap::setAlpha), + InstanceMethod("alpha", &QColorWrap::alpha), + StaticMethod("fromQVariant", &StaticQColorWrapMethods::fromQVariant), + COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); return exports; @@ -56,3 +66,66 @@ QColorWrap::QColorWrap(const Napi::CallbackInfo& info) QColorWrap::~QColorWrap() { this->instance.reset(); } QColor* QColorWrap::getInternalInstance() { return this->instance.get(); } + +Napi::Value QColorWrap::setRed(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + int red = info[0].As().Int32Value(); + this->instance->setRed(red); + return env.Null(); +} +Napi::Value QColorWrap::red(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + return Napi::Value::From(env, this->instance->red()); +} +Napi::Value QColorWrap::setGreen(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + int value = info[0].As().Int32Value(); + this->instance->setGreen(value); + return env.Null(); +} +Napi::Value QColorWrap::green(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + return Napi::Value::From(env, this->instance->green()); +} +Napi::Value QColorWrap::setBlue(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + int value = info[0].As().Int32Value(); + this->instance->setBlue(value); + return env.Null(); +} +Napi::Value QColorWrap::blue(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + return Napi::Value::From(env, this->instance->blue()); +} +Napi::Value QColorWrap::setAlpha(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + int value = info[0].As().Int32Value(); + this->instance->setAlpha(value); + return env.Null(); +} +Napi::Value QColorWrap::alpha(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + return Napi::Value::From(env, this->instance->alpha()); +} + +Napi::Value StaticQColorWrapMethods::fromQVariant( + const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + Napi::Object variantObject = info[0].As(); + QVariantWrap* variantWrap = + Napi::ObjectWrap::Unwrap(variantObject); + QVariant* variant = variantWrap->getInternalInstance(); + QColor color = variant->value(); + auto instance = QColorWrap::constructor.New( + {Napi::External::New(env, new QColor(color))}); + return instance; +} \ No newline at end of file diff --git a/src/cpp/lib/QtCore/QPoint/qpoint_wrap.cpp b/src/cpp/lib/QtCore/QPoint/qpoint_wrap.cpp index fb3d705df..668ba1f6b 100644 --- a/src/cpp/lib/QtCore/QPoint/qpoint_wrap.cpp +++ b/src/cpp/lib/QtCore/QPoint/qpoint_wrap.cpp @@ -8,8 +8,14 @@ Napi::FunctionReference QPointWrap::constructor; Napi::Object QPointWrap::init(Napi::Env env, Napi::Object exports) { Napi::HandleScope scope(env); char CLASSNAME[] = "QPoint"; - Napi::Function func = - DefineClass(env, CLASSNAME, {COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE}); + Napi::Function func = DefineClass( + env, CLASSNAME, + {InstanceMethod("setX", &QPointWrap::setX), + InstanceMethod("x", &QPointWrap::x), + InstanceMethod("setY", &QPointWrap::setY), + InstanceMethod("y", &QPointWrap::y), + StaticMethod("fromQVariant", &StaticQPointWrapMethods::fromQVariant), + COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); return exports; @@ -39,3 +45,42 @@ QPointWrap::QPointWrap(const Napi::CallbackInfo& info) QPointWrap::~QPointWrap() { this->instance.reset(); } QPoint* QPointWrap::getInternalInstance() { return this->instance.get(); } + +Napi::Value QPointWrap::setX(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + int x = info[0].As().Int32Value(); + this->instance->setX(x); + return env.Null(); +} +Napi::Value QPointWrap::setY(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + int y = info[0].As().Int32Value(); + this->instance->setY(y); + return env.Null(); +} +Napi::Value QPointWrap::x(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + return Napi::Value::From(env, this->instance->x()); +} +Napi::Value QPointWrap::y(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + return Napi::Value::From(env, this->instance->y()); +} + +Napi::Value StaticQPointWrapMethods::fromQVariant( + const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + Napi::Object variantObject = info[0].As(); + QVariantWrap* variantWrap = + Napi::ObjectWrap::Unwrap(variantObject); + QVariant* variant = variantWrap->getInternalInstance(); + QPoint point = variant->value(); + auto instance = QPointWrap::constructor.New( + {Napi::External::New(env, new QPoint(point))}); + return instance; +} diff --git a/src/lib/QtCore/QColor.ts b/src/lib/QtCore/QColor.ts index 7534e5125..b8d362afc 100644 --- a/src/lib/QtCore/QColor.ts +++ b/src/lib/QtCore/QColor.ts @@ -1,25 +1,52 @@ import { Component, NativeElement } from '../core/Component'; import addon from '../utils/addon'; import { checkIfNativeElement } from '../utils/helpers'; +import { QVariant } from './QVariant'; export class QColor extends Component { native: NativeElement; - - //eslint-disable-next-line @typescript-eslint/no-inferrable-types - constructor(arg?: NativeElement | number | string, g: number = 0, b: number = 0, a: number = 0) { + constructor(arg?: NativeElement | number | string, g = 0, b = 0, a = 255) { super(); - const count = arguments.length; - if (count == 4) { - this.native = new addon.QColor(arg, g, b, a); - } else if (count == 3) { - this.native = new addon.QColor(arg, g, b); - } else if (count == 1 && checkIfNativeElement(arg)) { + if (checkIfNativeElement(arg)) { this.native = arg as NativeElement; - } else if (count == 1) { - console.log(typeof arg); + } else if (typeof arg === 'number') { + if (arguments.length === 1) { + // This is for QGlobalColor enum + this.native = new addon.QColor(arg); + } else { + this.native = new addon.QColor(arg, g, b, a); + } + } else if (typeof arg === 'string') { this.native = new addon.QColor(arg); } else { this.native = new addon.QColor(); } } + setRed(value: number): void { + this.native.setRed(value); + } + red(): number { + return this.native.red(); + } + setGreen(value: number): void { + this.native.setGreen(value); + } + green(): number { + return this.native.green(); + } + setBlue(value: number): void { + this.native.setBlue(value); + } + blue(): number { + return this.native.blue(); + } + setAlpha(value: number): void { + this.native.setAlpha(value); + } + alpha(): number { + return this.native.alpha(); + } + static fromQVariant(variant: QVariant): QColor { + return new QColor(addon.QColor.fromQVariant(variant.native)); + } } diff --git a/src/lib/QtCore/QPoint.ts b/src/lib/QtCore/QPoint.ts index d59a11fbe..647e2e3b5 100644 --- a/src/lib/QtCore/QPoint.ts +++ b/src/lib/QtCore/QPoint.ts @@ -2,19 +2,31 @@ import { NativeElement, Component } from '../core/Component'; import addon from '../utils/addon'; import { checkIfNativeElement } from '../utils/helpers'; import { QVariant } from './QVariant'; - export class QPoint extends Component { native: NativeElement; - //eslint-disable-next-line @typescript-eslint/no-inferrable-types - constructor(arg?: NativeElement | number, y: number = 0) { + constructor(arg?: NativeElement | number, y = 0) { super(); - const count = arguments.length; - if (count > 1) { - this.native = new addon.QPoint(arg, y); - } else if (count == 1 && checkIfNativeElement(arg)) { + if (checkIfNativeElement(arg)) { this.native = arg as NativeElement; + } else if (typeof arg === 'number') { + this.native = new addon.QPoint(arg, y); } else { this.native = new addon.QPoint(); } } + setX(value: number): void { + this.native.setX(value); + } + setY(value: number): void { + this.native.setY(value); + } + x(): number { + return this.native.x(); + } + y(): number { + return this.native.y(); + } + static fromQVariant(variant: QVariant): QPoint { + return new QPoint(addon.QPoint.fromQVariant(variant.native)); + } } diff --git a/src/lib/QtCore/QRect.ts b/src/lib/QtCore/QRect.ts index 60208f97b..5ad616d4f 100644 --- a/src/lib/QtCore/QRect.ts +++ b/src/lib/QtCore/QRect.ts @@ -5,8 +5,7 @@ import { QVariant } from './QVariant'; export class QRect extends Component { native: NativeElement; - //eslint-disable-next-line @typescript-eslint/no-inferrable-types - constructor(arg?: NativeElement | number, y: number = 0, width: number = 0, height: number = 0) { + constructor(arg?: NativeElement | number, y = 0, width = 0, height = 0) { super(); const count = arguments.length; if (count > 1) { diff --git a/src/lib/QtCore/__tests__/QColor.test.ts b/src/lib/QtCore/__tests__/QColor.test.ts new file mode 100644 index 000000000..b1ac876e4 --- /dev/null +++ b/src/lib/QtCore/__tests__/QColor.test.ts @@ -0,0 +1,67 @@ +import { QColor } from '../QColor'; +import { GlobalColor } from '../../QtEnums'; +import { QVariant } from '../QVariant'; + +describe('QColor', () => { + it('initialize empty', () => { + const color = new QColor(); + expect(color).toBeTruthy(); + }); + it('initialize with enum GlobalColor', () => { + const color = new QColor(GlobalColor.green); + expect(color.green()).toBe(255); + expect(color.red()).toBe(0); + expect(color.blue()).toBe(0); + expect(color.alpha()).toBe(255); + }); + it('initialize with rgba', () => { + const color = new QColor(12, 30, 40, 50); + expect(color.red()).toBe(12); + expect(color.green()).toBe(30); + expect(color.blue()).toBe(40); + expect(color.alpha()).toBe(50); + + const color2 = new QColor(11, 12, 13); + expect(color2.red()).toBe(11); + expect(color2.green()).toBe(12); + expect(color2.blue()).toBe(13); + expect(color2.alpha()).toBe(255); + }); + it('initialize with string', () => { + const color = new QColor('blue'); + expect(color.red()).toBe(0); + expect(color.green()).toBe(0); + expect(color.blue()).toBe(255); + expect(color.alpha()).toBe(255); + }); + it('setRed', () => { + const color = new QColor(); + color.setRed(20); + expect(color.red()).toBe(20); + }); + + it('setGreen', () => { + const color = new QColor(); + color.setGreen(20); + expect(color.green()).toBe(20); + }); + + it('setBlue', () => { + const color = new QColor(); + color.setBlue(20); + expect(color.blue()).toBe(20); + }); + + it('setAlpha', () => { + const color = new QColor(); + color.setAlpha(20); + expect(color.alpha()).toBe(20); + }); + + it('initialize from QVariant', () => { + const color = new QColor(10, 10, 10); + const variant = new QVariant(color); + expect(variant).toBeTruthy(); + expect(QColor.fromQVariant(variant).red()).toBe(10); + }); +}); diff --git a/src/lib/QtCore/__tests__/QPoint.test.ts b/src/lib/QtCore/__tests__/QPoint.test.ts new file mode 100644 index 000000000..1c9a09236 --- /dev/null +++ b/src/lib/QtCore/__tests__/QPoint.test.ts @@ -0,0 +1,34 @@ +import { QPoint } from '../QPoint'; +import { QVariant } from '../QVariant'; + +describe('QPoint', () => { + it('initialize empty', () => { + const point = new QPoint(); + expect(point).toBeTruthy(); + }); + it('initialize with x, y', () => { + const point = new QPoint(10, 11); + expect(point).toBeTruthy(); + expect(point.x()).toBe(10); + expect(point.y()).toBe(11); + const point2 = new QPoint(10); + expect(point2.x()).toBe(10); + expect(point2.y()).toBe(0); + }); + it('setY', () => { + const point = new QPoint(); + point.setY(300); + expect(point.y()).toBe(300); + }); + it('setX', () => { + const point = new QPoint(); + point.setX(200); + expect(point.x()).toBe(200); + }); + it('initialize from QVariant', () => { + const point = new QPoint(10, 10); + const variant = new QVariant(point); + expect(variant).toBeTruthy(); + expect(QPoint.fromQVariant(variant).x()).toBe(point.x()); + }); +}); diff --git a/src/lib/QtCore/__tests__/QRect.test.ts b/src/lib/QtCore/__tests__/QRect.test.ts index 23fc101f0..d665bc2ce 100644 --- a/src/lib/QtCore/__tests__/QRect.test.ts +++ b/src/lib/QtCore/__tests__/QRect.test.ts @@ -22,13 +22,13 @@ describe('QRect', () => { }); it('left', () => { const rect = new QRect(); - rect.setHeight(200); - expect(rect.height()).toBe(200); + rect.setLeft(200); + expect(rect.left()).toBe(200); }); it('top', () => { const rect = new QRect(); - rect.setHeight(200); - expect(rect.height()).toBe(200); + rect.setTop(200); + expect(rect.top()).toBe(200); }); it('initialize from QVariant', () => { const rect = new QRect(10, 10, 300, 200); diff --git a/src/lib/QtCore/__tests__/nodegui.png b/src/lib/QtCore/__tests__/nodegui.png deleted file mode 100644 index 16c3c0282..000000000 Binary files a/src/lib/QtCore/__tests__/nodegui.png and /dev/null differ diff --git a/src/lib/QtWidgets/QPainter.ts b/src/lib/QtWidgets/QPainter.ts index 23dab8541..3f19201f1 100644 --- a/src/lib/QtWidgets/QPainter.ts +++ b/src/lib/QtWidgets/QPainter.ts @@ -1,6 +1,8 @@ import addon from '../utils/addon'; import { Component, NativeElement } from '../core/Component'; -import { PenStyle, QColor, QPoint } from '../..'; +import { PenStyle } from '../QtEnums'; +import { QColor } from '../QtCore/QColor'; +import { QPoint } from '../QtCore/QPoint'; export enum RenderHint { Antialiasing = 0x01, @@ -50,7 +52,7 @@ export class QPainter extends Component { } } - setRenderHint(hint: RenderHint, on = true) { + setRenderHint(hint: RenderHint, on = true): void { this.native.setRenderHint(hint, on); } @@ -79,7 +81,7 @@ export class QPainter extends Component { this.native.restore(); } - setBrush(color: QColor) { + setBrush(color: QColor): void { this.native.setBrush(color.native); } } diff --git a/src/lib/QtWidgets/QTreeWidget.ts b/src/lib/QtWidgets/QTreeWidget.ts index 2fc6519a9..d1bdb43af 100644 --- a/src/lib/QtWidgets/QTreeWidget.ts +++ b/src/lib/QtWidgets/QTreeWidget.ts @@ -2,7 +2,8 @@ import addon from '../utils/addon'; import { NodeWidget } from './QWidget'; import { BaseWidgetEvents } from '../core/EventWidget'; import { NativeElement } from '../core/Component'; -import { QAbstractScrollArea, QTreeWidgetItem } from '../..'; +import { QAbstractScrollArea } from './QAbstractScrollArea'; +import { QTreeWidgetItem } from './QTreeWidgetItem'; export const QTreeWidgetEvents = Object.freeze({ ...BaseWidgetEvents, diff --git a/src/lib/QtWidgets/QTreeWidgetItem.ts b/src/lib/QtWidgets/QTreeWidgetItem.ts index 61c998660..9fc3bf337 100644 --- a/src/lib/QtWidgets/QTreeWidgetItem.ts +++ b/src/lib/QtWidgets/QTreeWidgetItem.ts @@ -1,6 +1,7 @@ import addon from '../utils/addon'; import { Component, NativeElement } from '../core/Component'; -import { checkIfNativeElement, QTreeWidget } from '../..'; +import { checkIfNativeElement } from '../utils/helpers'; +import { QTreeWidget } from './QTreeWidget'; export class QTreeWidgetItem extends Component { native: NativeElement;