Merge pull request #98 from balthild/feature/icon-to-pixmap

Converting QIcon ot QPixmap
This commit is contained in:
Atul R 2019-09-20 23:04:18 +02:00 committed by GitHub
commit a2029da351
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 0 deletions

View File

@ -1,4 +1,5 @@
#include "qicon_wrap.h"
#include "src/cpp/QtGui/QPixmap/qpixmap_wrap.h"
#include "src/cpp/Extras/Utils/nutils.h"
#include "deps/spdlog/spdlog.h"
@ -9,6 +10,7 @@ Napi::Object QIconWrap::init(Napi::Env env, Napi::Object exports)
Napi::HandleScope scope(env);
char CLASSNAME[] = "QIcon";
Napi::Function func = DefineClass(env, CLASSNAME, {
InstanceMethod("pixmap", &QIconWrap::pixmap),
COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE
});
constructor = Napi::Persistent(func);
@ -45,3 +47,30 @@ QIcon *QIconWrap::getInternalInstance()
{
return this->instance.get();
}
Napi::Value QIconWrap::pixmap(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();
QIcon::Mode mode = QIcon::Normal;
if (info.Length() > 2) {
int modeInt = info[2].As<Napi::Number>().Int32Value();
mode = static_cast<QIcon::Mode>(modeInt);
}
QIcon::State state = QIcon::Off;
if (info.Length() > 3) {
int stateInt = info[3].As<Napi::Number>().Int32Value();
state = static_cast<QIcon::State>(stateInt);
}
QPixmap* pixmap = new QPixmap(this->instance->pixmap(width, height, mode, state));
auto instance = QPixmapWrap::constructor.New({ Napi::External<QPixmap>::New(env, pixmap) });
return instance;
}

View File

@ -17,4 +17,5 @@ public:
~QIconWrap();
QIcon *getInternalInstance();
// Wrapped methods
Napi::Value pixmap(const Napi::CallbackInfo& info);
};

View File

@ -1,5 +1,9 @@
import addon from "../../core/addon";
import { Component, NativeElement } from "../../core/Component";
import { QPixmap } from "../../QtGui/QPixmap";
export enum QIconMode { Normal, Disabled, Active, Selected }
export enum QIconState { Off, On }
type arg = string | NativeElement;
export class QIcon extends Component {
@ -13,4 +17,20 @@ export class QIcon extends Component {
this.native = new addon.QIcon();
}
}
pixmap = (
width: number,
height: number,
mode?: QIconMode,
state?: QIconState,
): QPixmap => {
let nativePixmap;
if (mode && state) {
nativePixmap = this.native.pixmap(width, height, mode, state);
} else if (mode) {
nativePixmap = this.native.pixmap(width, height, mode);
} else {
nativePixmap = this.native.pixmap(width, height);
}
return new QPixmap(nativePixmap);
};
}