tabs: adding insertTab function to insert tabs at a given index (#462)
* add cpp method to insert tab at a given index * add ts method to insert tab at given index * add insert tab demo * fixing lint issues in cpp code * fix: passing the native widgets to insertTab
This commit is contained in:
parent
d80d27a10c
commit
3b53e8b69d
@ -10,9 +10,8 @@
|
||||
#include "QtWidgets/QListWidget/nlistwidget.hpp"
|
||||
|
||||
class DLL_EXPORT QListWidgetWrap : public Napi::ObjectWrap<QListWidgetWrap> {
|
||||
QListView_WRAPPED_METHODS_DECLARATION
|
||||
private:
|
||||
QPointer<NListWidget> instance;
|
||||
QListView_WRAPPED_METHODS_DECLARATION private : QPointer<NListWidget>
|
||||
instance;
|
||||
|
||||
public:
|
||||
static Napi::Object init(Napi::Env env, Napi::Object exports);
|
||||
|
||||
@ -32,7 +32,7 @@ class DLL_EXPORT QTabBarWrap : public Napi::ObjectWrap<QTabBarWrap> {
|
||||
Napi::Value removeTab(const Napi::CallbackInfo& info);
|
||||
Napi::Value setTabButton(const Napi::CallbackInfo& info);
|
||||
Napi::Value setTabData(const Napi::CallbackInfo& info);
|
||||
Napi::Value tabData(const Napi::CallbackInfo& info);
|
||||
Napi::Value tabData(const Napi::CallbackInfo& info);
|
||||
Napi::Value setTabIcon(const Napi::CallbackInfo& info);
|
||||
Napi::Value tabIcon(const Napi::CallbackInfo& info);
|
||||
Napi::Value setTabText(const Napi::CallbackInfo& info);
|
||||
|
||||
@ -22,6 +22,7 @@ class DLL_EXPORT QTabWidgetWrap : public Napi::ObjectWrap<QTabWidgetWrap> {
|
||||
static Napi::FunctionReference constructor;
|
||||
// wrapped methods
|
||||
Napi::Value addTab(const Napi::CallbackInfo &info);
|
||||
Napi::Value insertTab(const Napi::CallbackInfo &info);
|
||||
Napi::Value indexOf(const Napi::CallbackInfo &info);
|
||||
Napi::Value setTabPosition(const Napi::CallbackInfo &info);
|
||||
Napi::Value setCurrentIndex(const Napi::CallbackInfo &info);
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
#include "QtGui/QClipboard/qclipboard_wrap.h"
|
||||
|
||||
#include <QtGui/QPixmap/qpixmap_wrap.h>
|
||||
|
||||
#include "Extras/Utils/nutils.h"
|
||||
|
||||
Napi::FunctionReference QClipboardWrap::constructor;
|
||||
|
||||
@ -27,7 +27,7 @@ Napi::Object QTabBarWrap::init(Napi::Env env, Napi::Object exports) {
|
||||
InstanceMethod("removeTab", &QTabBarWrap::removeTab),
|
||||
InstanceMethod("setTabButton", &QTabBarWrap::setTabButton),
|
||||
InstanceMethod("setTabData", &QTabBarWrap::setTabData),
|
||||
InstanceMethod("tabData", &QTabBarWrap::tabData),
|
||||
InstanceMethod("tabData", &QTabBarWrap::tabData),
|
||||
InstanceMethod("setTabIcon", &QTabBarWrap::setTabIcon),
|
||||
InstanceMethod("tabIcon", &QTabBarWrap::tabIcon),
|
||||
InstanceMethod("setTabText", &QTabBarWrap::setTabText),
|
||||
@ -134,8 +134,8 @@ Napi::Value QTabBarWrap::insertTab(const Napi::CallbackInfo& info) {
|
||||
QIconWrap* iconWrap = Napi::ObjectWrap<QIconWrap>::Unwrap(iconObject);
|
||||
std::string napiText = info[2].As<Napi::String>().Utf8Value();
|
||||
QString text = QString::fromUtf8(napiText.c_str());
|
||||
result = this->instance->insertTab(
|
||||
index, *iconWrap->getInternalInstance(), text);
|
||||
result = this->instance->insertTab(index, *iconWrap->getInternalInstance(),
|
||||
text);
|
||||
} else {
|
||||
int index = info[0].As<Napi::Number>().Int32Value();
|
||||
std::string napiText = info[1].As<Napi::String>().Utf8Value();
|
||||
@ -190,7 +190,8 @@ Napi::Value QTabBarWrap::setTabButton(const Napi::CallbackInfo& info) {
|
||||
int index = info[0].As<Napi::Number>().Int32Value();
|
||||
int position = info[1].As<Napi::Number>().Int32Value();
|
||||
Napi::Object widgetObject = info[2].As<Napi::Object>();
|
||||
NodeWidgetWrap* widgetWrap = Napi::ObjectWrap<NodeWidgetWrap>::Unwrap(widgetObject);
|
||||
NodeWidgetWrap* widgetWrap =
|
||||
Napi::ObjectWrap<NodeWidgetWrap>::Unwrap(widgetObject);
|
||||
this->instance->setTabButton(index,
|
||||
static_cast<QTabBar::ButtonPosition>(position),
|
||||
widgetWrap->getInternalInstance());
|
||||
|
||||
@ -14,6 +14,7 @@ Napi::Object QTabWidgetWrap::init(Napi::Env env, Napi::Object exports) {
|
||||
Napi::Function func = DefineClass(
|
||||
env, CLASSNAME,
|
||||
{InstanceMethod("addTab", &QTabWidgetWrap::addTab),
|
||||
InstanceMethod("insertTab", &QTabWidgetWrap::insertTab),
|
||||
InstanceMethod("setTabPosition", &QTabWidgetWrap::setTabPosition),
|
||||
InstanceMethod("indexOf", &QTabWidgetWrap::indexOf),
|
||||
InstanceMethod("setTabText", &QTabWidgetWrap::setTabText),
|
||||
@ -74,6 +75,26 @@ Napi::Value QTabWidgetWrap::addTab(const Napi::CallbackInfo& info) {
|
||||
return Napi::Number::New(env, index);
|
||||
}
|
||||
|
||||
Napi::Value QTabWidgetWrap::insertTab(const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
|
||||
int tabPosition = info[0].As<Napi::Number>().Int32Value();
|
||||
Napi::Object pageObject = info[1].As<Napi::Object>();
|
||||
Napi::Object iconObject = info[2].As<Napi::Object>();
|
||||
Napi::String napiLabel = info[3].As<Napi::String>();
|
||||
std::string label = napiLabel.Utf8Value();
|
||||
|
||||
NodeWidgetWrap* pageObjectWrap =
|
||||
Napi::ObjectWrap<NodeWidgetWrap>::Unwrap(pageObject);
|
||||
QIconWrap* iconWrap = Napi::ObjectWrap<QIconWrap>::Unwrap(iconObject);
|
||||
|
||||
int index = this->instance->insertTab(
|
||||
tabPosition, pageObjectWrap->getInternalInstance(),
|
||||
*iconWrap->getInternalInstance(), label.c_str());
|
||||
return Napi::Number::New(env, index);
|
||||
}
|
||||
|
||||
Napi::Value QTabWidgetWrap::indexOf(const Napi::CallbackInfo& info) {
|
||||
Napi::Env env = info.Env();
|
||||
Napi::HandleScope scope(env);
|
||||
|
||||
81
src/demo.ts
81
src/demo.ts
@ -1,63 +1,28 @@
|
||||
import {
|
||||
QMainWindow,
|
||||
QWidget,
|
||||
QPushButton,
|
||||
QLineEdit,
|
||||
FlexLayout,
|
||||
QApplication,
|
||||
QClipboardMode,
|
||||
QLabel,
|
||||
QPixmap,
|
||||
} from './index';
|
||||
import { QColorDialog } from './lib/QtWidgets/QColorDialog';
|
||||
import { QColor } from './lib/QtGui/QColor';
|
||||
import { QTabWidget } from './lib/QtWidgets/QTabWidget';
|
||||
import { QLabel } from './lib/QtWidgets/QLabel';
|
||||
import { QIcon } from './lib/QtGui/QIcon';
|
||||
|
||||
const win = new QMainWindow();
|
||||
const center = new QWidget();
|
||||
const label = new QLabel();
|
||||
const textInput = new QLineEdit();
|
||||
const getBtn = new QPushButton();
|
||||
const setBtn = new QPushButton();
|
||||
const colorBtn = new QPushButton();
|
||||
const icon = new QIcon('/Users/atulr/Project/nodegui/nodegui/src/lib/QtGui/__tests__/assets/nodegui.png');
|
||||
const title1 = 'title 1';
|
||||
const title2 = 'title 2';
|
||||
const tabContent1 = new QLabel();
|
||||
const tabContent2 = new QLabel();
|
||||
const newTabContent = new QLabel();
|
||||
|
||||
//----------
|
||||
label.setText('Copy any image onto the clipboard and click `Get clipbard image button`');
|
||||
getBtn.setText('Get clipboard image');
|
||||
getBtn.addEventListener('clicked', () => {
|
||||
const clip = QApplication.clipboard();
|
||||
const pixmap = clip.pixmap(QClipboardMode.Clipboard);
|
||||
label.setPixmap(pixmap);
|
||||
});
|
||||
tabContent1.setText('test text1');
|
||||
tabContent2.setText('test text2');
|
||||
newTabContent.setText('new inserted tab');
|
||||
|
||||
//--------------
|
||||
textInput.setPlaceholderText('Enter absolute image path to load into clipboard');
|
||||
setBtn.setText('Set clipboard image');
|
||||
setBtn.addEventListener('clicked', () => {
|
||||
const clip = QApplication.clipboard();
|
||||
const pixmap = new QPixmap();
|
||||
pixmap.load(textInput.text());
|
||||
clip.setPixmap(pixmap, QClipboardMode.Clipboard);
|
||||
label.setText(`Loaded image at ${textInput.text()} to global clipboard`);
|
||||
});
|
||||
const tabs = new QTabWidget();
|
||||
|
||||
//--------------
|
||||
colorBtn.setText('Open color dialog');
|
||||
colorBtn.addEventListener('clicked', () => {
|
||||
const dialog = new QColorDialog();
|
||||
dialog.setCurrentColor(new QColor('white'));
|
||||
dialog.exec();
|
||||
const color = dialog.currentColor();
|
||||
console.log(color.red(), color.green(), color.blue());
|
||||
});
|
||||
tabs.addTab(tabContent1, icon, title1);
|
||||
tabs.addTab(tabContent2, icon, title2);
|
||||
|
||||
center.setLayout(new FlexLayout());
|
||||
center.layout?.addWidget(textInput);
|
||||
center.layout?.addWidget(setBtn);
|
||||
center.layout?.addWidget(getBtn);
|
||||
center.layout?.addWidget(colorBtn);
|
||||
center.layout?.addWidget(label);
|
||||
center.setInlineStyle(`width: 400; height: 400;`);
|
||||
win.setCentralWidget(center);
|
||||
win.show();
|
||||
win.setFixedSize(400, 400);
|
||||
(global as any).win = win;
|
||||
// demo for the tab text change
|
||||
tabs.setTabText(0, 'new title 1');
|
||||
|
||||
tabs.insertTab(0, newTabContent, icon, 'new inserted tab');
|
||||
|
||||
tabs.show();
|
||||
|
||||
(global as any).tabs = tabs;
|
||||
|
||||
@ -49,6 +49,12 @@ export class QTabWidget extends NodeWidget<QTabWidgetSignals> {
|
||||
return index;
|
||||
}
|
||||
|
||||
insertTab(index: number, page: NodeWidget<any>, icon: QIcon, label: string): number {
|
||||
const newIndex = this.native.insertTab(index, page.native, icon.native, label);
|
||||
this.tabs.splice(index, 0, page);
|
||||
return newIndex;
|
||||
}
|
||||
|
||||
indexOf(widget: NodeWidget<any>): number {
|
||||
return this.native.indexOf(widget.native);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user