#include "QtWidgets/QFileDialog/qfiledialog_wrap.h" #include #include "Extras/Utils/nutils.h" #include "QtWidgets/QWidget/qwidget_wrap.h" Napi::FunctionReference QFileDialogWrap::constructor; Napi::Object QFileDialogWrap::init(Napi::Env env, Napi::Object exports) { Napi::HandleScope scope(env); char CLASSNAME[] = "QFileDialog"; Napi::Function func = DefineClass( env, CLASSNAME, {InstanceMethod("supportedSchemes", &QFileDialogWrap::supportedSchemes), InstanceMethod("setSupportedSchemes", &QFileDialogWrap::setSupportedSchemes), InstanceMethod("labelText", &QFileDialogWrap::labelText), InstanceMethod("setLabelText", &QFileDialogWrap::setLabelText), InstanceMethod("setOption", &QFileDialogWrap::setOption), QDIALOG_WRAPPED_METHODS_EXPORT_DEFINE(QFileDialogWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); return exports; } NFileDialog* QFileDialogWrap::getInternalInstance() { return this->instance; } QFileDialogWrap::~QFileDialogWrap() { extrautils::safeDelete(this->instance); } QFileDialogWrap::QFileDialogWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); Napi::HandleScope scope(env); if (info.Length() == 4) { Napi::Object parentObject = info[0].As(); QWidgetWrap* parentWidgetWrap = Napi::ObjectWrap::Unwrap(parentObject); QWidget* parent = parentWidgetWrap->getInternalInstance(); QString caption = QString::fromUtf8(info[1].As().Utf8Value().c_str()); QString directory = QString::fromUtf8(info[2].As().Utf8Value().c_str()); QString filter = QString::fromUtf8(info[3].As().Utf8Value().c_str()); this->instance = new NFileDialog(parent, caption, directory, filter); } else if (info.Length() == 0) { this->instance = new NFileDialog(); } else { Napi::TypeError::New(env, "Wrong number of arguments") .ThrowAsJavaScriptException(); } this->rawData = extrautils::configureQWidget( this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), false); } Napi::Value QFileDialogWrap::supportedSchemes(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); Napi::HandleScope scope(env); QStringList schemes = this->instance->supportedSchemes(); Napi::Array schemesNapi = Napi::Array::New(env, schemes.size()); for (int i = 0; i < schemes.size(); i++) { schemesNapi[i] = Napi::String::New(env, schemes[i].toStdString()); } return schemesNapi; } Napi::Value QFileDialogWrap::setSupportedSchemes( const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); Napi::HandleScope scope(env); Napi::Array schemesNapi = info[0].As(); QList list; for (int i = 0; i < schemesNapi.Length(); i++) { Napi::Value schemeNapi = schemesNapi[i]; list.append(schemeNapi.As().Utf8Value().c_str()); } QStringList schemes = QStringList(list); this->instance->setSupportedSchemes(schemes); return env.Null(); } Napi::Value QFileDialogWrap::labelText(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); Napi::HandleScope scope(env); int labelInt = info[0].As().Int32Value(); QFileDialog::DialogLabel label = static_cast(labelInt); QString labelString = this->instance->labelText(label); return Napi::String::New(env, labelString.toStdString()); } Napi::Value QFileDialogWrap::setLabelText(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); Napi::HandleScope scope(env); int labelInt = info[0].As().Int32Value(); QFileDialog::DialogLabel label = static_cast(labelInt); std::string textString = info[1].As().Utf8Value(); QString text = QString::fromUtf8(textString.c_str()); this->instance->setLabelText(label, text); return env.Null(); } Napi::Value QFileDialogWrap::setOption(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); Napi::HandleScope scope(env); int optionInt = info[0].As().Int32Value(); QFileDialog::Option option = static_cast(optionInt); bool on = info[1].As().Value(); this->instance->setOption(option, on); return env.Null(); }