diff --git a/docs/api/QLabel.md b/docs/api/QLabel.md index 21083824e..5c9d15c3e 100644 --- a/docs/api/QLabel.md +++ b/docs/api/QLabel.md @@ -39,9 +39,7 @@ the current text set on the label. ### Instance Methods -QLabel can access all the instance methods defined in [NodeWidget](api/NodeWidget.md) - -Additionally it also has the following instance methods: +QLabel can access all the instance methods defined in [NodeWidget](api/NodeWidget.md). Additionally it also has the following instance methods: #### `label.setText(text)` diff --git a/docs/api/QLineEdit.md b/docs/api/QLineEdit.md index 3f818728e..172046eea 100644 --- a/docs/api/QLineEdit.md +++ b/docs/api/QLineEdit.md @@ -26,8 +26,28 @@ QLineEdit can access all the static methods defined in [NodeWidget](api/NodeWidg ### Instance Properties -QLineEdit can access all the instance properties defined in [NodeWidget](api/NodeWidget.md) +QLineEdit can access all the instance properties defined in [NodeWidget](api/NodeWidget.md). Additionally it also has the following instance properties: + +#### `lineEdit.placeholderText` + +The placeholder text set on the lineEdit. ### Instance Methods -QLineEdit can access all the instance methods defined in [NodeWidget](api/NodeWidget.md) +QLineEdit can access all the instance methods defined in [NodeWidget](api/NodeWidget.md). Additionally it also has the following instance methods: + +#### `lineEdit.setText(text)` + +Sets the given text to the lineEdit. + +- `text` string + +#### `lineEdit.setPlaceholderText(text)` + +Sets the given text to the lineEdit's placeholder. + +- `text` string + +#### `lineEdit.text()` + +Returns the currently set text from native lineEdit widget. diff --git a/src/cpp/QtWidgets/QLabel/qlabel_wrap.cpp b/src/cpp/QtWidgets/QLabel/qlabel_wrap.cpp index 8a2f74a1a..4d1739546 100644 --- a/src/cpp/QtWidgets/QLabel/qlabel_wrap.cpp +++ b/src/cpp/QtWidgets/QLabel/qlabel_wrap.cpp @@ -76,13 +76,13 @@ Napi::Value QLabelWrap::text(const Napi::CallbackInfo &info) return Napi::String::New(env, labelText); } - Napi::Value QLabelWrap::setPixmap(const Napi::CallbackInfo &info) - { - Napi::Env env = info.Env(); - Napi::HandleScope scope(env); +Napi::Value QLabelWrap::setPixmap(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->setPixmap(*pixmapWrap->getInternalInstance()); - return env.Null(); - } + Napi::Object pixmapObject = info[0].As(); + QPixmapWrap* pixmapWrap = Napi::ObjectWrap::Unwrap(pixmapObject); + this->instance->setPixmap(*pixmapWrap->getInternalInstance()); + return env.Null(); +} diff --git a/src/cpp/QtWidgets/QLineEdit/nlineedit.h b/src/cpp/QtWidgets/QLineEdit/nlineedit.h index 20aea2f41..4e372bb62 100644 --- a/src/cpp/QtWidgets/QLineEdit/nlineedit.h +++ b/src/cpp/QtWidgets/QLineEdit/nlineedit.h @@ -8,6 +8,43 @@ class NLineEdit: public QLineEdit, public NodeWidget NODEWIDGET_IMPLEMENTATIONS public: using QLineEdit::QLineEdit; //inherit all constructors of QLineEdit -}; - + void connectWidgetSignalsToEventEmitter() { + // Qt Connects: Implement all signal connects here + QObject::connect(this, &QLineEdit::cursorPositionChanged, [=](int oldPost, int newPos) { + Napi::Env env = this->emitOnNode.Env(); + Napi::HandleScope scope(env); + this->emitOnNode.Call({ Napi::String::New(env, "cursorPositionChanged"), Napi::Value::From(env, oldPost),Napi::Value::From(env, newPos) }); + }); + QObject::connect(this, &QLineEdit::editingFinished, [=]() { + Napi::Env env = this->emitOnNode.Env(); + Napi::HandleScope scope(env); + this->emitOnNode.Call({ Napi::String::New(env, "editingFinished") }); + }); + QObject::connect(this, &QLineEdit::inputRejected, [=]() { + Napi::Env env = this->emitOnNode.Env(); + Napi::HandleScope scope(env); + this->emitOnNode.Call({ Napi::String::New(env, "inputRejected") }); + }); + QObject::connect(this, &QLineEdit::returnPressed, [=]() { + Napi::Env env = this->emitOnNode.Env(); + Napi::HandleScope scope(env); + this->emitOnNode.Call({ Napi::String::New(env, "returnPressed") }); + }); + QObject::connect(this, &QLineEdit::selectionChanged, [=]() { + Napi::Env env = this->emitOnNode.Env(); + Napi::HandleScope scope(env); + this->emitOnNode.Call({ Napi::String::New(env, "selectionChanged") }); + }); + QObject::connect(this, &QLineEdit::textChanged, [=](QString text) { + Napi::Env env = this->emitOnNode.Env(); + Napi::HandleScope scope(env); + this->emitOnNode.Call({ Napi::String::New(env, "textChanged"), Napi::Value::From(env, text.toStdString()) }); + }); + QObject::connect(this, &QLineEdit::textEdited, [=](QString text) { + Napi::Env env = this->emitOnNode.Env(); + Napi::HandleScope scope(env); + this->emitOnNode.Call({ Napi::String::New(env, "textEdited"), Napi::Value::From(env, text.toStdString()) }); + }); + } +}; \ No newline at end of file diff --git a/src/cpp/QtWidgets/QLineEdit/qlineedit_wrap.cpp b/src/cpp/QtWidgets/QLineEdit/qlineedit_wrap.cpp index fddd5587f..9a89acd76 100644 --- a/src/cpp/QtWidgets/QLineEdit/qlineedit_wrap.cpp +++ b/src/cpp/QtWidgets/QLineEdit/qlineedit_wrap.cpp @@ -11,6 +11,9 @@ Napi::Object QLineEditWrap::init(Napi::Env env, Napi::Object exports) { Napi::HandleScope scope(env); char CLASSNAME[] = "QLineEdit"; Napi::Function func = DefineClass(env, CLASSNAME, { + InstanceMethod("setPlaceholderText", &QLineEditWrap::setPlaceholderText), + InstanceMethod("setText", &QLineEditWrap::setText), + InstanceMethod("text", &QLineEditWrap::text), QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QLineEditWrap) }); constructor = Napi::Persistent(func); @@ -43,3 +46,26 @@ QLineEditWrap::~QLineEditWrap() { delete this->instance; } + +Napi::Value QLineEditWrap::setText(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + Napi::String text = info[0].As(); + this->instance->setText( text.Utf8Value().c_str()); + return env.Null(); +} + +Napi::Value QLineEditWrap::text(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + QString text = this->instance->text(); + return Napi::String::New(env, text.toStdString().c_str()); +} + +Napi::Value QLineEditWrap::setPlaceholderText(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + Napi::String text = info[0].As(); + this->instance->setPlaceholderText(text.Utf8Value().c_str()); + return env.Null(); +} \ No newline at end of file diff --git a/src/cpp/QtWidgets/QLineEdit/qlineedit_wrap.h b/src/cpp/QtWidgets/QLineEdit/qlineedit_wrap.h index 18f8d9401..1ca48fa6f 100644 --- a/src/cpp/QtWidgets/QLineEdit/qlineedit_wrap.h +++ b/src/cpp/QtWidgets/QLineEdit/qlineedit_wrap.h @@ -16,6 +16,9 @@ class QLineEditWrap : public Napi::ObjectWrap{ //class constructor static Napi::FunctionReference constructor; //wrapped methods + Napi::Value setText(const Napi::CallbackInfo& info); + Napi::Value text(const Napi::CallbackInfo& info); + Napi::Value setPlaceholderText(const Napi::CallbackInfo &info); QWIDGET_WRAPPED_METHODS_DECLARATION diff --git a/src/lib/QtGui/QPixmap/index.ts b/src/lib/QtGui/QPixmap/index.ts index 8a6119728..35e999769 100644 --- a/src/lib/QtGui/QPixmap/index.ts +++ b/src/lib/QtGui/QPixmap/index.ts @@ -3,6 +3,10 @@ import { Component, NativeElement } from "../../core/Component"; import { AspectRatioMode } from "../../QtEnums"; type arg = string | NativeElement; +const checkIfNativeElement = (arg: any) => { + const nativeArg = arg as NativeElement; + return typeof nativeArg === "object" && nativeArg.type === "native"; +}; export class QPixmap extends Component { native: NativeElement; constructor(arg?: arg) { @@ -10,7 +14,7 @@ export class QPixmap extends Component { if (typeof arg === "string") { const imageUrl = arg; this.native = new addon.QPixmap(imageUrl); - } else if ((arg as NativeElement).type === "native") { + } else if (checkIfNativeElement(arg)) { this.native = arg as NativeElement; } else { this.native = new addon.QPixmap(); diff --git a/src/lib/QtWidgets/QLineEdit/index.ts b/src/lib/QtWidgets/QLineEdit/index.ts index 182a7ecab..42f28733e 100644 --- a/src/lib/QtWidgets/QLineEdit/index.ts +++ b/src/lib/QtWidgets/QLineEdit/index.ts @@ -4,10 +4,18 @@ import { BaseWidgetEvents } from "../../core/EventWidget"; import { NativeElement } from "../../core/Component"; export const QLineEditEvents = Object.freeze({ - ...BaseWidgetEvents + ...BaseWidgetEvents, + cursorPositionChanged: "cursorPositionChanged", + editingFinished: "editingFinished", + inputRejected: "inputRejected", + returnPressed: "returnPressed", + selectionChanged: "selectionChanged", + textChanged: "textChanged", + textEdited: "textEdited" }); export class QLineEdit extends NodeWidget { native: NativeElement; + placeholderText?: string; constructor(parent?: NodeWidget) { let native; if (parent) { @@ -19,5 +27,18 @@ export class QLineEdit extends NodeWidget { this.native = native; this.parent = parent; // bind member functions + this.setText.bind(this); + this.text.bind(this); + this.setPlaceholderText.bind(this); + } + setText(text: string) { + text && this.native.setText(text); + } + text(): string { + return this.native.text(); + } + setPlaceholderText(text: string) { + this.placeholderText = text; + this.native.setPlaceholderText(text); } }