Cleanup and adds QColor, QPoint methods and test cases (#284)

* add more methods to qcolor, qpoint and add test cases and lint

* fix lint
This commit is contained in:
Atul R 2019-12-19 22:01:50 +05:30 committed by GitHub
parent 43d4ad7218
commit 4f41d3b12f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 307 additions and 37 deletions

View File

@ -1,7 +1,6 @@
#pragma once
#include <napi.h>
#include <stdlib.h>
#include <QColor>
@ -18,9 +17,17 @@ class QColorWrap : public Napi::ObjectWrap<QColorWrap> {
~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

View File

@ -1,7 +1,6 @@
#pragma once
#include <napi.h>
#include <stdlib.h>
#include <QPoint>
@ -18,7 +17,10 @@ class QPointWrap : public Napi::ObjectWrap<QPointWrap> {
~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
};

View File

@ -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<Napi::Number>().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<Napi::Number>().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<Napi::Number>().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<Napi::Number>().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<Napi::Object>();
QVariantWrap* variantWrap =
Napi::ObjectWrap<QVariantWrap>::Unwrap(variantObject);
QVariant* variant = variantWrap->getInternalInstance();
QColor color = variant->value<QColor>();
auto instance = QColorWrap::constructor.New(
{Napi::External<QColor>::New(env, new QColor(color))});
return instance;
}

View File

@ -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<Napi::Number>().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<Napi::Number>().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<Napi::Object>();
QVariantWrap* variantWrap =
Napi::ObjectWrap<QVariantWrap>::Unwrap(variantObject);
QVariant* variant = variantWrap->getInternalInstance();
QPoint point = variant->value<QPoint>();
auto instance = QPointWrap::constructor.New(
{Napi::External<QPoint>::New(env, new QPoint(point))});
return instance;
}

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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) {

View File

@ -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);
});
});

View File

@ -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());
});
});

View File

@ -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);

Binary file not shown.

Before

Width:  |  Height:  |  Size: 40 KiB

View File

@ -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);
}
}

View File

@ -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,

View File

@ -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;