Extra drawing related methods (#844)

* Add `setPixelSize()` & `pixelSize()` to `QFont`

* Add `xHeight()` to `QFontMetrics`

* Export `QImageFormat`

* Add `drawArc()`, `setFont()`, `setTransform()`, & `fillRect()` to `QPainter`

* Improve `QPainter.setTransform()` docs
This commit is contained in:
Simon Edwards 2021-06-19 11:34:40 +02:00 committed by GitHub
parent d0e9f67a25
commit a59274c371
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 171 additions and 9 deletions

View File

@ -22,7 +22,9 @@ class DLL_EXPORT QFontWrap : public Napi::ObjectWrap<QFontWrap> {
Napi::Value capitalization(const Napi::CallbackInfo& info);
Napi::Value setFamily(const Napi::CallbackInfo& info);
Napi::Value family(const Napi::CallbackInfo& info);
Napi::Value setPixelSize(const Napi::CallbackInfo& info);
Napi::Value setPointSize(const Napi::CallbackInfo& info);
Napi::Value pixelSize(const Napi::CallbackInfo& info);
Napi::Value pointSize(const Napi::CallbackInfo& info);
Napi::Value setStretch(const Napi::CallbackInfo& info);
Napi::Value stretch(const Napi::CallbackInfo& info);

View File

@ -37,4 +37,5 @@ class DLL_EXPORT QFontMetricsWrap : public Napi::ObjectWrap<QFontMetricsWrap> {
Napi::Value strikeOutPos(const Napi::CallbackInfo& info);
Napi::Value swap(const Napi::CallbackInfo& info);
Napi::Value underlinePos(const Napi::CallbackInfo& info);
Napi::Value xHeight(const Napi::CallbackInfo& info);
};

View File

@ -20,6 +20,7 @@ class DLL_EXPORT QPainterWrap : public Napi::ObjectWrap<QPainterWrap> {
// class constructor
static Napi::FunctionReference constructor;
// wrapped methods
Napi::Value drawArc(const Napi::CallbackInfo& info);
Napi::Value drawText(const Napi::CallbackInfo& info);
Napi::Value drawPath(const Napi::CallbackInfo& info);
Napi::Value strokePath(const Napi::CallbackInfo& info);
@ -28,8 +29,10 @@ class DLL_EXPORT QPainterWrap : public Napi::ObjectWrap<QPainterWrap> {
Napi::Value end(const Napi::CallbackInfo& info);
Napi::Value endNativePainting(const Napi::CallbackInfo& info);
Napi::Value rotate(const Napi::CallbackInfo& info);
Napi::Value setFont(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);
@ -39,4 +42,5 @@ class DLL_EXPORT QPainterWrap : public Napi::ObjectWrap<QPainterWrap> {
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);
};

View File

@ -15,7 +15,9 @@ Napi::Object QFontWrap::init(Napi::Env env, Napi::Object exports) {
InstanceMethod("capitalization", &QFontWrap::capitalization),
InstanceMethod("setFamily", &QFontWrap::setFamily),
InstanceMethod("family", &QFontWrap::family),
InstanceMethod("setPixelSize", &QFontWrap::setPixelSize),
InstanceMethod("setPointSize", &QFontWrap::setPointSize),
InstanceMethod("pixelSize", &QFontWrap::pixelSize),
InstanceMethod("pointSize", &QFontWrap::pointSize),
InstanceMethod("setStretch", &QFontWrap::setStretch),
InstanceMethod("stretch", &QFontWrap::stretch),
@ -91,6 +93,14 @@ Napi::Value QFontWrap::family(const Napi::CallbackInfo& info) {
return Napi::String::New(env, family.toStdString());
}
Napi::Value QFontWrap::setPixelSize(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
int pointSize = info[0].As<Napi::Number>().Int32Value();
this->instance->setPixelSize(pointSize);
return env.Null();
}
Napi::Value QFontWrap::setPointSize(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
@ -99,6 +109,12 @@ Napi::Value QFontWrap::setPointSize(const Napi::CallbackInfo& info) {
return env.Null();
}
Napi::Value QFontWrap::pixelSize(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
return Napi::Value::From(env, this->instance->pixelSize());
}
Napi::Value QFontWrap::pointSize(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);

View File

@ -30,6 +30,7 @@ Napi::Object QFontMetricsWrap::init(Napi::Env env, Napi::Object exports) {
InstanceMethod("strikeOutPos", &QFontMetricsWrap::strikeOutPos),
InstanceMethod("swap", &QFontMetricsWrap::swap),
InstanceMethod("underlinePos", &QFontMetricsWrap::underlinePos),
InstanceMethod("xHeight", &QFontMetricsWrap::xHeight),
COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE(QFontMetricsWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
@ -219,3 +220,10 @@ Napi::Value QFontMetricsWrap::underlinePos(const Napi::CallbackInfo& info) {
return Napi::Value::From(env, this->instance->underlinePos());
}
Napi::Value QFontMetricsWrap::xHeight(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
return Napi::Value::From(env, this->instance->xHeight());
}

View File

@ -3,6 +3,8 @@
#include "Extras/Utils/nutils.h"
#include "QtCore/QPoint/qpoint_wrap.h"
#include "QtGui/QColor/qcolor_wrap.h"
#include "QtGui/QFont/qfont_wrap.h"
#include "QtGui/QImage/qimage_wrap.h"
#include "QtGui/QPen/qpen_wrap.h"
#include "QtWidgets/QPainterPath/qpainterpath_wrap.h"
#include "QtWidgets/QWidget/qwidget_wrap.h"
@ -15,7 +17,8 @@ Napi::Object QPainterWrap::init(Napi::Env env, Napi::Object exports) {
char CLASSNAME[] = "QPainter";
Napi::Function func = DefineClass(
env, CLASSNAME,
{InstanceMethod("drawText", &QPainterWrap::drawText),
{InstanceMethod("drawArc", &QPainterWrap::drawArc),
InstanceMethod("drawText", &QPainterWrap::drawText),
InstanceMethod("drawPath", &QPainterWrap::drawPath),
InstanceMethod("drawPie", &QPainterWrap::drawPie),
InstanceMethod("drawEllipse", &QPainterWrap::drawEllipse),
@ -23,8 +26,10 @@ Napi::Object QPainterWrap::init(Napi::Env env, Napi::Object exports) {
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),
@ -35,6 +40,7 @@ Napi::Object QPainterWrap::init(Napi::Env env, Napi::Object exports) {
InstanceMethod("beginNativePainting",
&QPainterWrap::beginNativePainting),
InstanceMethod("endNativePainting", &QPainterWrap::endNativePainting),
InstanceMethod("fillRect", &QPainterWrap::fillRect),
COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE(QPainterWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
@ -62,6 +68,18 @@ QPainterWrap::QPainterWrap(const Napi::CallbackInfo& info)
}
this->rawData = extrautils::configureComponent(this->getInternalInstance());
}
Napi::Value QPainterWrap::drawArc(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
int x = info[0].As<Napi::Number>().Int32Value();
int y = info[1].As<Napi::Number>().Int32Value();
int width = info[2].As<Napi::Number>().Int32Value();
int height = info[3].As<Napi::Number>().Int32Value();
int startAngle = info[4].As<Napi::Number>().Int32Value();
int spanAngle = info[5].As<Napi::Number>().Int32Value();
this->instance->drawArc(x, y, width, height, startAngle, spanAngle);
return env.Null();
}
Napi::Value QPainterWrap::drawText(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
@ -99,11 +117,23 @@ Napi::Value QPainterWrap::begin(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::Object deviceObject = info[0].As<Napi::Object>();
NodeWidgetWrap* deviceWrap =
Napi::ObjectWrap<NodeWidgetWrap>::Unwrap(deviceObject);
QWidget* device = deviceWrap->getInternalInstance();
bool ret = this->instance->begin(device);
Napi::String napiType = info[1].As<Napi::String>();
std::string type = napiType.Utf8Value();
bool ret;
if (type == "widget") {
Napi::Object deviceObject = info[0].As<Napi::Object>();
NodeWidgetWrap* deviceWrap =
Napi::ObjectWrap<NodeWidgetWrap>::Unwrap(deviceObject);
QPaintDevice* device = deviceWrap->getInternalInstance();
ret = this->instance->begin(device);
} else if (type == "image") {
Napi::Object deviceObject = info[0].As<Napi::Object>();
QImageWrap* deviceWrap = Napi::ObjectWrap<QImageWrap>::Unwrap(deviceObject);
QPaintDevice* device = deviceWrap->getInternalInstance();
ret = this->instance->begin(device);
}
return Napi::Value::From(env, ret);
}
Napi::Value QPainterWrap::end(const Napi::CallbackInfo& info) {
@ -119,6 +149,15 @@ Napi::Value QPainterWrap::rotate(const Napi::CallbackInfo& info) {
this->instance->rotate(angle);
return env.Null();
}
Napi::Value QPainterWrap::setFont(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::Object fontObject = info[0].As<Napi::Object>();
QFontWrap* fontWrap = Napi::ObjectWrap<QFontWrap>::Unwrap(fontObject);
QFont* font = fontWrap->getInternalInstance();
this->instance->setFont(*font);
return env.Null();
}
Napi::Value QPainterWrap::setPen(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
@ -247,6 +286,28 @@ Napi::Value QPainterWrap::setRenderHint(const Napi::CallbackInfo& info) {
this->instance->setRenderHint(hint, true);
return env.Null();
}
Napi::Value QPainterWrap::setTransform(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::String napiType = info[0].As<Napi::String>();
std::string type = napiType.Utf8Value();
if (type == "matrix2x3") {
bool combine = info[1].As<Napi::Boolean>().Value();
qreal m11 = info[2].As<Napi::Number>().DoubleValue();
qreal m12 = info[3].As<Napi::Number>().DoubleValue();
qreal m21 = info[4].As<Napi::Number>().DoubleValue();
qreal m22 = info[5].As<Napi::Number>().DoubleValue();
qreal m31 = info[6].As<Napi::Number>().DoubleValue();
qreal m32 = info[7].As<Napi::Number>().DoubleValue();
QTransform xform(m11, m12, m21, m22, m31, m32);
this->instance->setTransform(xform, combine);
}
return env.Null();
}
Napi::Value QPainterWrap::beginNativePainting(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
@ -259,3 +320,16 @@ Napi::Value QPainterWrap::endNativePainting(const Napi::CallbackInfo& info) {
this->instance->endNativePainting();
return env.Null();
}
Napi::Value QPainterWrap::fillRect(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
int x = info[0].As<Napi::Number>().Int32Value();
int y = info[1].As<Napi::Number>().Int32Value();
int width = info[2].As<Napi::Number>().Int32Value();
int height = info[3].As<Napi::Number>().Int32Value();
Napi::Object colorObject = info[4].As<Napi::Object>();
QColorWrap* colorWrap = Napi::ObjectWrap<QColorWrap>::Unwrap(colorObject);
QColor* color = colorWrap->getInternalInstance();
this->instance->fillRect(x, y, width, height, *color);
return env.Null();
}

View File

@ -10,7 +10,7 @@ export { QKeySequence } from './lib/QtGui/QKeySequence';
export { QPicture } from './lib/QtGui/QPicture';
export { QPixmap, ImageFormats } from './lib/QtGui/QPixmap';
export { QIcon, QIconMode, QIconState } from './lib/QtGui/QIcon';
export { QImage } from './lib/QtGui/QImage';
export { QImage, QImageFormat } from './lib/QtGui/QImage';
export { QFont, QFontCapitalization, QFontStretch, QFontWeight } from './lib/QtGui/QFont';
export { QMovie, CacheMode, MovieState } from './lib/QtGui/QMovie';
export { QCursor } from './lib/QtGui/QCursor';

View File

@ -33,9 +33,15 @@ export class QFont extends Component {
family(): string {
return this.native.family();
}
setPixelSize(value: number): void {
this.native.setPixelSize(value);
}
setPointSize(value: number): void {
this.native.setPointSize(value);
}
pixelSize(): number {
return this.native.pixelSize();
}
pointSize(): number {
return this.native.pointSize();
}

View File

@ -115,4 +115,9 @@ export class QFontMetrics extends Component {
underlinePos(): number {
return this.native.underlinePos();
}
/** Returns the 'x' height of the font. This is often but not always the same as the height of the character 'x'. */
xHeight(): number {
return this.native.xHeight();
}
}

View File

@ -5,6 +5,9 @@ import { PenStyle } from '../QtEnums';
import { QColor } from '../QtGui/QColor';
import { QPoint } from '../QtCore/QPoint';
import { QPen } from '../QtGui/QPen';
import { QWidget } from './QWidget';
import { QImage } from '../QtGui/QImage';
import { QFont } from '../QtGui/QFont';
/**
@ -55,6 +58,10 @@ export class QPainter extends Component {
this.native = native;
}
drawArc(x: number, y: number, width: number, height: number, startAngle: number, spanAngle: number): void {
this.native.drawArc(x, y, width, height, startAngle, spanAngle);
}
drawText(x: number, y: number, text: string): void {
return this.native.drawText(x, y, text);
}
@ -67,8 +74,12 @@ export class QPainter extends Component {
return this.native.strokePath(path.native, pen.native);
}
begin(device: Component): boolean {
return this.native.begin(device.native);
begin(device: QWidget | QImage): boolean {
if (device instanceof QWidget) {
return this.native.begin(device.native, 'widget');
} else {
return this.native.begin(device.native, 'image');
}
}
beginNativePainting(): void {
@ -87,6 +98,10 @@ export class QPainter extends Component {
this.native.rotate(angle);
}
setFont(font: QFont): void {
this.native.setFont(font.native);
}
setPen(arg: PenStyle | QColor | QPen): void {
if (typeof arg == 'number') {
this.native.setPen(arg, 'style');
@ -101,6 +116,33 @@ export class QPainter extends Component {
this.native.setRenderHint(hint, on);
}
/**
* Sets the world transformation matrix.
*
* @param matrix2x3 An array of length 6 representing a 2x3 transformation
* matrix. The order of elements corresponds to the
* convention used in QTransform, i.e. m11, m12, m21, m22,
* dx, and dy.
* @param combine If set then this transform will be combining with the
* curent one. Otherwise it replaces it completely.
*/
setTransform(matrix2x3: number[] | Float32Array, combine = false): void {
if (matrix2x3.length !== 6) {
throw new Error('Parameter "matrix2x3" to QPainter.setTransform() must have length 6.');
}
this.native.setTransform(
'matrix2x3',
combine,
matrix2x3[0],
matrix2x3[1],
matrix2x3[2],
matrix2x3[3],
matrix2x3[4],
matrix2x3[5],
);
}
drawEllipse(x: number, y: number, width: number, height: number): void {
return this.native.drawEllipse(x, y, width, height);
}
@ -137,6 +179,10 @@ export class QPainter extends Component {
setBrush(color: QColor): void {
this.native.setBrush(color.native);
}
fillRect(x: number, y: number, width: number, height: number, color: QColor): void {
this.native.fillRect(x, y, width, height, color.native);
}
}
export enum RenderHint {