QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
QmlObjectListModel.cc
Go to the documentation of this file.
2
3#include <QtCore/QMetaMethod>
4#include <QtQml/QQmlEngine>
5
7
8QGC_LOGGING_CATEGORY(QmlObjectListModelLog, "API.QmlObjectListModel")
9
10namespace {
11
12constexpr const char* kDirtyChangedSignature = "dirtyChanged(bool)";
13constexpr const char* kChildDirtyChangedSlotSignature = "_childDirtyChanged(bool)";
14
15QMetaMethod childDirtyChangedSlot()
16{
17 const QMetaObject* metaObject = &ObjectListModelBase::staticMetaObject;
18 const int slotIndex = metaObject->indexOfSlot(kChildDirtyChangedSlotSignature);
19 if (slotIndex < 0) {
20 qCCritical(QmlObjectListModelLog)
21 << "Internal Error: slot signature mismatch — update kChildDirtyChangedSlotSignature";
22 return QMetaMethod();
23 }
24 return metaObject->method(slotIndex);
25}
26
27QMetaMethod dirtyChangedSignal(const QObject* object)
28{
29 if (!object) {
30 return QMetaMethod();
31 }
32
33 const int signalIndex = object->metaObject()->indexOfSignal(kDirtyChangedSignature);
34 return (signalIndex >= 0) ? object->metaObject()->method(signalIndex) : QMetaMethod();
35}
36
37void connectDirtyChangedIfAvailable(QObject* object, ObjectListModelBase* receiver)
38{
39 const QMetaMethod signal = dirtyChangedSignal(object);
40 const QMetaMethod slot = childDirtyChangedSlot();
41 if (signal.isValid() && slot.isValid()) {
42 QObject::connect(object, signal, receiver, slot);
43 }
44}
45
46void disconnectDirtyChangedIfAvailable(QObject* object, ObjectListModelBase* receiver)
47{
48 const QMetaMethod signal = dirtyChangedSignal(object);
49 const QMetaMethod slot = childDirtyChangedSlot();
50 if (signal.isValid() && slot.isValid()) {
51 QObject::disconnect(object, signal, receiver, slot);
52 }
53}
54
55} // namespace
56
62
64{
65 if (index < 0 || index >= _objectList.count()) {
66 qCWarning(QmlObjectListModelLog) << "InternalError: Invalid index - index:count" << index << _objectList.count() << this;
67 return nullptr;
68 }
69 return _objectList[index];
70}
71
72int QmlObjectListModel::rowCount(const QModelIndex& parent) const
73{
74 Q_UNUSED(parent);
75
76 return _objectList.count();
77}
78
79QVariant QmlObjectListModel::data(const QModelIndex &index, int role) const
80{
81 if (!index.isValid()) {
82 return QVariant();
83 }
84
85 if (index.row() < 0 || index.row() >= _objectList.count()) {
86 return QVariant();
87 }
88
89 if (role == ObjectRole) {
90 return QVariant::fromValue(_objectList[index.row()]);
91 } else if (role == TextRole) {
92 return QVariant::fromValue(_objectList[index.row()]->objectName());
93 } else {
94 return QVariant();
95 }
96}
97
98bool QmlObjectListModel::setData(const QModelIndex& index, const QVariant& value, int role)
99{
100 if (index.isValid() && role == ObjectRole) {
101 _objectList.replace(index.row(), value.value<QObject*>());
102 emit dataChanged(index, index);
103 return true;
104 }
105
106 return false;
107}
108
109bool QmlObjectListModel::insertRows(int position, int rows, const QModelIndex& parent)
110{
111 Q_UNUSED(parent);
112
113 if (position < 0 || position > _objectList.count() + 1) {
114 qCWarning(QmlObjectListModelLog) << "Invalid position - position:count" << position << _objectList.count() << this;
115 }
116
117 if (_resetModelNestingCount == 0) {
118 beginInsertRows(QModelIndex(), position, position + rows - 1);
119 endInsertRows();
120 }
121
123
124 return true;
125}
126
127bool QmlObjectListModel::removeRows(int position, int rows, const QModelIndex& parent)
128{
129 Q_UNUSED(parent);
130
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;
135 }
136
137 if (_resetModelNestingCount == 0) {
138 beginRemoveRows(QModelIndex(), position, position + rows - 1);
139 }
140 for (int row=0; row<rows; row++) {
141 _objectList.removeAt(position);
142 }
143 if (_resetModelNestingCount == 0) {
144 endRemoveRows();
145 }
146
148
149 return true;
150}
151
152void QmlObjectListModel::move(int from, int to)
153{
154 if(0 <= from && from < count() && 0 <= to && to < count() && from != to) {
155 // Workaround to allow move item to the bottom. Done according to
156 // beginMoveRows() documentation and implementation specificity:
157 // https://doc.qt.io/qt-5/qabstractitemmodel.html#beginMoveRows
158 // (see 3rd picture explanation there)
159 if(from == to - 1) {
160 to = from++;
161 }
162 beginMoveRows(QModelIndex(), from, from, QModelIndex(), to);
163 _objectList.move(from, to);
164 endMoveRows();
165 }
166}
167
169{
170 if (index < 0 || index >= _objectList.count()) {
171 return nullptr;
172 }
173 return _objectList[index];
174}
175
176const QObject* QmlObjectListModel::operator[](int index) const
177{
178 if (index < 0 || index >= _objectList.count()) {
179 return nullptr;
180 }
181 return _objectList[index];
182}
183
185{
187 _objectList.clear();
189}
190
192{
193 QObject* removedObject = _objectList[i];
194 if (removedObject) {
195 if (!_skipDirtyFirstItem || i != 0) {
196 disconnectDirtyChangedIfAvailable(_objectList[i], this);
197 }
198 }
199 removeRows(i, 1);
200 setDirty(true);
201 return removedObject;
202}
203
204void QmlObjectListModel::insert(int i, QObject* object)
205{
206 if (i < 0 || i > _objectList.count()) {
207 qCWarning(QmlObjectListModelLog) << "Invalid index - index:count" << i << _objectList.count() << this;
208 }
209 if (object) {
210 QQmlEngine::setObjectOwnership(object, QQmlEngine::CppOwnership);
211 if (!_skipDirtyFirstItem || i != 0) {
212 connectDirtyChangedIfAvailable(object, this);
213 }
214 }
215 _objectList.insert(i, object);
216 insertRows(i, 1);
217 setDirty(true);
218}
219
220void QmlObjectListModel::insert(int i, QList<QObject*> objects)
221{
222 if (i < 0 || i > _objectList.count()) {
223 qCWarning(QmlObjectListModelLog) << "Invalid index - index:count" << i << _objectList.count() << this;
224 }
225
226 int j = i;
227 for (QObject* object: objects) {
228 QQmlEngine::setObjectOwnership(object, QQmlEngine::CppOwnership);
229
230 if (!_skipDirtyFirstItem || j != 0) {
231 connectDirtyChangedIfAvailable(object, this);
232 }
233
234 _objectList.insert(j, object);
235 j++;
236 }
237
238 insertRows(i, objects.count());
239
240 setDirty(true);
241}
242
243void QmlObjectListModel::append(QObject* object)
244{
245 insert(_objectList.count(), object);
246}
247
248void QmlObjectListModel::append(QList<QObject*> objects)
249{
250 insert(_objectList.count(), objects);
251}
252
253QObjectList QmlObjectListModel::swapObjectList(const QObjectList& newlist)
254{
255 QObjectList oldlist(_objectList);
256 for (qsizetype i = 0; i < oldlist.count(); i++) {
257 if (oldlist[i] && (!_skipDirtyFirstItem || i != 0)) {
258 disconnectDirtyChangedIfAvailable(oldlist[i], this);
259 }
260 }
261 bool anyItemDirty = false;
262 for (qsizetype i = 0; i < newlist.count(); i++) {
263 if (newlist[i] && (!_skipDirtyFirstItem || i != 0)) {
264 connectDirtyChangedIfAvailable(newlist[i], this);
265 anyItemDirty |= newlist[i]->property("dirty").toBool();
266 }
267 }
269 _objectList = newlist;
271 if (anyItemDirty) {
272 setDirty(true);
273 }
274 return oldlist;
275}
276
278{
279 return rowCount();
280}
281
283{
284 if (_dirty != dirty) {
285 _dirty = dirty;
286 if (!dirty) {
287 // Need to clear dirty from all children
288 for(QObject* object: _objectList) {
289 if (object->property("dirty").isValid()) {
290 object->setProperty("dirty", false);
291 }
292 }
293 }
294 emit dirtyChanged(_dirty);
295 }
296}
297
299{
300 for (int i=0; i<_objectList.count(); i++) {
301 _objectList[i]->deleteLater();
302 }
303 clear();
304}
#define QGC_LOGGING_CATEGORY(name, categoryStr)
void endResetModel()
Depth-counted endResetModel — only the outermost call has effect.
void beginResetModel()
Depth-counted beginResetModel — only the outermost call has effect.
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
T value(int index) const
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)