QTreeWidget PR (#277)

* Added the beginning of QTreeWidget and QTreeWidgetItem

* Fixed garbage collection

* Fixed garbage collection

* Rewrote the constructor for QTreeWidgetItem

* Code cleanup

* Fixed up the QTreeWidgetItem constructor to accept only a string list of columns. Code cleanup

* Add setHeaderHidden to QTreeWidget

* Started adding signals to QTreeWidget

* Started adding signals to QTreeWidget

* Started adding signals to QTreeWidget

* Added TreeWidget back to main.cpp

* Added more functions to QTreeWidgetItem and QPlainTextEdit

* linting and cleanup

* parenting fix

* Fixes garbage collection for treewidget items
This commit is contained in:
Atul R 2019-12-18 17:36:44 +05:30 committed by GitHub
parent 7d33f25040
commit c90b2cb3ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
40 changed files with 543 additions and 38 deletions

View File

@ -80,6 +80,8 @@ add_library(${CORE_WIDGETS_ADDON} SHARED
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QMenuBar/qmenubar_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QMenu/qmenu_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QGroupBox/qgroupbox_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QTreeWidget/qtreewidget_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QTreeWidgetItem/qtreewidgetitem_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/core/FlexLayout/flexlayout_wrap.cpp"
# Custom widgets (include them for automoc since they contain Q_OBJECT)
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtCore/QObject/nobject.hpp"
@ -112,6 +114,8 @@ add_library(${CORE_WIDGETS_ADDON} SHARED
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QBoxLayout/nboxlayout.hpp"
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QComboBox/ncombobox.hpp"
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QGroupBox/ngroupbox.hpp"
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QTreeWidget/ntreewidget.hpp"
)
AddCommonConfig(${CORE_WIDGETS_ADDON})

View File

@ -32,4 +32,5 @@ class QPlainTextEditWrap : public Napi::ObjectWrap<QPlainTextEditWrap> {
Napi::Value wordWrapMode(const Napi::CallbackInfo &info);
Napi::Value setLineWrapMode(const Napi::CallbackInfo &info);
Napi::Value lineWrapMode(const Napi::CallbackInfo &info);
Napi::Value insertPlainText(const Napi::CallbackInfo &info);
};

View File

@ -0,0 +1,22 @@
#pragma once
#include <QTreeWidget>
#include "core/NodeWidget/nodewidget.h"
#include "napi.h"
class NTreeWidget : public QTreeWidget, public NodeWidget {
Q_OBJECT
NODEWIDGET_IMPLEMENTATIONS(QTreeWidget)
public:
using QTreeWidget::QTreeWidget; // inherit all constructors of QTreeWidget
void connectWidgetSignalsToEventEmitter() {
QObject::connect(this, &QTreeWidget::itemSelectionChanged, [=]() {
Napi::Env env = this->emitOnNode.Env();
Napi::HandleScope scope(env);
this->emitOnNode.Call({Napi::String::New(env, "itemSelectionChanged")});
});
}
};

View File

@ -0,0 +1,36 @@
#pragma once
#include <napi.h>
#include <stdlib.h>
#include <QPointer>
#include "QtWidgets/QWidget/qwidget_macro.h"
#include "ntreewidget.hpp"
class QTreeWidgetWrap : public Napi::ObjectWrap<QTreeWidgetWrap> {
private:
QPointer<NTreeWidget> instance;
public:
static Napi::Object init(Napi::Env env, Napi::Object exports);
QTreeWidgetWrap(const Napi::CallbackInfo &info);
~QTreeWidgetWrap();
NTreeWidget *getInternalInstance();
// class constructor
static Napi::FunctionReference constructor;
Napi::Value addTopLevelItem(const Napi::CallbackInfo &info);
Napi::Value selectedItems(const Napi::CallbackInfo &info);
// Napi::Value addTopLevelItems(const Napi::CallbackInfo& info);
// Napi::Value setHorizontalScrollBarPolicy(const Napi::CallbackInfo& info);
// Napi::Value setVerticalScrollBarPolicy(const Napi::CallbackInfo& info);
// Napi::Value takeTopLevelItem(const Napi::CallbackInfo& info);
// Napi::Value findItems(const Napi::CallbackInfo& info);
QWIDGET_WRAPPED_METHODS_DECLARATION
};

View File

@ -0,0 +1,37 @@
#pragma once
#include <napi.h>
#include <stdlib.h>
#include <QTreeWidgetItem>
#include "Extras/Utils/nutils.h"
#include "core/Component/component_wrap.h"
class QTreeWidgetItemWrap : public Napi::ObjectWrap<QTreeWidgetItemWrap> {
private:
QTreeWidgetItem *instance;
bool disableDeletion;
public:
static Napi::Object init(Napi::Env env, Napi::Object exports);
QTreeWidgetItemWrap(const Napi::CallbackInfo &info);
~QTreeWidgetItemWrap();
QTreeWidgetItem *getInternalInstance();
// class constructor
static Napi::FunctionReference constructor;
// wrapped methods
Napi::Value setText(const Napi::CallbackInfo &info);
Napi::Value parent(const Napi::CallbackInfo &info);
Napi::Value childCount(const Napi::CallbackInfo &info);
Napi::Value child(const Napi::CallbackInfo &info);
Napi::Value text(const Napi::CallbackInfo &info);
Napi::Value setSelected(const Napi::CallbackInfo &info);
Napi::Value setExpanded(const Napi::CallbackInfo &info);
COMPONENT_WRAPPED_METHODS_DECLARATION
};

View File

@ -23,6 +23,7 @@ Napi::Object QPlainTextEditWrap::init(Napi::Env env, Napi::Object exports) {
InstanceMethod("wordWrapMode", &QPlainTextEditWrap::wordWrapMode),
InstanceMethod("setLineWrapMode", &QPlainTextEditWrap::setLineWrapMode),
InstanceMethod("lineWrapMode", &QPlainTextEditWrap::lineWrapMode),
InstanceMethod("insertPlainText", &QPlainTextEditWrap::insertPlainText),
QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QPlainTextEditWrap)
QABSTRACTSCROLLAREA_WRAPPED_METHODS_EXPORT_DEFINE(
QPlainTextEditWrap)});
@ -132,3 +133,12 @@ Napi::Value QPlainTextEditWrap::lineWrapMode(const Napi::CallbackInfo &info) {
int value = static_cast<int>(this->instance->lineWrapMode());
return Napi::Number::From(env, value);
}
Napi::Value QPlainTextEditWrap::insertPlainText(
const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::String plainText = info[0].As<Napi::String>();
this->instance->insertPlainText(plainText.Utf8Value().c_str());
return env.Null();
}

