Added Image rendering support via qlabel and qpixmap

This commit is contained in:
Atul R
2019-08-03 23:35:11 +02:00
parent e50666c566
commit e6eccd06a9
14 changed files with 167 additions and 6 deletions
@@ -1,5 +1,4 @@
#ifndef QAPPLICATION_WRAP_H
#define QAPPLICATION_WRAP_H
#pragma once
#include <napi.h>
#include <QApplication>
@@ -22,4 +21,3 @@ public:
Napi::Value exec(const Napi::CallbackInfo& info);
};
#endif
+57
View File
@@ -0,0 +1,57 @@
#include "qpixmap_wrap.h"
#include "src/cpp/Extras/Utils/nutils.h"
Napi::FunctionReference QPixmapWrap::constructor;
Napi::Object QPixmapWrap::init(Napi::Env env, Napi::Object exports)
{
Napi::HandleScope scope(env);
char CLASSNAME[] = "QPixmap";
Napi::Function func = DefineClass(env, CLASSNAME,{
InstanceMethod("load", &QPixmapWrap::load),
});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
return exports;
}
QPixmapWrap::QPixmapWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap<QPixmapWrap>(info)
{
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if(info.Length() == 1) {
Napi::String url = info[0].As<Napi::String>();
QString imageUrl = QString::fromUtf8(url.Utf8Value().c_str());
this->instance = new QPixmap(imageUrl);
}else if (info.Length() == 0){
this->instance = new QPixmap();
}else {
extrautils::throwTypeError(env, "Wrong number of arguments");
}
}
QPixmapWrap::~QPixmapWrap()
{
delete this->instance;
}
QPixmap* QPixmapWrap::getInternalInstance()
{
return this->instance;
}
Napi::Value QPixmapWrap::load(const Napi::CallbackInfo& info)
{
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
bool loadSuccess = false;
if(info.Length() == 1) {
Napi::String url = info[0].As<Napi::String>();
QString imageUrl = QString::fromUtf8(url.Utf8Value().c_str());
loadSuccess = this->instance->load(imageUrl);
}else {
extrautils::throwTypeError(env, "Wrong number of arguments");
}
return Napi::Boolean::New(env, loadSuccess);
}
+18
View File
@@ -0,0 +1,18 @@
#pragma once
#include <napi.h>
#include <QPixmap>
class QPixmapWrap : public Napi::ObjectWrap<QPixmapWrap> {
private:
QPixmap* instance;
public:
static Napi::FunctionReference constructor;
static Napi::Object init(Napi::Env env, Napi::Object exports);
QPixmapWrap(const Napi::CallbackInfo& info);
~QPixmapWrap();
QPixmap* getInternalInstance();
// Wrapped methods
Napi::Value load(const Napi::CallbackInfo& info);
};
+13 -1
View File
@@ -1,5 +1,6 @@
#include "qlabel_wrap.h"
#include "src/cpp/QtGui/QWidget/qwidget_wrap.h"
#include "src/cpp/QtGui/QPixmap/qpixmap_wrap.h"
#include "src/cpp/Extras/Utils/nutils.h"
#include <QWidget>
@@ -12,7 +13,7 @@ Napi::Object QLabelWrap::init(Napi::Env env, Napi::Object exports) {
InstanceMethod("setWordWrap", &QLabelWrap::setWordWrap),
InstanceMethod("setText", &QLabelWrap::setText),
InstanceMethod("text", &QLabelWrap::text),
InstanceMethod("getFlexNode", &QLabelWrap::getFlexNode),
InstanceMethod("setPixmap", &QLabelWrap::setPixmap),
QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QLabelWrap)
});
constructor = Napi::Persistent(func);
@@ -74,3 +75,14 @@ Napi::Value QLabelWrap::text(const Napi::CallbackInfo &info)
std::string labelText = this->instance->text().toStdString();
return Napi::String::New(env, labelText);
}
Napi::Value QLabelWrap::setPixmap(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::Object pixmapObject = info[0].As<Napi::Object>();
QPixmapWrap* pixmapWrap = Napi::ObjectWrap<QPixmapWrap>::Unwrap(pixmapObject);
this->instance->setPixmap(*pixmapWrap->getInternalInstance());
return env.Null();
}
+1
View File
@@ -19,6 +19,7 @@ class QLabelWrap : public Napi::ObjectWrap<QLabelWrap>{
Napi::Value setWordWrap(const Napi::CallbackInfo& info);
Napi::Value setText(const Napi::CallbackInfo& info);
Napi::Value text(const Napi::CallbackInfo &info);
Napi::Value setPixmap(const Napi::CallbackInfo &info);
QWIDGET_WRAPPED_METHODS_DECLARATION
+2
View File
@@ -1,5 +1,6 @@
#include "src/cpp/QtGui/QApplication/qapplication_wrap.h"
#include "src/cpp/QtGui/QWidget/qwidget_wrap.h"
#include "src/cpp/QtGui/QPixmap/qpixmap_wrap.h"
#include "src/cpp/QtWidgets/QGridLayout/qgridlayout_wrap.h"
#include "src/cpp/QtWidgets/QLayout/qlayout_wrap.h"
#include "src/cpp/QtWidgets/QLabel/qlabel_wrap.h"
@@ -21,6 +22,7 @@ Napi::Object Main(Napi::Env env, Napi::Object exports) {
InitPrivateHelpers(env);
QApplicationWrap::init(env, exports);
QWidgetWrap::init(env, exports);
QPixmapWrap::init(env, exports);
QGridLayoutWrap::init(env, exports);
FlexLayoutWrap::init(env, exports);
QMainWindowWrap::init(env,exports);
+17
View File
@@ -0,0 +1,17 @@
import addon from "../../core/addon";
import { Component, NativeElement } from "../../core/Component";
export class QPixmap extends Component {
native: NativeElement;
constructor(imageUrl?: string) {
super();
if (imageUrl) {
this.native = new addon.QPixmap(imageUrl);
} else {
this.native = new addon.QPixmap();
}
}
load = (imageUrl: string) => {
return this.native.load(imageUrl);
};
}
+4
View File
@@ -2,6 +2,7 @@ import addon from "../../core/addon";
import { NodeWidget } from "../../QtGui/QWidget";
import { BaseWidgetEvents } from "../../core/EventWidget";
import { NativeElement } from "../../core/Component";
import { QPixmap } from "../../QtGui/QPixmap";
export const QLabelEvents = Object.freeze({
...BaseWidgetEvents
@@ -32,4 +33,7 @@ export class QLabel extends NodeWidget {
text() {
return this.native.text();
}
setPixmap(pixMap: QPixmap) {
this.native.setPixmap(pixMap.native);
}
}
+1
View File
@@ -1,5 +1,6 @@
export { QApplication } from "./QtGui/QApplication";
export { QWidget, QWidgetEvents } from "./QtGui/QWidget";
export { QPixmap } from "./QtGui/QPixmap";
// Abstract:
export { NodeWidget } from "./QtGui/QWidget";
export { NodeLayout } from "./QtWidgets/QLayout";