Adds qlineedit methods and properties

This commit is contained in:
Atul R 2019-08-04 20:32:18 +02:00
parent f79317a9a3
commit b0df94bdf7
8 changed files with 127 additions and 18 deletions

View File

@ -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)`

View File

@ -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.

View File

@ -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<Napi::Object>();
QPixmapWrap* pixmapWrap = Napi::ObjectWrap<QPixmapWrap>::Unwrap(pixmapObject);
this->instance->setPixmap(*pixmapWrap->getInternalInstance());
return env.Null();
}
Napi::Object pixmapObject = info[0].As<Napi::Object>();
QPixmapWrap* pixmapWrap = Napi::ObjectWrap<QPixmapWrap>::Unwrap(pixmapObject);
this->instance->setPixmap(*pixmapWrap->getInternalInstance());
return env.Null();
}

View File

@ -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()) });
});
}
};

View File

@ -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<Napi::String>();
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<Napi::String>();
this->instance->setPlaceholderText(text.Utf8Value().c_str());
return env.Null();
}

View File

@ -16,6 +16,9 @@ class QLineEditWrap : public Napi::ObjectWrap<QLineEditWrap>{
//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

View File

@ -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();

View File

@ -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);
}
}