Added Image rendering support via qlabel and qpixmap
This commit is contained in:
@@ -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
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
}
|
||||
@@ -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,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";
|
||||
|
||||
Reference in New Issue
Block a user