Skip to content

Commit

Permalink
Merge pull request #1040 from nodegui/qpainter_qimage
Browse files Browse the repository at this point in the history
Make `QPainter` accept `QImage` in constructor
  • Loading branch information
sedwards2009 committed Feb 24, 2024
2 parents 3c1b667 + 8b2bb2c commit c3d3b23
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
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

0 comments on commit c3d3b23

Please sign in to comment.