#include "QtGui/QKeySequence/qkeysequence_wrap.h" #include "Extras/Utils/nutils.h" #include "QtGui/QPixmap/qpixmap_wrap.h" Napi::FunctionReference QKeySequenceWrap::constructor; Napi::Object QKeySequenceWrap::init(Napi::Env env, Napi::Object exports) { Napi::HandleScope scope(env); char CLASSNAME[] = "QKeySequence"; Napi::Function func = DefineClass(env, CLASSNAME, {InstanceMethod("count", &QKeySequenceWrap::count), InstanceMethod("isEmpty", &QKeySequenceWrap::isEmpty), InstanceMethod("matches", &QKeySequenceWrap::matches), InstanceMethod("toString$", &QKeySequenceWrap::toString), StaticMethod("fromQVariant", &StaticQKeySequenceWrapMethods::fromQVariant), COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE(QKeySequenceWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); return exports; } QKeySequenceWrap::QKeySequenceWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); Napi::HandleScope scope(env); if (info.Length() == 1) { if (info[0].IsExternal()) { this->instance = std::unique_ptr( info[0].As>().Data()); } else { Napi::String sequenceString = info[0].As(); QString keySequence = QString::fromStdString(sequenceString.Utf8Value()); this->instance = std::make_unique(keySequence); } } else if (info.Length() == 0) { this->instance = std::make_unique(); } else { Napi::TypeError::New(env, "Wrong number of arguments") .ThrowAsJavaScriptException(); } this->rawData = extrautils::configureComponent(this->getInternalInstance()); } QKeySequenceWrap::~QKeySequenceWrap() { this->instance.reset(); } QKeySequence* QKeySequenceWrap::getInternalInstance() { return this->instance.get(); } Napi::Value QKeySequenceWrap::count(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); Napi::HandleScope scope(env); int count = this->instance->count(); return Napi::Value::From(env, count); } Napi::Value QKeySequenceWrap::isEmpty(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); Napi::HandleScope scope(env); return Napi::Value::From(env, this->instance->isEmpty()); } Napi::Value QKeySequenceWrap::matches(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); Napi::HandleScope scope(env); Napi::Object keyObject = info[0].As(); QKeySequenceWrap* keyWrap = Napi::ObjectWrap::Unwrap(keyObject); QKeySequence::SequenceMatch match = this->instance->matches(*keyWrap->getInternalInstance()); return Napi::Value::From(env, static_cast(match)); } Napi::Value QKeySequenceWrap::toString(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); Napi::HandleScope scope(env); int format = info[0].As().Int32Value(); QString result = this->instance->toString( static_cast(format)); return Napi::Value::From(env, result.toStdString()); } Napi::Value StaticQKeySequenceWrapMethods::fromQVariant( const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); Napi::HandleScope scope(env); Napi::Object variantObject = info[0].As(); QVariantWrap* variantWrap = Napi::ObjectWrap::Unwrap(variantObject); QVariant* variant = variantWrap->getInternalInstance(); QKeySequence key = variant->value(); auto instance = QKeySequenceWrap::constructor.New( {Napi::External::New(env, new QKeySequence(key))}); return instance; }