View File

@ -0,0 +1,76 @@
#include "QtWidgets/QTreeWidget/qtreewidget_wrap.h"
#include <QWidget>
#include "Extras/Utils/nutils.h"
#include "QtWidgets/QTreeWidgetItem/qtreewidgetitem_wrap.h"
#include "QtWidgets/QWidget/qwidget_wrap.h"
Napi::FunctionReference QTreeWidgetWrap::constructor;
Napi::Object QTreeWidgetWrap::init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
char CLASSNAME[] = "QTreeWidget";
Napi::Function func = DefineClass(
env, CLASSNAME,
{InstanceMethod("addTopLevelItem", &QTreeWidgetWrap::addTopLevelItem),
InstanceMethod("selectedItems", &QTreeWidgetWrap::selectedItems),
QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QTreeWidgetWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
return exports;
}
NTreeWidget* QTreeWidgetWrap::getInternalInstance() { return this->instance; }
QTreeWidgetWrap::QTreeWidgetWrap(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<QTreeWidgetWrap>(info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (info.Length() == 1) {
Napi::Object parentObject = info[0].As<Napi::Object>();
QWidgetWrap* parentWidgetWrap =
Napi::ObjectWrap<QWidgetWrap>::Unwrap(parentObject);
this->instance = new NTreeWidget(parentWidgetWrap->getInternalInstance());
} else if (info.Length() == 0) {
this->instance = new NTreeWidget();
} else {
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = extrautils::configureQWidget(
this->getInternalInstance(), this->getInternalInstance()->getFlexNode(),
true);
}
QTreeWidgetWrap::~QTreeWidgetWrap() { extrautils::safeDelete(this->instance); }
Napi::Value QTreeWidgetWrap::addTopLevelItem(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::Object itemObject = info[0].As<Napi::Object>();
QTreeWidgetItemWrap* itemWrap =
Napi::ObjectWrap<QTreeWidgetItemWrap>::Unwrap(itemObject);
QTreeWidgetItem* item = itemWrap->getInternalInstance();
this->instance->addTopLevelItem(item);
return env.Null();
}
Napi::Value QTreeWidgetWrap::selectedItems(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
QList<QTreeWidgetItem*> items = this->instance->selectedItems();
Napi::Array napiItems = Napi::Array::New(env, items.size());
for (int i = 0; i < items.size(); i++) {
QTreeWidgetItem* item = items[i];
// disable deletion of the native instance for these by passing true
Napi::Object val = QTreeWidgetItemWrap::constructor.New(
{Napi::External<QTreeWidgetItem>::New(env, item),
Napi::Boolean::New(env, true)});
napiItems[i] = val;
}
return napiItems;
}

View File

@ -0,0 +1,187 @@
#include "QtWidgets/QTreeWidgetItem/qtreewidgetitem_wrap.h"
#include <QDebug>
#include "Extras/Utils/nutils.h"
#include "QtWidgets/QTreeWidget/qtreewidget_wrap.h"
#include "QtWidgets/QTreeWidgetItem/qtreewidgetitem_wrap.h"
#include "core/Component/component_wrap.h"
Napi::FunctionReference QTreeWidgetItemWrap::constructor;
Napi::Object QTreeWidgetItemWrap::init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
char CLASSNAME[] = "QTreeWidgetItem";
Napi::Function func = DefineClass(
env, CLASSNAME,
{InstanceMethod("setText", &QTreeWidgetItemWrap::setText),
InstanceMethod("parent", &QTreeWidgetItemWrap::parent),
InstanceMethod("child", &QTreeWidgetItemWrap::child),
InstanceMethod("text", &QTreeWidgetItemWrap::text),
InstanceMethod("childCount", &QTreeWidgetItemWrap::childCount),
InstanceMethod("setSelected", &QTreeWidgetItemWrap::setSelected),
InstanceMethod("setExpanded", &QTreeWidgetItemWrap::setExpanded),
COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
return exports;
}
QTreeWidgetItem *QTreeWidgetItemWrap::getInternalInstance() {
return this->instance;
}
QTreeWidgetItemWrap::~QTreeWidgetItemWrap() {
if (!this->disableDeletion) {
delete this->instance;
}
}
QTreeWidgetItemWrap::QTreeWidgetItemWrap(const Napi::CallbackInfo &info)
: Napi::ObjectWrap<QTreeWidgetItemWrap>(info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (info.Length() > 0 && info[0].IsExternal()) {
// --- if external ---
this->instance = info[0].As<Napi::External<QTreeWidgetItem>>().Data();
if (info.Length() == 2) {
this->disableDeletion = info[1].As<Napi::Boolean>().Value();
}
} else {
// --- regular cases ---
if (info.Length() == 3) {
Napi::Array stringsNapi = info[1].As<Napi::Array>();
QList<QString> list;
for (int i = 0; i < stringsNapi.Length(); i++) {
Napi::Value stringNapi = stringsNapi[i];
list.append(stringNapi.As<Napi::String>().Utf8Value().c_str());
}
QStringList strings = QStringList(list);
std::string parentType = info[2].As<Napi::String>().Utf8Value();
if (parentType == "tree") {
Napi::Object parentObject = info[0].As<Napi::Object>();
QTreeWidgetWrap *parentWidgetWrap =
Napi::ObjectWrap<QTreeWidgetWrap>::Unwrap(parentObject);
QTreeWidget *parent = parentWidgetWrap->getInternalInstance();
this->instance = new QTreeWidgetItem(parent, strings);
} else if (parentType == "item") {
Napi::Object itemObject = info[0].As<Napi::Object>();
QTreeWidgetItemWrap *itemWidgetWrap =
Napi::ObjectWrap<QTreeWidgetItemWrap>::Unwrap(itemObject);
QTreeWidgetItem *item = itemWidgetWrap->getInternalInstance();
this->instance = new QTreeWidgetItem(item, strings);
} else {
Napi::TypeError::New(env, "Invalid parent type")
.ThrowAsJavaScriptException();
}
} else if (info.Length() == 2) {
std::string parentType = info[1].As<Napi::String>().Utf8Value();
if (parentType == "tree") {
Napi::Object parentObject = info[0].As<Napi::Object>();
QTreeWidgetWrap *parentWidgetWrap =
Napi::ObjectWrap<QTreeWidgetWrap>::Unwrap(parentObject);
QTreeWidget *parent = parentWidgetWrap->getInternalInstance();
this->instance = new QTreeWidgetItem(parent);
} else if (parentType == "item") {
Napi::Object itemObject = info[0].As<Napi::Object>();
QTreeWidgetItemWrap *itemWidgetWrap =
Napi::ObjectWrap<QTreeWidgetItemWrap>::Unwrap(itemObject);
QTreeWidgetItem *item = itemWidgetWrap->getInternalInstance();
this->instance = new QTreeWidgetItem(item);
} else {
Napi::TypeError::New(env, "Invalid parent type")
.ThrowAsJavaScriptException();
}
} else if (info.Length() == 1) {
Napi::Array stringsNapi = info[0].As<Napi::Array>();
QList<QString> list;
for (int i = 0; i < stringsNapi.Length(); i++) {
Napi::Value stringNapi = stringsNapi[i];
list.append(stringNapi.As<Napi::String>().Utf8Value().c_str());
}
QStringList strings = QStringList(list);
this->instance = new QTreeWidgetItem(strings);
} else if (info.Length() == 0) {
this->instance = new QTreeWidgetItem();
} else {
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
}
this->rawData = extrautils::configureComponent(this->getInternalInstance());
}
Napi::Value QTreeWidgetItemWrap::setText(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
int column = info[0].As<Napi::Number>().Int32Value();
Napi::String napiText = info[1].As<Napi::String>();
std::string text = napiText.Utf8Value();
this->instance->setText(column, QString::fromUtf8(text.c_str()));
return env.Null();
}
Napi::Value QTreeWidgetItemWrap::parent(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (this->instance->parent() == nullptr) {
return env.Null();
} else {
QTreeWidgetItem *item = this->instance->parent();
// disable deletion of the native instance for these by passing true
Napi::Object val = QTreeWidgetItemWrap::constructor.New(
{Napi::External<QTreeWidgetItem>::New(env, item),
Napi::Boolean::New(env, true)});
return val;
}
}
Napi::Value QTreeWidgetItemWrap::childCount(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
int value = static_cast<int>(this->instance->childCount());
return Napi::Number::From(env, value);
}
Napi::Value QTreeWidgetItemWrap::child(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::Number index = info[0].As<Napi::Number>();
if (this->instance->child(index) == nullptr) {
return env.Null();
} else {
QTreeWidgetItem *item = this->instance->child(index);
// disable deletion of the native instance for these by passing true
Napi::Object val = QTreeWidgetItemWrap::constructor.New(
{Napi::External<QTreeWidgetItem>::New(env, item),
Napi::Boolean::New(env, true)});
return val;
}
}
Napi::Value QTreeWidgetItemWrap::text(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::Number column = info[0].As<Napi::Number>();
if (this->instance->text(column) == nullptr) {
return env.Null();
} else {
return Napi::Value::From(env, this->instance->text(column).toStdString());
}
}
Napi::Value QTreeWidgetItemWrap::setSelected(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::Boolean selected = info[0].As<Napi::Boolean>();
this->instance->setSelected(selected.Value());
return env.Null();
}
Napi::Value QTreeWidgetItemWrap::setExpanded(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::Boolean expanded = info[0].As<Napi::Boolean>();
this->instance->setExpanded(expanded.Value());
return env.Null();
}

View File

@ -15,11 +15,11 @@ Napi::Object QWidgetWrap::init(Napi::Env env, Napi::Object exports) {
return exports;
}
NWidget* QWidgetWrap::getInternalInstance() { return this->instance; }
NWidget *QWidgetWrap::getInternalInstance() { return this->instance; }
QWidgetWrap::~QWidgetWrap() { extrautils::safeDelete(this->instance); }
QWidgetWrap::QWidgetWrap(const Napi::CallbackInfo& info)
QWidgetWrap::QWidgetWrap(const Napi::CallbackInfo &info)
: Napi::ObjectWrap<QWidgetWrap>(info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
@ -29,7 +29,7 @@ QWidgetWrap::QWidgetWrap(const Napi::CallbackInfo& info)
new NWidget(info[0].As<Napi::External<NWidget>>().Data());
} else {
Napi::Object parentObject = info[0].As<Napi::Object>();
QWidgetWrap* parentWidgetWrap =
QWidgetWrap *parentWidgetWrap =
Napi::ObjectWrap<QWidgetWrap>::Unwrap(parentObject);
this->instance = new NWidget(parentWidgetWrap->getInternalInstance());
}

View File

@ -43,6 +43,8 @@
#include "QtWidgets/QTableWidget/qtablewidget_wrap.h"
#include "QtWidgets/QTableWidgetItem/qtablewidgetitem_wrap.h"
#include "QtWidgets/QToolButton/qtoolbutton_wrap.h"
#include "QtWidgets/QTreeWidget/qtreewidget_wrap.h"
#include "QtWidgets/QTreeWidgetItem/qtreewidgetitem_wrap.h"
#include "QtWidgets/QWidget/qwidget_wrap.h"
#include "core/FlexLayout/flexlayout_wrap.h"
// These cant be instantiated in JS Side
@ -73,6 +75,8 @@ Napi::Object Main(Napi::Env env, Napi::Object exports) {
QFileDialogWrap::init(env, exports);
QTableWidgetWrap::init(env, exports);
QTableWidgetItemWrap::init(env, exports);
QTreeWidgetWrap::init(env, exports);
QTreeWidgetItemWrap::init(env, exports);
QGridLayoutWrap::init(env, exports);
QGroupBoxWrap::init(env, exports);
FlexLayoutWrap::init(env, exports);
@ -100,4 +104,4 @@ Napi::Object Main(Napi::Env env, Napi::Object exports) {
return exports;
}
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Main)
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Main)

View File

@ -1,17 +1,30 @@
import { QMainWindow, QPushButton, QLabel, QWidget, FlexLayout } from './index';
import { FlexLayout, QMainWindow, QTreeWidget, QTreeWidgetItem, QWidget } from './index';
const win = new QMainWindow();
const center = new QWidget();
const button = new QPushButton();
button.setText('World');
const hello = new QLabel();
hello.setText('Hello');
const tree = new QTreeWidget();
const item1 = new QTreeWidgetItem(tree, ['Hello item1!']);
const item2 = new QTreeWidgetItem(item1, ['Hello item2!']);
const item3 = new QTreeWidgetItem(['Hello item3!!']);
tree.addTopLevelItem(item3);
tree.addEventListener('itemSelectionChanged', () => {
const items = tree.selectedItems();
items.forEach(function(item) {
const parent = item.parent();
if (parent) {
const child = parent.child(0);
if (child) {
console.log(child.text(0));
}
}
console.log(item.childCount());
(global as any).gc();
});
});
center.setLayout(new FlexLayout());
center.layout?.addWidget(button);
center.layout?.addWidget(hello);
center.layout?.addWidget(tree);
win.setCentralWidget(center);
win.show();

View File

@ -45,6 +45,9 @@ export { QMenu, QMenuEvents } from './lib/QtWidgets/QMenu';
export { QMenuBar, QMenuBarEvents } from './lib/QtWidgets/QMenuBar';
export { QPlainTextEdit, QPlainTextEditEvents, LineWrapMode } from './lib/QtWidgets/QPlainTextEdit';
export { QScrollArea, QScrollAreaEvents } from './lib/QtWidgets/QScrollArea';
export { QTreeWidget, QTreeWidgetEvents } from './lib/QtWidgets/QTreeWidget';
export { QTreeWidgetItem } from './lib/QtWidgets/QTreeWidgetItem';
export {
QSystemTrayIcon,
QSystemTrayIconEvents,

View File

@ -38,7 +38,7 @@ export class QObject extends NodeObject {
native = new addon.QObject();
}
super(native);
this.nodeParent = parent;
this.setNodeParent(parent);
this.native = native;
}
}

View File

@ -1,4 +1,5 @@
import { NodeWidget, QWidget } from './QWidget';
import { ScrollBarPolicy } from '../QtEnums/ScrollBarPolicy';
export abstract class QAbstractScrollArea extends NodeWidget {
viewportWidget?: NodeWidget;
@ -12,4 +13,10 @@ export abstract class QAbstractScrollArea extends NodeWidget {
}
return this.viewportWidget;
}
setHorizontalScrollBarPolicy(policy: ScrollBarPolicy): void {
this.native.setProperty('horizontalScrollBarPolicy', policy);
}
setVerticalScrollBarPolicy(policy: ScrollBarPolicy): void {
this.native.setProperty('verticalScrollBarPolicy', policy);
}
}

View File

@ -15,7 +15,7 @@ export class QBoxLayout extends NodeLayout {
native = new addon.QBoxLayout(dir);
}
super(native);
this.nodeParent = parent;
this.setNodeParent(parent);
this.native = native;
this.childLayouts = new Set();
}

View File

@ -19,7 +19,7 @@ export class QCheckBox extends QAbstractButton {
}
super(native);
this.native = native;
this.nodeParent = parent;
this.setNodeParent(parent);
}
setChecked(check: boolean): void {
this.native.setChecked(check);

View File

@ -23,7 +23,7 @@ export class QComboBox extends NodeWidget {
}
super(native);
this.native = native;
this.nodeParent = parent;
this.setNodeParent(parent);
}
addItem(icon: QIcon | undefined, text: string, userData: QVariant = new QVariant()): void {
if (icon) {

View File

@ -23,7 +23,7 @@ export class QDial extends QAbstractSlider {
}
super(native);
this.native = native;
this.nodeParent = parent;
this.setNodeParent(parent);
}
setNotchesVisible(visible: boolean): void {
// react:✓

View File

@ -27,7 +27,7 @@ export class QFileDialog extends NodeWidget {
}
super(native);
this.native = native;
this.nodeParent = parent;
this.setNodeParent(parent);
}
open(): void {
this.native.open();

View File

@ -13,7 +13,7 @@ export class QGridLayout extends NodeLayout {
native = new addon.QGridLayout();
}
super(native);
this.nodeParent = parent;
this.setNodeParent(parent);
this.native = native;
}
addWidget(widget: NodeWidget, row = 0, col = 0, rowSpan = 1, colSpan = 1): void {

View File

@ -20,7 +20,7 @@ export class QGroupBox extends NodeWidget {
}
super(native);
this.native = native;
this.nodeParent = parent;
this.setNodeParent(parent);
}
alignment(): AlignmentFlag {
return this.native.alignment();

View File

@ -23,7 +23,7 @@ export class QLabel extends NodeWidget {
}
super(native);
this.native = native;
this.nodeParent = parent;
this.setNodeParent(parent);
}
setAlignment(alignment: AlignmentFlag): void {
this.native.setAlignment(alignment);

View File

@ -31,7 +31,7 @@ export class QLineEdit extends NodeWidget {
}
super(native);
this.native = native;
this.nodeParent = parent;
this.setNodeParent(parent);
}
setText(text: string): void {
// react:✓

View File

@ -21,7 +21,7 @@ export class QMainWindow extends NodeWidget {
}
super(native);
this.native = native;
this.nodeParent = parent;
this.setNodeParent(parent);
this.setLayout = (parentLayout: NodeLayout): void => {
if (this.centralWidget) {

View File

@ -19,7 +19,7 @@ export class QMenu extends NodeWidget {
}
super(native);
this.native = native;
this.nodeParent = parent;
this.setNodeParent(parent);
this.actions = new Set();
}
setTitle(title: string): void {

View File

@ -26,7 +26,7 @@ export class QMenuBar extends NodeWidget {
}
super(native);
this.native = native;
this.nodeParent = parent;
this.setNodeParent(parent);
}
addMenu(menu: QMenu): void {

View File

@ -33,7 +33,7 @@ export class QPlainTextEdit extends QAbstractScrollArea {
}
super(native);
this.native = native;
this.nodeParent = parent;
this.setNodeParent(parent);
}
setPlainText(text: string | number): void {
// react:✓
@ -68,4 +68,7 @@ export class QPlainTextEdit extends QAbstractScrollArea {
lineWrapMode(): LineWrapMode {
return this.native.lineWrapMode();
}
insertPlainText(text: string | number): void {
this.native.insertPlainText(`${text}`);
}
}

View File

@ -18,7 +18,7 @@ export class QProgressBar extends NodeWidget {
}
super(native);
this.native = native;
this.nodeParent = parent;
this.setNodeParent(parent);
}
setValue(value: number): void {
// react:✓

View File

@ -22,7 +22,7 @@ export class QPushButton extends QAbstractButton {
native = new addon.QPushButton();
}
super(native);
this.nodeParent = parent;
this.setNodeParent(parent);
this.native = native;
}
setFlat(isFlat: boolean): void {

View File

@ -18,6 +18,6 @@ export class QRadioButton extends QAbstractButton {
}
super(native);
this.native = native;
this.nodeParent = parent;
this.setNodeParent(parent);
}
}

View File

@ -19,7 +19,7 @@ export class QScrollArea extends QAbstractScrollArea {
}
super(native);
this.native = native;
this.nodeParent = parent;
this.setNodeParent(parent);
this.setWidgetResizable(true);
}
setWidget(widget: NodeWidget): void {

View File

@ -18,7 +18,7 @@ export class QSpinBox extends NodeWidget {
native = new addon.QSpinBox();
}
super(native);
this.nodeParent = parent;
this.setNodeParent(parent);
this.native = native;
}
setPrefix(prefix: string): void {

View File

@ -18,7 +18,7 @@ export class QStackedWidget extends NodeWidget {
native = new addon.QStackedWidget();
}
super(native);
this.nodeParent = parent;
this.setNodeParent(parent);
this.native = native;
}

View File

@ -24,7 +24,7 @@ export class QTabWidget extends NodeWidget {
native = new addon.QTabWidget();
}
super(native);
this.nodeParent = parent;
this.setNodeParent(parent);
this.tabs = [];
this.native = native;
}

