Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make QPainter accept QImage in constructor #1040

Merged
merged 2 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/cpp/lib/QtWidgets/QPainter/qpainter_wrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,24 @@ QPainterWrap::~QPainterWrap() { delete this->instance; }
QPainterWrap::QPainterWrap(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<QPainterWrap>(info) {
Napi::Env env = info.Env();
if (info.Length() == 1) {
Napi::Object deviceObject = info[0].As<Napi::Object>();
NodeWidgetWrap* deviceWrap =
Napi::ObjectWrap<NodeWidgetWrap>::Unwrap(deviceObject);
this->instance = new QPainter(deviceWrap->getInternalInstance());
} else if (info.Length() == 0) {

if (info.Length() == 0) {
this->instance = new QPainter();
} else if (info.Length() == 2) {
Napi::String napiType = info[0].As<Napi::String>();
std::string type = napiType.Utf8Value();

if (type == "qimage") {
Napi::Object deviceObject = info[1].As<Napi::Object>();
QImageWrap* deviceWrap =
Napi::ObjectWrap<QImageWrap>::Unwrap(deviceObject);
this->instance = new QPainter(deviceWrap->getInternalInstance());
} else if (type == "qwidget") {
Napi::Object deviceObject = info[1].As<Napi::Object>();
NodeWidgetWrap* deviceWrap =
Napi::ObjectWrap<NodeWidgetWrap>::Unwrap(deviceObject);
this->instance = new QPainter(deviceWrap->getInternalInstance());
}
} else {
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
Expand Down
8 changes: 6 additions & 2 deletions src/lib/QtWidgets/QPainter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,14 @@ win.show();
https://github.com/nodegui/examples/blob/master/nodegui/custom-native-widget-qpainter
*/
export class QPainter extends Component {
constructor(device?: QWidget) {
constructor(device?: QWidget | QImage) {
let native: NativeElement;
if (device) {
native = new addon.QPainter(device.native);
if (device instanceof QWidget) {
native = new addon.QPainter('qwidget', device.native);
} else {
native = new addon.QPainter('qimage', device.native);
}
} else {
native = new addon.QPainter();
}
Expand Down