From 50c55e090e151c67c400f9e56807adb067e1c498 Mon Sep 17 00:00:00 2001 From: robertkruis Date: Sat, 18 Jan 2020 11:51:45 +0100 Subject: [PATCH] QStatusBar support. Fixes #346 (#349) * Add QStatusBar * Add QStatusBar signals/events * Lint fixes --- CMakeLists.txt | 2 + .../QtWidgets/QMainWindow/qmainwindow_wrap.h | 2 + .../QtWidgets/QStatusBar/nstatusbar.hpp | 31 +++ .../QtWidgets/QStatusBar/qstatusbar_wrap.h | 37 ++++ .../QMainWindow/qmainwindow_wrap.cpp | 30 +++ .../QtWidgets/QStatusBar/qstatusbar_wrap.cpp | 208 ++++++++++++++++++ src/cpp/main.cpp | 2 + src/demo.ts | 65 ++---- src/index.ts | 1 + src/lib/QtWidgets/QMainWindow.ts | 27 +++ src/lib/QtWidgets/QStatusBar.ts | 158 +++++++++++++ 11 files changed, 512 insertions(+), 51 deletions(-) create mode 100644 src/cpp/include/nodegui/QtWidgets/QStatusBar/nstatusbar.hpp create mode 100644 src/cpp/include/nodegui/QtWidgets/QStatusBar/qstatusbar_wrap.h create mode 100644 src/cpp/lib/QtWidgets/QStatusBar/qstatusbar_wrap.cpp create mode 100644 src/lib/QtWidgets/QStatusBar.ts diff --git a/CMakeLists.txt b/CMakeLists.txt index 265c7d517..4ded53f39 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -100,6 +100,7 @@ add_library(${CORE_WIDGETS_ADDON} SHARED "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QTreeWidgetItem/qtreewidgetitem_wrap.cpp" "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QMessageBox/qmessagebox_wrap.cpp" "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QButtonGroup/qbuttongroup_wrap.cpp" + "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QStatusBar/qstatusbar_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" @@ -142,6 +143,7 @@ add_library(${CORE_WIDGETS_ADDON} SHARED "${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QTimeEdit/ntimeedit.hpp" "${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QTreeWidget/ntreewidget.hpp" "${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QButtonGroup/nbuttongroup.hpp" + "${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QStatusBar/nstatusbar.hpp" ) diff --git a/src/cpp/include/nodegui/QtWidgets/QMainWindow/qmainwindow_wrap.h b/src/cpp/include/nodegui/QtWidgets/QMainWindow/qmainwindow_wrap.h index fab35566a..f48f3eaf1 100644 --- a/src/cpp/include/nodegui/QtWidgets/QMainWindow/qmainwindow_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QMainWindow/qmainwindow_wrap.h @@ -27,4 +27,6 @@ class QMainWindowWrap : public Napi::ObjectWrap { Napi::Value menuBar(const Napi::CallbackInfo& info); Napi::Value setMenuWidget(const Napi::CallbackInfo& info); Napi::Value center(const Napi::CallbackInfo& info); + Napi::Value setStatusBar(const Napi::CallbackInfo& info); + Napi::Value statusBar(const Napi::CallbackInfo& info); }; diff --git a/src/cpp/include/nodegui/QtWidgets/QStatusBar/nstatusbar.hpp b/src/cpp/include/nodegui/QtWidgets/QStatusBar/nstatusbar.hpp new file mode 100644 index 000000000..d8aa2935c --- /dev/null +++ b/src/cpp/include/nodegui/QtWidgets/QStatusBar/nstatusbar.hpp @@ -0,0 +1,31 @@ +#pragma once + +#include + +#include "QtWidgets/QWidget/qwidget_macro.h" +#include "core/NodeWidget/nodewidget.h" +#include "napi.h" + +class NStatusBar : public QStatusBar, public NodeWidget { + Q_OBJECT + + public: + NODEWIDGET_IMPLEMENTATIONS(QStatusBar) + using QStatusBar::QStatusBar; // inherit all constructors of QStatusBar + + void connectSignalsToEventEmitter() { + QObject::connect( + this, &QStatusBar::messageChanged, [=](const QString &message) { + Napi::Env env = this->emitOnNode.Env(); + Napi::HandleScope scope(env); + + auto newMessage = message.toStdString(); + auto newMessageWrap = Napi::Value::From(env, newMessage); + + this->emitOnNode.Call( + {Napi::String::New(env, "messageChanged"), newMessageWrap}); + }); + + QWIDGET_SIGNALS + } +}; \ No newline at end of file diff --git a/src/cpp/include/nodegui/QtWidgets/QStatusBar/qstatusbar_wrap.h b/src/cpp/include/nodegui/QtWidgets/QStatusBar/qstatusbar_wrap.h new file mode 100644 index 000000000..c6861eccd --- /dev/null +++ b/src/cpp/include/nodegui/QtWidgets/QStatusBar/qstatusbar_wrap.h @@ -0,0 +1,37 @@ +#pragma once + +#include + +#include + +#include "QtWidgets/QWidget/qwidget_macro.h" +#include "nstatusbar.hpp" + +class QStatusBarWrap : public Napi::ObjectWrap { + QWIDGET_WRAPPED_METHODS_DECLARATION + + private: + QPointer instance; + + public: + static Napi::Object init(Napi::Env env, Napi::Object exports); + static Napi::FunctionReference constructor; + static Napi::Value fromQStatusBar(Napi::Env env, QStatusBar *statusBar); + + QStatusBarWrap(const Napi::CallbackInfo &info); + ~QStatusBarWrap(); + + NStatusBar *getInternalInstance(); + + // Wrapped methods + Napi::Value addPermanentWidget(const Napi::CallbackInfo &info); + Napi::Value addWidget(const Napi::CallbackInfo &info); + Napi::Value clearMessage(const Napi::CallbackInfo &info); + Napi::Value currentMessage(const Napi::CallbackInfo &info); + Napi::Value insertPermanentWidget(const Napi::CallbackInfo &info); + Napi::Value insertWidget(const Napi::CallbackInfo &info); + Napi::Value isSizeGripEnabled(const Napi::CallbackInfo &info); + Napi::Value removeWidget(const Napi::CallbackInfo &info); + Napi::Value showMessage(const Napi::CallbackInfo &info); + Napi::Value setSizeGripEnabled(const Napi::CallbackInfo &info); +}; \ No newline at end of file diff --git a/src/cpp/lib/QtWidgets/QMainWindow/qmainwindow_wrap.cpp b/src/cpp/lib/QtWidgets/QMainWindow/qmainwindow_wrap.cpp index afb4643b4..3df7ded2b 100644 --- a/src/cpp/lib/QtWidgets/QMainWindow/qmainwindow_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QMainWindow/qmainwindow_wrap.cpp @@ -5,6 +5,7 @@ #include "Extras/Utils/nutils.h" #include "QtWidgets/QMenuBar/qmenubar_wrap.h" +#include "QtWidgets/QStatusBar/qstatusbar_wrap.h" #include "QtWidgets/QWidget/qwidget_wrap.h" Napi::FunctionReference QMainWindowWrap::constructor; @@ -19,6 +20,8 @@ Napi::Object QMainWindowWrap::init(Napi::Env env, Napi::Object exports) { InstanceMethod("setMenuBar", &QMainWindowWrap::setMenuBar), InstanceMethod("setMenuWidget", &QMainWindowWrap::setMenuWidget), InstanceMethod("center", &QMainWindowWrap::center), + InstanceMethod("setStatusBar", &QMainWindowWrap::setStatusBar), + InstanceMethod("statusBar", &QMainWindowWrap::statusBar), QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QMainWindowWrap) }); @@ -106,3 +109,30 @@ Napi::Value QMainWindowWrap::center(const Napi::CallbackInfo& info) { return env.Null(); } + +Napi::Value QMainWindowWrap::setStatusBar(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + + Napi::Object statusBarArg = info[0].As(); + + QStatusBar* statusBar = nullptr; + if (!statusBarArg.IsUndefined() && !statusBarArg.IsNull()) { + QStatusBarWrap* statusBarWrap = + Napi::ObjectWrap::Unwrap(statusBarArg); + statusBar = statusBarWrap->getInternalInstance(); + } + + this->instance->setStatusBar(statusBar); + + return env.Null(); +} + +Napi::Value QMainWindowWrap::statusBar(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + + QStatusBar* statusBar = this->instance->statusBar(); + + return QStatusBarWrap::fromQStatusBar(env, statusBar); +} diff --git a/src/cpp/lib/QtWidgets/QStatusBar/qstatusbar_wrap.cpp b/src/cpp/lib/QtWidgets/QStatusBar/qstatusbar_wrap.cpp new file mode 100644 index 000000000..9dee90421 --- /dev/null +++ b/src/cpp/lib/QtWidgets/QStatusBar/qstatusbar_wrap.cpp @@ -0,0 +1,208 @@ +#include "QtWidgets/QStatusBar/qstatusbar_wrap.h" + +#include +#include + +#include + +Napi::FunctionReference QStatusBarWrap::constructor; + +Napi::Object QStatusBarWrap::init(Napi::Env env, Napi::Object exports) { + Napi::HandleScope scope(env); + char CLASSNAME[] = "QStatusBar"; + Napi::Function func = DefineClass( + env, CLASSNAME, + {InstanceMethod("addPermanentWidget", + &QStatusBarWrap::addPermanentWidget), + InstanceMethod("addWidget", &QStatusBarWrap::addWidget), + InstanceMethod("clearMessage", &QStatusBarWrap::clearMessage), + InstanceMethod("currentMessage", &QStatusBarWrap::currentMessage), + InstanceMethod("insertPermanentWidget", + &QStatusBarWrap::insertPermanentWidget), + InstanceMethod("insertWidget", &QStatusBarWrap::insertWidget), + InstanceMethod("isSizeGripEnabled", &QStatusBarWrap::isSizeGripEnabled), + InstanceMethod("removeWidget", &QStatusBarWrap::removeWidget), + InstanceMethod("showMessage", &QStatusBarWrap::showMessage), + InstanceMethod("setSizeGripEnabled", + &QStatusBarWrap::setSizeGripEnabled), + QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(QStatusBarWrap)}); + constructor = Napi::Persistent(func); + exports.Set(CLASSNAME, func); + return exports; +} + +NStatusBar *QStatusBarWrap::getInternalInstance() { return this->instance; } + +Napi::Value QStatusBarWrap::fromQStatusBar(Napi::Env env, + QStatusBar *statusBar) { + // The item might be a nullptr, therefore use env.Null() as return value. + if (statusBar == nullptr) { + return env.Null(); + } + + auto statusBarWrap = QStatusBarWrap::constructor.New( + {Napi::External::New(env, statusBar), + Napi::Boolean::New(env, true)}); +} + +QStatusBarWrap::~QStatusBarWrap() { extrautils::safeDelete(this->instance); } + +QStatusBarWrap::QStatusBarWrap(const Napi::CallbackInfo &info) + : Napi::ObjectWrap(info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + + if (info.Length() == 1) { + Napi::Object parentObject = info[0].As(); + QWidgetWrap *parentWidgetWrap = + Napi::ObjectWrap::Unwrap(parentObject); + + this->instance = new NStatusBar(parentWidgetWrap->getInternalInstance()); + } else if (info.Length() == 0) { + this->instance = new NStatusBar(); + } else { + Napi::TypeError::New(env, "Wrong number of arguments") + .ThrowAsJavaScriptException(); + } + + this->rawData = extrautils::configureQWidget( + this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), + true); +} + +Napi::Value QStatusBarWrap::addPermanentWidget(const Napi::CallbackInfo &info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + + Napi::Object widgetArg = info[0].As(); + Napi::Number stretchArg = info[1].As(); + + QWidgetWrap *widgetWrap = Napi::ObjectWrap::Unwrap(widgetArg); + QWidget *widget = widgetWrap->getInternalInstance(); + int stretch = stretchArg.Int32Value(); + + this->instance->addPermanentWidget(widget, stretch); + + return env.Null(); +} + +Napi::Value QStatusBarWrap::addWidget(const Napi::CallbackInfo &info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + + Napi::Object widgetArg = info[0].As(); + Napi::Number stretchArg = info[1].As(); + + QWidgetWrap *widgetWrap = Napi::ObjectWrap::Unwrap(widgetArg); + QWidget *widget = widgetWrap->getInternalInstance(); + int stretch = stretchArg.Int32Value(); + + this->instance->addWidget(widget, stretch); + + return env.Null(); +} + +Napi::Value QStatusBarWrap::clearMessage(const Napi::CallbackInfo &info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + + this->instance->clearMessage(); + + return env.Null(); +} + +Napi::Value QStatusBarWrap::currentMessage(const Napi::CallbackInfo &info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + + std::string currentMessage = this->instance->currentMessage().toStdString(); + + return Napi::String::New(env, currentMessage); +} + +Napi::Value QStatusBarWrap::insertPermanentWidget( + const Napi::CallbackInfo &info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + + Napi::Number indexArg = info[0].As(); + Napi::Object widgetArg = info[1].As(); + Napi::Number stretchArg = info[2].As(); + + int index = indexArg.Int32Value(); + QWidgetWrap *widgetWrap = Napi::ObjectWrap::Unwrap(widgetArg); + QWidget *widget = widgetWrap->getInternalInstance(); + int stretch = stretchArg.Int32Value(); + + int targetIndex = + this->instance->insertPermanentWidget(index, widget, stretch); + + return Napi::Value::From(env, targetIndex); +} + +Napi::Value QStatusBarWrap::insertWidget(const Napi::CallbackInfo &info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + + Napi::Number indexArg = info[0].As(); + Napi::Object widgetArg = info[1].As(); + Napi::Number stretchArg = info[2].As(); + + int index = indexArg.Int32Value(); + QWidgetWrap *widgetWrap = Napi::ObjectWrap::Unwrap(widgetArg); + QWidget *widget = widgetWrap->getInternalInstance(); + int stretch = stretchArg.Int32Value(); + + int targetIndex = this->instance->insertWidget(index, widget, stretch); + + return Napi::Value::From(env, targetIndex); +} + +Napi::Value QStatusBarWrap::isSizeGripEnabled(const Napi::CallbackInfo &info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + + bool isEnabled = this->instance->isSizeGripEnabled(); + + return Napi::Value::From(env, isEnabled); +} + +Napi::Value QStatusBarWrap::removeWidget(const Napi::CallbackInfo &info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + + Napi::Object widgetArg = info[0].As(); + + QWidgetWrap *widgetWrap = Napi::ObjectWrap::Unwrap(widgetArg); + QWidget *widget = widgetWrap->getInternalInstance(); + + this->instance->removeWidget(widget); + + return env.Null(); +} + +Napi::Value QStatusBarWrap::showMessage(const Napi::CallbackInfo &info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + + Napi::String message = info[0].As(); + Napi::Number timeout = info[1].As(); + + this->instance->showMessage(QString::fromStdString(message.Utf8Value()), + timeout.Int32Value()); + + return env.Null(); +} + +Napi::Value QStatusBarWrap::setSizeGripEnabled(const Napi::CallbackInfo &info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + + Napi::Boolean enableArg = info[0].As(); + + bool enable = enableArg.ToBoolean(); + + this->instance->setSizeGripEnabled(enable); + + return env.Null(); +} \ No newline at end of file diff --git a/src/cpp/main.cpp b/src/cpp/main.cpp index 0ba1f82bf..9178ef220 100644 --- a/src/cpp/main.cpp +++ b/src/cpp/main.cpp @@ -56,6 +56,7 @@ #include "QtWidgets/QSlider/qslider_wrap.h" #include "QtWidgets/QSpinBox/qspinbox_wrap.h" #include "QtWidgets/QStackedWidget/qstackedwidget_wrap.h" +#include "QtWidgets/QStatusBar/qstatusbar_wrap.h" #include "QtWidgets/QSystemTrayIcon/qsystemtrayicon_wrap.h" #include "QtWidgets/QTabWidget/qtabwidget_wrap.h" #include "QtWidgets/QTableWidget/qtablewidget_wrap.h" @@ -139,6 +140,7 @@ Napi::Object Main(Napi::Env env, Napi::Object exports) { QSliderWrap::init(env, exports); QTimeEditWrap::init(env, exports); QButtonGroupWrap::init(env, exports); + QStatusBarWrap::init(env, exports); return exports; } diff --git a/src/demo.ts b/src/demo.ts index cbd975636..07b687d25 100644 --- a/src/demo.ts +++ b/src/demo.ts @@ -2,6 +2,7 @@ import { QWidget, QMainWindow, FlexLayout, QTreeWidget, QTreeWidgetItem } from ' import { ItemFlag, CheckState } from './lib/QtEnums'; import { QSpinBox } from './lib/QtWidgets/QSpinBox'; import { QLineEdit } from './lib/QtWidgets/QLineEdit'; +import { QStatusBar } from './lib/QtWidgets/QStatusBar'; const win = new QMainWindow(); const center = new QWidget(); @@ -9,59 +10,21 @@ const layout = new FlexLayout(); center.setLayout(layout); win.setCentralWidget(center); -const tree = new QTreeWidget(); -tree.setColumnCount(2); -tree.setHeaderLabels(['Properties', 'Value']); -center.layout?.addWidget(tree); +const statusBar = new QStatusBar(); +const spinBox = new QSpinBox(); +statusBar.addPermanentWidget(spinBox); +statusBar.addEventListener('messageChanged', message => console.log(`Status bar message changed to: ${message}`)); -const root1 = new QTreeWidgetItem(tree); -root1.setText(0, 'Option 1'); -root1.setText(1, 'Option 1 Description'); - -const item1 = new QTreeWidgetItem(); -item1.setText(0, 'enabled'); -item1.setFlags(ItemFlag.ItemIsUserCheckable | ItemFlag.ItemIsEnabled); -item1.setCheckState(1, CheckState.Checked); -root1.addChild(item1); - -const item1_1 = new QTreeWidgetItem(); -item1_1.setText(0, 'height'); -root1.addChild(item1_1); -tree.setItemWidget(item1_1, 1, new QSpinBox()); - -const item1_2 = new QTreeWidgetItem(); -item1_2.setText(0, 'name'); -root1.addChild(item1_2); -tree.setItemWidget(item1_2, 1, new QLineEdit()); - -const root2 = new QTreeWidgetItem(tree); -root2.setText(0, 'Option 2'); -root2.setText(1, 'Option 2 Description'); - -const item2 = new QTreeWidgetItem(); -item2.setText(0, 'width'); -item2.setText(1, '300'); -root2.addChild(item2); - -const item2_1 = new QTreeWidgetItem(); -item2_1.setText(0, 'height'); -item2_1.setText(1, '200'); -root2.addChild(item2_1); - -/* const buttonGroup = new QButtonGroup(center); -const t: any[] = []; -for (let i = 0; i < 4; i++) { - const radioButton = new QRadioButton(); - radioButton.setText('Button #' + (i + 1)); - center.layout?.addWidget(radioButton); - t.push(radioButton); - buttonGroup.addButton(radioButton, i); -} */ +win.setStatusBar(statusBar); win.show(); -/* buttonGroup.addEventListener('buttonClicked', (id: number) => { - console.log('Button #' + (id + 1) + ' clicked!'); - buttonGroup.removeButton(t[0]); -}); */ + +statusBar.showMessage('Hello World', 3000); + +setTimeout(_ => { + statusBar.removeWidget(spinBox); + statusBar.showMessage('Removed spinBox'); +}, 3000); + (global as any).win = win; setInterval(() => null, 1000); diff --git a/src/index.ts b/src/index.ts index 4bc083849..8b0f357da 100644 --- a/src/index.ts +++ b/src/index.ts @@ -69,6 +69,7 @@ export { export { QAction, QActionSignals } from './lib/QtWidgets/QAction'; export { QShortcut, QShortcutSignals } from './lib/QtWidgets/QShortcut'; export { QGroupBox, QGroupBoxSignals } from './lib/QtWidgets/QGroupBox'; +export { QStatusBar, QStatusBarSignals } from './lib/QtWidgets/QStatusBar'; // Core export { QDate } from './lib/QtCore/QDate'; export { QDateTime } from './lib/QtCore/QDateTime'; diff --git a/src/lib/QtWidgets/QMainWindow.ts b/src/lib/QtWidgets/QMainWindow.ts index f802f81ff..a8c270f05 100644 --- a/src/lib/QtWidgets/QMainWindow.ts +++ b/src/lib/QtWidgets/QMainWindow.ts @@ -3,6 +3,7 @@ import { NodeWidget, QWidgetSignals } from './QWidget'; import { NativeElement } from '../core/Component'; import { NodeLayout } from './QLayout'; import { QMenuBar } from './QMenuBar'; +import { QStatusBar } from './QStatusBar'; /** @@ -34,6 +35,7 @@ export class QMainWindow extends NodeWidget { native: NativeElement; public centralWidget?: NodeWidget | null; private _menuBar?: QMenuBar; + private _statusBar?: QStatusBar | null; constructor(); constructor(parent: NodeWidget); constructor(parent?: NodeWidget) { @@ -89,6 +91,31 @@ export class QMainWindow extends NodeWidget { center(): void { this.native.center(); } + + /** + * Sets the status bar for the main window to statusbar. + * Note: QMainWindow takes ownership of the statusbar pointer and deletes it at the appropriate time. + * @param statusBar The status bar. + */ + setStatusBar(statusBar: QStatusBar): void { + this.native.setStatusBar(statusBar.native); + this._statusBar = statusBar; + } + + /** + * Removes the status bar from the main window. + */ + removeStatusBar(): void { + this.native.setStatusBar(null); + this._statusBar = null; + } + + /** + * Returns the status bar for the main window. + */ + statusBar(): QStatusBar { + return this.native.statusBar(); + } } export type QMainWindowSignals = QWidgetSignals; diff --git a/src/lib/QtWidgets/QStatusBar.ts b/src/lib/QtWidgets/QStatusBar.ts new file mode 100644 index 000000000..d3571b45a --- /dev/null +++ b/src/lib/QtWidgets/QStatusBar.ts @@ -0,0 +1,158 @@ +import addon from '../utils/addon'; +import { NodeWidget, QWidgetSignals, QWidget } from './QWidget'; +import { NativeElement } from '../core/Component'; + +export interface QStatusBarSignals extends QWidgetSignals { + messageChanged: (message: string) => void; +} + +/** + +> Create and control progress bar widgets. + +* **This class is a JS wrapper around Qt's [QStatusBar class](https://doc.qt.io/qt-5/qstatusbar.html)** + +A `QStatusBar` provides ability to add and manipulate the status bar of a window. + +### Example + +```javascript +const { QStatusBar } = require("@nodegui/nodegui"); + +const progressBar = new QStatusBar(); +``` + */ +export class QStatusBar extends NodeWidget { + native: NativeElement; + permanentWidgets: Set; + widgets: Set; + constructor(); + constructor(parent: NodeWidget); + constructor(parent?: NodeWidget) { + let native; + if (parent) { + native = new addon.QStatusBar(parent.native); + } else { + native = new addon.QStatusBar(); + } + + super(native); + this.native = native; + this.setNodeParent(parent); + + this.permanentWidgets = new Set(); + this.widgets = new Set(); + } + + /** + * Adds the given widget permanently to this status bar, reparenting the widget if it isn't already a child of this QStatusBar object. The stretch parameter is used to compute a suitable size for the given widget as the status bar grows and shrinks. The default stretch factor is 0, i.e giving the widget a minimum of space. + * Permanently means that the widget may not be obscured by temporary messages. It is is located at the far right of the status bar. + * @param widget The widget to permanently add to this status bar. + * @param stretch Used to compute a suitable size for the given widget as the status bar grows and shrinks. The default stretch factor is 0, i.e giving the widget a minimum of space. + */ + addPermanentWidget(widget: QWidget, stretch = 0): void { + this.native.addPermanentWidget(widget.native, stretch); + + if (!this.permanentWidgets.has(widget.native)) { + this.permanentWidgets.add(widget.native); + } + } + + /** + * Adds the given widget to this status bar, reparenting the widget if it isn't already a child of this QStatusBar object. The stretch parameter is used to compute a suitable size for the given widget as the status bar grows and shrinks. The default stretch factor is 0, i.e giving the widget a minimum of space. + * The widget is located to the far left of the first permanent widget (see addPermanentWidget()) and may be obscured by temporary messages. + * @param widget The widget to add to this status bar. + * @param stretch Used to compute a suitable size for the given widget as the status bar grows and shrinks. The default stretch factor is 0, i.e giving the widget a minimum of space. + */ + addWidget(widget: QWidget, stretch = 0): void { + this.native.addWidget(widget.native, stretch); + + if (!this.widgets.has(widget.native)) { + this.widgets.add(widget.native); + } + } + + /** + * Removes any temporary message being shown. + */ + clearMessage(): void { + this.native.clearMessage(); + } + + /** + * Returns the temporary message currently shown, or an empty string if there is no such message. + */ + currentMessage(): string { + return this.native.currentMessage(); + } + + /** + * Inserts the given widget at the given index permanently to this status bar, reparenting the widget if it isn't already a child of this QStatusBar object. If index is out of range, the widget is appended (in which case it is the actual index of the widget that is returned). + * The stretch parameter is used to compute a suitable size for the given widget as the status bar grows and shrinks. The default stretch factor is 0, i.e giving the widget a minimum of space. + * Permanently means that the widget may not be obscured by temporary messages. It is is located at the far right of the status bar. + * @param index The index at which to insert the given widget permanently. + * @param widget The widget to insert into this status bar permanently. + * @param stretch Used to compute a suitable size for the given widget as the status bar grows and shrinks. The default stretch factor is 0, i.e giving the widget a minimum of space. + */ + insertPermanentWidget(index: number, widget: QWidget, stretch = 0): number { + const insertionIndex = this.native.insertPermanentWidget(index, widget.native, stretch); + if (!this.permanentWidgets.has(widget.native)) { + this.permanentWidgets.add(widget.native); + } + + return insertionIndex; + } + + /** + * Inserts the given widget at the given index to this status bar, reparenting the widget if it isn't already a child of this QStatusBar object. If index is out of range, the widget is appended (in which case it is the actual index of the widget that is returned). + * The stretch parameter is used to compute a suitable size for the given widget as the status bar grows and shrinks. The default stretch factor is 0, i.e giving the widget a minimum of space. + * The widget is located to the far left of the first permanent widget (see addPermanentWidget()) and may be obscured by temporary messages. + * @param index The index at which to insert the given widget. + * @param widget The widget to insert into this status bar. + * @param stretch Used to compute a suitable size for the given widget as the status bar grows and shrinks. The default stretch factor is 0, i.e giving the widget a minimum of space. + */ + insertWidget(index: number, widget: QWidget, stretch = 0): number { + const insertionIndex = this.native.insertWidget(index, widget.native, stretch); + if (!this.widgets.has(widget.native)) { + this.widgets.add(widget.native); + } + + return insertionIndex; + } + + /** + * Determines whether the QSizeGrip in the bottom-right corner of the status bar is enabled + */ + isSizeGripEnabled(): boolean { + return this.native.isSizeGripEnabled(); + } + + /** + * Removes the specified widget from the status bar. + * Note: This function does not delete the widget but hides it. To add the widget again, you must call both the addWidget() and show() functions. + * @param widget The widget to remove from this status bar. + */ + removeWidget(widget: QWidget): void { + this.native.removeWidget(widget.native); + this.widgets.delete(widget.native); + this.permanentWidgets.delete(widget.native); + } + + /** + * Hides the normal status indications and displays the given message for the specified number of milli-seconds (timeout). If timeout is 0 (default), the message remains displayed until the clearMessage() slot is called or until the showMessage() slot is called again to change the message. + * Note that showMessage() is called to show temporary explanations of tool tip texts, so passing a timeout of 0 is not sufficient to display a permanent message. + * @param message The message to display. + * @param timeout The number of milliseconds to display the message. + */ + showMessage(message: string, timeout = 0): void { + this.native.showMessage(message, timeout); + } + + /** + * Enables or disables the QSizeGrip in the bottom-right corner of this status bar. + * @param enabled Determines if the QSizeGrip should be enabled or disabled. + */ + setSizeGripEnabled(enabled: boolean): void { + this.native.setSizeGripEnabled(enabled); + } +}