nodeguy/src/cpp/lib/QtWidgets/QTabWidget/qtabwidget_wrap.cpp
Shubham Zanwar 57cf0d4836
improv: adding the ability to change the tab text (#421)
* adding the setTabText to the QTabWidgetWrap definition

* implementing the setTabText funciton in the cpp module

* implementing the setTabText in the ts module

* adding a demo for the tab text change feature

* fixing lint issues

* exporting the QSettings enums
2020-02-25 09:36:58 +01:00

123 lines
4.4 KiB
C++

#include "QtWidgets/QTabWidget/qtabwidget_wrap.h"
#include <QWidget>
#include "Extras/Utils/nutils.h"
#include "QtGui/QIcon/qicon_wrap.h"
#include "QtWidgets/QWidget/qwidget_wrap.h"
Napi::FunctionReference QTabWidgetWrap::constructor;
Napi::Object QTabWidgetWrap::init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
char CLASSNAME[] = "QTabWidget";
Napi::Function func = DefineClass(
env, CLASSNAME,
{InstanceMethod("addTab", &QTabWidgetWrap::addTab),
InstanceMethod("setTabPosition", &QTabWidgetWrap::setTabPosition),
InstanceMethod("setTabText", &QTabWidgetWrap::setTabText),
InstanceMethod("setCurrentIndex", &QTabWidgetWrap::setCurrentIndex),
InstanceMethod("currentIndex", &QTabWidgetWrap::currentIndex),
InstanceMethod("removeTab", &QTabWidgetWrap::removeTab),
InstanceMethod("setTabsClosable", &QTabWidgetWrap::setTabsClosable),
QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QTabWidgetWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
return exports;
}
NTabWidget* QTabWidgetWrap::getInternalInstance() { return this->instance; }
QTabWidgetWrap::~QTabWidgetWrap() { extrautils::safeDelete(this->instance); }
QTabWidgetWrap::QTabWidgetWrap(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<QTabWidgetWrap>(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 NTabWidget(
parentWidgetWrap
->getInternalInstance()); // this sets the parent to current widget
} else if (info.Length() == 0) {
this->instance = new NTabWidget();
} else {
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = extrautils::configureQWidget(
this->getInternalInstance(), this->getInternalInstance()->getFlexNode(),
true);
}
Napi::Value QTabWidgetWrap::addTab(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::Object pageObject = info[0].As<Napi::Object>();
Napi::Object iconObject = info[1].As<Napi::Object>();
Napi::String napiLabel = info[2].As<Napi::String>();
std::string label = napiLabel.Utf8Value();
QWidgetWrap* pageObjectWrap =
Napi::ObjectWrap<QWidgetWrap>::Unwrap(pageObject);
QIconWrap* iconWrap = Napi::ObjectWrap<QIconWrap>::Unwrap(iconObject);
int index =
this->instance->addTab(pageObjectWrap->getInternalInstance(),
*iconWrap->getInternalInstance(), label.c_str());
return Napi::Number::New(env, index);
}
Napi::Value QTabWidgetWrap::setTabPosition(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
int tabPosition = info[0].As<Napi::Number>().Int32Value();
this->instance->setTabPosition(
static_cast<QTabWidget::TabPosition>(tabPosition));
return env.Null();
}
Napi::Value QTabWidgetWrap::setTabText(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
int tabIndex = info[0].As<Napi::Number>().Int32Value();
Napi::String napiLabel = info[1].As<Napi::String>();
std::string label = napiLabel.Utf8Value();
this->instance->setTabText(tabIndex, label.c_str());
return env.Null();
}
Napi::Value QTabWidgetWrap::setCurrentIndex(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::Number index = info[0].As<Napi::Number>();
this->instance->setCurrentIndex(index.Int32Value());
return env.Null();
}
Napi::Value QTabWidgetWrap::currentIndex(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
int value = this->instance->currentIndex();
return Napi::Number::New(env, value);
}
Napi::Value QTabWidgetWrap::removeTab(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::Number index = info[0].As<Napi::Number>();
this->instance->removeTab(index.Int32Value());
return env.Null();
}
Napi::Value QTabWidgetWrap::setTabsClosable(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
Napi::Boolean closable = info[0].As<Napi::Boolean>();
this->instance->setTabsClosable(closable.Value());
return env.Null();
}