6#include <QtCore/qsize.h>
8#include <QtCore/private/qabstractitemmodel_p.h>
14class QRangeModelPrivate : QAbstractItemModelPrivate
16 Q_DECLARE_PUBLIC(QRangeModel)
19 explicit QRangeModelPrivate(std::unique_ptr<QRangeModelImplBase, QRangeModelImplBase::Deleter> impl)
20 : impl(std::move(impl))
23 std::unique_ptr<QRangeModelImplBase, QRangeModelImplBase::Deleter> impl;
24 friend class QRangeModelImplBase;
26 static QRangeModelPrivate *get(QRangeModel *model) {
return model->d_func(); }
27 static const QRangeModelPrivate *get(
const QRangeModel *model) {
return model->d_func(); }
29 mutable QHash<
int, QByteArray> m_roleNames;
30 QRangeModel::AutoConnectPolicy m_autoConnectPolicy = QRangeModel::AutoConnectPolicy::None;
31 bool m_dataChangedDispatchBlocked =
false;
33 static void emitDataChanged(
const QModelIndex &index,
int role)
35 const auto *model =
static_cast<
const QRangeModel *>(index.model());
36 if (!get(model)->m_dataChangedDispatchBlocked) {
37 const auto *emitter = QRangeModelImplBase::getImplementation(model);
38 const_cast<QRangeModelImplBase *>(emitter)->dataChanged(index, index, {role});
54 Q_ASSERT(
std::holds_alternative<Data>(storage));
76 QMetaObject::Connection connection;
79 QPersistentModelIndex index;
87 Q_ASSERT(
std::holds_alternative<Data>(storage));
88 const auto &data =
std::get<Data>(storage);
89 if (!data.index.isValid()) {
90 if (!QObject::disconnect(connection))
91 qWarning() <<
"Failed to break connection for" << Qt::ItemDataRole(data.role);
93 QRangeModelPrivate::emitDataChanged(data.index, data.role);
107 void operator()() { QRangeModelPrivate::emitDataChanged(index, role); }
114QRangeModel::QRangeModel(QRangeModelImplBase *impl, QObject *parent)
115 : QAbstractItemModel(*
new QRangeModelPrivate({impl, {}}), parent)
119QRangeModelImplBase *QRangeModelImplBase::getImplementation(QRangeModel *model)
121 return model->d_func()->impl.get();
124const QRangeModelImplBase *QRangeModelImplBase::getImplementation(
const QRangeModel *model)
126 return model->d_func()->impl.get();
129QScopedValueRollback<
bool> QRangeModelImplBase::blockDataChangedDispatch()
131 return QScopedValueRollback(m_rangeModel->d_func()->m_dataChangedDispatchBlocked,
true);
135
136
137
138
139QHash<
int, QMetaProperty> QRangeModelImplBase::roleProperties(
const QAbstractItemModel &model,
140 const QMetaObject &metaObject)
142 const auto roles = model.roleNames();
143 QHash<
int, QMetaProperty> result;
144 for (
auto &&[role, roleName] : roles.asKeyValueRange()) {
145 if (role == Qt::RangeModelDataRole)
147 result[role] = metaObject.property(metaObject.indexOfProperty(roleName));
152QHash<
int, QMetaProperty> QRangeModelImplBase::columnProperties(
const QMetaObject &metaObject)
154 QHash<
int, QMetaProperty> result;
155 const int propertyOffset = metaObject.propertyOffset();
156 for (
int p = propertyOffset; p < metaObject.propertyCount(); ++p)
157 result[p - propertyOffset] = metaObject.property(p);
161QRangeModelDetails::AutoConnectContext::~AutoConnectContext() =
default;
163template <
auto Handler>
165 QRangeModelDetails::AutoConnectContext *context,
166 const QHash<
int, QMetaProperty> &properties)
171 auto connect = [item, context](
const QModelIndex &cell,
int role,
const QMetaProperty &property) {
172 if (property.hasNotifySignal()) {
173 if (!Handler(cell, item, context, role, property))
176 qWarning() <<
"Property" << property.name() <<
"for" << Qt::ItemDataRole(role)
177 <<
"at" << cell <<
"has no notify signal";
182 if (context->mapping == QRangeModelDetails::AutoConnectContext::AutoConnectMapping::Roles) {
183 for (
auto &&[role, property] : properties.asKeyValueRange())
184 connect(index, role, property);
186 for (
auto &&[column, property] : properties.asKeyValueRange())
187 connect(index.siblingAtColumn(column), Qt::DisplayRole, property);
192bool QRangeModelImplBase::connectProperty(
const QModelIndex &index,
const QObject *item,
193 QRangeModelDetails::AutoConnectContext *context,
194 int role,
const QMetaProperty &property)
198 PropertyChangedHandler handler{index, role};
199 auto connection = property.enclosingMetaObject()->connect(item, property.notifySignal(),
200 context, std::move(handler));
202 qWarning() <<
"Failed to connect to" << item << property.name();
210 handler = std::move(connection);
215bool QRangeModelImplBase::connectProperties(
const QModelIndex &index,
const QObject *item,
216 QRangeModelDetails::AutoConnectContext *context,
217 const QHash<
int, QMetaProperty> &properties)
219 return connectPropertiesHelper<QRangeModelImplBase::connectProperty>(index, item, context, properties);
222bool QRangeModelImplBase::connectPropertyConst(
const QModelIndex &index,
const QObject *item,
223 QRangeModelDetails::AutoConnectContext *context,
224 int role,
const QMetaProperty &property)
228 ConstPropertyChangedHandler handler{index, role};
229 if (!property.enclosingMetaObject()->connect(item, property.notifySignal(),
230 context, std::move(handler))) {
231 qWarning() <<
"Failed to connect to" << item << property.name();
238bool QRangeModelImplBase::connectPropertiesConst(
const QModelIndex &index,
const QObject *item,
239 QRangeModelDetails::AutoConnectContext *context,
240 const QHash<
int, QMetaProperty> &properties)
242 return connectPropertiesHelper<QRangeModelImplBase::connectPropertyConst>(index, item, context, properties);
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
845
846
847
848
849
850QRangeModel::~QRangeModel() =
default;
853
854
855
856
857
858
859
860
861
862QModelIndex QRangeModel::index(
int row,
int column,
const QModelIndex &parent)
const
864 Q_D(
const QRangeModel);
865 return d->impl->call<QRangeModelImplBase::Index>(row, column, parent);
869
870
871
872
873
874
875
876
877
878
879
880QModelIndex QRangeModel::parent(
const QModelIndex &child)
const
882 Q_D(
const QRangeModel);
883 return d->impl->call<QRangeModelImplBase::Parent>(child);
887
888
889
890
891
892
893
894
895
896
897QModelIndex QRangeModel::sibling(
int row,
int column,
const QModelIndex &index)
const
899 Q_D(
const QRangeModel);
900 return d->impl->call<QRangeModelImplBase::Sibling>(row, column, index);
904
905
906
907
908
909
910
911
912
913
914
915
916int QRangeModel::rowCount(
const QModelIndex &parent)
const
918 Q_D(
const QRangeModel);
919 return d->impl->call<QRangeModelImplBase::RowCount>(parent);
923
924
925
926
927
928
929
930
931
932
933
934
935int QRangeModel::columnCount(
const QModelIndex &parent)
const
937 Q_D(
const QRangeModel);
938 return d->impl->call<QRangeModelImplBase::ColumnCount>(parent);
942
943
944
945
946
947
948
949
950
951
952
953Qt::ItemFlags QRangeModel::flags(
const QModelIndex &index)
const
955 Q_D(
const QRangeModel);
956 return d->impl->call<QRangeModelImplBase::Flags>(index);
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980QVariant QRangeModel::headerData(
int section, Qt::Orientation orientation,
int role)
const
982 Q_D(
const QRangeModel);
983 return d->impl->call<QRangeModelImplBase::HeaderData>(section, orientation, role);
987
988
989bool QRangeModel::setHeaderData(
int section, Qt::Orientation orientation,
const QVariant &data,
992 return QAbstractItemModel::setHeaderData(section, orientation, data, role);
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016QVariant QRangeModel::data(
const QModelIndex &index,
int role)
const
1018 Q_D(
const QRangeModel);
1019 return d->impl->call<QRangeModelImplBase::Data>(index, role);
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048bool QRangeModel::setData(
const QModelIndex &index,
const QVariant &data,
int role)
1051 return d->impl->call<QRangeModelImplBase::SetData>(index, data, role);
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072QMap<
int, QVariant> QRangeModel::itemData(
const QModelIndex &index)
const
1074 Q_D(
const QRangeModel);
1075 return d->impl->call<QRangeModelImplBase::ItemData>(index);
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104bool QRangeModel::setItemData(
const QModelIndex &index,
const QMap<
int, QVariant> &data)
1107 return d->impl->call<QRangeModelImplBase::SetItemData>(index, data);
1111
1112
1113
1114
1115
1116
1117
1118bool QRangeModel::clearItemData(
const QModelIndex &index)
1121 return d->impl->call<QRangeModelImplBase::ClearItemData>(index);
1125
1126
1127
1128
1129
1130
1131
1132
1133
1136
1137
1138
1139
1140
1141
1142
1143
1144bool QRangeModel::insertColumns(
int column,
int count,
const QModelIndex &parent)
1147 return d->impl->call<QRangeModelImplBase::InsertColumns>(column, count, parent);
1151
1152
1153
1154
1155
1156
1157
1158
1159bool QRangeModel::removeColumns(
int column,
int count,
const QModelIndex &parent)
1162 return d->impl->call<QRangeModelImplBase::RemoveColumns>(column, count, parent);
1166
1167
1168
1169
1170
1171
1172
1173
1174bool QRangeModel::moveColumns(
const QModelIndex &sourceParent,
int sourceColumn,
int count,
1175 const QModelIndex &destinationParent,
int destinationColumn)
1178 return d->impl->call<QRangeModelImplBase::MoveColumns>(
1179 sourceParent, sourceColumn, count,
1180 destinationParent, destinationColumn);
1184
1185
1186
1187
1188
1189
1190
1191
1192
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205bool QRangeModel::insertRows(
int row,
int count,
const QModelIndex &parent)
1208 return d->impl->call<QRangeModelImplBase::InsertRows>(row, count, parent);
1212
1213
1214
1215
1216
1217
1218
1219bool QRangeModel::removeRows(
int row,
int count,
const QModelIndex &parent)
1222 return d->impl->call<QRangeModelImplBase::RemoveRows>(row, count, parent);
1226
1227
1228
1229
1230
1231
1232
1233
1234bool QRangeModel::moveRows(
const QModelIndex &sourceParent,
int sourceRow,
int count,
1235 const QModelIndex &destinationParent,
int destinationRow)
1238 return d->impl->call<QRangeModelImplBase::MoveRows>(
1239 sourceParent, sourceRow, count,
1240 destinationParent, destinationRow);
1244
1245
1246bool QRangeModel::canFetchMore(
const QModelIndex &parent)
const
1248 return QAbstractItemModel::canFetchMore(parent);
1252
1253
1254void QRangeModel::fetchMore(
const QModelIndex &parent)
1256 QAbstractItemModel::fetchMore(parent);
1260
1261
1262bool QRangeModel::hasChildren(
const QModelIndex &parent)
const
1264 return QAbstractItemModel::hasChildren(parent);
1268
1269
1270QModelIndex QRangeModel::buddy(
const QModelIndex &index)
const
1272 return QAbstractItemModel::buddy(index);
1276
1277
1278bool QRangeModel::canDropMimeData(
const QMimeData *data, Qt::DropAction action,
1279 int row,
int column,
const QModelIndex &parent)
const
1281 return QAbstractItemModel::canDropMimeData(data, action, row, column, parent);
1285
1286
1287bool QRangeModel::dropMimeData(
const QMimeData *data, Qt::DropAction action,
1288 int row,
int column,
const QModelIndex &parent)
1290 return QAbstractItemModel::dropMimeData(data, action, row, column, parent);
1294
1295
1296QMimeData *QRangeModel::mimeData(
const QModelIndexList &indexes)
const
1298 return QAbstractItemModel::mimeData(indexes);
1302
1303
1304QStringList QRangeModel::mimeTypes()
const
1306 return QAbstractItemModel::mimeTypes();
1310
1311
1312QModelIndexList QRangeModel::match(
const QModelIndex &start,
int role,
const QVariant &value,
1313 int hits, Qt::MatchFlags flags)
const
1315 return QAbstractItemModel::match(start, role, value, hits, flags);
1319
1320
1321void QRangeModel::multiData(
const QModelIndex &index, QModelRoleDataSpan roleDataSpan)
const
1323 Q_D(
const QRangeModel);
1324 d->impl->call<QRangeModelImplBase::MultiData>(index, roleDataSpan);
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1345QHash<
int, QByteArray> QRangeModelImplBase::roleNamesForMetaObject(
const QAbstractItemModel &model,
1346 const QMetaObject &metaObject)
1348 const auto defaults = model.QAbstractItemModel::roleNames();
1349 QHash<
int, QByteArray> result = {{Qt::RangeModelDataRole,
"modelData"}};
1350 int offset = metaObject.propertyOffset();
1351 for (
int i = offset; i < metaObject.propertyCount(); ++i) {
1352 const auto name = metaObject.property(i).name();
1353 const int defaultRole = defaults.key(name, -1);
1354 if (defaultRole != -1) {
1356 result[defaultRole] = name;
1358 result[Qt::UserRole + i - offset] = name;
1364QHash<
int, QByteArray> QRangeModelImplBase::roleNamesForSimpleType()
1367 return QHash<
int, QByteArray>{
1368 {Qt::DisplayRole,
"display"},
1369 {Qt::EditRole,
"edit"},
1370 {Qt::RangeModelDataRole,
"modelData"},
1375
1376
1377
1378
1379
1380QHash<
int, QByteArray> QRangeModel::roleNames()
const
1382 Q_D(
const QRangeModel);
1383 if (d->m_roleNames.isEmpty())
1384 d->m_roleNames = d->impl->call<QRangeModelImplBase::RoleNames>();
1386 return d->m_roleNames;
1389void QRangeModel::setRoleNames(
const QHash<
int, QByteArray> &names)
1392 if (d->m_roleNames == names)
1395 d->impl->call<QRangeModelImplBase::InvalidateCaches>();
1396 if (d->m_autoConnectPolicy != AutoConnectPolicy::None)
1397 d->impl->call<QRangeModelImplBase::SetAutoConnectPolicy>();
1399 d->m_roleNames = names;
1401 Q_EMIT roleNamesChanged();
1404void QRangeModel::resetRoleNames()
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1462QRangeModel::AutoConnectPolicy QRangeModel::autoConnectPolicy()
const
1464 Q_D(
const QRangeModel);
1465 return d->m_autoConnectPolicy;
1468void QRangeModel::setAutoConnectPolicy(QRangeModel::AutoConnectPolicy policy)
1471 if (d->m_autoConnectPolicy == policy)
1474 d->m_autoConnectPolicy = policy;
1475 d->impl->call<QRangeModelImplBase::SetAutoConnectPolicy>();
1476 Q_EMIT autoConnectPolicyChanged(policy);
1480
1481
1482void QRangeModel::sort(
int column, Qt::SortOrder order)
1484 return QAbstractItemModel::sort(column, order);
1488
1489
1490QSize QRangeModel::span(
const QModelIndex &index)
const
1492 return QAbstractItemModel::span(index);
1496
1497
1498Qt::DropActions QRangeModel::supportedDragActions()
const
1500 return QAbstractItemModel::supportedDragActions();
1504
1505
1506Qt::DropActions QRangeModel::supportedDropActions()
const
1508 return QAbstractItemModel::supportedDropActions();
1512
1513
1514void QRangeModel::resetInternalData()
1516 QAbstractItemModel::resetInternalData();
1520
1521
1522bool QRangeModel::event(QEvent *event)
1524 return QAbstractItemModel::event(event);
1528
1529
1530bool QRangeModel::eventFilter(QObject *object, QEvent *event)
1532 return QAbstractItemModel::eventFilter(object, event);
1537#include "moc_qrangemodel.cpp"
static bool connectPropertiesHelper(const QModelIndex &index, const QObject *item, QRangeModelDetails::AutoConnectContext *context, const QHash< int, QMetaProperty > &properties)
~ConstPropertyChangedHandler()=default
ConstPropertyChangedHandler(ConstPropertyChangedHandler &&other) noexcept=default
ConstPropertyChangedHandler(const QModelIndex &index, int role)
PropertyChangedHandler & operator=(PropertyChangedHandler &&)=delete
PropertyChangedHandler(PropertyChangedHandler &&other) noexcept
PropertyChangedHandler(const PropertyChangedHandler &)=delete
PropertyChangedHandler & operator=(const PropertyChangedHandler &)=delete
~PropertyChangedHandler()=default
PropertyChangedHandler(const QPersistentModelIndex &index, int role)
PropertyChangedHandler & operator=(QMetaObject::Connection &&connection)