View File

@ -38,7 +38,7 @@ export class QTableWidget extends QAbstractScrollArea {
}
super(native);
this.native = native;
this.nodeParent = parent;
this.setNodeParent(parent);
this.items = new Set();
}
selectedRanges(): Range[] {

View File

@ -33,7 +33,7 @@ export class QToolButton extends QAbstractButton {
native = new addon.QToolButton();
}
super(native);
this.nodeParent = parent;
this.setNodeParent(parent);
this.native = native;
}
setArrowType(type: ArrowType): void {

View File

@ -0,0 +1,43 @@
import addon from '../utils/addon';
import { NodeWidget } from './QWidget';
import { BaseWidgetEvents } from '../core/EventWidget';
import { NativeElement } from '../core/Component';
import { QAbstractScrollArea, QTreeWidgetItem } from '../..';
export const QTreeWidgetEvents = Object.freeze({
...BaseWidgetEvents,
itemSelectionChanged: 'itemSelectionChanged',
});
export class QTreeWidget extends QAbstractScrollArea {
native: NativeElement;
topLevelItems: Set<QTreeWidgetItem>;
constructor(parent?: NodeWidget) {
let native;
if (parent) {
native = new addon.QTreeWidget(parent.native);
} else {
native = new addon.QTreeWidget();
}
super(native);
this.native = native;
this.setNodeParent(parent);
this.topLevelItems = new Set<QTreeWidgetItem>();
}
addTopLevelItem(item: QTreeWidgetItem): void {
this.topLevelItems.add(item);
this.native.addTopLevelItem(item.native);
}
setHeaderHidden(hide: boolean): void {
this.native.setProperty('headerHidden', hide);
}
selectedItems(): QTreeWidgetItem[] {
const nativeItems = this.native.selectedItems();
return nativeItems.map(function(eachItem: QTreeWidgetItem) {
return new QTreeWidgetItem(eachItem);
});
}
}

View File

@ -0,0 +1,55 @@
import addon from '../utils/addon';
import { Component, NativeElement } from '../core/Component';
import { checkIfNativeElement, QTreeWidget } from '../..';
export class QTreeWidgetItem extends Component {
native: NativeElement;
constructor(parent?: NativeElement | QTreeWidgetItem | QTreeWidget | string[], strings?: string[]) {
super();
if (checkIfNativeElement(parent)) {
this.native = parent as NativeElement;
} else {
if (parent instanceof QTreeWidgetItem || parent instanceof QTreeWidget) {
this.setNodeParent(parent);
const type = parent instanceof QTreeWidgetItem ? 'item' : 'tree';
if (strings) {
this.native = new addon.QTreeWidgetItem(parent.native, strings, type);
} else {
this.native = new addon.QTreeWidgetItem(parent.native, type);
}
} else if (Array.isArray(parent)) {
const strings = parent;
this.native = new addon.QTreeWidgetItem(strings);
} else {
this.native = new addon.QTreeWidgetItem();
}
}
}
setText(column: number, text: string): void {
this.native.setText(column, text);
}
parent(): QTreeWidgetItem | undefined {
const parent = this.native.parent();
if (parent) {
return new QTreeWidgetItem(parent);
} else {
return undefined;
}
}
childCount(): number {
return this.native.childCount();
}
child(index: number): QTreeWidgetItem | undefined {
return this.native.child(index);
}
text(column: number): string {
return this.native.text(column);
}
setSelected(selected: boolean): void {
this.native.setSelected(selected);
}
setExpanded(expanded: boolean): void {
this.native.setExpanded(expanded);
}
}

View File

@ -193,7 +193,7 @@ export class QWidget extends NodeWidget {
native = new addon.QWidget();
}
super(native);
this.nodeParent = parent;
this.setNodeParent(parent);
this.native = native;
}
}

View File

@ -1,6 +1,10 @@
export type NativeElement = { type: 'native'; [key: string]: any };
export abstract class Component {
protected nodeChildren = new Set<Component>();
protected nodeParent?: Component;
nodeChildren = new Set<Component>();
nodeParent?: Component;
setNodeParent(parent?: Component): void {
this.nodeParent = parent;
parent?.nodeChildren.add(this);
}
abstract native: NativeElement;
}