diff --git a/CMakeLists.txt b/CMakeLists.txt index 7ad3b4ff1..ed805fd6a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -68,6 +68,7 @@ add_library(${CORE_WIDGETS_ADDON} SHARED "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtCore/QAbstractItemModel/qabstractitemmodel_wrap.cpp" "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtCore/QDate/qdate_wrap.cpp" "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtCore/QDateTime/qdatetime_wrap.cpp" + "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtCore/QItemSelectionModel/qitemselectionmodel_wrap.cpp" "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtCore/QModelIndex/qmodelindex_wrap.cpp" "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtCore/QObject/qobject_wrap.cpp" "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtCore/QVariant/qvariant_wrap.cpp" @@ -147,6 +148,7 @@ add_library(${CORE_WIDGETS_ADDON} SHARED "${PROJECT_SOURCE_DIR}/src/cpp/lib/QtWidgets/QDesktopWidget/qdesktopwidget_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" "${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtCore/QObject/nobject.hpp" "${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/core/FlexLayout/flexlayout.hpp" "${PROJECT_SOURCE_DIR}/src/cpp/include/nodegui/QtGui/QMovie/nmovie.hpp" diff --git a/src/cpp/include/nodegui/QtCore/QItemSelectionModel/nitemselectionmodel.hpp b/src/cpp/include/nodegui/QtCore/QItemSelectionModel/nitemselectionmodel.hpp new file mode 100644 index 000000000..ad9d1aadc --- /dev/null +++ b/src/cpp/include/nodegui/QtCore/QItemSelectionModel/nitemselectionmodel.hpp @@ -0,0 +1,20 @@ +#pragma once +#include + +#include "Extras/Export/export.h" +#include "QtCore/QObject/qobject_macro.h" +#include "core/NodeWidget/nodewidget.h" +#include "napi.h" + +class DLL_EXPORT NItemSelectionModel : public QItemSelectionModel, + public EventWidget { + Q_OBJECT + EVENTWIDGET_IMPLEMENTATIONS(QItemSelectionModel) + public: + Napi::FunctionReference dispatchOnNode; + + virtual void connectSignalsToEventEmitter() { + // Qt Connects: Implement all signal connects here + QOBJECT_SIGNALS + } +}; diff --git a/src/cpp/include/nodegui/QtCore/QItemSelectionModel/qitemselectionmodel_wrap.h b/src/cpp/include/nodegui/QtCore/QItemSelectionModel/qitemselectionmodel_wrap.h new file mode 100644 index 000000000..3f4ac5101 --- /dev/null +++ b/src/cpp/include/nodegui/QtCore/QItemSelectionModel/qitemselectionmodel_wrap.h @@ -0,0 +1,44 @@ +#pragma once + +#include + +#include + +#include "Extras/Export/export.h" +#include "QtCore/QObject/qobject_macro.h" +#include "nitemselectionmodel.hpp" + +class DLL_EXPORT QItemSelectionModelWrap + : public Napi::ObjectWrap { + QOBJECT_WRAPPED_METHODS_DECLARATION + + private: + QPointer instance; + bool disableDeletion; + + public: + static Napi::Object init(Napi::Env env, Napi::Object exports); + QItemSelectionModelWrap(const Napi::CallbackInfo& info); + ~QItemSelectionModelWrap(); + QItemSelectionModel* getInternalInstance(); + + // class constructor + static Napi::FunctionReference constructor; + // wrapped methods + Napi::Value columnIntersectsSelection(const Napi::CallbackInfo& info); + Napi::Value currentIndex(const Napi::CallbackInfo& info); + Napi::Value hasSelection(const Napi::CallbackInfo& info); + Napi::Value isColumnSelected(const Napi::CallbackInfo& info); + Napi::Value isRowSelected(const Napi::CallbackInfo& info); + Napi::Value isSelected(const Napi::CallbackInfo& info); + Napi::Value rowIntersectsSelection(const Napi::CallbackInfo& info); + Napi::Value clear(const Napi::CallbackInfo& info); + Napi::Value clearCurrentIndex(const Napi::CallbackInfo& info); + Napi::Value clearSelection(const Napi::CallbackInfo& info); + Napi::Value reset(const Napi::CallbackInfo& info); + Napi::Value select(const Napi::CallbackInfo& info); + Napi::Value setCurrentIndex(const Napi::CallbackInfo& info); + Napi::Value selectedColumns(const Napi::CallbackInfo& info); + Napi::Value selectedIndexes(const Napi::CallbackInfo& info); + Napi::Value selectedRows(const Napi::CallbackInfo& info); +}; diff --git a/src/cpp/include/nodegui/QtWidgets/QAbstractItemView/qabstractitemview_macro.h b/src/cpp/include/nodegui/QtWidgets/QAbstractItemView/qabstractitemview_macro.h index 7cabe5c07..84903ee25 100644 --- a/src/cpp/include/nodegui/QtWidgets/QAbstractItemView/qabstractitemview_macro.h +++ b/src/cpp/include/nodegui/QtWidgets/QAbstractItemView/qabstractitemview_macro.h @@ -1,6 +1,7 @@ #pragma once #include "QtCore/QAbstractItemModel/qabstractitemmodel_wrap.h" +#include "QtCore/QItemSelectionModel/qitemselectionmodel_wrap.h" #include "QtCore/QModelIndex/qmodelindex_wrap.h" #include "QtWidgets/QAbstractScrollArea/qabstractscrollarea_macro.h" #include "QtWidgets/QWidget/qwidget_wrap.h" @@ -13,92 +14,210 @@ */ #ifndef QABSTRACTITEMVIEW_WRAPPED_METHODS_DECLARATION -#define QABSTRACTITEMVIEW_WRAPPED_METHODS_DECLARATION \ - QABSTRACTSCROLLAREA_WRAPPED_METHODS_DECLARATION \ - Napi::Value setCurrentIndex(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - Napi::Object indexObject = info[0].As(); \ - QModelIndexWrap* indexWrap = \ - Napi::ObjectWrap::Unwrap(indexObject); \ - this->instance->setCurrentIndex(*indexWrap->getInternalInstance()); \ - return env.Null(); \ - } \ - Napi::Value currentIndex(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - QModelIndex current = this->instance->currentIndex(); \ - auto instance = QModelIndexWrap::constructor.New( \ - {Napi::External::New(env, new QModelIndex(current))}); \ - return instance; \ - } \ - Napi::Value setIndexWidget(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - Napi::Object indexObject = info[0].As(); \ - Napi::Object widgetObject = info[1].As(); \ - QModelIndexWrap* indexWrap = \ - Napi::ObjectWrap::Unwrap(indexObject); \ - NodeWidgetWrap* widgetWrap = \ - Napi::ObjectWrap::Unwrap(widgetObject); \ - this->instance->setIndexWidget(*indexWrap->getInternalInstance(), \ - widgetWrap->getInternalInstance()); \ - return env.Null(); \ - } \ - Napi::Value indexWidget(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - Napi::Object indexObject = info[0].As(); \ - QModelIndexWrap* indexWrap = \ - Napi::ObjectWrap::Unwrap(indexObject); \ - QWidget* widget = \ - this->instance->indexWidget(*indexWrap->getInternalInstance()); \ - auto instance = QWidgetWrap::constructor.New( \ - {Napi::External::New(env, widget), \ - Napi::Boolean::New(env, true)}); \ - return instance; \ - } \ - Napi::Value resetHorizontalScrollMode(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - this->instance->resetHorizontalScrollMode(); \ - return env.Null(); \ - } \ - Napi::Value resetVerticalScrollMode(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - this->instance->resetVerticalScrollMode(); \ - return env.Null(); \ - } \ - Napi::Value rootIndex(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - QModelIndex root = this->instance->rootIndex(); \ - auto instance = QModelIndexWrap::constructor.New( \ - {Napi::External::New(env, new QModelIndex(root))}); \ - return instance; \ - } \ - Napi::Value scrollToBottom(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - this->instance->scrollToBottom(); \ - return env.Null(); \ - } \ - Napi::Value scrollToTop(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - this->instance->scrollToTop(); \ - return env.Null(); \ - } \ - Napi::Value setModel(const Napi::CallbackInfo& info) { \ - Napi::Env env = info.Env(); \ - Napi::HandleScope scope(env); \ - Napi::Object modelObject = info[0].As(); \ - QAbstractItemModelWrap* modelWrap = \ - Napi::ObjectWrap::Unwrap(modelObject); \ - QAbstractItemView* instance = this->instance; \ - instance->setModel(modelWrap->getInternalInstance()); \ - return env.Null(); \ + +// This silly *WITHOUT_INDEXAT nonsense is for the benefit of QHeaderView and +// its redefinition of `indexAt()` as protected(!) instead of plain public. +// *sigh* +#define QABSTRACTITEMVIEW_WRAPPED_METHODS_DECLARATION_NO_QHEADERVIEW_PROTECTED \ + QABSTRACTSCROLLAREA_WRAPPED_METHODS_DECLARATION \ + Napi::Value setCurrentIndex(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + Napi::Object indexObject = info[0].As(); \ + QModelIndexWrap* indexWrap = \ + Napi::ObjectWrap::Unwrap(indexObject); \ + this->instance->setCurrentIndex(*indexWrap->getInternalInstance()); \ + return env.Null(); \ + } \ + Napi::Value currentIndex(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + QModelIndex current = this->instance->currentIndex(); \ + auto instance = QModelIndexWrap::constructor.New( \ + {Napi::External::New(env, new QModelIndex(current))}); \ + return instance; \ + } \ + Napi::Value setIndexWidget(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + Napi::Object indexObject = info[0].As(); \ + Napi::Object widgetObject = info[1].As(); \ + QModelIndexWrap* indexWrap = \ + Napi::ObjectWrap::Unwrap(indexObject); \ + NodeWidgetWrap* widgetWrap = \ + Napi::ObjectWrap::Unwrap(widgetObject); \ + this->instance->setIndexWidget(*indexWrap->getInternalInstance(), \ + widgetWrap->getInternalInstance()); \ + return env.Null(); \ + } \ + Napi::Value indexWidget(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + Napi::Object indexObject = info[0].As(); \ + QModelIndexWrap* indexWrap = \ + Napi::ObjectWrap::Unwrap(indexObject); \ + QWidget* widget = \ + this->instance->indexWidget(*indexWrap->getInternalInstance()); \ + auto instance = QWidgetWrap::constructor.New( \ + {Napi::External::New(env, widget), \ + Napi::Boolean::New(env, true)}); \ + return instance; \ + } \ + Napi::Value resetHorizontalScrollMode(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + this->instance->resetHorizontalScrollMode(); \ + return env.Null(); \ + } \ + Napi::Value resetVerticalScrollMode(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + this->instance->resetVerticalScrollMode(); \ + return env.Null(); \ + } \ + Napi::Value rootIndex(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + QModelIndex root = this->instance->rootIndex(); \ + auto instance = QModelIndexWrap::constructor.New( \ + {Napi::External::New(env, new QModelIndex(root))}); \ + return instance; \ + } \ + Napi::Value scrollToBottom(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + this->instance->scrollToBottom(); \ + return env.Null(); \ + } \ + Napi::Value scrollToTop(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + this->instance->scrollToTop(); \ + return env.Null(); \ + } \ + Napi::Value setModel(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + Napi::Object modelObject = info[0].As(); \ + QAbstractItemModelWrap* modelWrap = \ + Napi::ObjectWrap::Unwrap(modelObject); \ + QAbstractItemView* instance = this->instance; \ + instance->setModel(modelWrap->getInternalInstance()); \ + return env.Null(); \ + } \ + Napi::Value closePersistentEditor(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + QModelIndexWrap* indexWrap = \ + Napi::ObjectWrap::Unwrap(info[0].As()); \ + QModelIndex* index = indexWrap->getInternalInstance(); \ + this->instance->QAbstractItemView::closePersistentEditor(*index); \ + return env.Null(); \ + } \ + Napi::Value clearSelection(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + this->instance->clearSelection(); \ + return env.Null(); \ + } \ + Napi::Value edit(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + QModelIndexWrap* indexWrap = \ + Napi::ObjectWrap::Unwrap(info[0].As()); \ + QModelIndex* index = indexWrap->getInternalInstance(); \ + this->instance->edit(*index); \ + return env.Null(); \ + } \ + Napi::Value reset(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + this->instance->reset(); \ + return env.Null(); \ + } \ + Napi::Value selectAll(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + this->instance->selectAll(); \ + return env.Null(); \ + } \ + Napi::Value setRootIndex(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + QModelIndexWrap* indexWrap = \ + Napi::ObjectWrap::Unwrap(info[0].As()); \ + QModelIndex* index = indexWrap->getInternalInstance(); \ + this->instance->setRootIndex(*index); \ + return env.Null(); \ + } \ + Napi::Value update_QModelIndex(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + QModelIndexWrap* indexWrap = \ + Napi::ObjectWrap::Unwrap(info[0].As()); \ + QModelIndex* index = indexWrap->getInternalInstance(); \ + this->instance->update(*index); \ + return env.Null(); \ + } \ + Napi::Value selectionModel(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + QItemSelectionModel* model = this->instance->selectionModel(); \ + auto modelExt = Napi::External::New(env, model); \ + auto instance = QItemSelectionModelWrap::constructor.New({modelExt}); \ + return instance; \ + } \ + Napi::Value isPersistentEditorOpen(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + QModelIndexWrap* indexWrap = \ + Napi::ObjectWrap::Unwrap(info[0].As()); \ + QModelIndex* index = indexWrap->getInternalInstance(); \ + bool result = this->instance->isPersistentEditorOpen(*index); \ + return Napi::Boolean::New(env, result); \ + } \ + Napi::Value openPersistentEditor(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + QModelIndexWrap* indexWrap = \ + Napi::ObjectWrap::Unwrap(info[0].As()); \ + QModelIndex* index = indexWrap->getInternalInstance(); \ + this->instance->QAbstractItemView::openPersistentEditor(*index); \ + return env.Null(); \ + } \ + Napi::Value keyboardSearch(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + std::string searchNapiText = info[0].As().Utf8Value(); \ + QString search = QString::fromUtf8(searchNapiText.c_str()); \ + this->instance->keyboardSearch(search); \ + return env.Null(); \ + } + +#define QABSTRACTITEMVIEW_WRAPPED_METHODS_DECLARATION \ + QABSTRACTITEMVIEW_WRAPPED_METHODS_DECLARATION_NO_QHEADERVIEW_PROTECTED \ + Napi::Value indexAt(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + QPointWrap* pointWrap = \ + Napi::ObjectWrap::Unwrap(info[0].As()); \ + QPoint* point = pointWrap->getInternalInstance(); \ + QModelIndex result = this->instance->indexAt(*point); \ + auto resultInstance = QModelIndexWrap::constructor.New( \ + {Napi::External::New(env, new QModelIndex(result))}); \ + return resultInstance; \ + } \ + Napi::Value scrollTo(const Napi::CallbackInfo& info) { \ + Napi::Env env = info.Env(); \ + Napi::HandleScope scope(env); \ + QModelIndexWrap* indexWrap = \ + Napi::ObjectWrap::Unwrap(info[0].As()); \ + QModelIndex* index = indexWrap->getInternalInstance(); \ + QAbstractItemView::ScrollHint hint = \ + static_cast( \ + info[1].As().Int32Value()); \ + this->instance->scrollTo(*index, hint); \ + return env.Null(); \ } #endif // QABSTRACTITEMVIEW_WRAPPED_METHODS_DECLARATION @@ -117,17 +236,88 @@ InstanceMethod("rootIndex", &WidgetWrapName::rootIndex), \ InstanceMethod("scrollToBottom", &WidgetWrapName::scrollToBottom), \ InstanceMethod("scrollToTop", &WidgetWrapName::scrollToTop), \ - InstanceMethod("setModel", &WidgetWrapName::setModel), + 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), #endif // QABSTRACTITEMVIEW_WRAPPED_METHODS_EXPORT_DEFINE #ifndef QABSTRACTITEMVIEW_SIGNALS -#define QABSTRACTITEMVIEW_SIGNALS \ - QABSTRACTSCROLLAREA_SIGNALS \ - QObject::connect(this, &QAbstractItemView::viewportEntered, [=]() { \ - Napi::Env env = this->emitOnNode.Env(); \ - Napi::HandleScope scope(env); \ - this->emitOnNode.Call({Napi::String::New(env, "viewportEntered")}); \ +#define QABSTRACTITEMVIEW_SIGNALS \ + QABSTRACTSCROLLAREA_SIGNALS \ + QObject::connect( \ + this, &QAbstractItemView::activated, [=](const QModelIndex& index) { \ + Napi::Env env = this->emitOnNode.Env(); \ + Napi::HandleScope scope(env); \ + auto indexInstance = QModelIndexWrap::constructor.New( \ + {Napi::External::New(env, new QModelIndex(index))}); \ + this->emitOnNode.Call( \ + {Napi::String::New(env, "activated"), indexInstance}); \ + }); \ + QObject::connect( \ + this, &QAbstractItemView::clicked, [=](const QModelIndex& index) { \ + Napi::Env env = this->emitOnNode.Env(); \ + Napi::HandleScope scope(env); \ + auto indexInstance = QModelIndexWrap::constructor.New( \ + {Napi::External::New(env, new QModelIndex(index))}); \ + this->emitOnNode.Call( \ + {Napi::String::New(env, "clicked"), indexInstance}); \ + }); \ + QObject::connect( \ + this, &QAbstractItemView::doubleClicked, [=](const QModelIndex& index) { \ + Napi::Env env = this->emitOnNode.Env(); \ + Napi::HandleScope scope(env); \ + auto indexInstance = QModelIndexWrap::constructor.New( \ + {Napi::External::New(env, new QModelIndex(index))}); \ + this->emitOnNode.Call( \ + {Napi::String::New(env, "doubleClicked"), indexInstance}); \ + }); \ + QObject::connect( \ + this, &QAbstractItemView::entered, [=](const QModelIndex& index) { \ + Napi::Env env = this->emitOnNode.Env(); \ + Napi::HandleScope scope(env); \ + auto indexInstance = QModelIndexWrap::constructor.New( \ + {Napi::External::New(env, new QModelIndex(index))}); \ + this->emitOnNode.Call( \ + {Napi::String::New(env, "entered"), indexInstance}); \ + }); \ + QObject::connect( \ + this, &QAbstractItemView::iconSizeChanged, [=](const QSize& size) { \ + Napi::Env env = this->emitOnNode.Env(); \ + Napi::HandleScope scope(env); \ + auto sizeInstance = QSizeWrap::constructor.New( \ + {Napi::External::New(env, new QSize(size))}); \ + this->emitOnNode.Call( \ + {Napi::String::New(env, "iconSizeChanged"), sizeInstance}); \ + }); \ + QObject::connect( \ + this, &QAbstractItemView::pressed, [=](const QModelIndex& index) { \ + Napi::Env env = this->emitOnNode.Env(); \ + Napi::HandleScope scope(env); \ + auto indexInstance = QModelIndexWrap::constructor.New( \ + {Napi::External::New(env, new QModelIndex(index))}); \ + this->emitOnNode.Call( \ + {Napi::String::New(env, "pressed"), indexInstance}); \ + }); \ + QObject::connect(this, &QAbstractItemView::viewportEntered, [=]() { \ + Napi::Env env = this->emitOnNode.Env(); \ + Napi::HandleScope scope(env); \ + this->emitOnNode.Call({Napi::String::New(env, "viewportEntered")}); \ }); #endif // QABSTRACTITEMVIEW_SIGNALS diff --git a/src/cpp/include/nodegui/QtWidgets/QHeaderView/qheaderview_wrap.h b/src/cpp/include/nodegui/QtWidgets/QHeaderView/qheaderview_wrap.h index 8874a6c4e..422d8b89a 100644 --- a/src/cpp/include/nodegui/QtWidgets/QHeaderView/qheaderview_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QHeaderView/qheaderview_wrap.h @@ -9,7 +9,33 @@ #include "QtWidgets/QHeaderView/nheaderview.hpp" class DLL_EXPORT QHeaderViewWrap : public Napi::ObjectWrap { - QABSTRACTITEMVIEW_WRAPPED_METHODS_DECLARATION + QABSTRACTITEMVIEW_WRAPPED_METHODS_DECLARATION_NO_QHEADERVIEW_PROTECTED + + Napi::Value indexAt(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + QPointWrap* pointWrap = + Napi::ObjectWrap::Unwrap(info[0].As()); + QPoint* point = pointWrap->getInternalInstance(); + QModelIndex result = this->instance->QAbstractItemView::indexAt(*point); + auto resultInstance = QModelIndexWrap::constructor.New( + {Napi::External::New(env, new QModelIndex(result))}); + return resultInstance; + } + + Napi::Value scrollTo(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + QModelIndexWrap* indexWrap = + Napi::ObjectWrap::Unwrap(info[0].As()); + QModelIndex* index = indexWrap->getInternalInstance(); + QAbstractItemView::ScrollHint hint = + static_cast( + info[1].As().Int32Value()); + this->instance->QAbstractItemView::scrollTo(*index, hint); + return env.Null(); + } + private: QPointer instance; bool disableDeletion; diff --git a/src/cpp/include/nodegui/QtWidgets/QListWidget/qlistwidget_wrap.h b/src/cpp/include/nodegui/QtWidgets/QListWidget/qlistwidget_wrap.h index a2594b992..2dd7fe50e 100644 --- a/src/cpp/include/nodegui/QtWidgets/QListWidget/qlistwidget_wrap.h +++ b/src/cpp/include/nodegui/QtWidgets/QListWidget/qlistwidget_wrap.h @@ -24,17 +24,20 @@ class DLL_EXPORT QListWidgetWrap : public Napi::ObjectWrap { // wrapped methods Napi::Value addItem(const Napi::CallbackInfo& info); Napi::Value addItems(const Napi::CallbackInfo& info); - Napi::Value closePersistentEditor(const Napi::CallbackInfo& info); + Napi::Value closePersistentEditor_QListWidgetItem( + const Napi::CallbackInfo& info); Napi::Value currentItem(const Napi::CallbackInfo& info); Napi::Value editItem(const Napi::CallbackInfo& info); Napi::Value findItems(const Napi::CallbackInfo& info); Napi::Value insertItem(const Napi::CallbackInfo& info); Napi::Value insertItems(const Napi::CallbackInfo& info); - Napi::Value isPersistentEditorOpen(const Napi::CallbackInfo& info); + Napi::Value isPersistentEditorOpen_QListWidgetItem( + const Napi::CallbackInfo& info); Napi::Value item(const Napi::CallbackInfo& info); Napi::Value itemAt(const Napi::CallbackInfo& info); Napi::Value itemWidget(const Napi::CallbackInfo& info); - Napi::Value openPersistentEditor(const Napi::CallbackInfo& info); + Napi::Value openPersistentEditor_QListWidgetItem( + const Napi::CallbackInfo& info); Napi::Value removeItemWidget(const Napi::CallbackInfo& info); Napi::Value row(const Napi::CallbackInfo& info); Napi::Value selectedItems(const Napi::CallbackInfo& info); diff --git a/src/cpp/lib/QtCore/QItemSelectionModel/qitemselectionmodel_wrap.cpp b/src/cpp/lib/QtCore/QItemSelectionModel/qitemselectionmodel_wrap.cpp new file mode 100644 index 000000000..5d0aa227b --- /dev/null +++ b/src/cpp/lib/QtCore/QItemSelectionModel/qitemselectionmodel_wrap.cpp @@ -0,0 +1,238 @@ +#include "QtCore/QItemSelectionModel/qitemselectionmodel_wrap.h" + +#include "Extras/Utils/nutils.h" +#include "QtCore/QModelIndex/qmodelindex_wrap.h" + +Napi::FunctionReference QItemSelectionModelWrap::constructor; + +Napi::Object QItemSelectionModelWrap::init(Napi::Env env, + Napi::Object exports) { + Napi::HandleScope scope(env); + char CLASSNAME[] = "QItemSelectionModel"; + Napi::Function func = DefineClass( + env, CLASSNAME, + {InstanceMethod("columnIntersectsSelection", + &QItemSelectionModelWrap::columnIntersectsSelection), + InstanceMethod("currentIndex", &QItemSelectionModelWrap::currentIndex), + InstanceMethod("hasSelection", &QItemSelectionModelWrap::hasSelection), + InstanceMethod("isColumnSelected", + &QItemSelectionModelWrap::isColumnSelected), + InstanceMethod("isRowSelected", &QItemSelectionModelWrap::isRowSelected), + InstanceMethod("isSelected", &QItemSelectionModelWrap::isSelected), + InstanceMethod("rowIntersectsSelection", + &QItemSelectionModelWrap::rowIntersectsSelection), + InstanceMethod("clear", &QItemSelectionModelWrap::clear), + InstanceMethod("clearCurrentIndex", + &QItemSelectionModelWrap::clearCurrentIndex), + InstanceMethod("clearSelection", + &QItemSelectionModelWrap::clearSelection), + InstanceMethod("reset", &QItemSelectionModelWrap::reset), + InstanceMethod("select", &QItemSelectionModelWrap::select), + InstanceMethod("setCurrentIndex", + &QItemSelectionModelWrap::setCurrentIndex), + InstanceMethod("selectedColumns", + &QItemSelectionModelWrap::selectedColumns), + InstanceMethod("selectedIndexes", + &QItemSelectionModelWrap::selectedIndexes), + InstanceMethod("selectedRows", &QItemSelectionModelWrap::selectedRows), + QOBJECT_WRAPPED_METHODS_EXPORT_DEFINE(QItemSelectionModelWrap)}); + constructor = Napi::Persistent(func); + exports.Set(CLASSNAME, func); + return exports; +} + +QItemSelectionModel* QItemSelectionModelWrap::getInternalInstance() { + return this->instance; +} +QItemSelectionModelWrap::~QItemSelectionModelWrap() { + if (!this->disableDeletion) { + extrautils::safeDelete(this->instance); + } +} + +QItemSelectionModelWrap::QItemSelectionModelWrap(const Napi::CallbackInfo& info) + : Napi::ObjectWrap(info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + + if (info.Length() == 0) { + this->instance = new NItemSelectionModel(); + this->disableDeletion = false; + } else { + this->instance = info[0].As>().Data(); + this->disableDeletion = true; + } +} +Napi::Value QItemSelectionModelWrap::columnIntersectsSelection( + const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + int column = info[0].As().Int32Value(); + QModelIndexWrap* parentWrap = + Napi::ObjectWrap::Unwrap(info[1].As()); + QModelIndex* parent = parentWrap->getInternalInstance(); + bool result = this->instance->columnIntersectsSelection(column, *parent); + return Napi::Boolean::New(env, result); +} + +Napi::Value QItemSelectionModelWrap::currentIndex( + const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + QModelIndex result = this->instance->currentIndex(); + auto resultInstance = QModelIndexWrap::constructor.New( + {Napi::External::New(env, new QModelIndex(result))}); + return resultInstance; +} + +Napi::Value QItemSelectionModelWrap::hasSelection( + const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + bool result = this->instance->hasSelection(); + return Napi::Boolean::New(env, result); +} + +Napi::Value QItemSelectionModelWrap::isColumnSelected( + const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + int column = info[0].As().Int32Value(); + QModelIndexWrap* parentWrap = + Napi::ObjectWrap::Unwrap(info[1].As()); + QModelIndex* parent = parentWrap->getInternalInstance(); + bool result = this->instance->isColumnSelected(column, *parent); + return Napi::Boolean::New(env, result); +} + +Napi::Value QItemSelectionModelWrap::isRowSelected( + const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + int row = info[0].As().Int32Value(); + QModelIndexWrap* parentWrap = + Napi::ObjectWrap::Unwrap(info[1].As()); + QModelIndex* parent = parentWrap->getInternalInstance(); + bool result = this->instance->isRowSelected(row, *parent); + return Napi::Boolean::New(env, result); +} + +Napi::Value QItemSelectionModelWrap::isSelected( + const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + QModelIndexWrap* indexWrap = + Napi::ObjectWrap::Unwrap(info[0].As()); + QModelIndex* index = indexWrap->getInternalInstance(); + bool result = this->instance->isSelected(*index); + return Napi::Boolean::New(env, result); +} + +Napi::Value QItemSelectionModelWrap::rowIntersectsSelection( + const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + int row = info[0].As().Int32Value(); + QModelIndexWrap* parentWrap = + Napi::ObjectWrap::Unwrap(info[1].As()); + QModelIndex* parent = parentWrap->getInternalInstance(); + bool result = this->instance->rowIntersectsSelection(row, *parent); + return Napi::Boolean::New(env, result); +} + +Napi::Value QItemSelectionModelWrap::clear(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + this->instance->clear(); + return env.Null(); +} + +Napi::Value QItemSelectionModelWrap::clearCurrentIndex( + const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + this->instance->clearCurrentIndex(); + return env.Null(); +} + +Napi::Value QItemSelectionModelWrap::clearSelection( + const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + this->instance->clearSelection(); + return env.Null(); +} + +Napi::Value QItemSelectionModelWrap::reset(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + this->instance->reset(); + return env.Null(); +} + +Napi::Value QItemSelectionModelWrap::select(const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + QModelIndexWrap* indexWrap = + Napi::ObjectWrap::Unwrap(info[0].As()); + QModelIndex* index = indexWrap->getInternalInstance(); + QItemSelectionModel::SelectionFlags command = + static_cast( + info[1].As().Int32Value()); + this->instance->select(*index, command); + return env.Null(); +} + +Napi::Value QItemSelectionModelWrap::setCurrentIndex( + const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + QModelIndexWrap* indexWrap = + Napi::ObjectWrap::Unwrap(info[0].As()); + QModelIndex* index = indexWrap->getInternalInstance(); + QItemSelectionModel::SelectionFlags command = + static_cast( + info[1].As().Int32Value()); + this->instance->setCurrentIndex(*index, command); + return env.Null(); +} +Napi::Value QItemSelectionModelWrap::selectedColumns( + const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + int row = info[0].As().Int32Value(); + QModelIndexList result = this->instance->selectedColumns(row); + Napi::Array resultArrayNapi = Napi::Array::New(env, result.size()); + for (int i = 0; i < result.size(); i++) { + resultArrayNapi[i] = QModelIndexWrap::constructor.New( + {Napi::External::New(env, new QModelIndex(result[i]))}); + } + return resultArrayNapi; +} + +Napi::Value QItemSelectionModelWrap::selectedIndexes( + const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + QModelIndexList result = this->instance->selectedIndexes(); + Napi::Array resultArrayNapi = Napi::Array::New(env, result.size()); + for (int i = 0; i < result.size(); i++) { + resultArrayNapi[i] = QModelIndexWrap::constructor.New( + {Napi::External::New(env, new QModelIndex(result[i]))}); + } + return resultArrayNapi; +} + +Napi::Value QItemSelectionModelWrap::selectedRows( + const Napi::CallbackInfo& info) { + Napi::Env env = info.Env(); + Napi::HandleScope scope(env); + int column = info[0].As().Int32Value(); + QModelIndexList result = this->instance->selectedRows(column); + Napi::Array resultArrayNapi = Napi::Array::New(env, result.size()); + for (int i = 0; i < result.size(); i++) { + resultArrayNapi[i] = QModelIndexWrap::constructor.New( + {Napi::External::New(env, new QModelIndex(result[i]))}); + } + return resultArrayNapi; +} diff --git a/src/cpp/lib/QtWidgets/QListWidget/qlistwidget_wrap.cpp b/src/cpp/lib/QtWidgets/QListWidget/qlistwidget_wrap.cpp index 2fed91f23..f048e67bb 100644 --- a/src/cpp/lib/QtWidgets/QListWidget/qlistwidget_wrap.cpp +++ b/src/cpp/lib/QtWidgets/QListWidget/qlistwidget_wrap.cpp @@ -14,20 +14,20 @@ Napi::Object QListWidgetWrap::init(Napi::Env env, Napi::Object exports) { env, CLASSNAME, {InstanceMethod("addItem", &QListWidgetWrap::addItem), InstanceMethod("addItems", &QListWidgetWrap::addItems), - InstanceMethod("closePersistentEditor", - &QListWidgetWrap::closePersistentEditor), + InstanceMethod("closePersistentEditor_QListWidgetItem", + &QListWidgetWrap::closePersistentEditor_QListWidgetItem), InstanceMethod("currentItem", &QListWidgetWrap::currentItem), InstanceMethod("editItem", &QListWidgetWrap::editItem), InstanceMethod("findItems", &QListWidgetWrap::findItems), InstanceMethod("insertItem", &QListWidgetWrap::insertItem), InstanceMethod("insertItems", &QListWidgetWrap::insertItems), - InstanceMethod("isPersistentEditorOpen", - &QListWidgetWrap::isPersistentEditorOpen), + InstanceMethod("isPersistentEditorOpen_QModelIndex", + &QListWidgetWrap::isPersistentEditorOpen_QListWidgetItem), InstanceMethod("item", &QListWidgetWrap::item), InstanceMethod("itemAt", &QListWidgetWrap::itemAt), InstanceMethod("itemWidget", &QListWidgetWrap::itemWidget), - InstanceMethod("openPersistentEditor", - &QListWidgetWrap::openPersistentEditor), + InstanceMethod("openPersistentEditor_QListWidgetItem", + &QListWidgetWrap::openPersistentEditor_QListWidgetItem), InstanceMethod("removeItemWidget", &QListWidgetWrap::removeItemWidget), InstanceMethod("row", &QListWidgetWrap::row), InstanceMethod("selectedItems", &QListWidgetWrap::selectedItems), @@ -95,7 +95,7 @@ Napi::Value QListWidgetWrap::addItems(const Napi::CallbackInfo& info) { return env.Null(); } -Napi::Value QListWidgetWrap::closePersistentEditor( +Napi::Value QListWidgetWrap::closePersistentEditor_QListWidgetItem( const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); Napi::HandleScope scope(env); @@ -178,7 +178,7 @@ Napi::Value QListWidgetWrap::insertItems(const Napi::CallbackInfo& info) { return env.Null(); } -Napi::Value QListWidgetWrap::isPersistentEditorOpen( +Napi::Value QListWidgetWrap::isPersistentEditorOpen_QListWidgetItem( const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); Napi::HandleScope scope(env); @@ -230,7 +230,7 @@ Napi::Value QListWidgetWrap::itemWidget(const Napi::CallbackInfo& info) { return instance; } -Napi::Value QListWidgetWrap::openPersistentEditor( +Napi::Value QListWidgetWrap::openPersistentEditor_QListWidgetItem( const Napi::CallbackInfo& info) { Napi::Env env = info.Env(); Napi::HandleScope scope(env); diff --git a/src/cpp/main.cpp b/src/cpp/main.cpp index 630456585..62f7bfc1a 100644 --- a/src/cpp/main.cpp +++ b/src/cpp/main.cpp @@ -4,6 +4,7 @@ #include "QtCore/QAbstractItemModel/qabstractitemmodel_wrap.h" #include "QtCore/QDate/qdate_wrap.h" #include "QtCore/QDateTime/qdatetime_wrap.h" +#include "QtCore/QItemSelectionModel/qitemselectionmodel_wrap.h" #include "QtCore/QMimeData/qmimedata_wrap.h" #include "QtCore/QModelIndex/qmodelindex_wrap.h" #include "QtCore/QObject/qobject_wrap.h" @@ -224,6 +225,7 @@ Napi::Object Main(Napi::Env env, Napi::Object exports) { QPaletteWrap::init(env, exports); QAbstractItemModelWrap::init(env, exports); QHeaderViewWrap::init(env, exports); + QItemSelectionModelWrap::init(env, exports); return exports; } diff --git a/src/index.ts b/src/index.ts index b77d85f98..c7fe1177b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -38,7 +38,15 @@ export { NodeLayout, QLayoutSignals, SizeConstraint } from './lib/QtWidgets/QLay export { QAbstractScrollArea } from './lib/QtWidgets/QAbstractScrollArea'; export { QAbstractSlider, QAbstractSliderSignals, SliderAction } from './lib/QtWidgets/QAbstractSlider'; export { QAbstractButton, QAbstractButtonSignals } from './lib/QtWidgets/QAbstractButton'; -export { QAbstractItemView, QAbstractItemViewSignals } from './lib/QtWidgets/QAbstractItemView'; +export { + DragDropMode, + EditTrigger, + QAbstractItemView, + QAbstractItemViewSignals, + QAbstractItemViewSelectionBehavior, + ScrollMode, + SelectionMode, +} from './lib/QtWidgets/QAbstractItemView'; export { QAbstractSpinBox, QAbstractSpinBoxSignals, @@ -133,6 +141,7 @@ export { QAbstractItemModel } from './lib/QtCore/QAbstractItemModel'; export { QAbstractTableModel } from './lib/QtCore/QAbstractTableModel'; export { QDate } from './lib/QtCore/QDate'; export { QDateTime } from './lib/QtCore/QDateTime'; +export { QItemSelectionModel, SelectionFlag } from './lib/QtCore/QItemSelectionModel'; export { QModelIndex } from './lib/QtCore/QModelIndex'; export { QMimeData } from './lib/QtCore/QMimeData'; export { QObject, QObjectSignals, NodeObject } from './lib/QtCore/QObject'; diff --git a/src/lib/QtCore/QAbstractItemModel.ts b/src/lib/QtCore/QAbstractItemModel.ts index 9c9ff30a4..7a5834b78 100644 --- a/src/lib/QtCore/QAbstractItemModel.ts +++ b/src/lib/QtCore/QAbstractItemModel.ts @@ -217,7 +217,13 @@ export class QAbstractItemModel extends NodeObject { destinationParent: QModelIndex, destinationChild: number, ): boolean { - return this.native.beginMoveColumns(sourceParent.native, sourceFirst, sourceLast, destinationParent.native, destinationChild); + return this.native.beginMoveColumns( + sourceParent.native, + sourceFirst, + sourceLast, + destinationParent.native, + destinationChild, + ); } beginMoveRows( @@ -227,7 +233,13 @@ export class QAbstractItemModel extends NodeObject { destinationParent: QModelIndex, destinationChild: number, ): boolean { - return this.native.beginMoveRows(sourceParent.native, sourceFirst, sourceLast, destinationParent.native, destinationChild); + return this.native.beginMoveRows( + sourceParent.native, + sourceFirst, + sourceLast, + destinationParent.native, + destinationChild, + ); } beginRemoveColumns(parent: QModelIndex, first: number, last: number): void { diff --git a/src/lib/QtCore/QItemSelectionModel.ts b/src/lib/QtCore/QItemSelectionModel.ts new file mode 100644 index 000000000..d468a156c --- /dev/null +++ b/src/lib/QtCore/QItemSelectionModel.ts @@ -0,0 +1,120 @@ +import addon from '../utils/addon'; +import { NativeElement } from '../core/Component'; +import { NodeObject, QObjectSignals } from '../QtCore/QObject'; +import { QModelIndex } from './QModelIndex'; +import { checkIfNativeElement } from '../utils/helpers'; + +export type QItemSelectionModelSignals = QObjectSignals; + +export class QItemSelectionModel extends NodeObject { + native: NativeElement; + constructor(arg?: NativeElement) { + let native = null; + if (arg == null) { + native = new addon.QItemSelectionModel(); + } else if (checkIfNativeElement(arg)) { + native = arg as NativeElement; + } else { + throw new Error('QItemSelectionModel cannot be initialised this way.'); + } + super(native); + this.native = native; + } + + // *** Public Functions *** + columnIntersectsSelection(column: number, parent: QModelIndex = new QModelIndex()): boolean { + return this.native.columnIntersectsSelection(column, parent.native); + } + + currentIndex(): QModelIndex { + return this.native.currentIndex(); + } + + hasSelection(): boolean { + return this.native.hasSelection(); + } + + isColumnSelected(column: number, parent: QModelIndex = new QModelIndex()): boolean { + return this.native.isColumnSelected(column, parent.native); + } + + isRowSelected(row: number, parent: QModelIndex = new QModelIndex()): boolean { + return this.native.isRowSelected(row, parent.native); + } + + isSelected(index: QModelIndex): boolean { + return this.native.isSelected(index.native); + } + + // TODO: const QAbstractItemModel * model() const + // TODO: QAbstractItemModel * model() + rowIntersectsSelection(row: number, parent: QModelIndex = new QModelIndex()): boolean { + return this.native.rowIntersectsSelection(row, parent.native); + } + + selectedColumns(row = 0): QModelIndex[] { + const methodResult = this.native.selectedColumns(row); + return methodResult.map((item: any) => new QModelIndex(item)); + } + + selectedIndexes(): QModelIndex[] { + const methodResult = this.native.selectedIndexes(); + return methodResult.map((item: any) => new QModelIndex(item)); + } + + selectedRows(column = 0): QModelIndex[] { + const methodResult = this.native.selectedRows(column); + return methodResult.map((item: any) => new QModelIndex(item)); + } + + // TODO: const QItemSelection selection() const + // TODO: void setModel(QAbstractItemModel *model) + + // *** Public Slots *** + clear(): void { + this.native.clear(); + } + + clearCurrentIndex(): void { + this.native.clearCurrentIndex(); + } + + clearSelection(): void { + this.native.clearSelection(); + } + + reset(): void { + this.native.reset(); + } + + // TODO: virtual void select(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command) + + select(index: QModelIndex, command: SelectionFlag): void { + this.native.select(index.native, command); + } + + setCurrentIndex(index: QModelIndex, command: SelectionFlag): void { + this.native.setCurrentIndex(index.native, command); + } + + // *** Signals *** + // TODO: void currentChanged(const QModelIndex ¤t, const QModelIndex &previous) + // TODO: void currentColumnChanged(const QModelIndex ¤t, const QModelIndex &previous) + // TODO: void currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous) + // TODO: void modelChanged(QAbstractItemModel *model) + // TODO: void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) +} + +export enum SelectionFlag { + NoUpdate = 0x0000, + Clear = 0x0001, + Select = 0x0002, + Deselect = 0x0004, + Toggle = 0x0008, + Current = 0x0010, + Rows = 0x0020, + Columns = 0x0040, + SelectCurrent = SelectionFlag.Select | SelectionFlag.Current, + ToggleCurrent = SelectionFlag.Toggle | SelectionFlag.Current, + ClearAndSelect = SelectionFlag.Clear | SelectionFlag.Select, +} diff --git a/src/lib/QtWidgets/QAbstractItemView.ts b/src/lib/QtWidgets/QAbstractItemView.ts index c1fd28a3e..a7f0d482e 100644 --- a/src/lib/QtWidgets/QAbstractItemView.ts +++ b/src/lib/QtWidgets/QAbstractItemView.ts @@ -5,6 +5,9 @@ import { QSize } from '../QtCore/QSize'; import { DropAction } from '../QtEnums/DropAction'; import { TextElideMode } from '../QtEnums/TextElideMode'; import { QAbstractItemModel } from '../QtCore/QAbstractItemModel'; +import { QPoint } from '../QtCore/QPoint'; +import { QItemSelectionModel } from '../QtCore/QItemSelectionModel'; +import { NativeElement } from '../core/Component'; /** @@ -18,138 +21,201 @@ It is inherited by QListWidget. (n/a QColumnView, QHeaderView, QListView, QTable */ export abstract class QAbstractItemView extends QAbstractScrollArea { - setCurrentIndex(index: QModelIndex): void { - this.native.setCurrentIndex(index.native); + // *** Public Functions *** + alternatingRowColors(): boolean { + return this.property('alternatingRowColors').toBool(); + } + autoScrollMargin(): number { + return this.property('autoScrollMargin').toInt(); + } + closePersistentEditor(index: QModelIndex): void { + this.native.closePersistentEditor(index); } currentIndex(): QModelIndex { return new QModelIndex(this.native.currentIndex()); } - setIndexWidget(index: QModelIndex, widget: QWidget): void { - this.native.setIndexWidget(index.native, widget.native); + defaultDropAction(): DropAction { + return this.property('defaultDropAction').toInt(); + } + dragDropMode(): DragDropMode { + return this.property('dragDropMode').toInt(); + } + dragDropOverwriteMode(): boolean { + return this.property('dragDropOverwriteMode').toBool(); + } + dragEnabled(): boolean { + return this.property('dragEnabled').toBool(); + } + editTriggers(): number { + return this.property('editTriggers').toInt(); + } + hasAutoScroll(): boolean { + return this.property('autoScroll').toBool(); + } + horizontalScrollMode(): ScrollMode { + return this.property('horizontalScrollMode').toInt(); + } + iconSize(): QSize { + const iconSize = this.property('iconSize'); + return QSize.fromQVariant(iconSize); + } + indexAt(point: QPoint): QModelIndex { + return this.native.indexAt(point); } indexWidget(index: QModelIndex): QWidget { return new QWidget(this.native.indexWidget(index)); } + + isPersistentEditorOpen(index: QModelIndex): boolean { + return this.native.isPersistentEditorOpen(index.native); + } + // TODO: QAbstractItemDelegate * itemDelegate() const + // TODO: QAbstractItemDelegate * itemDelegate(const QModelIndex &index) const + // TODO: QAbstractItemDelegate * itemDelegateForColumn(int column) const + // TODO: QAbstractItemDelegate * itemDelegateForRow(int row) const + keyboardSearch(search: string): void { + this.native.keyboardSearch(search); + } + // TODO: QAbstractItemModel * model() const + openPersistentEditor(index: QModelIndex): void { + this.native.openPersistentEditor(index.native); + } + resetHorizontalScrollMode(): void { + this.native.resetHorizontalScrollMode(); + } + resetVerticalScrollMode(): void { + this.native.resetVerticalScrollMode(); + } + rootIndex(): QModelIndex { + return new QModelIndex(this.native.rootIndex()); + } + scrollTo(index: QModelIndex, hint = ScrollHint.EnsureVisible): void { + this.native.scrollTo(index.native, hint); + } + selectionBehavior(): QAbstractItemViewSelectionBehavior { + return this.property('selectionBehavior').toInt(); + } + selectionMode(): SelectionMode { + return this.property('selectionMode').toInt(); + } + selectionModel(): QItemSelectionModel { + return new QItemSelectionModel(this.native.selectionModel()); + } + setAlternatingRowColors(enable: boolean): void { + this.setProperty('alternatingRowColors', enable); + } + setAutoScroll(enable: boolean): void { + this.setProperty('autoScroll', enable); + } + setAutoScrollMargin(margin: number): void { + this.setProperty('autoScrollMargin', margin); + } + setDefaultDropAction(dropAction: DropAction): void { + this.setProperty('defaultDropAction', dropAction); + } + setDragDropMode(behavior: DragDropMode): void { + this.setProperty('dragDropMode', behavior); + } + setDragDropOverwriteMode(overwrite: boolean): void { + this.setProperty('dragDropOverwriteMode', overwrite); + } + setDragEnabled(enable: boolean): void { + this.setProperty('dragEnabled', enable); + } + setDropIndicatorShown(enable: boolean): void { + this.setProperty('showDropIndicator', enable); + } + setEditTriggers(triggers: number): void { + this.setProperty('editTriggers', triggers); + } + setHorizontalScrollMode(mode: boolean): void { + this.setProperty('horizontalScrollMode', mode); + } + setIconSize(iconSize: QSize): void { + this.setProperty('iconSize', iconSize.native); + } + 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) + setModel(model: QAbstractItemModel): void { + this.native.setModel(model.native); + } + setSelectionBehavior(behavior: QAbstractItemViewSelectionBehavior): void { + this.setProperty('selectionBehavior', behavior); + } + setSelectionMode(mode: SelectionMode): void { + this.setProperty('selectionMode', mode); + } + // TODO: virtual void setSelectionModel(QItemSelectionModel *selectionModel) + setTabKeyNavigation(enable: boolean): void { + this.setProperty('tabKeyNavigation', enable); + } + setTextElideMode(mode: TextElideMode): void { + this.setProperty('textElideMode', mode); + } + setVerticalScrollMode(mode: ScrollMode): void { + this.setProperty('verticalScrollMode', mode); + } + showDropIndicator(): boolean { + return this.property('showDropIndicator').toBool(); + } + // TODO: virtual int sizeHintForColumn(int column) const + // TODO: QSize sizeHintForIndex(const QModelIndex &index) const + // TODO: virtual int sizeHintForRow(int row) const + tabKeyNavigation(): boolean { + return this.property('tabKeyNavigation').toBool(); + } + textElideMode(): TextElideMode { + return this.property('textElideMode').toInt(); + } + verticalScrollMode(): ScrollMode { + return this.property('verticalScrollMode').toInt(); + } + // TODO: virtual QRect visualRect(const QModelIndex &index) const = 0 + + // *** Public Slots *** + clearSelection(): void { + this.native.clearSelection(); + } + edit(index: QModelIndex): void { + this.native.edit(index.native); + } + reset(): void { + this.native.reset(); + } scrollToBottom(): void { this.native.scrollToBottom(); } scrollToTop(): void { this.native.scrollToTop(); } - setAlternatingRowColors(enable: boolean): void { - this.setProperty('alternatingRowColors', enable); + selectAll(): void { + this.native.selectAll(); } - alternatingRowColors(): boolean { - return this.property('alternatingRowColors').toBool(); + setCurrentIndex(index: QModelIndex): void { + this.native.setCurrentIndex(index.native); } - setAutoScroll(enable: boolean): void { - this.setProperty('autoScroll', enable); + setRootIndex(index: QModelIndex): void { + this.native.setRootIndex(index.native); } - hasAutoScroll(): boolean { - return this.property('autoScroll').toBool(); - } - setAutoScrollMargin(margin: number): void { - this.setProperty('autoScrollMargin', margin); - } - autoScrollMargin(): number { - return this.property('autoScrollMargin').toInt(); - } - setDefaultDropAction(dropAction: DropAction): void { - this.setProperty('defaultDropAction', dropAction); - } - defaultDropAction(): DropAction { - return this.property('defaultDropAction').toInt(); - } - setDragDropMode(behavior: DragDropMode): void { - this.setProperty('dragDropMode', behavior); - } - dragDropMode(): DragDropMode { - return this.property('dragDropMode').toInt(); - } - setDragDropOverwriteMode(overwrite: boolean): void { - this.setProperty('dragDropOverwriteMode', overwrite); - } - dragDropOverwriteMode(): boolean { - return this.property('dragDropOverwriteMode').toBool(); - } - setDragEnabled(enable: boolean): void { - this.setProperty('dragEnabled', enable); - } - dragEnabled(): boolean { - return this.property('dragEnabled').toBool(); - } - setEditTriggers(triggers: number): void { - this.setProperty('editTriggers', triggers); - } - editTriggers(): number { - return this.property('editTriggers').toInt(); - } - setHorizontalScrollMode(mode: boolean): void { - this.setProperty('horizontalScrollMode', mode); - } - horizontalScrollMode(): ScrollMode { - return this.property('horizontalScrollMode').toInt(); - } - resetHorizontalScrollMode(): void { - this.native.resetHorizontalScrollMode(); - } - setIconSize(iconSize: QSize): void { - this.setProperty('iconSize', iconSize.native); - } - iconSize(): QSize { - const iconSize = this.property('iconSize'); - return QSize.fromQVariant(iconSize); - } - setSelectionBehavior(behavior: SelectionBehavior): void { - this.setProperty('selectionBehavior', behavior); - } - selectionBehavior(): SelectionBehavior { - return this.property('selectionBehavior').toInt(); - } - setSelectionMode(mode: SelectionMode): void { - this.setProperty('selectionMode', mode); - } - selectionMode(): SelectionMode { - return this.property('selectionMode').toInt(); - } - setDropIndicatorShown(enable: boolean): void { - this.setProperty('showDropIndicator', enable); - } - showDropIndicator(): boolean { - return this.property('showDropIndicator').toBool(); - } - setTabKeyNavigation(enable: boolean): void { - this.setProperty('tabKeyNavigation', enable); - } - tabKeyNavigation(): boolean { - return this.property('tabKeyNavigation').toBool(); - } - setTextElideMode(mode: TextElideMode): void { - this.setProperty('textElideMode', mode); - } - textElideMode(): TextElideMode { - return this.property('textElideMode').toInt(); - } - setVerticalScrollMode(mode: ScrollMode): void { - this.setProperty('verticalScrollMode', mode); - } - verticalScrollMode(): ScrollMode { - return this.property('verticalScrollMode').toInt(); - } - resetVerticalScrollMode(): void { - this.native.resetVerticalScrollMode(); - } - setModel(model: QAbstractItemModel): void { - this.native.setModel(model.native); + update(index?: QModelIndex): void { + if (index == null) { + super.update(); + } else { + this.native.update_QModelIndex(index.native); + } } } export enum DragDropMode { - NoDragDrop, - DragOnly, - DropOnly, - DragDrop, - InternalMove, + NoDragDrop = 0, + DragOnly = 1, + DropOnly = 2, + DragDrop = 3, + InternalMove = 4, } export enum EditTrigger { @@ -162,25 +228,39 @@ export enum EditTrigger { AllEditTriggers = 31, } -export enum ScrollMode { - ScrollPerItem, - ScrollPerPixel, +export enum ScrollHint { + EnsureVisible = 0, + PositionAtTop = 1, + PositionAtBottom = 2, + PositionAtCenter = 3, } -export enum SelectionBehavior { - SelectItems, - SelectRows, - SelectColumns, +export enum ScrollMode { + ScrollPerItem = 0, + ScrollPerPixel = 1, +} + +// QTabBar also has a SelectionBehavior, so we prefix this one. +export enum QAbstractItemViewSelectionBehavior { + SelectItems = 0, + SelectRows = 1, + SelectColumns = 2, } export enum SelectionMode { - NoSelection, - SingleSelection, - MultiSelection, - ExtendedSelection, - ContiguousSelection, + NoSelection = 0, + SingleSelection = 1, + MultiSelection = 2, + ExtendedSelection = 3, + ContiguousSelection = 4, } export interface QAbstractItemViewSignals extends QAbstractScrollAreaSignals { + activated: (index: NativeElement /* QModelIndex */) => void; + clicked: (index: NativeElement /* QModelIndex */) => void; + doubleClicked: (index: NativeElement /* QModelIndex */) => void; + entered: (index: NativeElement /* QModelIndex */) => void; + iconSizeChanged: (size: NativeElement /* QSize */) => void; + pressed: (index: NativeElement /* QModelIndex */) => void; viewportEntered: () => void; } diff --git a/src/lib/QtWidgets/QListWidget.ts b/src/lib/QtWidgets/QListWidget.ts index 616f409bf..d4fb1ca36 100644 --- a/src/lib/QtWidgets/QListWidget.ts +++ b/src/lib/QtWidgets/QListWidget.ts @@ -5,9 +5,10 @@ import { QListWidgetItem } from './QListWidgetItem'; import { NodeListView, QListViewSignals } from './QListView'; import { QRect } from '../QtCore/QRect'; import { SortOrder, ScrollHint, MatchFlag } from '../QtEnums'; +import { QModelIndex } from '../QtCore/QModelIndex'; /** - + > Create and control a item-based list. * **This class is a JS wrapper around Qt's [QListWidget class](https://doc.qt.io/qt-5/qlistwidget.html)** @@ -70,8 +71,12 @@ export class QListWidget extends NodeListView { addItems(labels: string[]): void { this.native.addItems(labels); } - closePersistentEditor(item: QListWidgetItem): void { - this.native.closePersistentEditor(item.native); + closePersistentEditor(itemOrIndex: QListWidgetItem | QModelIndex): void { + if (itemOrIndex instanceof QListWidgetItem) { + this.native.closePersistentEditor_QListWidgetItem(itemOrIndex.native); + } else { + this.native.closePersistentEditor(itemOrIndex.native); + } } currentItem(): QListWidgetItem { return new QListWidgetItem(this.native.currentItem()); @@ -92,8 +97,12 @@ export class QListWidget extends NodeListView { insertItems(row: number, labels: string[]): void { this.native.insertItems(row, labels); } - isPersistentEditorOpen(item: QListWidgetItem): boolean { - return this.native.isPersistentEditorOpen(item.native); + isPersistentEditorOpen(itemOrIndex: QListWidgetItem | QModelIndex): boolean { + if (itemOrIndex instanceof QListWidgetItem) { + return this.native.isPersistentEditorOpen_QListWidgetItem(itemOrIndex.native); + } else { + return this.native.isPersistentEditorOpen(itemOrIndex.native); + } } item(row: number): QListWidgetItem { return new QListWidgetItem(this.native.item(row)); @@ -104,8 +113,12 @@ export class QListWidget extends NodeListView { itemWidget(item: QListWidgetItem): QWidget { return new QWidget(this.native.itemWidget(item.native)); } - openPersistentEditor(item: QListWidgetItem): void { - this.native.openPersistentEditor(item.native); + openPersistentEditor(itemOrIndex: QListWidgetItem | QModelIndex): void { + if (itemOrIndex instanceof QListWidgetItem) { + this.native.openPersistentEditor_QListWidgetItem(itemOrIndex.native); + } else { + this.native.openPersistentEditor(itemOrIndex.native); + } } removeItemWidget(item: QListWidgetItem): void { this.native.removeItemWidget(item.native); diff --git a/src/lib/QtWidgets/QTableWidget.ts b/src/lib/QtWidgets/QTableWidget.ts index 127350b35..ee9dcf507 100644 --- a/src/lib/QtWidgets/QTableWidget.ts +++ b/src/lib/QtWidgets/QTableWidget.ts @@ -7,7 +7,7 @@ import { QAbstractScrollArea, QAbstractScrollAreaSignals } from './QAbstractScro import { QRect } from '../QtCore/QRect'; /** - + > Creates and item-based table view. * **This class is a JS wrapper around Qt's [QTableWidget class](https://doc.qt.io/qt-5/qtablewidget.html)**