Add QBrush (#468)
* Add QDoubleSpinBox * Add QBrush Co-authored-by: wuxiaofeng <wuxiaofeng@erayt.com>
This commit is contained in:
parent
05ba79d185
commit
5e241f43b0
@ -38,6 +38,7 @@ add_library(${CORE_WIDGETS_ADDON} SHARED
|
||||
"${PROJECT_SOURCE_DIR}/src/cpp/include/deps/yoga/event/event.cpp"
|
||||
"${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/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"
|
||||
|
||||
33
src/cpp/include/nodegui/QtGui/QBrush/qbrush_wrap.h
Normal file
33
src/cpp/include/nodegui/QtGui/QBrush/qbrush_wrap.h
Normal file
@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
#include <napi.h>
|
||||
|
||||
#include <QBrush>
|
||||
|
||||
#include "Extras/Export/export.h"
|
||||
#include "core/Component/component_macro.h"
|
||||
|
||||
class DLL_EXPORT QBrushWrap : public Napi::ObjectWrap<QBrushWrap> {
|
||||
COMPONENT_WRAPPED_METHODS_DECLARATION
|
||||
private:
|
||||
std::unique_ptr<QBrush> instance;
|
||||
|
||||
public:
|
||||
static Napi::FunctionReference constructor;
|
||||
static Napi::Object init(Napi::Env env, Napi::Object exports);
|
||||
QBrushWrap(const Napi::CallbackInfo& info);
|
||||
~QBrushWrap();
|
||||
QBrush* getInternalInstance();
|
||||
// Wrapped methods
|
||||
Napi::Value isOpaque(const Napi::CallbackInfo& info);
|
||||
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 setTexture(const Napi::CallbackInfo& info);
|
||||
Napi::Value texture(const Napi::CallbackInfo& info);
|
||||
};
|
||||
|
||||
namespace StaticQBrushWrapMethods {
|
||||
Napi::Value fromQVariant(const Napi::CallbackInfo& info);
|
||||
} // namespace StaticQBrushWrapMethods
|
||||
149
src/cpp/lib/QtGui/QBrush/qbrush_wrap.cpp
Normal file
149
src/cpp/lib/QtGui/QBrush/qbrush_wrap.cpp
Normal file
@ -0,0 +1,149 @@
|
||||
#include "QtGui/QBrush/qbrush_wrap.h"
|
||||
|
||||
#include "Extras/Utils/nutils.h"
|
||||
#include "QtCore/QVariant/qvariant_wrap.h"
|
||||
#include "QtGui/QColor/qcolor_wrap.h"
|
||||
#include "QtGui/QPixmap/qpixmap_wrap.h"
|
||||
|
||||
Napi::FunctionReference QBrushWrap::constructor;
|
||||
|
||||
Napi::Object QBrushWrap::init(Napi::Env env, Napi::Object exports) {
|
||||
Napi::HandleScope scope(env);
|
||||
char CLASSNAME[] = "QBrush";
|
||||
Napi::Function func = DefineClass(
|
||||
env, CLASSNAME,
|
||||
{InstanceMethod("isOpaque", &QBrushWrap::isOpaque),
|
||||
InstanceMethod("setColor", &QBrushWrap::setColor),
|
||||
InstanceMethod("color", &QBrushWrap::color),
|
||||
InstanceMethod("setStyle", &QBrushWrap::setStyle),
|
||||
InstanceMethod("style", &QBrushWrap::style),
|
||||
InstanceMethod("setTexture", &QBrushWrap::setTexture),
|
||||
InstanceMethod("texture", &QBrushWrap::texture),
|
||||
StaticMethod("fromQVariant", &StaticQBrushWrapMethods::fromQVariant),
|
||||
COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE(QBrushWrap)});
|
||||
constructor = Napi::Persistent(func);
|
||||
exports.Set(CLASSNAME, func);
|
||||
return exports;
|
||||
}
|
||||
|
||||
QBrushWrap::QBrushWrap(const Napi::CallbackInfo& info)
|
||||
: Napi::ObjectWrap<QBrushWrap>(info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
|
||||
if (info.Length() == 2) {
|
||||
if (info[0].IsNumber()) {
|
||||
Qt::GlobalColor color =
|
||||
(Qt::GlobalColor)info[0].As<Napi::Number>().Int32Value();
|
||||
Qt::BrushStyle style =
|
||||
(Qt::BrushStyle)info[1].As<Napi::Number>().Int32Value();
|
||||
this->instance = std::make_unique<QBrush>(color, style);
|
||||
} else {
|
||||
Napi::Object colorObject = info[0].As<Napi::Object>();
|
||||
QColorWrap* colorWrap = Napi::ObjectWrap<QColorWrap>::Unwrap(colorObject);
|
||||
Qt::BrushStyle style =
|
||||
(Qt::BrushStyle)info[1].As<Napi::Number>().Int32Value();
|
||||
this->instance =
|
||||
std::make_unique<QBrush>(*colorWrap->getInternalInstance(), style);
|
||||
}
|
||||
} else if (info.Length() == 1) {
|
||||
this->instance =
|
||||
std::unique_ptr<QBrush>(info[0].As<Napi::External<QBrush>>().Data());
|
||||
} else if (info.Length() == 0) {
|
||||
this->instance = std::make_unique<QBrush>();
|
||||
} else {
|
||||
Napi::TypeError::New(env, "Wrong number of arguments")
|
||||
.ThrowAsJavaScriptException();
|
||||
}
|
||||
this->rawData = extrautils::configureComponent(this->getInternalInstance());
|
||||
}
|
||||
|
||||
QBrushWrap::~QBrushWrap() { this->instance.reset(); }
|
||||
|
||||
QBrush* QBrushWrap::getInternalInstance() { return this->instance.get(); }
|
||||
|
||||
Napi::Value QBrushWrap::isOpaque(const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
|
||||
return Napi::Boolean::New(env, this->instance->isOpaque());
|
||||
}
|
||||
|
||||
Napi::Value QBrushWrap::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 QBrushWrap::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 QBrushWrap::setStyle(const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
|
||||
Qt::BrushStyle style =
|
||||
(Qt::BrushStyle)info[0].As<Napi::Number>().Int32Value();
|
||||
this->instance->setStyle(style);
|
||||
return env.Null();
|
||||
}
|
||||
|
||||
Napi::Value QBrushWrap::style(const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
|
||||
Qt::BrushStyle style = this->instance->style();
|
||||
return Napi::Number::New(env, static_cast<int>(style));
|
||||
}
|
||||
|
||||
Napi::Value QBrushWrap::setTexture(const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
|
||||
Napi::Object pixmapObject = info[0].As<Napi::Object>();
|
||||
QPixmapWrap* pixmapWrap = Napi::ObjectWrap<QPixmapWrap>::Unwrap(pixmapObject);
|
||||
this->instance->setTexture(*pixmapWrap->getInternalInstance());
|
||||
return env.Null();
|
||||
}
|
||||
|
||||
Napi::Value QBrushWrap::texture(const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
|
||||
QPixmap pixmap = this->instance->texture();
|
||||
auto instance = QPixmapWrap::constructor.New(
|
||||
{Napi::External<QPixmap>::New(env, new QPixmap(pixmap))});
|
||||
return instance;
|
||||
}
|
||||
|
||||
Napi::Value StaticQBrushWrapMethods::fromQVariant(
|
||||
const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
|
||||
Napi::Object variantObject = info[0].As<Napi::Object>();
|
||||
QVariantWrap* variantWrap =
|
||||
Napi::ObjectWrap<QVariantWrap>::Unwrap(variantObject);
|
||||
QVariant* variant = variantWrap->getInternalInstance();
|
||||
QBrush brush = variant->value<QBrush>();
|
||||
auto instance = QBrushWrap::constructor.New(
|
||||
{Napi::External<QBrush>::New(env, new QBrush(brush))});
|
||||
return instance;
|
||||
}
|
||||
@ -13,6 +13,7 @@
|
||||
#include "QtCore/QUrl/qurl_wrap.h"
|
||||
#include "QtCore/QVariant/qvariant_wrap.h"
|
||||
#include "QtGui/QApplication/qapplication_wrap.h"
|
||||
#include "QtGui/QBrush/qbrush_wrap.h"
|
||||
#include "QtGui/QClipboard/qclipboard_wrap.h"
|
||||
#include "QtGui/QColor/qcolor_wrap.h"
|
||||
#include "QtGui/QCursor/qcursor_wrap.h"
|
||||
@ -99,6 +100,7 @@ Napi::Object Main(Napi::Env env, Napi::Object exports) {
|
||||
QTimeWrap::init(env, exports);
|
||||
QUrlWrap::init(env, exports);
|
||||
QVariantWrap::init(env, exports);
|
||||
QBrushWrap::init(env, exports);
|
||||
QColorWrap::init(env, exports);
|
||||
QClipboardWrap::init(env, exports);
|
||||
QDialogWrap::init(env, exports);
|
||||
|
||||
@ -4,6 +4,7 @@ import './lib/core/bootstrap';
|
||||
export * from './lib/QtEnums';
|
||||
// Gui:
|
||||
export { QApplication } from './lib/QtGui/QApplication';
|
||||
export { QBrush } from './lib/QtGui/QBrush';
|
||||
export { QKeySequence } from './lib/QtGui/QKeySequence';
|
||||
export { QPicture } from './lib/QtGui/QPicture';
|
||||
export { QPixmap, ImageFormats } from './lib/QtGui/QPixmap';
|
||||
|
||||
71
src/lib/QtGui/QBrush.ts
Normal file
71
src/lib/QtGui/QBrush.ts
Normal file
@ -0,0 +1,71 @@
|
||||
import { Component, NativeElement } from '../core/Component';
|
||||
import addon from '../utils/addon';
|
||||
import { checkIfNativeElement } from '../utils/helpers';
|
||||
import { QVariant } from '../QtCore/QVariant';
|
||||
import { GlobalColor, BrushStyle } from '../QtEnums';
|
||||
import { QColor } from './QColor';
|
||||
import { QPixmap } from './QPixmap';
|
||||
|
||||
/**
|
||||
|
||||
> The QBrush class defines the fill pattern of shapes drawn by QPainter.
|
||||
|
||||
* **This class is a JS wrapper around Qt's [QBrush class](https://doc.qt.io/qt-5/qbrush.html)**
|
||||
|
||||
### Example
|
||||
|
||||
```javascript
|
||||
const { QBrush } = require("@nodegui/nodegui");
|
||||
|
||||
const brush = new QBrush();
|
||||
```
|
||||
*/
|
||||
export class QBrush extends Component {
|
||||
native: NativeElement;
|
||||
constructor();
|
||||
constructor(nativeElement: NativeElement);
|
||||
constructor(color: GlobalColor, style: BrushStyle);
|
||||
constructor(color: QColor, style: BrushStyle);
|
||||
constructor(arg?: NativeElement | GlobalColor | QColor, style = BrushStyle.SolidPattern) {
|
||||
super();
|
||||
if (arguments.length === 2) {
|
||||
if (typeof arg === 'number') {
|
||||
this.native = new addon.QBrush(arg, style);
|
||||
} else {
|
||||
this.native = new addon.QBrush(arg?.native, style);
|
||||
}
|
||||
} else if (checkIfNativeElement(arg)) {
|
||||
this.native = arg as NativeElement;
|
||||
} else {
|
||||
this.native = new addon.QBrush();
|
||||
}
|
||||
}
|
||||
isOpaque(): boolean {
|
||||
return this.native.isOpaque();
|
||||
}
|
||||
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: BrushStyle): void {
|
||||
this.native.setStyle(style);
|
||||
}
|
||||
style(): BrushStyle {
|
||||
return this.native.style();
|
||||
}
|
||||
setTexture(pixmap: QPixmap): void {
|
||||
this.native.setTexture(pixmap.native);
|
||||
}
|
||||
texture(): QPixmap {
|
||||
return new QPixmap(this.native.texture());
|
||||
}
|
||||
static fromQVariant(variant: QVariant): QBrush {
|
||||
return new QBrush(addon.QBrush.fromQVariant(variant.native));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user