190 for (
int role : roles) {
191 if (role == Qt::AccessibleTextRole
192 || (role == Qt::DisplayRole
193 && index.data(Qt::AccessibleTextRole).toString().isEmpty())) {
194 QAccessibleEvent event(q, QAccessible::NameChanged);
195 event.setChild(childIndex);
196 QAccessible::updateAccessibility(&event);
197 }
else if (role == Qt::AccessibleDescriptionRole) {
198 QAccessibleEvent event(q, QAccessible::DescriptionChanged);
199 event.setChild(childIndex);
200 QAccessible::updateAccessibility(&event);
201 }
else if (role == Qt::CheckStateRole) {
202 QAccessible::State state;
203 state.checked =
true;
204 QAccessibleStateChangeEvent event(q, state);
205 event.setChild(childIndex);
206 QAccessible::updateAccessibility(&event);
212#if QT_CONFIG(gestures) && QT_CONFIG(scroller)
215void QAbstractItemViewPrivate::scrollerStateChanged()
217 Q_Q(QAbstractItemView);
219 if (QScroller *scroller = QScroller::scroller(viewport)) {
220 switch (scroller->state()) {
221 case QScroller::Pressed:
223 if (q->selectionModel()) {
224 oldSelection = q->selectionModel()->selection();
225 oldCurrent = q->selectionModel()->currentIndex();
229 case QScroller::Dragging:
231 if (q->selectionModel()) {
232 q->selectionModel()->select(oldSelection, QItemSelectionModel::ClearAndSelect);
234 const bool wasAutoScroll = autoScroll;
236 q->selectionModel()->setCurrentIndex(oldCurrent, QItemSelectionModel::NoUpdate);
237 autoScroll = wasAutoScroll;
242 oldSelection = QItemSelection();
243 oldCurrent = QModelIndex();
251void QAbstractItemViewPrivate::delegateSizeHintChanged(
const QModelIndex &index)
253 Q_Q(QAbstractItemView);
255 if (!model->checkIndex(index))
256 qWarning(
"Delegate size hint changed for a model index that does not belong to this view");
258 QMetaObject::invokeMethod(q, &QAbstractItemView::doItemsLayout, Qt::QueuedConnection);
261void QAbstractItemViewPrivate::connectDelegate(QAbstractItemDelegate *delegate)
265 Q_Q(QAbstractItemView);
266 QObject::connect(delegate, &QAbstractItemDelegate::closeEditor,
267 q, &QAbstractItemView::closeEditor);
268 QObject::connect(delegate, &QAbstractItemDelegate::commitData,
269 q, &QAbstractItemView::commitData);
270 QObjectPrivate::connect(delegate, &QAbstractItemDelegate::sizeHintChanged,
271 this, &QAbstractItemViewPrivate::delegateSizeHintChanged);
274void QAbstractItemViewPrivate::disconnectDelegate(QAbstractItemDelegate *delegate)
278 Q_Q(QAbstractItemView);
279 QObject::disconnect(delegate, &QAbstractItemDelegate::closeEditor,
280 q, &QAbstractItemView::closeEditor);
281 QObject::disconnect(delegate, &QAbstractItemDelegate::commitData,
282 q, &QAbstractItemView::commitData);
283 QObjectPrivate::disconnect(delegate, &QAbstractItemDelegate::sizeHintChanged,
284 this, &QAbstractItemViewPrivate::delegateSizeHintChanged);
287void QAbstractItemViewPrivate::disconnectAll()
289 Q_Q(QAbstractItemView);
290 for (
const QMetaObject::Connection &connection : modelConnections)
291 QObject::disconnect(connection);
292 for (
const QMetaObject::Connection &connection : scrollbarConnections)
293 QObject::disconnect(connection);
294 disconnectDelegate(itemDelegate);
295 for (QAbstractItemDelegate *delegate : std::as_const(rowDelegates))
296 disconnectDelegate(delegate);
297 for (QAbstractItemDelegate *delegate : std::as_const(columnDelegates))
298 disconnectDelegate(delegate);
299 if (model && selectionModel) {
300 QObject::disconnect(model, &QAbstractItemModel::destroyed,
301 selectionModel, &QItemSelectionModel::deleteLater);
303 if (selectionModel) {
304 QObject::disconnect(selectionModel, &QItemSelectionModel::selectionChanged,
305 q, &QAbstractItemView::selectionChanged);
306 QObject::disconnect(selectionModel, &QItemSelectionModel::currentChanged,
307 q, &QAbstractItemView::currentChanged);
309 for (
const auto &info : std::as_const(indexEditorHash)) {
310 if (!info.isStatic && info.widget)
311 QObject::disconnect(info.widget, &QWidget::destroyed, q, &QAbstractItemView::editorDestroyed);
313#if QT_CONFIG(gestures) && QT_CONFIG(scroller)
314 QObject::disconnect(scollerConnection);
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
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
455
456
457
458
459
460
463
464
465
466
467
468
469
470
471
472
476
477
478
479
480
481
482
483
484
485
486
487
488
489
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
510
511
512
513
514
515
516
517
518
519
520
521
522
525
526
527
528
529
530
531
532
533
534
537
538
539
540
541
542
543
544
545
546
547
550
551
552
553
554
555
556
559
560
561
562
563
564
565
566
569
570
571
572
573
574
575
576
577
580
581
582
583
584
585
586
587
590
591
592
593
594
595
596
599
600
601
602
603
604
605
606
607
608
609
612
613
614
615
616
617
618
619
622
623
624
625
626
627
628
629
632
633
634
635
636
637
638
639
642
643
644
645
646
647
648
649
652
653
654
655
656
657
658
659
662
663
664
665
666
667
668
669
670
671
674
675
676
677
678
679
680
681
682
683
684
685
688
689
690
691
692
693
694
695
696
699
700
701QAbstractItemView::QAbstractItemView(QWidget *parent)
702 : QAbstractScrollArea(*(
new QAbstractItemViewPrivate), parent)
708
709
710QAbstractItemView::QAbstractItemView(QAbstractItemViewPrivate &dd, QWidget *parent)
711 : QAbstractScrollArea(dd, parent)
717
718
719QAbstractItemView::~QAbstractItemView()
721 Q_D(QAbstractItemView);
723 d->delayedReset.stop();
724 d->updateTimer.stop();
725 d->delayedEditing.stop();
726 d->delayedAutoScroll.stop();
727 d->autoScrollTimer.stop();
728 d->delayedLayout.stop();
729 d->fetchMoreTimer.stop();
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753void QAbstractItemView::setModel(QAbstractItemModel *model)
755 Q_D(QAbstractItemView);
756 if (model == d->model)
758 if (d->model && d->model != QAbstractItemModelPrivate::staticEmptyModel()) {
759 for (
const QMetaObject::Connection &connection : d->modelConnections)
760 disconnect(connection);
762 d->model = (model ? model : QAbstractItemModelPrivate::staticEmptyModel());
764 if (d->model != QAbstractItemModelPrivate::staticEmptyModel()) {
765 d->modelConnections = {
766 QObjectPrivate::connect(d->model, &QAbstractItemModel::destroyed,
767 d, &QAbstractItemViewPrivate::modelDestroyed),
768 QObject::connect(d->model, &QAbstractItemModel::dataChanged,
769 this, &QAbstractItemView::dataChanged),
770 QObjectPrivate::connect(d->model, &QAbstractItemModel::headerDataChanged,
771 d, &QAbstractItemViewPrivate::headerDataChanged),
772 QObject::connect(d->model, &QAbstractItemModel::rowsInserted,
773 this, &QAbstractItemView::rowsInserted),
774 QObjectPrivate::connect(d->model, &QAbstractItemModel::rowsInserted,
775 d, &QAbstractItemViewPrivate::rowsInserted),
776 QObject::connect(d->model, &QAbstractItemModel::rowsAboutToBeRemoved,
777 this, &QAbstractItemView::rowsAboutToBeRemoved),
778 QObjectPrivate::connect(d->model, &QAbstractItemModel::rowsRemoved,
779 d, &QAbstractItemViewPrivate::rowsRemoved),
780 QObjectPrivate::connect(d->model, &QAbstractItemModel::rowsMoved,
781 d, &QAbstractItemViewPrivate::rowsMoved),
782 QObjectPrivate::connect(d->model, &QAbstractItemModel::columnsAboutToBeRemoved,
783 d, &QAbstractItemViewPrivate::columnsAboutToBeRemoved),
784 QObjectPrivate::connect(d->model, &QAbstractItemModel::columnsRemoved,
785 d, &QAbstractItemViewPrivate::columnsRemoved),
786 QObjectPrivate::connect(d->model, &QAbstractItemModel::columnsInserted,
787 d, &QAbstractItemViewPrivate::columnsInserted),
788 QObjectPrivate::connect(d->model, &QAbstractItemModel::columnsMoved,
789 d, &QAbstractItemViewPrivate::columnsMoved),
790 QObject::connect(d->model, &QAbstractItemModel::modelReset,
791 this, &QAbstractItemView::reset),
792 QObjectPrivate::connect(d->model, &QAbstractItemModel::layoutChanged,
793 d, &QAbstractItemViewPrivate::layoutChanged),
797 QItemSelectionModel *selection_model =
new QItemSelectionModel(d->model,
this);
798 connect(d->model, &QAbstractItemModel::destroyed,
799 selection_model, &QItemSelectionModel::deleteLater);
800 setSelectionModel(selection_model);
806
807
808QAbstractItemModel *QAbstractItemView::model()
const
810 Q_D(
const QAbstractItemView);
811 return (d->model == QAbstractItemModelPrivate::staticEmptyModel() ?
nullptr : d->model);
815
816
817
818
819
820
821
822
823
824
825
826
827
828void QAbstractItemView::setSelectionModel(QItemSelectionModel *selectionModel)
831 Q_ASSERT(selectionModel);
832 Q_D(QAbstractItemView);
834 if (Q_UNLIKELY(selectionModel->model() != d->model)) {
835 qWarning(
"QAbstractItemView::setSelectionModel() failed: "
836 "Trying to set a selection model, which works on "
837 "a different model than the view.");
841 QItemSelection oldSelection;
842 QModelIndex oldCurrentIndex;
844 if (d->selectionModel) {
845 if (d->selectionModel->model() == selectionModel->model()) {
846 oldSelection = d->selectionModel->selection();
847 oldCurrentIndex = d->selectionModel->currentIndex();
849 disconnect(d->selectionModel, &QItemSelectionModel::selectionChanged,
850 this, &QAbstractItemView::selectionChanged);
851 disconnect(d->selectionModel, &QItemSelectionModel::currentChanged,
852 this, &QAbstractItemView::currentChanged);
855 d->selectionModel = selectionModel;
857 if (d->selectionModel) {
858 connect(d->selectionModel, &QItemSelectionModel::selectionChanged,
859 this, &QAbstractItemView::selectionChanged);
860 connect(d->selectionModel, &QItemSelectionModel::currentChanged,
861 this, &QAbstractItemView::currentChanged);
863 selectionChanged(d->selectionModel->selection(), oldSelection);
864 currentChanged(d->selectionModel->currentIndex(), oldCurrentIndex);
869
870
871
872
873QItemSelectionModel* QAbstractItemView::selectionModel()
const
875 Q_D(
const QAbstractItemView);
876 return d->selectionModel;
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894void QAbstractItemView::setItemDelegate(QAbstractItemDelegate *delegate)
896 Q_D(QAbstractItemView);
897 if (delegate == d->itemDelegate)
900 if (d->itemDelegate) {
901 if (d->delegateRefCount(d->itemDelegate) == 1)
902 d->disconnectDelegate(d->itemDelegate);
906 if (d->delegateRefCount(delegate) == 0)
907 d->connectDelegate(delegate);
909 d->itemDelegate = delegate;
910 viewport()->update();
911 d->doDelayedItemsLayout();
915
916
917
918
919
920QAbstractItemDelegate *QAbstractItemView::itemDelegate()
const
922 return d_func()->itemDelegate;
926
927
928QVariant QAbstractItemView::inputMethodQuery(Qt::InputMethodQuery query)
const
930 Q_D(
const QAbstractItemView);
931 const QModelIndex current = currentIndex();
933 if (current.isValid()) {
934 if (QWidget *currentEditor;
935 d->waitForIMCommit && (currentEditor = d->editorForIndex(current).widget)) {
938 result = currentEditor->inputMethodQuery(query);
939 if (result.typeId() == QMetaType::QRect) {
940 const QRect editorRect = result.value<QRect>();
941 result = QRect(currentEditor->mapTo(
this, editorRect.topLeft()), editorRect.size());
943 }
else if (query == Qt::ImCursorRectangle) {
944 result = visualRect(current);
947 if (!result.isValid())
948 result = QAbstractScrollArea::inputMethodQuery(query);
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971void QAbstractItemView::setItemDelegateForRow(
int row, QAbstractItemDelegate *delegate)
973 Q_D(QAbstractItemView);
974 if (QAbstractItemDelegate *rowDelegate = d->rowDelegates.value(row,
nullptr)) {
975 if (d->delegateRefCount(rowDelegate) == 1)
976 d->disconnectDelegate(rowDelegate);
977 d->rowDelegates.remove(row);
980 if (d->delegateRefCount(delegate) == 0)
981 d->connectDelegate(delegate);
982 d->rowDelegates.insert(row, delegate);
984 viewport()->update();
985 d->doDelayedItemsLayout();
989
990
991
992
993
994
995QAbstractItemDelegate *QAbstractItemView::itemDelegateForRow(
int row)
const
997 Q_D(
const QAbstractItemView);
998 return d->rowDelegates.value(row,
nullptr);
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019void QAbstractItemView::setItemDelegateForColumn(
int column, QAbstractItemDelegate *delegate)
1021 Q_D(QAbstractItemView);
1022 if (QAbstractItemDelegate *columnDelegate = d->columnDelegates.value(column,
nullptr)) {
1023 if (d->delegateRefCount(columnDelegate) == 1)
1024 d->disconnectDelegate(columnDelegate);
1025 d->columnDelegates.remove(column);
1028 if (d->delegateRefCount(delegate) == 0)
1029 d->connectDelegate(delegate);
1030 d->columnDelegates.insert(column, delegate);
1032 viewport()->update();
1033 d->doDelayedItemsLayout();
1037
1038
1039
1040
1041
1042
1043QAbstractItemDelegate *QAbstractItemView::itemDelegateForColumn(
int column)
const
1045 Q_D(
const QAbstractItemView);
1046 return d->columnDelegates.value(column,
nullptr);
1050
1051
1052
1053
1054
1057
1058
1059
1060
1061
1062
1063
1064QAbstractItemDelegate *QAbstractItemView::itemDelegateForIndex(
const QModelIndex &index)
const
1066 Q_D(
const QAbstractItemView);
1067 return d->delegateForIndex(index);
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080void QAbstractItemView::setSelectionMode(SelectionMode mode)
1082 Q_D(QAbstractItemView);
1083 d->selectionMode = mode;
1086QAbstractItemView::SelectionMode QAbstractItemView::selectionMode()
const
1088 Q_D(
const QAbstractItemView);
1089 return d->selectionMode;
1093
1094
1095
1096
1097
1098
1099
1100
1102void QAbstractItemView::setSelectionBehavior(QAbstractItemView::SelectionBehavior behavior)
1104 Q_D(QAbstractItemView);
1105 d->selectionBehavior = behavior;
1108QAbstractItemView::SelectionBehavior QAbstractItemView::selectionBehavior()
const
1110 Q_D(
const QAbstractItemView);
1111 return d->selectionBehavior;
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128void QAbstractItemView::setCurrentIndex(
const QModelIndex &index)
1130 Q_D(QAbstractItemView);
1131 if (d->selectionModel && (!index.isValid() || d->isIndexEnabled(index))) {
1132 QItemSelectionModel::SelectionFlags command = selectionCommand(index,
nullptr);
1133 d->selectionModel->setCurrentIndex(index, command);
1134 d->currentIndexSet =
true;
1139
1140
1141
1142
1143QModelIndex QAbstractItemView::currentIndex()
const
1145 Q_D(
const QAbstractItemView);
1146 return d->selectionModel ? d->selectionModel->currentIndex() : QModelIndex();
1151
1152
1153
1154
1155
1156
1157
1158
1159void QAbstractItemView::reset()
1161 Q_D(QAbstractItemView);
1162 d->delayedReset.stop();
1166 const auto copy = d->indexEditorHash;
1167 for (
const auto &[index, info] : copy.asKeyValueRange()) {
1169 d->releaseEditor(info.widget.data(), d->indexForEditor(info.widget.data()));
1171 d->editorIndexHash.clear();
1172 d->indexEditorHash.clear();
1173 d->persistent.clear();
1174 d->currentIndexSet =
false;
1176 setRootIndex(QModelIndex());
1177 if (d->selectionModel)
1178 d->selectionModel->reset();
1179#if QT_CONFIG(accessibility)
1180 if (QAccessible::isActive()) {
1181 QAccessibleTableModelChangeEvent accessibleEvent(
this, QAccessibleTableModelChangeEvent::ModelReset);
1182 QAccessible::updateAccessibility(&accessibleEvent);
1185 d->updateGeometry();
1189
1190
1191
1192
1193void QAbstractItemView::setRootIndex(
const QModelIndex &index)
1195 Q_D(QAbstractItemView);
1196 if (Q_UNLIKELY(index.isValid() && index.model() != d->model)) {
1197 qWarning(
"QAbstractItemView::setRootIndex failed : index must be from the currently set model");
1201#if QT_CONFIG(accessibility)
1202 if (QAccessible::isActive()) {
1203 QAccessibleTableModelChangeEvent accessibleEvent(
this, QAccessibleTableModelChangeEvent::ModelReset);
1204 QAccessible::updateAccessibility(&accessibleEvent);
1207 d->doDelayedItemsLayout();
1208 d->updateGeometry();
1212
1213
1214
1215
1216
1217QModelIndex QAbstractItemView::rootIndex()
const
1219 return QModelIndex(d_func()->root);
1223
1224
1225
1226
1227
1228
1229void QAbstractItemView::selectAll()
1231 Q_D(QAbstractItemView);
1232 const SelectionMode mode = d->selectionMode;
1234 case MultiSelection:
1235 case ExtendedSelection:
1236 d->selectAll(QItemSelectionModel::ClearAndSelect
1237 | d->selectionBehaviorFlags());
1240 case ContiguousSelection:
1241 if (d->model->hasChildren(d->root))
1242 d->selectAll(selectionCommand(d->model->index(0, 0, d->root)));
1244 case SingleSelection:
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261void QAbstractItemView::edit(
const QModelIndex &index)
1263 Q_D(QAbstractItemView);
1264 if (Q_UNLIKELY(!d->isIndexValid(index)))
1265 qWarning(
"edit: index was invalid");
1266 if (Q_UNLIKELY(!edit(index, AllEditTriggers,
nullptr)))
1267 qWarning(
"edit: editing failed");
1271
1272
1273
1274
1275void QAbstractItemView::clearSelection()
1277 Q_D(QAbstractItemView);
1278 if (d->selectionModel)
1279 d->selectionModel->clearSelection();
1283
1284
1285
1286
1287
1288void QAbstractItemView::doItemsLayout()
1290 Q_D(QAbstractItemView);
1291 d->interruptDelayedItemsLayout();
1293 d->viewport->update();
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311void QAbstractItemView::setEditTriggers(EditTriggers actions)
1313 Q_D(QAbstractItemView);
1314 d->editTriggers = actions;
1317QAbstractItemView::EditTriggers QAbstractItemView::editTriggers()
const
1319 Q_D(
const QAbstractItemView);
1320 return d->editTriggers;
1324
1325
1326
1327
1328
1329
1330
1332void QAbstractItemView::setVerticalScrollMode(ScrollMode mode)
1334 Q_D(QAbstractItemView);
1335 d->verticalScrollModeSet =
true;
1336 if (mode == d->verticalScrollMode)
1338 QModelIndex topLeft = indexAt(QPoint(0, 0));
1339 d->verticalScrollMode = mode;
1340 if (mode == ScrollPerItem)
1341 verticalScrollBar()->d_func()->itemviewChangeSingleStep(1);
1343 verticalScrollBar()->setSingleStep(-1);
1345 scrollTo(topLeft, QAbstractItemView::PositionAtTop);
1348QAbstractItemView::ScrollMode QAbstractItemView::verticalScrollMode()
const
1350 Q_D(
const QAbstractItemView);
1351 return d->verticalScrollMode;
1354void QAbstractItemView::resetVerticalScrollMode()
1356 auto sm =
static_cast<ScrollMode>(style()->styleHint(QStyle::SH_ItemView_ScrollMode,
nullptr,
this,
nullptr));
1357 setVerticalScrollMode(sm);
1358 d_func()->verticalScrollModeSet =
false;
1362
1363
1364
1365
1366
1367
1368
1370void QAbstractItemView::setHorizontalScrollMode(ScrollMode mode)
1372 Q_D(QAbstractItemView);
1373 d->horizontalScrollModeSet =
true;
1374 if (mode == d->horizontalScrollMode)
1376 d->horizontalScrollMode = mode;
1377 if (mode == ScrollPerItem)
1378 horizontalScrollBar()->d_func()->itemviewChangeSingleStep(1);
1380 horizontalScrollBar()->setSingleStep(-1);
1384QAbstractItemView::ScrollMode QAbstractItemView::horizontalScrollMode()
const
1386 Q_D(
const QAbstractItemView);
1387 return d->horizontalScrollMode;
1390void QAbstractItemView::resetHorizontalScrollMode()
1392 auto sm =
static_cast<ScrollMode>(style()->styleHint(QStyle::SH_ItemView_ScrollMode,
nullptr,
this,
nullptr));
1393 setHorizontalScrollMode(sm);
1394 d_func()->horizontalScrollModeSet =
false;
1397#if QT_CONFIG(draganddrop)
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418void QAbstractItemView::setDragDropOverwriteMode(
bool overwrite)
1420 Q_D(QAbstractItemView);
1421 d->overwrite = overwrite;
1424bool QAbstractItemView::dragDropOverwriteMode()
const
1426 Q_D(
const QAbstractItemView);
1427 return d->overwrite;
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1445void QAbstractItemView::setAutoScroll(
bool enable)
1447 Q_D(QAbstractItemView);
1448 d->autoScroll = enable;
1451bool QAbstractItemView::hasAutoScroll()
const
1453 Q_D(
const QAbstractItemView);
1454 return d->autoScroll;
1458
1459
1460
1461
1462
1463
1464void QAbstractItemView::setAutoScrollMargin(
int margin)
1466 Q_D(QAbstractItemView);
1467 d->autoScrollMargin = margin;
1470int QAbstractItemView::autoScrollMargin()
const
1472 Q_D(
const QAbstractItemView);
1473 return d->autoScrollMargin;
1477
1478
1479
1481void QAbstractItemView::setTabKeyNavigation(
bool enable)
1483 Q_D(QAbstractItemView);
1484 d->tabKeyNavigation = enable;
1487bool QAbstractItemView::tabKeyNavigation()
const
1489 Q_D(
const QAbstractItemView);
1490 return d->tabKeyNavigation;
1494
1495
1496
1497QSize QAbstractItemView::viewportSizeHint()
const
1499 return QAbstractScrollArea::viewportSizeHint();
1502#if QT_CONFIG(draganddrop)
1504
1505
1506
1507
1508
1510void QAbstractItemView::setDropIndicatorShown(
bool enable)
1512 Q_D(QAbstractItemView);
1513 d->showDropIndicator = enable;
1516bool QAbstractItemView::showDropIndicator()
const
1518 Q_D(
const QAbstractItemView);
1519 return d->showDropIndicator;
1523
1524
1525
1526
1527
1529void QAbstractItemView::setDragEnabled(
bool enable)
1531 Q_D(QAbstractItemView);
1532 d->dragEnabled = enable;
1535bool QAbstractItemView::dragEnabled()
const
1537 Q_D(
const QAbstractItemView);
1538 return d->dragEnabled;
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1561
1562
1563
1564
1565
1566void QAbstractItemView::setDragDropMode(DragDropMode behavior)
1568 Q_D(QAbstractItemView);
1569 d->dragDropMode = behavior;
1570 setDragEnabled(behavior == DragOnly || behavior == DragDrop || behavior == InternalMove);
1571 setAcceptDrops(behavior == DropOnly || behavior == DragDrop || behavior == InternalMove);
1574QAbstractItemView::DragDropMode QAbstractItemView::dragDropMode()
const
1576 Q_D(
const QAbstractItemView);
1577 DragDropMode setBehavior = d->dragDropMode;
1578 if (!dragEnabled() && !acceptDrops())
1581 if (dragEnabled() && !acceptDrops())
1584 if (!dragEnabled() && acceptDrops())
1587 if (dragEnabled() && acceptDrops()) {
1588 if (setBehavior == InternalMove)
1598
1599
1600
1601
1602
1603
1604
1605
1606void QAbstractItemView::setDefaultDropAction(Qt::DropAction dropAction)
1608 Q_D(QAbstractItemView);
1609 d->defaultDropAction = dropAction;
1612Qt::DropAction QAbstractItemView::defaultDropAction()
const
1614 Q_D(
const QAbstractItemView);
1615 return d->defaultDropAction;
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630void QAbstractItemView::setAlternatingRowColors(
bool enable)
1632 Q_D(QAbstractItemView);
1633 d->alternatingColors = enable;
1635 d->viewport->update();
1638bool QAbstractItemView::alternatingRowColors()
const
1640 Q_D(
const QAbstractItemView);
1641 return d->alternatingColors;
1645
1646
1647
1648
1649
1650
1651void QAbstractItemView::setIconSize(
const QSize &size)
1653 Q_D(QAbstractItemView);
1654 if (size == d->iconSize)
1657 d->doDelayedItemsLayout();
1658 emit iconSizeChanged(size);
1661QSize QAbstractItemView::iconSize()
const
1663 Q_D(
const QAbstractItemView);
1668
1669
1670
1671
1672
1673
1674void QAbstractItemView::setTextElideMode(Qt::TextElideMode mode)
1676 Q_D(QAbstractItemView);
1677 d->textElideMode = mode;
1680Qt::TextElideMode QAbstractItemView::textElideMode()
const
1682 return d_func()->textElideMode;
1686
1687
1688bool QAbstractItemView::focusNextPrevChild(
bool next)
1690 Q_D(QAbstractItemView);
1691 if (d->tabKeyNavigation && isVisible() && isEnabled() && d->viewport->isEnabled()) {
1692 QKeyEvent event(QEvent::KeyPress, next ? Qt::Key_Tab : Qt::Key_Backtab, Qt::NoModifier);
1693 keyPressEvent(&event);
1694 if (event.isAccepted())
1697 return QAbstractScrollArea::focusNextPrevChild(next);
1701
1702
1703bool QAbstractItemView::event(QEvent *event)
1705 Q_D(QAbstractItemView);
1706 switch (event->type()) {
1710 d->executePostedLayout();
1713 d->executePostedLayout();
1714 if (d->shouldScrollToCurrentOnShow) {
1715 d->shouldScrollToCurrentOnShow =
false;
1716 const QModelIndex current = currentIndex();
1717 if (current.isValid() && (d->state == QAbstractItemView::EditingState || d->autoScroll))
1721 case QEvent::LocaleChange:
1722 viewport()->update();
1724 case QEvent::LayoutDirectionChange:
1725 case QEvent::ApplicationLayoutDirectionChange:
1728 case QEvent::StyleChange:
1730 if (!d->verticalScrollModeSet)
1731 resetVerticalScrollMode();
1732 if (!d->horizontalScrollModeSet)
1733 resetHorizontalScrollMode();
1735 case QEvent::FocusOut:
1736 d->checkPersistentEditorFocus();
1738 case QEvent::FontChange:
1739 d->doDelayedItemsLayout();
1744 return QAbstractScrollArea::event(event);
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758bool QAbstractItemView::viewportEvent(QEvent *event)
1760 Q_D(QAbstractItemView);
1761 switch (event->type()) {
1766 d->executePostedLayout();
1768 case QEvent::HoverMove:
1769 case QEvent::HoverEnter:
1770 d->setHoverIndex(indexAt(
static_cast<QHoverEvent*>(event)->position().toPoint()));
1772 case QEvent::HoverLeave:
1773 d->setHoverIndex(QModelIndex());
1776 d->viewportEnteredNeeded =
true;
1779 d->setHoverIndex(QModelIndex());
1780 #if QT_CONFIG(statustip)
1781 if (d->shouldClearStatusTip && d->parent) {
1783 QStatusTipEvent tip(empty);
1784 QCoreApplication::sendEvent(d->parent, &tip);
1785 d->shouldClearStatusTip =
false;
1788 d->enteredIndex = QModelIndex();
1790 case QEvent::ToolTip:
1791 case QEvent::QueryWhatsThis:
1792 case QEvent::WhatsThis: {
1793 QHelpEvent *he =
static_cast<QHelpEvent*>(event);
1794 const QModelIndex index = indexAt(he->pos());
1795 QStyleOptionViewItem option;
1796 initViewItemOption(&option);
1797 option.rect = visualRect(index);
1798 option.state |= (index == currentIndex() ? QStyle::State_HasFocus : QStyle::State_None);
1800 QAbstractItemDelegate *delegate = itemDelegateForIndex(index);
1803 return delegate->helpEvent(he,
this, option, index);
1805 case QEvent::FontChange:
1806 d->doDelayedItemsLayout();
1808 case QEvent::WindowActivate:
1809 case QEvent::WindowDeactivate:
1810 d->viewport->update();
1812 case QEvent::ScrollPrepare:
1813 executeDelayedItemsLayout();
1814#if QT_CONFIG(gestures) && QT_CONFIG(scroller)
1815 d->scollerConnection = QObjectPrivate::connect(
1816 QScroller::scroller(d->viewport), &QScroller::stateChanged,
1817 d, &QAbstractItemViewPrivate::scrollerStateChanged,
1818 Qt::UniqueConnection);
1825 return QAbstractScrollArea::viewportEvent(event);
1829
1830
1831
1832
1833void QAbstractItemView::mousePressEvent(QMouseEvent *event)
1835 Q_D(QAbstractItemView);
1836 d->releaseFromDoubleClick =
false;
1837 d->delayedAutoScroll.stop();
1838 QPoint pos = event->position().toPoint();
1839 QPersistentModelIndex index = indexAt(pos);
1842 d->pressClosedEditor = d->pressClosedEditorWatcher.isActive() && d->lastEditedIndex == index;
1844 if (!d->selectionModel || (d->state == EditingState && d->hasEditor(index)))
1847 d->pressedAlreadySelected = d->selectionModel->isSelected(index);
1848 d->pressedIndex = index;
1849 d->pressedModifiers = event->modifiers();
1850 QItemSelectionModel::SelectionFlags command = selectionCommand(index, event);
1851 d->noSelectionOnMousePress = command == QItemSelectionModel::NoUpdate || !index.isValid();
1852 QPoint offset = d->offset();
1853 d->draggedPosition = pos;
1854 d->draggedPositionOffset = offset;
1856#if QT_CONFIG(draganddrop)
1859 d->pressedPosition = d->draggedPosition + d->draggedPositionOffset;
1862 if (!(command & QItemSelectionModel::Current)) {
1863 d->pressedPosition = pos + offset;
1864 d->currentSelectionStartIndex = index;
1866 else if (!d->currentSelectionStartIndex.isValid())
1867 d->currentSelectionStartIndex = currentIndex();
1869 if (edit(index, NoEditTriggers, event))
1872 if (index.isValid() && d->isIndexEnabled(index)) {
1875 bool autoScroll = d->autoScroll;
1876 d->autoScroll =
false;
1877 d->selectionModel->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
1878 d->autoScroll = autoScroll;
1879 if (command.testFlag(QItemSelectionModel::Toggle)) {
1880 command &= ~QItemSelectionModel::Toggle;
1881 d->ctrlDragSelectionFlag = d->selectionModel->isSelected(index) ? QItemSelectionModel::Deselect : QItemSelectionModel::Select;
1882 command |= d->ctrlDragSelectionFlag;
1885 if (!(command & QItemSelectionModel::Current)) {
1886 setSelection(QRect(pos, QSize(1, 1)), command);
1888 QRect rect(visualRect(d->currentSelectionStartIndex).center(), pos);
1889 setSelection(rect, command);
1893 emit pressed(index);
1894 if (d->autoScroll) {
1897 d->delayedAutoScroll.start(QApplication::doubleClickInterval()+100,
this);
1902 d->selectionModel->select(QModelIndex(), QItemSelectionModel::Select);
1907
1908
1909
1910
1911void QAbstractItemView::mouseMoveEvent(QMouseEvent *event)
1913 Q_D(QAbstractItemView);
1914 QPoint bottomRight = event->position().toPoint();
1916 d->draggedPosition = bottomRight;
1917 d->draggedPositionOffset = d->offset();
1919 if (state() == ExpandingState || state() == CollapsingState)
1922#if QT_CONFIG(draganddrop)
1923 if (state() == DraggingState) {
1924 d->maybeStartDrag(bottomRight);
1929 QPersistentModelIndex index = indexAt(bottomRight);
1930 QModelIndex buddy = d->model->buddy(d->pressedIndex);
1931 if ((state() == EditingState && d->hasEditor(buddy))
1932 || edit(index, NoEditTriggers, event))
1935 const QPoint topLeft =
1936 d->selectionMode != SingleSelection ? d->pressedPosition - d->offset() : bottomRight;
1938 d->checkMouseMove(index);
1940#if QT_CONFIG(draganddrop)
1941 if (d->pressedIndex.isValid()
1943 && (state() != DragSelectingState)
1944 && (event->buttons() != Qt::NoButton)
1945 && !d->selectedDraggableIndexes().isEmpty()) {
1946 setState(DraggingState);
1947 d->maybeStartDrag(bottomRight);
1952 if ((event->buttons() & Qt::LeftButton) && d->selectionAllowed(index) && d->selectionModel) {
1953 setState(DragSelectingState);
1954 QItemSelectionModel::SelectionFlags command = selectionCommand(index, event);
1955 if (d->ctrlDragSelectionFlag != QItemSelectionModel::NoUpdate && command.testFlag(QItemSelectionModel::Toggle)) {
1956 command &= ~QItemSelectionModel::Toggle;
1957 command |= d->ctrlDragSelectionFlag;
1961 QRect selectionRect = QRect(topLeft, bottomRight);
1962 setSelection(selectionRect, command);
1965 if (index.isValid() && (index != d->selectionModel->currentIndex()) && d->isIndexEnabled(index))
1966 d->selectionModel->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
1967 else if (d->shouldAutoScroll(event->pos()) && !d->autoScrollTimer.isActive())
1973
1974
1975
1976
1977
1978
1979void QAbstractItemView::mouseReleaseEvent(QMouseEvent *event)
1981 Q_D(QAbstractItemView);
1982 const bool releaseFromDoubleClick = d->releaseFromDoubleClick;
1983 d->releaseFromDoubleClick =
false;
1985 QPoint pos = event->position().toPoint();
1986 QPersistentModelIndex index = indexAt(pos);
1988 if (state() == EditingState) {
1989 if (d->isIndexValid(index)
1990 && d->isIndexEnabled(index)
1991 && d->sendDelegateEvent(index, event))
1996 bool click = (index == d->pressedIndex && index.isValid() && !releaseFromDoubleClick);
1997 bool selectedClicked = click && d->pressedAlreadySelected
1998 && (event->button() == Qt::LeftButton)
1999 && (event->modifiers() == Qt::NoModifier);
2000 EditTrigger trigger = (selectedClicked ? SelectedClicked : NoEditTriggers);
2001 const bool edited = click && !d->pressClosedEditor ? edit(index, trigger, event) :
false;
2003 d->ctrlDragSelectionFlag = QItemSelectionModel::NoUpdate;
2005 if (d->selectionModel && d->noSelectionOnMousePress) {
2006 d->noSelectionOnMousePress =
false;
2007 if (!d->pressClosedEditor)
2008 d->selectionModel->select(index, selectionCommand(index, event));
2011 d->pressClosedEditor =
false;
2015 if (event->button() == Qt::LeftButton)
2016 emit clicked(index);
2019 QStyleOptionViewItem option;
2020 initViewItemOption(&option);
2021 if (d->pressedAlreadySelected)
2022 option.state |= QStyle::State_Selected;
2023 if ((d->model->flags(index) & Qt::ItemIsEnabled)
2024 && style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick, &option,
this))
2025 emit activated(index);
2030
2031
2032
2033
2034void QAbstractItemView::mouseDoubleClickEvent(QMouseEvent *event)
2036 Q_D(QAbstractItemView);
2038 QModelIndex index = indexAt(event->position().toPoint());
2039 if (!index.isValid()
2040 || !d->isIndexEnabled(index)
2041 || (d->pressedIndex != index)) {
2042 QMouseEvent me(QEvent::MouseButtonPress,
2043 event->position(), event->scenePosition(), event->globalPosition(),
2044 event->button(), event->buttons(), event->modifiers(),
2045 event->source(), event->pointingDevice());
2046 mousePressEvent(&me);
2050 QPersistentModelIndex persistent = index;
2051 emit doubleClicked(persistent);
2052 if ((event->button() == Qt::LeftButton) && !edit(persistent, DoubleClicked, event)
2053 && !style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick,
nullptr,
this))
2054 emit activated(persistent);
2055 d->releaseFromDoubleClick =
true;
2058#if QT_CONFIG(draganddrop)
2061
2062
2063
2064
2065
2066
2067void QAbstractItemView::dragEnterEvent(QDragEnterEvent *event)
2069 if (dragDropMode() == InternalMove
2070 && (event->source() !=
this|| !(event->possibleActions() & Qt::MoveAction)))
2073 if (d_func()->canDrop(event)) {
2075 setState(DraggingState);
2082
2083
2084
2085
2086
2087
2088
2089void QAbstractItemView::dragMoveEvent(QDragMoveEvent *event)
2091 Q_D(QAbstractItemView);
2092 d->draggedPosition = event->position().toPoint();
2093 d->draggedPositionOffset = d->offset();
2094 if (dragDropMode() == InternalMove
2095 && (event->source() !=
this || !(event->possibleActions() & Qt::MoveAction)))
2101 QModelIndex index = indexAt(event->position().toPoint());
2103 if (!d->droppingOnItself(event, index)
2104 && d->canDrop(event)) {
2106 if (index.isValid() && d->showDropIndicator) {
2107 QRect rect = visualRect(index);
2108 d->dropIndicatorPosition = d->position(event->position().toPoint(), rect, index);
2109 if (d->selectionBehavior == QAbstractItemView::SelectRows
2110 && d->dropIndicatorPosition != OnViewport
2111 && (d->dropIndicatorPosition != OnItem || event->source() ==
this)) {
2112 const int maxCol = d->model->columnCount(index.parent()) - 1;
2113 const auto idx = index.column() > 0 ? index.siblingAtColumn(0) : index;
2114 rect = d->intersectedRect(viewport()->rect(), idx, idx.siblingAtColumn(maxCol));
2116 switch (d->dropIndicatorPosition) {
2118 if (d->isIndexDropEnabled(index.parent())) {
2119 d->dropIndicatorRect = QRect(rect.left(), rect.top(), rect.width(), 0);
2120 event->acceptProposedAction();
2122 d->dropIndicatorRect = QRect();
2126 if (d->isIndexDropEnabled(index.parent())) {
2127 d->dropIndicatorRect = QRect(rect.left(), rect.bottom(), rect.width(), 0);
2128 event->acceptProposedAction();
2130 d->dropIndicatorRect = QRect();
2134 if (d->isIndexDropEnabled(index)) {
2135 d->dropIndicatorRect = rect;
2136 event->acceptProposedAction();
2138 d->dropIndicatorRect = QRect();
2142 d->dropIndicatorRect = QRect();
2143 if (d->isIndexDropEnabled(rootIndex())) {
2144 event->acceptProposedAction();
2149 d->dropIndicatorRect = QRect();
2150 d->dropIndicatorPosition = OnViewport;
2151 if (d->isIndexDropEnabled(rootIndex())) {
2152 event->acceptProposedAction();
2155 d->viewport->update();
2158 if (d->shouldAutoScroll(event->position().toPoint()))
2163
2164
2165
2166
2167bool QAbstractItemViewPrivate::droppingOnItself(QDropEvent *event,
const QModelIndex &index)
2169 Q_Q(QAbstractItemView);
2170 Qt::DropAction dropAction = event->dropAction();
2171 if (q->dragDropMode() == QAbstractItemView::InternalMove)
2172 dropAction = Qt::MoveAction;
2173 if (event->source() == q
2174 && event->possibleActions() & Qt::MoveAction
2175 && dropAction == Qt::MoveAction) {
2176 QModelIndexList selectedIndexes = q->selectedIndexes();
2177 QModelIndex child = index;
2178 while (child.isValid() && child != root) {
2179 if (selectedIndexes.contains(child))
2181 child = child.parent();
2188
2189
2190
2191
2192
2193void QAbstractItemView::dragLeaveEvent(QDragLeaveEvent *)
2195 Q_D(QAbstractItemView);
2198 d->hover = QModelIndex();
2199 d->viewport->update();
2203
2204
2205
2206
2207
2208
2209void QAbstractItemView::dropEvent(QDropEvent *event)
2211 Q_D(QAbstractItemView);
2212 if (dragDropMode() == InternalMove) {
2213 if (event->source() !=
this || !(event->possibleActions() & Qt::MoveAction))
2220 if (d->dropOn(event, &row, &col, &index)) {
2221 const Qt::DropAction action = dragDropMode() == InternalMove ? Qt::MoveAction : event->dropAction();
2222 if (d->model->dropMimeData(event->mimeData(), action, row, col, index)) {
2223 if (action != event->dropAction()) {
2224 event->setDropAction(action);
2227 event->acceptProposedAction();
2233 d->viewport->update();
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247bool QAbstractItemViewPrivate::dropOn(QDropEvent *event,
int *dropRow,
int *dropCol, QModelIndex *dropIndex)
2249 Q_Q(QAbstractItemView);
2250 if (event->isAccepted())
2255 if (viewport->rect().contains(event->position().toPoint())) {
2256 index = q->indexAt(event->position().toPoint());
2257 if (!index.isValid())
2262 if (model->supportedDropActions() & event->dropAction()) {
2265 if (index != root) {
2266 dropIndicatorPosition = position(event->position().toPoint(), q->visualRect(index), index);
2267 switch (dropIndicatorPosition) {
2268 case QAbstractItemView::AboveItem:
2270 col = index.column();
2271 index = index.parent();
2273 case QAbstractItemView::BelowItem:
2274 row = index.row() + 1;
2275 col = index.column();
2276 index = index.parent();
2278 case QAbstractItemView::OnItem:
2279 case QAbstractItemView::OnViewport:
2283 dropIndicatorPosition = QAbstractItemView::OnViewport;
2288 if (!droppingOnItself(event, index))
2294QAbstractItemView::DropIndicatorPosition
2295QAbstractItemViewPrivate::position(
const QPoint &pos,
const QRect &rect,
const QModelIndex &index)
const
2297 QAbstractItemView::DropIndicatorPosition r = QAbstractItemView::OnViewport;
2299 const int margin = qBound(2, qRound(qreal(rect.height()) / 5.5), 12);
2300 if (pos.y() - rect.top() < margin) {
2301 r = QAbstractItemView::AboveItem;
2302 }
else if (rect.bottom() - pos.y() < margin) {
2303 r = QAbstractItemView::BelowItem;
2304 }
else if (rect.contains(pos,
true)) {
2305 r = QAbstractItemView::OnItem;
2308 QRect touchingRect = rect;
2309 touchingRect.adjust(-1, -1, 1, 1);
2310 if (touchingRect.contains(pos,
false)) {
2311 r = QAbstractItemView::OnItem;
2315 if (r == QAbstractItemView::OnItem && (!(model->flags(index) & Qt::ItemIsDropEnabled)))
2316 r = pos.y() < rect.center().y() ? QAbstractItemView::AboveItem : QAbstractItemView::BelowItem;
2324
2325
2326
2327
2328
2329void QAbstractItemView::focusInEvent(QFocusEvent *event)
2331 Q_D(QAbstractItemView);
2332 QAbstractScrollArea::focusInEvent(event);
2334 const QItemSelectionModel* model = selectionModel();
2335 bool currentIndexValid = currentIndex().isValid();
2338 && !d->currentIndexSet
2339 && !currentIndexValid) {
2340 bool autoScroll = d->autoScroll;
2341 d->autoScroll =
false;
2342 QModelIndex index = moveCursor(MoveNext, Qt::NoModifier);
2343 if (index.isValid() && d->isIndexEnabled(index) && event->reason() != Qt::MouseFocusReason) {
2344 selectionModel()->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
2345 currentIndexValid =
true;
2347 d->autoScroll = autoScroll;
2350 if (model && currentIndexValid)
2351 setAttribute(Qt::WA_InputMethodEnabled, (currentIndex().flags() & Qt::ItemIsEditable));
2352 else if (!currentIndexValid)
2353 setAttribute(Qt::WA_InputMethodEnabled,
false);
2355 d->viewport->update();
2359
2360
2361
2362
2363
2364void QAbstractItemView::focusOutEvent(QFocusEvent *event)
2366 Q_D(QAbstractItemView);
2367 QAbstractScrollArea::focusOutEvent(event);
2368 d->viewport->update();
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382void QAbstractItemView::keyPressEvent(QKeyEvent *event)
2384 Q_D(QAbstractItemView);
2385 d->delayedAutoScroll.stop();
2387#ifdef QT_KEYPAD_NAVIGATION
2388 switch (event->key()) {
2389 case Qt::Key_Select:
2390 if (QApplicationPrivate::keypadNavigationEnabled()) {
2391 if (!hasEditFocus()) {
2398 if (QApplicationPrivate::keypadNavigationEnabled() && hasEditFocus()) {
2399 setEditFocus(
false);
2409 if (QApplicationPrivate::keypadNavigationEnabled() && !hasEditFocus()
2410 && QWidgetPrivate::canKeypadNavigate(Qt::Vertical)) {
2418 if (QApplicationPrivate::keypadNavigationEnabled() && !hasEditFocus()
2419 && (QWidgetPrivate::canKeypadNavigate(Qt::Horizontal) || QWidgetPrivate::inTabWidget(
this))) {
2425 if (QApplicationPrivate::keypadNavigationEnabled() && !hasEditFocus()) {
2432#if !defined(QT_NO_CLIPBOARD) && !defined(QT_NO_SHORTCUT)
2433 if (event == QKeySequence::Copy) {
2434 const QModelIndex index = currentIndex();
2435 if (index.isValid() && d->model) {
2436 const QVariant variant = d->model->data(index, Qt::DisplayRole);
2437 if (variant.canConvert<QString>())
2438 QGuiApplication::clipboard()->setText(variant.toString());
2444 QPersistentModelIndex newCurrent;
2445 d->moveCursorUpdatedView =
false;
2446 switch (event->key()) {
2448 newCurrent = moveCursor(MoveDown, event->modifiers());
2451 newCurrent = moveCursor(MoveUp, event->modifiers());
2454 newCurrent = moveCursor(MoveLeft, event->modifiers());
2457 newCurrent = moveCursor(MoveRight, event->modifiers());
2460 newCurrent = moveCursor(MoveHome, event->modifiers());
2463 newCurrent = moveCursor(MoveEnd, event->modifiers());
2465 case Qt::Key_PageUp:
2466 newCurrent = moveCursor(MovePageUp, event->modifiers());
2468 case Qt::Key_PageDown:
2469 newCurrent = moveCursor(MovePageDown, event->modifiers());
2472 if (d->tabKeyNavigation)
2473 newCurrent = moveCursor(MoveNext, event->modifiers());
2475 case Qt::Key_Backtab:
2476 if (d->tabKeyNavigation)
2477 newCurrent = moveCursor(MovePrevious, event->modifiers());
2481 QPersistentModelIndex oldCurrent = currentIndex();
2482 if (newCurrent != oldCurrent && newCurrent.isValid() && d->isIndexEnabled(newCurrent)) {
2483 if (!hasFocus() && QApplication::focusWidget() == indexWidget(oldCurrent))
2485 QItemSelectionModel::SelectionFlags command = selectionCommand(newCurrent, event);
2486 if (command != QItemSelectionModel::NoUpdate
2487 || style()->styleHint(QStyle::SH_ItemView_MovementWithoutUpdatingSelection,
nullptr,
this)) {
2489 if (command & QItemSelectionModel::Current) {
2490 d->selectionModel->setCurrentIndex(newCurrent, QItemSelectionModel::NoUpdate);
2491 if (!d->currentSelectionStartIndex.isValid())
2492 d->currentSelectionStartIndex = oldCurrent;
2493 QRect rect(visualRect(d->currentSelectionStartIndex).center(), visualRect(newCurrent).center());
2494 setSelection(rect, command);
2496 d->selectionModel->setCurrentIndex(newCurrent, command);
2497 d->currentSelectionStartIndex = newCurrent;
2498 if (newCurrent.isValid()) {
2500 QRect rect(visualRect(newCurrent).center(), QSize(1, 1));
2501 setSelection(rect, command);
2509 switch (event->key()) {
2513#ifdef QT_KEYPAD_NAVIGATION
2514 if (QApplicationPrivate::keypadNavigationEnabled()
2515 && QWidgetPrivate::canKeypadNavigate(Qt::Vertical)) {
2522#ifdef QT_KEYPAD_NAVIGATION
2523 if (QApplication::navigationMode() == Qt::NavigationModeKeypadDirectional
2524 && (QWidgetPrivate::canKeypadNavigate(Qt::Horizontal)
2525 || (QWidgetPrivate::inTabWidget(
this) && d->model->columnCount(d->root) > 1))) {
2532 case Qt::Key_PageUp:
2533 case Qt::Key_PageDown:
2534 case Qt::Key_Escape:
2536 case Qt::Key_Control:
2537 case Qt::Key_Delete:
2538 case Qt::Key_Backspace:
2542 case Qt::Key_Select:
2543 if (!edit(currentIndex(), AnyKeyPressed, event)) {
2544 if (d->selectionModel)
2545 d->selectionModel->select(currentIndex(), selectionCommand(currentIndex(), event));
2546 if (event->key() == Qt::Key_Space) {
2547 keyboardSearch(event->text());
2551#ifdef QT_KEYPAD_NAVIGATION
2552 if ( event->key()==Qt::Key_Select ) {
2554 if (currentIndex().isValid()) {
2555 if (state() != EditingState)
2556 emit activated(currentIndex());
2565 case Qt::Key_Return:
2568 if (!edit(currentIndex(), EditKeyPressed, event) && d->editorIndexHash.isEmpty())
2573 if (!edit(currentIndex(), EditKeyPressed, event))
2577 case Qt::Key_Return:
2581 if (state() != EditingState || hasFocus()) {
2582 if (currentIndex().isValid())
2583 emit activated(currentIndex());
2589#ifndef QT_NO_SHORTCUT
2590 if (event == QKeySequence::SelectAll && selectionMode() != NoSelection) {
2596 if (event->key() == Qt::Key_O && event->modifiers() & Qt::ControlModifier && currentIndex().isValid()) {
2597 emit activated(currentIndex());
2601 bool modified = (event->modifiers() & (Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier));
2602 if (!event->text().isEmpty() && !modified && !edit(currentIndex(), AnyKeyPressed, event)) {
2603 keyboardSearch(event->text());
2610 if (d->moveCursorUpdatedView)
2615
2616
2617
2618
2619
2620void QAbstractItemView::resizeEvent(QResizeEvent *event)
2622 QAbstractScrollArea::resizeEvent(event);
2627
2628
2629
2630
2631
2632void QAbstractItemView::timerEvent(QTimerEvent *event)
2634 Q_D(QAbstractItemView);
2635 if (event->timerId() == d->fetchMoreTimer.timerId())
2637 else if (event->timerId() == d->delayedReset.timerId())
2639 else if (event->timerId() == d->autoScrollTimer.timerId())
2641 else if (event->timerId() == d->updateTimer.timerId())
2642 d->updateDirtyRegion();
2643 else if (event->timerId() == d->delayedEditing.timerId()) {
2644 d->delayedEditing.stop();
2645 edit(currentIndex());
2646 }
else if (event->timerId() == d->delayedLayout.timerId()) {
2647 d->delayedLayout.stop();
2649 d->interruptDelayedItemsLayout();
2651 const QModelIndex current = currentIndex();
2652 if (current.isValid() && d->state == QAbstractItemView::EditingState)
2655 }
else if (event->timerId() == d->delayedAutoScroll.timerId()) {
2656 d->delayedAutoScroll.stop();
2659 if (d->pressedIndex.isValid() && d->pressedIndex == currentIndex())
2660 scrollTo(d->pressedIndex);
2661 }
else if (event->timerId() == d->pressClosedEditorWatcher.timerId()) {
2662 d->pressClosedEditorWatcher.stop();
2667
2668
2669void QAbstractItemView::inputMethodEvent(QInputMethodEvent *event)
2671 Q_D(QAbstractItemView);
2678 bool forwardEventToEditor =
false;
2679 const bool commit = !event->commitString().isEmpty();
2680 const bool preediting = !event->preeditString().isEmpty();
2681 if (QWidget *currentEditor = d->editorForIndex(currentIndex()).widget) {
2682 if (d->waitForIMCommit) {
2683 if (commit || !preediting) {
2685 d->waitForIMCommit =
false;
2686 QApplication::sendEvent(currentEditor, event);
2688 QAbstractItemDelegate *delegate = itemDelegateForIndex(currentIndex());
2690 delegate->setEditorData(currentEditor, currentIndex());
2691 d->selectAllInEditor(currentEditor);
2693 if (currentEditor->focusPolicy() != Qt::NoFocus)
2694 currentEditor->setFocus();
2697 QApplication::sendEvent(currentEditor, event);
2701 }
else if (preediting) {
2703 d->waitForIMCommit =
true;
2705 forwardEventToEditor =
true;
2706 }
else if (!commit) {
2710 if (!edit(currentIndex(), AnyKeyPressed, event)) {
2711 d->waitForIMCommit =
false;
2713 keyboardSearch(event->commitString());
2715 }
else if (QWidget *currentEditor; forwardEventToEditor
2716 && (currentEditor = d->editorForIndex(currentIndex()).widget)) {
2717 QApplication::sendEvent(currentEditor, event);
2721#if QT_CONFIG(draganddrop)
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2741
2742
2743QAbstractItemView::DropIndicatorPosition QAbstractItemView::dropIndicatorPosition()
const
2745 Q_D(
const QAbstractItemView);
2746 return d->dropIndicatorPosition;
2751
2752
2753
2754
2755
2756
2757QModelIndexList QAbstractItemView::selectedIndexes()
const
2759 Q_D(
const QAbstractItemView);
2760 QModelIndexList indexes;
2761 if (d->selectionModel) {
2762 indexes = d->selectionModel->selectedIndexes();
2763 auto isHidden = [
this](
const QModelIndex &idx) {
2764 return isIndexHidden(idx);
2766 indexes.removeIf(isHidden);
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784bool QAbstractItemView::edit(
const QModelIndex &index, EditTrigger trigger, QEvent *event)
2786 Q_D(QAbstractItemView);
2788 if (!d->isIndexValid(index))
2791 if (QWidget *w = (d->persistent.isEmpty() ?
static_cast<QWidget*>(
nullptr) : d->editorForIndex(index).widget.data())) {
2792 if (w->focusPolicy() == Qt::NoFocus)
2794 if (!d->waitForIMCommit)
2801 if (trigger == DoubleClicked) {
2802 d->delayedEditing.stop();
2803 d->delayedAutoScroll.stop();
2804 }
else if (trigger == CurrentChanged) {
2805 d->delayedEditing.stop();
2809 QPersistentModelIndex safeIndex(index);
2811 if (d->sendDelegateEvent(index, event)) {
2816 if (!safeIndex.isValid()) {
2821 EditTriggers lastTrigger = d->lastTrigger;
2822 d->lastTrigger = trigger;
2824 if (!d->shouldEdit(trigger, d->model->buddy(safeIndex)))
2827 if (d->delayedEditing.isActive())
2832 if (lastTrigger == DoubleClicked && trigger == SelectedClicked)
2836 if (trigger == SelectedClicked)
2837 d->delayedEditing.start(QApplication::doubleClickInterval(),
this);
2839 d->openEditor(safeIndex, d->shouldForwardEvent(trigger, event) ? event :
nullptr);
2845
2846
2847
2848void QAbstractItemView::updateEditorData()
2850 Q_D(QAbstractItemView);
2851 d->updateEditorData(QModelIndex(), QModelIndex());
2855
2856
2857
2858void QAbstractItemView::updateEditorGeometries()
2860 Q_D(QAbstractItemView);
2861 if (d->editorIndexHash.isEmpty())
2863 if (d->delayedPendingLayout) {
2865 d->executePostedLayout();
2868 QStyleOptionViewItem option;
2869 initViewItemOption(&option);
2870 QEditorIndexHash::iterator it = d->editorIndexHash.begin();
2871 QWidgetList editorsToRelease;
2872 QWidgetList editorsToHide;
2873 while (it != d->editorIndexHash.end()) {
2874 QModelIndex index = it.value();
2875 QWidget *editor = it.key();
2876 if (index.isValid() && editor) {
2877 option.rect = visualRect(index);
2878 if (option.rect.isValid()) {
2880 QAbstractItemDelegate *delegate = itemDelegateForIndex(index);
2882 delegate->updateEditorGeometry(editor, option, index);
2884 editorsToHide << editor;
2888 d->indexEditorHash.remove(it.value());
2889 it = d->editorIndexHash.erase(it);
2890 editorsToRelease << editor;
2896 for (
int i = 0; i < editorsToHide.size(); ++i) {
2897 editorsToHide.at(i)->hide();
2899 for (
int i = 0; i < editorsToRelease.size(); ++i) {
2900 d->releaseEditor(editorsToRelease.at(i));
2905
2906
2907void QAbstractItemView::updateGeometries()
2909 Q_D(QAbstractItemView);
2910 updateEditorGeometries();
2911 d->fetchMoreTimer.start(0,
this);
2912 d->updateGeometry();
2916
2917
2918void QAbstractItemView::verticalScrollbarValueChanged(
int value)
2920 Q_D(QAbstractItemView);
2921 if (verticalScrollBar()->maximum() == value && d->model->canFetchMore(d->root))
2922 d->model->fetchMore(d->root);
2923 QPoint posInVp = viewport()->mapFromGlobal(QCursor::pos());
2924 if (viewport()->rect().contains(posInVp))
2925 d->checkMouseMove(posInVp);
2929
2930
2931void QAbstractItemView::horizontalScrollbarValueChanged(
int value)
2933 Q_D(QAbstractItemView);
2934 if (horizontalScrollBar()->maximum() == value && d->model->canFetchMore(d->root))
2935 d->model->fetchMore(d->root);
2936 QPoint posInVp = viewport()->mapFromGlobal(QCursor::pos());
2937 if (viewport()->rect().contains(posInVp))
2938 d->checkMouseMove(posInVp);
2942
2943
2944void QAbstractItemView::verticalScrollbarAction(
int)
2950
2951
2952void QAbstractItemView::horizontalScrollbarAction(
int)
2958
2959
2960
2961
2962
2963
2964
2966void QAbstractItemView::closeEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint)
2968 Q_D(QAbstractItemView);
2972 const bool isPersistent = d->persistent.contains(editor);
2973 const QModelIndex index = d->indexForEditor(editor);
2974 if (!index.isValid()) {
2975 if (!editor->isVisible()) {
2983 qWarning(
"QAbstractItemView::closeEditor called with an editor that does not belong to this view");
2987 const bool hadFocus = editor->hasFocus();
2991 d->pressClosedEditorWatcher.start(0,
this);
2992 d->lastEditedIndex = index;
2994 if (!isPersistent) {
2996 QModelIndex index = d->indexForEditor(editor);
2997 editor->removeEventFilter(itemDelegateForIndex(index));
2998 d->removeEditor(editor);
3001 if (focusPolicy() != Qt::NoFocus)
3004 editor->clearFocus();
3006 d->checkPersistentEditorFocus();
3009 QPointer<QWidget> ed = editor;
3010 QCoreApplication::sendPostedEvents(editor, 0);
3013 if (!isPersistent && editor)
3014 d->releaseEditor(editor, index);
3019 QItemSelectionModel::SelectionFlags flags = QItemSelectionModel::NoUpdate;
3020 if (d->selectionMode != NoSelection)
3021 flags = QItemSelectionModel::ClearAndSelect | d->selectionBehaviorFlags();
3023 case QAbstractItemDelegate::EditNextItem: {
3024 QModelIndex index = moveCursor(MoveNext, Qt::NoModifier);
3025 if (index.isValid()) {
3026 QPersistentModelIndex persistent(index);
3027 d->selectionModel->setCurrentIndex(persistent, flags);
3029 if (index.flags() & Qt::ItemIsEditable
3030 && (!(editTriggers() & QAbstractItemView::CurrentChanged)))
3033 case QAbstractItemDelegate::EditPreviousItem: {
3034 QModelIndex index = moveCursor(MovePrevious, Qt::NoModifier);
3035 if (index.isValid()) {
3036 QPersistentModelIndex persistent(index);
3037 d->selectionModel->setCurrentIndex(persistent, flags);
3039 if (index.flags() & Qt::ItemIsEditable
3040 && (!(editTriggers() & QAbstractItemView::CurrentChanged)))
3043 case QAbstractItemDelegate::SubmitModelCache:
3046 case QAbstractItemDelegate::RevertModelCache:
3055
3056
3057
3058
3059void QAbstractItemView::commitData(QWidget *editor)
3061 Q_D(QAbstractItemView);
3062 if (!editor || !d->itemDelegate || d->currentlyCommittingEditor)
3064 QModelIndex index = d->indexForEditor(editor);
3065 if (!index.isValid()) {
3066 qWarning(
"QAbstractItemView::commitData called with an editor that does not belong to this view");
3069 d->currentlyCommittingEditor = editor;
3070 QAbstractItemDelegate *delegate = itemDelegateForIndex(index);
3071 editor->removeEventFilter(delegate);
3072 delegate->setModelData(editor, d->model, index);
3073 editor->installEventFilter(delegate);
3074 d->currentlyCommittingEditor =
nullptr;
3078
3079
3080
3081
3082void QAbstractItemView::editorDestroyed(QObject *editor)
3084 Q_D(QAbstractItemView);
3085 QWidget *w = qobject_cast<QWidget*>(editor);
3087 d->persistent.remove(w);
3088 if (state() == EditingState)
3095
3096
3097
3098
3099
3100
3101
3102void QAbstractItemView::keyboardSearch(
const QString &search)
3104 Q_D(QAbstractItemView);
3105 if (!d->model->rowCount(d->root) || !d->model->columnCount(d->root))
3108 QModelIndex start = currentIndex().isValid() ? currentIndex()
3109 : d->model->index(0, 0, d->root);
3110 bool skipRow =
false;
3111 bool keyboardTimeWasValid = d->keyboardInputTime.isValid();
3112 qint64 keyboardInputTimeElapsed;
3113 if (keyboardTimeWasValid)
3114 keyboardInputTimeElapsed = d->keyboardInputTime.restart();
3116 d->keyboardInputTime.start();
3117 if (search.isEmpty() || !keyboardTimeWasValid
3118 || keyboardInputTimeElapsed > QApplication::keyboardInputInterval()) {
3119 d->keyboardInput = search;
3120 skipRow = currentIndex().isValid();
3122 d->keyboardInput += search;
3126 bool sameKey =
false;
3127 if (d->keyboardInput.size() > 1) {
3128 int c = d->keyboardInput.count(d->keyboardInput.at(d->keyboardInput.size() - 1));
3129 sameKey = (c == d->keyboardInput.size());
3136 QModelIndex parent = start.parent();
3137 int newRow = (start.row() < d->model->rowCount(parent) - 1) ? start.row() + 1 : 0;
3138 start = d->model->index(newRow, start.column(), parent);
3142 QModelIndex current = start;
3143 QModelIndexList match;
3144 QModelIndex firstMatch;
3145 QModelIndex startMatch;
3146 QModelIndexList previous;
3148 match = d->model->match(current, Qt::DisplayRole, d->keyboardInput, 1,
3149 d->keyboardSearchFlags);
3150 if (match == previous)
3152 firstMatch = match.value(0);
3154 if (firstMatch.isValid()) {
3155 if (d->isIndexEnabled(firstMatch)) {
3156 setCurrentIndex(firstMatch);
3159 int row = firstMatch.row() + 1;
3160 if (row >= d->model->rowCount(firstMatch.parent()))
3162 current = firstMatch.sibling(row, firstMatch.column());
3165 if (!startMatch.isValid())
3166 startMatch = firstMatch;
3167 else if (startMatch == firstMatch)
3170 }
while (current != start && firstMatch.isValid());
3174
3175
3176
3177
3178
3179QSize QAbstractItemView::sizeHintForIndex(
const QModelIndex &index)
const
3181 Q_D(
const QAbstractItemView);
3182 if (!d->isIndexValid(index))
3184 const auto delegate = itemDelegateForIndex(index);
3185 QStyleOptionViewItem option;
3186 initViewItemOption(&option);
3187 return delegate ? delegate->sizeHint(option, index) : QSize();
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206int QAbstractItemView::sizeHintForRow(
int row)
const
3208 Q_D(
const QAbstractItemView);
3210 if (row < 0 || row >= d->model->rowCount(d->root))
3215 QStyleOptionViewItem option;
3216 initViewItemOption(&option);
3218 int colCount = d->model->columnCount(d->root);
3219 for (
int c = 0; c < colCount; ++c) {
3220 const QModelIndex index = d->model->index(row, c, d->root);
3221 if (QWidget *editor = d->editorForIndex(index).widget.data())
3222 height = qMax(height, editor->height());
3223 if (
const QAbstractItemDelegate *delegate = itemDelegateForIndex(index))
3224 height = qMax(height, delegate->sizeHint(option, index).height());
3230
3231
3232
3233
3234
3235
3236
3237int QAbstractItemView::sizeHintForColumn(
int column)
const
3239 Q_D(
const QAbstractItemView);
3241 if (column < 0 || column >= d->model->columnCount(d->root))
3246 QStyleOptionViewItem option;
3247 initViewItemOption(&option);
3249 int rows = d->model->rowCount(d->root);
3250 for (
int r = 0; r < rows; ++r) {
3251 const QModelIndex index = d->model->index(r, column, d->root);
3252 if (QWidget *editor = d->editorForIndex(index).widget.data())
3253 width = qMax(width, editor->sizeHint().width());
3254 if (
const QAbstractItemDelegate *delegate = itemDelegateForIndex(index))
3255 width = qMax(width, delegate->sizeHint(option, index).width());
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277int QAbstractItemView::updateThreshold()
const
3279 Q_D(
const QAbstractItemView);
3280 return d->updateThreshold;
3283void QAbstractItemView::setUpdateThreshold(
int threshold)
3285 Q_D(QAbstractItemView);
3286 if (d->updateThreshold == threshold)
3288 d->updateThreshold = threshold;
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3303Qt::MatchFlags QAbstractItemView::keyboardSearchFlags()
const
3305 Q_D(
const QAbstractItemView);
3306 return d->keyboardSearchFlags;
3309void QAbstractItemView::setKeyboardSearchFlags(Qt::MatchFlags searchFlags)
3311 Q_D(QAbstractItemView);
3312 d->keyboardSearchFlags = searchFlags;
3316
3317
3318
3319
3320
3321void QAbstractItemView::openPersistentEditor(
const QModelIndex &index)
3323 Q_D(QAbstractItemView);
3324 QStyleOptionViewItem options;
3325 initViewItemOption(&options);
3326 options.rect = visualRect(index);
3327 options.state |= (index == currentIndex() ? QStyle::State_HasFocus : QStyle::State_None);
3329 QWidget *editor = d->editor(index, options);
3332 d->persistent.insert(editor);
3337
3338
3339
3340
3341void QAbstractItemView::closePersistentEditor(
const QModelIndex &index)
3343 Q_D(QAbstractItemView);
3344 if (QWidget *editor = d->editorForIndex(index).widget.data()) {
3345 if (index == selectionModel()->currentIndex())
3346 closeEditor(editor, QAbstractItemDelegate::RevertModelCache);
3347 d->persistent.remove(editor);
3348 d->removeEditor(editor);
3349 d->releaseEditor(editor, index);
3354
3355
3356
3357
3358
3359
3360bool QAbstractItemView::isPersistentEditorOpen(
const QModelIndex &index)
const
3362 Q_D(
const QAbstractItemView);
3363 QWidget *editor = d->editorForIndex(index).widget;
3364 return editor && d->persistent.contains(editor);
4132void QAbstractItemView::doAutoScroll()
4135 Q_D(QAbstractItemView);
4136 QScrollBar *verticalScroll = verticalScrollBar();
4137 QScrollBar *horizontalScroll = horizontalScrollBar();
4141 QHeaderView *hv = qobject_cast<QHeaderView*>(
this);
4143 QAbstractScrollArea *parent = qobject_cast<QAbstractScrollArea*>(parentWidget());
4145 if (hv->orientation() == Qt::Horizontal) {
4146 if (!hv->horizontalScrollBar() || !hv->horizontalScrollBar()->isVisible())
4147 horizontalScroll = parent->horizontalScrollBar();
4149 if (!hv->verticalScrollBar() || !hv->verticalScrollBar()->isVisible())
4150 verticalScroll = parent->verticalScrollBar();
4155 const int verticalStep = verticalScroll->pageStep();
4156 const int horizontalStep = horizontalScroll->pageStep();
4157 if (d->autoScrollCount < qMax(verticalStep, horizontalStep))
4158 ++d->autoScrollCount;
4160 const int margin = d->autoScrollMargin;
4161 const int verticalValue = verticalScroll->value();
4162 const int horizontalValue = horizontalScroll->value();
4164 const QPoint pos = d->draggedPosition;
4166 const QRect area = QWidgetPrivate::get(d->viewport)->clipRect();
4169 if (pos.y() - area.top() < margin)
4170 verticalScroll->setValue(verticalValue - d->autoScrollCount);
4171 else if (area.bottom() - pos.y() < margin)
4172 verticalScroll->setValue(verticalValue + d->autoScrollCount);
4173 if (pos.x() - area.left() < margin)
4174 horizontalScroll->setValue(horizontalValue - d->autoScrollCount);
4175 else if (area.right() - pos.x() < margin)
4176 horizontalScroll->setValue(horizontalValue + d->autoScrollCount);
4178 const bool verticalUnchanged = (verticalValue == verticalScroll->value());
4179 const bool horizontalUnchanged = (horizontalValue == horizontalScroll->value());
4180 if (verticalUnchanged && horizontalUnchanged) {
4183#if QT_CONFIG(draganddrop)
4184 d->dropIndicatorRect = QRect();
4185 d->dropIndicatorPosition = QAbstractItemView::OnViewport;
4188 case QAbstractItemView::DragSelectingState: {
4191 const QPoint globalPos = d->viewport->mapToGlobal(pos);
4192 const QPoint windowPos = window()->mapFromGlobal(globalPos);
4193 QMouseEvent mm(QEvent::MouseMove, pos, windowPos, globalPos,
4194 Qt::NoButton, Qt::LeftButton, d->pressedModifiers,
4195 Qt::MouseEventSynthesizedByQt);
4196 QApplication::sendEvent(viewport(), &mm);
4199 case QAbstractItemView::DraggingState: {
4205 d->draggedPosition = pos;
4206 d->draggedPositionOffset = d->offset();
4212 d->viewport->update();
4316QItemSelectionModel::SelectionFlags QAbstractItemViewPrivate::extendedSelectionCommand(
4317 const QModelIndex &index,
const QEvent *event)
const
4319 Qt::KeyboardModifiers modifiers = event && event->isInputEvent()
4320 ?
static_cast<
const QInputEvent*>(event)->modifiers()
4321 : QGuiApplication::keyboardModifiers();
4323 switch (event->type()) {
4324 case QEvent::MouseMove: {
4326 if (modifiers & Qt::ControlModifier)
4327 return QItemSelectionModel::ToggleCurrent|selectionBehaviorFlags();
4330 case QEvent::MouseButtonPress: {
4331 const Qt::MouseButton button =
static_cast<
const QMouseEvent*>(event)->button();
4332 const bool rightButtonPressed = button & Qt::RightButton;
4333 const bool shiftKeyPressed = modifiers & Qt::ShiftModifier;
4334 const bool controlKeyPressed = modifiers & Qt::ControlModifier;
4335 const bool indexIsSelected = selectionModel->isSelected(index);
4336 if ((shiftKeyPressed || controlKeyPressed) && rightButtonPressed)
4337 return QItemSelectionModel::NoUpdate;
4338 if (!shiftKeyPressed && !controlKeyPressed && indexIsSelected)
4339 return QItemSelectionModel::NoUpdate;
4340 if (!index.isValid() && !rightButtonPressed && !shiftKeyPressed && !controlKeyPressed)
4341 return QItemSelectionModel::Clear;
4342 if (!index.isValid())
4343 return QItemSelectionModel::NoUpdate;
4345 if (controlKeyPressed && !rightButtonPressed && pressedAlreadySelected
4346#if QT_CONFIG(draganddrop)
4347 && dragEnabled && isIndexDragEnabled(index)
4350 return QItemSelectionModel::NoUpdate;
4354 case QEvent::MouseButtonRelease: {
4356 const Qt::MouseButton button =
static_cast<
const QMouseEvent*>(event)->button();
4357 const bool rightButtonPressed = button & Qt::RightButton;
4358 const bool shiftKeyPressed = modifiers & Qt::ShiftModifier;
4359 const bool controlKeyPressed = modifiers & Qt::ControlModifier;
4360 if (((index == pressedIndex && selectionModel->isSelected(index))
4361 || !index.isValid()) && state != QAbstractItemView::DragSelectingState
4362 && !shiftKeyPressed && !controlKeyPressed && (!rightButtonPressed || !index.isValid()))
4363 return QItemSelectionModel::ClearAndSelect|selectionBehaviorFlags();
4364 if (index == pressedIndex && controlKeyPressed && !rightButtonPressed
4365#if QT_CONFIG(draganddrop)
4366 && dragEnabled && isIndexDragEnabled(index)
4371 return QItemSelectionModel::NoUpdate;
4373 case QEvent::KeyPress: {
4375 switch (
static_cast<
const QKeyEvent*>(event)->key()) {
4376 case Qt::Key_Backtab:
4377 modifiers = modifiers & ~Qt::ShiftModifier;
4385 case Qt::Key_PageUp:
4386 case Qt::Key_PageDown:
4388 if (modifiers & Qt::ControlModifier
4389#ifdef QT_KEYPAD_NAVIGATION
4391 || QApplication::navigationMode() == Qt::NavigationModeKeypadTabOrder
4394 return QItemSelectionModel::NoUpdate;
4396 case Qt::Key_Select:
4397 return QItemSelectionModel::Toggle|selectionBehaviorFlags();
4399 if (modifiers & Qt::ControlModifier)
4400 return QItemSelectionModel::Toggle|selectionBehaviorFlags();
4401 return QItemSelectionModel::Select|selectionBehaviorFlags();
4412 if (modifiers & Qt::ShiftModifier)
4413 return QItemSelectionModel::SelectCurrent|selectionBehaviorFlags();
4414 if (modifiers & Qt::ControlModifier)
4415 return QItemSelectionModel::Toggle|selectionBehaviorFlags();
4416 if (state == QAbstractItemView::DragSelectingState) {
4418 return QItemSelectionModel::Clear|QItemSelectionModel::SelectCurrent|selectionBehaviorFlags();
4421 return QItemSelectionModel::ClearAndSelect|selectionBehaviorFlags();