add QPainterPath and additional QPainter functions (#552)

This commit is contained in:
Stephen A 2020-05-12 02:43:32 -05:00 committed by GitHub
parent 4215695759
commit b4c8cf61e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 407 additions and 1 deletions

View File

@ -39,6 +39,7 @@ add_library(${CORE_WIDGETS_ADDON} SHARED
"${PROJECT_SOURCE_DIR}/src/cpp/include/deps/yoga/internal/experiments.cpp"
# wrapped cpps
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QBrush/qbrush_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QPen/qpen_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QColor/qcolor_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QApplication/qapplication_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QClipboard/qclipboard_wrap.cpp"
@ -86,6 +87,7 @@ add_library(${CORE_WIDGETS_ADDON} SHARED
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QTableWidget/qtablewidget_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QTableWidgetItem/qtablewidgetitem_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QPainter/qpainter_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QPainterPath/qpainterpath_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QGridLayout/qgridlayout_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QDial/qdial_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QLabel/qlabel_wrap.cpp"

View File

@ -0,0 +1,28 @@
#pragma once
#include <napi.h>
#include <QPen>
#include "Extras/Export/export.h"
#include "core/Component/component_macro.h"
class DLL_EXPORT QPenWrap : public Napi::ObjectWrap<QPenWrap> {
COMPONENT_WRAPPED_METHODS_DECLARATION
private:
std::unique_ptr<QPen> instance;
public:
static Napi::FunctionReference constructor;
static Napi::Object init(Napi::Env env, Napi::Object exports);
QPenWrap(const Napi::CallbackInfo& info);
~QPenWrap();
QPen* getInternalInstance();
// Wrapped methods
Napi::Value setColor(const Napi::CallbackInfo& info);
Napi::Value color(const Napi::CallbackInfo& info);
Napi::Value setStyle(const Napi::CallbackInfo& info);
Napi::Value style(const Napi::CallbackInfo& info);
Napi::Value setCapStyle(const Napi::CallbackInfo& info);
Napi::Value setWidth(const Napi::CallbackInfo& info);
};

View File

@ -21,6 +21,8 @@ class DLL_EXPORT QPainterWrap : public Napi::ObjectWrap<QPainterWrap> {
static Napi::FunctionReference constructor;
// wrapped methods
Napi::Value drawText(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 end(const Napi::CallbackInfo& info);
Napi::Value rotate(const Napi::CallbackInfo& info);

View File

@ -0,0 +1,28 @@
#pragma once
#include <napi.h>
#include <QPainterPath>
#include "Extras/Export/export.h"
#include "core/Component/component_wrap.h"
class DLL_EXPORT QPainterPathWrap : public Napi::ObjectWrap<QPainterPathWrap> {
COMPONENT_WRAPPED_METHODS_DECLARATION
private:
std::unique_ptr<QPainterPath> instance;
public:
static Napi::FunctionReference constructor;
static Napi::Object init(Napi::Env env, Napi::Object exports);
QPainterPathWrap(const Napi::CallbackInfo& info);
~QPainterPathWrap();
QPainterPath* getInternalInstance();
// wrapped methods
Napi::Value moveTo(const Napi::CallbackInfo& info);
Napi::Value addRect(const Napi::CallbackInfo& info);
Napi::Value lineTo(const Napi::CallbackInfo& info);
Napi::Value cubicTo(const Napi::CallbackInfo& info);
Napi::Value quadTo(const Napi::CallbackInfo& info);
Napi::Value closeSubpath(const Napi::CallbackInfo& info);
};

View File

@ -0,0 +1,107 @@
#include "QtGui/QPen/qpen_wrap.h"
#include "Extras/Utils/nutils.h"
#include "QtCore/QVariant/qvariant_wrap.h"
#include "QtGui/QColor/qcolor_wrap.h"
#include "QtGui/QBrush/qbrush_wrap.h"
#include "QtGui/QPixmap/qpixmap_wrap.h"
Napi::FunctionReference QPenWrap::constructor;
Napi::Object QPenWrap::init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
char CLASSNAME[] = "QPen";
Napi::Function func = DefineClass(
env, CLASSNAME,
{InstanceMethod("setColor", &QPenWrap::setColor),
InstanceMethod("color", &QPenWrap::color),
InstanceMethod("setStyle", &QPenWrap::setStyle),
InstanceMethod("style", &QPenWrap::style),
InstanceMethod("setCapStyle", &QPenWrap::setCapStyle),
InstanceMethod("setWidth", &QPenWrap::setWidth),
COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE(QPenWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
return exports;
}
QPenWrap::QPenWrap(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<QPenWrap>(info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (info.Length() == 0) {
this->instance = std::make_unique<QPen>();
} else {
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = extrautils::configureComponent(this->getInternalInstance());
}
QPenWrap::~QPenWrap() { this->instance.reset(); }
QPen* QPenWrap::getInternalInstance() { return this->instance.get(); }
Napi::Value QPenWrap::setColor(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (info[0].IsNumber()) {
Qt::GlobalColor color =
(Qt::GlobalColor)info[0].As<Napi::Number>().Int32Value();
this->instance->setColor(color);
} else {
Napi::Object colorObject = info[0].As<Napi::Object>();
QColorWrap* colorWrap = Napi::ObjectWrap<QColorWrap>::Unwrap(colorObject);
this->instance->setColor(*colorWrap->getInternalInstance());
}
return env.Null();
}
Napi::Value QPenWrap::color(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
QColor color = this->instance->color();
auto instance = QColorWrap::constructor.New(
{Napi::External<QColor>::New(env, new QColor(color))});
return instance;
}
Napi::Value QPenWrap::setStyle(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Qt::PenStyle style =
(Qt::PenStyle)info[0].As<Napi::Number>().Int32Value();
this->instance->setStyle(style);
return env.Null();
}
Napi::Value QPenWrap::style(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Qt::PenStyle style = this->instance->style();
return Napi::Number::New(env, static_cast<int>(style));
}
Napi::Value QPenWrap::setCapStyle(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Qt::PenCapStyle style =
(Qt::PenCapStyle)info[0].As<Napi::Number>().Int32Value();
this->instance->setCapStyle(style);
return env.Null();
}
Napi::Value QPenWrap::setWidth(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
int width = info[0].As<Napi::Number>().Int32Value();
this->instance->setWidth(width);
return env.Null();
}

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/QPen/qpen_wrap.h"
#include "QtWidgets/QPainterPath/qpainterpath_wrap.h"
#include "QtWidgets/QWidget/qwidget_wrap.h"
#include "core/Component/component_wrap.h"
@ -14,6 +16,8 @@ Napi::Object QPainterWrap::init(Napi::Env env, Napi::Object exports) {
Napi::Function func = DefineClass(
env, CLASSNAME,
{InstanceMethod("drawText", &QPainterWrap::drawText),
InstanceMethod("drawPath", &QPainterWrap::drawPath),
InstanceMethod("strokePath", &QPainterWrap::strokePath),
InstanceMethod("begin", &QPainterWrap::begin),
InstanceMethod("end", &QPainterWrap::end),
InstanceMethod("rotate", &QPainterWrap::rotate),
@ -63,6 +67,27 @@ Napi::Value QPainterWrap::drawText(const Napi::CallbackInfo& info) {
this->instance->drawText(x, y, QString::fromUtf8(text.c_str()));
return env.Null();
}
Napi::Value QPainterWrap::drawPath(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::Object pathObject = info[0].As<Napi::Object>();
QPainterPathWrap* pathWrap = Napi::ObjectWrap<QPainterPathWrap>::Unwrap(pathObject);
QPainterPath* path = pathWrap->getInternalInstance();
this->instance->drawPath(*path);
return env.Null();
}
Napi::Value QPainterWrap::strokePath(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::Object pathObject = info[0].As<Napi::Object>();
QPainterPathWrap* pathWrap = Napi::ObjectWrap<QPainterPathWrap>::Unwrap(pathObject);
QPainterPath* path = pathWrap->getInternalInstance();
Napi::Object penObject = info[1].As<Napi::Object>();
QPenWrap* penWrap = Napi::ObjectWrap<QPenWrap>::Unwrap(penObject);
QPen* pen = penWrap->getInternalInstance();
this->instance->strokePath(*path, *pen);
return env.Null();
}
Napi::Value QPainterWrap::begin(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
@ -101,6 +126,11 @@ Napi::Value QPainterWrap::setPen(const Napi::CallbackInfo& info) {
} else if (type == "style") {
Qt::PenStyle style = (Qt::PenStyle)info[0].As<Napi::Number>().Int32Value();
this->instance->setPen(style);
} else if (type == "pen") {
Napi::Object penObject = info[0].As<Napi::Object>();
QPenWrap* penWrap = Napi::ObjectWrap<QPenWrap>::Unwrap(penObject);
QPen* pen = penWrap->getInternalInstance();
this->instance->setPen(*pen);
}
return env.Null();
}

View File

@ -0,0 +1,94 @@
#include "QtWidgets/QPainterPath/qpainterpath_wrap.h"
#include "Extras/Utils/nutils.h"
#include "QtCore/QPoint/qpoint_wrap.h"
Napi::FunctionReference QPainterPathWrap::constructor;
Napi::Object QPainterPathWrap::init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
char CLASSNAME[] = "QPainterPath";
Napi::Function func = DefineClass(
env, CLASSNAME,
{InstanceMethod("moveTo", &QPainterPathWrap::moveTo),
InstanceMethod("addRect", &QPainterPathWrap::addRect),
InstanceMethod("lineTo", &QPainterPathWrap::lineTo),
InstanceMethod("cubicTo", &QPainterPathWrap::cubicTo),
InstanceMethod("quadTo", &QPainterPathWrap::quadTo),
InstanceMethod("closeSubpath", &QPainterPathWrap::closeSubpath),
COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE(QPainterPathWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
return exports;
}
QPainterPath* QPainterPathWrap::getInternalInstance() { return this->instance.get(); }
QPainterPathWrap::~QPainterPathWrap() { this->instance.reset(); }
QPainterPathWrap::QPainterPathWrap(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<QPainterPathWrap>(info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (info.Length() == 0) {
this->instance = std::make_unique<QPainterPath>();
} else {
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = extrautils::configureComponent(this->getInternalInstance());
}
Napi::Value QPainterPathWrap::moveTo(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();
this->instance->moveTo(x, y);
return env.Null();
}
Napi::Value QPainterPathWrap::addRect(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();
this->instance->addRect(x, y, width, height);
return env.Null();
}
Napi::Value QPainterPathWrap::lineTo(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();
this->instance->lineTo(x, y);
return env.Null();
}
Napi::Value QPainterPathWrap::cubicTo(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
int c1X = info[0].As<Napi::Number>().Int32Value();
int c1Y = info[1].As<Napi::Number>().Int32Value();
int c2X = info[2].As<Napi::Number>().Int32Value();
int c2Y = info[3].As<Napi::Number>().Int32Value();
int endPointX = info[4].As<Napi::Number>().Int32Value();
int endPointY = info[5].As<Napi::Number>().Int32Value();
this->instance->cubicTo(c1X, c1Y, c2X, c2Y, endPointX, endPointY);
return env.Null();
}
Napi::Value QPainterPathWrap::quadTo(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
int cx = info[0].As<Napi::Number>().Int32Value();
int cy = info[1].As<Napi::Number>().Int32Value();
int endPointX = info[2].As<Napi::Number>().Int32Value();
int endPointY = info[3].As<Napi::Number>().Int32Value();
this->instance->quadTo(cx, cy, endPointX, endPointY);
return env.Null();
}
Napi::Value QPainterPathWrap::closeSubpath(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
this->instance->closeSubpath();
return env.Null();
}

View File

@ -14,6 +14,7 @@
#include "QtCore/QVariant/qvariant_wrap.h"
#include "QtGui/QApplication/qapplication_wrap.h"
#include "QtGui/QBrush/qbrush_wrap.h"
#include "QtGui/QPen/qpen_wrap.h"
#include "QtGui/QClipboard/qclipboard_wrap.h"
#include "QtGui/QColor/qcolor_wrap.h"
#include "QtGui/QCursor/qcursor_wrap.h"
@ -60,6 +61,7 @@
#include "QtWidgets/QMenuBar/qmenubar_wrap.h"
#include "QtWidgets/QMessageBox/qmessagebox_wrap.h"
#include "QtWidgets/QPainter/qpainter_wrap.h"
#include "QtWidgets/QPainterPath/qpainterpath_wrap.h"
#include "QtWidgets/QPlainTextEdit/qplaintextedit_wrap.h"
#include "QtWidgets/QProgressBar/qprogressbar_wrap.h"
#include "QtWidgets/QProgressDialog/qprogressdialog_wrap.h"
@ -110,6 +112,7 @@ Napi::Object Main(Napi::Env env, Napi::Object exports) {
QUrlWrap::init(env, exports);
QVariantWrap::init(env, exports);
QBrushWrap::init(env, exports);
QPenWrap::init(env, exports);
QColorWrap::init(env, exports);
QClipboardWrap::init(env, exports);
QDialogWrap::init(env, exports);
@ -143,6 +146,7 @@ Napi::Object Main(Napi::Env env, Napi::Object exports) {
QTableWidgetWrap::init(env, exports);
QTableWidgetItemWrap::init(env, exports);
QPainterWrap::init(env, exports);
QPainterPathWrap::init(env, exports);
QTreeWidgetWrap::init(env, exports);
QTreeWidgetItemWrap::init(env, exports);
QGridLayoutWrap::init(env, exports);

View File

@ -5,6 +5,7 @@ export * from './lib/QtEnums';
// Gui:
export { QApplication } from './lib/QtGui/QApplication';
export { QBrush } from './lib/QtGui/QBrush';
export { QPen } from './lib/QtGui/QPen';
export { QKeySequence } from './lib/QtGui/QKeySequence';
export { QPicture } from './lib/QtGui/QPicture';
export { QPixmap, ImageFormats } from './lib/QtGui/QPixmap';
@ -87,6 +88,7 @@ export { QTimeEdit } from './lib/QtWidgets/QTimeEdit';
export { QTreeWidget, QTreeWidgetSignals } from './lib/QtWidgets/QTreeWidget';
export { QTreeWidgetItem } from './lib/QtWidgets/QTreeWidgetItem';
export { QPainter, RenderHint } from './lib/QtWidgets/QPainter';
export { QPainterPath } from './lib/QtWidgets/QPainterPath';
export { QDialog, QDialogSignals } from './lib/QtWidgets/QDialog';
export { QMessageBox, QMessageBoxSignals, QMessageBoxIcon, ButtonRole } from './lib/QtWidgets/QMessageBox';
export { QInputDialog, QInputDialogSignals, InputDialogOptions, InputMode } from './lib/QtWidgets/QInputDialog';

View File

@ -0,0 +1,5 @@
export enum PenCapStyle {
FlatCap = 0x0,
SquareCap = 0x10,
RoundCap = 0x20,
}

View File

@ -88,4 +88,5 @@ export { WindowModality } from './WindowModality';
export { WindowState } from './WindowState';
export { WindowType } from './WindowType';
export { PenStyle } from './PenStyle';
export { PenCapStyle } from './PenCapStyle';
export { DialogCode } from './DialogCode';

48
src/lib/QtGui/QPen.ts Normal file
View File

@ -0,0 +1,48 @@
import { Component, NativeElement } from '../core/Component';
import addon from '../utils/addon';
import { GlobalColor, PenStyle, PenCapStyle } from '../QtEnums';
import { QColor } from './QColor';
/**
> The QPen class defines the outline of shapes drawn by QPainter.
* **This class is a JS wrapper around Qt's [QPen class](https://doc.qt.io/qt-5/qpen.html)**
### Example
```javascript
const { QPen } = require("@nodegui/nodegui");
const pen = new QPen();
```
*/
export class QPen extends Component {
native: NativeElement;
constructor() {
super();
this.native = new addon.QPen();
}
setColor(color: QColor | GlobalColor): void {
if (typeof color === 'number') {
this.native.setColor(color);
} else {
this.native.setColor(color.native);
}
}
color(): QColor {
return new QColor(this.native.color());
}
setStyle(style: PenStyle): void {
this.native.setStyle(style);
}
style(): PenStyle {
return this.native.style();
}
setCapStyle(style: PenCapStyle): void {
this.native.setCapStyle(style);
}
setWidth(width: number): void {
this.native.setWidth(width);
}
}

View File

@ -1,8 +1,10 @@
import addon from '../utils/addon';
import { Component, NativeElement } from '../core/Component';
import { QPainterPath } from '../QtWidgets/QPainterPath';
import { PenStyle } from '../QtEnums';
import { QColor } from '../QtGui/QColor';
import { QPoint } from '../QtCore/QPoint';
import { QPen } from '../QtGui/QPen';
/**
@ -57,6 +59,14 @@ export class QPainter extends Component {
return this.native.drawText(x, y, text);
}
drawPath(path: QPainterPath): void {
return this.native.drawPath(path.native);
}
strokePath(path: QPainterPath, pen: QPen): void {
return this.native.strokePath(path.native, pen.native);
}
begin(device: Component): boolean {
return this.native.begin(device.native);
}
@ -69,11 +79,13 @@ export class QPainter extends Component {
this.native.rotate(angle);
}
setPen(arg: PenStyle | QColor): void {
setPen(arg: PenStyle | QColor | QPen): void {
if (typeof arg == 'number') {
this.native.setPen(arg, 'style');
} else if (arg instanceof QColor) {
this.native.setPen(arg.native, 'color');
} else if (arg instanceof QPen) {
this.native.setPen(arg.native, 'pen');
}
}

View File

@ -0,0 +1,43 @@
import addon from '../utils/addon';
import { Component, NativeElement } from '../core/Component';
/**
> Lets you paint paths on widgets.
* **This class is a JS wrapper around Qt's [QPainterPath class](https://doc.qt.io/qt-5/qpainterpath.html)**
*/
export class QPainterPath extends Component {
native: NativeElement;
constructor() {
super();
const native = new addon.QPainterPath();
this.native = native;
}
moveTo(x: number, y: number): void {
return this.native.moveTo(x, y);
}
addRect(x: number, y: number, width: number, height: number): void {
return this.native.addRect(x, y, width, height);
}
lineTo(x: number, y: number): boolean {
return this.native.lineTo(x, y);
}
cubicTo(c1X: number, c1Y: number, c2X: number, c2Y: number, endPointX: number, endPointY: number): boolean {
return this.native.cubicTo(c1X, c1Y, c2X, c2Y, endPointX, endPointY);
}
quadTo(cx: number, cy: number, endPointX: number, endPointY: number): boolean {
return this.native.quadTo(cx, cy, endPointX, endPointY);
}
closeSubpath(): void {
return this.native.closeSubpath();
}
}