7#include <QtCore/qloggingcategory.h>
9#include <QtQml/qqmlinfo.h>
10#include <QtQml/qqmlengine.h>
14using namespace Qt::StringLiterals;
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
134QQmlTableModel::QQmlTableModel(QObject *parent)
135 : QQmlAbstractColumnModel(parent)
139QQmlTableModel::~QQmlTableModel()
143
144
145
146
147
148
149
150
151QVariant QQmlTableModel::rows()
const
156void QQmlTableModel::setRows(
const QVariant &rows)
158 if (rows.userType() != qMetaTypeId<QJSValue>()) {
159 qmlWarning(
this) <<
"setRows(): \"rows\" must be an array; actual type is " << rows.typeName();
163 const auto rowsAsJSValue = rows.value<QJSValue>();
164 const QVariantList rowsAsVariantList = rowsAsJSValue.toVariant().toList();
165 if (rowsAsVariantList == mRows) {
170 if (!mComponentCompleted) {
172 mRows = rowsAsVariantList;
176 setRowsPrivate(rowsAsVariantList);
179void QQmlTableModel::setRowsPrivate(
const QVariantList &rowsAsVariantList)
181 Q_ASSERT(mComponentCompleted);
184 if (mColumns.isEmpty()) {
185 qmlWarning(
this) <<
"No TableModelColumns were set; model will be empty";
189 const bool firstTimeValidRowsHaveBeenSet = mColumnMetadata.isEmpty();
190 if (!firstTimeValidRowsHaveBeenSet) {
192 for (
int rowIndex = 0; rowIndex < rowsAsVariantList.size(); ++rowIndex) {
195 const QVariant row = QVariant::fromValue(rowsAsVariantList.at(rowIndex));
196 if (!validateNewRow(
"setRows()"_L1, row, SetRowsOperation))
201 const int oldRowCount = mRowCount;
207 mRows = rowsAsVariantList;
208 mRowCount = mRows.size();
211 if (firstTimeValidRowsHaveBeenSet && !mRows.isEmpty())
212 fetchColumnMetadata();
217 if (mRowCount != oldRowCount)
218 emit rowCountChanged();
221QVariant QQmlTableModel::dataPrivate(
const QModelIndex &index,
const QString &roleName)
const
223 const ColumnMetadata columnMetadata = mColumnMetadata.at(index.column());
224 const QString propertyName = columnMetadata.roles.value(roleName).name;
225 const QVariantMap rowData = mRows.at(index.row()).toMap();
226 return rowData.value(propertyName);
229void QQmlTableModel::setDataPrivate(
const QModelIndex &index,
const QString &roleName, QVariant value)
231 int row = index.row();
232 QVariantMap modifiedRow = mRows.at(row).toMap();
233 modifiedRow[roleName] = value;
234 mRows[row] = modifiedRow;
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256void QQmlTableModel::appendRow(
const QVariant &row)
258 if (!validateNewRow(
"appendRow()"_L1, row, AppendOperation))
261 doInsert(mRowCount, row);
265
266
267
268
269
270
271void QQmlTableModel::clear()
273 QQmlEngine *engine = qmlEngine(
this);
275 setRows(QVariant::fromValue(engine->newArray()));
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299QVariant QQmlTableModel::getRow(
int rowIndex)
301 if (!validateRowIndex(
"getRow()"_L1,
"rowIndex"_L1, rowIndex, NeedsExisting))
303 return mRows.at(rowIndex);
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327void QQmlTableModel::insertRow(
int rowIndex,
const QVariant &row)
329 if (!validateNewRow(
"insertRow()"_L1, row) ||
330 !validateRowIndex(
"insertRow()"_L1,
"rowIndex"_L1, rowIndex, CanAppend))
333 doInsert(rowIndex, row);
336void QQmlTableModel::doInsert(
int rowIndex,
const QVariant &row)
338 beginInsertRows(QModelIndex(), rowIndex, rowIndex);
342 const QVariant rowAsVariant = row.userType() == QMetaType::QVariantMap ? row : row.value<QJSValue>().toVariant();
344 mRows.insert(rowIndex, rowAsVariant);
347 qCDebug(lcTableModel).nospace() <<
"inserted the following row to the model at index "
348 << rowIndex <<
":\n" << rowAsVariant.toMap();
351 if (mColumnMetadata.isEmpty())
352 fetchColumnMetadata();
355 emit rowCountChanged();
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374void QQmlTableModel::moveRow(
int fromRowIndex,
int toRowIndex,
int rows)
376 if (fromRowIndex == toRowIndex) {
377 qmlWarning(
this) <<
"moveRow(): \"fromRowIndex\" cannot be equal to \"toRowIndex\"";
382 qmlWarning(
this) <<
"moveRow(): \"rows\" is less than or equal to 0";
386 if (!validateRowIndex(
"moveRow()"_L1,
"fromRowIndex"_L1, fromRowIndex, NeedsExisting))
389 if (!validateRowIndex(
"moveRow()"_L1,
"toRowIndex"_L1, toRowIndex, NeedsExisting))
392 if (fromRowIndex + rows > mRowCount) {
393 qmlWarning(
this) <<
"moveRow(): \"fromRowIndex\" (" << fromRowIndex
394 <<
") + \"rows\" (" << rows <<
") = " << (fromRowIndex + rows)
395 <<
", which is greater than rowCount() of " << mRowCount;
399 if (toRowIndex + rows > mRowCount) {
400 qmlWarning(
this) <<
"moveRow(): \"toRowIndex\" (" << toRowIndex
401 <<
") + \"rows\" (" << rows <<
") = " << (toRowIndex + rows)
402 <<
", which is greater than rowCount() of " << mRowCount;
406 qCDebug(lcTableModel).nospace() <<
"moving " << rows
407 <<
" row(s) from index " << fromRowIndex
408 <<
" to index " << toRowIndex;
411 beginMoveRows(QModelIndex(), fromRowIndex, fromRowIndex + rows - 1, QModelIndex(),
412 toRowIndex > fromRowIndex ? toRowIndex + rows : toRowIndex);
415 if (fromRowIndex > toRowIndex) {
417 const int from = fromRowIndex;
418 const int to = toRowIndex;
420 toRowIndex = to + rows;
424 QVector<QVariant> store;
426 for (
int i = 0; i < (toRowIndex - fromRowIndex); ++i)
427 store.append(mRows.at(fromRowIndex + rows + i));
428 for (
int i = 0; i < rows; ++i)
429 store.append(mRows.at(fromRowIndex + i));
430 for (
int i = 0; i < store.size(); ++i)
431 mRows[fromRowIndex + i] = store[i];
433 qCDebug(lcTableModel).nospace() <<
"after moving, rows are:\n" << mRows;
440
441
442
443
444
445
446void QQmlTableModel::removeRow(
int rowIndex,
int rows)
448 if (!validateRowIndex(
"removeRow()"_L1,
"rowIndex"_L1, rowIndex, NeedsExisting))
452 qmlWarning(
this) <<
"removeRow(): \"rows\" is less than or equal to zero";
456 if (rowIndex + rows - 1 >= mRowCount) {
457 qmlWarning(
this) <<
"removeRow(): \"rows\" " << rows
458 <<
" exceeds available rowCount() of " << mRowCount
459 <<
" when removing from \"rowIndex\" " << rowIndex;
463 beginRemoveRows(QModelIndex(), rowIndex, rowIndex + rows - 1);
465 auto firstIterator = mRows.begin() + rowIndex;
467 auto lastIterator = firstIterator + rows;
468 mRows.erase(firstIterator, lastIterator);
472 emit rowCountChanged();
475 qCDebug(lcTableModel).nospace() <<
"removed " << rows
476 <<
" items from the model, starting at index " << rowIndex;
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501void QQmlTableModel::setRow(
int rowIndex,
const QVariant &row)
503 if (!validateNewRow(
"setRow()"_L1, row) ||
504 !validateRowIndex(
"setRow()"_L1,
"rowIndex"_L1, rowIndex, CanAppend))
507 if (rowIndex != mRowCount) {
509 mRows[rowIndex] = row;
512 const QModelIndex topLeftModelIndex(createIndex(rowIndex, 0));
513 const QModelIndex bottomRightModelIndex(createIndex(rowIndex, mColumnCount - 1));
514 emit dataChanged(topLeftModelIndex, bottomRightModelIndex);
518 doInsert(rowIndex, row);
523QVariant QQmlTableModel::firstRow()
const
525 return mRows.first();
528void QQmlTableModel::setInitialRows()
530 setRowsPrivate(mRows);
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
568QModelIndex QQmlTableModel::index(
int row,
int column,
const QModelIndex &parent)
const
570 return row >= 0 && row < rowCount() && column >= 0 && column < columnCount() && !parent.isValid()
571 ? createIndex(row, column)
575QModelIndex QQmlTableModel::parent(
const QModelIndex &index)
const
582
583
584
585
586
587
588
589int QQmlTableModel::rowCount(
const QModelIndex &parent)
const
591 if (parent.isValid())
598
599
600
601
602
603
604
605
606
607int QQmlTableModel::columnCount(
const QModelIndex &parent)
const
615
616
617
618
619
620
621
624
625
626
627
628
629
630
632bool QQmlTableModel::validateRowIndex(QLatin1StringView functionName, QLatin1StringView argumentName,
633 int rowIndex, RowOption operation)
const
636 qmlWarning(
this).noquote() << functionName <<
": \"" << argumentName <<
"\" cannot be negative";
640 if (operation == NeedsExisting) {
641 if (rowIndex >= mRowCount) {
642 qmlWarning(
this).noquote() << functionName <<
": \"" << argumentName
643 <<
"\" " << rowIndex <<
" is greater than or equal to rowCount() of " << mRowCount;
647 if (rowIndex > mRowCount) {
648 qmlWarning(
this).noquote() << functionName <<
": \"" << argumentName
649 <<
"\" " << rowIndex <<
" is greater than rowCount() of " << mRowCount;
659#include "moc_qqmltablemodel_p.cpp"
Q_STATIC_LOGGING_CATEGORY(lcAccessibilityCore, "qt.accessibility.core")