diff --git a/CMakeLists.txt b/CMakeLists.txt index f4649531d..885b6d972 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -159,6 +159,7 @@ add_library(${CORE_WIDGETS_ADDON} SHARED "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QSvgWidget/qsvgwidget_wrap.cpp" "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QStyleFactory/qstylefactory_wrap.cpp" "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QSplitter/qsplitter_wrap.cpp" + "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QAbstractItemDelegate/qabstractitemdelegate_wrap.cpp" # Custom widgets (include them for automoc since they contain Q_OBJECT) "${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtCore/QAbstractItemModel/nabstractitemmodel.hpp" "${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtCore/QItemSelectionModel/nitemselectionmodel.hpp" diff --git a/package.json b/package.json index 597a84c23..fa52b1e9e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@nodegui/nodegui", - "version": "0.53.0", + "version": "0.54.0", "description": "A cross-platform library to build native desktop apps.", "main": "dist/index.js", "typings": "dist/index.d.ts", diff --git a/src/cpp/include/nodegui/QtWidgets/QAbstractItemDelegate/qabstractitemdelegate_macro.h b/src/cpp/include/nodegui/QtWidgets/QAbstractItemDelegate/qabstractitemdelegate_macro.h new file mode 100644 index 000000000..2af2efd25 --- /dev/null +++ b/src/cpp/include/nodegui/QtWidgets/QAbstractItemDelegate/qabstractitemdelegate_macro.h @@ -0,0 +1,21 @@ +#pragma once + +#include "QtCore/QObject/qobject_macro.h" + +/* + This macro adds common QAbstractItemDelete exported methods + */ + +#ifndef QABSTRACTITEMDELEGATE_WRAPPED_METHODS_DECLARATION +#define QABSTRACTITEMDELEGATE_WRAPPED_METHODS_DECLARATION \ + QOBJECT_WRAPPED_METHODS_DECLARATION +#endif + +#ifndef QABSTRACTITEMDELEGATE_WRAPPED_METHODS_EXPORT_DEFINE +#define QABSTRACTITEMDELEGATE_WRAPPED_METHODS_EXPORT_DEFINE(WrapName) \ + QOBJECT_WRAPPED_METHODS_EXPORT_DEFINE(WrapName) +#endif // QABSTRACTITEMDELEGATE_WRAPPED_METHODS_EXPORT_DEFINE + +#ifndef QABSTRACTITEMDELEGATE_SIGNALS +#define QABSTRACTITEMDELEGATE_SIGNALS QOBJECT_SIGNALS +#endif // QABSTRACTITEMDELEGATE_SIGNALS diff --git a/src/cpp/include/nodegui/QtWidgets/QAbstractItemDelegate/qabstractitemdelegate_wrap.h b/src/cpp/include/nodegui/QtWidgets/QAbstractItemDelegate/qabstractitemdelegate_wrap.h new file mode 100644 index 000000000..683b98804 --- /dev/null +++ b/src/cpp/include/nodegui/QtWidgets/QAbstractItemDelegate/qabstractitemdelegate_wrap.h @@ -0,0 +1,25 @@ +#pragma once + +#include + +#include +#include + +#include "Extras/Utils/nutils.h" +#include "QtWidgets/QAbstractItemDelegate/qabstractitemdelegate_macro.h" + +class DLL_EXPORT QAbstractItemDelegateWrap + : public Napi::ObjectWrap { + QABSTRACTITEMDELEGATE_WRAPPED_METHODS_DECLARATION + private: + QPointer instance; + + public: + static Napi::Object init(Napi::Env env, Napi::Object exports); + QAbstractItemDelegateWrap(const Napi::CallbackInfo& info); + ~QAbstractItemDelegateWrap(); + QAbstractItemDelegate* getInternalInstance(); + // class constructor + static Napi::FunctionReference constructor; + // wrapped methods +}; diff --git a/src/cpp/include/nodegui/QtWidgets/QAbstractItemView/qabstractitemview_macro.h b/src/cpp/include/nodegui/QtWidgets/QAbstractItemView/qabstractitemview_macro.h index 9e6781716..ea1cf49c8 100644 --- a/src/cpp/include/nodegui/QtWidgets/QAbstractItemView/qabstractitemview_macro.h +++ b/src/cpp/include/nodegui/QtWidgets/QAbstractItemView/qabstractitemview_macro.h @@ -3,6 +3,7 @@ #include "QtCore/QAbstractItemModel/qabstractitemmodel_wrap.h" #include "QtCore/QItemSelectionModel/qitemselectionmodel_wrap.h" #include "QtCore/QModelIndex/qmodelindex_wrap.h" +#include "QtWidgets/QAbstractItemDelegate/qabstractitemdelegate_wrap.h" #include "QtWidgets/QAbstractScrollArea/qabstractscrollarea_macro.h" #include "QtWidgets/QWidget/qwidget_wrap.h" @@ -169,6 +170,47 @@ QString search = QString::fromUtf8(searchNapiText.c_str()); \ this->instance->keyboardSearch(search); \ return env.Null(); \ + } \ + Napi::Value setItemDelegate(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + if (info[0].IsNull()) { \ + this->instance->setItemDelegate(nullptr); \ + } else { \ + QAbstractItemDelegateWrap* delegateWrap = \ + Napi::ObjectWrap::Unwrap( \ + info[0].As()); \ + QAbstractItemDelegate* delegate = delegateWrap->getInternalInstance(); \ + this->instance->setItemDelegate(delegate); \ + } \ + return env.Null(); \ + } \ + Napi::Value setItemDelegateForColumn(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + int column = info[0].As().Int32Value(); \ + if (info[1].IsNull()) { \ + this->instance->setItemDelegateForColumn(column, nullptr); \ + } else { \ + QAbstractItemDelegateWrap* delegateWrap = \ + Napi::ObjectWrap::Unwrap( \ + info[1].As()); \ + QAbstractItemDelegate* delegate = delegateWrap->getInternalInstance(); \ + this->instance->setItemDelegateForColumn(column, delegate); \ + } \ + return env.Null(); \ + } \ + Napi::Value setItemDelegateForRow(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + int row = info[0].As().Int32Value(); \ + if (info[1].IsNull()) { \ + this->instance->setItemDelegateForRow(row, nullptr); \ + } else { \ + QAbstractItemDelegateWrap* delegateWrap = \ + Napi::ObjectWrap::Unwrap( \ + info[1].As()); \ + QAbstractItemDelegate* delegate = delegateWrap->getInternalInstance(); \ + this->instance->setItemDelegateForRow(row, delegate); \ + } \ + return env.Null(); \ } #define QABSTRACTITEMVIEW_WRAPPED_METHODS_DECLARATION \ @@ -198,37 +240,42 @@ #endif // QABSTRACTITEMVIEW_WRAPPED_METHODS_DECLARATION #ifndef QABSTRACTITEMVIEW_WRAPPED_METHODS_EXPORT_DEFINE -#define QABSTRACTITEMVIEW_WRAPPED_METHODS_EXPORT_DEFINE(WidgetWrapName) \ - QABSTRACTSCROLLAREA_WRAPPED_METHODS_EXPORT_DEFINE(WidgetWrapName) \ - InstanceMethod("setCurrentIndex", &WidgetWrapName::setCurrentIndex), \ - InstanceMethod("currentIndex", &WidgetWrapName::currentIndex), \ - InstanceMethod("setIndexWidget", &WidgetWrapName::setIndexWidget), \ - InstanceMethod("indexWidget", &WidgetWrapName::indexWidget), \ - InstanceMethod("resetHorizontalScrollMode", \ - &WidgetWrapName::resetHorizontalScrollMode), \ - InstanceMethod("resetVerticalScrollMode", \ - &WidgetWrapName::resetVerticalScrollMode), \ - InstanceMethod("rootIndex", &WidgetWrapName::rootIndex), \ - InstanceMethod("scrollToBottom", &WidgetWrapName::scrollToBottom), \ - InstanceMethod("scrollToTop", &WidgetWrapName::scrollToTop), \ - InstanceMethod("setModel", &WidgetWrapName::setModel), \ - InstanceMethod("closePersistentEditor", \ - &WidgetWrapName::closePersistentEditor), \ - InstanceMethod("clearSelection", &WidgetWrapName::clearSelection), \ - InstanceMethod("edit", &WidgetWrapName::edit), \ - InstanceMethod("reset", &WidgetWrapName::reset), \ - InstanceMethod("selectAll", &WidgetWrapName::selectAll), \ - InstanceMethod("setRootIndex", &WidgetWrapName::setRootIndex), \ - InstanceMethod("update_QModelIndex", \ - &WidgetWrapName::update_QModelIndex), \ - InstanceMethod("indexAt", &WidgetWrapName::indexAt), \ - InstanceMethod("selectionModel", &WidgetWrapName::selectionModel), \ - InstanceMethod("scrollTo", &WidgetWrapName::scrollTo), \ - InstanceMethod("isPersistentEditorOpen", \ - &WidgetWrapName::isPersistentEditorOpen), \ - InstanceMethod("openPersistentEditor", \ - &WidgetWrapName::openPersistentEditor), \ - InstanceMethod("keyboardSearch", &WidgetWrapName::keyboardSearch), +#define QABSTRACTITEMVIEW_WRAPPED_METHODS_EXPORT_DEFINE(WidgetWrapName) \ + QABSTRACTSCROLLAREA_WRAPPED_METHODS_EXPORT_DEFINE(WidgetWrapName) \ + InstanceMethod("setCurrentIndex", &WidgetWrapName::setCurrentIndex), \ + InstanceMethod("currentIndex", &WidgetWrapName::currentIndex), \ + InstanceMethod("setIndexWidget", &WidgetWrapName::setIndexWidget), \ + InstanceMethod("indexWidget", &WidgetWrapName::indexWidget), \ + InstanceMethod("resetHorizontalScrollMode", \ + &WidgetWrapName::resetHorizontalScrollMode), \ + InstanceMethod("resetVerticalScrollMode", \ + &WidgetWrapName::resetVerticalScrollMode), \ + InstanceMethod("rootIndex", &WidgetWrapName::rootIndex), \ + InstanceMethod("scrollToBottom", &WidgetWrapName::scrollToBottom), \ + InstanceMethod("scrollToTop", &WidgetWrapName::scrollToTop), \ + InstanceMethod("setModel", &WidgetWrapName::setModel), \ + InstanceMethod("closePersistentEditor", \ + &WidgetWrapName::closePersistentEditor), \ + InstanceMethod("clearSelection", &WidgetWrapName::clearSelection), \ + InstanceMethod("edit", &WidgetWrapName::edit), \ + InstanceMethod("reset", &WidgetWrapName::reset), \ + InstanceMethod("selectAll", &WidgetWrapName::selectAll), \ + InstanceMethod("setRootIndex", &WidgetWrapName::setRootIndex), \ + InstanceMethod("update_QModelIndex", \ + &WidgetWrapName::update_QModelIndex), \ + InstanceMethod("indexAt", &WidgetWrapName::indexAt), \ + InstanceMethod("selectionModel", &WidgetWrapName::selectionModel), \ + InstanceMethod("scrollTo", &WidgetWrapName::scrollTo), \ + InstanceMethod("isPersistentEditorOpen", \ + &WidgetWrapName::isPersistentEditorOpen), \ + InstanceMethod("openPersistentEditor", \ + &WidgetWrapName::openPersistentEditor), \ + InstanceMethod("keyboardSearch", &WidgetWrapName::keyboardSearch), \ + InstanceMethod("setItemDelegate", &WidgetWrapName::setItemDelegate), \ + InstanceMethod("setItemDelegateForColumn", \ + &WidgetWrapName::setItemDelegateForColumn), \ + InstanceMethod("setItemDelegateForRow", \ + &WidgetWrapName::setItemDelegateForRow), #endif // QABSTRACTITEMVIEW_WRAPPED_METHODS_EXPORT_DEFINE diff --git a/src/cpp/include/nodegui/core/Events/eventwidget_macro.h b/src/cpp/include/nodegui/core/Events/eventwidget_macro.h index b1e8f654b..718f09e72 100644 --- a/src/cpp/include/nodegui/core/Events/eventwidget_macro.h +++ b/src/cpp/include/nodegui/core/Events/eventwidget_macro.h @@ -99,13 +99,13 @@ struct InitHelper { #endif // EVENTWIDGET_WRAPPED_METHODS_EXPORT_DEFINE #ifndef EVENTWIDGET_IMPLEMENTATIONS -#define EVENTWIDGET_IMPLEMENTATIONS(BaseWidgetName) \ - bool event(QEvent* event) override { \ - if (EventWidget::event(event)) { \ - return true; \ - } \ - bool baseWidgetResult = BaseWidgetName::event(event); \ - return EventWidget::eventAfterDefault(event, baseWidgetResult); \ +#define EVENTWIDGET_IMPLEMENTATIONS(BaseWidgetName) \ + bool event(QEvent* event) override { \ + if (EventWidget::event(event)) { \ + return true; \ + } \ + bool baseWidgetResult = BaseWidgetName::event(event); \ + return EventWidget::eventAfterDefault(event, baseWidgetResult); \ } #endif // EVENTWIDGET_IMPLEMENTATIONS \ No newline at end of file diff --git a/src/cpp/lib/QtCore/QObject/qobject_wrap.cpp b/src/cpp/lib/QtCore/QObject/qobject_wrap.cpp index c138abedb..8ad43de99 100644 --- a/src/cpp/lib/QtCore/QObject/qobject_wrap.cpp +++ b/src/cpp/lib/QtCore/QObject/qobject_wrap.cpp @@ -29,9 +29,9 @@ QObjectWrap::QObjectWrap(const Napi::CallbackInfo& info) this->instance = info[0].As>().Data(); } else { Napi::Object parentObject = info[0].As(); - QObjectWrap* parentWidgetWrap = + QObjectWrap* parentObjectWrap = Napi::ObjectWrap::Unwrap(parentObject); - this->instance = new NObject(parentWidgetWrap->getInternalInstance()); + this->instance = new NObject(parentObjectWrap->getInternalInstance()); } } else if (argCount == 0) { this->instance = new NObject(); diff --git a/src/cpp/lib/QtWidgets/QAbstractItemDelegate/qabstractitemdelegate_wrap.cpp b/src/cpp/lib/QtWidgets/QAbstractItemDelegate/qabstractitemdelegate_wrap.cpp new file mode 100644 index 000000000..1bab2de46 --- /dev/null +++ b/src/cpp/lib/QtWidgets/QAbstractItemDelegate/qabstractitemdelegate_wrap.cpp @@ -0,0 +1,43 @@ +#include "QtWidgets/QAbstractItemDelegate/qabstractitemdelegate_wrap.h" + +#include "Extras/Utils/nutils.h" + +Napi::FunctionReference QAbstractItemDelegateWrap::constructor; + +Napi::Object QAbstractItemDelegateWrap::init(Napi::Env env, + Napi::Object exports) { + Napi::HandleScope scope(env); + char CLASSNAME[] = "QAbstractItemDelegate"; + Napi::Function func = + DefineClass(env, CLASSNAME, + {QABSTRACTITEMDELEGATE_WRAPPED_METHODS_EXPORT_DEFINE( + QAbstractItemDelegateWrap)}); + constructor = Napi::Persistent(func); + exports.Set(CLASSNAME, func); + QOBJECT_REGISTER_WRAPPER(QAbstractItemDelegate, QAbstractItemDelegateWrap); + return exports; +} + +QAbstractItemDelegate* QAbstractItemDelegateWrap::getInternalInstance() { + return this->instance; +} + +QAbstractItemDelegateWrap::~QAbstractItemDelegateWrap() { + extrautils::safeDelete(this->instance); +} + +QAbstractItemDelegateWrap::QAbstractItemDelegateWrap( + const Napi::CallbackInfo& info) + : Napi::ObjectWrap(info) { + Napi::Env env = info.Env(); + size_t argCount = info.Length(); + if (argCount == 1 && info[0].IsExternal()) { + // --- Wrap a given C++ instance + this->instance = info[0].As>().Data(); + } else { + Napi::TypeError::New(env, + "NodeGui: QAbstractItemDelegateWrap: Wrong number of " + "arguments to constructor") + .ThrowAsJavaScriptException(); + } +} diff --git a/src/cpp/main.cpp b/src/cpp/main.cpp index 39609727a..bd4627072 100644 --- a/src/cpp/main.cpp +++ b/src/cpp/main.cpp @@ -51,6 +51,7 @@ #include "QtGui/QScreen/qscreen_wrap.h" #include "QtGui/QStyle/qstyle_wrap.h" #include "QtGui/QWindow/qwindow_wrap.h" +#include "QtWidgets/QAbstractItemDelegate/qabstractitemdelegate_wrap.h" #include "QtWidgets/QAction/qaction_wrap.h" #include "QtWidgets/QBoxLayout/qboxlayout_wrap.h" #include "QtWidgets/QButtonGroup/qbuttongroup_wrap.h" @@ -244,6 +245,7 @@ Napi::Object Main(Napi::Env env, Napi::Object exports) { QWindowWrap::init(env, exports); QResizeEventWrap::init(env, exports); QTimerEventWrap::init(env, exports); + QAbstractItemDelegateWrap::init(env, exports); // Test CacheTestQObjectWrap::init(env, exports); diff --git a/src/index.ts b/src/index.ts index 15ba532f1..18b318a2a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -144,6 +144,7 @@ export { } from './lib/QtWidgets/QTextEdit'; export { QStyleFactory } from './lib/QtWidgets/QStyleFactory'; export { QSizePolicyPolicy, QSizePolicyPolicyFlag } from './lib/QtWidgets/QSizePolicy'; +export { QAbstractItemDelegate } from './lib/QtWidgets/QAbstractItemDelegate'; // Core export { QAbstractItemModel } from './lib/QtCore/QAbstractItemModel'; diff --git a/src/lib/QtWidgets/QAbstractItemDelegate.ts b/src/lib/QtWidgets/QAbstractItemDelegate.ts new file mode 100644 index 000000000..37e859053 --- /dev/null +++ b/src/lib/QtWidgets/QAbstractItemDelegate.ts @@ -0,0 +1,3 @@ +import { QObject, QObjectSignals } from '../QtCore/QObject'; + +export abstract class QAbstractItemDelegate extends QObject {} diff --git a/src/lib/QtWidgets/QAbstractItemView.ts b/src/lib/QtWidgets/QAbstractItemView.ts index 472163564..eb7bcbfb7 100644 --- a/src/lib/QtWidgets/QAbstractItemView.ts +++ b/src/lib/QtWidgets/QAbstractItemView.ts @@ -9,6 +9,7 @@ import { QPoint } from '../QtCore/QPoint'; import { QItemSelectionModel } from '../QtCore/QItemSelectionModel'; import { NativeElement } from '../core/Component'; import { wrapperCache } from '../core/WrapperCache'; +import { QAbstractItemDelegate } from './QAbstractItemDelegate'; /** @@ -138,9 +139,42 @@ export abstract class QAbstractItemView