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 = 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
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
759
760
761
762
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
802
803
804
805
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
841
842
843
844
845
846QRangeModel::~QRangeModel() =
default;
849
850
851
852
853
854
855
856
857
858QModelIndex QRangeModel::index(
int row,
int column,
const QModelIndex &parent)
const
860 Q_D(
const QRangeModel);
861 return d->impl->call<QRangeModelImplBase::Index>(row, column, parent);
865
866
867
868
869
870
871
872
873
874
875
876QModelIndex QRangeModel::parent(
const QModelIndex &child)
const
878 Q_D(
const QRangeModel);
879 return d->impl->call<QRangeModelImplBase::Parent>(child);
883
884
885
886
887
888
889
890
891
892
893QModelIndex QRangeModel::sibling(
int row,
int column,
const QModelIndex &index)
const
895 Q_D(
const QRangeModel);
896 return d->impl->call<QRangeModelImplBase::Sibling>(row, column, index);
900
901
902
903
904
905
906
907
908
909
910
911
912int QRangeModel::rowCount(
const QModelIndex &parent)
const
914 Q_D(
const QRangeModel);
915 return d->impl->call<QRangeModelImplBase::RowCount>(parent);
919
920
921
922
923
924
925
926
927
928
929
930
931int QRangeModel::columnCount(
const QModelIndex &parent)
const
933 Q_D(
const QRangeModel);
934 return d->impl->call<QRangeModelImplBase::ColumnCount>(parent);
938
939
940
941
942
943
944
945
946
947
948
949Qt::ItemFlags QRangeModel::flags(
const QModelIndex &index)
const
951 Q_D(
const QRangeModel);
952 return d->impl->call<QRangeModelImplBase::Flags>(index);
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976QVariant QRangeModel::headerData(
int section, Qt::Orientation orientation,
int role)
const
978 Q_D(
const QRangeModel);
979 return d->impl->call<QRangeModelImplBase::HeaderData>(section, orientation, role);
983
984
985bool QRangeModel::setHeaderData(
int section, Qt::Orientation orientation,
const QVariant &data,
988 return QAbstractItemModel::setHeaderData(section, orientation, data, role);
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012QVariant QRangeModel::data(
const QModelIndex &index,
int role)
const
1014 Q_D(
const QRangeModel);
1015 return d->impl->call<QRangeModelImplBase::Data>(index, role);
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044bool QRangeModel::setData(
const QModelIndex &index,
const QVariant &data,
int role)
1047 return d->impl->call<QRangeModelImplBase::SetData>(index, data, role);
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068QMap<
int, QVariant> QRangeModel::itemData(
const QModelIndex &index)
const
1070 Q_D(
const QRangeModel);
1071 return d->impl->call<QRangeModelImplBase::ItemData>(index);
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100bool QRangeModel::setItemData(
const QModelIndex &index,
const QMap<
int, QVariant> &data)
1103 return d->impl->call<QRangeModelImplBase::SetItemData>(index, data);
1107
1108
1109
1110
1111
1112
1113
1114bool QRangeModel::clearItemData(
const QModelIndex &index)
1117 return d->impl->call<QRangeModelImplBase::ClearItemData>(index);
1121
1122
1123
1124
1125
1126
1127
1128
1129
1132
1133
1134
1135
1136
1137
1138
1139
1140bool QRangeModel::insertColumns(
int column,
int count,
const QModelIndex &parent)
1143 return d->impl->call<QRangeModelImplBase::InsertColumns>(column, count, parent);
1147
1148
1149
1150
1151
1152
1153
1154
1155bool QRangeModel::removeColumns(
int column,
int count,
const QModelIndex &parent)
1158 return d->impl->call<QRangeModelImplBase::RemoveColumns>(column, count, parent);
1162
1163
1164
1165
1166
1167
1168
1169
1170bool QRangeModel::moveColumns(
const QModelIndex &sourceParent,
int sourceColumn,
int count,
1171 const QModelIndex &destinationParent,
int destinationColumn)
1174 return d->impl->call<QRangeModelImplBase::MoveColumns>(
1175 sourceParent, sourceColumn, count,
1176 destinationParent, destinationColumn);
1180
1181
1182
1183
1184
1185
1186
1187
1188
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201bool QRangeModel::insertRows(
int row,
int count,
const QModelIndex &parent)
1204 return d->impl->call<QRangeModelImplBase::InsertRows>(row, count, parent);
1208
1209
1210
1211
1212
1213
1214
1215bool QRangeModel::removeRows(
int row,
int count,
const QModelIndex &parent)
1218 return d->impl->call<QRangeModelImplBase::RemoveRows>(row, count, parent);
1222
1223
1224
1225
1226
1227
1228
1229
1230bool QRangeModel::moveRows(
const QModelIndex &sourceParent,
int sourceRow,
int count,
1231 const QModelIndex &destinationParent,
int destinationRow)
1234 return d->impl->call<QRangeModelImplBase::MoveRows>(
1235 sourceParent, sourceRow, count,
1236 destinationParent, destinationRow);
1240
1241
1242bool QRangeModel::canFetchMore(
const QModelIndex &parent)
const
1244 return QAbstractItemModel::canFetchMore(parent);
1248
1249
1250void QRangeModel::fetchMore(
const QModelIndex &parent)
1252 QAbstractItemModel::fetchMore(parent);
1256
1257
1258bool QRangeModel::hasChildren(
const QModelIndex &parent)
const
1260 return QAbstractItemModel::hasChildren(parent);
1264
1265
1266QModelIndex QRangeModel::buddy(
const QModelIndex &index)
const
1268 return QAbstractItemModel::buddy(index);
1272
1273
1274bool QRangeModel::canDropMimeData(
const QMimeData *data, Qt::DropAction action,
1275 int row,
int column,
const QModelIndex &parent)
const
1277 return QAbstractItemModel::canDropMimeData(data, action, row, column, parent);
1281
1282
1283bool QRangeModel::dropMimeData(
const QMimeData *data, Qt::DropAction action,
1284 int row,
int column,
const QModelIndex &parent)
1286 return QAbstractItemModel::dropMimeData(data, action, row, column, parent);
1290
1291
1292QMimeData *QRangeModel::mimeData(
const QModelIndexList &indexes)
const
1294 return QAbstractItemModel::mimeData(indexes);
1298
1299
1300QStringList QRangeModel::mimeTypes()
const
1302 return QAbstractItemModel::mimeTypes();
1306
1307
1308QModelIndexList QRangeModel::match(
const QModelIndex &start,
int role,
const QVariant &value,
1309 int hits, Qt::MatchFlags flags)
const
1311 return QAbstractItemModel::match(start, role, value, hits, flags);
1315
1316
1317void QRangeModel::multiData(
const QModelIndex &index, QModelRoleDataSpan roleDataSpan)
const
1319 Q_D(
const QRangeModel);
1320 d->impl->call<QRangeModelImplBase::MultiData>(index, roleDataSpan);
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1341QHash<
int, QByteArray> QRangeModelImplBase::roleNamesForMetaObject(
const QAbstractItemModel &model,
1342 const QMetaObject &metaObject)
1344 const auto defaults = model.QAbstractItemModel::roleNames();
1345 QHash<
int, QByteArray> result = {{Qt::RangeModelDataRole,
"modelData"}};
1346 int offset = metaObject.propertyOffset();
1347 for (
int i = offset; i < metaObject.propertyCount(); ++i) {
1348 const auto name = metaObject.property(i).name();
1349 const int defaultRole = defaults.key(name, -1);
1350 if (defaultRole != -1) {
1352 result[defaultRole] = name;
1354 result[Qt::UserRole + i - offset] = name;
1360QHash<
int, QByteArray> QRangeModelImplBase::roleNamesForSimpleType()
1363 return QHash<
int, QByteArray>{
1364 {Qt::DisplayRole,
"display"},
1365 {Qt::EditRole,
"edit"},
1366 {Qt::RangeModelDataRole,
"modelData"},
1371
1372
1373
1374
1375
1376QHash<
int, QByteArray> QRangeModel::roleNames()
const
1378 Q_D(
const QRangeModel);
1379 if (d->m_roleNames.isEmpty())
1380 d->m_roleNames = d->impl->call<QRangeModelImplBase::RoleNames>();
1382 return d->m_roleNames;
1385void QRangeModel::setRoleNames(
const QHash<
int, QByteArray> &names)
1388 if (d->m_roleNames == names)
1391 d->impl->call<QRangeModelImplBase::InvalidateCaches>();
1392 if (d->m_autoConnectPolicy != AutoConnectPolicy::None)
1393 d->impl->call<QRangeModelImplBase::SetAutoConnectPolicy>();
1395 d->m_roleNames = names;
1397 Q_EMIT roleNamesChanged();
1400void QRangeModel::resetRoleNames()
1406
1407
1408
1409
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
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1458QRangeModel::AutoConnectPolicy QRangeModel::autoConnectPolicy()
const
1460 Q_D(
const QRangeModel);
1461 return d->m_autoConnectPolicy;
1464void QRangeModel::setAutoConnectPolicy(QRangeModel::AutoConnectPolicy policy)
1467 if (d->m_autoConnectPolicy == policy)
1470 d->m_autoConnectPolicy = policy;
1471 d->impl->call<QRangeModelImplBase::SetAutoConnectPolicy>();
1472 Q_EMIT autoConnectPolicyChanged(policy);
1476
1477
1478void QRangeModel::sort(
int column, Qt::SortOrder order)
1480 return QAbstractItemModel::sort(column, order);
1484
1485
1486QSize QRangeModel::span(
const QModelIndex &index)
const
1488 return QAbstractItemModel::span(index);
1492
1493
1494Qt::DropActions QRangeModel::supportedDragActions()
const
1496 return QAbstractItemModel::supportedDragActions();
1500
1501
1502Qt::DropActions QRangeModel::supportedDropActions()
const
1504 return QAbstractItemModel::supportedDropActions();
1508
1509
1510void QRangeModel::resetInternalData()
1512 QAbstractItemModel::resetInternalData();
1516
1517
1518bool QRangeModel::event(QEvent *event)
1520 return QAbstractItemModel::event(event);
1524
1525
1526bool QRangeModel::eventFilter(QObject *object, QEvent *event)
1528 return QAbstractItemModel::eventFilter(object, event);
1533#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 & operator=(const QMetaObject::Connection &connection) noexcept
PropertyChangedHandler(const QPersistentModelIndex &index, int role)