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
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ add_library(${CORE_WIDGETS_ADDON} SHARED
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtCore/QUrl/qurl_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QWidget/qwidget_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QBoxLayout/qboxlayout_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QComboBox/qcombobox_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QGridLayout/qgridlayout_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QDial/qdial_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QLabel/qlabel_wrap.cpp"
Expand Down Expand Up @@ -99,6 +100,7 @@ add_library(${CORE_WIDGETS_ADDON} SHARED
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QLayout/nlayout.hpp"
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QGridLayout/ngridlayout.hpp"
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QBoxLayout/nboxlayout.hpp"
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QComboBox/ncombobox.hpp"
)

AddCommonConfig(${CORE_WIDGETS_ADDON})
Expand Down
40 changes: 40 additions & 0 deletions src/cpp/include/nodegui/QtWidgets/QComboBox/ncombobox.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

#include <QComboBox>

#include "core/NodeWidget/nodewidget.h"
#include "napi.h"

class NComboBox : public QComboBox, public NodeWidget {
public:
Q_OBJECT
NODEWIDGET_IMPLEMENTATIONS(QComboBox)
using QComboBox::QComboBox;

void connectWidgetSignalsToEventEmitter() {
// Qt Connects: Implement all signal connects here
QObject::connect(
this, QOverload<int>::of(&QComboBox::currentIndexChanged),
[=](int index) {
Napi::Env env = this->emitOnNode.Env();
Napi::HandleScope scope(env);
this->emitOnNode.Call({Napi::String::New(env, "currentIndexChanged"),
Napi::Number::From(env, index)});
});
QObject::connect(
this, QOverload<const QString &>::of(&QComboBox::currentTextChanged),
[=](const QString &text) {
Napi::Env env = this->emitOnNode.Env();
Napi::HandleScope scope(env);
this->emitOnNode.Call({Napi::String::New(env, "currentTextChanged"),
Napi::String::New(env, text.toStdString())});
});
QObject::connect(
this, &QComboBox::editTextChanged, [=](const QString &text) {
Napi::Env env = this->emitOnNode.Env();
Napi::HandleScope scope(env);
this->emitOnNode.Call({Napi::String::New(env, "editTextChanged"),
Napi::String::New(env, text.toStdString())});
});
}
};
39 changes: 39 additions & 0 deletions src/cpp/include/nodegui/QtWidgets/QComboBox/qcombobox_wrap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include <napi.h>
#include <stdlib.h>

#include <QPointer>

#include "Extras/Utils/nutils.h"
#include "QtWidgets/QComboBox/ncombobox.hpp"
#include "QtWidgets/QWidget/qwidget_macro.h"

class QComboBoxWrap : public Napi::ObjectWrap<QComboBoxWrap> {
private:
QPointer<NComboBox> instance;

public:
static Napi::Object init(Napi::Env env, Napi::Object exports);
QComboBoxWrap(const Napi::CallbackInfo& info);
~QComboBoxWrap();
NComboBox* getInternalInstance();
// class constructor
static Napi::FunctionReference constructor;
// wrapped methods
Napi::Value addItem(const Napi::CallbackInfo& info);
Napi::Value insertItem(const Napi::CallbackInfo& info);
Napi::Value currentIndex(const Napi::CallbackInfo& info);
Napi::Value currentText(const Napi::CallbackInfo& info);
Napi::Value insertSeparator(const Napi::CallbackInfo& info);
Napi::Value itemText(const Napi::CallbackInfo& info);
Napi::Value removeItem(const Napi::CallbackInfo& info);
Napi::Value sizeAdjustPolicy(const Napi::CallbackInfo& info);
Napi::Value setSizeAdjustPolicy(const Napi::CallbackInfo& info);
Napi::Value maxVisibleItems(const Napi::CallbackInfo& info);
Napi::Value setMaxVisibleItems(const Napi::CallbackInfo& info);
Napi::Value isEditable(const Napi::CallbackInfo& info);
Napi::Value setEditable(const Napi::CallbackInfo& info);

QWIDGET_WRAPPED_METHODS_DECLARATION
};
3 changes: 1 addition & 2 deletions src/cpp/lib/QtGui/QApplication/qapplication_wrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ Napi::Value StaticQApplicationWrapMethods::style(
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
QStyle* style = QApplication::style();
return QStyleWrap::constructor.New(
{Napi::External<QStyle>::New(env, style)});
return QStyleWrap::constructor.New({Napi::External<QStyle>::New(env, style)});
}

Napi::Value QApplicationWrap::setQuitOnLastWindowClosed(
Expand Down
172 changes: 172 additions & 0 deletions src/cpp/lib/QtWidgets/QComboBox/qcombobox_wrap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@

#include "QtWidgets/QComboBox/qcombobox_wrap.h"

#include <QWidget>

#include "Extras/Utils/nutils.h"
#include "QtWidgets/QWidget/qwidget_wrap.h"

Napi::FunctionReference QComboBoxWrap::constructor;

Napi::Object QComboBoxWrap::init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
char CLASSNAME[] = "QComboBox";
Napi::Function func = DefineClass(
env, CLASSNAME,
{InstanceMethod("addItem", &QComboBoxWrap::addItem),
InstanceMethod("insertItem", &QComboBoxWrap::insertItem),
InstanceMethod("currentIndex", &QComboBoxWrap::currentIndex),
InstanceMethod("currentText", &QComboBoxWrap::currentText),
InstanceMethod("insertSeparator", &QComboBoxWrap::insertSeparator),
InstanceMethod("itemText", &QComboBoxWrap::itemText),
InstanceMethod("removeItem", &QComboBoxWrap::removeItem),
InstanceMethod("sizeAdjustPolicy", &QComboBoxWrap::sizeAdjustPolicy),
InstanceMethod("setSizeAdjustPolicy",
&QComboBoxWrap::setSizeAdjustPolicy),
InstanceMethod("maxVisibleItems", &QComboBoxWrap::maxVisibleItems),
InstanceMethod("setMaxVisibleItems", &QComboBoxWrap::setMaxVisibleItems),
InstanceMethod("isEditable", &QComboBoxWrap::isEditable),
InstanceMethod("setEditable", &QComboBoxWrap::setEditable),
QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QComboBoxWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
return exports;
}

NComboBox* QComboBoxWrap::getInternalInstance() { return this->instance; }
QComboBoxWrap::~QComboBoxWrap() { extrautils::safeDelete(this->instance); }

QComboBoxWrap::QComboBoxWrap(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<QComboBoxWrap>(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 NComboBox(parentWidgetWrap->getInternalInstance());
} else if (info.Length() == 0) {
this->instance = new NComboBox();
} else {
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = extrautils::configureQWidget(
this->getInternalInstance(), this->getInternalInstance()->getFlexNode(),
true);
}

Napi::Value QComboBoxWrap::addItem(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);

std::string text = info[0].As<Napi::String>().Utf8Value();

this->instance->addItem(text.c_str());
return env.Null();
}

Napi::Value QComboBoxWrap::insertItem(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);

int index = info[0].As<Napi::Number>().Int32Value();
std::string text = info[1].As<Napi::String>().Utf8Value();

this->instance->insertItem(index, text.c_str());
return env.Null();
}

Napi::Value QComboBoxWrap::currentIndex(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);

return Napi::Number::New(env, this->instance->currentIndex());
}

Napi::Value QComboBoxWrap::currentText(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);

return Napi::String::New(env, this->instance->currentText().toStdString());
}

