Merge pull request #27 from nodegui/pushbutton-icon

Pushbutton icon
This commit is contained in:
Atul R 2019-08-18 20:13:57 +02:00 committed by GitHub
commit 800429035d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 160 additions and 16 deletions

View File

@ -13,6 +13,7 @@
"../src/cpp/QtGui/QApplication/qapplication_wrap.cpp",
"../src/cpp/QtGui/QEvent/QKeyEvent/qkeyevent_wrap.cpp",
"../src/cpp/QtGui/QPixmap/qpixmap_wrap.cpp",
"../src/cpp/QtGui/QIcon/qicon_wrap.cpp",
'../src/cpp/core/FlexLayout/flexlayout_wrap.cpp',
"../src/cpp/QtWidgets/QWidget/qwidget_wrap.cpp",
"../src/cpp/QtWidgets/QGridLayout/qgridlayout_wrap.cpp",

32
docs/api/QIcon.md Normal file
View File

@ -0,0 +1,32 @@
## Class: QIcon
> The QIcon class provides scalable icons in different modes and states.
**This class is a JS wrapper around Qt's [QIcon class](https://doc.qt.io/qt-5/qicon.html)**
**QIcon inherits from [Component](api/Component.md)**
### Example
```javascript
const { QIcon } = require("@nodegui/nodegui");
const imageUrl = "path/to/png";
const icon = new QIcon(imageUrl);
```
### `new QIcon(imageUrl?)`
- `imageUrl` string (_optional_). Absolute path of the image that needs to be loaded in the memory.
### Static Methods
QIcon can access all the static methods defined in [Component](api/Component.md)
### Instance Properties
QIcon can access all the instance properties defined in [Component](api/Component.md)
### Instance Methods
QIcon can access all the instance methods defined in [Component](api/Component.md)

View File

@ -45,4 +45,10 @@ Sets the given text to the button.
Sets whether the button border is raised.
- `isFlat` boolean
- `isFlat` boolean
#### `button.setIcon(icon)`
Sets an icon in the button.
- `icon` QIcon

Binary file not shown.

After

Width:  |  Height:  |  Size: 55 KiB

View File

@ -0,0 +1,47 @@
#include "qicon_wrap.h"
#include "src/cpp/Extras/Utils/nutils.h"
#include "deps/spdlog/spdlog.h"
Napi::FunctionReference QIconWrap::constructor;
Napi::Object QIconWrap::init(Napi::Env env, Napi::Object exports)
{
Napi::HandleScope scope(env);
char CLASSNAME[] = "QIcon";
Napi::Function func = DefineClass(env, CLASSNAME, {
COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE
});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
return exports;
}
QIconWrap::QIconWrap(const Napi::CallbackInfo &info) : Napi::ObjectWrap<QIconWrap>(info)
{
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (info.Length() == 1)
{
Napi::String url = info[0].As<Napi::String>();
QString imageUrl = QString::fromUtf8(url.Utf8Value().c_str());
this->instance = new QIcon(imageUrl);
}
else if (info.Length() == 0)
{
this->instance = new QIcon();
}
else
{
Napi::TypeError::New(env, "Wrong number of arguments").ThrowAsJavaScriptException();
}
}
QIconWrap::~QIconWrap()
{
delete this->instance;
}
QIcon *QIconWrap::getInternalInstance()
{
return this->instance;
}

View File

@ -0,0 +1,19 @@
#pragma once
#include <napi.h>
#include <QIcon>
#include "src/cpp/core/Component/component_macro.h"
class QIconWrap : public Napi::ObjectWrap<QIconWrap>
{
private:
QIcon *instance;
public:
static Napi::FunctionReference constructor;
static Napi::Object init(Napi::Env env, Napi::Object exports);
QIconWrap(const Napi::CallbackInfo &info);
~QIconWrap();
QIcon *getInternalInstance();
// Wrapped methods
};

View File

@ -1,5 +1,6 @@
#include "qpushbutton_wrap.h"
#include "src/cpp/QtWidgets/QWidget/qwidget_wrap.h"
#include "src/cpp/QtGui/QIcon/qicon_wrap.h"
#include "src/cpp/Extras/Utils/nutils.h"
Napi::FunctionReference QPushButtonWrap::constructor;
@ -10,6 +11,7 @@ Napi::Object QPushButtonWrap::init(Napi::Env env, Napi::Object exports) {
Napi::Function func = DefineClass(env, CLASSNAME, {
InstanceMethod("setText", &QPushButtonWrap::setText),
InstanceMethod("setFlat", &QPushButtonWrap::setFlat),
InstanceMethod("setIcon", &QPushButtonWrap::setIcon),
QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QPushButtonWrap)
});
constructor = Napi::Persistent(func);
@ -29,9 +31,9 @@ QPushButtonWrap::QPushButtonWrap(const Napi::CallbackInfo& info): Napi::ObjectWr
Napi::Object parentObject = info[0].As<Napi::Object>();
QWidgetWrap* parentWidgetWrap = Napi::ObjectWrap<QWidgetWrap>::Unwrap(parentObject);
this->instance = new NPushButton(parentWidgetWrap->getInternalInstance()); //this sets the parent to current widget
}else if (info.Length() == 0){
} else if (info.Length() == 0){
this->instance = new NPushButton();
}else {
} else {
Napi::TypeError::New(env, "Wrong number of arguments").ThrowAsJavaScriptException();
}
// Adds measure function on yoga node so that widget size is calculated based on its text also.
@ -49,7 +51,7 @@ Napi::Value QPushButtonWrap::setText(const Napi::CallbackInfo& info) {
Napi::String napiText = info[0].As<Napi::String>();
std::string text = napiText.Utf8Value();
this->instance->setText(text.c_str());
this->instance->setText(text.c_str());
return env.Null();
}
@ -59,8 +61,17 @@ Napi::Value QPushButtonWrap::setFlat(const Napi::CallbackInfo& info) {
Napi::HandleScope scope(env);
Napi::Boolean isFlat = info[0].As<Napi::Boolean>();
this->instance->setFlat(isFlat.Value());
this->instance->setFlat(isFlat.Value());
return env.Null();
}
Napi::Value QPushButtonWrap::setIcon(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->setIcon(*iconWrap->getInternalInstance());
return env.Null();
}

