From 8d5b3ca476b1e7c5b9eea92ecd885f64bb5b4452 Mon Sep 17 00:00:00 2001 From: feng8848 <40539968+feng8848@users.noreply.github.com> Date: Mon, 13 Jan 2020 01:35:33 +0800 Subject: [PATCH] Add QSlider and other improvements (#348) --- CMakeLists.txt | 2 + .../QtGui/QKeySequence/qkeysequence_wrap.h | 7 ++ .../QAbstractButton/qabstractbutton_macro.h | 47 +++++----- .../QAbstractSlider/qabstractslider_macro.h | 93 +++++-------------- .../nodegui/QtWidgets/QSlider/nslider.hpp | 18 ++++ .../nodegui/QtWidgets/QSlider/qslider_wrap.h | 24 +++++ .../QtGui/QKeySequence/qkeysequence_wrap.cpp | 64 +++++++++++-- .../lib/QtWidgets/QSlider/qslider_wrap.cpp | 45 +++++++++ src/cpp/main.cpp | 2 + src/index.ts | 1 + src/lib/QtGui/QKeySequence.ts | 33 ++++++- src/lib/QtWidgets/QAbstractButton.ts | 72 +++++++++++++- src/lib/QtWidgets/QAbstractSlider.ts | 85 ++++++++++++++--- src/lib/QtWidgets/QSlider.ts | 60 ++++++++++++ 14 files changed, 434 insertions(+), 119 deletions(-) create mode 100644 src/cpp/include/nodegui/QtWidgets/QSlider/nslider.hpp create mode 100644 src/cpp/include/nodegui/QtWidgets/QSlider/qslider_wrap.h create mode 100644 src/cpp/lib/QtWidgets/QSlider/qslider_wrap.cpp create mode 100644 src/lib/QtWidgets/QSlider.ts diff --git a/CMakeLists.txt b/CMakeLists.txt index b608b2609..265c7d517 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -94,6 +94,7 @@ add_library(${CORE_WIDGETS_ADDON} SHARED "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QMenuBar/qmenubar_wrap.cpp" "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QMenu/qmenu_wrap.cpp" "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QGroupBox/qgroupbox_wrap.cpp" + "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QSlider/qslider_wrap.cpp" "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QTimeEdit/qtimeedit_wrap.cpp" "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QTreeWidget/qtreewidget_wrap.cpp" "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QTreeWidgetItem/qtreewidgetitem_wrap.cpp" @@ -137,6 +138,7 @@ add_library(${CORE_WIDGETS_ADDON} SHARED "${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QBoxLayout/nboxlayout.hpp" "${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QComboBox/ncombobox.hpp" "${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QGroupBox/ngroupbox.hpp" + "${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtWidgets/QSlider/nslider.hpp" "${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" diff --git a/src/cpp/include/nodegui/QtGui/QKeySequence/qkeysequence_wrap.h b/src/cpp/include/nodegui/QtGui/QKeySequence/qkeysequence_wrap.h index 12fcd1cd8..e0b8649cb 100644 --- a/src/cpp/include/nodegui/QtGui/QKeySequence/qkeysequence_wrap.h +++ b/src/cpp/include/nodegui/QtGui/QKeySequence/qkeysequence_wrap.h @@ -20,4 +20,11 @@ class QKeySequenceWrap : public Napi::ObjectWrap { QKeySequence *getInternalInstance(); // Wrapped methods Napi::Value count(const Napi::CallbackInfo &info); + Napi::Value isEmpty(const Napi::CallbackInfo &info); + Napi::Value matches(const Napi::CallbackInfo &info); + Napi::Value toString(const Napi::CallbackInfo &info); }; + +namespace StaticQKeySequenceWrapMethods { +Napi::Value fromQVariant(const Napi::CallbackInfo &info); +} // namespace StaticQKeySequenceWrapMethods diff --git a/src/cpp/include/nodegui/QtWidgets/QAbstractButton/qabstractbutton_macro.h b/src/cpp/include/nodegui/QtWidgets/QAbstractButton/qabstractbutton_macro.h index a6014f651..4d3b07d6e 100644 --- a/src/cpp/include/nodegui/QtWidgets/QAbstractButton/qabstractbutton_macro.h +++ b/src/cpp/include/nodegui/QtWidgets/QAbstractButton/qabstractbutton_macro.h @@ -6,32 +6,32 @@ /* - This macro adds common QAbstractScrollArea exported methods + This macro adds common QAbstractButton exported methods The exported methods are taken into this macro to avoid writing them in each and every widget we export. */ #ifndef QABSTRACTBUTTON_WRAPPED_METHODS_DECLARATION -#define QABSTRACTBUTTON_WRAPPED_METHODS_DECLARATION \ - QWIDGET_WRAPPED_METHODS_DECLARATION \ - Napi::Value setText(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - \ - Napi::String napiText = info[0].As(); \ - std::string text = napiText.Utf8Value(); \ - this->instance->setText(QString::fromUtf8(text.c_str())); \ - return env.Null(); \ - } \ - \ - Napi::Value setIcon(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - \ - Napi::Object iconObject = info[0].As(); \ - QIconWrap* iconWrap = Napi::ObjectWrap::Unwrap(iconObject); \ - this->instance->setIcon(*iconWrap->getInternalInstance()); \ - return env.Null(); \ +#define QABSTRACTBUTTON_WRAPPED_METHODS_DECLARATION \ + QWIDGET_WRAPPED_METHODS_DECLARATION \ + Napi::Value animateClick(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + int msec = info[0].As().Int32Value(); \ + this->instance->animateClick(msec); \ + return env.Null(); \ + } \ + Napi::Value click(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + this->instance->click(); \ + return env.Null(); \ + } \ + Napi::Value toggle(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + this->instance->toggle(); \ + return env.Null(); \ } #endif // QABSTRACTBUTTON_WRAPPED_METHODS_DECLARATION @@ -39,8 +39,9 @@ #ifndef QABSTRACTBUTTON_WRAPPED_METHODS_EXPORT_DEFINE #define QABSTRACTBUTTON_WRAPPED_METHODS_EXPORT_DEFINE(WidgetWrapName) \ QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(WidgetWrapName) \ - InstanceMethod("setText", &WidgetWrapName::setText), \ - InstanceMethod("setIcon", &WidgetWrapName::setIcon), + InstanceMethod("animateClick", &WidgetWrapName::animateClick), \ + InstanceMethod("click", &WidgetWrapName::click), \ + InstanceMethod("toggle", &WidgetWrapName::toggle), #endif // QABSTRACTBUTTON_WRAPPED_METHODS_EXPORT_DEFINE diff --git a/src/cpp/include/nodegui/QtWidgets/QAbstractSlider/qabstractslider_macro.h b/src/cpp/include/nodegui/QtWidgets/QAbstractSlider/qabstractslider_macro.h index 6289fcf35..8d75be7a4 100644 --- a/src/cpp/include/nodegui/QtWidgets/QAbstractSlider/qabstractslider_macro.h +++ b/src/cpp/include/nodegui/QtWidgets/QAbstractSlider/qabstractslider_macro.h @@ -10,83 +10,32 @@ */ #ifndef QABSTRACTSLIDER_WRAPPED_METHODS_DECLARATION -#define QABSTRACTSLIDER_WRAPPED_METHODS_DECLARATION \ - QWIDGET_WRAPPED_METHODS_DECLARATION \ - Napi::Value setSingleStep(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - Napi::Number step = info[0].As(); \ - this->instance->setSingleStep(step.Int32Value()); \ - return env.Null(); \ - } \ - \ - Napi::Value setMaximum(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - Napi::Number maximum = info[0].As(); \ - this->instance->setMaximum(maximum.Int32Value()); \ - return env.Null(); \ - } \ - \ - Napi::Value setMinimum(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - Napi::Number minimum = info[0].As(); \ - this->instance->setMinimum(minimum.Int32Value()); \ - return env.Null(); \ - } \ - \ - Napi::Value setValue(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - Napi::Number value = info[0].As(); \ - this->instance->setValue(value.Int32Value()); \ - return env.Null(); \ - } \ - \ - Napi::Value setOrientation(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - Napi::Number orientation = info[0].As(); \ - this->instance->setOrientation( \ - static_cast(orientation.Int32Value())); \ - return env.Null(); \ - } \ - \ - Napi::Value maximum(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - int maximum = this->instance->maximum(); \ - return Napi::Number::New(env, maximum); \ - } \ - \ - Napi::Value minimum(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - int minimum = this->instance->minimum(); \ - return Napi::Number::New(env, minimum); \ - } \ - \ - Napi::Value value(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - int value = this->instance->value(); \ - return Napi::Number::New(env, value); \ +#define QABSTRACTSLIDER_WRAPPED_METHODS_DECLARATION \ + QWIDGET_WRAPPED_METHODS_DECLARATION \ + Napi::Value triggerAction(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + int action = info[0].As().Int32Value(); \ + this->instance->triggerAction( \ + static_cast(action)); \ + return env.Null(); \ + } \ + Napi::Value setRange(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + int min = info[0].As().Int32Value(); \ + int max = info[1].As().Int32Value(); \ + this->instance->setRange(min, max); \ + return env.Null(); \ } #endif // QABSTRACTSLIDER_WRAPPED_METHODS_DECLARATION #ifndef QABSTRACTSLIDER_WRAPPED_METHODS_EXPORT_DEFINE -#define QABSTRACTSLIDER_WRAPPED_METHODS_EXPORT_DEFINE(WidgetWrapName) \ - QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(WidgetWrapName) \ - InstanceMethod("setSingleStep", &WidgetWrapName::setSingleStep), \ - InstanceMethod("setMaximum", &WidgetWrapName::setMaximum), \ - InstanceMethod("setMinimum", &WidgetWrapName::setMinimum), \ - InstanceMethod("setValue", &WidgetWrapName::setValue), \ - InstanceMethod("setOrientation", &WidgetWrapName::setOrientation), \ - InstanceMethod("maximum", &WidgetWrapName::maximum), \ - InstanceMethod("minimum", &WidgetWrapName::minimum), \ - InstanceMethod("value", &WidgetWrapName::value), +#define QABSTRACTSLIDER_WRAPPED_METHODS_EXPORT_DEFINE(WidgetWrapName) \ + QWIDGET_WRAPPED_METHODS_EXPORT_DEFINE(WidgetWrapName) \ + InstanceMethod("triggerAction", &WidgetWrapName::triggerAction), \ + InstanceMethod("setRange", &WidgetWrapName::setRange), #endif // QABSTRACTSLIDER_WRAPPED_METHODS_EXPORT_DEFINE diff --git a/src/cpp/include/nodegui/QtWidgets/QSlider/nslider.hpp b/src/cpp/include/nodegui/QtWidgets/QSlider/nslider.hpp new file mode 100644 index 000000000..f70da83b2 --- /dev/null +++ b/src/cpp/include/nodegui/QtWidgets/QSlider/nslider.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include + +#include "QtWidgets/QAbstractSlider/qabstractslider_macro.h" +#include "core/NodeWidget/nodewidget.h" + +class NSlider : public QSlider, public NodeWidget { + Q_OBJECT + NODEWIDGET_IMPLEMENTATIONS(QSlider) + public: + using QSlider::QSlider; // inherit all constructors of QSlider + + void connectSignalsToEventEmitter() { + // Qt Connects: Implement all signal connects here + QABSTRACT_SLIDER_SIGNALS + } +}; diff --git a/src/cpp/include/nodegui/QtWidgets/QSlider/qslider_wrap.h b/src/cpp/include/nodegui/QtWidgets/QSlider/qslider_wrap.h new file mode 100644 index 000000000..a850e0721 --- /dev/null +++ b/src/cpp/include/nodegui/QtWidgets/QSlider/qslider_wrap.h @@ -0,0 +1,24 @@ +#pragma once + +#include + +#include + +#include "QtWidgets/QAbstractSlider/qabstractslider_macro.h" +#include "QtWidgets/QWidget/qwidget_macro.h" +#include "nslider.hpp" + +class QSliderWrap : public Napi::ObjectWrap { + QABSTRACTSLIDER_WRAPPED_METHODS_DECLARATION + private: + QPointer instance; + + public: + static Napi::Object init(Napi::Env env, Napi::Object exports); + QSliderWrap(const Napi::CallbackInfo& info); + ~QSliderWrap(); + NSlider* getInternalInstance(); + // class constructor + static Napi::FunctionReference constructor; + // wrapped methods +}; diff --git a/src/cpp/lib/QtGui/QKeySequence/qkeysequence_wrap.cpp b/src/cpp/lib/QtGui/QKeySequence/qkeysequence_wrap.cpp index afcdf9b2b..ff8c57ab6 100644 --- a/src/cpp/lib/QtGui/QKeySequence/qkeysequence_wrap.cpp +++ b/src/cpp/lib/QtGui/QKeySequence/qkeysequence_wrap.cpp @@ -11,20 +11,30 @@ Napi::Object QKeySequenceWrap::init(Napi::Env env, Napi::Object exports) { Napi::Function func = DefineClass(env, CLASSNAME, {InstanceMethod("count", &QKeySequenceWrap::count), + InstanceMethod("isEmpty", &QKeySequenceWrap::isEmpty), + InstanceMethod("matches", &QKeySequenceWrap::matches), + InstanceMethod("toString$", &QKeySequenceWrap::toString), + StaticMethod("fromQVariant", + &StaticQKeySequenceWrapMethods::fromQVariant), COMPONENT_WRAPPED_METHODS_EXPORT_DEFINE(QKeySequenceWrap)}); constructor = Napi::Persistent(func); exports.Set(CLASSNAME, func); return exports; } -QKeySequenceWrap::QKeySequenceWrap(const Napi::CallbackInfo &info) +QKeySequenceWrap::QKeySequenceWrap(const Napi::CallbackInfo& info) : Napi::ObjectWrap(info) { Napi::Env env = info.Env(); Napi::HandleScope scope(env); if (info.Length() == 1) { - Napi::String sequenceString = info[0].As(); - QString keySequence = QString::fromStdString(sequenceString.Utf8Value()); - this->instance = std::make_unique(keySequence); + if (info[0].IsExternal()) { + this->instance = std::unique_ptr( + info[0].As>().Data()); + } else { + Napi::String sequenceString = info[0].As(); + QString keySequence = QString::fromStdString(sequenceString.Utf8Value()); + this->instance = std::make_unique(keySequence); + } } else if (info.Length() == 0) { this->instance = std::make_unique(); } else { @@ -36,14 +46,56 @@ QKeySequenceWrap::QKeySequenceWrap(const Napi::CallbackInfo &info) QKeySequenceWrap::~QKeySequenceWrap() { this->instance.reset(); } -QKeySequence *QKeySequenceWrap::getInternalInstance() { +QKeySequence* QKeySequenceWrap::getInternalInstance() { return this->instance.get(); } -Napi::Value QKeySequenceWrap::count(const Napi::CallbackInfo &info) { +Napi::Value QKeySequenceWrap::count(const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); Napi::HandleScope scope(env); int count = this->instance->count(); return Napi::Value::From(env, count); } + +Napi::Value QKeySequenceWrap::isEmpty(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + return Napi::Value::From(env, this->instance->isEmpty()); +} + +Napi::Value QKeySequenceWrap::matches(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + + Napi::Object keyObject = info[0].As(); + QKeySequenceWrap* keyWrap = + Napi::ObjectWrap::Unwrap(keyObject); + QKeySequence::SequenceMatch match = + this->instance->matches(*keyWrap->getInternalInstance()); + return Napi::Value::From(env, static_cast(match)); +} + +Napi::Value QKeySequenceWrap::toString(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + + int format = info[0].As().Int32Value(); + QString result = this->instance->toString( + static_cast(format)); + return Napi::Value::From(env, result.toStdString()); +} + +Napi::Value StaticQKeySequenceWrapMethods::fromQVariant( + const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + Napi::Object variantObject = info[0].As(); + QVariantWrap* variantWrap = + Napi::ObjectWrap::Unwrap(variantObject); + QVariant* variant = variantWrap->getInternalInstance(); + QKeySequence key = variant->value(); + auto instance = QKeySequenceWrap::constructor.New( + {Napi::External::New(env, new QKeySequence(key))}); + return instance; +} diff --git a/src/cpp/lib/QtWidgets/QSlider/qslider_wrap.cpp b/src/cpp/lib/QtWidgets/QSlider/qslider_wrap.cpp new file mode 100644 index 000000000..1b731d20a --- /dev/null +++ b/src/cpp/lib/QtWidgets/QSlider/qslider_wrap.cpp @@ -0,0 +1,45 @@ +#include "QtWidgets/QSlider/qslider_wrap.h" + +#include + +#include "Extras/Utils/nutils.h" +#include "QtWidgets/QWidget/qwidget_wrap.h" + +Napi::FunctionReference QSliderWrap::constructor; + +Napi::Object QSliderWrap::init(Napi::Env env, Napi::Object exports) { + Napi::HandleScope scope(env); + char CLASSNAME[] = "QSlider"; + Napi::Function func = + DefineClass(env, CLASSNAME, + {QABSTRACTSLIDER_WRAPPED_METHODS_EXPORT_DEFINE(QSliderWrap)}); + constructor = Napi::Persistent(func); + exports.Set(CLASSNAME, func); + return exports; +} + +NSlider* QSliderWrap::getInternalInstance() { return this->instance; } + +QSliderWrap::QSliderWrap(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 NSlider(parentWidgetWrap->getInternalInstance()); + } else if (info.Length() == 0) { + this->instance = new NSlider(); + } else { + Napi::TypeError::New(env, "Wrong number of arguments") + .ThrowAsJavaScriptException(); + } + + this->rawData = extrautils::configureQWidget( + this->getInternalInstance(), this->getInternalInstance()->getFlexNode(), + true); +} + +QSliderWrap::~QSliderWrap() { extrautils::safeDelete(this->instance); } diff --git a/src/cpp/main.cpp b/src/cpp/main.cpp index 0e1aec75a..0ba1f82bf 100644 --- a/src/cpp/main.cpp +++ b/src/cpp/main.cpp @@ -53,6 +53,7 @@ #include "QtWidgets/QRadioButton/qradiobutton_wrap.h" #include "QtWidgets/QScrollArea/qscrollarea_wrap.h" #include "QtWidgets/QShortcut/qshortcut_wrap.h" +#include "QtWidgets/QSlider/qslider_wrap.h" #include "QtWidgets/QSpinBox/qspinbox_wrap.h" #include "QtWidgets/QStackedWidget/qstackedwidget_wrap.h" #include "QtWidgets/QSystemTrayIcon/qsystemtrayicon_wrap.h" @@ -135,6 +136,7 @@ Napi::Object Main(Napi::Env env, Napi::Object exports) { QMenuWrap::init(env, exports); QMenuBarWrap::init(env, exports); QMessageBoxWrap::init(env, exports); + QSliderWrap::init(env, exports); QTimeEditWrap::init(env, exports); QButtonGroupWrap::init(env, exports); return exports; diff --git a/src/index.ts b/src/index.ts index 915f9cafc..4bc083849 100644 --- a/src/index.ts +++ b/src/index.ts @@ -52,6 +52,7 @@ export { QMenu, QMenuSignals } from './lib/QtWidgets/QMenu'; export { QMenuBar, QMenuBarSignals } from './lib/QtWidgets/QMenuBar'; export { QPlainTextEdit, QPlainTextEditSignals, LineWrapMode } from './lib/QtWidgets/QPlainTextEdit'; export { QScrollArea, QScrollAreaSignals } from './lib/QtWidgets/QScrollArea'; +export { QSlider, QSliderSignals, TickPosition } from './lib/QtWidgets/QSlider'; export { QTimeEdit } from './lib/QtWidgets/QTimeEdit'; export { QTreeWidget, QTreeWidgetSignals } from './lib/QtWidgets/QTreeWidget'; export { QTreeWidgetItem } from './lib/QtWidgets/QTreeWidgetItem'; diff --git a/src/lib/QtGui/QKeySequence.ts b/src/lib/QtGui/QKeySequence.ts index f505bf277..158ad191f 100644 --- a/src/lib/QtGui/QKeySequence.ts +++ b/src/lib/QtGui/QKeySequence.ts @@ -1,5 +1,7 @@ import addon from '../utils/addon'; +import { checkIfNativeElement } from '../utils/helpers'; import { Component, NativeElement } from '../core/Component'; +import { QVariant } from '../QtCore/QVariant'; /** @@ -18,11 +20,15 @@ const keySequence = new QKeySequence(`Ctrl+L`); export class QKeySequence extends Component { native: NativeElement; constructor(); + constructor(native: NativeElement); constructor(keySequence: string); - constructor(keySequence?: string) { + constructor(arg?: string | NativeElement) { super(); - if (typeof keySequence === 'string') { + if (typeof arg === 'string') { + const keySequence = arg; this.native = new addon.QKeySequence(keySequence); + } else if (checkIfNativeElement(arg)) { + this.native = arg as NativeElement; } else { this.native = new addon.QKeySequence(); } @@ -30,4 +36,27 @@ export class QKeySequence extends Component { count(): number { return this.native.count(); } + isEmpty(): boolean { + return this.native.isEmpty(); + } + matches(seq: QKeySequence): SequenceMatch { + return this.native.matches(seq.native); + } + toString(format: SequenceFormat): string { + return this.native.toString$(format); + } + static fromQVariant(variant: QVariant): QKeySequence { + return new QKeySequence(addon.QKeySequence.fromQVariant(variant.native)); + } +} + +export enum SequenceMatch { + NoMatch, + PartialMatch, + ExactMatch, +} + +export enum SequenceFormat { + NativeText, + PortableText, } diff --git a/src/lib/QtWidgets/QAbstractButton.ts b/src/lib/QtWidgets/QAbstractButton.ts index 720bbc72f..b2e61b295 100644 --- a/src/lib/QtWidgets/QAbstractButton.ts +++ b/src/lib/QtWidgets/QAbstractButton.ts @@ -1,6 +1,7 @@ import { NodeWidget, QWidgetSignals } from './QWidget'; import { QIcon } from '../QtGui/QIcon'; import { QSize } from '../QtCore/QSize'; +import { QKeySequence } from '../QtGui/QKeySequence'; /** @@ -13,11 +14,63 @@ It is inherited by QCheckBox, QPushButton, QRadioButton, and QToolButton. */ export abstract class QAbstractButton extends NodeWidget { - setText(text: string): void { - this.native.setText(text); + animateClick(msec: number): void { + this.native.animateClick(msec); + } + click(): void { + this.native.click(); + } + toggle(): void { + this.native.toggle(); + } + setAutoExclusive(enable: boolean): void { + this.setProperty('autoExclusive', enable); + } + autoExclusive(): boolean { + return this.property('autoExclusive').toBool(); + } + setAutoRepeat(enable: boolean): void { + this.setProperty('autoRepeat', enable); + } + autoRepeat(): boolean { + return this.property('autoRepeat').toBool(); + } + setAutoRepeatDelay(delay: number): void { + this.setProperty('autoRepeatDelay', delay); + } + autoRepeatDelay(): number { + return this.property('autoRepeatDelay').toInt(); + } + setAutoRepeatInterval(interval: number): void { + this.setProperty('autoRepeatInterval', interval); + } + autoRepeatInterval(): number { + return this.property('autoRepeatInterval').toInt(); + } + setCheckable(checkable: boolean): void { + this.setProperty('checkable', checkable); + } + isCheckable(): boolean { + return this.property('checkable').toBool(); + } + setChecked(checked: boolean): void { + this.setProperty('checked', checked); + } + isChecked(): boolean { + return this.property('checked').toBool(); + } + setDown(down: boolean): void { + this.setProperty('down', down); + } + isDown(): boolean { + return this.property('down').toBool(); } setIcon(icon: QIcon): void { - this.native.setIcon(icon.native); + this.setProperty('icon', icon.native); + } + icon(): QIcon { + const icon = this.property('icon'); + return QIcon.fromQVariant(icon); } setIconSize(iconSize: QSize): void { this.setProperty('iconSize', iconSize.native); @@ -26,6 +79,19 @@ export abstract class QAbstractButton ex const iconSize = this.property('iconSize'); return QSize.fromQVariant(iconSize); } + setShortcut(key: QKeySequence): void { + this.setProperty('shortcut', key.native); + } + shortcut(): QKeySequence { + const key = this.property('shortcut'); + return QKeySequence.fromQVariant(key); + } + setText(text: string): void { + this.setProperty('text', text); + } + text(): string { + return this.property('text').toString(); + } } export interface QAbstractButtonSignals extends QWidgetSignals { clicked: (checked: boolean) => void; diff --git a/src/lib/QtWidgets/QAbstractSlider.ts b/src/lib/QtWidgets/QAbstractSlider.ts index 90df52944..17a89f19a 100644 --- a/src/lib/QtWidgets/QAbstractSlider.ts +++ b/src/lib/QtWidgets/QAbstractSlider.ts @@ -15,30 +15,89 @@ QAbstractSlider will list all methods and properties that are common to all slid */ export abstract class QAbstractSlider extends NodeWidget { - setSingleStep(step: number): void { - this.native.setSingleStep(step); + triggerAction(action: SliderAction): void { + this.native.triggerAction(action); + } + setRange(min: number, max: number): void { + this.native.setRange(min, max); + } + setInvertedAppearance(inverted: boolean): void { + this.setProperty('invertedAppearance', inverted); + } + invertedAppearance(): boolean { + return this.property('invertedAppearance').toBool(); + } + setInvertedControls(inverted: boolean): void { + this.setProperty('invertedControls', inverted); + } + invertedControls(): boolean { + return this.property('invertedControls').toBool(); } setMaximum(maximum: number): void { - this.native.setMaximum(maximum); + this.setProperty('maximum', maximum); } maximum(): number { - return this.native.maximum(); + return this.property('maximum').toInt(); } setMinimum(minimum: number): void { - this.native.setMinimum(minimum); + this.setProperty('minimum', minimum); } minimum(): number { - return this.native.minimum(); - } - setValue(value: number): void { - this.native.setValue(value); - } - value(): number { - return this.native.value(); + return this.property('minimum').toInt(); } setOrientation(orientation: Orientation): void { - this.native.setOrientation(orientation); + this.setProperty('orientation', orientation); } + orientation(): Orientation { + return this.property('orientation').toInt(); + } + setPageStep(step: number): void { + this.setProperty('pageStep', step); + } + pageStep(): number { + return this.property('pageStep').toInt(); + } + setSingleStep(step: number): void { + this.setProperty('singleStep', step); + } + singleStep(): number { + return this.property('singleStep').toInt(); + } + setSliderDown(down: boolean): void { + this.setProperty('sliderDown', down); + } + isSliderDown(): boolean { + return this.property('sliderDown').toBool(); + } + setSliderPosition(position: number): void { + this.setProperty('sliderPosition', position); + } + sliderPosition(): number { + return this.property('sliderPosition').toInt(); + } + setTracking(enable: boolean): void { + this.setProperty('tracking', enable); + } + hasTracking(): boolean { + return this.property('tracking').toBool(); + } + setValue(value: number): void { + this.setProperty('value', value); + } + value(): number { + return this.property('value').toInt(); + } +} + +export enum SliderAction { + SliderNoAction, + SliderSingleStepAdd, + SliderSingleStepSub, + SliderPageStepAdd, + SliderPageStepSub, + SliderToMinimum, + SliderToMaximum, + SliderMove, } export interface QAbstractSliderSignals extends QWidgetSignals { diff --git a/src/lib/QtWidgets/QSlider.ts b/src/lib/QtWidgets/QSlider.ts new file mode 100644 index 000000000..f3fde92db --- /dev/null +++ b/src/lib/QtWidgets/QSlider.ts @@ -0,0 +1,60 @@ +import addon from '../utils/addon'; +import { NodeWidget } from './QWidget'; +import { NativeElement } from '../core/Component'; +import { QAbstractSlider, QAbstractSliderSignals } from './QAbstractSlider'; + +/** + +> Create and control slider widgets. + +* **This class is a JS wrapper around Qt's [QSlider class](https://doc.qt.io/qt-5/qslider.html)** + +A `QSlider` provides ability to add and manipulate native slider widgets. + +### Example + +```javascript +const { QSlider } = require("@nodegui/nodegui"); + +const slider = new QSlider(); +``` + */ +export class QSlider extends QAbstractSlider { + native: NativeElement; + constructor(); + constructor(parent: NodeWidget); + constructor(parent?: NodeWidget) { + let native; + if (parent) { + native = new addon.QSlider(parent.native); + } else { + native = new addon.QSlider(); + } + super(native); + this.native = native; + this.setNodeParent(parent); + } + setTickInterval(ti: number): void { + this.setProperty('tickInterval', ti); + } + tickInterval(): number { + return this.property('tickInterval').toInt(); + } + setTickPosition(position: TickPosition): void { + this.setProperty('tickPosition', position); + } + tickPosition(): TickPosition { + return this.property('tickPosition').toInt(); + } +} + +export enum TickPosition { + NoTicks, + TicksAbove, + TicksBelow, + TicksBothSides, + TicksLeft = TicksAbove, + TicksRight = TicksBelow, +} + +export type QSliderSignals = QAbstractSliderSignals;