Extra font and painter related methods and classes (#845)

* Add `QPaintEvent`

* Add `setStyleName()` to `QFont`

* Add `isFixedPitch()` and `styles()` to `QFontDatabase`

* Add `drawImage()` to `QPainter`

* Add `setFocusPolicy()` to `QWidget`
This commit is contained in:
Simon Edwards 2021-06-26 18:37:53 +02:00 committed by GitHub
parent 52d122583e
commit 2b26ea4ddd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 174 additions and 2 deletions

View File

@ -48,6 +48,7 @@ add_library(${CORE_WIDGETS_ADDON} SHARED
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QEvent/QMouseEvent/qmouseevent_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QEvent/QWheelEvent/qwheelevent_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QEvent/QNativeGestureEvent/qnativegestureevent_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QEvent/QPaintEvent/qpaintevent_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QEvent/QTabletEvent/qtabletevent_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QEvent/QDropEvent/qdropevent_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QEvent/QDragMoveEvent/qdragmoveevent_wrap.cpp"

View File

@ -0,0 +1,25 @@
#pragma once
#include <napi.h>
#include <QPaintEvent>
#include "Extras/Export/export.h"
#include "core/Component/component_macro.h"
class DLL_EXPORT QPaintEventWrap : public Napi::ObjectWrap<QPaintEventWrap> {
COMPONENT_WRAPPED_METHODS_DECLARATION
private:
QPaintEvent* instance;
public:
static Napi::Object init(Napi::Env env, Napi::Object exports);
QPaintEventWrap(const Napi::CallbackInfo& info);
~QPaintEventWrap();
QPaintEvent* getInternalInstance();
// class constructor
static Napi::FunctionReference constructor;
// wrapped methods
Napi::Value rect(const Napi::CallbackInfo& info);
};

View File

@ -32,6 +32,7 @@ class DLL_EXPORT QFontWrap : public Napi::ObjectWrap<QFontWrap> {
Napi::Value weight(const Napi::CallbackInfo& info);
Napi::Value setItalic(const Napi::CallbackInfo& info);
Napi::Value italic(const Napi::CallbackInfo& info);
Napi::Value setStyleName(const Napi::CallbackInfo& info);
Napi::Value toString(const Napi::CallbackInfo& info);
COMPONENT_WRAPPED_METHODS_DECLARATION
};

View File

@ -22,7 +22,9 @@ class DLL_EXPORT QFontDatabaseWrap
// Wrapped methods
Napi::Value bold(const Napi::CallbackInfo& info);
Napi::Value families(const Napi::CallbackInfo& info);
Napi::Value isFixedPitch(const Napi::CallbackInfo& info);
Napi::Value italic(const Napi::CallbackInfo& info);
Napi::Value styles(const Napi::CallbackInfo& info);
Napi::Value weight(const Napi::CallbackInfo& info);
};

View File

@ -22,6 +22,7 @@ class DLL_EXPORT QPainterWrap : public Napi::ObjectWrap<QPainterWrap> {
// 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);

View File

@ -0,0 +1,49 @@
#include "QtGui/QEvent/QPaintEvent/qpaintevent_wrap.h"
#include <QString>
#include "Extras/Utils/nutils.h"
#include "QtCore/QRect/qrect_wrap.h"
Napi::FunctionReference QPaintEventWrap::constructor;
Napi::Object QPaintEventWrap::init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
char CLASSNAME[] = "QPaintEvent";
Napi::Function func =
DefineClass(env, CLASSNAME,
{InstanceMethod("rect", &QPaintEventWrap::rect),
COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE(QPaintEventWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
return exports;
}
QPaintEvent* QPaintEventWrap::getInternalInstance() { return this->instance; }
QPaintEventWrap::QPaintEventWrap(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<QPaintEventWrap>(info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (info.Length() == 1) {
Napi::External<QPaintEvent> eventObject =
info[0].As<Napi::External<QPaintEvent>>();
this->instance = static_cast<QPaintEvent*>(eventObject.Data());
} else {
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = extrautils::configureComponent(this->getInternalInstance());
}
QPaintEventWrap::~QPaintEventWrap() {
// Do not destroy instance here. It will be done by Qt Event loop.
}
Napi::Value QPaintEventWrap::rect(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
QRect ret = this->instance->rect();
auto instance = QRectWrap::constructor.New(
{Napi::External<QRect>::New(env, new QRect(ret))});
return instance;
}

View File

@ -25,6 +25,7 @@ Napi::Object QFontWrap::init(Napi::Env env, Napi::Object exports) {
InstanceMethod("weight", &QFontWrap::weight),
InstanceMethod("setItalic", &QFontWrap::setItalic),
InstanceMethod("italic", &QFontWrap::italic),
InstanceMethod("setStyleName", &QFontWrap::setStyleName),
InstanceMethod("toString", &QFontWrap::toString),
StaticMethod("fromQVariant", &StaticQFontWrapMethods::fromQVariant),
COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE(QFontWrap)});
@ -163,6 +164,14 @@ Napi::Value QFontWrap::italic(const Napi::CallbackInfo& info) {
return Napi::Value::From(env, this->instance->italic());
}
Napi::Value QFontWrap::setStyleName(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
std::string styleName = info[0].As<Napi::String>().Utf8Value();
this->instance->setStyleName(QString::fromStdString(styleName.c_str()));
return env.Null();
}
Napi::Value QFontWrap::toString(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);

View File

@ -10,7 +10,9 @@ Napi::Object QFontDatabaseWrap::init(Napi::Env env, Napi::Object exports) {
Napi::Function func = DefineClass(
env, CLASSNAME,
{InstanceMethod("bold", &QFontDatabaseWrap::bold),
InstanceMethod("isFixedPitch", &QFontDatabaseWrap::isFixedPitch),
InstanceMethod("italic", &QFontDatabaseWrap::italic),
InstanceMethod("styles", &QFontDatabaseWrap::styles),
InstanceMethod("weight", &QFontDatabaseWrap::weight),
InstanceMethod("families", &QFontDatabaseWrap::families),
StaticMethod("addApplicationFont",
@ -61,6 +63,23 @@ Napi::Value QFontDatabaseWrap::bold(const Napi::CallbackInfo& info) {
return Napi::Value::From(env, result);
}
Napi::Value QFontDatabaseWrap::isFixedPitch(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
std::string family = info[0].As<Napi::String>().Utf8Value();
QString qstyle;
if (!info[1].IsNull()) {
std::string style = info[1].As<Napi::String>().Utf8Value();
qstyle = QString::fromUtf8(style.c_str());
}
bool result =
this->instance->isFixedPitch(QString::fromUtf8(family.c_str()), qstyle);
return Napi::Value::From(env, result);
}
Napi::Value QFontDatabaseWrap::italic(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
@ -71,6 +90,19 @@ Napi::Value QFontDatabaseWrap::italic(const Napi::CallbackInfo& info) {
return Napi::Value::From(env, result);
}
Napi::Value QFontDatabaseWrap::styles(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
std::string family = info[0].As<Napi::String>().Utf8Value();
QStringList styles =
this->instance->styles(QString::fromUtf8(family.c_str()));
Napi::Array stylesNapi = Napi::Array::New(env, styles.size());
for (int i = 0; i < styles.size(); i++) {
stylesNapi[i] = Napi::String::New(env, styles[i].toStdString());
}
return stylesNapi;
}
Napi::Value QFontDatabaseWrap::weight(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);

View File

@ -22,6 +22,7 @@ Napi::Object QPainterWrap::init(Napi::Env env, Napi::Object exports) {
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),
@ -196,6 +197,27 @@ Napi::Value QPainterWrap::drawEllipse(const Napi::CallbackInfo& info) {
return env.Null();
}
Napi::Value QPainterWrap::drawImage(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();
Napi::Object imageObject = info[2].As<Napi::Object>();
QImageWrap* imageWrap = Napi::ObjectWrap<QImageWrap>::Unwrap(imageObject);
QImage* image = imageWrap->getInternalInstance();
int sx = info[3].As<Napi::Number>().Int32Value();
int sy = info[4].As<Napi::Number>().Int32Value();
int sw = info[5].As<Napi::Number>().Int32Value();
int sh = info[6].As<Napi::Number>().Int32Value();
this->instance->drawImage(x, y, *image, sx, sy, sw, sh);
return env.Null();
}
Napi::Value QPainterWrap::drawPie(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);

View File

@ -27,6 +27,7 @@
#include "QtGui/QEvent/QKeyEvent/qkeyevent_wrap.h"
#include "QtGui/QEvent/QMouseEvent/qmouseevent_wrap.h"
#include "QtGui/QEvent/QNativeGestureEvent/qnativegestureevent_wrap.h"
#include "QtGui/QEvent/QPaintEvent/qpaintevent_wrap.h"
#include "QtGui/QEvent/QTabletEvent/qtabletevent_wrap.h"
#include "QtGui/QEvent/QWheelEvent/qwheelevent_wrap.h"
#include "QtGui/QFont/qfont_wrap.h"
@ -216,6 +217,7 @@ Napi::Object Main(Napi::Env env, Napi::Object exports) {
QStandardItemWrap::init(env, exports);
QSvgWidgetWrap::init(env, exports);
QDesktopWidgetWrap::init(env, exports);
QPaintEventWrap::init(env, exports);
return exports;
}

View File

@ -29,6 +29,7 @@ export { QDrag } from './lib/QtGui/QDrag';
export { QDropEvent } from './lib/QtGui/QEvent/QDropEvent';
export { QDragMoveEvent } from './lib/QtGui/QEvent/QDragMoveEvent';
export { QDragLeaveEvent } from './lib/QtGui/QEvent/QDragLeaveEvent';
export { QPaintEvent } from './lib/QtGui/QEvent/QPaintEvent';
export { WidgetEventTypes } from './lib/core/EventWidget';
// Abstract:
export { NodeWidget, QWidget, QWidgetSignals } from './lib/QtWidgets/QWidget';

View File

@ -0,0 +1,13 @@
import addon from '../../utils/addon';
import { NativeElement, NativeRawPointer } from '../../core/Component';
import { QRect } from '../../..';
export class QPaintEvent {
native: NativeElement;
constructor(event: NativeRawPointer<'QEvent'>) {
this.native = new addon.QPaintEvent(event);
}
rect(): QRect {
return this.native.rect();
}
}

View File

@ -63,6 +63,9 @@ export class QFont extends Component {
italic(): boolean {
return this.native.italic();
}
setStyleName(style: string): void {
this.native.setStyleName(style);
}
toString(): string {
return this.native.toString();
}

View File

@ -51,9 +51,15 @@ export class QFontDatabase extends Component {
bold(family: string, style: string): boolean {
return this.native.bold(family, style);
}
isFixedPitch(family: string, style: string | null = null): boolean {
return this.native.isFixedPitch(family, style);
}
italic(family: string, style: string): boolean {
return this.native.italic(family, style);
}
styles(family: string): string[] {
return this.native.styles(family);
}
weight(family: string, style: string): number {
return this.native.weight(family, style);
}

View File

@ -61,7 +61,9 @@ 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);
}
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);
}
drawText(x: number, y: number, text: string): void {
return this.native.drawText(x, y, text);
}

View File

@ -2,7 +2,7 @@ import addon from '../utils/addon';
import { NodeLayout } from './QLayout';
import { NativeElement } from '../core/Component';
import { FlexLayout } from '../core/FlexLayout';
import { WidgetAttribute, WindowType, ContextMenuPolicy, FocusReason } from '../QtEnums';
import { WidgetAttribute, WindowType, ContextMenuPolicy, FocusReason, FocusPolicy } from '../QtEnums';
import { QIcon } from '../QtGui/QIcon';
import { QCursor } from '../QtGui/QCursor';
import { CursorShape, WindowState } from '../QtEnums';
@ -236,6 +236,9 @@ export abstract class NodeWidget<Signals extends QWidgetSignals> extends YogaWid
setContextMenuPolicy(contextMenuPolicy: ContextMenuPolicy): void {
this.setProperty('contextMenuPolicy', contextMenuPolicy);
}
setFocusPolicy(policy: FocusPolicy): void {
this.setProperty('focusPolicy', policy);
}
showFullScreen(): void {
this.native.showFullScreen();
}