Napi::Value QComboBoxWrap::insertSeparator(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);

int index = info[0].As<Napi::Number>().Int32Value();

this->instance->insertSeparator(index);
return env.Null();
}

Napi::Value QComboBoxWrap::itemText(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);

int index = info[0].As<Napi::Number>().Int32Value();

return Napi::String::New(env, this->instance->itemText(index).toStdString());
}

Napi::Value QComboBoxWrap::removeItem(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);

int index = info[0].As<Napi::Number>().Int32Value();

this->instance->removeItem(index);
return env.Null();
}

Napi::Value QComboBoxWrap::sizeAdjustPolicy(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);

return Napi::Number::New(env, this->instance->sizeAdjustPolicy());
}
Napi::Value QComboBoxWrap::setSizeAdjustPolicy(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
QComboBox::SizeAdjustPolicy policy = static_cast<QComboBox::SizeAdjustPolicy>(
info[0].As<Napi::Number>().Int32Value());
this->instance->setSizeAdjustPolicy(policy);
return env.Null();
}

Napi::Value QComboBoxWrap::maxVisibleItems(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);

return Napi::Number::New(env, this->instance->maxVisibleItems());
}

Napi::Value QComboBoxWrap::setMaxVisibleItems(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);

int maxItems = info[0].As<Napi::Number>().Int32Value();

this->instance->setMaxVisibleItems(maxItems);
return env.Null();
}

Napi::Value QComboBoxWrap::isEditable(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);

return Napi::Boolean::New(env, this->instance->isEditable());
}

Napi::Value QComboBoxWrap::setEditable(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);

bool editable = info[0].As<Napi::Boolean>().Value();

