diff --git a/binding.gyp b/binding.gyp index 40e0ce6..13117d5 100644 --- a/binding.gyp +++ b/binding.gyp @@ -12,6 +12,7 @@ "src/QtGui/qpixmap.cpp", "src/QtGui/qcombobox.cpp", "src/QtGui/qplaintextedit.cpp", + "src/QtGui/qpushbutton.cpp", "src/misc.cpp", "src/utils/unwrapper.cpp", ], @@ -59,10 +60,12 @@ "buttonReleasedCallback_.Call({}); +} + +Napi::Object QPushButtonWrap::Init(Napi::Env env, Napi::Object exports) +{ + Napi::HandleScope scope(env); + // clang-format off + Napi::Function func = DefineClass(env, "QPushButton", { + InstanceMethod("setText", &QPushButtonWrap::setText), + InstanceMethod("text", &QPushButtonWrap::text), + InstanceMethod("buttonReleasedEvent", &QPushButtonWrap::buttonReleasedEvent), + QWIDGET_JS_DEFINES(QPushButtonWrap) + }); + // clang-format on + + constructor = Napi::Persistent(func); + constructor.SuppressDestruct(); + + exports.Set("QPushButton", func); + return exports; +} + +QPushButtonWrap::QPushButtonWrap(const Napi::CallbackInfo &info) : Napi::ObjectWrap(info), slotHandler(this) +{ + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + + if (info.Length() > 0) + { + QWidget *parent = unwrap(info[0]); + q_ = new QPushButtonImpl(parent, env); + } + else + { + QPushButtonImpl *q_parent = 0; + q_ = new QPushButtonImpl(q_parent, env); + } + q_->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); +} + +QPushButtonWrap::~QPushButtonWrap() +{ + q_ = NULL; +} + +Napi::Value QPushButtonWrap::setText(const Napi::CallbackInfo &info) +{ + q_->setText(QString::fromStdString(info[0].ToString().Utf8Value())); + return Napi::Value(); +} + +Napi::Value QPushButtonWrap::text(const Napi::CallbackInfo &info) +{ + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + + return Napi::String::New(env, q_->text().toStdString()); +} + +Napi::Value QPushButtonWrap::buttonReleasedEvent(const Napi::CallbackInfo &info) +{ + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + + buttonReleasedCallback_ = Napi::Persistent(info[0].As()); + QObject::connect(q_, SIGNAL(released()), &slotHandler, SLOT(buttonReleasedSlot())); + + return Napi::Value(); +} + +#include "qpushbutton.moc" + +// QWidget functions +QWIDGET_BASE_FUNCS(QPushButtonWrap) \ No newline at end of file diff --git a/src/QtGui/qpushbutton.hpp b/src/QtGui/qpushbutton.hpp new file mode 100644 index 0000000..396a0cd --- /dev/null +++ b/src/QtGui/qpushbutton.hpp @@ -0,0 +1,51 @@ +#ifndef QPUSHBUTTONWRAP_H +#define QPUSHBUTTONWRAP_H +#include +#include +#include +#include "qwidget.hpp" +#include "qwidget_macros.hpp" +#include "../utils/unwrapper.hpp" +#include + +QWIDGET_IMPL_DEF(QPushButton) + +class QPushButtonWrap; +class SlotHandlerPushButton : public QObject +{ + + Q_OBJECT + +public: + SlotHandlerPushButton(QPushButtonWrap *element) : element(element){}; + QPushButtonWrap *element; + +public slots: + void buttonReleasedSlot(); +}; + +class QPushButtonWrap : public Napi::ObjectWrap +{ +public: + static Napi::Object Init(Napi::Env env, Napi::Object exports); + + QPushButtonWrap(const Napi::CallbackInfo &info); + ~QPushButtonWrap(); + + QPushButtonImpl *q_; + + Napi::FunctionReference buttonReleasedCallback_; + +private: + static Napi::FunctionReference constructor; + + Napi::Value setText(const Napi::CallbackInfo &info); + Napi::Value text(const Napi::CallbackInfo &info); + Napi::Value buttonReleasedEvent(const Napi::CallbackInfo &info); + + SlotHandlerPushButton slotHandler; + + // QWidget Funcs + QWIDGET_DEFS +}; +#endif \ No newline at end of file diff --git a/src/QtGui/qpushbutton.moc b/src/QtGui/qpushbutton.moc new file mode 100644 index 0000000..4d9ecc8 --- /dev/null +++ b/src/QtGui/qpushbutton.moc @@ -0,0 +1,115 @@ +/**************************************************************************** +** Meta object code from reading C++ file 'qpushbutton.hpp' +** +** Created by: The Qt Meta Object Compiler version 67 (Qt 5.9.5) +** +** WARNING! All changes made in this file will be lost! +*****************************************************************************/ + +#include "qpushbutton.hpp" +#include +#include +#if !defined(Q_MOC_OUTPUT_REVISION) +#error "The header file 'qpushbutton.hpp' doesn't include ." +#elif Q_MOC_OUTPUT_REVISION != 67 +#error "This file was generated using the moc from 5.9.5. It" +#error "cannot be used with the include files from this version of Qt." +#error "(The moc has changed too much.)" +#endif + +QT_BEGIN_MOC_NAMESPACE +QT_WARNING_PUSH +QT_WARNING_DISABLE_DEPRECATED +struct qt_meta_stringdata_SlotHandlerPushButton_t { + QByteArrayData data[3]; + char stringdata0[42]; +}; +#define QT_MOC_LITERAL(idx, ofs, len) \ + Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(len, \ + qptrdiff(offsetof(qt_meta_stringdata_SlotHandlerPushButton_t, stringdata0) + ofs \ + - idx * sizeof(QByteArrayData)) \ + ) +static const qt_meta_stringdata_SlotHandlerPushButton_t qt_meta_stringdata_SlotHandlerPushButton = { + { +QT_MOC_LITERAL(0, 0, 21), // "SlotHandlerPushButton" +QT_MOC_LITERAL(1, 22, 18), // "buttonReleasedSlot" +QT_MOC_LITERAL(2, 41, 0) // "" + + }, + "SlotHandlerPushButton\0buttonReleasedSlot\0" + "" +}; +#undef QT_MOC_LITERAL + +static const uint qt_meta_data_SlotHandlerPushButton[] = { + + // content: + 7, // revision + 0, // classname + 0, 0, // classinfo + 1, 14, // methods + 0, 0, // properties + 0, 0, // enums/sets + 0, 0, // constructors + 0, // flags + 0, // signalCount + + // slots: name, argc, parameters, tag, flags + 1, 0, 19, 2, 0x0a /* Public */, + + // slots: parameters + QMetaType::Void, + + 0 // eod +}; + +void SlotHandlerPushButton::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a) +{ + if (_c == QMetaObject::InvokeMetaMethod) { + SlotHandlerPushButton *_t = static_cast(_o); + Q_UNUSED(_t) + switch (_id) { + case 0: _t->buttonReleasedSlot(); break; + default: ; + } + } + Q_UNUSED(_a); +} + +const QMetaObject SlotHandlerPushButton::staticMetaObject = { + { &QObject::staticMetaObject, qt_meta_stringdata_SlotHandlerPushButton.data, + qt_meta_data_SlotHandlerPushButton, qt_static_metacall, nullptr, nullptr} +}; + + +const QMetaObject *SlotHandlerPushButton::metaObject() const +{ + return QObject::d_ptr->metaObject ? QObject::d_ptr->dynamicMetaObject() : &staticMetaObject; +} + +void *SlotHandlerPushButton::qt_metacast(const char *_clname) +{ + if (!_clname) return nullptr; + if (!strcmp(_clname, qt_meta_stringdata_SlotHandlerPushButton.stringdata0)) + return static_cast(this); + return QObject::qt_metacast(_clname); +} + +int SlotHandlerPushButton::qt_metacall(QMetaObject::Call _c, int _id, void **_a) +{ + _id = QObject::qt_metacall(_c, _id, _a); + if (_id < 0) + return _id; + if (_c == QMetaObject::InvokeMetaMethod) { + if (_id < 1) + qt_static_metacall(this, _c, _id, _a); + _id -= 1; + } else if (_c == QMetaObject::RegisterMethodArgumentMetaType) { + if (_id < 1) + *reinterpret_cast(_a[0]) = -1; + _id -= 1; + } + return _id; +} +QT_WARNING_POP +QT_END_MOC_NAMESPACE diff --git a/src/qt.cpp b/src/qt.cpp index 294308b..faa7476 100644 --- a/src/qt.cpp +++ b/src/qt.cpp @@ -7,6 +7,7 @@ #include "QtGui/qpixmap.hpp" #include "QtGui/qcombobox.hpp" #include "QtGui/qplaintextedit.hpp" +#include "QtGui/qpushbutton.hpp" #include "misc.hpp" Napi::Object Init(Napi::Env env, Napi::Object exports) @@ -19,6 +20,7 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) QPixmapWrap::Init(env, exports); QComboBoxWrap::Init(env, exports); QPlainTextEditWrap::Init(env, exports); + QPushButtonWrap::Init(env, exports); MiscInit(env, exports); return exports; }