3#include <QtCore/QMetaMethod>
4#include <QtQml/QQmlEngine>
12constexpr const char* kDirtyChangedSignature =
"dirtyChanged(bool)";
13constexpr const char* kChildDirtyChangedSlotSignature =
"_childDirtyChanged(bool)";
15QMetaMethod childDirtyChangedSlot()
17 const QMetaObject* metaObject = &ObjectListModelBase::staticMetaObject;
18 const int slotIndex = metaObject->indexOfSlot(kChildDirtyChangedSlotSignature);
20 qCCritical(QmlObjectListModelLog)
21 <<
"Internal Error: slot signature mismatch — update kChildDirtyChangedSlotSignature";
24 return metaObject->method(slotIndex);
27QMetaMethod dirtyChangedSignal(
const QObject*
object)
33 const int signalIndex =
object->metaObject()->indexOfSignal(kDirtyChangedSignature);
34 return (signalIndex >= 0) ?
object->metaObject()->method(signalIndex) : QMetaMethod();
39 const QMetaMethod signal = dirtyChangedSignal(
object);
40 const QMetaMethod slot = childDirtyChangedSlot();
41 if (signal.isValid() && slot.isValid()) {
42 QObject::connect(
object, signal, receiver, slot);
48 const QMetaMethod signal = dirtyChangedSignal(
object);
49 const QMetaMethod slot = childDirtyChangedSlot();
50 if (signal.isValid() && slot.isValid()) {
51 QObject::disconnect(
object, signal, receiver, slot);
66 qCWarning(QmlObjectListModelLog) <<
"InternalError: Invalid index - index:count" <<
index << _objectList.count() <<
this;
69 return _objectList[
index];
76 return _objectList.count();
81 if (!
index.isValid()) {
85 if (
index.row() < 0 ||
index.row() >= _objectList.count()) {
90 return QVariant::fromValue(_objectList[
index.row()]);
92 return QVariant::fromValue(_objectList[
index.row()]->objectName());
101 _objectList.replace(
index.row(),
value.value<QObject*>());
113 if (position < 0 || position > _objectList.count() + 1) {
114 qCWarning(QmlObjectListModelLog) <<
"Invalid position - position:count" << position << _objectList.count() <<
this;
118 beginInsertRows(QModelIndex(), position, position + rows - 1);
131 if (position < 0 || position >= _objectList.count()) {
132 qCWarning(QmlObjectListModelLog) <<
"Invalid position - position:count" << position << _objectList.count() <<
this;
133 }
else if (position + rows > _objectList.count()) {
134 qCWarning(QmlObjectListModelLog) <<
"Invalid rows - position:rows:count" << position << rows << _objectList.count() <<
this;
138 beginRemoveRows(QModelIndex(), position, position + rows - 1);
140 for (
int row=0; row<rows; row++) {
141 _objectList.removeAt(position);
154 if(0 <= from && from <
count() && 0 <= to && to <
count() && from != to) {
162 beginMoveRows(QModelIndex(), from, from, QModelIndex(), to);
163 _objectList.move(from, to);
173 return _objectList[
index];
181 return _objectList[
index];
193 QObject* removedObject = _objectList[i];
196 disconnectDirtyChangedIfAvailable(_objectList[i],
this);
201 return removedObject;
206 if (i < 0 || i > _objectList.count()) {
207 qCWarning(QmlObjectListModelLog) <<
"Invalid index - index:count" << i << _objectList.count() <<
this;
210 QQmlEngine::setObjectOwnership(
object, QQmlEngine::CppOwnership);
212 connectDirtyChangedIfAvailable(
object,
this);
215 _objectList.insert(i,
object);
222 if (i < 0 || i > _objectList.count()) {
223 qCWarning(QmlObjectListModelLog) <<
"Invalid index - index:count" << i << _objectList.count() <<
this;
227 for (QObject*
object: objects) {
228 QQmlEngine::setObjectOwnership(
object, QQmlEngine::CppOwnership);
231 connectDirtyChangedIfAvailable(
object,
this);
234 _objectList.insert(j,
object);
245 insert(_objectList.count(),
object);
250 insert(_objectList.count(), objects);
255 QObjectList oldlist(_objectList);
256 for (qsizetype i = 0; i < oldlist.count(); i++) {
258 disconnectDirtyChangedIfAvailable(oldlist[i],
this);
261 bool anyItemDirty =
false;
262 for (qsizetype i = 0; i < newlist.count(); i++) {
264 connectDirtyChangedIfAvailable(newlist[i],
this);
265 anyItemDirty |= newlist[i]->property(
"dirty").toBool();
269 _objectList = newlist;
288 for(QObject*
object: _objectList) {
289 if (object->property(
"dirty").isValid()) {
290 object->setProperty(
"dirty",
false);
300 for (
int i=0; i<_objectList.count(); i++) {
301 _objectList[i]->deleteLater();
#define QGC_LOGGING_CATEGORY(name, categoryStr)
void endResetModel()
Depth-counted endResetModel — only the outermost call has effect.
uint _resetModelNestingCount
void beginResetModel()
Depth-counted beginResetModel — only the outermost call has effect.
void _signalCountChangedIfNotNested()
static constexpr int ObjectRole
static constexpr int TextRole
void dirtyChanged(bool dirty)
Base class for flat QObject* list models. Inherits common dirty/reset/role handling from ObjectItemMo...
QModelIndex index(int row, int column=0, const QModelIndex &parent=QModelIndex()) const override
ObjectListModelBase(QObject *parent=nullptr)
QModelIndex parent(const QModelIndex &child) const override
void append(QObject *object)
Caller maintains responsibility for object ownership and deletion.
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
void move(int from, int to)
void setDirty(bool dirty) override final
Q_INVOKABLE QObject * get(int index)
bool removeRows(int position, int rows, const QModelIndex &index=QModelIndex()) override
QObject * removeAt(int index)
int rowCount(const QModelIndex &parent=QModelIndex()) const override
QObject * operator[](int index)
int count() const override final
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
void clear() override final
bool insertRows(int position, int rows, const QModelIndex &index=QModelIndex()) override
void clearAndDeleteContents() override final
Clears the list and calls deleteLater on each entry.
QObjectList swapObjectList(const QObjectList &newlist)
void insert(int index, QObject *object)
QmlObjectListModel(QObject *parent=nullptr)