Add QAbstractItemDelegate to support plugins

This commit is contained in:
Simon Edwards 2022-06-06 12:25:07 +02:00
parent 4a55d9ec45
commit 9fcc5e99d3
12 changed files with 221 additions and 44 deletions

View File

@ -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"

View File

@ -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",

View File

@ -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

View File

@ -0,0 +1,25 @@
#pragma once
#include <napi.h>
#include <QAbstractItemDelegate>
#include <QPointer>
#include "Extras/Utils/nutils.h"
#include "QtWidgets/QAbstractItemDelegate/qabstractitemdelegate_macro.h"
class DLL_EXPORT QAbstractItemDelegateWrap
: public Napi::ObjectWrap<QAbstractItemDelegateWrap> {
QABSTRACTITEMDELEGATE_WRAPPED_METHODS_DECLARATION
private:
QPointer<QAbstractItemDelegate> 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
};

View File

@ -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<QAbstractItemDelegateWrap>::Unwrap( \
info[0].As<Napi::Object>()); \
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<Napi::Number>().Int32Value(); \
if (info[1].IsNull()) { \
this->instance->setItemDelegateForColumn(column, nullptr); \
} else { \
QAbstractItemDelegateWrap* delegateWrap = \
Napi::ObjectWrap<QAbstractItemDelegateWrap>::Unwrap( \
info[1].As<Napi::Object>()); \
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<Napi::Number>().Int32Value(); \
if (info[1].IsNull()) { \
this->instance->setItemDelegateForRow(row, nullptr); \
} else { \
QAbstractItemDelegateWrap* delegateWrap = \
Napi::ObjectWrap<QAbstractItemDelegateWrap>::Unwrap( \
info[1].As<Napi::Object>()); \
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

View File

@ -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

View File

@ -29,9 +29,9 @@ QObjectWrap::QObjectWrap(const Napi::CallbackInfo& info)
this->instance = info[0].As<Napi::External<QObject>>().Data();
} else {
Napi::Object parentObject = info[0].As<Napi::Object>();
QObjectWrap* parentWidgetWrap =
QObjectWrap* parentObjectWrap =
Napi::ObjectWrap<QObjectWrap>::Unwrap(parentObject);
this->instance = new NObject(parentWidgetWrap->getInternalInstance());
this->instance = new NObject(parentObjectWrap->getInternalInstance());
}
} else if (argCount == 0) {
this->instance = new NObject();

View File

@ -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<QAbstractItemDelegateWrap>(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<Napi::External<QAbstractItemDelegate>>().Data();
} else {
Napi::TypeError::New(env,
"NodeGui: QAbstractItemDelegateWrap: Wrong number of "
"arguments to constructor")
.ThrowAsJavaScriptException();
}
}

View File

@ -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);

View File

@ -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';

View File

@ -0,0 +1,3 @@
import { QObject, QObjectSignals } from '../QtCore/QObject';
export abstract class QAbstractItemDelegate<Signals extends QObjectSignals = QObjectSignals> extends QObject<Signals> {}

View File

@ -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<Signals extends QAbstractItemViewSignals
setIndexWidget(index: QModelIndex, widget: QWidget): void {
this.native.setIndexWidget(index.native, widget.native);
}
// TODO: void setItemDelegate(QAbstractItemDelegate *delegate)
// TODO: void setItemDelegateForColumn(int column, QAbstractItemDelegate *delegate)
// TODO: void setItemDelegateForRow(int row, QAbstractItemDelegate *delegate)
setItemDelegate(delegate: QAbstractItemDelegate): void {
if (delegate != null) {
const parent = delegate.parent();
if (parent != null && parent != this) {
throw new Error('NodeGui: QAbstractItemDelegate instances can only be given to one view instance.');
}
delegate.setParent(this);
this.native.setItemDelegate(delegate.native);
} else {
this.native.setItemDelegate(null);
}
}
setItemDelegateForColumn(column: number, delegate: QAbstractItemDelegate): void {
if (delegate != null) {
const parent = delegate.parent();
if (parent != null && parent != this) {
throw new Error('NodeGui: QAbstractItemDelegate instances can only be given to one view instance.');
}
delegate.setParent(this);
this.native.setItemDelegateForColumn(column, delegate.native);
} else {
this.native.setItemDelegateForColumn(column, null);
}
}
setItemDelegateForRow(row: number, delegate: QAbstractItemDelegate): void {
if (delegate != null) {
const parent = delegate.parent();
if (parent != null && parent != this) {
throw new Error('NodeGui: QAbstractItemDelegate instances can only be given to one view instance.');
}
delegate.setParent(this);
this.native.setItemDelegateForColumn(row, delegate.native);
} else {
this.native.setItemDelegateForColumn(row, null);
}
}
setModel(model: QAbstractItemModel): void {
this.native.setModel(model.native);
}