From 4d617c93213da8ef493c23a88424799e5689f0a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Gurin?= Date: Mon, 30 Sep 2019 16:33:30 -0300 Subject: [PATCH] inherits (#125) --- .../nodegui/QtWidgets/QWidget/qwidget_macro.h | 8 +++++++ src/demo.ts | 23 +++++++++++++++---- src/lib/QtWidgets/QWidget/index.ts | 3 +++ website/docs/api/NodeWidget.md | 21 +++++++++++++---- 4 files changed, 46 insertions(+), 9 deletions(-) diff --git a/src/cpp/include/nodegui/QtWidgets/QWidget/qwidget_macro.h b/src/cpp/include/nodegui/QtWidgets/QWidget/qwidget_macro.h index 7bbeb3568..f09fbe513 100644 --- a/src/cpp/include/nodegui/QtWidgets/QWidget/qwidget_macro.h +++ b/src/cpp/include/nodegui/QtWidgets/QWidget/qwidget_macro.h @@ -88,6 +88,13 @@ Napi::Value setWindowTitle(const Napi::CallbackInfo& info){ \ this->instance->setWindowTitle(title.c_str()); \ return env.Null(); \ } \ +Napi::Value inherits(const Napi::CallbackInfo& info){ \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + Napi::String className = info[0].As(); \ + std::string s = className.Utf8Value(); \ + return Napi::Boolean::New(env, this->instance->inherits(s.c_str())); \ +} \ Napi::Value styleSheet(const Napi::CallbackInfo& info){ \ Napi::Env env = info.Env(); \ Napi::HandleScope scope(env); \ @@ -261,6 +268,7 @@ Napi::Value setWindowFlag(const Napi::CallbackInfo& info){ \ InstanceMethod("close",&WidgetWrapName::close), \ InstanceMethod("setLayout",&WidgetWrapName::setLayout), \ InstanceMethod("setStyleSheet",&WidgetWrapName::setStyleSheet), \ + InstanceMethod("inherits",&WidgetWrapName::inherits), \ InstanceMethod("setCursor",&WidgetWrapName::setCursor), \ InstanceMethod("setWindowIcon",&WidgetWrapName::setWindowIcon), \ InstanceMethod("setWindowState",&WidgetWrapName::setWindowState), \ diff --git a/src/demo.ts b/src/demo.ts index dbbee54cd..3cf6d5a51 100644 --- a/src/demo.ts +++ b/src/demo.ts @@ -84,10 +84,11 @@ if (tab1.layout && tab2.layout) { tabs.addTab(tab1, icon, "Tab 1"); tabs.addTab(tab2, icon, "Tab 2"); -const progressbar = new QProgressBar(); -progressbar.setValue(6); -progressbar.setMinimum(1); -progressbar.setMaximum(15); +const progressBar = new QProgressBar(); +progressBar.setValue(6); +equal(progressBar.value(), 6) +progressBar.setMinimum(1); +progressBar.setMaximum(15); const radioButton = new QRadioButton(); radioButton.setText("Roger that!"); @@ -103,6 +104,18 @@ textEdit.setWordWrapMode(QTextOptionWrapMode.NoWrap); const scrollArea = new QScrollArea(); scrollArea.setInlineStyle("flex: 1; width:'100%';"); +// NodeWidget.inherits tests +ok(tab1.inherits("QWidget")) +ok(tabs.inherits("QTabWidget")) +ok(tabs.inherits("QWidget")) +ok(tabs.inherits("QObject")) +ok(!tabs.inherits("QProgressBar")) +ok(progressBar.inherits("QProgressBar")) +ok(!progressBar.inherits("QTabWidget")) +ok(tabs.inherits("QWidget")) +ok(tabs.inherits("QObject")) +ok(!tabs.inherits("unknown")) + const imageLabel = new QLabel(); const pixmap = new QPixmap( resolve(__dirname, "../extras/assets/kitchen.png") @@ -142,7 +155,7 @@ if (rootView.layout) { rootView.layout.addWidget(radioButton); rootView.layout.addWidget(lineEdit); rootView.layout.addWidget(button); - rootView.layout.addWidget(progressbar); + rootView.layout.addWidget(progressBar); rootView.layout.addWidget(textEdit); rootView.layout.addWidget(scrollArea); rootView.layout.addWidget(dial); diff --git a/src/lib/QtWidgets/QWidget/index.ts b/src/lib/QtWidgets/QWidget/index.ts index 1295fe377..79c68c5de 100644 --- a/src/lib/QtWidgets/QWidget/index.ts +++ b/src/lib/QtWidgets/QWidget/index.ts @@ -76,6 +76,9 @@ export abstract class NodeWidget extends EventWidget { // react:✓, //TODO:getter return this.native.setWindowTitle(title); }; + inherits(className: string) { + return this.native.inherits(className); + }; setWindowState = async (state: WindowState) => { // react:✓, //TODO:getter return this.native.setWindowState(state); diff --git a/website/docs/api/NodeWidget.md b/website/docs/api/NodeWidget.md index a0c69891e..d1db1d833 100644 --- a/website/docs/api/NodeWidget.md +++ b/website/docs/api/NodeWidget.md @@ -7,7 +7,7 @@ title: NodeWidget **This class implements all methods, properties of Qt's [QWidget class](https://doc.qt.io/qt-5/qwidget.html) so that it can be inherited by all widgets** -`NodeWidget` is an abstract class and hence no instances of the same should be created. It exists so that we can add similar functionalities to all widget's easily. Additionally it helps in typechecking process. If you wish to create a `div` like widget use [QWidget](api/QWidget.md) instead. +`NodeWidget` is an abstract class and hence no instances of the same should be created. It exists so that we can add similar functionalities to all widget's easily. Additionally it helps in type checking process. If you wish to create a `div` like widget use [QWidget](api/QWidget.md) instead. **NodeWidget is the base class for all widgets. It inherits from another abstract class [EventWidget](api/EventWidget.md)** @@ -186,11 +186,11 @@ Updates the widget. It calls the native method [QWidget: update](https://doc.qt. ### `widget.pos()` -returns the current widget position. It calls the native method [QWidget: pos](https://doc.qt.io/qt-5/qwidget.html#pos-prop). The returned size object contains x and y coordinates in pixels. +Returns the current widget position. It calls the native method [QWidget: pos](https://doc.qt.io/qt-5/qwidget.html#pos-prop). The returned size object contains x and y coordinates in pixels. ### `widget.size()` -returns the current widget size. It calls the native method [QWidget: size](https://doc.qt.io/qt-5/qwidget.html#size-prop). The returned size object contains width and height in pixels. +Returns the current widget size. It calls the native method [QWidget: size](https://doc.qt.io/qt-5/qwidget.html#size-prop). The returned size object contains width and height in pixels. ### `widget.updateGeometry()` @@ -201,10 +201,23 @@ Notifies the layout system that this widget has changed and may need to change g Sets the attribute attribute on this widget if on is true; otherwise clears the attribute. It calls the native method [QWidget: setAttribute](https://doc.qt.io/qt-5/qwidget.html#setAttribute). - `attributeName` WidgetAttribute - Enum from WidgetAttribute. -- `switchOn` - set it to true if you want to enable an attribute. +- `switchOn` boolean - set it to true if you want to enable an attribute. ### `widget.testAttribute(attributeName)` Returns true if attribute attribute is set on this widget; otherwise returns false. It calls the native method [QWidget: testAttribute](https://doc.qt.io/qt-5/qwidget.html#testAttribute). - `attributeName` WidgetAttribute - Enum from WidgetAttribute. + +#### `widget.inherits(className)` + +Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false. A class is considered to inherit itself. Example: + +```ts +const progressBar = new QProgressBar(); +progressBar.inherits("QObject"); // returns true +progressBar.inherits("QProgressBar"); // returns true +progressBar.inherits("QTabWidget"); // returns false +``` + +- `className` string - Name of the class to get if this widget inherits from.