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 (QMetaObject::Connection &connection : modelConnections)
291 QObject::disconnect(connection);
292 for (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 (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 + offset;
1855#if QT_CONFIG(draganddrop)
1858 d->pressedPosition = d->draggedPosition;
1861 if (!(command & QItemSelectionModel::Current)) {
1862 d->pressedPosition = pos + offset;
1863 d->currentSelectionStartIndex = index;
1865 else if (!d->currentSelectionStartIndex.isValid())
1866 d->currentSelectionStartIndex = currentIndex();
1868 if (edit(index, NoEditTriggers, event))
1871 if (index.isValid() && d->isIndexEnabled(index)) {
1874 bool autoScroll = d->autoScroll;
1875 d->autoScroll =
false;
1876 d->selectionModel->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
1877 d->autoScroll = autoScroll;
1878 if (command.testFlag(QItemSelectionModel::Toggle)) {
1879 command &= ~QItemSelectionModel::Toggle;
1880 d->ctrlDragSelectionFlag = d->selectionModel->isSelected(index) ? QItemSelectionModel::Deselect : QItemSelectionModel::Select;
1881 command |= d->ctrlDragSelectionFlag;
1884 if (!(command & QItemSelectionModel::Current)) {
1885 setSelection(QRect(pos, QSize(1, 1)), command);
1887 QRect rect(visualRect(d->currentSelectionStartIndex).center(), pos);
1888 setSelection(rect, command);
1892 emit pressed(index);
1893 if (d->autoScroll) {
1896 d->delayedAutoScroll.start(QApplication::doubleClickInterval()+100,
this);
1901 d->selectionModel->select(QModelIndex(), QItemSelectionModel::Select);
1906
1907
1908
1909
1910void QAbstractItemView::mouseMoveEvent(QMouseEvent *event)
1912 Q_D(QAbstractItemView);
1913 QPoint bottomRight = event->position().toPoint();
1915 d->draggedPosition = bottomRight + d->offset();
1917 if (state() == ExpandingState || state() == CollapsingState)
1920#if QT_CONFIG(draganddrop)
1921 if (state() == DraggingState) {
1922 d->maybeStartDrag(bottomRight);
1927 QPersistentModelIndex index = indexAt(bottomRight);
1928 QModelIndex buddy = d->model->buddy(d->pressedIndex);
1929 if ((state() == EditingState && d->hasEditor(buddy))
1930 || edit(index, NoEditTriggers, event))
1933 const QPoint topLeft =
1934 d->selectionMode != SingleSelection ? d->pressedPosition - d->offset() : bottomRight;
1936 d->checkMouseMove(index);
1938#if QT_CONFIG(draganddrop)
1939 if (d->pressedIndex.isValid()
1941 && (state() != DragSelectingState)
1942 && (event->buttons() != Qt::NoButton)
1943 && !d->selectedDraggableIndexes().isEmpty()) {
1944 setState(DraggingState);
1945 d->maybeStartDrag(bottomRight);
1950 if ((event->buttons() & Qt::LeftButton) && d->selectionAllowed(index) && d->selectionModel) {
1951 setState(DragSelectingState);
1952 QItemSelectionModel::SelectionFlags command = selectionCommand(index, event);
1953 if (d->ctrlDragSelectionFlag != QItemSelectionModel::NoUpdate && command.testFlag(QItemSelectionModel::Toggle)) {
1954 command &= ~QItemSelectionModel::Toggle;
1955 command |= d->ctrlDragSelectionFlag;
1959 QRect selectionRect = QRect(topLeft, bottomRight);
1960 setSelection(selectionRect, command);
1963 if (index.isValid() && (index != d->selectionModel->currentIndex()) && d->isIndexEnabled(index))
1964 d->selectionModel->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
1965 else if (d->shouldAutoScroll(event->pos()) && !d->autoScrollTimer.isActive())
1971
1972
1973
1974
1975
1976
1977void QAbstractItemView::mouseReleaseEvent(QMouseEvent *event)
1979 Q_D(QAbstractItemView);
1980 const bool releaseFromDoubleClick = d->releaseFromDoubleClick;
1981 d->releaseFromDoubleClick =
false;
1983 QPoint pos = event->position().toPoint();
1984 QPersistentModelIndex index = indexAt(pos);
1986 if (state() == EditingState) {
1987 if (d->isIndexValid(index)
1988 && d->isIndexEnabled(index)
1989 && d->sendDelegateEvent(index, event))
1994 bool click = (index == d->pressedIndex && index.isValid() && !releaseFromDoubleClick);
1995 bool selectedClicked = click && d->pressedAlreadySelected
1996 && (event->button() == Qt::LeftButton)
1997 && (event->modifiers() == Qt::NoModifier);
1998 EditTrigger trigger = (selectedClicked ? SelectedClicked : NoEditTriggers);
1999 const bool edited = click && !d->pressClosedEditor ? edit(index, trigger, event) :
false;
2001 d->ctrlDragSelectionFlag = QItemSelectionModel::NoUpdate;
2003 if (d->selectionModel && d->noSelectionOnMousePress) {
2004 d->noSelectionOnMousePress =
false;
2005 if (!d->pressClosedEditor)
2006 d->selectionModel->select(index, selectionCommand(index, event));
2009 d->pressClosedEditor =
false;
2013 if (event->button() == Qt::LeftButton)
2014 emit clicked(index);
2017 QStyleOptionViewItem option;
2018 initViewItemOption(&option);
2019 if (d->pressedAlreadySelected)
2020 option.state |= QStyle::State_Selected;
2021 if ((d->model->flags(index) & Qt::ItemIsEnabled)
2022 && style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick, &option,
this))
2023 emit activated(index);
2028
2029
2030
2031
2032void QAbstractItemView::mouseDoubleClickEvent(QMouseEvent *event)
2034 Q_D(QAbstractItemView);
2036 QModelIndex index = indexAt(event->position().toPoint());
2037 if (!index.isValid()
2038 || !d->isIndexEnabled(index)
2039 || (d->pressedIndex != index)) {
2040 QMouseEvent me(QEvent::MouseButtonPress,
2041 event->position(), event->scenePosition(), event->globalPosition(),
2042 event->button(), event->buttons(), event->modifiers(),
2043 event->source(), event->pointingDevice());
2044 mousePressEvent(&me);
2048 QPersistentModelIndex persistent = index;
2049 emit doubleClicked(persistent);
2050 if ((event->button() == Qt::LeftButton) && !edit(persistent, DoubleClicked, event)
2051 && !style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick,
nullptr,
this))
2052 emit activated(persistent);
2053 d->releaseFromDoubleClick =
true;
2056#if QT_CONFIG(draganddrop)
2059
2060
2061
2062
2063
2064
2065void QAbstractItemView::dragEnterEvent(QDragEnterEvent *event)
2067 if (dragDropMode() == InternalMove
2068 && (event->source() !=
this|| !(event->possibleActions() & Qt::MoveAction)))
2071 if (d_func()->canDrop(event)) {
2073 setState(DraggingState);
2080
2081
2082
2083
2084
2085
2086
2087void QAbstractItemView::dragMoveEvent(QDragMoveEvent *event)
2089 Q_D(QAbstractItemView);
2090 d->draggedPosition = event->position().toPoint() + d->offset();
2091 if (dragDropMode() == InternalMove
2092 && (event->source() !=
this || !(event->possibleActions() & Qt::MoveAction)))
2098 QModelIndex index = indexAt(event->position().toPoint());
2100 if (!d->droppingOnItself(event, index)
2101 && d->canDrop(event)) {
2103 if (index.isValid() && d->showDropIndicator) {
2104 QRect rect = visualRect(index);
2105 d->dropIndicatorPosition = d->position(event->position().toPoint(), rect, index);
2106 if (d->selectionBehavior == QAbstractItemView::SelectRows
2107 && d->dropIndicatorPosition != OnViewport
2108 && (d->dropIndicatorPosition != OnItem || event->source() ==
this)) {
2109 const int maxCol = d->model->columnCount(index.parent()) - 1;
2110 const auto idx = index.column() > 0 ? index.siblingAtColumn(0) : index;
2111 rect = d->intersectedRect(viewport()->rect(), idx, idx.siblingAtColumn(maxCol));
2113 switch (d->dropIndicatorPosition) {
2115 if (d->isIndexDropEnabled(index.parent())) {
2116 d->dropIndicatorRect = QRect(rect.left(), rect.top(), rect.width(), 0);
2117 event->acceptProposedAction();
2119 d->dropIndicatorRect = QRect();
2123 if (d->isIndexDropEnabled(index.parent())) {
2124 d->dropIndicatorRect = QRect(rect.left(), rect.bottom(), rect.width(), 0);
2125 event->acceptProposedAction();
2127 d->dropIndicatorRect = QRect();
2131 if (d->isIndexDropEnabled(index)) {
2132 d->dropIndicatorRect = rect;
2133 event->acceptProposedAction();
2135 d->dropIndicatorRect = QRect();
2139 d->dropIndicatorRect = QRect();
2140 if (d->isIndexDropEnabled(rootIndex())) {
2141 event->acceptProposedAction();
2146 d->dropIndicatorRect = QRect();
2147 d->dropIndicatorPosition = OnViewport;
2148 if (d->isIndexDropEnabled(rootIndex())) {
2149 event->acceptProposedAction();
2152 d->viewport->update();
2155 if (d->shouldAutoScroll(event->position().toPoint()))
2160
2161
2162
2163
2164bool QAbstractItemViewPrivate::droppingOnItself(QDropEvent *event,
const QModelIndex &index)
2166 Q_Q(QAbstractItemView);
2167 Qt::DropAction dropAction = event->dropAction();
2168 if (q->dragDropMode() == QAbstractItemView::InternalMove)
2169 dropAction = Qt::MoveAction;
2170 if (event->source() == q
2171 && event->possibleActions() & Qt::MoveAction
2172 && dropAction == Qt::MoveAction) {
2173 QModelIndexList selectedIndexes = q->selectedIndexes();
2174 QModelIndex child = index;
2175 while (child.isValid() && child != root) {
2176 if (selectedIndexes.contains(child))
2178 child = child.parent();
2185
2186
2187
2188
2189
2190void QAbstractItemView::dragLeaveEvent(QDragLeaveEvent *)
2192 Q_D(QAbstractItemView);
2195 d->hover = QModelIndex();
2196 d->viewport->update();
2200
2201
2202
2203
2204
2205
2206void QAbstractItemView::dropEvent(QDropEvent *event)
2208 Q_D(QAbstractItemView);
2209 if (dragDropMode() == InternalMove) {
2210 if (event->source() !=
this || !(event->possibleActions() & Qt::MoveAction))
2217 if (d->dropOn(event, &row, &col, &index)) {
2218 const Qt::DropAction action = dragDropMode() == InternalMove ? Qt::MoveAction : event->dropAction();
2219 if (d->model->dropMimeData(event->mimeData(), action, row, col, index)) {
2220 if (action != event->dropAction()) {
2221 event->setDropAction(action);
2224 event->acceptProposedAction();
2230 d->viewport->update();
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244bool QAbstractItemViewPrivate::dropOn(QDropEvent *event,
int *dropRow,
int *dropCol, QModelIndex *dropIndex)
2246 Q_Q(QAbstractItemView);
2247 if (event->isAccepted())
2252 if (viewport->rect().contains(event->position().toPoint())) {
2253 index = q->indexAt(event->position().toPoint());
2254 if (!index.isValid())
2259 if (model->supportedDropActions() & event->dropAction()) {
2262 if (index != root) {
2263 dropIndicatorPosition = position(event->position().toPoint(), q->visualRect(index), index);
2264 switch (dropIndicatorPosition) {
2265 case QAbstractItemView::AboveItem:
2267 col = index.column();
2268 index = index.parent();
2270 case QAbstractItemView::BelowItem:
2271 row = index.row() + 1;
2272 col = index.column();
2273 index = index.parent();
2275 case QAbstractItemView::OnItem:
2276 case QAbstractItemView::OnViewport:
2280 dropIndicatorPosition = QAbstractItemView::OnViewport;
2285 if (!droppingOnItself(event, index))
2291QAbstractItemView::DropIndicatorPosition
2292QAbstractItemViewPrivate::position(
const QPoint &pos,
const QRect &rect,
const QModelIndex &index)
const
2294 QAbstractItemView::DropIndicatorPosition r = QAbstractItemView::OnViewport;
2296 const int margin = qBound(2, qRound(qreal(rect.height()) / 5.5), 12);
2297 if (pos.y() - rect.top() < margin) {
2298 r = QAbstractItemView::AboveItem;
2299 }
else if (rect.bottom() - pos.y() < margin) {
2300 r = QAbstractItemView::BelowItem;
2301 }
else if (rect.contains(pos,
true)) {
2302 r = QAbstractItemView::OnItem;
2305 QRect touchingRect = rect;
2306 touchingRect.adjust(-1, -1, 1, 1);
2307 if (touchingRect.contains(pos,
false)) {
2308 r = QAbstractItemView::OnItem;
2312 if (r == QAbstractItemView::OnItem && (!(model->flags(index) & Qt::ItemIsDropEnabled)))
2313 r = pos.y() < rect.center().y() ? QAbstractItemView::AboveItem : QAbstractItemView::BelowItem;
2321
2322
2323
2324
2325
2326void QAbstractItemView::focusInEvent(QFocusEvent *event)
2328 Q_D(QAbstractItemView);
2329 QAbstractScrollArea::focusInEvent(event);
2331 const QItemSelectionModel* model = selectionModel();
2332 bool currentIndexValid = currentIndex().isValid();
2335 && !d->currentIndexSet
2336 && !currentIndexValid) {
2337 bool autoScroll = d->autoScroll;
2338 d->autoScroll =
false;
2339 QModelIndex index = moveCursor(MoveNext, Qt::NoModifier);
2340 if (index.isValid() && d->isIndexEnabled(index) && event->reason() != Qt::MouseFocusReason) {
2341 selectionModel()->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
2342 currentIndexValid =
true;
2344 d->autoScroll = autoScroll;
2347 if (model && currentIndexValid)
2348 setAttribute(Qt::WA_InputMethodEnabled, (currentIndex().flags() & Qt::ItemIsEditable));
2349 else if (!currentIndexValid)
2350 setAttribute(Qt::WA_InputMethodEnabled,
false);
2352 d->viewport->update();
2356
2357
2358
2359
2360
2361void QAbstractItemView::focusOutEvent(QFocusEvent *event)
2363 Q_D(QAbstractItemView);
2364 QAbstractScrollArea::focusOutEvent(event);
2365 d->viewport->update();
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379void QAbstractItemView::keyPressEvent(QKeyEvent *event)
2381 Q_D(QAbstractItemView);
2382 d->delayedAutoScroll.stop();
2384#ifdef QT_KEYPAD_NAVIGATION
2385 switch (event->key()) {
2386 case Qt::Key_Select:
2387 if (QApplicationPrivate::keypadNavigationEnabled()) {
2388 if (!hasEditFocus()) {
2395 if (QApplicationPrivate::keypadNavigationEnabled() && hasEditFocus()) {
2396 setEditFocus(
false);
2406 if (QApplicationPrivate::keypadNavigationEnabled() && !hasEditFocus()
2407 && QWidgetPrivate::canKeypadNavigate(Qt::Vertical)) {
2415 if (QApplicationPrivate::keypadNavigationEnabled() && !hasEditFocus()
2416 && (QWidgetPrivate::canKeypadNavigate(Qt::Horizontal) || QWidgetPrivate::inTabWidget(
this))) {
2422 if (QApplicationPrivate::keypadNavigationEnabled() && !hasEditFocus()) {
2429#if !defined(QT_NO_CLIPBOARD) && !defined(QT_NO_SHORTCUT)
2430 if (event == QKeySequence::Copy) {
2431 const QModelIndex index = currentIndex();
2432 if (index.isValid() && d->model) {
2433 const QVariant variant = d->model->data(index, Qt::DisplayRole);
2434 if (variant.canConvert<QString>())
2435 QGuiApplication::clipboard()->setText(variant.toString());
2441 QPersistentModelIndex newCurrent;
2442 d->moveCursorUpdatedView =
false;
2443 switch (event->key()) {
2445 newCurrent = moveCursor(MoveDown, event->modifiers());
2448 newCurrent = moveCursor(MoveUp, event->modifiers());
2451 newCurrent = moveCursor(MoveLeft, event->modifiers());
2454 newCurrent = moveCursor(MoveRight, event->modifiers());
2457 newCurrent = moveCursor(MoveHome, event->modifiers());
2460 newCurrent = moveCursor(MoveEnd, event->modifiers());
2462 case Qt::Key_PageUp:
2463 newCurrent = moveCursor(MovePageUp, event->modifiers());
2465 case Qt::Key_PageDown:
2466 newCurrent = moveCursor(MovePageDown, event->modifiers());
2469 if (d->tabKeyNavigation)
2470 newCurrent = moveCursor(MoveNext, event->modifiers());
2472 case Qt::Key_Backtab:
2473 if (d->tabKeyNavigation)
2474 newCurrent = moveCursor(MovePrevious, event->modifiers());
2478 QPersistentModelIndex oldCurrent = currentIndex();
2479 if (newCurrent != oldCurrent && newCurrent.isValid() && d->isIndexEnabled(newCurrent)) {
2480 if (!hasFocus() && QApplication::focusWidget() == indexWidget(oldCurrent))
2482 QItemSelectionModel::SelectionFlags command = selectionCommand(newCurrent, event);
2483 if (command != QItemSelectionModel::NoUpdate
2484 || style()->styleHint(QStyle::SH_ItemView_MovementWithoutUpdatingSelection,
nullptr,
this)) {
2486 if (command & QItemSelectionModel::Current) {
2487 d->selectionModel->setCurrentIndex(newCurrent, QItemSelectionModel::NoUpdate);
2488 if (!d->currentSelectionStartIndex.isValid())
2489 d->currentSelectionStartIndex = oldCurrent;
2490 QRect rect(visualRect(d->currentSelectionStartIndex).center(), visualRect(newCurrent).center());
2491 setSelection(rect, command);
2493 d->selectionModel->setCurrentIndex(newCurrent, command);
2494 d->currentSelectionStartIndex = newCurrent;
2495 if (newCurrent.isValid()) {
2497 QRect rect(visualRect(newCurrent).center(), QSize(1, 1));
2498 setSelection(rect, command);
2506 switch (event->key()) {
2510#ifdef QT_KEYPAD_NAVIGATION
2511 if (QApplicationPrivate::keypadNavigationEnabled()
2512 && QWidgetPrivate::canKeypadNavigate(Qt::Vertical)) {
2519#ifdef QT_KEYPAD_NAVIGATION
2520 if (QApplication::navigationMode() == Qt::NavigationModeKeypadDirectional
2521 && (QWidgetPrivate::canKeypadNavigate(Qt::Horizontal)
2522 || (QWidgetPrivate::inTabWidget(
this) && d->model->columnCount(d->root) > 1))) {
2529 case Qt::Key_PageUp:
2530 case Qt::Key_PageDown:
2531 case Qt::Key_Escape:
2533 case Qt::Key_Control:
2534 case Qt::Key_Delete:
2535 case Qt::Key_Backspace:
2539 case Qt::Key_Select:
2540 if (!edit(currentIndex(), AnyKeyPressed, event)) {
2541 if (d->selectionModel)
2542 d->selectionModel->select(currentIndex(), selectionCommand(currentIndex(), event));
2543 if (event->key() == Qt::Key_Space) {
2544 keyboardSearch(event->text());
2548#ifdef QT_KEYPAD_NAVIGATION
2549 if ( event->key()==Qt::Key_Select ) {
2551 if (currentIndex().isValid()) {
2552 if (state() != EditingState)
2553 emit activated(currentIndex());
2562 case Qt::Key_Return:
2565 if (!edit(currentIndex(), EditKeyPressed, event) && d->editorIndexHash.isEmpty())
2570 if (!edit(currentIndex(), EditKeyPressed, event))
2574 case Qt::Key_Return:
2578 if (state() != EditingState || hasFocus()) {
2579 if (currentIndex().isValid())
2580 emit activated(currentIndex());
2586#ifndef QT_NO_SHORTCUT
2587 if (event == QKeySequence::SelectAll && selectionMode() != NoSelection) {
2593 if (event->key() == Qt::Key_O && event->modifiers() & Qt::ControlModifier && currentIndex().isValid()) {
2594 emit activated(currentIndex());
2598 bool modified = (event->modifiers() & (Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier));
2599 if (!event->text().isEmpty() && !modified && !edit(currentIndex(), AnyKeyPressed, event)) {
2600 keyboardSearch(event->text());
2607 if (d->moveCursorUpdatedView)
2612
2613
2614
2615
2616
2617void QAbstractItemView::resizeEvent(QResizeEvent *event)
2619 QAbstractScrollArea::resizeEvent(event);
2624
2625
2626
2627
2628
2629void QAbstractItemView::timerEvent(QTimerEvent *event)
2631 Q_D(QAbstractItemView);
2632 if (event->timerId() == d->fetchMoreTimer.timerId())
2634 else if (event->timerId() == d->delayedReset.timerId())
2636 else if (event->timerId() == d->autoScrollTimer.timerId())
2638 else if (event->timerId() == d->updateTimer.timerId())
2639 d->updateDirtyRegion();
2640 else if (event->timerId() == d->delayedEditing.timerId()) {
2641 d->delayedEditing.stop();
2642 edit(currentIndex());
2643 }
else if (event->timerId() == d->delayedLayout.timerId()) {
2644 d->delayedLayout.stop();
2646 d->interruptDelayedItemsLayout();
2648 const QModelIndex current = currentIndex();
2649 if (current.isValid() && d->state == QAbstractItemView::EditingState)
2652 }
else if (event->timerId() == d->delayedAutoScroll.timerId()) {
2653 d->delayedAutoScroll.stop();
2656 if (d->pressedIndex.isValid() && d->pressedIndex == currentIndex())
2657 scrollTo(d->pressedIndex);
2658 }
else if (event->timerId() == d->pressClosedEditorWatcher.timerId()) {
2659 d->pressClosedEditorWatcher.stop();
2664
2665
2666void QAbstractItemView::inputMethodEvent(QInputMethodEvent *event)
2668 Q_D(QAbstractItemView);
2675 bool forwardEventToEditor =
false;
2676 const bool commit = !event->commitString().isEmpty();
2677 const bool preediting = !event->preeditString().isEmpty();
2678 if (QWidget *currentEditor = d->editorForIndex(currentIndex()).widget) {
2679 if (d->waitForIMCommit) {
2680 if (commit || !preediting) {
2682 d->waitForIMCommit =
false;
2683 QApplication::sendEvent(currentEditor, event);
2685 QAbstractItemDelegate *delegate = itemDelegateForIndex(currentIndex());
2687 delegate->setEditorData(currentEditor, currentIndex());
2688 d->selectAllInEditor(currentEditor);
2690 if (currentEditor->focusPolicy() != Qt::NoFocus)
2691 currentEditor->setFocus();
2694 QApplication::sendEvent(currentEditor, event);
2698 }
else if (preediting) {
2700 d->waitForIMCommit =
true;
2702 forwardEventToEditor =
true;
2703 }
else if (!commit) {
2707 if (!edit(currentIndex(), AnyKeyPressed, event)) {
2708 d->waitForIMCommit =
false;
2710 keyboardSearch(event->commitString());
2712 }
else if (QWidget *currentEditor; forwardEventToEditor
2713 && (currentEditor = d->editorForIndex(currentIndex()).widget)) {
2714 QApplication::sendEvent(currentEditor, event);
2718#if QT_CONFIG(draganddrop)
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2738
2739
2740QAbstractItemView::DropIndicatorPosition QAbstractItemView::dropIndicatorPosition()
const
2742 Q_D(
const QAbstractItemView);
2743 return d->dropIndicatorPosition;
2748
2749
2750
2751
2752
2753
2754QModelIndexList QAbstractItemView::selectedIndexes()
const
2756 Q_D(
const QAbstractItemView);
2757 QModelIndexList indexes;
2758 if (d->selectionModel) {
2759 indexes = d->selectionModel->selectedIndexes();
2760 auto isHidden = [
this](
const QModelIndex &idx) {
2761 return isIndexHidden(idx);
2763 indexes.removeIf(isHidden);
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781bool QAbstractItemView::edit(
const QModelIndex &index, EditTrigger trigger, QEvent *event)
2783 Q_D(QAbstractItemView);
2785 if (!d->isIndexValid(index))
2788 if (QWidget *w = (d->persistent.isEmpty() ?
static_cast<QWidget*>(
nullptr) : d->editorForIndex(index).widget.data())) {
2789 if (w->focusPolicy() == Qt::NoFocus)
2791 if (!d->waitForIMCommit)
2798 if (trigger == DoubleClicked) {
2799 d->delayedEditing.stop();
2800 d->delayedAutoScroll.stop();
2801 }
else if (trigger == CurrentChanged) {
2802 d->delayedEditing.stop();
2806 QPersistentModelIndex safeIndex(index);
2808 if (d->sendDelegateEvent(index, event)) {
2813 if (!safeIndex.isValid()) {
2818 EditTriggers lastTrigger = d->lastTrigger;
2819 d->lastTrigger = trigger;
2821 if (!d->shouldEdit(trigger, d->model->buddy(safeIndex)))
2824 if (d->delayedEditing.isActive())
2829 if (lastTrigger == DoubleClicked && trigger == SelectedClicked)
2833 if (trigger == SelectedClicked)
2834 d->delayedEditing.start(QApplication::doubleClickInterval(),
this);
2836 d->openEditor(safeIndex, d->shouldForwardEvent(trigger, event) ? event :
nullptr);
2842
2843
2844
2845void QAbstractItemView::updateEditorData()
2847 Q_D(QAbstractItemView);
2848 d->updateEditorData(QModelIndex(), QModelIndex());
2852
2853
2854
2855void QAbstractItemView::updateEditorGeometries()
2857 Q_D(QAbstractItemView);
2858 if (d->editorIndexHash.isEmpty())
2860 if (d->delayedPendingLayout) {
2862 d->executePostedLayout();
2865 QStyleOptionViewItem option;
2866 initViewItemOption(&option);
2867 QEditorIndexHash::iterator it = d->editorIndexHash.begin();
2868 QWidgetList editorsToRelease;
2869 QWidgetList editorsToHide;
2870 while (it != d->editorIndexHash.end()) {
2871 QModelIndex index = it.value();
2872 QWidget *editor = it.key();
2873 if (index.isValid() && editor) {
2874 option.rect = visualRect(index);
2875 if (option.rect.isValid()) {
2877 QAbstractItemDelegate *delegate = itemDelegateForIndex(index);
2879 delegate->updateEditorGeometry(editor, option, index);
2881 editorsToHide << editor;
2885 d->indexEditorHash.remove(it.value());
2886 it = d->editorIndexHash.erase(it);
2887 editorsToRelease << editor;
2893 for (
int i = 0; i < editorsToHide.size(); ++i) {
2894 editorsToHide.at(i)->hide();
2896 for (
int i = 0; i < editorsToRelease.size(); ++i) {
2897 d->releaseEditor(editorsToRelease.at(i));
2902
2903
2904void QAbstractItemView::updateGeometries()
2906 Q_D(QAbstractItemView);
2907 updateEditorGeometries();
2908 d->fetchMoreTimer.start(0,
this);
2909 d->updateGeometry();
2913
2914
2915void QAbstractItemView::verticalScrollbarValueChanged(
int value)
2917 Q_D(QAbstractItemView);
2918 if (verticalScrollBar()->maximum() == value && d->model->canFetchMore(d->root))
2919 d->model->fetchMore(d->root);
2920 QPoint posInVp = viewport()->mapFromGlobal(QCursor::pos());
2921 if (viewport()->rect().contains(posInVp))
2922 d->checkMouseMove(posInVp);
2926
2927
2928void QAbstractItemView::horizontalScrollbarValueChanged(
int value)
2930 Q_D(QAbstractItemView);
2931 if (horizontalScrollBar()->maximum() == value && d->model->canFetchMore(d->root))
2932 d->model->fetchMore(d->root);
2933 QPoint posInVp = viewport()->mapFromGlobal(QCursor::pos());
2934 if (viewport()->rect().contains(posInVp))
2935 d->checkMouseMove(posInVp);
2939
2940
2941void QAbstractItemView::verticalScrollbarAction(
int)
2947
2948
2949void QAbstractItemView::horizontalScrollbarAction(
int)
2955
2956
2957
2958
2959
2960
2961
2963void QAbstractItemView::closeEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint)
2965 Q_D(QAbstractItemView);
2969 const bool isPersistent = d->persistent.contains(editor);
2970 const QModelIndex index = d->indexForEditor(editor);
2971 if (!index.isValid()) {
2972 if (!editor->isVisible()) {
2980 qWarning(
"QAbstractItemView::closeEditor called with an editor that does not belong to this view");
2984 const bool hadFocus = editor->hasFocus();
2988 d->pressClosedEditorWatcher.start(0,
this);
2989 d->lastEditedIndex = index;
2991 if (!isPersistent) {
2993 QModelIndex index = d->indexForEditor(editor);
2994 editor->removeEventFilter(itemDelegateForIndex(index));
2995 d->removeEditor(editor);
2998 if (focusPolicy() != Qt::NoFocus)
3001 editor->clearFocus();
3003 d->checkPersistentEditorFocus();
3006 QPointer<QWidget> ed = editor;
3007 QCoreApplication::sendPostedEvents(editor, 0);
3010 if (!isPersistent && editor)
3011 d->releaseEditor(editor, index);
3016 QItemSelectionModel::SelectionFlags flags = QItemSelectionModel::NoUpdate;
3017 if (d->selectionMode != NoSelection)
3018 flags = QItemSelectionModel::ClearAndSelect | d->selectionBehaviorFlags();
3020 case QAbstractItemDelegate::EditNextItem: {
3021 QModelIndex index = moveCursor(MoveNext, Qt::NoModifier);
3022 if (index.isValid()) {
3023 QPersistentModelIndex persistent(index);
3024 d->selectionModel->setCurrentIndex(persistent, flags);
3026 if (index.flags() & Qt::ItemIsEditable
3027 && (!(editTriggers() & QAbstractItemView::CurrentChanged)))
3030 case QAbstractItemDelegate::EditPreviousItem: {
3031 QModelIndex index = moveCursor(MovePrevious, Qt::NoModifier);
3032 if (index.isValid()) {
3033 QPersistentModelIndex persistent(index);
3034 d->selectionModel->setCurrentIndex(persistent, flags);
3036 if (index.flags() & Qt::ItemIsEditable
3037 && (!(editTriggers() & QAbstractItemView::CurrentChanged)))
3040 case QAbstractItemDelegate::SubmitModelCache:
3043 case QAbstractItemDelegate::RevertModelCache:
3052
3053
3054
3055
3056void QAbstractItemView::commitData(QWidget *editor)
3058 Q_D(QAbstractItemView);
3059 if (!editor || !d->itemDelegate || d->currentlyCommittingEditor)
3061 QModelIndex index = d->indexForEditor(editor);
3062 if (!index.isValid()) {
3063 qWarning(
"QAbstractItemView::commitData called with an editor that does not belong to this view");
3066 d->currentlyCommittingEditor = editor;
3067 QAbstractItemDelegate *delegate = itemDelegateForIndex(index);
3068 editor->removeEventFilter(delegate);
3069 delegate->setModelData(editor, d->model, index);
3070 editor->installEventFilter(delegate);
3071 d->currentlyCommittingEditor =
nullptr;
3075
3076
3077
3078
3079void QAbstractItemView::editorDestroyed(QObject *editor)
3081 Q_D(QAbstractItemView);
3082 QWidget *w = qobject_cast<QWidget*>(editor);
3084 d->persistent.remove(w);
3085 if (state() == EditingState)
3092
3093
3094
3095
3096
3097
3098
3099void QAbstractItemView::keyboardSearch(
const QString &search)
3101 Q_D(QAbstractItemView);
3102 if (!d->model->rowCount(d->root) || !d->model->columnCount(d->root))
3105 QModelIndex start = currentIndex().isValid() ? currentIndex()
3106 : d->model->index(0, 0, d->root);
3107 bool skipRow =
false;
3108 bool keyboardTimeWasValid = d->keyboardInputTime.isValid();
3109 qint64 keyboardInputTimeElapsed;
3110 if (keyboardTimeWasValid)
3111 keyboardInputTimeElapsed = d->keyboardInputTime.restart();
3113 d->keyboardInputTime.start();
3114 if (search.isEmpty() || !keyboardTimeWasValid
3115 || keyboardInputTimeElapsed > QApplication::keyboardInputInterval()) {
3116 d->keyboardInput = search;
3117 skipRow = currentIndex().isValid();
3119 d->keyboardInput += search;
3123 bool sameKey =
false;
3124 if (d->keyboardInput.size() > 1) {
3125 int c = d->keyboardInput.count(d->keyboardInput.at(d->keyboardInput.size() - 1));
3126 sameKey = (c == d->keyboardInput.size());
3133 QModelIndex parent = start.parent();
3134 int newRow = (start.row() < d->model->rowCount(parent) - 1) ? start.row() + 1 : 0;
3135 start = d->model->index(newRow, start.column(), parent);
3139 QModelIndex current = start;
3140 QModelIndexList match;
3141 QModelIndex firstMatch;
3142 QModelIndex startMatch;
3143 QModelIndexList previous;
3145 match = d->model->match(current, Qt::DisplayRole, d->keyboardInput, 1,
3146 d->keyboardSearchFlags);
3147 if (match == previous)
3149 firstMatch = match.value(0);
3151 if (firstMatch.isValid()) {
3152 if (d->isIndexEnabled(firstMatch)) {
3153 setCurrentIndex(firstMatch);
3156 int row = firstMatch.row() + 1;
3157 if (row >= d->model->rowCount(firstMatch.parent()))
3159 current = firstMatch.sibling(row, firstMatch.column());
3162 if (!startMatch.isValid())
3163 startMatch = firstMatch;
3164 else if (startMatch == firstMatch)
3167 }
while (current != start && firstMatch.isValid());
3171
3172
3173
3174
3175
3176QSize QAbstractItemView::sizeHintForIndex(
const QModelIndex &index)
const
3178 Q_D(
const QAbstractItemView);
3179 if (!d->isIndexValid(index))
3181 const auto delegate = itemDelegateForIndex(index);
3182 QStyleOptionViewItem option;
3183 initViewItemOption(&option);
3184 return delegate ? delegate->sizeHint(option, index) : QSize();
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203int QAbstractItemView::sizeHintForRow(
int row)
const
3205 Q_D(
const QAbstractItemView);
3207 if (row < 0 || row >= d->model->rowCount(d->root))
3212 QStyleOptionViewItem option;
3213 initViewItemOption(&option);
3215 int colCount = d->model->columnCount(d->root);
3216 for (
int c = 0; c < colCount; ++c) {
3217 const QModelIndex index = d->model->index(row, c, d->root);
3218 if (QWidget *editor = d->editorForIndex(index).widget.data())
3219 height = qMax(height, editor->height());
3220 if (
const QAbstractItemDelegate *delegate = itemDelegateForIndex(index))
3221 height = qMax(height, delegate->sizeHint(option, index).height());
3227
3228
3229
3230
3231
3232
3233
3234int QAbstractItemView::sizeHintForColumn(
int column)
const
3236 Q_D(
const QAbstractItemView);
3238 if (column < 0 || column >= d->model->columnCount(d->root))
3243 QStyleOptionViewItem option;
3244 initViewItemOption(&option);
3246 int rows = d->model->rowCount(d->root);
3247 for (
int r = 0; r < rows; ++r) {
3248 const QModelIndex index = d->model->index(r, column, d->root);
3249 if (QWidget *editor = d->editorForIndex(index).widget.data())
3250 width = qMax(width, editor->sizeHint().width());
3251 if (
const QAbstractItemDelegate *delegate = itemDelegateForIndex(index))
3252 width = qMax(width, delegate->sizeHint(option, index).width());
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274int QAbstractItemView::updateThreshold()
const
3276 Q_D(
const QAbstractItemView);
3277 return d->updateThreshold;
3280void QAbstractItemView::setUpdateThreshold(
int threshold)
3282 Q_D(QAbstractItemView);
3283 if (d->updateThreshold == threshold)
3285 d->updateThreshold = threshold;
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3300Qt::MatchFlags QAbstractItemView::keyboardSearchFlags()
const
3302 Q_D(
const QAbstractItemView);
3303 return d->keyboardSearchFlags;
3306void QAbstractItemView::setKeyboardSearchFlags(Qt::MatchFlags searchFlags)
3308 Q_D(QAbstractItemView);
3309 d->keyboardSearchFlags = searchFlags;
3313
3314
3315
3316
3317
3318void QAbstractItemView::openPersistentEditor(
const QModelIndex &index)
3320 Q_D(QAbstractItemView);
3321 QStyleOptionViewItem options;
3322 initViewItemOption(&options);
3323 options.rect = visualRect(index);
3324 options.state |= (index == currentIndex() ? QStyle::State_HasFocus : QStyle::State_None);
3326 QWidget *editor = d->editor(index, options);
3329 d->persistent.insert(editor);
3334
3335
3336
3337
3338void QAbstractItemView::closePersistentEditor(
const QModelIndex &index)
3340 Q_D(QAbstractItemView);
3341 if (QWidget *editor = d->editorForIndex(index).widget.data()) {
3342 if (index == selectionModel()->currentIndex())
3343 closeEditor(editor, QAbstractItemDelegate::RevertModelCache);
3344 d->persistent.remove(editor);
3345 d->removeEditor(editor);
3346 d->releaseEditor(editor, index);
3351
3352
3353
3354
3355
3356
3357bool QAbstractItemView::isPersistentEditorOpen(
const QModelIndex &index)
const
3359 Q_D(
const QAbstractItemView);
3360 QWidget *editor = d->editorForIndex(index).widget;
3361 return editor && d->persistent.contains(editor);
4129void QAbstractItemView::doAutoScroll()
4132 Q_D(QAbstractItemView);
4133 QScrollBar *verticalScroll = verticalScrollBar();
4134 QScrollBar *horizontalScroll = horizontalScrollBar();
4138 QHeaderView *hv = qobject_cast<QHeaderView*>(
this);
4140 QAbstractScrollArea *parent = qobject_cast<QAbstractScrollArea*>(parentWidget());
4142 if (hv->orientation() == Qt::Horizontal) {
4143 if (!hv->horizontalScrollBar() || !hv->horizontalScrollBar()->isVisible())
4144 horizontalScroll = parent->horizontalScrollBar();
4146 if (!hv->verticalScrollBar() || !hv->verticalScrollBar()->isVisible())
4147 verticalScroll = parent->verticalScrollBar();
4152 const int verticalStep = verticalScroll->pageStep();
4153 const int horizontalStep = horizontalScroll->pageStep();
4154 if (d->autoScrollCount < qMax(verticalStep, horizontalStep))
4155 ++d->autoScrollCount;
4157 const int margin = d->autoScrollMargin;
4158 const int verticalValue = verticalScroll->value();
4159 const int horizontalValue = horizontalScroll->value();
4161 const QPoint pos = d->draggedPosition - d->offset();
4162 const QRect area = QWidgetPrivate::get(d->viewport)->clipRect();
4165 if (pos.y() - area.top() < margin)
4166 verticalScroll->setValue(verticalValue - d->autoScrollCount);
4167 else if (area.bottom() - pos.y() < margin)
4168 verticalScroll->setValue(verticalValue + d->autoScrollCount);
4169 if (pos.x() - area.left() < margin)
4170 horizontalScroll->setValue(horizontalValue - d->autoScrollCount);
4171 else if (area.right() - pos.x() < margin)
4172 horizontalScroll->setValue(horizontalValue + d->autoScrollCount);
4174 const bool verticalUnchanged = (verticalValue == verticalScroll->value());
4175 const bool horizontalUnchanged = (horizontalValue == horizontalScroll->value());
4176 if (verticalUnchanged && horizontalUnchanged) {
4179#if QT_CONFIG(draganddrop)
4180 d->dropIndicatorRect = QRect();
4181 d->dropIndicatorPosition = QAbstractItemView::OnViewport;
4184 case QAbstractItemView::DragSelectingState: {
4187 const QPoint globalPos = d->viewport->mapToGlobal(pos);
4188 const QPoint windowPos = window()->mapFromGlobal(globalPos);
4189 QMouseEvent mm(QEvent::MouseMove, pos, windowPos, globalPos,
4190 Qt::NoButton, Qt::LeftButton, d->pressedModifiers,
4191 Qt::MouseEventSynthesizedByQt);
4192 QApplication::sendEvent(viewport(), &mm);
4195 case QAbstractItemView::DraggingState: {
4201 d->draggedPosition = pos + d->offset();
4207 d->viewport->update();
4311QItemSelectionModel::SelectionFlags QAbstractItemViewPrivate::extendedSelectionCommand(
4312 const QModelIndex &index,
const QEvent *event)
const
4314 Qt::KeyboardModifiers modifiers = event && event->isInputEvent()
4315 ?
static_cast<
const QInputEvent*>(event)->modifiers()
4316 : QGuiApplication::keyboardModifiers();
4318 switch (event->type()) {
4319 case QEvent::MouseMove: {
4321 if (modifiers & Qt::ControlModifier)
4322 return QItemSelectionModel::ToggleCurrent|selectionBehaviorFlags();
4325 case QEvent::MouseButtonPress: {
4326 const Qt::MouseButton button =
static_cast<
const QMouseEvent*>(event)->button();
4327 const bool rightButtonPressed = button & Qt::RightButton;
4328 const bool shiftKeyPressed = modifiers & Qt::ShiftModifier;
4329 const bool controlKeyPressed = modifiers & Qt::ControlModifier;
4330 const bool indexIsSelected = selectionModel->isSelected(index);
4331 if ((shiftKeyPressed || controlKeyPressed) && rightButtonPressed)
4332 return QItemSelectionModel::NoUpdate;
4333 if (!shiftKeyPressed && !controlKeyPressed && indexIsSelected)
4334 return QItemSelectionModel::NoUpdate;
4335 if (!index.isValid() && !rightButtonPressed && !shiftKeyPressed && !controlKeyPressed)
4336 return QItemSelectionModel::Clear;
4337 if (!index.isValid())
4338 return QItemSelectionModel::NoUpdate;
4340 if (controlKeyPressed && !rightButtonPressed && pressedAlreadySelected
4341#if QT_CONFIG(draganddrop)
4342 && dragEnabled && isIndexDragEnabled(index)
4345 return QItemSelectionModel::NoUpdate;
4349 case QEvent::MouseButtonRelease: {
4351 const Qt::MouseButton button =
static_cast<
const QMouseEvent*>(event)->button();
4352 const bool rightButtonPressed = button & Qt::RightButton;
4353 const bool shiftKeyPressed = modifiers & Qt::ShiftModifier;
4354 const bool controlKeyPressed = modifiers & Qt::ControlModifier;
4355 if (((index == pressedIndex && selectionModel->isSelected(index))
4356 || !index.isValid()) && state != QAbstractItemView::DragSelectingState
4357 && !shiftKeyPressed && !controlKeyPressed && (!rightButtonPressed || !index.isValid()))
4358 return QItemSelectionModel::ClearAndSelect|selectionBehaviorFlags();
4359 if (index == pressedIndex && controlKeyPressed && !rightButtonPressed
4360#if QT_CONFIG(draganddrop)
4361 && dragEnabled && isIndexDragEnabled(index)
4366 return QItemSelectionModel::NoUpdate;
4368 case QEvent::KeyPress: {
4370 switch (
static_cast<
const QKeyEvent*>(event)->key()) {
4371 case Qt::Key_Backtab:
4372 modifiers = modifiers & ~Qt::ShiftModifier;
4380 case Qt::Key_PageUp:
4381 case Qt::Key_PageDown:
4383 if (modifiers & Qt::ControlModifier
4384#ifdef QT_KEYPAD_NAVIGATION
4386 || QApplication::navigationMode() == Qt::NavigationModeKeypadTabOrder
4389 return QItemSelectionModel::NoUpdate;
4391 case Qt::Key_Select:
4392 return QItemSelectionModel::Toggle|selectionBehaviorFlags();
4394 if (modifiers & Qt::ControlModifier)
4395 return QItemSelectionModel::Toggle|selectionBehaviorFlags();
4396 return QItemSelectionModel::Select|selectionBehaviorFlags();
4407 if (modifiers & Qt::ShiftModifier)
4408 return QItemSelectionModel::SelectCurrent|selectionBehaviorFlags();
4409 if (modifiers & Qt::ControlModifier)
4410 return QItemSelectionModel::Toggle|selectionBehaviorFlags();
4411 if (state == QAbstractItemView::DragSelectingState) {
4413 return QItemSelectionModel::Clear|QItemSelectionModel::SelectCurrent|selectionBehaviorFlags();
4416 return QItemSelectionModel::ClearAndSelect|selectionBehaviorFlags();