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 const std::optional<QVariantList> validated = validateRowsArgument(rows);
162 const QVariantList rowsAsVariantList = *validated;
163 if (rowsAsVariantList == mRows) {
168 if (!mComponentCompleted) {
170 mRows = rowsAsVariantList;
174 setRowsPrivate(rowsAsVariantList);
177void QQmlTableModel::setRowsPrivate(
const QVariantList &rowsAsVariantList)
179 Q_ASSERT(mComponentCompleted);
182 if (mColumns.isEmpty()) {
183 qmlWarning(
this) <<
"No TableModelColumns were set; model will be empty";
187 const bool firstTimeValidRowsHaveBeenSet = mColumnMetadata.isEmpty();
188 if (!firstTimeValidRowsHaveBeenSet) {
190 for (
int rowIndex = 0; rowIndex < rowsAsVariantList.size(); ++rowIndex) {
193 const QVariant row = QVariant::fromValue(rowsAsVariantList.at(rowIndex));
194 if (!validateNewRow(
"setRows()"_L1, row, SetRowsOperation))
199 const int oldRowCount = mRowCount;
205 mRows = rowsAsVariantList;
206 mRowCount = mRows.size();
209 if (firstTimeValidRowsHaveBeenSet && !mRows.isEmpty())
210 fetchColumnMetadata();
215 if (mRowCount != oldRowCount)
216 emit rowCountChanged();
219QVariant QQmlTableModel::dataPrivate(
const QModelIndex &index,
const QString &roleName)
const
221 const ColumnMetadata columnMetadata = mColumnMetadata.at(index.column());
222 const QString propertyName = columnMetadata.roles.value(roleName).name;
223 const QVariantMap rowData = mRows.at(index.row()).toMap();
224 return rowData.value(propertyName);
227void QQmlTableModel::setDataPrivate(
const QModelIndex &index,
const QString &roleName, QVariant value)
229 int row = index.row();
230 QVariantMap modifiedRow = mRows.at(row).toMap();
231 modifiedRow[roleName] = value;
232 mRows[row] = modifiedRow;
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254void QQmlTableModel::appendRow(
const QVariant &row)
256 if (!validateNewRow(
"appendRow()"_L1, row, AppendOperation))
259 doInsert(mRowCount, row);
263
264
265
266
267
268
269void QQmlTableModel::clear()
271 QQmlEngine *engine = qmlEngine(
this);
273 setRows(QVariant::fromValue(engine->newArray()));
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297QVariant QQmlTableModel::getRow(
int rowIndex)
299 if (!validateRowIndex(
"getRow()"_L1,
"rowIndex"_L1, rowIndex, NeedsExisting))
301 return mRows.at(rowIndex);
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325void QQmlTableModel::insertRow(
int rowIndex,
const QVariant &row)
327 if (!validateNewRow(
"insertRow()"_L1, row) ||
328 !validateRowIndex(
"insertRow()"_L1,
"rowIndex"_L1, rowIndex, CanAppend))
331 doInsert(rowIndex, row);
334void QQmlTableModel::doInsert(
int rowIndex,
const QVariant &row)
336 beginInsertRows(QModelIndex(), rowIndex, rowIndex);
340 const QVariant rowAsVariant = row.userType() == QMetaType::QVariantMap ? row : row.value<QJSValue>().toVariant();
342 mRows.insert(rowIndex, rowAsVariant);
345 qCDebug(lcTableModel).nospace() <<
"inserted the following row to the model at index "
346 << rowIndex <<
":\n" << rowAsVariant.toMap();
349 if (mColumnMetadata.isEmpty())
350 fetchColumnMetadata();
353 emit rowCountChanged();
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372void QQmlTableModel::moveRow(
int fromRowIndex,
int toRowIndex,
int rows)
374 if (fromRowIndex == toRowIndex) {
375 qmlWarning(
this) <<
"moveRow(): \"fromRowIndex\" cannot be equal to \"toRowIndex\"";
380 qmlWarning(
this) <<
"moveRow(): \"rows\" is less than or equal to 0";
384 if (!validateRowIndex(
"moveRow()"_L1,
"fromRowIndex"_L1, fromRowIndex, NeedsExisting))
387 if (!validateRowIndex(
"moveRow()"_L1,
"toRowIndex"_L1, toRowIndex, NeedsExisting))
390 if (fromRowIndex + rows > mRowCount) {
391 qmlWarning(
this) <<
"moveRow(): \"fromRowIndex\" (" << fromRowIndex
392 <<
") + \"rows\" (" << rows <<
") = " << (fromRowIndex + rows)
393 <<
", which is greater than rowCount() of " << mRowCount;
397 if (toRowIndex + rows > mRowCount) {
398 qmlWarning(
this) <<
"moveRow(): \"toRowIndex\" (" << toRowIndex
399 <<
") + \"rows\" (" << rows <<
") = " << (toRowIndex + rows)
400 <<
", which is greater than rowCount() of " << mRowCount;
404 qCDebug(lcTableModel).nospace() <<
"moving " << rows
405 <<
" row(s) from index " << fromRowIndex
406 <<
" to index " << toRowIndex;
409 beginMoveRows(QModelIndex(), fromRowIndex, fromRowIndex + rows - 1, QModelIndex(),
410 toRowIndex > fromRowIndex ? toRowIndex + rows : toRowIndex);
413 if (fromRowIndex > toRowIndex) {
415 const int from = fromRowIndex;
416 const int to = toRowIndex;
418 toRowIndex = to + rows;
422 QList<QVariant> store;
424 for (
int i = 0; i < (toRowIndex - fromRowIndex); ++i)
425 store.append(mRows.at(fromRowIndex + rows + i));
426 for (
int i = 0; i < rows; ++i)
427 store.append(mRows.at(fromRowIndex + i));
428 for (
int i = 0; i < store.size(); ++i)
429 mRows[fromRowIndex + i] = store[i];
431 qCDebug(lcTableModel).nospace() <<
"after moving, rows are:\n" << mRows;
438
439
440
441
442
443
444void QQmlTableModel::removeRow(
int rowIndex,
int rows)
446 if (!validateRowIndex(
"removeRow()"_L1,
"rowIndex"_L1, rowIndex, NeedsExisting))
450 qmlWarning(
this) <<
"removeRow(): \"rows\" is less than or equal to zero";
454 if (rowIndex + rows - 1 >= mRowCount) {
455 qmlWarning(
this) <<
"removeRow(): \"rows\" " << rows
456 <<
" exceeds available rowCount() of " << mRowCount
457 <<
" when removing from \"rowIndex\" " << rowIndex;
461 beginRemoveRows(QModelIndex(), rowIndex, rowIndex + rows - 1);
463 auto firstIterator = mRows.begin() + rowIndex;
465 auto lastIterator = firstIterator + rows;
466 mRows.erase(firstIterator, lastIterator);
470 emit rowCountChanged();
473 qCDebug(lcTableModel).nospace() <<
"removed " << rows
474 <<
" items from the model, starting at index " << rowIndex;
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499void QQmlTableModel::setRow(
int rowIndex,
const QVariant &row)
501 if (!validateNewRow(
"setRow()"_L1, row) ||
502 !validateRowIndex(
"setRow()"_L1,
"rowIndex"_L1, rowIndex, CanAppend))
505 if (rowIndex != mRowCount) {
507 mRows[rowIndex] = row;
510 const QModelIndex topLeftModelIndex(createIndex(rowIndex, 0));
511 const QModelIndex bottomRightModelIndex(createIndex(rowIndex, mColumnCount - 1));
512 emit dataChanged(topLeftModelIndex, bottomRightModelIndex);
516 doInsert(rowIndex, row);
521QVariant QQmlTableModel::firstRow()
const
523 return mRows.first();
526void QQmlTableModel::setInitialRows()
528 setRowsPrivate(mRows);
532
533
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
566QModelIndex QQmlTableModel::index(
int row,
int column,
const QModelIndex &parent)
const
568 return row >= 0 && row < rowCount() && column >= 0 && column < columnCount() && !parent.isValid()
569 ? createIndex(row, column)
573QModelIndex QQmlTableModel::parent(
const QModelIndex &index)
const
580
581
582
583
584
585
586
587int QQmlTableModel::rowCount(
const QModelIndex &parent)
const
589 if (parent.isValid())
596
597
598
599
600
601
602
603
604
605int QQmlTableModel::columnCount(
const QModelIndex &parent)
const
613
614
615
616
617
618
619
622
623
624
625
626
627
628
630bool QQmlTableModel::validateRowIndex(QLatin1StringView functionName, QLatin1StringView argumentName,
631 int rowIndex, RowOption operation)
const
634 qmlWarning(
this).noquote() << functionName <<
": \"" << argumentName <<
"\" cannot be negative";
638 if (operation == NeedsExisting) {
639 if (rowIndex >= mRowCount) {
640 qmlWarning(
this).noquote() << functionName <<
": \"" << argumentName
641 <<
"\" " << rowIndex <<
" is greater than or equal to rowCount() of " << mRowCount;
645 if (rowIndex > mRowCount) {
646 qmlWarning(
this).noquote() << functionName <<
": \"" << argumentName
647 <<
"\" " << rowIndex <<
" is greater than rowCount() of " << mRowCount;
657#include "moc_qqmltablemodel_p.cpp"
Combined button and popup list for selecting options.
QT_BEGIN_NAMESPACE Q_STATIC_LOGGING_CATEGORY(lcSynthesizedIterableAccess, "qt.iterable.synthesized", QtWarningMsg)