From 576ba1ace6e6e67bd65889303c9f1909249cce31 Mon Sep 17 00:00:00 2001 From: Dimitar Nestorov Date: Sun, 27 Oct 2019 11:41:16 +0200 Subject: [PATCH] Implemented QAction checkable and checked (#159) --- .../nodegui/QtWidgets/QAction/qaction_wrap.h | 4 ++ .../lib/QtWidgets/QAction/qaction_wrap.cpp | 46 +++++++++++++++++++ src/demo.ts | 19 ++++++++ src/lib/QtWidgets/QAction/index.ts | 12 +++++ website/docs/api/QAction.md | 29 ++++++++++++ 5 files changed, 110 insertions(+) diff --git a/src/cpp/include/nodegui/QtWidgets/QAction/qaction_wrap.h b/src/cpp/include/nodegui/QtWidgets/QAction/qaction_wrap.h index a016f1627..7ced9a27e 100644 --- a/src/cpp/include/nodegui/QtWidgets/QAction/qaction_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QAction/qaction_wrap.h @@ -21,6 +21,10 @@ class QActionWrap : public Napi::ObjectWrap{ 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 }; diff --git a/src/cpp/lib/QtWidgets/QAction/qaction_wrap.cpp b/src/cpp/lib/QtWidgets/QAction/qaction_wrap.cpp index 458ac2b04..3656b1ece 100644 --- a/src/cpp/lib/QtWidgets/QAction/qaction_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QAction/qaction_wrap.cpp @@ -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: "<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(); + 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(); + this->instance->setChecked(isChecked); + } else { + Napi::TypeError::New(env, "Wrong number of arguments").ThrowAsJavaScriptException(); + } + + return env.Null(); +} diff --git a/src/demo.ts b/src/demo.ts index ec60bc054..e973ca97c 100644 --- a/src/demo.ts +++ b/src/demo.ts @@ -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")); diff --git a/src/lib/QtWidgets/QAction/index.ts b/src/lib/QtWidgets/QAction/index.ts index 94f919f60..8bdc464dd 100644 --- a/src/lib/QtWidgets/QAction/index.ts +++ b/src/lib/QtWidgets/QAction/index.ts @@ -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); + } } diff --git a/website/docs/api/QAction.md b/website/docs/api/QAction.md index 69850b9a4..6ac1ffb93 100644 --- a/website/docs/api/QAction.md +++ b/website/docs/api/QAction.md @@ -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 \ No newline at end of file