diff options
author | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2018-02-02 14:36:40 +0100 |
---|---|---|
committer | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2018-02-09 08:46:15 +0000 |
commit | 2f9afadd5d9b4899397dca02588a719123ebc518 (patch) | |
tree | 36e670c45d452ee5d7891259a48ed2c1cd0f09fb /src/qml/util | |
parent | 508dcab350521a7dd7a84bbcb7fea973f9483611 (diff) |
QQmlAdaptorModel: add support for row and column
QQmlAdaptorModel is a helper class used by QQmlDelegateModel for
proxying many different models, including QAbstractItemModel,
QQuickListModel etc. This patch will add support for using both
row and column, and not just an index.
Change-Id: I403f4dfc2797ea6af4248485e83a1f0fdf8a90f9
Reviewed-by: J-P Nurmi <jpnurmi@qt.io>
Reviewed-by: Frederik Gladhorn <frederik.gladhorn@qt.io>
Diffstat (limited to 'src/qml/util')
-rw-r--r-- | src/qml/util/qqmladaptormodel.cpp | 33 | ||||
-rw-r--r-- | src/qml/util/qqmladaptormodel_p.h | 9 |
2 files changed, 40 insertions, 2 deletions
diff --git a/src/qml/util/qqmladaptormodel.cpp b/src/qml/util/qqmladaptormodel.cpp index 96a18a1c3d..f97854314e 100644 --- a/src/qml/util/qqmladaptormodel.cpp +++ b/src/qml/util/qqmladaptormodel.cpp @@ -985,7 +985,38 @@ void QQmlAdaptorModel::invalidateModel(QQmlDelegateModel *vdm) bool QQmlAdaptorModel::isValid() const { - return accessors != &qt_vdm_null_accessors; + return accessors != &qt_vdm_null_accessors || rows.isValid(); +} + +int QQmlAdaptorModel::count() const +{ + return rowCount() * columnCount(); +} + +int QQmlAdaptorModel::rowCount() const +{ + if (rows.isValid()) + return rows.value; + return qMax(0, accessors->rowCount(*this)); +} + +int QQmlAdaptorModel::columnCount() const +{ + if (columns.isValid()) + return columns.value; + return qMax(isValid() ? 1 : 0, accessors->columnCount(*this)); +} + +int QQmlAdaptorModel::rowAt(int index) const +{ + int count = rowCount(); + return count <= 0 ? -1 : index % count; +} + +int QQmlAdaptorModel::columnAt(int index) const +{ + int count = rowCount(); + return count <= 0 ? -1 : index / count; } void QQmlAdaptorModel::objectDestroyed(QObject *) diff --git a/src/qml/util/qqmladaptormodel_p.h b/src/qml/util/qqmladaptormodel_p.h index 4d922c68f3..480aa50459 100644 --- a/src/qml/util/qqmladaptormodel_p.h +++ b/src/qml/util/qqmladaptormodel_p.h @@ -56,6 +56,7 @@ #include "private/qqmllistaccessor_p.h" #include <private/qqmlguard_p.h> +#include <private/qqmlnullablevalue_p.h> QT_BEGIN_NAMESPACE @@ -104,6 +105,8 @@ public: virtual void fetchMore(QQmlAdaptorModel &) const {} }; + QQmlNullableValue<int> rows; + QQmlNullableValue<int> columns; const Accessors *accessors; QPersistentModelIndex rootIndex; QQmlListAccessor list; @@ -116,11 +119,15 @@ public: void invalidateModel(QQmlDelegateModel *vdm); bool isValid() const; + int count() const; + int rowCount() const; + int columnCount() const; + int rowAt(int index) const; + int columnAt(int index) const; inline QAbstractItemModel *aim() { return static_cast<QAbstractItemModel *>(object()); } inline const QAbstractItemModel *aim() const { return static_cast<const QAbstractItemModel *>(object()); } - inline int count() const { return qMax(0, accessors->count(*this)); } inline QVariant value(int index, const QString &role) const { return accessors->value(*this, index, role); } inline QQmlDelegateModelItem *createItem(QQmlDelegateModelItemMetaType *metaType, int index) { |