Implemented QAction checkable and checked (#159)

This commit is contained in:
Dimitar Nestorov 2019-10-27 11:41:16 +02:00 committed by Atul R
parent 42790d5321
commit 576ba1ace6
5 changed files with 110 additions and 0 deletions

View File

@ -21,6 +21,10 @@ class QActionWrap : public Napi::ObjectWrap<QActionWrap>{
Napi::Value setMenu(const Napi::CallbackInfo& info);
Napi::Value setShortcut(const Napi::CallbackInfo& info);
Napi::Value setShortcutContext(const Napi::CallbackInfo& info);
Napi::Value isCheckable(const Napi::CallbackInfo& info);
Napi::Value setCheckable(const Napi::CallbackInfo& info);
Napi::Value isChecked(const Napi::CallbackInfo& info);
Napi::Value setChecked(const Napi::CallbackInfo& info);
EVENTWIDGET_WRAPPED_METHODS_DECLARATION
};

View File

@ -19,6 +19,10 @@ Napi::Object QActionWrap::init(Napi::Env env, Napi::Object exports) {
InstanceMethod("setMenu", &QActionWrap::setMenu),
InstanceMethod("setShortcut", &QActionWrap::setShortcut),
InstanceMethod("setShortcutContext", &QActionWrap::setShortcutContext),
InstanceMethod("isCheckable", &QActionWrap::isCheckable),
InstanceMethod("setCheckable", &QActionWrap::setCheckable),
InstanceMethod("isChecked", &QActionWrap::isChecked),
InstanceMethod("setChecked", &QActionWrap::setChecked),
COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE
EVENTWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QActionWrap)
});
@ -107,3 +111,45 @@ Napi::Value QActionWrap::setShortcutContext(const Napi::CallbackInfo& info) {
qDebug()<<"shortCutContext: "<<shortCutContext;
return env.Null();
}
Napi::Value QActionWrap::isCheckable(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
return Napi::Boolean::New(env, this->instance->isCheckable());
}
Napi::Value QActionWrap::setCheckable(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (info.Length() == 1) {
Napi::Boolean isCheckable = info[0].As<Napi::Boolean>();
this->instance->setCheckable(isCheckable);
} else {
Napi::TypeError::New(env, "Wrong number of arguments").ThrowAsJavaScriptException();
}
return env.Null();
}
Napi::Value QActionWrap::isChecked(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
return Napi::Boolean::New(env, this->instance->isChecked());
}
Napi::Value QActionWrap::setChecked(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (info.Length() == 1) {
Napi::Boolean isChecked = info[0].As<Napi::Boolean>();
this->instance->setChecked(isChecked);
} else {
Napi::TypeError::New(env, "Wrong number of arguments").ThrowAsJavaScriptException();
}
return env.Null();
}

View File

@ -33,6 +33,23 @@ win.setMenuBar(menuBar);
tray.setContextMenu(menu);
const showActionCheckable = new QAction();
showActionCheckable.setCheckable(true);
showActionCheckable.setChecked(true);
showActionCheckable.setText("Show");
showActionCheckable.addEventListener("triggered", () => {
if (win.isVisible()) {
win.hide();
showHideAction.setText("Show");
showActionCheckable.setChecked(false);
} else {
win.show();
showHideAction.setText("Hide");
showActionCheckable.setChecked(true);
}
});
menu.addAction(showActionCheckable);
const showHideAction = new QAction();
showHideAction.setText("Hide");
showHideAction.setIcon(trayIcon);
@ -41,9 +58,11 @@ showHideAction.addEventListener("triggered", () => {
if (win.isVisible()) {
win.hide();
showHideAction.setText("Show");
showActionCheckable.setChecked(false);
} else {
win.show();
showHideAction.setText("Hide");
showActionCheckable.setChecked(true);
}
});
showHideAction.setShortcut(new QKeySequence("Ctrl+L"));

View File

@ -47,4 +47,16 @@ export class QAction extends QObject {
setShortcutContext(shortcutContext: ShortcutContext) {
this.native.setShortcutContext(shortcutContext);
}
isCheckable(): boolean {
return this.native.isCheckable();
}
setCheckable(isCheckable: boolean) {
this.native.setCheckable(isCheckable);
}
isChecked(): boolean {
return this.native.isChecked();
}
setChecked(isChecked: boolean) {
this.native.setChecked(isChecked);
}
}

View File

@ -74,3 +74,32 @@ This property holds the action's key sequence. It calls the native method [QActi
This property holds the context in which the action is valid. It calls the native method [QAction: setShortcutContext](https://doc.qt.io/qt-5/qaction.html#shortcutContext-prop).
- `contextEnum` - ShortcutContext enum.
### `action.isCheckable()`
Returns true if this action has been marked as checkable. It calls the native method [QIcon: isCheckable](https://doc.qt.io/qt-5/qaction.html#checkable-prop).
### `icon.setCheckable(isCheckable)`
Indicate that this action is checkable.
A checkable action is one which has an on/off state. For example, in a word processor, a Bold toolbar button may be either on or off. An action which is not a toggle action is a command action; a command action is simply executed, e.g. file save. By default, this property is `false`.
It calls the native method [QIcon: setCheckable](https://doc.qt.io/qt-5/qaction.html#checkable-prop).
- `isCheckable`: boolean
### `action.isChecked()`
Returns true if this action has been marked as checked. It calls the native method [QIcon: isChecked](https://doc.qt.io/qt-5/qaction.html#checked-prop).
### `icon.setChecked(isChecked)`
Indicate that this action is checked.
Only checkable actions can be checked. By default, this is false (the action is unchecked).
It calls the native method [QIcon: setChecked](https://doc.qt.io/qt-5/qaction.html#checkable-prop).
- `isChecked`: boolean