#include "QtWidgets/QLabel/qlabel_wrap.h" #include "QtWidgets/QWidget/qwidget_wrap.h" #include "QtGui/QPixmap/qpixmap_wrap.h" #include "Extras/Utils/nutils.h" #include Napi::FunctionReference QLabelWrap::constructor; Napi::Object QLabelWrap::init(Napi::Env env, Napi::Object exports) { Napi::HandleScope scope(env); char CLASSNAME[] = "QLabel"; Napi::Function func = DefineClass(env, CLASSNAME, { InstanceMethod("setWordWrap", &QLabelWrap::setWordWrap), InstanceMethod("wordWrap", &QLabelWrap::wordWrap), InstanceMethod("setText", &QLabelWrap::setText), InstanceMethod("text", &QLabelWrap::text), InstanceMethod("setPixmap", &QLabelWrap::setPixmap), QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QLabelWrap) }); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); return exports; } NLabel* QLabelWrap::getInternalInstance() { return this->instance.get(); } QLabelWrap::QLabelWrap(const Napi::CallbackInfo& info): Napi::ObjectWrap(info) { Napi::Env env = info.Env(); Napi::HandleScope scope(env); if(info.Length() == 1) { Napi::Object parentObject = info[0].As(); QWidgetWrap* parentWidgetWrap = Napi::ObjectWrap::Unwrap(parentObject); this->instance = std::make_unique(parentWidgetWrap->getInternalInstance()); //this sets the parent to current widget }else if (info.Length() == 0){ this->instance = std::make_unique(); }else { Napi::TypeError::New(env, "Wrong number of arguments").ThrowAsJavaScriptException(); } // Adds measure function on yoga node so that widget size is calculated based on its text also. YGNodeSetMeasureFunc(this->instance->getFlexNode(), &extrautils::measureQtWidget); } QLabelWrap::~QLabelWrap() { this->instance.reset(); } Napi::Value QLabelWrap::setWordWrap(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); Napi::HandleScope scope(env); Napi::Boolean On = info[0].As(); this->instance->setWordWrap(On); return env.Null(); } Napi::Value QLabelWrap::wordWrap(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::HandleScope scope(env); bool isWordWrap = this->instance->wordWrap(); return Napi::Value::From(env, isWordWrap); } Napi::Value QLabelWrap::setText(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); Napi::HandleScope scope(env); Napi::String text = info[0].As(); std::string label = text.Utf8Value(); this->instance->setText(label.c_str()); return env.Null(); } Napi::Value QLabelWrap::text(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::HandleScope scope(env); 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(); QPixmapWrap* pixmapWrap = Napi::ObjectWrap::Unwrap(pixmapObject); this->instance->setPixmap(*pixmapWrap->getInternalInstance()); return env.Null(); }