View File

@ -11,16 +11,15 @@ class QPushButtonWrap : public Napi::ObjectWrap<QPushButtonWrap> {
NPushButton* instance;
public:
static Napi::Object init(Napi::Env env, Napi::Object exports);
QPushButtonWrap(const Napi::CallbackInfo& info);
QPushButtonWrap(const Napi::CallbackInfo &info);
~QPushButtonWrap();
NPushButton* getInternalInstance();
NPushButton *getInternalInstance();
//class constructor
static Napi::FunctionReference constructor;
//wrapped methods
Napi::Value setText(const Napi::CallbackInfo& info);
Napi::Value setFlat(const Napi::CallbackInfo& info);
Napi::Value setText(const Napi::CallbackInfo &info);
Napi::Value setFlat(const Napi::CallbackInfo &info);
Napi::Value setIcon(const Napi::CallbackInfo &info);
QWIDGET_WRAPPED_METHODS_DECLARATION
};

View File

@ -1,6 +1,7 @@
#include "src/cpp/QtGui/QApplication/qapplication_wrap.h"
#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/QtWidgets/QGridLayout/qgridlayout_wrap.h"
#include "src/cpp/QtWidgets/QLayout/qlayout_wrap.h"
#include "src/cpp/QtWidgets/QLabel/qlabel_wrap.h"
@ -23,9 +24,10 @@ Napi::Object Main(Napi::Env env, Napi::Object exports) {
QApplicationWrap::init(env, exports);
QWidgetWrap::init(env, exports);
QPixmapWrap::init(env, exports);
QIconWrap::init(env, exports);
QGridLayoutWrap::init(env, exports);
FlexLayoutWrap::init(env, exports);
QMainWindowWrap::init(env,exports);
QMainWindowWrap::init(env, exports);
QPushButtonWrap::init(env, exports);
QCheckBoxWrap::init(env, exports);
QProgressBarWrap::init(env, exports);
@ -35,5 +37,4 @@ Napi::Object Main(Napi::Env env, Napi::Object exports) {
return QLabelWrap::init(env, exports);
}
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Main)

View File

@ -7,9 +7,12 @@ import {
QProgressBar,
QRadioButton,
FlexLayout,
QWidget
QWidget,
QIcon,
} from "./index";
const path = require('path');
const win = new QMainWindow();
const label = new QLabel();
@ -29,6 +32,9 @@ button.setText("Push Push Push!");
button.setObjectName("btn");
button.setFlat(true);
const icon = new QIcon(path.resolve(__dirname, '../extras/assets/start_icon.png'));
button.setIcon(icon);
const progressbar = new QProgressBar();
progressbar.setValue(6);
progressbar.setMinimum(1);

View File

@ -3,6 +3,7 @@ export { AspectRatioMode, WidgetAttribute, WindowType } from "./lib/QtEnums";
export { QApplication } from "./lib/QtGui/QApplication";
export { QWidget, QWidgetEvents } from "./lib/QtGui/QWidget";
export { QPixmap } from "./lib/QtGui/QPixmap";
export { QIcon } from "./lib/QtGui/QIcon";
// Abstract:
export { NodeWidget } from "./lib/QtGui/QWidget";
export { NodeLayout } from "./lib/QtWidgets/QLayout";

View File

@ -0,0 +1,16 @@
import addon from "../../core/addon";
import { Component, NativeElement } from "../../core/Component";
type arg = string | NativeElement;
export class QIcon extends Component {
native: NativeElement;
constructor(arg?: arg) {
super();
if (typeof arg === "string") {
const imageUrl = arg;
this.native = new addon.QIcon(imageUrl);
} else {
this.native = new addon.QIcon();
}
}
}

View File

@ -2,6 +2,7 @@ import addon from "../../core/addon";
import { NodeWidget } from "../../QtGui/QWidget";
import { BaseWidgetEvents } from "../../core/EventWidget";
import { NativeElement } from "../../core/Component";
import { QIcon } from "../../QtGui/QIcon";
export const QPushButtonEvents = Object.freeze({
...BaseWidgetEvents,
@ -35,4 +36,8 @@ export class QPushButton extends NodeWidget {
setFlat(isFlat: boolean) {
this.native.setFlat(isFlat);
}
setIcon(icon: QIcon) {
this.native.setIcon(icon.native);
}
}