Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/application.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 18 additions & 0 deletions docs/api/NodeWidget.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
17 changes: 17 additions & 0 deletions docs/api/QCursor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## Class: QCursor

> The QCursor class provides scalable icons in different modes and states.

**This class is a JS wrapper around Qt's [QCursor class](https://doc.qt.io/qt-5/qcursor.html)**

### Example

```javascript
const { QCursor } = require("@nodegui/nodegui");

const cursor = new QCursor();
```

### `new QCursor(cursor)`

- `cursor` CursorShape (_optional_). Defines shape for the cursor. [CursorShape is an enum from Qt](api/QtEnums.md)
Binary file added extras/assets/nodegui.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 68 additions & 0 deletions src/cpp/QtGui/QCursor/qcursor_wrap.cpp
Original file line number Diff line number Diff line change
@@ -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<QCursorWrap>(info)
{
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (info.Length() == 1)
{
Napi::Number cursor = info[0].As<Napi::Number>();
this->instance = new QCursor(static_cast<Qt::CursorShape>(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>();
Napi::Number y = info[1].As<Napi::Number>();
this->instance->setPos(x.Int32Value(), y.Int32Value());
return env.Null();
}
21 changes: 21 additions & 0 deletions src/cpp/QtGui/QCursor/qcursor_wrap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <napi.h>
#include <QCursor>
#include "src/cpp/core/Component/component_macro.h"

class QCursorWrap : public Napi::ObjectWrap<QCursorWrap>
{
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);
};
26 changes: 26 additions & 0 deletions src/cpp/QtWidgets/QWidget/qwidget_macro.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <QSize>
/*

Expand Down Expand Up @@ -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<Napi::Number>(); \
this->instance->setCursor(static_cast<Qt::CursorShape>(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<Napi::Object>(); \
QIconWrap *iconWrap = Napi::ObjectWrap<QIconWrap>::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<Napi::Number>(); \
this->instance->setWindowState(static_cast<Qt::WindowState>(state.Int32Value())); \
return env.Null(); \
} \
Napi::Value setWindowTitle(const Napi::CallbackInfo& info){ \
Napi::Env env = info.Env(); \
Napi::HandleScope scope(env); \
Expand Down Expand Up @@ -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), \
Expand Down
2 changes: 2 additions & 0 deletions src/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 9 additions & 1 deletion src/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -22,6 +23,7 @@ const win = new QMainWindow();
const label = new QLabel();
label.setText("Hello world 🧙");
label.setInlineStyle("font-size: 20px;");
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

label.setCursor(CursorShape.ForbiddenCursor);

const checkbox = new QCheckBox();
checkbox.setText("Check me out?");
Expand All @@ -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")
);
Expand Down Expand Up @@ -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.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
22 changes: 22 additions & 0 deletions src/lib/QtGui/QCursor/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import addon from "../../core/addon";
import { Component, NativeElement } from "../../core/Component";
import { QPixmap } from "../QPixmap";

type arg = NativeElement | number | QPixmap;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be just number
We could remove both NativeElement and QPixmap from here

Copy link
Copy Markdown
Contributor Author

@Uriziel01 Uriziel01 Aug 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually was left intentionally because I intend to add full support for QBitmap /QPixmap/QMasks/QCursor instances cursors, just did not have time on hand right now.
Do you think this should be removed from this PR?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No its okay. Let it be.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will merge this PR then ? @Uriziel01

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

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);
}
}
12 changes: 12 additions & 0 deletions src/lib/QtWidgets/QWidget/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
};
Expand Down