this->instance->setEditable(editable);
return env.Null();
}
2 changes: 2 additions & 0 deletions src/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "QtWidgets/QAction/qaction_wrap.h"
#include "QtWidgets/QBoxLayout/qboxlayout_wrap.h"
#include "QtWidgets/QCheckBox/qcheckbox_wrap.h"
#include "QtWidgets/QComboBox/qcombobox_wrap.h"
#include "QtWidgets/QDial/qdial_wrap.h"
#include "QtWidgets/QGridLayout/qgridlayout_wrap.h"
#include "QtWidgets/QLabel/qlabel_wrap.h"
Expand Down Expand Up @@ -60,6 +61,7 @@ Napi::Object Main(Napi::Env env, Napi::Object exports) {
QMovieWrap::init(env, exports);
QStyleWrap::init(env, exports);
QCursorWrap::init(env, exports);
QComboBoxWrap::init(env, exports);
QBoxLayoutWrap::init(env, exports);
QGridLayoutWrap::init(env, exports);
FlexLayoutWrap::init(env, exports);
Expand Down
47 changes: 47 additions & 0 deletions src/demo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { QWidget, QScrollArea, FlexLayout, QPushButton } from './index';
import { QLabel } from './lib/QtWidgets/QLabel';
import { QMainWindow } from './lib/QtWidgets/QMainWindow';
import { QComboBox, QComboBoxEvents } from './lib/QtWidgets/QComboBox';

import { SizeAdjustPolicy } from './lib/QtEnums';

const win = new QMainWindow();
const scroll = new QScrollArea();
Expand All @@ -15,6 +18,50 @@ btn.setText('helloo');

const text = new QLabel();
text.setText('1 oncererer');
const combo = new QComboBox();
combo.addItem('test');
combo.addItem('test2');
combo.addEventListener(QComboBoxEvents.currentTextChanged, e => {
console.log('text changed', e);
});
combo.addEventListener(QComboBoxEvents.currentIndexChanged, e => {
console.log('index changed', e);
});
combo.addEventListener(QComboBoxEvents.editTextChanged, e => {
console.log('edit changed', e);
});
const btn1 = new QPushButton();
const btn2 = new QPushButton();
const btn3 = new QPushButton();
const btn4 = new QPushButton();
const btn5 = new QPushButton();

btn1.setText('add to index 1');
btn1.addEventListener('clicked', () => {
combo.insertItem(1, 'inserted');
});
btn2.setText('remove index 1');
btn2.addEventListener('clicked', () => {
combo.removeItem(1);
});
btn3.setText('editable');
btn3.addEventListener('clicked', () => {
combo.setEditable(!combo.isEditable());
});
btn4.setText('max 4');
btn4.addEventListener('clicked', () => {
combo.setMaxVisibleItems(4);
});
btn5.setText('Adjust Size');
btn5.addEventListener('clicked', () => {
combo.setSizeAdjustPolicy(SizeAdjustPolicy.AdjustToContents);
});
center.layout?.addWidget(combo);
center.layout?.addWidget(btn1);
center.layout?.addWidget(btn2);
center.layout?.addWidget(btn3);
center.layout?.addWidget(btn4);
center.layout?.addWidget(btn5);
btn.addEventListener('clicked', () => {
text.setText(`
Yoloooooooooo
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export { QDial, QDialEvents } from './lib/QtWidgets/QDial';
export { QLineEdit, QLineEditEvents, EchoMode } from './lib/QtWidgets/QLineEdit';
export { QMainWindow, QMainWindowEvents } from './lib/QtWidgets/QMainWindow';
export { QProgressBar, QProgressBarEvents } from './lib/QtWidgets/QProgressBar';
export { QComboBox, QComboBoxEvents } from './lib/QtWidgets/QComboBox';
export { QPushButton, QPushButtonEvents } from './lib/QtWidgets/QPushButton';
export { QSpinBox, QSpinBoxEvents } from './lib/QtWidgets/QSpinBox';
export { QRadioButton, QRadioButtonEvents } from './lib/QtWidgets/QRadioButton';
Expand Down
6 changes: 6 additions & 0 deletions src/lib/QtEnums/SizeAdjustPolicy/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum SizeAdjustPolicy {
AdjustToContents = 0,
AdjustToContentsOnFirstShow = 1,
AdjustToMinimumContentsLength = 2,
AdjustToMinimumContentsLengthWithIcon = 3,
}
1 change: 1 addition & 0 deletions src/lib/QtEnums/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export { ScreenOrientation } from './ScreenOrientation';
export { ScrollBarPolicy } from './ScrollBarPolicy';
export { ScrollPhase } from './ScrollPhase';
export { ShortcutContext } from './ShortcutContext';
export { SizeAdjustPolicy } from './SizeAdjustPolicy';
export { SizeHint } from './SizeHint';
export { SizeMode } from './SizeMode';
export { SortOrder } from './SortOrder';
Expand Down
Loading