diff --git a/CMakeLists.txt b/CMakeLists.txt index 60a24aa14..773385708 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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" diff --git a/src/cpp/include/nodegui/QtGui/QBrush/qbrush_wrap.h b/src/cpp/include/nodegui/QtGui/QBrush/qbrush_wrap.h new file mode 100644 index 000000000..7161c2aee --- /dev/null +++ b/src/cpp/include/nodegui/QtGui/QBrush/qbrush_wrap.h @@ -0,0 +1,33 @@ +#pragma once + +#include + +#include + +#include "Extras/Export/export.h" +#include "core/Component/component_macro.h" + +class DLL_EXPORT QBrushWrap : public Napi::ObjectWrap { + COMPONENT_WRAPPED_METHODS_DECLARATION + private: + std::unique_ptr 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 diff --git a/src/cpp/lib/QtGui/QBrush/qbrush_wrap.cpp b/src/cpp/lib/QtGui/QBrush/qbrush_wrap.cpp new file mode 100644 index 000000000..ffe6a1dbe --- /dev/null +++ b/src/cpp/lib/QtGui/QBrush/qbrush_wrap.cpp @@ -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(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().Int32Value(); + Qt::BrushStyle style = + (Qt::BrushStyle)info[1].As().Int32Value(); + this->instance = std::make_unique(color, style); + } else { + Napi::Object colorObject = info[0].As(); + QColorWrap* colorWrap = Napi::ObjectWrap::Unwrap(colorObject); + Qt::BrushStyle style = + (Qt::BrushStyle)info[1].As().Int32Value(); + this->instance = + std::make_unique(*colorWrap->getInternalInstance(), style); + } + } else if (info.Length() == 1) { + this->instance = + std::unique_ptr(info[0].As>().Data()); + } else if (info.Length() == 0) { + this->instance = std::make_unique(); + } 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().Int32Value(); + this->instance->setColor(color); + } else { + Napi::Object colorObject = info[0].As(); + QColorWrap* colorWrap = Napi::ObjectWrap::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::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().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(style)); +} + +Napi::Value QBrushWrap::setTexture(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + + Napi::Object pixmapObject = info[0].As(); + QPixmapWrap* pixmapWrap = Napi::ObjectWrap::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::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(); + QVariantWrap* variantWrap = + Napi::ObjectWrap::Unwrap(variantObject); + QVariant* variant = variantWrap->getInternalInstance(); + QBrush brush = variant->value(); + auto instance = QBrushWrap::constructor.New( + {Napi::External::New(env, new QBrush(brush))}); + return instance; +} diff --git a/src/cpp/main.cpp b/src/cpp/main.cpp index 8cf29c7bc..73cea906a 100644 --- a/src/cpp/main.cpp +++ b/src/cpp/main.cpp @@ -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); diff --git a/src/index.ts b/src/index.ts index bc3809c55..7fb93d2ce 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'; diff --git a/src/lib/QtGui/QBrush.ts b/src/lib/QtGui/QBrush.ts new file mode 100644 index 000000000..8783e1cb5 --- /dev/null +++ b/src/lib/QtGui/QBrush.ts @@ -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)); + } +}