forked from nodegui/nodegui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqpixmap_wrap.cpp
More file actions
81 lines (74 loc) · 2.71 KB
/
qpixmap_wrap.cpp
File metadata and controls
81 lines (74 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "qpixmap_wrap.h"
#include "src/cpp/Extras/Utils/nutils.h"
#include "deps/spdlog/spdlog.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),
InstanceMethod("scaled",&QPixmapWrap::scaled),
COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE
});
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) {
if(info[0].IsExternal()){
this->instance = info[0].As<Napi::External<QPixmap>>().Data();
} else {
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 {
Napi::TypeError::New(env, "Wrong number of arguments").ThrowAsJavaScriptException();
}
}
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 {
Napi::TypeError::New(env, "Wrong number of arguments").ThrowAsJavaScriptException();
}
return Napi::Boolean::New(env, loadSuccess);
}
Napi::Value QPixmapWrap::scaled(const Napi::CallbackInfo& info)
{
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::Number widthValue = info[0].As<Napi::Number>();
Napi::Number heightValue = info[1].As<Napi::Number>();
int width = widthValue.Int32Value();
int height = heightValue.Int32Value();
Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio;
if(info.Length() > 2){
int aspectRatioModeInt = info[2].As<Napi::Number>().Int32Value();
aspectRatioMode = static_cast<Qt::AspectRatioMode>(aspectRatioModeInt);
}
QPixmap* updatedPixMap = new QPixmap(this->instance->scaled(width, height, aspectRatioMode));
auto instance = QPixmapWrap::constructor.New({ Napi::External<QPixmap>::New(env, updatedPixMap) });
return instance;
}