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
804
805
806
807
808
809
810
811
812
813
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
843
844
845
846
847
848
849
850
851
852
855
856
857
858
859
860QRangeModel::~QRangeModel() =
default;
863
864
865
866
867
868
869
870
871
872QModelIndex QRangeModel::index(
int row,
int column,
const QModelIndex &parent)
const
874 Q_D(
const QRangeModel);
875 return d->impl->call<QRangeModelImplBase::Index>(row, column, parent);
879
880
881
882
883
884
885
886
887
888
889
890QModelIndex QRangeModel::parent(
const QModelIndex &child)
const
892 Q_D(
const QRangeModel);
893 return d->impl->call<QRangeModelImplBase::Parent>(child);
897
898
899
900
901
902
903
904
905
906
907QModelIndex QRangeModel::sibling(
int row,
int column,
const QModelIndex &index)
const
909 Q_D(
const QRangeModel);
910 return d->impl->call<QRangeModelImplBase::Sibling>(row, column, index);
914
915
916
917
918
919
920
921
922
923
924
925
926int QRangeModel::rowCount(
const QModelIndex &parent)
const
928 Q_D(
const QRangeModel);
929 return d->impl->call<QRangeModelImplBase::RowCount>(parent);
933
934
935
936
937
938
939
940
941
942
943
944
945int QRangeModel::columnCount(
const QModelIndex &parent)
const
947 Q_D(
const QRangeModel);
948 return d->impl->call<QRangeModelImplBase::ColumnCount>(parent);
952
953
954
955
956
957
958
959
960
961
962
963Qt::ItemFlags QRangeModel::flags(
const QModelIndex &index)
const
965 Q_D(
const QRangeModel);
966 return d->impl->call<QRangeModelImplBase::Flags>(index);
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990QVariant QRangeModel::headerData(
int section, Qt::Orientation orientation,
int role)
const
992 Q_D(
const QRangeModel);
993 return d->impl->call<QRangeModelImplBase::HeaderData>(section, orientation, role);
997
998
999bool QRangeModel::setHeaderData(
int section, Qt::Orientation orientation,
const QVariant &data,
1002 return QAbstractItemModel::setHeaderData(section, orientation, data, role);
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026QVariant QRangeModel::data(
const QModelIndex &index,
int role)
const
1028 Q_D(
const QRangeModel);
1029 return d->impl->call<QRangeModelImplBase::Data>(index, role);
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058bool QRangeModel::setData(
const QModelIndex &index,
const QVariant &data,
int role)
1061 return d->impl->call<QRangeModelImplBase::SetData>(index, data, role);
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082QMap<
int, QVariant> QRangeModel::itemData(
const QModelIndex &index)
const
1084 Q_D(
const QRangeModel);
1085 return d->impl->call<QRangeModelImplBase::ItemData>(index);
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114bool QRangeModel::setItemData(
const QModelIndex &index,
const QMap<
int, QVariant> &data)
1117 return d->impl->call<QRangeModelImplBase::SetItemData>(index, data);
1121
1122
1123
1124
1125
1126
1127
1128bool QRangeModel::clearItemData(
const QModelIndex &index)
1131 return d->impl->call<QRangeModelImplBase::ClearItemData>(index);
1135
1136
1137
1138
1139
1140
1141
1142
1143
1146
1147
1148
1149
1150
1151
1152
1153
1154bool QRangeModel::insertColumns(
int column,
int count,
const QModelIndex &parent)
1157 return d->impl->call<QRangeModelImplBase::InsertColumns>(column, count, parent);
1161
1162
1163
1164
1165
1166
1167
1168
1169bool QRangeModel::removeColumns(
int column,
int count,
const QModelIndex &parent)
1172 return d->impl->call<QRangeModelImplBase::RemoveColumns>(column, count, parent);
1176
1177
1178
1179
1180
1181
1182
1183
1184bool QRangeModel::moveColumns(
const QModelIndex &sourceParent,
int sourceColumn,
int count,
1185 const QModelIndex &destinationParent,
int destinationColumn)
1188 return d->impl->call<QRangeModelImplBase::MoveColumns>(
1189 sourceParent, sourceColumn, count,
1190 destinationParent, destinationColumn);
1194
1195
1196
1197
1198
1199
1200
1201
1202
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215bool QRangeModel::insertRows(
int row,
int count,
const QModelIndex &parent)
1218 return d->impl->call<QRangeModelImplBase::InsertRows>(row, count, parent);
1222
1223
1224
1225
1226
1227
1228
1229bool QRangeModel::removeRows(
int row,
int count,
const QModelIndex &parent)
1232 return d->impl->call<QRangeModelImplBase::RemoveRows>(row, count, parent);
1236
1237
1238
1239
1240
1241
1242
1243
1244bool QRangeModel::moveRows(
const QModelIndex &sourceParent,
int sourceRow,
int count,
1245 const QModelIndex &destinationParent,
int destinationRow)
1248 return d->impl->call<QRangeModelImplBase::MoveRows>(
1249 sourceParent, sourceRow, count,
1250 destinationParent, destinationRow);
1254
1255
1256bool QRangeModel::canFetchMore(
const QModelIndex &parent)
const
1258 return QAbstractItemModel::canFetchMore(parent);
1262
1263
1264void QRangeModel::fetchMore(
const QModelIndex &parent)
1266 QAbstractItemModel::fetchMore(parent);
1270
1271
1272bool QRangeModel::hasChildren(
const QModelIndex &parent)
const
1274 return QAbstractItemModel::hasChildren(parent);
1278
1279
1280QModelIndex QRangeModel::buddy(
const QModelIndex &index)
const
1282 return QAbstractItemModel::buddy(index);
1286
1287
1288bool QRangeModel::canDropMimeData(
const QMimeData *data, Qt::DropAction action,
1289 int row,
int column,
const QModelIndex &parent)
const
1291 return QAbstractItemModel::canDropMimeData(data, action, row, column, parent);
1295
1296
1297bool QRangeModel::dropMimeData(
const QMimeData *data, Qt::DropAction action,
1298 int row,
int column,
const QModelIndex &parent)
1300 return QAbstractItemModel::dropMimeData(data, action, row, column, parent);
1304
1305
1306QMimeData *QRangeModel::mimeData(
const QModelIndexList &indexes)
const
1308 return QAbstractItemModel::mimeData(indexes);
1312
1313
1314QStringList QRangeModel::mimeTypes()
const
1316 return QAbstractItemModel::mimeTypes();
1320
1321
1322QModelIndexList QRangeModel::match(
const QModelIndex &start,
int role,
const QVariant &value,
1323 int hits, Qt::MatchFlags flags)
const
1325 return QAbstractItemModel::match(start, role, value, hits, flags);
1329
1330
1331void QRangeModel::multiData(
const QModelIndex &index, QModelRoleDataSpan roleDataSpan)
const
1333 Q_D(
const QRangeModel);
1334 d->impl->call<QRangeModelImplBase::MultiData>(index, roleDataSpan);
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1355QHash<
int, QByteArray> QRangeModelImplBase::roleNamesForMetaObject(
const QAbstractItemModel &model,
1356 const QMetaObject &metaObject)
1358 const auto defaults = model.QAbstractItemModel::roleNames();
1359 QHash<
int, QByteArray> result = {{Qt::RangeModelDataRole,
"modelData"}};
1360 int offset = metaObject.propertyOffset();
1361 for (
int i = offset; i < metaObject.propertyCount(); ++i) {
1362 const auto name = metaObject.property(i).name();
1363 const int defaultRole = defaults.key(name, -1);
1364 if (defaultRole != -1) {
1366 result[defaultRole] = name;
1368 result[Qt::UserRole + i - offset] = name;
1374QHash<
int, QByteArray> QRangeModelImplBase::roleNamesForSimpleType()
1377 return QHash<
int, QByteArray>{
1378 {Qt::DisplayRole,
"display"},
1379 {Qt::EditRole,
"edit"},
1380 {Qt::RangeModelDataRole,
"modelData"},
1385
1386
1387
1388
1389
1390QHash<
int, QByteArray> QRangeModel::roleNames()
const
1392 Q_D(
const QRangeModel);
1393 if (d->m_roleNames.isEmpty())
1394 d->m_roleNames = d->impl->call<QRangeModelImplBase::RoleNames>();
1396 return d->m_roleNames;
1399void QRangeModel::setRoleNames(
const QHash<
int, QByteArray> &names)
1402 if (d->m_roleNames == names)
1405 d->impl->call<QRangeModelImplBase::InvalidateCaches>();
1406 if (d->m_autoConnectPolicy != AutoConnectPolicy::None)
1407 d->impl->call<QRangeModelImplBase::SetAutoConnectPolicy>();
1409 d->m_roleNames = names;
1411 Q_EMIT roleNamesChanged();
1414void QRangeModel::resetRoleNames()
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1472QRangeModel::AutoConnectPolicy QRangeModel::autoConnectPolicy()
const
1474 Q_D(
const QRangeModel);
1475 return d->m_autoConnectPolicy;
1478void QRangeModel::setAutoConnectPolicy(QRangeModel::AutoConnectPolicy policy)
1481 if (d->m_autoConnectPolicy == policy)
1484 d->m_autoConnectPolicy = policy;
1485 d->impl->call<QRangeModelImplBase::SetAutoConnectPolicy>();
1486 Q_EMIT autoConnectPolicyChanged(policy);
1490
1491
1492void QRangeModel::sort(
int column, Qt::SortOrder order)
1494 return QAbstractItemModel::sort(column, order);
1498
1499
1500QSize QRangeModel::span(
const QModelIndex &index)
const
1502 return QAbstractItemModel::span(index);
1506
1507
1508Qt::DropActions QRangeModel::supportedDragActions()
const
1510 return QAbstractItemModel::supportedDragActions();
1514
1515
1516Qt::DropActions QRangeModel::supportedDropActions()
const
1518 return QAbstractItemModel::supportedDropActions();
1522
1523
1524void QRangeModel::resetInternalData()
1526 QAbstractItemModel::resetInternalData();
1530
1531
1532bool QRangeModel::event(QEvent *event)
1534 return QAbstractItemModel::event(event);
1538
1539
1540bool QRangeModel::eventFilter(QObject *object, QEvent *event)
1542 return QAbstractItemModel::eventFilter(object, event);
1547#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)