From e09333c9480fb24904538682cb654f09095d051f Mon Sep 17 00:00:00 2001 From: feng8848 <40539968+feng8848@users.noreply.github.com> Date: Tue, 31 Mar 2020 02:05:27 +0800 Subject: [PATCH] fix issue #473 (#475) * fix issue #473 * fix ts lint Co-authored-by: wuxiaofeng --- .../nodegui/QtWidgets/QWidget/qwidget_macro.h | 679 ++++++++++-------- src/cpp/lib/QtWidgets/QMenu/qmenu_wrap.cpp | 16 +- src/demo.ts | 17 + src/lib/QtGui/__tests__/QPixmap.test.ts | 8 +- src/lib/QtWidgets/QListWidget.ts | 4 +- src/lib/QtWidgets/QMenu.ts | 9 +- src/lib/QtWidgets/QPainter.ts | 2 +- src/lib/QtWidgets/QTreeWidget.ts | 8 +- src/lib/QtWidgets/QWidget.ts | 13 + 9 files changed, 418 insertions(+), 338 deletions(-) diff --git a/src/cpp/include/nodegui/QtWidgets/QWidget/qwidget_macro.h b/src/cpp/include/nodegui/QtWidgets/QWidget/qwidget_macro.h index edb6ea8a0..f26798705 100644 --- a/src/cpp/include/nodegui/QtWidgets/QWidget/qwidget_macro.h +++ b/src/cpp/include/nodegui/QtWidgets/QWidget/qwidget_macro.h @@ -4,6 +4,7 @@ #include #include "QtCore/QObject/qobject_macro.h" +#include "QtCore/QPoint/qpoint_wrap.h" #include "QtCore/QSize/qsize_wrap.h" #include "QtGui/QIcon/qicon_wrap.h" #include "QtWidgets/QAction/qaction_wrap.h" @@ -17,322 +18,364 @@ */ #ifndef QWIDGET_WRAPPED_METHODS_DECLARATION -#define QWIDGET_WRAPPED_METHODS_DECLARATION \ - \ - QOBJECT_WRAPPED_METHODS_DECLARATION \ - YOGAWIDGET_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(); \ - QString newStyle = QString::fromStdString(style); \ - QString currentStyleSheet = this->instance->styleSheet(); \ - if (newStyle != currentStyleSheet) { \ - this->instance->setStyleSheet(newStyle); \ - } \ - 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 windowState(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - int state = static_cast(this->instance->windowState()); \ - return Napi::Value::From(env, state); \ - } \ - 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 windowTitle(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - QString title = this->instance->windowTitle(); \ - return Napi::String::New(env, title.toStdString()); \ - } \ - 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 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(); \ - } \ - Napi::Value adjustSize(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - this->instance->adjustSize(); \ - return env.Null(); \ - } \ - Napi::Value activateWindow(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - this->instance->activateWindow(); \ - return env.Null(); \ - } \ - Napi::Value raise(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - this->instance->raise(); \ - return env.Null(); \ - } \ - Napi::Value lower(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - this->instance->lower(); \ - return env.Null(); \ - } \ - Napi::Value showFullScreen(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - this->instance->showFullScreen(); \ - return env.Null(); \ - } \ - Napi::Value showMaximized(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - this->instance->showMaximized(); \ - return env.Null(); \ - } \ - Napi::Value showMinimized(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - this->instance->showMinimized(); \ - return env.Null(); \ - } \ - Napi::Value showNormal(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - this->instance->showNormal(); \ - return env.Null(); \ - } \ - Napi::Value addAction(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - Napi::Object actionObject = info[0].As(); \ - QActionWrap* actionWrap = \ - Napi::ObjectWrap::Unwrap(actionObject); \ - this->instance->addAction(actionWrap->getInternalInstance()); \ - return env.Null(); \ - } \ - Napi::Value repolish(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - this->instance->style()->unpolish(this->instance); \ - this->instance->style()->polish(this->instance); \ - return env.Null(); \ +#define QWIDGET_WRAPPED_METHODS_DECLARATION \ + \ + QOBJECT_WRAPPED_METHODS_DECLARATION \ + YOGAWIDGET_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 mapFromGlobal(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + Napi::Object posObject = info[0].As(); \ + QPointWrap* posWrap = Napi::ObjectWrap::Unwrap(posObject); \ + QPoint pt = \ + this->instance->mapFromGlobal(*posWrap->getInternalInstance()); \ + auto instance = QPointWrap::constructor.New( \ + {Napi::External::New(env, new QPoint(pt.x(), pt.y()))}); \ + return instance; \ + } \ + Napi::Value mapFromParent(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + Napi::Object posObject = info[0].As(); \ + QPointWrap* posWrap = Napi::ObjectWrap::Unwrap(posObject); \ + QPoint pt = \ + this->instance->mapFromParent(*posWrap->getInternalInstance()); \ + auto instance = QPointWrap::constructor.New( \ + {Napi::External::New(env, new QPoint(pt.x(), pt.y()))}); \ + return instance; \ + } \ + Napi::Value mapToGlobal(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + Napi::Object posObject = info[0].As(); \ + QPointWrap* posWrap = Napi::ObjectWrap::Unwrap(posObject); \ + QPoint pt = this->instance->mapToGlobal(*posWrap->getInternalInstance()); \ + auto instance = QPointWrap::constructor.New( \ + {Napi::External::New(env, new QPoint(pt.x(), pt.y()))}); \ + return instance; \ + } \ + Napi::Value mapToParent(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + Napi::Object posObject = info[0].As(); \ + QPointWrap* posWrap = Napi::ObjectWrap::Unwrap(posObject); \ + QPoint pt = this->instance->mapToParent(*posWrap->getInternalInstance()); \ + auto instance = QPointWrap::constructor.New( \ + {Napi::External::New(env, new QPoint(pt.x(), pt.y()))}); \ + return instance; \ + } \ + 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(); \ + QString newStyle = QString::fromStdString(style); \ + QString currentStyleSheet = this->instance->styleSheet(); \ + if (newStyle != currentStyleSheet) { \ + this->instance->setStyleSheet(newStyle); \ + } \ + 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 windowState(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + int state = static_cast(this->instance->windowState()); \ + return Napi::Value::From(env, state); \ + } \ + 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 windowTitle(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + QString title = this->instance->windowTitle(); \ + return Napi::String::New(env, title.toStdString()); \ + } \ + 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 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(); \ + } \ + Napi::Value adjustSize(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + this->instance->adjustSize(); \ + return env.Null(); \ + } \ + Napi::Value activateWindow(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + this->instance->activateWindow(); \ + return env.Null(); \ + } \ + Napi::Value raise(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + this->instance->raise(); \ + return env.Null(); \ + } \ + Napi::Value lower(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + this->instance->lower(); \ + return env.Null(); \ + } \ + Napi::Value showFullScreen(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + this->instance->showFullScreen(); \ + return env.Null(); \ + } \ + Napi::Value showMaximized(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + this->instance->showMaximized(); \ + return env.Null(); \ + } \ + Napi::Value showMinimized(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + this->instance->showMinimized(); \ + return env.Null(); \ + } \ + Napi::Value showNormal(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + this->instance->showNormal(); \ + return env.Null(); \ + } \ + Napi::Value addAction(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + Napi::Object actionObject = info[0].As(); \ + QActionWrap* actionWrap = \ + Napi::ObjectWrap::Unwrap(actionObject); \ + this->instance->addAction(actionWrap->getInternalInstance()); \ + return env.Null(); \ + } \ + Napi::Value repolish(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + this->instance->style()->unpolish(this->instance); \ + this->instance->style()->polish(this->instance); \ + return env.Null(); \ } #endif // QWIDGET_WRAPPED_METHODS_DECLARATION @@ -346,6 +389,10 @@ InstanceMethod("resize", &WidgetWrapName::resize), \ InstanceMethod("isVisible", &WidgetWrapName::isVisible), \ InstanceMethod("close", &WidgetWrapName::close), \ + InstanceMethod("mapFromGlobal", &WidgetWrapName::mapFromGlobal), \ + InstanceMethod("mapFromParent", &WidgetWrapName::mapFromParent), \ + InstanceMethod("mapToGlobal", &WidgetWrapName::mapToGlobal), \ + InstanceMethod("mapToParent", &WidgetWrapName::mapToParent), \ InstanceMethod("setLayout", &WidgetWrapName::setLayout), \ InstanceMethod("setStyleSheet", &WidgetWrapName::setStyleSheet), \ InstanceMethod("setCursor", &WidgetWrapName::setCursor), \ diff --git a/src/cpp/lib/QtWidgets/QMenu/qmenu_wrap.cpp b/src/cpp/lib/QtWidgets/QMenu/qmenu_wrap.cpp index ddfd57fe9..082300b6e 100644 --- a/src/cpp/lib/QtWidgets/QMenu/qmenu_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QMenu/qmenu_wrap.cpp @@ -73,22 +73,24 @@ Napi::Value QMenuWrap::exec(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); Napi::HandleScope scope(env); - if (info.Length() == 2) { + if (info.Length() > 0) { Napi::Object pointObject = info[0].As(); QPointWrap* pointWrap = Napi::ObjectWrap::Unwrap(pointObject); - QPoint* qpoint = pointWrap->getInternalInstance(); - Napi::Object actionObject = info[1].As(); - QActionWrap* actionWrap = - Napi::ObjectWrap::Unwrap(actionObject); - this->instance->exec(*qpoint, actionWrap->getInternalInstance()); + QAction* action = nullptr; + if (info.Length() == 2) { + Napi::Object actionObject = info[1].As(); + QActionWrap* actionWrap = + Napi::ObjectWrap::Unwrap(actionObject); + action = actionWrap->getInternalInstance(); + } + this->instance->exec(*pointWrap->getInternalInstance(), action); } else if (info.Length() == 0) { this->instance->exec(); } else { Napi::TypeError::New(env, "Wrong number of arguments") .ThrowAsJavaScriptException(); } - return env.Null(); } diff --git a/src/demo.ts b/src/demo.ts index 6f64b80f1..82215ee8c 100644 --- a/src/demo.ts +++ b/src/demo.ts @@ -1,16 +1,33 @@ import { QMainWindow, QLabel, WidgetEventTypes, QMouseEvent } from './index'; +import { QPoint } from './lib/QtCore/QPoint'; +import { ContextMenuPolicy } from './lib/QtEnums'; +import { QMenu } from './lib/QtWidgets/QMenu'; +import { QAction } from './lib/QtWidgets/QAction'; const win = new QMainWindow(); const label = new QLabel(); label.setText('Move your mouse here'); label.setMouseTracking(true); +label.setContextMenuPolicy(ContextMenuPolicy.CustomContextMenu); label.addEventListener(WidgetEventTypes.MouseMove, (nativeEvt) => { const mouseEvt = new QMouseEvent(nativeEvt as any); console.log('mouseMoved at: ', { x: mouseEvt.x(), y: mouseEvt.y() }); }); +label.addEventListener('customContextMenuRequested', (pos: { x: number; y: number }): void => { + console.log(pos); + const position = new QPoint(pos.x, pos.y); + const menu = new QMenu(); + const action = new QAction(); + + action.setText(`Hello World`); + menu.addAction(action); + const ptGlobal = label.mapToGlobal(position); + menu.exec(ptGlobal); +}); + win.setCentralWidget(label); win.show(); (global as any).win = win; diff --git a/src/lib/QtGui/__tests__/QPixmap.test.ts b/src/lib/QtGui/__tests__/QPixmap.test.ts index f8f5e06e7..a69ae9b2a 100644 --- a/src/lib/QtGui/__tests__/QPixmap.test.ts +++ b/src/lib/QtGui/__tests__/QPixmap.test.ts @@ -57,18 +57,18 @@ describe('QPixmap', () => { }); it('save to a file', async () => { const outputFilePath = path.resolve(__dirname, 'assets', 'nodegui_save.png'); - await new Promise(resolve => fs.unlink(outputFilePath, resolve)); + 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)); + 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)); + 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)); + const exists = await new Promise((resolve) => fs.exists(outputFilePath, resolve)); expect(exists).toBe(true); }); }); diff --git a/src/lib/QtWidgets/QListWidget.ts b/src/lib/QtWidgets/QListWidget.ts index 19fa2cde0..616f409bf 100644 --- a/src/lib/QtWidgets/QListWidget.ts +++ b/src/lib/QtWidgets/QListWidget.ts @@ -81,7 +81,7 @@ export class QListWidget extends NodeListView { } findItems(text: string, flags: MatchFlag): QListWidgetItem[] { const nativeItems = this.native.findItems(text, flags); - return nativeItems.map(function(item: QListWidgetItem) { + return nativeItems.map(function (item: QListWidgetItem) { return new QListWidgetItem(item); }); } @@ -115,7 +115,7 @@ export class QListWidget extends NodeListView { } selectedItems(): QListWidgetItem[] { const nativeItems = this.native.selectedItems(); - return nativeItems.map(function(item: QListWidgetItem) { + return nativeItems.map(function (item: QListWidgetItem) { return new QListWidgetItem(item); }); } diff --git a/src/lib/QtWidgets/QMenu.ts b/src/lib/QtWidgets/QMenu.ts index 598ba26b8..2152e1964 100644 --- a/src/lib/QtWidgets/QMenu.ts +++ b/src/lib/QtWidgets/QMenu.ts @@ -36,16 +36,17 @@ export class QMenu extends NodeWidget { setTitle(title: string): void { this.native.setTitle(title); } - addSeparator(): QAction { return this.native.addSeparator(); } - exec(point?: QPoint, action?: QAction): void { + exec(point?: QPoint, action?: QAction | null): void { if (point && action) { this.native.exec(point.native, action.native); - return; + } else if (point) { + this.native.exec(point.native); + } else { + this.native.exec(); } - this.native.exec(); } popup(point: QPoint, action?: QAction): void { this.native.popup(point.native, action?.native); diff --git a/src/lib/QtWidgets/QPainter.ts b/src/lib/QtWidgets/QPainter.ts index a8c617ec8..447bfbad8 100644 --- a/src/lib/QtWidgets/QPainter.ts +++ b/src/lib/QtWidgets/QPainter.ts @@ -94,7 +94,7 @@ export class QPainter extends Component { } drawConvexPolygon(points: QPoint[]): void { - const nativePoints = points.map(point => point.native); + const nativePoints = points.map((point) => point.native); this.native.drawConvexPolygon(nativePoints); } diff --git a/src/lib/QtWidgets/QTreeWidget.ts b/src/lib/QtWidgets/QTreeWidget.ts index 2f7d4d4b1..da862c460 100644 --- a/src/lib/QtWidgets/QTreeWidget.ts +++ b/src/lib/QtWidgets/QTreeWidget.ts @@ -75,7 +75,7 @@ export class QTreeWidget extends QAbstractScrollArea { addTopLevelItems(items: QTreeWidgetItem[]): void { const napiItems: NativeElement[] = []; - items.forEach(item => { + items.forEach((item) => { this.topLevelItems.add(item); napiItems.push(item.native); }); @@ -89,7 +89,7 @@ export class QTreeWidget extends QAbstractScrollArea { insertTopLevelItems(index: number, items: QTreeWidgetItem[]): void { const napiItems: NativeElement[] = []; - items.forEach(item => { + items.forEach((item) => { this.topLevelItems.add(item); napiItems.push(item.native); }); @@ -102,7 +102,7 @@ export class QTreeWidget extends QAbstractScrollArea { selectedItems(): QTreeWidgetItem[] { const nativeItems = this.native.selectedItems(); - return nativeItems.map(function(eachItem: QTreeWidgetItem) { + return nativeItems.map(function (eachItem: QTreeWidgetItem) { return new QTreeWidgetItem(eachItem); }); } @@ -164,7 +164,7 @@ export class QTreeWidget extends QAbstractScrollArea { findItems(text: string, flags: MatchFlag, column: number): QTreeWidgetItem[] { const nativeItems = this.native.findItems(text, flags, column); - return nativeItems.map(function(eachItem: QTreeWidgetItem) { + return nativeItems.map(function (eachItem: QTreeWidgetItem) { return new QTreeWidgetItem(eachItem); }); } diff --git a/src/lib/QtWidgets/QWidget.ts b/src/lib/QtWidgets/QWidget.ts index 730654151..6cb06c9ef 100644 --- a/src/lib/QtWidgets/QWidget.ts +++ b/src/lib/QtWidgets/QWidget.ts @@ -9,6 +9,7 @@ import { CursorShape, WindowState } from '../QtEnums'; import { StyleSheet, prepareInlineStyleSheet } from '../core/Style/StyleSheet'; import { checkIfNativeElement } from '../utils/helpers'; import { YogaWidget } from '../core/YogaWidget'; +import { QPoint } from '../QtCore/QPoint'; import { QSize } from '../QtCore/QSize'; import { QRect } from '../QtCore/QRect'; import { QObjectSignals } from '../QtCore/QObject'; @@ -73,6 +74,18 @@ export abstract class NodeWidget extends YogaWid close(): boolean { return this.native.close(); } + mapFromGlobal(pos: QPoint): QPoint { + return new QPoint(this.native.mapFromGlobal(pos.native)); + } + mapFromParent(pos: QPoint): QPoint { + return new QPoint(this.native.mapFromParent(pos.native)); + } + mapToGlobal(pos: QPoint): QPoint { + return new QPoint(this.native.mapToGlobal(pos.native)); + } + mapToParent(pos: QPoint): QPoint { + return new QPoint(this.native.mapToParent(pos.native)); + } setStyleSheet(styleSheet: string): void { const preparedSheet = StyleSheet.create(styleSheet); this.native.setStyleSheet(preparedSheet);