diff --git a/CMakeLists.txt b/CMakeLists.txt index 5233ec216..bc2802e54 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,6 +44,7 @@ add_library(${CORE_WIDGETS_ADDON} SHARED "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtGui/QKeySequence/qkeysequence_wrap.cpp" "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtCore/QObject/qobject_wrap.cpp" "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtCore/QVariant/qvariant_wrap.cpp" + "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtCore/QSize/qsize_wrap.cpp" "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QWidget/qwidget_wrap.cpp" "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QGridLayout/qgridlayout_wrap.cpp" "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QDial/qdial_wrap.cpp" diff --git a/src/cpp/include/nodegui/QtCore/QSize/qsize_wrap.h b/src/cpp/include/nodegui/QtCore/QSize/qsize_wrap.h new file mode 100644 index 000000000..e69927d62 --- /dev/null +++ b/src/cpp/include/nodegui/QtCore/QSize/qsize_wrap.h @@ -0,0 +1,31 @@ +#pragma once + +#include +#include + +#include + +#include "core/Component/component_macro.h" + +class QSizeWrap : public Napi::ObjectWrap { + private: + std::unique_ptr instance; + + public: + static Napi::FunctionReference constructor; + static Napi::Object init(Napi::Env env, Napi::Object exports); + QSizeWrap(const Napi::CallbackInfo& info); + ~QSizeWrap(); + QSize* getInternalInstance(); + // Wrapped methods + Napi::Value setHeight(const Napi::CallbackInfo& info); + Napi::Value setWidth(const Napi::CallbackInfo& info); + Napi::Value height(const Napi::CallbackInfo& info); + Napi::Value width(const Napi::CallbackInfo& info); + + COMPONENT_WRAPPED_METHODS_DECLARATION +}; + +namespace StaticQSizeWrapMethods { +Napi::Value fromQVariant(const Napi::CallbackInfo& info); +} // namespace StaticQSizeWrapMethods \ No newline at end of file diff --git a/src/cpp/include/nodegui/QtWidgets/QWidget/qwidget_macro.h b/src/cpp/include/nodegui/QtWidgets/QWidget/qwidget_macro.h index f942bc839..f7523b2d8 100644 --- a/src/cpp/include/nodegui/QtWidgets/QWidget/qwidget_macro.h +++ b/src/cpp/include/nodegui/QtWidgets/QWidget/qwidget_macro.h @@ -3,6 +3,7 @@ #include #include "QtCore/QObject/qobject_macro.h" +#include "QtCore/QSize/qsize_wrap.h" #include "QtGui/QIcon/qicon_wrap.h" #include "QtWidgets/QLayout/qlayout_wrap.h" #include "core/YogaWidget/yogawidget_macro.h" @@ -14,254 +15,253 @@ */ #ifndef QWIDGET_WRAPPED_METHODS_DECLARATION -#define QWIDGET_WRAPPED_METHODS_DECLARATION \ - \ - YOGAWIDGET_WRAPPED_METHODS_DECLARATION \ - QOBJECT_WRAPPED_METHODS_DECLARATION \ - \ - Napi::Value show(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - this->instance->show(); \ - return env.Null(); \ - } \ - Napi::Value resize(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - Napi::Number width = info[0].As(); \ - Napi::Number height = info[1].As(); \ - this->instance->resize(width.Int32Value(), height.Int32Value()); \ - return env.Null(); \ - } \ - Napi::Value close(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - bool hasClosed = this->instance->close(); \ - return Napi::Boolean::New(env, hasClosed); \ - } \ - Napi::Value isVisible(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - bool isVisible = this->instance->isVisible(); \ - return Napi::Boolean::New(env, isVisible); \ - } \ - \ - Napi::Value setLayout(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - Napi::Object layoutObject = info[0].As(); \ - QLayoutWrap* layoutWrap = \ - Napi::ObjectWrap::Unwrap(layoutObject); \ - this->instance->setLayout(layoutWrap->getInternalInstance()); \ - return env.Null(); \ - } \ - \ - Napi::Value setStyleSheet(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - Napi::String text = info[0].As(); \ - std::string style = text.Utf8Value(); \ - this->instance->setStyleSheet(style.c_str()); \ - return env.Null(); \ - } \ - Napi::Value setCursor(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - Napi::Number cursor = info[0].As(); \ - this->instance->setCursor( \ - static_cast(cursor.Int32Value())); \ - return env.Null(); \ - } \ - Napi::Value setWindowIcon(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - Napi::Object iconObject = info[0].As(); \ - QIconWrap* iconWrap = Napi::ObjectWrap::Unwrap(iconObject); \ - this->instance->setWindowIcon(*iconWrap->getInternalInstance()); \ - return env.Null(); \ - } \ - Napi::Value setWindowState(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - Napi::Number state = info[0].As(); \ - this->instance->setWindowState( \ - static_cast(state.Int32Value())); \ - return env.Null(); \ - } \ - Napi::Value setWindowTitle(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - Napi::String napiTitle = info[0].As(); \ - std::string title = napiTitle.Utf8Value(); \ - this->instance->setWindowTitle(title.c_str()); \ - return env.Null(); \ - } \ - Napi::Value styleSheet(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - QString stylesheet = this->instance->styleSheet(); \ - return Napi::String::New(env, stylesheet.toStdString()); \ - } \ - Napi::Value hide(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - this->instance->hide(); \ - return env.Null(); \ - } \ - Napi::Value move(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - int x = info[0].As().Int32Value(); \ - int y = info[1].As().Int32Value(); \ - this->instance->move(x, y); \ - return env.Null(); \ - } \ - \ - Napi::Value setMouseTracking(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - Napi::Boolean isMouseTracked = info[0].As(); \ - this->instance->setMouseTracking(isMouseTracked.Value()); \ - return env.Null(); \ - } \ - Napi::Value hasMouseTracking(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - bool isMouseTracked = this->instance->hasMouseTracking(); \ - return Napi::Value::From(env, isMouseTracked); \ - } \ - Napi::Value setEnabled(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - Napi::Boolean enabled = info[0].As(); \ - this->instance->setEnabled(enabled.Value()); \ - return env.Null(); \ - } \ - Napi::Value isEnabled(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - bool enabled = this->instance->isEnabled(); \ - return Napi::Value::From(env, enabled); \ - } \ - Napi::Value setFixedSize(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - int width = info[0].As().Int32Value(); \ - int height = info[1].As().Int32Value(); \ - this->instance->setFixedSize(width, height); \ - return env.Null(); \ - } \ - Napi::Value setGeometry(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - int x = info[0].As().Int32Value(); \ - int y = info[1].As().Int32Value(); \ - int width = info[2].As().Int32Value(); \ - int height = info[3].As().Int32Value(); \ - this->instance->setGeometry(x, y, width, height); \ - return env.Null(); \ - } \ - Napi::Value geometry(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - QRect geometry = this->instance->geometry(); \ - Napi::Object geometryObj = Napi::Object::New(env); \ - geometryObj.Set("width", geometry.width()); \ - geometryObj.Set("height", geometry.height()); \ - geometryObj.Set("x", geometry.x()); \ - geometryObj.Set("y", geometry.y()); \ - return geometryObj; \ - } \ - Napi::Value setMaximumSize(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - int width = info[0].As().Int32Value(); \ - int height = info[1].As().Int32Value(); \ - this->instance->setMaximumSize(width, height); \ - return env.Null(); \ - } \ - Napi::Value setMinimumSize(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - int width = info[0].As().Int32Value(); \ - int height = info[1].As().Int32Value(); \ - this->instance->setMinimumSize(width, height); \ - return env.Null(); \ - } \ - Napi::Value repaint(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - this->instance->repaint(); \ - return env.Null(); \ - } \ - Napi::Value update(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - this->instance->update(); \ - return env.Null(); \ - } \ - Napi::Value updateGeometry(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - this->instance->updateGeometry(); \ - return env.Null(); \ - } \ - Napi::Value pos(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - QPoint pos = this->instance->pos(); \ - Napi::Object posObj = Napi::Object::New(env); \ - posObj.Set("x", pos.x()); \ - posObj.Set("y", pos.y()); \ - return posObj; \ - } \ - Napi::Value size(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - QSize size = this->instance->size(); \ - Napi::Object sizeObj = Napi::Object::New(env); \ - sizeObj.Set("width", size.width()); \ - sizeObj.Set("height", size.height()); \ - return sizeObj; \ - } \ - Napi::Value setAttribute(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - int attributeId = info[0].As().Int32Value(); \ - bool switchOn = info[1].As().Value(); \ - this->instance->setAttribute( \ - static_cast(attributeId), switchOn); \ - return env.Null(); \ - } \ - Napi::Value testAttribute(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - int attributeId = info[0].As().Int32Value(); \ - bool isOn = this->instance->testAttribute( \ - static_cast(attributeId)); \ - return Napi::Boolean::New(env, isOn); \ - } \ - Napi::Value setWindowOpacity(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - float opacity = info[0].As().FloatValue(); \ - this->instance->setWindowOpacity(opacity); \ - return env.Null(); \ - } \ - Napi::Value windowOpacity(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - float opacity = this->instance->windowOpacity(); \ - return Napi::Value::From(env, opacity); \ - } \ - Napi::Value setWindowFlag(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - int windowType = info[0].As().Int32Value(); \ - bool switchOn = info[1].As().Value(); \ - this->instance->setWindowFlag(static_cast(windowType), \ - switchOn); \ - return env.Null(); \ +#define QWIDGET_WRAPPED_METHODS_DECLARATION \ + \ + YOGAWIDGET_WRAPPED_METHODS_DECLARATION \ + QOBJECT_WRAPPED_METHODS_DECLARATION \ + \ + Napi::Value show(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + this->instance->show(); \ + return env.Null(); \ + } \ + Napi::Value resize(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + Napi::Number width = info[0].As(); \ + Napi::Number height = info[1].As(); \ + this->instance->resize(width.Int32Value(), height.Int32Value()); \ + return env.Null(); \ + } \ + Napi::Value close(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + bool hasClosed = this->instance->close(); \ + return Napi::Boolean::New(env, hasClosed); \ + } \ + Napi::Value isVisible(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + bool isVisible = this->instance->isVisible(); \ + return Napi::Boolean::New(env, isVisible); \ + } \ + \ + Napi::Value setLayout(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + Napi::Object layoutObject = info[0].As(); \ + QLayoutWrap* layoutWrap = \ + Napi::ObjectWrap::Unwrap(layoutObject); \ + this->instance->setLayout(layoutWrap->getInternalInstance()); \ + return env.Null(); \ + } \ + \ + Napi::Value setStyleSheet(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + Napi::String text = info[0].As(); \ + std::string style = text.Utf8Value(); \ + this->instance->setStyleSheet(style.c_str()); \ + return env.Null(); \ + } \ + Napi::Value setCursor(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + Napi::Number cursor = info[0].As(); \ + this->instance->setCursor( \ + static_cast(cursor.Int32Value())); \ + return env.Null(); \ + } \ + Napi::Value setWindowIcon(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + Napi::Object iconObject = info[0].As(); \ + QIconWrap* iconWrap = Napi::ObjectWrap::Unwrap(iconObject); \ + this->instance->setWindowIcon(*iconWrap->getInternalInstance()); \ + return env.Null(); \ + } \ + Napi::Value setWindowState(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + Napi::Number state = info[0].As(); \ + this->instance->setWindowState( \ + static_cast(state.Int32Value())); \ + return env.Null(); \ + } \ + Napi::Value setWindowTitle(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + Napi::String napiTitle = info[0].As(); \ + std::string title = napiTitle.Utf8Value(); \ + this->instance->setWindowTitle(title.c_str()); \ + return env.Null(); \ + } \ + Napi::Value styleSheet(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + QString stylesheet = this->instance->styleSheet(); \ + return Napi::String::New(env, stylesheet.toStdString()); \ + } \ + Napi::Value hide(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + this->instance->hide(); \ + return env.Null(); \ + } \ + Napi::Value move(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + int x = info[0].As().Int32Value(); \ + int y = info[1].As().Int32Value(); \ + this->instance->move(x, y); \ + return env.Null(); \ + } \ + \ + Napi::Value setMouseTracking(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + Napi::Boolean isMouseTracked = info[0].As(); \ + this->instance->setMouseTracking(isMouseTracked.Value()); \ + return env.Null(); \ + } \ + Napi::Value hasMouseTracking(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + bool isMouseTracked = this->instance->hasMouseTracking(); \ + return Napi::Value::From(env, isMouseTracked); \ + } \ + Napi::Value setEnabled(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + Napi::Boolean enabled = info[0].As(); \ + this->instance->setEnabled(enabled.Value()); \ + return env.Null(); \ + } \ + Napi::Value isEnabled(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + bool enabled = this->instance->isEnabled(); \ + return Napi::Value::From(env, enabled); \ + } \ + Napi::Value setFixedSize(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + int width = info[0].As().Int32Value(); \ + int height = info[1].As().Int32Value(); \ + this->instance->setFixedSize(width, height); \ + return env.Null(); \ + } \ + Napi::Value setGeometry(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + int x = info[0].As().Int32Value(); \ + int y = info[1].As().Int32Value(); \ + int width = info[2].As().Int32Value(); \ + int height = info[3].As().Int32Value(); \ + this->instance->setGeometry(x, y, width, height); \ + return env.Null(); \ + } \ + Napi::Value geometry(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + QRect geometry = this->instance->geometry(); \ + Napi::Object geometryObj = Napi::Object::New(env); \ + geometryObj.Set("width", geometry.width()); \ + geometryObj.Set("height", geometry.height()); \ + geometryObj.Set("x", geometry.x()); \ + geometryObj.Set("y", geometry.y()); \ + return geometryObj; \ + } \ + Napi::Value setMaximumSize(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + int width = info[0].As().Int32Value(); \ + int height = info[1].As().Int32Value(); \ + this->instance->setMaximumSize(width, height); \ + return env.Null(); \ + } \ + Napi::Value setMinimumSize(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + int width = info[0].As().Int32Value(); \ + int height = info[1].As().Int32Value(); \ + this->instance->setMinimumSize(width, height); \ + return env.Null(); \ + } \ + Napi::Value repaint(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + this->instance->repaint(); \ + return env.Null(); \ + } \ + Napi::Value update(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + this->instance->update(); \ + return env.Null(); \ + } \ + Napi::Value updateGeometry(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + this->instance->updateGeometry(); \ + return env.Null(); \ + } \ + Napi::Value pos(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + QPoint pos = this->instance->pos(); \ + Napi::Object posObj = Napi::Object::New(env); \ + posObj.Set("x", pos.x()); \ + posObj.Set("y", pos.y()); \ + return posObj; \ + } \ + Napi::Value size(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + QSize size = this->instance->size(); \ + auto sizeWrap = QSizeWrap::constructor.New({Napi::External::New( \ + env, new QSize(size.width(), size.height()))}); \ + return sizeWrap; \ + } \ + Napi::Value setAttribute(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + int attributeId = info[0].As().Int32Value(); \ + bool switchOn = info[1].As().Value(); \ + this->instance->setAttribute( \ + static_cast(attributeId), switchOn); \ + return env.Null(); \ + } \ + Napi::Value testAttribute(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + int attributeId = info[0].As().Int32Value(); \ + bool isOn = this->instance->testAttribute( \ + static_cast(attributeId)); \ + return Napi::Boolean::New(env, isOn); \ + } \ + Napi::Value setWindowOpacity(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + float opacity = info[0].As().FloatValue(); \ + this->instance->setWindowOpacity(opacity); \ + return env.Null(); \ + } \ + Napi::Value windowOpacity(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + float opacity = this->instance->windowOpacity(); \ + return Napi::Value::From(env, opacity); \ + } \ + Napi::Value setWindowFlag(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + int windowType = info[0].As().Int32Value(); \ + bool switchOn = info[1].As().Value(); \ + this->instance->setWindowFlag(static_cast(windowType), \ + switchOn); \ + return env.Null(); \ } #endif // QWIDGET_WRAPPED_METHODS_DECLARATION diff --git a/src/cpp/lib/QtCore/QSize/qsize_wrap.cpp b/src/cpp/lib/QtCore/QSize/qsize_wrap.cpp new file mode 100644 index 000000000..27fa4b637 --- /dev/null +++ b/src/cpp/lib/QtCore/QSize/qsize_wrap.cpp @@ -0,0 +1,86 @@ +#include "QtCore/QSize/qsize_wrap.h" + +#include "Extras/Utils/nutils.h" +#include "QtCore/QVariant/qvariant_wrap.h" + +Napi::FunctionReference QSizeWrap::constructor; + +Napi::Object QSizeWrap::init(Napi::Env env, Napi::Object exports) { + Napi::HandleScope scope(env); + char CLASSNAME[] = "QSize"; + Napi::Function func = DefineClass( + env, CLASSNAME, + {InstanceMethod("setHeight", &QSizeWrap::setHeight), + InstanceMethod("setWidth", &QSizeWrap::setWidth), + InstanceMethod("height", &QSizeWrap::height), + InstanceMethod("width", &QSizeWrap::width), + StaticMethod("fromQVariant", &StaticQSizeWrapMethods::fromQVariant), + COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE}); + constructor = Napi::Persistent(func); + exports.Set(CLASSNAME, func); + return exports; +} + +QSizeWrap::QSizeWrap(const Napi::CallbackInfo& info) + : Napi::ObjectWrap(info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + + if (info.Length() == 2) { + int width = info[0].As().Int32Value(); + int height = info[1].As().Int32Value(); + this->instance = std::make_unique(width, height); + } 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 = this->getInternalInstance(); +} + +QSizeWrap::~QSizeWrap() { this->instance.reset(); } + +QSize* QSizeWrap::getInternalInstance() { return this->instance.get(); } + +Napi::Value QSizeWrap::setHeight(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + int height = info[0].As().Int32Value(); + this->instance->setHeight(height); + return env.Null(); +} +Napi::Value QSizeWrap::setWidth(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + int width = info[0].As().Int32Value(); + this->instance->setWidth(width); + return env.Null(); +} +Napi::Value QSizeWrap::height(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + return Napi::Value::From(env, this->instance->height()); +} +Napi::Value QSizeWrap::width(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + return Napi::Value::From(env, this->instance->width()); +} + +Napi::Value StaticQSizeWrapMethods::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(); + QSize size = variant->value(); + auto instance = QSizeWrap::constructor.New({Napi::External::New( + env, new QSize(size.width(), size.height()))}); + return instance; +} diff --git a/src/cpp/main.cpp b/src/cpp/main.cpp index 398046b91..8786ab8f9 100644 --- a/src/cpp/main.cpp +++ b/src/cpp/main.cpp @@ -1,6 +1,7 @@ #include #include "QtCore/QObject/qobject_wrap.h" +#include "QtCore/QSize/qsize_wrap.h" #include "QtCore/QVariant/qvariant_wrap.h" #include "QtGui/QApplication/qapplication_wrap.h" #include "QtGui/QClipboard/qclipboard_wrap.h" @@ -40,6 +41,7 @@ Napi::Object Main(Napi::Env env, Napi::Object exports) { QApplicationWrap::init(env, exports); QObjectWrap::init(env, exports); QVariantWrap::init(env, exports); + QSizeWrap::init(env, exports); QClipboardWrap::init(env, exports); QWidgetWrap::init(env, exports); QPixmapWrap::init(env, exports); diff --git a/src/index.ts b/src/index.ts index 2de27289d..f9a476851 100644 --- a/src/index.ts +++ b/src/index.ts @@ -40,6 +40,7 @@ export { QShortcut, QShortcutEvents } from './lib/QtWidgets/QShortcut'; // Core export { QObject, NodeObject } from './lib/QtCore/QObject'; export { QVariant } from './lib/QtCore/QVariant'; +export { QSize } from './lib/QtCore/QSize'; // Layouts: export { QGridLayout } from './lib/QtWidgets/QGridLayout'; export { FlexLayout } from './lib/core/FlexLayout'; diff --git a/src/lib/QtCore/QSize.ts b/src/lib/QtCore/QSize.ts new file mode 100644 index 000000000..70a026738 --- /dev/null +++ b/src/lib/QtCore/QSize.ts @@ -0,0 +1,35 @@ +import { NativeElement, Component } from '../core/Component'; +import addon from '../utils/addon'; +import { checkIfNativeElement } from '../utils/helpers'; +import { QVariant } from './QVariant'; + +type argument = number | NativeElement; + +export class QSize extends Component { + native: NativeElement; + constructor(arg?: argument, height?: number) { + super(); + if (!arg) { + this.native = new addon.QSize(); + } else if (checkIfNativeElement(arg)) { + this.native = arg as NativeElement; + } else { + this.native = new addon.QSize(arg, height); + } + } + setWidth(width: number): void { + return this.native.setWidth(width); + } + width(): number { + return this.native.width(); + } + setHeight(height: number): void { + return this.native.setHeight(height); + } + height(): number { + return this.native.height(); + } + static fromQVariant(variant: QVariant): QSize { + return addon.QSize.fromQVariant(variant.native); + } +} diff --git a/src/lib/QtCore/__tests__/QSize.test.ts b/src/lib/QtCore/__tests__/QSize.test.ts new file mode 100644 index 000000000..7adbdd3ec --- /dev/null +++ b/src/lib/QtCore/__tests__/QSize.test.ts @@ -0,0 +1,29 @@ +import { QSize } from '../QSize'; +import { QVariant } from '../QVariant'; + +describe('QSize', () => { + it('initialize empty', () => { + const size = new QSize(); + expect(size).toBeTruthy(); + }); + it('initialize with width and height', () => { + const size = new QSize(300, 200); + expect(size).toBeTruthy(); + }); + it('width', () => { + const size = new QSize(); + size.setWidth(300); + expect(size.width()).toBe(300); + }); + it('height', () => { + const size = new QSize(); + size.setHeight(200); + expect(size.height()).toBe(200); + }); + it('initialize from QVariant', () => { + const size = new QSize(300, 200); + const variant = new QVariant(size); + expect(variant).toBeTruthy(); + expect(QSize.fromQVariant(variant).height()).toBe(size.height()); + }); +}); diff --git a/src/lib/QtCore/__tests__/QVariant.test.ts b/src/lib/QtCore/__tests__/QVariant.test.ts index 6addaf2f3..78b0f8ce3 100644 --- a/src/lib/QtCore/__tests__/QVariant.test.ts +++ b/src/lib/QtCore/__tests__/QVariant.test.ts @@ -3,38 +3,34 @@ import { QPixmap } from '../../QtGui/QPixmap'; import path from 'path'; describe('QVariant', () => { - it('inherits from QVariant', () => { - const variant = new QVariant(); - expect(variant.constructor.name).toEqual('QVariant'); - }); it('initialize empty', () => { const variant = new QVariant(); - expect(variant.constructor.name).toEqual('QVariant'); + expect(variant).toBeTruthy(); }); it('initialize with string', () => { const variant = new QVariant('hello'); - expect(variant.constructor.name).toBe('QVariant'); + expect(variant).toBeTruthy(); expect(variant.toString()).toEqual('hello'); }); it('initialize with int', () => { const variant = new QVariant(123); - expect(variant.constructor.name).toBe('QVariant'); + expect(variant).toBeTruthy(); expect(variant.toInt()).toEqual(123); }); it('initialize with double', () => { const variant = new QVariant(12.33); - expect(variant.constructor.name).toBe('QVariant'); + expect(variant).toBeTruthy(); expect(variant.toDouble()).toEqual(12.33); }); it('initialize with boolean', () => { const variant = new QVariant(true); - expect(variant.constructor.name).toBe('QVariant'); + expect(variant).toBeTruthy(); expect(variant.toBool()).toEqual(true); }); it('initialize with complex objects', () => { const pixmap = new QPixmap(path.resolve(__dirname, 'nodegui.png')); const variant = new QVariant(pixmap); - expect(variant.constructor.name).toBe('QVariant'); + expect(variant).toBeTruthy(); expect(QPixmap.fromQVariant(variant).height()).toBe(pixmap.height()); }); }); diff --git a/src/lib/QtGui/QPixmap.ts b/src/lib/QtGui/QPixmap.ts index 54a3a9a65..63382e1b1 100644 --- a/src/lib/QtGui/QPixmap.ts +++ b/src/lib/QtGui/QPixmap.ts @@ -21,14 +21,13 @@ export class QPixmap extends Component { this.native = new addon.QPixmap(); } } - load = (imageUrl: string): boolean => { + load(imageUrl: string): boolean { return this.native.load(imageUrl); - }; - save = (fileName: string, format?: ReadWriteImageFormats): boolean => { - //TODO: quality argument + } + save(fileName: string, format?: ReadWriteImageFormats): boolean { return format ? this.native.save(fileName, format) : this.native.save(fileName); - }; - scaled = (width: number, height: number, aspectRatioMode?: AspectRatioMode): QPixmap => { + } + scaled(width: number, height: number, aspectRatioMode?: AspectRatioMode): QPixmap { let nativePixmap; if (aspectRatioMode) { nativePixmap = this.native.scaled(width, height, aspectRatioMode); @@ -36,7 +35,7 @@ export class QPixmap extends Component { nativePixmap = this.native.scaled(width, height); } return new QPixmap(nativePixmap); - }; + } height(): number { return this.native.height(); } diff --git a/src/lib/QtGui/__tests__/QPixmap.test.ts b/src/lib/QtGui/__tests__/QPixmap.test.ts new file mode 100644 index 000000000..68596bd9d --- /dev/null +++ b/src/lib/QtGui/__tests__/QPixmap.test.ts @@ -0,0 +1,66 @@ +import { QPixmap } from '../QPixmap'; +import path from 'path'; +import { QVariant } from '../../QtCore/QVariant'; +import { AspectRatioMode } from '../../QtEnums'; +import fs from 'fs'; + +const testImagePath = path.resolve(__dirname, 'assets', 'nodegui.png'); +describe('QPixmap', () => { + it('initialize empty', () => { + const pixmap = new QPixmap(); + expect(pixmap).toBeTruthy(); + }); + it('initialize with string', () => { + const pixmap = new QPixmap(testImagePath); + expect(pixmap).toBeTruthy(); + }); + it('width', () => { + const pixmap = new QPixmap(testImagePath); + expect(pixmap.width()).toBe(1000); + }); + it('height', () => { + const pixmap = new QPixmap(testImagePath); + expect(pixmap.height()).toBe(1083); + }); + it('initialize from QVariant', () => { + const pixmap = new QPixmap(testImagePath); + const variant = new QVariant(pixmap); + expect(variant).toBeTruthy(); + expect(QPixmap.fromQVariant(variant).height()).toBe(pixmap.height()); + }); + it('scaled without aspect ratio', () => { + const pixmap = new QPixmap(testImagePath); + const newPixmap = pixmap.scaled(300, 340, AspectRatioMode.IgnoreAspectRatio); + expect(newPixmap.width()).toBe(300); + expect(newPixmap.height()).toBe(340); + }); + it('scaled with aspect ratio', () => { + const pixmap = new QPixmap(testImagePath); + const newPixmap = pixmap.scaled(100, 100, AspectRatioMode.KeepAspectRatio); + expect(newPixmap.width()).toBe(92); + expect(newPixmap.height()).toBe(100); + }); + it('load from file', () => { + const pixmap = new QPixmap(); + expect(pixmap.height()).toBe(0); + const isLoaded = pixmap.load(testImagePath); + expect(isLoaded).toBe(true); + expect(pixmap.height()).toBe(1083); + }); + it('save to a file', async () => { + const outputFilePath = path.resolve(__dirname, 'assets', 'nodegui_save.png'); + await new Promise(resolve => fs.unlink(outputFilePath, resolve)); + const pixmap = new QPixmap(testImagePath); + pixmap.save(outputFilePath); + const exists = await new Promise(resolve => fs.exists(outputFilePath, resolve)); + expect(exists).toBe(true); + }); + it('save to a file of different format', async () => { + const outputFilePath = path.resolve(__dirname, 'assets', 'nodegui_save.jpg'); + await new Promise(resolve => fs.unlink(outputFilePath, resolve)); + const pixmap = new QPixmap(testImagePath); + pixmap.save(outputFilePath, 'JPG'); + const exists = await new Promise(resolve => fs.exists(outputFilePath, resolve)); + expect(exists).toBe(true); + }); +}); diff --git a/src/lib/QtGui/__tests__/assets/.gitignore b/src/lib/QtGui/__tests__/assets/.gitignore new file mode 100644 index 000000000..3df6667dd --- /dev/null +++ b/src/lib/QtGui/__tests__/assets/.gitignore @@ -0,0 +1,2 @@ +nodegui_save.png +nodegui_save.jpg \ No newline at end of file diff --git a/src/lib/QtGui/__tests__/assets/nodegui.png b/src/lib/QtGui/__tests__/assets/nodegui.png new file mode 100644 index 000000000..79d6b33a1 Binary files /dev/null and b/src/lib/QtGui/__tests__/assets/nodegui.png differ diff --git a/src/lib/QtWidgets/QWidget.ts b/src/lib/QtWidgets/QWidget.ts index a3d2cc05f..bac276cac 100644 --- a/src/lib/QtWidgets/QWidget.ts +++ b/src/lib/QtWidgets/QWidget.ts @@ -10,6 +10,7 @@ import { CursorShape, WindowState } from '../QtEnums'; import { applyStyleSheet, StyleSheet, prepareInlineStyleSheet } from '../core/Style/StyleSheet'; import { checkIfNativeElement } from '../utils/helpers'; import { YogaWidget } from '../core/YogaWidget'; +import { QSize } from '../QtCore/QSize'; // All Widgets should extend from NodeWidget // Implement all native QWidget methods here so that all widgets get access to those aswell export abstract class NodeWidget extends YogaWidget { @@ -90,8 +91,8 @@ export abstract class NodeWidget extends YogaWidget { resize(width: number, height: number): void { this.native.resize(width, height); } - size(): { width: number; height: number } { - return this.native.size(); + size(): QSize { + return new QSize(this.native.size()); } move(x: number, y: number): void { this.native.move(x, y); diff --git a/src/lib/QtWidgets/__tests__/QWidget.test.ts b/src/lib/QtWidgets/__tests__/QWidget.test.ts index ebd840eb5..2441dbd12 100644 --- a/src/lib/QtWidgets/__tests__/QWidget.test.ts +++ b/src/lib/QtWidgets/__tests__/QWidget.test.ts @@ -64,7 +64,9 @@ describe('QWidget', () => { }); it('setFixedSize', () => { view.setFixedSize(10, 12); - expect(view.size()).toEqual({ width: 10, height: 12 }); + const size = view.size(); + expect(size.width()).toBe(10); + expect(size.height()).toBe(12); }); it('setWindowOpacity', () => { view.setWindowOpacity(0.6);