From d715a4299df1262a61ee48a4c2eb954b10c6ff7f Mon Sep 17 00:00:00 2001 From: Simon Edwards Date: Sat, 31 Dec 2022 13:53:30 +0100 Subject: [PATCH] Add floating point versions of `QPainter` methods --- .../QtWidgets/QPainter/qpainter_wrap.h | 62 +++-- src/cpp/lib/QtGui/QImage/qimage_wrap.cpp | 2 +- .../lib/QtWidgets/QPainter/qpainter_wrap.cpp | 248 +++++++++++++++--- src/lib/QtWidgets/QPainter.ts | 82 +++++- 4 files changed, 334 insertions(+), 60 deletions(-) diff --git a/src/cpp/include/nodegui/QtWidgets/QPainter/qpainter_wrap.h b/src/cpp/include/nodegui/QtWidgets/QPainter/qpainter_wrap.h index 3ba6cf478..1a1c2f586 100644 --- a/src/cpp/include/nodegui/QtWidgets/QPainter/qpainter_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QPainter/qpainter_wrap.h @@ -20,38 +20,52 @@ class DLL_EXPORT QPainterWrap : public Napi::ObjectWrap { // class constructor static Napi::FunctionReference constructor; // wrapped methods - Napi::Value drawArc(const Napi::CallbackInfo& info); - Napi::Value drawText(const Napi::CallbackInfo& info); - Napi::Value drawImage(const Napi::CallbackInfo& info); - Napi::Value drawPath(const Napi::CallbackInfo& info); - Napi::Value strokePath(const Napi::CallbackInfo& info); Napi::Value begin(const Napi::CallbackInfo& info); Napi::Value beginNativePainting(const Napi::CallbackInfo& info); + Napi::Value boundingRect(const Napi::CallbackInfo& info); + Napi::Value boundingRectF(const Napi::CallbackInfo& info); + Napi::Value compositionMode(const Napi::CallbackInfo& info); + Napi::Value drawArc(const Napi::CallbackInfo& info); + Napi::Value drawArcF(const Napi::CallbackInfo& info); + Napi::Value drawChord(const Napi::CallbackInfo& info); + Napi::Value drawChordF(const Napi::CallbackInfo& info); + Napi::Value drawConvexPolygon(const Napi::CallbackInfo& info); + Napi::Value drawConvexPolygonF(const Napi::CallbackInfo& info); + Napi::Value drawEllipse(const Napi::CallbackInfo& info); + Napi::Value drawEllipseF(const Napi::CallbackInfo& info); + Napi::Value drawImage(const Napi::CallbackInfo& info); + Napi::Value drawImageF(const Napi::CallbackInfo& info); + Napi::Value drawLine(const Napi::CallbackInfo& info); + Napi::Value drawLineF(const Napi::CallbackInfo& info); + Napi::Value drawPath(const Napi::CallbackInfo& info); + Napi::Value drawPie(const Napi::CallbackInfo& info); + Napi::Value drawPieF(const Napi::CallbackInfo& info); + Napi::Value drawPoint(const Napi::CallbackInfo& info); + Napi::Value drawPointF(const Napi::CallbackInfo& info); + Napi::Value drawRect(const Napi::CallbackInfo& info); + Napi::Value drawRectF(const Napi::CallbackInfo& info); + Napi::Value drawText(const Napi::CallbackInfo& info); + Napi::Value drawTextF(const Napi::CallbackInfo& info); Napi::Value end(const Napi::CallbackInfo& info); Napi::Value endNativePainting(const Napi::CallbackInfo& info); + Napi::Value eraseRect(const Napi::CallbackInfo& info); + Napi::Value eraseRectF(const Napi::CallbackInfo& info); + Napi::Value fillRect(const Napi::CallbackInfo& info); + Napi::Value fillRectF(const Napi::CallbackInfo& info); + Napi::Value opacity(const Napi::CallbackInfo& info); + Napi::Value restore(const Napi::CallbackInfo& info); Napi::Value rotate(const Napi::CallbackInfo& info); + Napi::Value save(const Napi::CallbackInfo& info); + Napi::Value scale(const Napi::CallbackInfo& info); + Napi::Value setBrush(const Napi::CallbackInfo& info); + Napi::Value setBrushOrigin(const Napi::CallbackInfo& info); + Napi::Value setBrushOriginF(const Napi::CallbackInfo& info); + Napi::Value setCompositionMode(const Napi::CallbackInfo& info); Napi::Value setFont(const Napi::CallbackInfo& info); + Napi::Value setOpacity(const Napi::CallbackInfo& info); Napi::Value setPen(const Napi::CallbackInfo& info); Napi::Value setRenderHint(const Napi::CallbackInfo& info); Napi::Value setTransform(const Napi::CallbackInfo& info); - Napi::Value setBrush(const Napi::CallbackInfo& info); - Napi::Value drawLine(const Napi::CallbackInfo& info); - Napi::Value drawEllipse(const Napi::CallbackInfo& info); - Napi::Value drawPie(const Napi::CallbackInfo& info); - Napi::Value scale(const Napi::CallbackInfo& info); + Napi::Value strokePath(const Napi::CallbackInfo& info); Napi::Value translate(const Napi::CallbackInfo& info); - Napi::Value drawConvexPolygon(const Napi::CallbackInfo& info); - Napi::Value save(const Napi::CallbackInfo& info); - Napi::Value restore(const Napi::CallbackInfo& info); - Napi::Value fillRect(const Napi::CallbackInfo& info); - Napi::Value compositionMode(const Napi::CallbackInfo& info); - Napi::Value setCompositionMode(const Napi::CallbackInfo& info); - Napi::Value opacity(const Napi::CallbackInfo& info); - Napi::Value setOpacity(const Napi::CallbackInfo& info); - Napi::Value drawPoint(const Napi::CallbackInfo& info); - Napi::Value drawRect(const Napi::CallbackInfo& info); - Napi::Value eraseRect(const Napi::CallbackInfo& info); - Napi::Value boundingRect(const Napi::CallbackInfo& info); - Napi::Value drawChord(const Napi::CallbackInfo& info); - Napi::Value setBrushOrigin(const Napi::CallbackInfo& info); }; diff --git a/src/cpp/lib/QtGui/QImage/qimage_wrap.cpp b/src/cpp/lib/QtGui/QImage/qimage_wrap.cpp index ae277e3f1..1db82e93c 100644 --- a/src/cpp/lib/QtGui/QImage/qimage_wrap.cpp +++ b/src/cpp/lib/QtGui/QImage/qimage_wrap.cpp @@ -453,7 +453,7 @@ void QImageWrap::setColorCount(const Napi::CallbackInfo& info) { void QImageWrap::setDevicePixelRatio(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); - int64_t scaleFactor = info[0].As(); + qreal scaleFactor = info[0].As(); this->instance->setDevicePixelRatio(scaleFactor); } diff --git a/src/cpp/lib/QtWidgets/QPainter/qpainter_wrap.cpp b/src/cpp/lib/QtWidgets/QPainter/qpainter_wrap.cpp index 75f1d0d44..d9fa34d4e 100644 --- a/src/cpp/lib/QtWidgets/QPainter/qpainter_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QPainter/qpainter_wrap.cpp @@ -2,7 +2,9 @@ #include "Extras/Utils/nutils.h" #include "QtCore/QPoint/qpoint_wrap.h" +#include "QtCore/QPointF/qpointf_wrap.h" #include "QtCore/QRect/qrect_wrap.h" +#include "QtCore/QRectF/qrectf_wrap.h" #include "QtGui/QBrush/qbrush_wrap.h" #include "QtGui/QColor/qcolor_wrap.h" #include "QtGui/QFont/qfont_wrap.h" @@ -19,42 +21,55 @@ Napi::Object QPainterWrap::init(Napi::Env env, Napi::Object exports) { char CLASSNAME[] = "QPainter"; Napi::Function func = DefineClass( env, CLASSNAME, - {InstanceMethod("drawArc", &QPainterWrap::drawArc), - InstanceMethod("drawText", &QPainterWrap::drawText), - InstanceMethod("drawPath", &QPainterWrap::drawPath), - InstanceMethod("drawPie", &QPainterWrap::drawPie), - InstanceMethod("drawEllipse", &QPainterWrap::drawEllipse), - InstanceMethod("drawImage", &QPainterWrap::drawImage), - InstanceMethod("strokePath", &QPainterWrap::strokePath), - InstanceMethod("begin", &QPainterWrap::begin), - InstanceMethod("end", &QPainterWrap::end), - InstanceMethod("rotate", &QPainterWrap::rotate), - InstanceMethod("setFont", &QPainterWrap::setFont), - InstanceMethod("setPen", &QPainterWrap::setPen), - InstanceMethod("setBrush", &QPainterWrap::setBrush), - InstanceMethod("setTransform", &QPainterWrap::setTransform), - InstanceMethod("drawLine", &QPainterWrap::drawLine), - InstanceMethod("scale", &QPainterWrap::scale), - InstanceMethod("translate", &QPainterWrap::translate), - InstanceMethod("setRenderHint", &QPainterWrap::setRenderHint), - InstanceMethod("drawConvexPolygon", &QPainterWrap::drawConvexPolygon), - InstanceMethod("save", &QPainterWrap::save), - InstanceMethod("restore", &QPainterWrap::restore), + {InstanceMethod("begin", &QPainterWrap::begin), InstanceMethod("beginNativePainting", &QPainterWrap::beginNativePainting), - InstanceMethod("endNativePainting", &QPainterWrap::endNativePainting), - InstanceMethod("fillRect", &QPainterWrap::fillRect), - InstanceMethod("compositionMode", &QPainterWrap::compositionMode), - InstanceMethod("setCompositionMode", &QPainterWrap::setCompositionMode), - InstanceMethod("opacity", &QPainterWrap::opacity), - InstanceMethod("setOpacity", &QPainterWrap::setOpacity), - InstanceMethod("drawPoint", &QPainterWrap::drawPoint), - InstanceMethod("drawRect", &QPainterWrap::drawRect), - InstanceMethod("eraseRect", &QPainterWrap::eraseRect), InstanceMethod("boundingRect", &QPainterWrap::boundingRect), + InstanceMethod("boundingRectF", &QPainterWrap::boundingRectF), + InstanceMethod("compositionMode", &QPainterWrap::compositionMode), + InstanceMethod("drawArc", &QPainterWrap::drawArc), + InstanceMethod("drawArcF", &QPainterWrap::drawArcF), InstanceMethod("drawChord", &QPainterWrap::drawChord), + InstanceMethod("drawChordF", &QPainterWrap::drawChord), + InstanceMethod("drawConvexPolygon", &QPainterWrap::drawConvexPolygon), + InstanceMethod("drawConvexPolygonF", &QPainterWrap::drawConvexPolygonF), + InstanceMethod("drawEllipse", &QPainterWrap::drawEllipse), + InstanceMethod("drawEllipseF", &QPainterWrap::drawEllipseF), + InstanceMethod("drawImage", &QPainterWrap::drawImage), + InstanceMethod("drawImageF", &QPainterWrap::drawImageF), + InstanceMethod("drawLine", &QPainterWrap::drawLine), + InstanceMethod("drawLineF", &QPainterWrap::drawLineF), + InstanceMethod("drawPath", &QPainterWrap::drawPath), InstanceMethod("drawPie", &QPainterWrap::drawPie), + InstanceMethod("drawPieF", &QPainterWrap::drawPieF), + InstanceMethod("drawPoint", &QPainterWrap::drawPoint), + InstanceMethod("drawPointF", &QPainterWrap::drawPointF), + InstanceMethod("drawRect", &QPainterWrap::drawRect), + InstanceMethod("drawRectF", &QPainterWrap::drawRectF), + InstanceMethod("drawText", &QPainterWrap::drawText), + InstanceMethod("drawTextF", &QPainterWrap::drawTextF), + InstanceMethod("end", &QPainterWrap::end), + InstanceMethod("endNativePainting", &QPainterWrap::endNativePainting), + InstanceMethod("eraseRect", &QPainterWrap::eraseRect), + InstanceMethod("eraseRectF", &QPainterWrap::eraseRectF), + InstanceMethod("fillRect", &QPainterWrap::fillRect), + InstanceMethod("fillRectF", &QPainterWrap::fillRectF), + InstanceMethod("opacity", &QPainterWrap::opacity), + InstanceMethod("restore", &QPainterWrap::restore), + InstanceMethod("rotate", &QPainterWrap::rotate), + InstanceMethod("save", &QPainterWrap::save), + InstanceMethod("scale", &QPainterWrap::scale), + InstanceMethod("setBrush", &QPainterWrap::setBrush), InstanceMethod("setBrushOrigin", &QPainterWrap::setBrushOrigin), + InstanceMethod("setBrushOriginF", &QPainterWrap::setBrushOriginF), + InstanceMethod("setCompositionMode", &QPainterWrap::setCompositionMode), + InstanceMethod("setFont", &QPainterWrap::setFont), + InstanceMethod("setOpacity", &QPainterWrap::setOpacity), + InstanceMethod("setPen", &QPainterWrap::setPen), + InstanceMethod("setRenderHint", &QPainterWrap::setRenderHint), + InstanceMethod("setTransform", &QPainterWrap::setTransform), + InstanceMethod("strokePath", &QPainterWrap::strokePath), + InstanceMethod("translate", &QPainterWrap::translate), COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE(QPainterWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); @@ -91,6 +106,17 @@ Napi::Value QPainterWrap::drawArc(const Napi::CallbackInfo& info) { this->instance->drawArc(x, y, width, height, startAngle, spanAngle); return env.Null(); } +Napi::Value QPainterWrap::drawArcF(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + qreal x = info[0].As().DoubleValue(); + qreal y = info[1].As().DoubleValue(); + qreal width = info[2].As().DoubleValue(); + qreal height = info[3].As().DoubleValue(); + int startAngle = info[4].As().Int32Value(); + int spanAngle = info[5].As().Int32Value(); + this->instance->drawArc(QRectF(x, y, width, height), startAngle, spanAngle); + return env.Null(); +} Napi::Value QPainterWrap::drawText(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); int x = info[0].As().Int32Value(); @@ -100,6 +126,15 @@ Napi::Value QPainterWrap::drawText(const Napi::CallbackInfo& info) { this->instance->drawText(x, y, QString::fromUtf8(text.c_str())); return env.Null(); } +Napi::Value QPainterWrap::drawTextF(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + qreal x = info[0].As().DoubleValue(); + qreal y = info[1].As().DoubleValue(); + Napi::String napiText = info[2].As(); + std::string text = napiText.Utf8Value(); + this->instance->drawText(QPointF(x, y), QString::fromUtf8(text.c_str())); + return env.Null(); +} Napi::Value QPainterWrap::drawPath(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); Napi::Object pathObject = info[0].As(); @@ -189,11 +224,27 @@ Napi::Value QPainterWrap::drawEllipse(const Napi::CallbackInfo& info) { .ThrowAsJavaScriptException(); return env.Null(); } + int x = info[0].As().Int32Value(); + int y = info[1].As().Int32Value(); + int width = info[2].As().Int32Value(); + int height = info[3].As().Int32Value(); + this->instance->drawEllipse(x, y, width, height); + + return env.Null(); +} + +Napi::Value QPainterWrap::drawEllipseF(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + if (info.Length() < 4) { + Napi::TypeError::New(env, "Invalid number of arguments to drawEllipseF") + .ThrowAsJavaScriptException(); + return env.Null(); + } qreal x = info[0].As().DoubleValue(); qreal y = info[1].As().DoubleValue(); qreal width = info[2].As().DoubleValue(); qreal height = info[3].As().DoubleValue(); - this->instance->drawEllipse(x, y, width, height); + this->instance->drawEllipse(QRectF(x, y, width, height)); return env.Null(); } @@ -217,7 +268,44 @@ Napi::Value QPainterWrap::drawImage(const Napi::CallbackInfo& info) { return env.Null(); } +Napi::Value QPainterWrap::drawImageF(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + + qreal x = info[0].As().DoubleValue(); + qreal y = info[1].As().DoubleValue(); + + Napi::Object imageObject = info[2].As(); + QImageWrap* imageWrap = Napi::ObjectWrap::Unwrap(imageObject); + QImage* image = imageWrap->getInternalInstance(); + + qreal sx = info[3].As().DoubleValue(); + qreal sy = info[4].As().DoubleValue(); + qreal sw = info[5].As().DoubleValue(); + qreal sh = info[6].As().DoubleValue(); + this->instance->drawImage(QPointF(x, y), *image, QRectF(sx, sy, sw, sh)); + + return env.Null(); +} + Napi::Value QPainterWrap::drawPie(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + if (info.Length() < 6) { + Napi::TypeError::New(env, "Invalid number of arguments to drawPie") + .ThrowAsJavaScriptException(); + return env.Null(); + } + int x = info[0].As().Int32Value(); + int y = info[1].As().Int32Value(); + int width = info[2].As().Int32Value(); + int height = info[3].As().Int32Value(); + int startAngle = info[4].As().Int32Value(); + int sweepLength = info[5].As().Int32Value(); + this->instance->drawPie(x, y, width, height, startAngle, sweepLength); + + return env.Null(); +} + +Napi::Value QPainterWrap::drawPieF(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); if (info.Length() < 6) { Napi::TypeError::New(env, "Invalid number of arguments to drawPie") @@ -228,12 +316,13 @@ Napi::Value QPainterWrap::drawPie(const Napi::CallbackInfo& info) { qreal y = info[1].As().DoubleValue(); qreal width = info[2].As().DoubleValue(); qreal height = info[3].As().DoubleValue(); - qreal startAngle = info[4].As().DoubleValue(); - qreal sweepLength = info[5].As().DoubleValue(); - this->instance->drawPie(x, y, width, height, startAngle, sweepLength); + int startAngle = info[4].As().Int32Value(); + int sweepLength = info[5].As().Int32Value(); + this->instance->drawPie(QRectF(x, y, width, height), startAngle, sweepLength); return env.Null(); } + Napi::Value QPainterWrap::drawLine(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); int x1 = info[0].As().Int32Value(); @@ -243,6 +332,15 @@ Napi::Value QPainterWrap::drawLine(const Napi::CallbackInfo& info) { this->instance->drawLine(x1, y1, x2, y2); return env.Null(); } +Napi::Value QPainterWrap::drawLineF(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + qreal x1 = info[0].As().DoubleValue(); + qreal y1 = info[1].As().DoubleValue(); + qreal x2 = info[2].As().DoubleValue(); + qreal y2 = info[3].As().DoubleValue(); + this->instance->drawLine(QLineF(x1, y1, x2, y2)); + return env.Null(); +} Napi::Value QPainterWrap::scale(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); qreal sx = info[0].As().DoubleValue(); @@ -270,6 +368,19 @@ Napi::Value QPainterWrap::drawConvexPolygon(const Napi::CallbackInfo& info) { this->instance->drawConvexPolygon(polygon); return env.Null(); } +Napi::Value QPainterWrap::drawConvexPolygonF(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::Array pointsNapi = info[0].As(); + QPolygonF polygon; + for (int i = 0; i < pointsNapi.Length(); i++) { + Napi::Object pointObject = pointsNapi.Get(i).As(); + QPointFWrap* pointWrap = Napi::ObjectWrap::Unwrap(pointObject); + QPointF* point = pointWrap->getInternalInstance(); + polygon << *point; + } + this->instance->drawConvexPolygon(polygon); + return env.Null(); +} Napi::Value QPainterWrap::save(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); this->instance->save(); @@ -349,6 +460,18 @@ Napi::Value QPainterWrap::fillRect(const Napi::CallbackInfo& info) { this->instance->fillRect(x, y, width, height, *color); return env.Null(); } +Napi::Value QPainterWrap::fillRectF(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + qreal x = info[0].As().DoubleValue(); + qreal y = info[1].As().DoubleValue(); + qreal width = info[2].As().DoubleValue(); + qreal height = info[3].As().DoubleValue(); + Napi::Object colorObject = info[4].As(); + QColorWrap* colorWrap = Napi::ObjectWrap::Unwrap(colorObject); + QColor* color = colorWrap->getInternalInstance(); + this->instance->fillRect(QRectF(x, y, width, height), *color); + return env.Null(); +} Napi::Value QPainterWrap::compositionMode(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); uint mode = static_cast(this->instance->compositionMode()); @@ -379,6 +502,13 @@ Napi::Value QPainterWrap::drawPoint(const Napi::CallbackInfo& info) { this->instance->drawPoint(x, y); return env.Null(); } +Napi::Value QPainterWrap::drawPointF(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + qreal x = info[0].As().DoubleValue(); + qreal y = info[1].As().DoubleValue(); + this->instance->drawPoint(QPointF(x, y)); + return env.Null(); +} Napi::Value QPainterWrap::drawRect(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); int x = info[0].As().Int32Value(); @@ -388,6 +518,15 @@ Napi::Value QPainterWrap::drawRect(const Napi::CallbackInfo& info) { this->instance->drawRect(x, y, width, height); return env.Null(); } +Napi::Value QPainterWrap::drawRectF(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + qreal x = info[0].As().DoubleValue(); + qreal y = info[1].As().DoubleValue(); + qreal width = info[2].As().DoubleValue(); + qreal height = info[3].As().DoubleValue(); + this->instance->drawRect(QRectF(x, y, width, height)); + return env.Null(); +} Napi::Value QPainterWrap::eraseRect(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); int x = info[0].As().Int32Value(); @@ -397,6 +536,15 @@ Napi::Value QPainterWrap::eraseRect(const Napi::CallbackInfo& info) { this->instance->eraseRect(x, y, width, height); return env.Null(); } +Napi::Value QPainterWrap::eraseRectF(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + qreal x = info[0].As().DoubleValue(); + qreal y = info[1].As().DoubleValue(); + qreal width = info[2].As().DoubleValue(); + qreal height = info[3].As().DoubleValue(); + this->instance->eraseRect(QRectF(x, y, width, height)); + return env.Null(); +} Napi::Value QPainterWrap::boundingRect(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); int x = info[0].As().Int32Value(); @@ -411,6 +559,20 @@ Napi::Value QPainterWrap::boundingRect(const Napi::CallbackInfo& info) { {Napi::External::New(env, new QRect(result))}); return resultInstance; } +Napi::Value QPainterWrap::boundingRectF(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + qreal x = info[0].As().DoubleValue(); + qreal y = info[1].As().DoubleValue(); + qreal w = info[2].As().DoubleValue(); + qreal h = info[3].As().DoubleValue(); + int flags = info[4].As().Int32Value(); + std::string textNapiText = info[5].As().Utf8Value(); + QString text = QString::fromUtf8(textNapiText.c_str()); + QRectF result = this->instance->boundingRect(QRectF(x, y, w, h), flags, text); + auto resultInstance = QRectFWrap::constructor.New( + {Napi::External::New(env, new QRectF(result))}); + return resultInstance; +} Napi::Value QPainterWrap::drawChord(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); int x = info[0].As().Int32Value(); @@ -422,6 +584,17 @@ Napi::Value QPainterWrap::drawChord(const Napi::CallbackInfo& info) { this->instance->drawChord(x, y, width, height, startAngle, spanAngle); return env.Null(); } +Napi::Value QPainterWrap::drawChordF(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + qreal x = info[0].As().DoubleValue(); + qreal y = info[1].As().DoubleValue(); + qreal width = info[2].As().DoubleValue(); + qreal height = info[3].As().DoubleValue(); + int startAngle = info[4].As().Int32Value(); + int spanAngle = info[5].As().Int32Value(); + this->instance->drawChord(QRectF(x, y, width, height), startAngle, spanAngle); + return env.Null(); +} Napi::Value QPainterWrap::setBrushOrigin(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); int x = info[0].As().Int32Value(); @@ -429,3 +602,10 @@ Napi::Value QPainterWrap::setBrushOrigin(const Napi::CallbackInfo& info) { this->instance->setBrushOrigin(x, y); return env.Null(); } +Napi::Value QPainterWrap::setBrushOriginF(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + qreal x = info[0].As().DoubleValue(); + qreal y = info[1].As().DoubleValue(); + this->instance->setBrushOrigin(QPointF(x, y)); + return env.Null(); +} diff --git a/src/lib/QtWidgets/QPainter.ts b/src/lib/QtWidgets/QPainter.ts index 97673b2f6..297fa8574 100644 --- a/src/lib/QtWidgets/QPainter.ts +++ b/src/lib/QtWidgets/QPainter.ts @@ -10,6 +10,7 @@ import { QWidget } from './QWidget'; import { QImage } from '../QtGui/QImage'; import { QFont } from '../QtGui/QFont'; import { QRect } from '../QtCore/QRect'; +import { QRectF } from '../QtCore/QRectF'; /** @@ -46,7 +47,7 @@ win.show(); https://github.com/nodegui/examples/blob/master/nodegui/custom-native-widget-qpainter */ export class QPainter extends Component { - constructor(device?: Component) { + constructor(device?: QWidget) { let native: NativeElement; if (device) { native = new addon.QPainter(device.native); @@ -74,6 +75,12 @@ export class QPainter extends Component { boundingRect(x: number, y: number, w: number, h: number, flags: number, text: string): QRect { return new QRect(this.native.boundingRect(x, y, w, h, flags, text)); } + /** + * Version of boundingRect() which takes floating point parameters. + */ + boundingRectF(x: number, y: number, w: number, h: number, flags: number, text: string): QRect { + return new QRectF(this.native.boundingRectF(x, y, w, h, flags, text)); + } // TODO: QRectF boundingRect(const QRectF &rectangle, const QString &text, const QTextOption &option = QTextOption()) // TODO: const QBrush & brush() const // TODO: QPoint brushOrigin() const @@ -90,27 +97,61 @@ export class QPainter extends Component { drawArc(x: number, y: number, width: number, height: number, startAngle: number, spanAngle: number): void { this.native.drawArc(x, y, width, height, startAngle, spanAngle); } + /** + * Version of drawArc() which takes floating point parameters. + */ + drawArcF(x: number, y: number, width: number, height: number, startAngle: number, spanAngle: number): void { + this.native.drawArcF(x, y, width, height, startAngle, spanAngle); + } // TODO: void drawChord(const QRectF &rectangle, int startAngle, int spanAngle) drawChord(x: number, y: number, width: number, height: number, startAngle: number, spanAngle: number): void { this.native.drawChord(x, y, width, height, startAngle, spanAngle); } + /** + * Version of drawChord() which takes floating point parameters. + */ + drawChordF(x: number, y: number, width: number, height: number, startAngle: number, spanAngle: number): void { + this.native.drawChordF(x, y, width, height, startAngle, spanAngle); + } // TODO: void drawChord(const QRect &rectangle, int startAngle, int spanAngle) drawConvexPolygon(points: QPoint[]): void { const nativePoints = points.map((point) => point.native); this.native.drawConvexPolygon(nativePoints); } + drawConvexPolygonF(points: QPoint[]): void { + const nativePoints = points.map((point) => point.native); + this.native.drawConvexPolygonF(nativePoints); + } // TODO: void drawConvexPolygon(const QPolygonF &polygon) drawEllipse(x: number, y: number, width: number, height: number): void { return this.native.drawEllipse(x, y, width, height); } + /** + * Version of drawEllipse() which takes floating point parameters. + */ + drawEllipseF(x: number, y: number, width: number, height: number): void { + return this.native.drawEllipseF(x, y, width, height); + } // TODO: void drawEllipse(const QRectF &rectangle) // TODO: void drawGlyphRun(const QPointF &position, const QGlyphRun &glyphs) drawImage(x: number, y: number, image: QImage, sx = 0, sy = 0, sw = -1, sh = -1): void { this.native.drawImage(x, y, image.native, sx, sy, sw, sh); } + /** + * Version of drawImage() which takes floating point parameters. + */ + drawImageF(x: number, y: number, image: QImage, sx = 0, sy = 0, sw = -1, sh = -1): void { + this.native.drawImageF(x, y, image.native, sx, sy, sw, sh); + } drawLine(x1: number, y1: number, x2: number, y2: number): void { this.native.drawLine(x1, y1, x2, y2); } + /** + * Version of drawLine() which takes floating point parameters. + */ + drawLineF(x1: number, y1: number, x2: number, y2: number): void { + this.native.drawLineF(x1, y1, x2, y2); + } // TODO: void drawLines(const QVector &lines) drawPath(path: QPainterPath): void { return this.native.drawPath(path.native); @@ -119,22 +160,43 @@ export class QPainter extends Component { drawPie(x: number, y: number, width: number, height: number, startAngle: number, sweepLength: number): void { return this.native.drawPie(x, y, width, height, startAngle, sweepLength); } + /** + * Version of drawPie() which takes floating point parameters. + */ + drawPieF(x: number, y: number, width: number, height: number, startAngle: number, sweepLength: number): void { + return this.native.drawPieF(x, y, width, height, startAngle, sweepLength); + } // TODO: void drawPixmap(int x, int y, int w, int h, const QPixmap &pixmap, int sx, int sy, int sw, int sh) // TODO: void drawPixmapFragments(const QPainter::PixmapFragment *fragments, int fragmentCount, const QPixmap &pixmap, QPainter::PixmapFragmentHints hints = PixmapFragmentHints()) drawPoint(x: number, y: number): void { this.native.drawPoint(x, y); } + drawPointF(x: number, y: number): void { + this.native.drawPointF(x, y); + } // TODO: void drawPoints(const QPointF *points, int pointCount) // TODO: void drawPolygon(const QPointF *points, int pointCount, Qt::FillRule fillRule = Qt::OddEvenFill) drawRect(x: number, y: number, width: number, height: number): void { this.native.drawRect(x, y, width, height); } + /** + * Version of drawRect() which takes floating point parameters. + */ + drawRectF(x: number, y: number, width: number, height: number): void { + this.native.drawRectF(x, y, width, height); + } // TODO: void drawRects(const QVector &rectangles) // TODO: void drawRoundedRect(int x, int y, int w, int h, qreal xRadius, qreal yRadius, Qt::SizeMode mode = Qt::AbsoluteSize) // TODO: void drawStaticText(int left, int top, const QStaticText &staticText) drawText(x: number, y: number, text: string): void { return this.native.drawText(x, y, text); } + /** + * Version of drawText() which takes floating point parameters + */ + drawTextF(x: number, y: number, text: string): void { + return this.native.drawTextF(x, y, text); + } // TODO: void drawText(int x, int y, int width, int height, int flags, const QString &text, QRect *boundingRect = nullptr) // TODO: void drawTiledPixmap(int x, int y, int width, int height, const QPixmap &pixmap, int sx = 0, int sy = 0) end(): boolean { @@ -146,9 +208,21 @@ export class QPainter extends Component { eraseRect(x: number, y: number, width: number, height: number): void { this.native.eraseRect(x, y, width, height); } + /** + * Version of eraseRect() which takes floating point parameters. + */ + eraseRectF(x: number, y: number, width: number, height: number): void { + this.native.eraseRectF(x, y, width, height); + } fillRect(x: number, y: number, width: number, height: number, color: QColor): void { this.native.fillRect(x, y, width, height, color.native); } + /** + * Version of fillRect() which takes floating point parameters. + */ + fillRectF(x: number, y: number, width: number, height: number, color: QColor): void { + this.native.fillRectF(x, y, width, height, color.native); + } // TODO: const QFont & font() const // TODO: QFontInfo fontInfo() const // TODO: QFontMetrics fontMetrics() const @@ -187,6 +261,12 @@ export class QPainter extends Component { setBrushOrigin(x: number, y: number): void { this.native.setBrushOrigin(x, y); } + /** + * Version of setBrushOrigin() which takes floating point parameters. + */ + setBrushOriginF(x: number, y: number): void { + this.native.setBrushOriginF(x, y); + } setCompositionMode(mode: CompositionMode): void { this.native.setCompositionMode(mode); }