diff --git a/config/application.gypi b/config/application.gypi index 60e08454d..b3a835dab 100644 --- a/config/application.gypi +++ b/config/application.gypi @@ -14,6 +14,7 @@ "../src/cpp/QtGui/QEvent/QKeyEvent/qkeyevent_wrap.cpp", "../src/cpp/QtGui/QPixmap/qpixmap_wrap.cpp", "../src/cpp/QtGui/QIcon/qicon_wrap.cpp", + "../src/cpp/QtGui/QCursor/qcursor_wrap.cpp", '../src/cpp/core/FlexLayout/flexlayout_wrap.cpp', "../src/cpp/QtWidgets/QWidget/qwidget_wrap.cpp", "../src/cpp/QtWidgets/QGridLayout/qgridlayout_wrap.cpp", diff --git a/docs/api/NodeWidget.md b/docs/api/NodeWidget.md index fc61f7d02..e29571bb7 100644 --- a/docs/api/NodeWidget.md +++ b/docs/api/NodeWidget.md @@ -82,6 +82,24 @@ Sets the property that holds the widget's style sheet. It calls the native metho - `styleSheet` string - String which holds the widget's style sheet. Make sure you create this string using `StyleSheet.create()` +#### `widget.setCursor(cursor)` + +Sets the window mouse cursor. It calls the native method [QWidget: setCursor](https://doc.qt.io/qt-5/qwidget.html#cursor-prop). + +- `cursor` CursorShape - Specifies current cursor for the window [CursorShape is an enum from Qt](api/QtEnums.md) + +#### `widget.setWindowIcon(icon)` + +Sets the window icon. It calls the native method [QWidget: setWindowIcon](https://doc.qt.io/qt-5/qwidget.html#windowIcon-prop). + +- `icon` QIcon - Specifies icon for the window. + +#### `widget.setWindowState(state)` + +Sets the window state. It calls the native method [QWidget: setWindowState](https://doc.qt.io/qt-5/qwidget.html#setWindowState). + +- `state` WindowState - Specifies current state for the window [WindowState is an enum from Qt](api/QtEnums.md) + #### `widget.setWindowTitle(title)` Sets the window title property. It calls the native method [QWidget: setWindowTitle](https://doc.qt.io/qt-5/qwidget.html#windowTitle-prop). diff --git a/extras/assets/nodegui.png b/extras/assets/nodegui.png new file mode 100644 index 000000000..16c3c0282 Binary files /dev/null and b/extras/assets/nodegui.png differ diff --git a/src/cpp/QtGui/QCursor/qcursor_wrap.cpp b/src/cpp/QtGui/QCursor/qcursor_wrap.cpp new file mode 100644 index 000000000..6d2fa2720 --- /dev/null +++ b/src/cpp/QtGui/QCursor/qcursor_wrap.cpp @@ -0,0 +1,68 @@ +#include "qcursor_wrap.h" +#include "src/cpp/Extras/Utils/nutils.h" +#include "deps/spdlog/spdlog.h" +#include "src/cpp/QtGui/QPixmap/qpixmap_wrap.h" + +Napi::FunctionReference QCursorWrap::constructor; + +Napi::Object QCursorWrap::init(Napi::Env env, Napi::Object exports) +{ + Napi::HandleScope scope(env); + char CLASSNAME[] = "QCursor"; + Napi::Function func = DefineClass(env, CLASSNAME, { + InstanceMethod("pos", &QCursorWrap::pos), + InstanceMethod("setPos", &QCursorWrap::setPos), + COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE + }); + constructor = Napi::Persistent(func); + exports.Set(CLASSNAME, func); + return exports; +} + +QCursorWrap::QCursorWrap(const Napi::CallbackInfo &info) : Napi::ObjectWrap(info) +{ + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + if (info.Length() == 1) + { + Napi::Number cursor = info[0].As(); + this->instance = new QCursor(static_cast(cursor.Int32Value())); + } + else if (info.Length() == 0) + { + this->instance = new QCursor(); + } + else + { + Napi::TypeError::New(env, "Wrong number of arguments").ThrowAsJavaScriptException(); + } +} + +QCursorWrap::~QCursorWrap() +{ + delete this->instance; +} + +QCursor *QCursorWrap::getInternalInstance() +{ + return this->instance; +} + +Napi::Value QCursorWrap::pos(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + QPoint pos = this->instance->pos(); + Napi::Object posObj = Napi::Object::New(env); + posObj.Set("x", pos.x()); + posObj.Set("y", pos.y()); + return posObj; +} + +Napi::Value QCursorWrap::setPos(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + Napi::Number x = info[0].As(); + Napi::Number y = info[1].As(); + this->instance->setPos(x.Int32Value(), y.Int32Value()); + return env.Null(); +} \ No newline at end of file diff --git a/src/cpp/QtGui/QCursor/qcursor_wrap.h b/src/cpp/QtGui/QCursor/qcursor_wrap.h new file mode 100644 index 000000000..aca55ebdc --- /dev/null +++ b/src/cpp/QtGui/QCursor/qcursor_wrap.h @@ -0,0 +1,21 @@ +#pragma once + +#include +#include +#include "src/cpp/core/Component/component_macro.h" + +class QCursorWrap : public Napi::ObjectWrap +{ +private: + QCursor *instance; + +public: + static Napi::FunctionReference constructor; + static Napi::Object init(Napi::Env env, Napi::Object exports); + QCursorWrap(const Napi::CallbackInfo &info); + ~QCursorWrap(); + QCursor *getInternalInstance(); + // Wrapped methods + Napi::Value pos(const Napi::CallbackInfo& info); + Napi::Value setPos(const Napi::CallbackInfo& info); +}; diff --git a/src/cpp/QtWidgets/QWidget/qwidget_macro.h b/src/cpp/QtWidgets/QWidget/qwidget_macro.h index 4beddf507..f0a8a3729 100644 --- a/src/cpp/QtWidgets/QWidget/qwidget_macro.h +++ b/src/cpp/QtWidgets/QWidget/qwidget_macro.h @@ -4,6 +4,7 @@ #include "src/cpp/core/YogaWidget/yogawidget_macro.h" #include "src/cpp/core/Events/eventwidget_macro.h" #include "src/cpp/core/Component/component_macro.h" +#include "src/cpp/QtGui/QIcon/qicon_wrap.h" #include /* @@ -57,6 +58,28 @@ Napi::Value setStyleSheet(const Napi::CallbackInfo& info){ \ this->instance->setStyleSheet(style.c_str()); \ return env.Null(); \ } \ +Napi::Value setCursor(const Napi::CallbackInfo& info){ \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + Napi::Number cursor = info[0].As(); \ + this->instance->setCursor(static_cast(cursor.Int32Value())); \ + return env.Null(); \ +} \ +Napi::Value setWindowIcon(const Napi::CallbackInfo& info){ \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + Napi::Object iconObject = info[0].As(); \ + QIconWrap *iconWrap = Napi::ObjectWrap::Unwrap(iconObject); \ + this->instance->setWindowIcon(*iconWrap->getInternalInstance()); \ + return env.Null(); \ +} \ +Napi::Value setWindowState(const Napi::CallbackInfo& info){ \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + Napi::Number state = info[0].As(); \ + this->instance->setWindowState(static_cast(state.Int32Value())); \ + return env.Null(); \ +} \ Napi::Value setWindowTitle(const Napi::CallbackInfo& info){ \ Napi::Env env = info.Env(); \ Napi::HandleScope scope(env); \ @@ -227,6 +250,9 @@ Napi::Value setWindowFlag(const Napi::CallbackInfo& info){ \ InstanceMethod("close",&WidgetWrapName::close), \ InstanceMethod("setLayout",&WidgetWrapName::setLayout), \ InstanceMethod("setStyleSheet",&WidgetWrapName::setStyleSheet), \ + InstanceMethod("setCursor",&WidgetWrapName::setCursor), \ + InstanceMethod("setWindowIcon",&WidgetWrapName::setWindowIcon), \ + InstanceMethod("setWindowState",&WidgetWrapName::setWindowState), \ InstanceMethod("setWindowTitle",&WidgetWrapName::setWindowTitle), \ InstanceMethod("styleSheet",&WidgetWrapName::styleSheet), \ InstanceMethod("hide",&WidgetWrapName::hide), \ diff --git a/src/cpp/main.cpp b/src/cpp/main.cpp index d25f3e18f..3ff51c7e5 100644 --- a/src/cpp/main.cpp +++ b/src/cpp/main.cpp @@ -2,6 +2,7 @@ #include "src/cpp/QtWidgets/QWidget/qwidget_wrap.h" #include "src/cpp/QtGui/QPixmap/qpixmap_wrap.h" #include "src/cpp/QtGui/QIcon/qicon_wrap.h" +#include "src/cpp/QtGui/QCursor/qcursor_wrap.h" #include "src/cpp/QtWidgets/QGridLayout/qgridlayout_wrap.h" #include "src/cpp/QtWidgets/QLayout/qlayout_wrap.h" #include "src/cpp/QtWidgets/QDial/qdial_wrap.h" @@ -29,6 +30,7 @@ Napi::Object Main(Napi::Env env, Napi::Object exports) { QWidgetWrap::init(env, exports); QPixmapWrap::init(env, exports); QIconWrap::init(env, exports); + QCursorWrap::init(env, exports); QGridLayoutWrap::init(env, exports); FlexLayoutWrap::init(env, exports); QMainWindowWrap::init(env, exports); diff --git a/src/demo.ts b/src/demo.ts index ee41f3e5e..4352000d0 100644 --- a/src/demo.ts +++ b/src/demo.ts @@ -14,6 +14,7 @@ import { } from "./index"; import { QScrollArea } from "./lib/QtWidgets/QScrollArea"; import { QPixmap } from "./lib/QtGui/QPixmap"; +import { CursorShape, WindowState } from "./lib/QtEnums" const path = require("path"); @@ -22,6 +23,7 @@ const win = new QMainWindow(); const label = new QLabel(); label.setText("Hello world 🧙"); label.setInlineStyle("font-size: 20px;"); +label.setCursor(CursorShape.ForbiddenCursor); const checkbox = new QCheckBox(); checkbox.setText("Check me out?"); @@ -40,6 +42,10 @@ button.setText("Push Push Push!"); button.setObjectName("btn"); button.setFlat(true); +const nodeguiLogo = new QIcon( + path.resolve(__dirname, "../extras/assets/nodegui.png") +); + const icon = new QIcon( path.resolve(__dirname, "../extras/assets/start_icon.png") ); @@ -92,9 +98,11 @@ win.setStyleSheet(` } `); -win.setWindowTitle("hello"); +win.setWindowIcon(nodeguiLogo); +win.setWindowTitle("NodeGUI Demo"); win.resize(400, 400); win.show(); +win.setWindowState(WindowState.WindowActive); (global as any).win = win; // To prevent win from being garbage collected. diff --git a/src/index.ts b/src/index.ts index 6a483ecfb..37e86a29f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,6 +4,7 @@ export { QApplication } from "./lib/QtGui/QApplication"; export { QWidget, QWidgetEvents } from "./lib/QtWidgets/QWidget"; export { QPixmap } from "./lib/QtGui/QPixmap"; export { QIcon } from "./lib/QtGui/QIcon"; +export { QCursor } from "./lib/QtGui/QCursor"; // Abstract: export { NodeWidget } from "./lib/QtWidgets/QWidget"; export { NodeLayout } from "./lib/QtWidgets/QLayout"; diff --git a/src/lib/QtGui/QCursor/index.ts b/src/lib/QtGui/QCursor/index.ts new file mode 100644 index 000000000..352e3d62e --- /dev/null +++ b/src/lib/QtGui/QCursor/index.ts @@ -0,0 +1,22 @@ +import addon from "../../core/addon"; +import { Component, NativeElement } from "../../core/Component"; +import { QPixmap } from "../QPixmap"; + +type arg = NativeElement | number | QPixmap; +export class QCursor extends Component { + native: NativeElement; + constructor(arg?: arg) { + super(); + if (arg) { + this.native = new addon.QCursor(arg); + } else { + this.native = new addon.QCursor(); + } + } + pos = (): { x: number; y: number } => { + return this.native.pos(); + } + setPos = (x: number, y: number) => { + return this.native.setPos(x, y); + } +} diff --git a/src/lib/QtWidgets/QWidget/index.ts b/src/lib/QtWidgets/QWidget/index.ts index 497fba1b4..2f184d5ef 100644 --- a/src/lib/QtWidgets/QWidget/index.ts +++ b/src/lib/QtWidgets/QWidget/index.ts @@ -4,6 +4,9 @@ import { EventWidget, BaseWidgetEvents } from "../../core/EventWidget"; import { NativeElement } from "../../core/Component"; import { FlexLayout } from "../../core/FlexLayout"; import { WidgetAttribute, WindowType } from "../../QtEnums"; +import { QIcon } from "../../QtGui/QIcon"; +import { QCursor } from "../../QtGui/QCursor"; +import { CursorShape, WindowState } from "../../QtEnums"; import { applyStyleSheet, StyleSheet, @@ -37,6 +40,15 @@ export abstract class NodeWidget extends EventWidget { const preparedSheet = await StyleSheet.create(styleSheet); await applyStyleSheet(this, preparedSheet); }; + setCursor(cursor: CursorShape | QCursor) { + this.native.setCursor(cursor); + } + setWindowIcon(icon: QIcon) { + this.native.setWindowIcon(icon.native); + } + setWindowState = async (state: WindowState) => { + return this.native.setWindowState(state); + }; setWindowTitle = async (title: string) => { return this.native.setWindowTitle(title); };