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));
152template <
auto Handler>
154 const QHash<
int, QMetaProperty> &properties)
158 for (
auto &&[role, property] : properties.asKeyValueRange()) {
159 if (property.hasNotifySignal()) {
160 if (!Handler(index, item, context, role, property))
163 qWarning() <<
"Property" << property.name() <<
"for" << Qt::ItemDataRole(role)
164 <<
"has no notify signal";
170bool QRangeModelImplBase::connectProperty(
const QModelIndex &index, QObject *item, QObject *context,
171 int role,
const QMetaProperty &property)
175 PropertyChangedHandler handler{index, role};
176 auto connection = property.enclosingMetaObject()->connect(item, property.notifySignal(),
177 context, std::move(handler));
179 qWarning() <<
"Failed to connect to" << item << property.name();
187 handler = connection;
192bool QRangeModelImplBase::connectProperties(
const QModelIndex &index, QObject *item, QObject *context,
193 const QHash<
int, QMetaProperty> &properties)
195 return connectPropertiesHelper<QRangeModelImplBase::connectProperty>(index, item, context, properties);
198bool QRangeModelImplBase::connectPropertyConst(
const QModelIndex &index, QObject *item, QObject *context,
199 int role,
const QMetaProperty &property)
203 ConstPropertyChangedHandler handler{index, role};
204 if (!property.enclosingMetaObject()->connect(item, property.notifySignal(),
205 context, std::move(handler))) {
206 qWarning() <<
"Failed to connect to" << item << property.name();
213bool QRangeModelImplBase::connectPropertiesConst(
const QModelIndex &index, QObject *item, QObject *context,
214 const QHash<
int, QMetaProperty> &properties)
216 return connectPropertiesHelper<QRangeModelImplBase::connectPropertyConst>(index, item, context, properties);
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
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
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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
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
796
797
798
799
800
801QRangeModel::~QRangeModel() =
default;
804
805
806
807
808
809
810
811
812
813QModelIndex QRangeModel::index(
int row,
int column,
const QModelIndex &parent)
const
815 Q_D(
const QRangeModel);
816 return d->impl->call<QRangeModelImplBase::Index>(row, column, parent);
820
821
822
823
824
825
826
827
828
829
830
831QModelIndex QRangeModel::parent(
const QModelIndex &child)
const
833 Q_D(
const QRangeModel);
834 return d->impl->call<QRangeModelImplBase::Parent>(child);
838
839
840
841
842
843
844
845
846
847
848QModelIndex QRangeModel::sibling(
int row,
int column,
const QModelIndex &index)
const
850 Q_D(
const QRangeModel);
851 return d->impl->call<QRangeModelImplBase::Sibling>(row, column, index);
855
856
857
858
859
860
861
862
863
864
865
866
867int QRangeModel::rowCount(
const QModelIndex &parent)
const
869 Q_D(
const QRangeModel);
870 return d->impl->call<QRangeModelImplBase::RowCount>(parent);
874
875
876
877
878
879
880
881
882
883
884
885
886int QRangeModel::columnCount(
const QModelIndex &parent)
const
888 Q_D(
const QRangeModel);
889 return d->impl->call<QRangeModelImplBase::ColumnCount>(parent);
893
894
895
896
897
898
899
900
901
902
903
904Qt::ItemFlags QRangeModel::flags(
const QModelIndex &index)
const
906 Q_D(
const QRangeModel);
907 return d->impl->call<QRangeModelImplBase::Flags>(index);
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931QVariant QRangeModel::headerData(
int section, Qt::Orientation orientation,
int role)
const
933 Q_D(
const QRangeModel);
934 return d->impl->call<QRangeModelImplBase::HeaderData>(section, orientation, role);
938
939
940bool QRangeModel::setHeaderData(
int section, Qt::Orientation orientation,
const QVariant &data,
943 return QAbstractItemModel::setHeaderData(section, orientation, data, role);
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967QVariant QRangeModel::data(
const QModelIndex &index,
int role)
const
969 Q_D(
const QRangeModel);
970 return d->impl->call<QRangeModelImplBase::Data>(index, role);
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999bool QRangeModel::setData(
const QModelIndex &index,
const QVariant &data,
int role)
1002 return d->impl->call<QRangeModelImplBase::SetData>(index, data, role);
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023QMap<
int, QVariant> QRangeModel::itemData(
const QModelIndex &index)
const
1025 Q_D(
const QRangeModel);
1026 return d->impl->call<QRangeModelImplBase::ItemData>(index);
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055bool QRangeModel::setItemData(
const QModelIndex &index,
const QMap<
int, QVariant> &data)
1058 return d->impl->call<QRangeModelImplBase::SetItemData>(index, data);
1062
1063
1064
1065
1066
1067
1068
1069bool QRangeModel::clearItemData(
const QModelIndex &index)
1072 return d->impl->call<QRangeModelImplBase::ClearItemData>(index);
1076
1077
1078
1079
1080
1081
1082
1083
1084
1087
1088
1089
1090
1091
1092
1093
1094
1095bool QRangeModel::insertColumns(
int column,
int count,
const QModelIndex &parent)
1098 return d->impl->call<QRangeModelImplBase::InsertColumns>(column, count, parent);
1102
1103
1104
1105
1106
1107
1108
1109
1110bool QRangeModel::removeColumns(
int column,
int count,
const QModelIndex &parent)
1113 return d->impl->call<QRangeModelImplBase::RemoveColumns>(column, count, parent);
1117
1118
1119
1120
1121
1122
1123
1124
1125bool QRangeModel::moveColumns(
const QModelIndex &sourceParent,
int sourceColumn,
int count,
1126 const QModelIndex &destinationParent,
int destinationColumn)
1129 return d->impl->call<QRangeModelImplBase::MoveColumns>(
1130 sourceParent, sourceColumn, count,
1131 destinationParent, destinationColumn);
1135
1136
1137
1138
1139
1140
1141
1142
1143
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156bool QRangeModel::insertRows(
int row,
int count,
const QModelIndex &parent)
1159 return d->impl->call<QRangeModelImplBase::InsertRows>(row, count, parent);
1163
1164
1165
1166
1167
1168
1169
1170bool QRangeModel::removeRows(
int row,
int count,
const QModelIndex &parent)
1173 return d->impl->call<QRangeModelImplBase::RemoveRows>(row, count, parent);
1177
1178
1179
1180
1181
1182
1183
1184
1185bool QRangeModel::moveRows(
const QModelIndex &sourceParent,
int sourceRow,
int count,
1186 const QModelIndex &destinationParent,
int destinationRow)
1189 return d->impl->call<QRangeModelImplBase::MoveRows>(
1190 sourceParent, sourceRow, count,
1191 destinationParent, destinationRow);
1195
1196
1197bool QRangeModel::canFetchMore(
const QModelIndex &parent)
const
1199 return QAbstractItemModel::canFetchMore(parent);
1203
1204
1205void QRangeModel::fetchMore(
const QModelIndex &parent)
1207 QAbstractItemModel::fetchMore(parent);
1211
1212
1213bool QRangeModel::hasChildren(
const QModelIndex &parent)
const
1215 return QAbstractItemModel::hasChildren(parent);
1219
1220
1221QModelIndex QRangeModel::buddy(
const QModelIndex &index)
const
1223 return QAbstractItemModel::buddy(index);
1227
1228
1229bool QRangeModel::canDropMimeData(
const QMimeData *data, Qt::DropAction action,
1230 int row,
int column,
const QModelIndex &parent)
const
1232 return QAbstractItemModel::canDropMimeData(data, action, row, column, parent);
1236
1237
1238bool QRangeModel::dropMimeData(
const QMimeData *data, Qt::DropAction action,
1239 int row,
int column,
const QModelIndex &parent)
1241 return QAbstractItemModel::dropMimeData(data, action, row, column, parent);
1245
1246
1247QMimeData *QRangeModel::mimeData(
const QModelIndexList &indexes)
const
1249 return QAbstractItemModel::mimeData(indexes);
1253
1254
1255QStringList QRangeModel::mimeTypes()
const
1257 return QAbstractItemModel::mimeTypes();
1261
1262
1263QModelIndexList QRangeModel::match(
const QModelIndex &start,
int role,
const QVariant &value,
1264 int hits, Qt::MatchFlags flags)
const
1266 return QAbstractItemModel::match(start, role, value, hits, flags);
1270
1271
1272void QRangeModel::multiData(
const QModelIndex &index, QModelRoleDataSpan roleDataSpan)
const
1274 Q_D(
const QRangeModel);
1275 d->impl->call<QRangeModelImplBase::MultiData>(index, roleDataSpan);
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1296QHash<
int, QByteArray> QRangeModelImplBase::roleNamesForMetaObject(
const QAbstractItemModel &model,
1297 const QMetaObject &metaObject)
1299 const auto defaults = model.QAbstractItemModel::roleNames();
1300 QHash<
int, QByteArray> result = {{Qt::RangeModelDataRole,
"modelData"}};
1301 int offset = metaObject.propertyOffset();
1302 for (
int i = offset; i < metaObject.propertyCount(); ++i) {
1303 const auto name = metaObject.property(i).name();
1304 const int defaultRole = defaults.key(name, -1);
1305 if (defaultRole != -1) {
1307 result[defaultRole] = name;
1309 result[Qt::UserRole + i - offset] = name;
1315QHash<
int, QByteArray> QRangeModelImplBase::roleNamesForSimpleType()
1318 return QHash<
int, QByteArray>{
1319 {Qt::DisplayRole,
"display"},
1320 {Qt::EditRole,
"edit"},
1321 {Qt::RangeModelDataRole,
"modelData"},
1326
1327
1328
1329
1330
1331QHash<
int, QByteArray> QRangeModel::roleNames()
const
1333 Q_D(
const QRangeModel);
1334 if (d->m_roleNames.isEmpty())
1335 d->m_roleNames = d->impl->call<QRangeModelImplBase::RoleNames>();
1337 return d->m_roleNames;
1340void QRangeModel::setRoleNames(
const QHash<
int, QByteArray> &names)
1343 if (d->m_roleNames == names)
1346 d->impl->call<QRangeModelImplBase::InvalidateCaches>();
1347 if (d->m_autoConnectPolicy != AutoConnectPolicy::None)
1348 d->impl->call<QRangeModelImplBase::SetAutoConnectPolicy>();
1350 d->m_roleNames = names;
1352 Q_EMIT roleNamesChanged();
1355void QRangeModel::resetRoleNames()
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1411QRangeModel::AutoConnectPolicy QRangeModel::autoConnectPolicy()
const
1413 Q_D(
const QRangeModel);
1414 return d->m_autoConnectPolicy;
1417void QRangeModel::setAutoConnectPolicy(QRangeModel::AutoConnectPolicy policy)
1420 if (d->m_autoConnectPolicy == policy)
1423 d->m_autoConnectPolicy = policy;
1424 d->impl->call<QRangeModelImplBase::SetAutoConnectPolicy>();
1425 Q_EMIT autoConnectPolicyChanged();
1429
1430
1431void QRangeModel::sort(
int column, Qt::SortOrder order)
1433 return QAbstractItemModel::sort(column, order);
1437
1438
1439QSize QRangeModel::span(
const QModelIndex &index)
const
1441 return QAbstractItemModel::span(index);
1445
1446
1447Qt::DropActions QRangeModel::supportedDragActions()
const
1449 return QAbstractItemModel::supportedDragActions();
1453
1454
1455Qt::DropActions QRangeModel::supportedDropActions()
const
1457 return QAbstractItemModel::supportedDropActions();
1461
1462
1463void QRangeModel::resetInternalData()
1465 QAbstractItemModel::resetInternalData();
1469
1470
1471bool QRangeModel::event(QEvent *event)
1473 return QAbstractItemModel::event(event);
1477
1478
1479bool QRangeModel::eventFilter(QObject *object, QEvent *event)
1481 return QAbstractItemModel::eventFilter(object, event);
1486#include "moc_qrangemodel.cpp"
static bool connectPropertiesHelper(const QModelIndex &index, QObject *item, QObject *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)