Add qvariant object translation (#185)

* working - translation of any wrapped object

* clang format
This commit is contained in:
Atul R 2019-11-10 12:30:26 +01:00 committed by GitHub
parent f8696508fa
commit 21e5e78876
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
44 changed files with 105 additions and 22 deletions

View File

@ -16,7 +16,7 @@
"license": "MIT",
"private": false,
"scripts": {
"dev": "npm run build && qode dist/demo.js",
"dev": "npm run build && qode --inspect dist/demo.js",
"postinstall": "npm run build:addon",
"build": "tsc && npm run build:addon",
"build:addon": "cross-env CMAKE_BUILD_PARALLEL_LEVEL=8 cmake-js build",

View File

@ -10,9 +10,13 @@
namespace extrautils {
YGSize measureQtWidget(YGNodeRef node, float width, YGMeasureMode widthMode,
float height, YGMeasureMode heightMode);
QVariant* convertToQVariant(Napi::Env& env, Napi::Value& value);
bool isNapiValueInt(Napi::Env& env, Napi::Value& num);
std::string getNapiObjectClassName(Napi::Object& object);
template <typename T>
void safeDelete(QPointer<T> component) {
if (component.isNull()) {

View File

@ -1,7 +1,6 @@
#pragma once
#include "Extras/Utils/nutils.h"
#include "core/Component/component_macro.h"
#include "core/Events/eventwidget_macro.h"
/*
@ -53,7 +52,6 @@
#define QOBJECT_WRAPPED_METHODS_EXPORT_DEFINE(ComponentWrapName) \
\
EVENTWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(ComponentWrapName) \
COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE \
\
InstanceMethod("inherits", &ComponentWrapName::inherits), \
InstanceMethod("setProperty", &ComponentWrapName::setProperty), \

View File

@ -4,6 +4,9 @@
#include <QApplication>
#include <QPointer>
#include "core/Component/component_macro.h"
class QApplicationWrap : public Napi::ObjectWrap<QApplicationWrap> {
private:
QPointer<QApplication> instance;
@ -24,6 +27,8 @@ class QApplicationWrap : public Napi::ObjectWrap<QApplicationWrap> {
Napi::Value exit(const Napi::CallbackInfo& info);
Napi::Value setQuitOnLastWindowClosed(const Napi::CallbackInfo& info);
Napi::Value quitOnLastWindowClosed(const Napi::CallbackInfo& info);
COMPONENT_WRAPPED_METHODS_DECLARATION
};
namespace StaticQApplicationWrapMethods {

View File

@ -19,4 +19,6 @@ class QClipboardWrap : public Napi::ObjectWrap<QClipboardWrap> {
Napi::Value clear(const Napi::CallbackInfo& info);
Napi::Value setText(const Napi::CallbackInfo& info);
Napi::Value text(const Napi::CallbackInfo& info);
COMPONENT_WRAPPED_METHODS_DECLARATION
};

View File

@ -20,4 +20,6 @@ class QCursorWrap : public Napi::ObjectWrap<QCursorWrap> {
// Wrapped methods
Napi::Value pos(const Napi::CallbackInfo& info);
Napi::Value setPos(const Napi::CallbackInfo& info);
COMPONENT_WRAPPED_METHODS_DECLARATION
};

View File

@ -5,6 +5,8 @@
#include <QKeyEvent>
#include "core/Component/component_macro.h"
class QKeyEventWrap : public Napi::ObjectWrap<QKeyEventWrap> {
private:
QKeyEvent* instance;
@ -18,5 +20,6 @@ class QKeyEventWrap : public Napi::ObjectWrap<QKeyEventWrap> {
static Napi::FunctionReference constructor;
// wrapped methods
Napi::Value text(const Napi::CallbackInfo& info);
// Napi::Value setFlexNode(const Napi::CallbackInfo& info);
COMPONENT_WRAPPED_METHODS_DECLARATION
};

View File

@ -21,4 +21,6 @@ class QIconWrap : public Napi::ObjectWrap<QIconWrap> {
Napi::Value pixmap(const Napi::CallbackInfo& info);
Napi::Value isMask(const Napi::CallbackInfo& info);
Napi::Value setIsMask(const Napi::CallbackInfo& info);
COMPONENT_WRAPPED_METHODS_DECLARATION
};

View File

@ -19,4 +19,6 @@ class QKeySequenceWrap : public Napi::ObjectWrap<QKeySequenceWrap> {
QKeySequence *getInternalInstance();
// Wrapped methods
Napi::Value count(const Napi::CallbackInfo &info);
COMPONENT_WRAPPED_METHODS_DECLARATION
};

View File

@ -21,4 +21,6 @@ class QPixmapWrap : public Napi::ObjectWrap<QPixmapWrap> {
Napi::Value load(const Napi::CallbackInfo& info);
Napi::Value save(const Napi::CallbackInfo& info);
Napi::Value scaled(const Napi::CallbackInfo& info);
COMPONENT_WRAPPED_METHODS_DECLARATION
};

View File

@ -2,7 +2,7 @@
#include <QSize>
#include "core/Component/component_macro.h"
#include "QtCore/QObject/qobject_macro.h"
/*
This macro adds common QLayout exported methods
@ -12,6 +12,7 @@
#ifndef QLAYOUT_WRAPPED_METHODS_DECLARATION
#define QLAYOUT_WRAPPED_METHODS_DECLARATION \
COMPONENT_WRAPPED_METHODS_DECLARATION \
\
Napi::Value activate(const Napi::CallbackInfo& info) { \
Napi::Env env = info.Env(); \

View File

@ -1,7 +1,14 @@
#pragma once
#ifndef COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE
#define COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE \
\
InstanceValue("type", Napi::String::New(env, "native")),
#endif
#endif // COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE
#ifndef COMPONENT_WRAPPED_METHODS_DECLARATION
#define COMPONENT_WRAPPED_METHODS_DECLARATION \
public: \
void* rawData = nullptr;
#endif

View File

@ -0,0 +1,16 @@
/*
This wrapper can be used to get the value of the actual instance inside a
wrapper for any component exported
as long as the component wrapper has this macro
COMPONENT_WRAPPED_METHODS_DECLARATION and components this->rawData has been
assigned in the constructor
*/
#pragma once
#include <napi.h>
#include "component_macro.h"
class ComponentWrap : public Napi::ObjectWrap<ComponentWrap> {
COMPONENT_WRAPPED_METHODS_DECLARATION
};

View File

@ -2,6 +2,7 @@
#include <QWidget>
#include "core/Component/component_macro.h"
#include "eventwidget.h"
/*
@ -13,7 +14,7 @@
#ifndef EVENTWIDGET_WRAPPED_METHODS_DECLARATION
#define EVENTWIDGET_WRAPPED_METHODS_DECLARATION \
\
COMPONENT_WRAPPED_METHODS_DECLARATION \
Napi::Value initNodeEventEmitter(const Napi::CallbackInfo& info) { \
Napi::Env env = info.Env(); \
this->instance->emitOnNode = \
@ -39,7 +40,7 @@
#ifndef EVENTWIDGET_WRAPPED_METHODS_EXPORT_DEFINE
#define EVENTWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(WidgetWrapName) \
\
COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE \
InstanceMethod("initNodeEventEmitter", \
&WidgetWrapName::initNodeEventEmitter), \
InstanceMethod("subscribeToQtEvent", \

View File

@ -1,9 +1,12 @@
#include "Extras/Utils/nutils.h"
#include <QDebug>
#include <QMetaType>
#include <QWidget>
#include <string>
#include "core/Component/component_wrap.h"
YGSize extrautils::measureQtWidget(YGNodeRef node, float width,
YGMeasureMode widthMode, float height,
YGMeasureMode heightMode) {
@ -37,6 +40,14 @@ bool extrautils::isNapiValueInt(Napi::Env& env, Napi::Value& num) {
.Value();
}
std::string extrautils::getNapiObjectClassName(Napi::Object& object) {
return object.Get("constructor")
.As<Napi::Object>()
.Get("name")
.As<Napi::String>()
.Utf8Value();
}
QVariant* extrautils::convertToQVariant(Napi::Env& env, Napi::Value& value) {
// Warning: Make sure you delete the QVariant fron this function upon use.
if (value.IsBoolean()) {
@ -62,8 +73,12 @@ QVariant* extrautils::convertToQVariant(Napi::Env& env, Napi::Value& value) {
// TODO: fix this
return new QVariant();
} else if (value.IsObject()) {
// TODO: fix this
return new QVariant();
Napi::Object object = value.As<Napi::Object>();
std::string className = getNapiObjectClassName(object);
int typeId = QMetaType::type(className.c_str());
ComponentWrap* componentWrap =
Napi::ObjectWrap<ComponentWrap>::Unwrap(object);
return new QVariant(typeId, componentWrap->rawData);
} else if (value.IsFunction()) {
return new QVariant();
} else if (value.IsPromise()) {

View File

@ -37,4 +37,5 @@ QObjectWrap::QObjectWrap(const Napi::CallbackInfo& info)
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = this->getInternalInstance();
}

View File

@ -2,7 +2,6 @@
#include "Extras/Utils/nutils.h"
#include "QtGui/QClipboard/qclipboard_wrap.h"
#include "core/Component/component_macro.h"
Napi::FunctionReference QApplicationWrap::constructor;
int QApplicationWrap::argc = 0;
@ -42,6 +41,7 @@ QApplicationWrap::QApplicationWrap(const Napi::CallbackInfo& info)
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = this->getInternalInstance();
}
QApplicationWrap::~QApplicationWrap() {
if (this->_wasManuallyCreated) {

View File

@ -29,6 +29,7 @@ QClipboardWrap::QClipboardWrap(const Napi::CallbackInfo& info)
Napi::TypeError::New(env, "Incorrect initialization of QClipboardWrap")
.ThrowAsJavaScriptException();
}
this->rawData = this->getInternalInstance();
}
QClipboard* QClipboardWrap::getInternalInstance() { return this->instance; }

View File

@ -33,6 +33,7 @@ QCursorWrap::QCursorWrap(const Napi::CallbackInfo& info)
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = this->getInternalInstance();
}
QCursorWrap::~QCursorWrap() { this->instance.reset(); }

View File

@ -3,8 +3,6 @@
#include <QString>
#include "Extras/Utils/nutils.h"
#include "core/Component/component_macro.h"
#include "deps/spdlog/spdlog.h"
Napi::FunctionReference QKeyEventWrap::constructor;
@ -34,6 +32,7 @@ QKeyEventWrap::QKeyEventWrap(const Napi::CallbackInfo& info)
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = this->getInternalInstance();
}
QKeyEventWrap::~QKeyEventWrap() {

View File

@ -34,6 +34,7 @@ QIconWrap::QIconWrap(const Napi::CallbackInfo& info)
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = this->getInternalInstance();
}
QIconWrap::~QIconWrap() { this->instance.reset(); }

View File

@ -32,6 +32,7 @@ QKeySequenceWrap::QKeySequenceWrap(const Napi::CallbackInfo &info)
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = this->getInternalInstance();
}
QKeySequenceWrap::~QKeySequenceWrap() { this->instance.reset(); }

View File

@ -38,6 +38,7 @@ QPixmapWrap::QPixmapWrap(const Napi::CallbackInfo& info)
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = this->getInternalInstance();
}
QPixmapWrap::~QPixmapWrap() { this->instance.reset(); }

View File

@ -26,8 +26,7 @@ Napi::Object QActionWrap::init(Napi::Env env, Napi::Object exports) {
InstanceMethod("setChecked", &QActionWrap::setChecked),
InstanceMethod("isSeparator", &QActionWrap::isSeparator),
InstanceMethod("setSeparator", &QActionWrap::setSeparator),
COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE
EVENTWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QActionWrap)});
EVENTWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QActionWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
return exports;
@ -51,6 +50,7 @@ QActionWrap::QActionWrap(const Napi::CallbackInfo& info)
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = this->getInternalInstance();
}
QActionWrap::~QActionWrap() { extrautils::safeDelete(this->instance); }

View File

@ -39,6 +39,7 @@ QCheckBoxWrap::QCheckBoxWrap(const Napi::CallbackInfo& info)
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = this->getInternalInstance();
// Adds measure function on yoga node so that widget size is calculated based
// on its text also.
YGNodeSetMeasureFunc(this->instance->getFlexNode(),

View File

@ -43,6 +43,7 @@ QDialWrap::QDialWrap(const Napi::CallbackInfo& info)
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = this->getInternalInstance();
// Adds measure function on yoga node so that widget size is calculated based
// on its own size.
YGNodeSetMeasureFunc(this->instance->getFlexNode(),

View File

@ -37,6 +37,7 @@ QGridLayoutWrap::QGridLayoutWrap(const Napi::CallbackInfo& info)
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = this->getInternalInstance();
}
Napi::Value QGridLayoutWrap::addWidget(const Napi::CallbackInfo& info) {

View File

@ -45,6 +45,7 @@ QLabelWrap::QLabelWrap(const Napi::CallbackInfo& info)
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = this->getInternalInstance();
// Adds measure function on yoga node so that widget size is calculated based
// on its text also.
YGNodeSetMeasureFunc(this->instance->getFlexNode(),

View File

@ -42,6 +42,7 @@ QLineEditWrap::QLineEditWrap(const Napi::CallbackInfo& info)
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = this->getInternalInstance();
// Adds measure function on yoga node so that widget size is calculated based
// on its text also.
YGNodeSetMeasureFunc(this->instance->getFlexNode(),

View File

@ -46,6 +46,7 @@ QMainWindowWrap::QMainWindowWrap(const Napi::CallbackInfo& info)
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = this->getInternalInstance();
}
Napi::Value QMainWindowWrap::setCentralWidget(const Napi::CallbackInfo& info) {

View File

@ -40,6 +40,7 @@ QMenuWrap::QMenuWrap(const Napi::CallbackInfo& info)
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = this->getInternalInstance();
// Adds measure function on yoga node so that widget size is calculated based
// on its text also.
YGNodeSetMeasureFunc(this->instance->getFlexNode(),

View File

@ -45,6 +45,7 @@ QMenuBarWrap::QMenuBarWrap(const Napi::CallbackInfo& info)
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = this->getInternalInstance();
// Adds measure function on yoga node so that widget size is calculated based
// on its text also.
YGNodeSetMeasureFunc(this->instance->getFlexNode(),

View File

@ -52,6 +52,7 @@ QPlainTextEditWrap::QPlainTextEditWrap(const Napi::CallbackInfo &info)
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = this->getInternalInstance();
// Adds measure function on yoga node so that widget size is calculated based
// on its text also.
YGNodeSetMeasureFunc(this->instance->getFlexNode(),

View File

@ -42,6 +42,7 @@ QProgressBarWrap::QProgressBarWrap(const Napi::CallbackInfo& info)
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = this->getInternalInstance();
// Adds measure function on yoga node so that widget size is calculated based
// on its own size.
YGNodeSetMeasureFunc(this->instance->getFlexNode(),

View File

@ -38,6 +38,7 @@ QRadioButtonWrap::QRadioButtonWrap(const Napi::CallbackInfo& info)
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = this->getInternalInstance();
// Adds measure function on yoga node so that widget size is calculated based
// on its own size.
YGNodeSetMeasureFunc(this->instance->getFlexNode(),

View File

@ -40,7 +40,7 @@ QScrollAreaWrap::QScrollAreaWrap(const Napi::CallbackInfo& info)
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->instance->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
this->rawData = this->getInternalInstance();
// Adds measure function on yoga node so that widget size is calculated based
// on its own size.
YGNodeSetMeasureFunc(this->instance->getFlexNode(),

View File

@ -18,8 +18,7 @@ Napi::Object QShortcutWrap::init(Napi::Env env, Napi::Object exports) {
InstanceMethod("setAutoRepeat", &QShortcutWrap::setAutoRepeat),
InstanceMethod("setKey", &QShortcutWrap::setKey),
InstanceMethod("setContext", &QShortcutWrap::setContext),
COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE
EVENTWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QShortcutWrap)});
EVENTWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QShortcutWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
return exports;
@ -41,6 +40,7 @@ QShortcutWrap::QShortcutWrap(const Napi::CallbackInfo& info)
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = this->getInternalInstance();
}
QShortcutWrap::~QShortcutWrap() { extrautils::safeDelete(this->instance); }

View File

@ -45,6 +45,7 @@ QSpinBoxWrap::QSpinBoxWrap(const Napi::CallbackInfo& info)
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = this->getInternalInstance();
// Adds measure function on yoga node so that widget size is calculated based
// on its text also.
YGNodeSetMeasureFunc(this->instance->getFlexNode(),

View File

@ -20,9 +20,7 @@ Napi::Object QSystemTrayIconWrap::init(Napi::Env env, Napi::Object exports) {
InstanceMethod("isVisible", &QSystemTrayIconWrap::isVisible),
InstanceMethod("setToolTip", &QSystemTrayIconWrap::setToolTip),
InstanceMethod("setContextMenu", &QSystemTrayIconWrap::setContextMenu),
COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE
EVENTWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QSystemTrayIconWrap)});
EVENTWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QSystemTrayIconWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
return exports;
@ -50,6 +48,7 @@ QSystemTrayIconWrap::QSystemTrayIconWrap(const Napi::CallbackInfo& info)
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = this->getInternalInstance();
}
QSystemTrayIconWrap::~QSystemTrayIconWrap() {

View File

@ -47,6 +47,7 @@ QTabWidgetWrap::QTabWidgetWrap(const Napi::CallbackInfo& info)
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = this->getInternalInstance();
// Adds measure function on yoga node so that widget size is calculated based
// on its text also.
YGNodeSetMeasureFunc(this->instance->getFlexNode(),

View File

@ -39,4 +39,5 @@ QWidgetWrap::QWidgetWrap(const Napi::CallbackInfo& info)
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = this->getInternalInstance();
}

View File

@ -41,6 +41,7 @@ FlexLayoutWrap::FlexLayoutWrap(const Napi::CallbackInfo& info)
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = this->getInternalInstance();
}
Napi::Value FlexLayoutWrap::addWidget(const Napi::CallbackInfo& info) {

View File

@ -3,6 +3,7 @@ import { QWidget } from './lib/QtWidgets/QWidget';
import { FlexLayout } from './lib/core/FlexLayout';
import { QLabel } from './lib/QtWidgets/QLabel';
import { AlignmentFlag } from './lib/QtEnums';
import { QPixmap } from './lib/QtGui/QPixmap';
const win = new QMainWindow();
const view = new QWidget();
@ -23,6 +24,8 @@ world.setStyleSheet(`
border: 1px solid blue;
qproperty-alignment: AlignCenter;
`);
const pixmap = new QPixmap('/Users/atulr/Project/nodegui/nodegui/extras/assets/kitchen.png');
hello.setProperty('pixmap', pixmap);
hello.setProperty('alignment', AlignmentFlag.AlignCenter);

View File

@ -8,7 +8,8 @@ export abstract class NodeObject extends EventWidget {
return this.native.inherits(className);
}
setProperty(name: string, value: any): boolean {
return this.native.setProperty(name, value);
const finalValue = value.native || value;
return this.native.setProperty(name, finalValue);
}
setObjectName(objectName: string): void {
this.native.setObjectName(objectName);