Add QDoubleSpinBox (#463)

Co-authored-by: wuxiaofeng <wuxiaofeng@erayt.com>
This commit is contained in:
feng8848 2020-03-25 03:15:57 +08:00 committed by GitHub
parent 67a7302595
commit cf4e221fdf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 242 additions and 0 deletions

View File

@ -71,6 +71,7 @@ add_library(${CORE_WIDGETS_ADDON} SHARED
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QComboBox/qcombobox_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QDateEdit/qdateedit_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QDateTimeEdit/qdatetimeedit_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QDoubleSpinBox/qdoublespinbox_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QFileDialog/qfiledialog_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QFrame/qframe_wrap.cpp"
"${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QListWidget/qlistwidget_wrap.cpp"
@ -126,6 +127,7 @@ add_library(${CORE_WIDGETS_ADDON} SHARED
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QDateEdit/ndateedit.hpp"
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QDateTimeEdit/ndatetimeedit.hpp"
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QDial/ndial.hpp"
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QDoubleSpinBox/ndoublespinbox.hpp"
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QFileDialog/nfiledialog.hpp"
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QFrame/nframe.hpp"
"${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QListWidget/nlistwidget.hpp"

View File

@ -0,0 +1,29 @@
#pragma once
#include <QDoubleSpinBox>
#include "Extras/Export/export.h"
#include "QtWidgets/QAbstractSpinBox/qabstractspinbox_macro.h"
#include "core/NodeWidget/nodewidget.h"
#include "napi.h"
class DLL_EXPORT NDoubleSpinBox : public QDoubleSpinBox, public NodeWidget {
Q_OBJECT
NODEWIDGET_IMPLEMENTATIONS(QDoubleSpinBox)
public:
using QDoubleSpinBox::QDoubleSpinBox; // inherit all constructors of
// QDoubleSpinBox
void connectSignalsToEventEmitter() {
QABSTRACTSPINBOX_SIGNALS
// Qt Connects: Implement all signal connects here
QObject::connect(
this, QOverload<double>::of(&QDoubleSpinBox::valueChanged),
[=](double val) {
Napi::Env env = this->emitOnNode.Env();
Napi::HandleScope scope(env);
this->emitOnNode.Call({Napi::String::New(env, "valueChanged"),
Napi::Number::New(env, val)});
});
}
};

View File

@ -0,0 +1,28 @@
#pragma once
#include <napi.h>
#include <QPointer>
#include "Extras/Export/export.h"
#include "QtWidgets/QAbstractSpinBox/qabstractspinbox_macro.h"
#include "ndoublespinbox.hpp"
class DLL_EXPORT QDoubleSpinBoxWrap
: public Napi::ObjectWrap<QDoubleSpinBoxWrap> {
QABSTRACTSPINBOX_WRAPPED_METHODS_DECLARATION
private:
QPointer<NDoubleSpinBox> instance;
public:
static Napi::Object init(Napi::Env env, Napi::Object exports);
QDoubleSpinBoxWrap(const Napi::CallbackInfo& info);
~QDoubleSpinBoxWrap();
NDoubleSpinBox* getInternalInstance();
// class constructor
static Napi::FunctionReference constructor;
// wrapped methods
Napi::Value setRange(const Napi::CallbackInfo& info);
Napi::Value textFromValue(const Napi::CallbackInfo& info);
Napi::Value valueFromText(const Napi::CallbackInfo& info);
};

View File

@ -0,0 +1,79 @@
#include "QtWidgets/QDoubleSpinBox/qdoublespinbox_wrap.h"
#include "Extras/Utils/nutils.h"
#include "QtWidgets/QWidget/qwidget_wrap.h"
Napi::FunctionReference QDoubleSpinBoxWrap::constructor;
Napi::Object QDoubleSpinBoxWrap::init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
char CLASSNAME[] = "QDoubleSpinBox";
Napi::Function func = DefineClass(
env, CLASSNAME,
{InstanceMethod("setRange", &QDoubleSpinBoxWrap::setRange),
InstanceMethod("textFromValue", &QDoubleSpinBoxWrap::textFromValue),
InstanceMethod("valueFromText", &QDoubleSpinBoxWrap::valueFromText),
QABSTRACTSPINBOX_WRAPPED_METHODS_EXPORT_DEFINE(QDoubleSpinBoxWrap)});
constructor = Napi::Persistent(func);
exports.Set(CLASSNAME, func);
return exports;
}
NDoubleSpinBox* QDoubleSpinBoxWrap::getInternalInstance() {
return this->instance;
}
QDoubleSpinBoxWrap::QDoubleSpinBoxWrap(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<QDoubleSpinBoxWrap>(info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
if (info.Length() == 1) {
Napi::Object parentObject = info[0].As<Napi::Object>();
NodeWidgetWrap* parentWidgetWrap =
Napi::ObjectWrap<NodeWidgetWrap>::Unwrap(parentObject);
this->instance =
new NDoubleSpinBox(parentWidgetWrap->getInternalInstance());
} else if (info.Length() == 0) {
this->instance = new NDoubleSpinBox();
} else {
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
}
this->rawData = extrautils::configureQWidget(
this->getInternalInstance(), this->getInternalInstance()->getFlexNode(),
true);
}
QDoubleSpinBoxWrap::~QDoubleSpinBoxWrap() {
extrautils::safeDelete(this->instance);
}
Napi::Value QDoubleSpinBoxWrap::setRange(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
double minimum = info[0].As<Napi::Number>().DoubleValue();
double maximum = info[1].As<Napi::Number>().DoubleValue();
this->instance->setRange(minimum, maximum);
return env.Null();
}
Napi::Value QDoubleSpinBoxWrap::textFromValue(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
double value = info[0].As<Napi::Number>().DoubleValue();
QString text = this->instance->textFromValue(value);
return Napi::String::New(env, text.toStdString());
}
Napi::Value QDoubleSpinBoxWrap::valueFromText(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::HandleScope scope(env);
std::string napiText = info[0].As<Napi::String>().Utf8Value();
QString text = QString::fromUtf8(napiText.c_str());
double value = this->instance->valueFromText(text);
return Napi::Number::New(env, value);
}

View File

@ -37,6 +37,7 @@
#include "QtWidgets/QDateTimeEdit/qdatetimeedit_wrap.h"
#include "QtWidgets/QDial/qdial_wrap.h"
#include "QtWidgets/QDialog/qdialog_wrap.h"
#include "QtWidgets/QDoubleSpinBox/qdoublespinbox_wrap.h"
#include "QtWidgets/QFileDialog/qfiledialog_wrap.h"
#include "QtWidgets/QFrame/qframe_wrap.h"
#include "QtWidgets/QGridLayout/qgridlayout_wrap.h"
@ -112,6 +113,7 @@ Napi::Object Main(Napi::Env env, Napi::Object exports) {
QCalendarWidgetWrap::init(env, exports);
QColorDialogWrap::init(env, exports);
QComboBoxWrap::init(env, exports);
QDoubleSpinBoxWrap::init(env, exports);
QBoxLayoutWrap::init(env, exports);
QDateEditWrap::init(env, exports);
QDateTimeEditWrap::init(env, exports);

View File

@ -41,6 +41,7 @@ export { QDateEdit } from './lib/QtWidgets/QDateEdit';
export { QDateTimeEdit, NodeDateTimeEdit, QDateTimeEditSignals } from './lib/QtWidgets/QDateTimeEdit';
export { QLabel, QLabelSignals } from './lib/QtWidgets/QLabel';
export { QDial, QDialSignals } from './lib/QtWidgets/QDial';
export { QDoubleSpinBox, QDoubleSpinBoxSignals } from './lib/QtWidgets/QDoubleSpinBox';
export { QFileDialog, QFileDialogSignals } from './lib/QtWidgets/QFileDialog';
export { QFrame, QFrameSignals, Shadow, Shape } from './lib/QtWidgets/QFrame';
export { QLineEdit, QLineEditSignals, EchoMode } from './lib/QtWidgets/QLineEdit';

View File

@ -0,0 +1,101 @@
import addon from '../utils/addon';
import { NodeWidget } from './QWidget';
import { NativeElement } from '../core/Component';
import { QAbstractSpinBox, QAbstractSpinBoxSignals, StepType } from './QAbstractSpinBox';
/**
> Create and control double spin box widgets.
* **This class is a JS wrapper around Qt's [QDoubleSpinBox class](https://doc.qt.io/qt-5/qdoublespinbox.html)**
A `QDoubleSpinBox` provides ability to add and manipulate native double spin box widgets.
### Example
```javascript
const { QDoubleSpinBox } = require("@nodegui/nodegui");
const doublespinBox = new QDoubleSpinBox();
```
*/
export class QDoubleSpinBox extends QAbstractSpinBox<QDoubleSpinBoxSignals> {
native: NativeElement;
constructor();
constructor(parent: NodeWidget<any>);
constructor(parent?: NodeWidget<any>) {
let native;
if (parent) {
native = new addon.QDoubleSpinBox(parent.native);
} else {
native = new addon.QDoubleSpinBox();
}
super(native);
this.native = native;
parent && this.setNodeParent(parent);
}
cleanText(): string {
return this.property('cleanText').toString();
}
setDecimals(prec: number): void {
this.setProperty('decimals', prec);
}
decimals(): number {
return this.property('decimals').toInt();
}
setMaximum(max: number): void {
this.setProperty('maximum', max);
}
maximum(): number {
return this.property('maximum').toDouble();
}
setMinimum(min: number): void {
this.setProperty('minimum', min);
}
minimum(): number {
return this.property('minimum').toDouble();
}
setPrefix(prefix: string): void {
this.setProperty('prefix', prefix);
}
prefix(): string {
return this.property('prefix').toString();
}
setSingleStep(val: number): void {
this.setProperty('singleStep', val);
}
singleStep(): number {
return this.property('singleStep').toDouble();
}
setStepType(stepType: StepType): void {
this.setProperty('stepType', stepType);
}
stepType(): StepType {
return this.property('stepType').toInt();
}
setSuffix(suffix: string): void {
this.setProperty('suffix', suffix);
}
suffix(): string {
return this.property('suffix').toString();
}
setValue(val: number): void {
this.setProperty('value', val);
}
value(): number {
return this.property('value').toDouble();
}
setRange(minimum: number, maximum: number): void {
this.native.setRange(minimum, maximum);
}
textFromValue(value: number): string {
return this.native.textFromValue(value);
}
valueFromText(text: string): number {
return this.native.valueFromText(text);
}
}
export interface QDoubleSpinBoxSignals extends QAbstractSpinBoxSignals {
valueChanged: (value: number) => void;
}