diff options
Diffstat (limited to 'src/widgets/itemviews/qlistview.cpp')
-rw-r--r-- | src/widgets/itemviews/qlistview.cpp | 124 |
1 files changed, 68 insertions, 56 deletions
diff --git a/src/widgets/itemviews/qlistview.cpp b/src/widgets/itemviews/qlistview.cpp index 30bf327af7..a7f1931947 100644 --- a/src/widgets/itemviews/qlistview.cpp +++ b/src/widgets/itemviews/qlistview.cpp @@ -88,7 +88,7 @@ extern bool qt_sendSpontaneousEvent(QObject *receiver, QEvent *event); that can be taken for views that are intended to display items with equal sizes is to set the \l uniformItemSizes property to true. - \sa {View Classes}, {Item Views Puzzle Example}, QTreeView, QTableView, QListWidget + \sa {View Classes}, QTreeView, QTableView, QListWidget */ /*! @@ -352,7 +352,7 @@ int QListView::spacing() const /*! \property QListView::batchSize \brief the number of items laid out in each batch if \l layoutMode is - set to \l Batched + set to \l Batched. The default value is 100. @@ -755,7 +755,10 @@ void QListView::mouseMoveEvent(QMouseEvent *e) && d->selectionMode != NoSelection) { QRect rect(d->pressedPosition, e->position().toPoint() + QPoint(horizontalOffset(), verticalOffset())); rect = rect.normalized(); - d->viewport->update(d->mapToViewport(rect.united(d->elasticBand))); + const int margin = 2 * style()->pixelMetric(QStyle::PM_DefaultFrameWidth); + const QRect viewPortRect = rect.united(d->elasticBand) + .adjusted(-margin, -margin, margin, margin); + d->viewport->update(d->mapToViewport(viewPortRect)); d->elasticBand = rect; } } @@ -769,7 +772,9 @@ void QListView::mouseReleaseEvent(QMouseEvent *e) QAbstractItemView::mouseReleaseEvent(e); // #### move this implementation into a dynamic class if (d->showElasticBand && d->elasticBand.isValid()) { - d->viewport->update(d->mapToViewport(d->elasticBand)); + const int margin = 2 * style()->pixelMetric(QStyle::PM_DefaultFrameWidth); + const QRect viewPortRect = d->elasticBand.adjusted(-margin, -margin, margin, margin); + d->viewport->update(d->mapToViewport(viewPortRect)); d->elasticBand = QRect(); } } @@ -891,7 +896,7 @@ void QListView::dropEvent(QDropEvent *event) if (!event->isAccepted() && d->dropOn(event, &row, &col, &topIndex)) { const QList<QModelIndex> selIndexes = selectedIndexes(); QList<QPersistentModelIndex> persIndexes; - persIndexes.reserve(selIndexes.count()); + persIndexes.reserve(selIndexes.size()); for (const auto &index : selIndexes) { persIndexes.append(index); @@ -908,9 +913,10 @@ void QListView::dropEvent(QDropEvent *event) int r = row == -1 ? model()->rowCount() : (dropRow.row() >= 0 ? dropRow.row() : row); bool dataMoved = false; - for (int i = 0; i < persIndexes.count(); ++i) { + for (int i = 0; i < persIndexes.size(); ++i) { const QPersistentModelIndex &pIndex = persIndexes.at(i); - if (r != pIndex.row()) { + // only generate a move when not same row or behind itself + if (r != pIndex.row() && r != pIndex.row() + 1) { // try to move (preserves selection) dataMoved |= model()->moveRow(QModelIndex(), pIndex.row(), QModelIndex(), r); if (!dataMoved) // can't move - abort and let QAbstractItemView handle this @@ -1097,7 +1103,7 @@ QModelIndex QListView::indexAt(const QPoint &p) const Q_D(const QListView); QRect rect(p.x() + horizontalOffset(), p.y() + verticalOffset(), 1, 1); const QList<QModelIndex> intersectVector = d->intersectingSet(rect); - QModelIndex index = intersectVector.count() > 0 + QModelIndex index = intersectVector.size() > 0 ? intersectVector.last() : QModelIndex(); if (index.isValid() && visualRect(index).contains(p)) return index; @@ -1621,6 +1627,12 @@ void QListView::setModelColumn(int column) return; d->column = column; d->doDelayedItemsLayout(); +#if QT_CONFIG(accessibility) + if (QAccessible::isActive()) { + QAccessibleTableModelChangeEvent event(this, QAccessibleTableModelChangeEvent::ModelReset); + QAccessible::updateAccessibility(&event); + } +#endif } int QListView::modelColumn() const @@ -1790,7 +1802,7 @@ void QListViewPrivate::prepareItemsLayout() if (q->style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents)) { QStyleOption option; option.initFrom(q); - frameAroundContents = q->style()->pixelMetric(QStyle::PM_DefaultFrameWidth, &option) * 2; + frameAroundContents = q->style()->pixelMetric(QStyle::PM_DefaultFrameWidth, &option, q) * 2; } // maximumViewportSize() already takes scrollbar into account if policy is @@ -2159,7 +2171,7 @@ void QListModeViewBase::dragMoveEvent(QDragMoveEvent *event) QRect rect(p.x() + horizontalOffset(), p.y() + verticalOffset(), 1, 1); rect.adjust(-dd->spacing(), -dd->spacing(), dd->spacing(), dd->spacing()); const QList<QModelIndex> intersectVector = dd->intersectingSet(rect); - QModelIndex index = intersectVector.count() > 0 + QModelIndex index = intersectVector.size() > 0 ? intersectVector.last() : QModelIndex(); dd->hover = index; if (!dd->droppingOnItself(event, index) @@ -2238,7 +2250,7 @@ bool QListModeViewBase::dropOn(QDropEvent *event, int *dropRow, int *dropCol, QM QRect rect(p.x() + horizontalOffset(), p.y() + verticalOffset(), 1, 1); rect.adjust(-dd->spacing(), -dd->spacing(), dd->spacing(), dd->spacing()); const QList<QModelIndex> intersectVector = dd->intersectingSet(rect); - index = intersectVector.count() > 0 + index = intersectVector.size() > 0 ? intersectVector.last() : QModelIndex(); if (!index.isValid()) index = dd->root; @@ -2284,7 +2296,7 @@ void QListModeViewBase::updateVerticalScrollBar(const QSize &step) if (verticalScrollMode() == QAbstractItemView::ScrollPerItem && ((flow() == QListView::TopToBottom && !isWrapping()) || (flow() == QListView::LeftToRight && isWrapping()))) { - const int steps = (flow() == QListView::TopToBottom ? scrollValueMap : segmentPositions).count() - 1; + const int steps = (flow() == QListView::TopToBottom ? scrollValueMap : segmentPositions).size() - 1; if (steps > 0) { const int pageSteps = perItemScrollingPageSteps(viewport()->height(), contentsSize.height(), isWrapping()); verticalScrollBar()->setSingleStep(1); @@ -2305,7 +2317,7 @@ void QListModeViewBase::updateHorizontalScrollBar(const QSize &step) if (horizontalScrollMode() == QAbstractItemView::ScrollPerItem && ((flow() == QListView::TopToBottom && isWrapping()) || (flow() == QListView::LeftToRight && !isWrapping()))) { - int steps = (flow() == QListView::TopToBottom ? segmentPositions : scrollValueMap).count() - 1; + int steps = (flow() == QListView::TopToBottom ? segmentPositions : scrollValueMap).size() - 1; if (steps > 0) { const int pageSteps = perItemScrollingPageSteps(viewport()->width(), contentsSize.width(), isWrapping()); horizontalScrollBar()->setSingleStep(1); @@ -2329,10 +2341,10 @@ int QListModeViewBase::verticalScrollToValue(int index, QListView::ScrollHint hi } else { int scrollBarValue = verticalScrollBar()->value(); int numHidden = 0; - for (const auto &idx : qAsConst(dd->hiddenRows)) + for (const auto &idx : std::as_const(dd->hiddenRows)) if (idx.row() <= scrollBarValue) ++numHidden; - value = qBound(0, scrollValueMap.at(verticalScrollBar()->value()) - numHidden, flowPositions.count() - 1); + value = qBound(0, scrollValueMap.at(verticalScrollBar()->value()) - numHidden, flowPositions.size() - 1); } if (above) hint = QListView::PositionAtTop; @@ -2352,7 +2364,7 @@ int QListModeViewBase::horizontalOffset() const if (horizontalScrollMode() == QAbstractItemView::ScrollPerItem) { if (isWrapping()) { if (flow() == QListView::TopToBottom && !segmentPositions.isEmpty()) { - const int max = segmentPositions.count() - 1; + const int max = segmentPositions.size() - 1; int currentValue = qBound(0, horizontalScrollBar()->value(), max); int position = segmentPositions.at(currentValue); int maximumValue = qBound(0, horizontalScrollBar()->maximum(), max); @@ -2374,13 +2386,13 @@ int QListModeViewBase::verticalOffset() const if (isWrapping()) { if (flow() == QListView::LeftToRight && !segmentPositions.isEmpty()) { int value = verticalScrollBar()->value(); - if (value >= segmentPositions.count()) + if (value >= segmentPositions.size()) return 0; return segmentPositions.at(value) - spacing(); } } else if (flow() == QListView::TopToBottom && !flowPositions.isEmpty()) { int value = verticalScrollBar()->value(); - if (value > scrollValueMap.count()) + if (value > scrollValueMap.size()) return 0; return flowPositions.at(scrollValueMap.at(value)) - spacing(); } @@ -2398,7 +2410,7 @@ int QListModeViewBase::horizontalScrollToValue(int index, QListView::ScrollHint if (scrollValueMap.isEmpty()) value = 0; else - value = qBound(0, scrollValueMap.at(horizontalScrollBar()->value()), flowPositions.count() - 1); + value = qBound(0, scrollValueMap.at(horizontalScrollBar()->value()), flowPositions.size() - 1); if (leftOf) hint = QListView::PositionAtTop; else if (rightOf) @@ -2420,7 +2432,7 @@ void QListModeViewBase::scrollContentsBy(int dx, int dy, bool scrollElasticBand) if (isWrapping()) { if (segmentPositions.isEmpty()) return; - const int max = segmentPositions.count() - 1; + const int max = segmentPositions.size() - 1; if (horizontal && flow() == QListView::TopToBottom && dx != 0) { int currentValue = qBound(0, horizontalValue, max); int previousValue = qBound(0, currentValue + dx, max); @@ -2437,7 +2449,7 @@ void QListModeViewBase::scrollContentsBy(int dx, int dy, bool scrollElasticBand) } else { if (flowPositions.isEmpty()) return; - const int max = scrollValueMap.count() - 1; + const int max = scrollValueMap.size() - 1; if (vertical && flow() == QListView::TopToBottom && dy != 0) { int currentValue = qBound(0, verticalValue, max); int previousValue = qBound(0, currentValue + dy, max); @@ -2465,11 +2477,11 @@ QListViewItem QListModeViewBase::indexToListViewItem(const QModelIndex &index) c { if (flowPositions.isEmpty() || segmentPositions.isEmpty() - || index.row() >= flowPositions.count() - 1) + || index.row() >= flowPositions.size() - 1) return QListViewItem(); const int segment = qBinarySearch<int>(segmentStartRows, index.row(), - 0, segmentStartRows.count() - 1); + 0, segmentStartRows.size() - 1); QStyleOptionViewItem options; @@ -2487,7 +2499,7 @@ QListViewItem QListModeViewBase::indexToListViewItem(const QModelIndex &index) c pos.setY(flowPositions.at(index.row())); pos.setX(segmentPositions.at(segment)); if (isWrapping()) { // make the items as wide as the segment - int right = (segment + 1 >= segmentPositions.count() + int right = (segment + 1 >= segmentPositions.size() ? contentsSize.width() : segmentPositions.at(segment + 1)); cellSize.setWidth(right - pos.x()); @@ -2611,7 +2623,7 @@ void QListModeViewBase::doStaticLayout(const QListViewLayoutInfo &info) deltaSegPosition = 0; } // save the flow position of this item - scrollValueMap.append(flowPositions.count()); + scrollValueMap.append(flowPositions.size()); flowPositions.append(flowPosition); // prepare for the next item deltaSegPosition = qMax(deltaSegHint, deltaSegPosition); @@ -2627,17 +2639,17 @@ void QListModeViewBase::doStaticLayout(const QListViewLayoutInfo &info) // set the contents size QRect rect = info.bounds; if (info.flow == QListView::LeftToRight) { - rect.setRight(segmentPositions.count() == 1 ? flowPosition : info.bounds.right()); + rect.setRight(segmentPositions.size() == 1 ? flowPosition : info.bounds.right()); rect.setBottom(segPosition + deltaSegPosition); } else { // TopToBottom rect.setRight(segPosition + deltaSegPosition); - rect.setBottom(segmentPositions.count() == 1 ? flowPosition : info.bounds.bottom()); + rect.setBottom(segmentPositions.size() == 1 ? flowPosition : info.bounds.bottom()); } contentsSize = QSize(rect.right(), rect.bottom()); // if it is the last batch, save the end of the segments if (info.last == info.max) { segmentExtents.append(flowPosition); - scrollValueMap.append(flowPositions.count()); + scrollValueMap.append(flowPositions.size()); flowPositions.append(flowPosition); segmentPositions.append(info.wrap ? segPosition + deltaSegPosition : INT_MAX); } @@ -2670,10 +2682,10 @@ QList<QModelIndex> QListModeViewBase::intersectingSet(const QRect &area) const flowStartPosition = area.top(); flowEndPosition = area.bottom(); } - if (segmentPositions.count() < 2 || flowPositions.isEmpty()) + if (segmentPositions.size() < 2 || flowPositions.isEmpty()) return ret; // the last segment position is actually the edge of the last segment - const int segLast = segmentPositions.count() - 2; + const int segLast = segmentPositions.size() - 2; int seg = qBinarySearch<int>(segmentPositions, segStartPosition, 0, segLast + 1); for (; seg <= segLast && segmentPositions.at(seg) <= segEndPosition; ++seg) { int first = segmentStartRows.at(seg); @@ -2740,15 +2752,15 @@ int QListModeViewBase::perItemScrollingPageSteps(int length, int bounds, bool wr positions.append(flowPositions.at(itemShown)); } if (positions.isEmpty() || bounds <= length) - return positions.count(); + return positions.size(); if (uniformItemSizes()) { - for (int i = 1; i < positions.count(); ++i) + for (int i = 1; i < positions.size(); ++i) if (positions.at(i) > 0) return length / positions.at(i); return 0; // all items had height 0 } int pageSteps = 0; - int steps = positions.count() - 1; + int steps = positions.size() - 1; int max = qMax(length, bounds); int min = qMin(length, bounds); int pos = min - (max - positions.constLast()); @@ -2810,7 +2822,7 @@ int QListModeViewBase::perItemScrollToValue(int index, int scrollValue, int view // ### wrapped scrolling in the flow direction return flowPositions.at(index + hiddenRowsBefore); // ### always pixel based for now } else if (!segmentStartRows.isEmpty()) { // we are scrolling in the "segment" direction - int segment = qBinarySearch<int>(segmentStartRows, index, 0, segmentStartRows.count() - 1); + int segment = qBinarySearch<int>(segmentStartRows, index, 0, segmentStartRows.size() - 1); int leftSegment = segment; const int rightSegment = leftSegment; const int bottomCoordinate = segmentPositions.at(segment); @@ -2853,7 +2865,7 @@ void QListModeViewBase::clear() void QIconModeViewBase::setPositionForIndex(const QPoint &position, const QModelIndex &index) { - if (index.row() >= items.count()) + if (index.row() >= items.size()) return; const QSize oldContents = contentsSize; qq->update(index); // update old position @@ -2866,7 +2878,7 @@ void QIconModeViewBase::setPositionForIndex(const QPoint &position, const QModel void QIconModeViewBase::appendHiddenRow(int row) { - if (row >= 0 && row < items.count()) //remove item + if (row >= 0 && row < items.size()) //remove item tree.removeLeaf(items.at(row).rect(), row); QCommonListViewBase::appendHiddenRow(row); } @@ -2874,7 +2886,7 @@ void QIconModeViewBase::appendHiddenRow(int row) void QIconModeViewBase::removeHiddenRow(int row) { QCommonListViewBase::removeHiddenRow(row); - if (row >= 0 && row < items.count()) //insert item + if (row >= 0 && row < items.size()) //insert item tree.insertLeaf(items.at(row).rect(), row); } @@ -2885,7 +2897,7 @@ bool QIconModeViewBase::filterStartDrag(Qt::DropActions supportedActions) // plus adding viewitems to the draggedItems list. // We need these items to draw the drag items QModelIndexList indexes = dd->selectionModel->selectedIndexes(); - if (indexes.count() > 0 ) { + if (indexes.size() > 0 ) { if (viewport()->acceptDrops()) { QModelIndexList::ConstIterator it = indexes.constBegin(); for (; it != indexes.constEnd(); ++it) @@ -2990,7 +3002,7 @@ bool QIconModeViewBase::filterDragMoveEvent(QDragMoveEvent *e) if (movement() == QListView::Snap) { QRect rect(snapToGrid(e->position().toPoint() + offset()), gridSize()); const QList<QModelIndex> intersectVector = intersectingSet(rect); - index = intersectVector.count() > 0 ? intersectVector.last() : QModelIndex(); + index = intersectVector.size() > 0 ? intersectVector.last() : QModelIndex(); } else { index = qq->indexAt(e->position().toPoint()); } @@ -3029,7 +3041,7 @@ void QIconModeViewBase::dataChanged(const QModelIndex &topLeft, const QModelInde if (column() >= topLeft.column() && column() <= bottomRight.column()) { QStyleOptionViewItem option; initViewItemOption(&option); - const int bottom = qMin(items.count(), bottomRight.row() + 1); + const int bottom = qMin(items.size(), bottomRight.row() + 1); const bool useItemSize = !dd->grid.isValid(); for (int row = topLeft.row(); row < bottom; ++row) { @@ -3046,11 +3058,11 @@ void QIconModeViewBase::dataChanged(const QModelIndex &topLeft, const QModelInde bool QIconModeViewBase::doBatchedItemLayout(const QListViewLayoutInfo &info, int max) { - if (info.last >= items.count()) { + if (info.last >= items.size()) { //first we create the items QStyleOptionViewItem option; initViewItemOption(&option); - for (int row = items.count(); row <= info.last; ++row) { + for (int row = items.size(); row <= info.last; ++row) { QSize size = itemSize(option, modelIndex(row)); QListViewItem item(QRect(0, 0, size.width(), size.height()), row); // default pos items.append(item); @@ -3062,7 +3074,7 @@ bool QIconModeViewBase::doBatchedItemLayout(const QListViewLayoutInfo &info, int QListViewItem QIconModeViewBase::indexToListViewItem(const QModelIndex &index) const { - if (index.isValid() && index.row() < items.count()) + if (index.isValid() && index.row() < items.size()) return items.at(index.row()); return QListViewItem(); } @@ -3140,8 +3152,8 @@ void QIconModeViewBase::doDynamicLayout(const QListViewLayoutInfo &info) segPosition = topLeft.x(); } - if (moved.count() != items.count()) - moved.resize(items.count()); + if (moved.size() != items.size()) + moved.resize(items.size()); QRect rect(QPoint(), topLeft); QListViewItem *item = nullptr; @@ -3269,15 +3281,15 @@ int QIconModeViewBase::itemIndex(const QListViewItem &item) const if (!item.isValid()) return -1; int i = item.indexHint; - if (i < items.count()) { + if (i < items.size()) { if (items.at(i) == item) return i; } else { - i = items.count() - 1; + i = items.size() - 1; } int j = i; - int c = items.count(); + int c = items.size(); bool a = true; bool b = true; @@ -3305,9 +3317,9 @@ void QIconModeViewBase::addLeaf(QList<int> &leaf, const QRect &area, uint visite { QListViewItem *vi; QIconModeViewBase *_this = static_cast<QIconModeViewBase *>(data.ptr); - for (int i = 0; i < leaf.count(); ++i) { + for (int i = 0; i < leaf.size(); ++i) { int idx = leaf.at(i); - if (idx < 0 || idx >= _this->items.count()) + if (idx < 0 || idx >= _this->items.size()) continue; vi = &_this->items[idx]; Q_ASSERT(vi); @@ -3335,8 +3347,8 @@ void QIconModeViewBase::moveItem(int index, const QPoint &dest) contentsSize = (QRect(QPoint(0, 0), contentsSize)|QRect(dest, rect.size())).size(); // mark the item as moved - if (moved.count() != items.count()) - moved.resize(items.count()); + if (moved.size() != items.size()) + moved.resize(items.size()); moved.setBit(index, true); } @@ -3388,7 +3400,7 @@ void QIconModeViewBase::clear() void QIconModeViewBase::updateContentsSize() { QRect bounding; - for (int i = 0; i < items.count(); ++i) + for (int i = 0; i < items.size(); ++i) bounding |= items.at(i).rect(); contentsSize = bounding.size(); } @@ -3398,9 +3410,10 @@ void QIconModeViewBase::updateContentsSize() */ void QListView::currentChanged(const QModelIndex ¤t, const QModelIndex &previous) { + QAbstractItemView::currentChanged(current, previous); #if QT_CONFIG(accessibility) if (QAccessible::isActive()) { - if (current.isValid()) { + if (current.isValid() && hasFocus()) { int entry = visualIndex(current); QAccessibleEvent event(this, QAccessible::Focus); event.setChild(entry); @@ -3408,7 +3421,6 @@ void QListView::currentChanged(const QModelIndex ¤t, const QModelIndex &pr } } #endif - QAbstractItemView::currentChanged(current, previous); } /*! @@ -3445,7 +3457,7 @@ int QListView::visualIndex(const QModelIndex &index) const d->executePostedLayout(); QListViewItem itm = d->indexToListViewItem(index); int visualIndex = d->commonListView->itemIndex(itm); - for (const auto &idx : qAsConst(d->hiddenRows)) { + for (const auto &idx : std::as_const(d->hiddenRows)) { if (idx.row() <= index.row()) --visualIndex; } |