Adds basic push button

This commit is contained in:
Atul R 2019-05-21 18:46:34 +02:00
parent 2eb95d7c05
commit 6f8db2db1c
9 changed files with 125 additions and 3 deletions

View File

@ -14,6 +14,7 @@
"src/cpp/QtWidgets/QLabel/qlabel_wrap.cpp",
"src/cpp/QtWidgets/QLayout/qlayout_wrap.cpp",
"src/cpp/QtWidgets/QMainWindow/qmainwindow_wrap.cpp",
"src/cpp/QtWidgets/QPushButton/qpushbutton_wrap.cpp"
],
'conditions': [
['OS=="mac"', {

View File

@ -14,8 +14,12 @@ const label2 = new QtWidgets.QLabel();
label2.setText("Hello12321");
label2.setStyleSheet("background-color:blue; color:white;");
const button1 = new QtWidgets.QPushButton();
button1.setText("Yolo");
gridLayout.addWidget(label);
gridLayout.addWidget(label2);
gridLayout.addWidget(button1);
view.setLayout(gridLayout);

View File

@ -0,0 +1,69 @@
#include "qpushbutton_wrap.h"
#include "../../QtGui/QWidget/qwidget_wrap.h"
#include "../../Extras/Utils/utils.h"
Napi::FunctionReference QPushButtonWrap::constructor;
Napi::Object QPushButtonWrap::init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
char CLASSNAME[] = "QPushButton";
Napi::Function func = DefineClass(env, CLASSNAME, {
InstanceMethod("setStyleSheet", &QPushButtonWrap::setStyleSheet),
InstanceMethod("setText", &QPushButtonWrap::setText),
});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
return exports;
}
QPushButton* QPushButtonWrap::getInternalInstance() {
return this->instance;
}
QPushButtonWrap::QPushButtonWrap(const Napi::CallbackInfo& info): Napi::ObjectWrap<QPushButtonWrap>(info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if(info.Length() == 1) {
if(info[0].IsObject()){
Napi::Object object_parent = info[0].As<Napi::Object>();
QWidgetWrap* parent = Napi::ObjectWrap<QWidgetWrap>::Unwrap(object_parent);
this->instance = new QPushButton(parent->getInternalInstance()); //this sets the parent to current widget
}else{
extrautils::throwTypeError(env, "Wrong type of arguments");
}
}else if (info.Length() == 0){
this->instance = new QPushButton();
}else {
extrautils::throwTypeError(env, "Wrong number of arguments");
}
}
QPushButtonWrap::~QPushButtonWrap() {
delete this->instance;
}
Napi::Value QPushButtonWrap::setStyleSheet(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::String text = info[0].As<Napi::String>();
std::string style = text.Utf8Value();
this->instance->setStyleSheet(style.c_str());
// this->instance->repaint();
return env.Null();
}
Napi::Value QPushButtonWrap::setText(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::String napiText = info[0].As<Napi::String>();
std::string text = napiText.Utf8Value();
this->instance->setText(text.c_str());
// this->instance->repaint();
return env.Null();
}

View File

@ -0,0 +1,21 @@
#ifndef QPUSHBUTTON_WRAP_H
#define QPUSHBUTTON_WRAP_H
#include <napi.h>
#include <QPushButton>
class QPushButtonWrap : public Napi::ObjectWrap<QPushButtonWrap> {
private:
QPushButton* instance;
public:
static Napi::Object init(Napi::Env env, Napi::Object exports);
QPushButtonWrap(const Napi::CallbackInfo& info);
~QPushButtonWrap();
QPushButton* getInternalInstance();
//class constructor
static Napi::FunctionReference constructor;
//wrapped methods
Napi::Value setStyleSheet(const Napi::CallbackInfo& info);
Napi::Value setText(const Napi::CallbackInfo& info);
};
#endif

View File

@ -4,6 +4,7 @@
#include "QtWidgets/QLayout/qlayout_wrap.h"
#include "QtWidgets/QLabel/qlabel_wrap.h"
#include "QtWidgets/QMainWindow/qmainwindow_wrap.h"
#include "QtWidgets/QPushButton/qpushbutton_wrap.h"
#include <napi.h>
@ -19,6 +20,7 @@ Napi::Object Main(Napi::Env env, Napi::Object exports) {
QWidgetWrap::init(env, exports);
QGridLayoutWrap::init(env, exports);
QMainWindowWrap::init(env,exports);
QPushButtonWrap::init(env, exports);
return QLabelWrap::init(env, exports);
}

View File

@ -4,6 +4,7 @@ import { QGridLayout } from "./lib/QtWidgets/QGridLayout";
import { QLabel } from "./lib/QtWidgets/QLabel";
import { QLayout } from "./lib/QtWidgets/QLayout";
import { QMainWindow } from "./lib/QtWidgets/QMainWindow";
import { QPushButton } from "./lib/QtWidgets/QPushButton";
export const QtGui = {
QApplication,
@ -14,5 +15,6 @@ export const QtWidgets = {
QGridLayout,
QLabel,
QMainWindow,
QLayout
QLayout,
QPushButton
};

View File

@ -4,6 +4,7 @@ import { Component } from "../../core/Component";
export class QWidget extends Component {
native: any;
layout?: QLayout;
constructor(parent?: QWidget) {
super();
if (parent) {
@ -24,7 +25,7 @@ export class QWidget extends Component {
}
setLayout(parentLayout: QLayout) {
this.native.setLayout(parentLayout.native);
this.parent = parentLayout;
this.layout = parentLayout;
}
setStyleSheet(style: string) {
this.native.setStyleSheet(style);

View File

@ -0,0 +1,22 @@
import addon from "../../core/addon";
import { QWidget } from "../../QtGui/QWidget";
import { Component } from "../../core/Component";
export class QPushButton extends Component {
native: any;
constructor(parent?: QWidget) {
super();
if (parent) {
this.native = new addon.QPushButton(parent.native);
this.parent = parent;
} else {
this.native = new addon.QPushButton();
}
}
setText(text: string) {
this.native.setText(text);
}
setStyleSheet(style: string) {
this.native.setStyleSheet(style);
}
}

View File

@ -1,5 +1,5 @@
export abstract class Component {
children = new Set<Component>();
children = new Set<Component>(); //TODO if react stub these as react will manage the instances from beings gc'ed better.
parent: Component | null = null;
abstract native: any;
}