summaryrefslogtreecommitdiffstats
path: root/src/widgets/itemviews/qlistview.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/widgets/itemviews/qlistview.cpp')
-rw-r--r--src/widgets/itemviews/qlistview.cpp104
1 files changed, 57 insertions, 47 deletions
diff --git a/src/widgets/itemviews/qlistview.cpp b/src/widgets/itemviews/qlistview.cpp
index 814f0f4985..3303aac6ed 100644
--- a/src/widgets/itemviews/qlistview.cpp
+++ b/src/widgets/itemviews/qlistview.cpp
@@ -1,32 +1,38 @@
/****************************************************************************
**
-** Copyright (C) 2015 The Qt Company Ltd.
+** Copyright (C) 2016 The Qt Company Ltd.
** Copyright (C) 2013 Samuel Gaist <samuel.gaist@deltech.ch>
-** Contact: http://www.qt.io/licensing/
+** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtWidgets module of the Qt Toolkit.
**
-** $QT_BEGIN_LICENSE:LGPL21$
+** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see http://www.qt.io/terms-conditions. For further
-** information use the contact form at http://www.qt.io/contact-us.
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 2.1 or version 3 as published by the Free
-** Software Foundation and appearing in the file LICENSE.LGPLv21 and
-** LICENSE.LGPLv3 included in the packaging of this file. Please review the
-** following information to ensure the GNU Lesser General Public License
-** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
-** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
-** As a special exception, The Qt Company gives you certain additional
-** rights. These rights are described in The Qt Company LGPL Exception
-** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-2.0.html and
+** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
@@ -46,11 +52,14 @@
#include <qscrollbar.h>
#include <qrubberband.h>
#include <private/qlistview_p.h>
+#include <private/qscrollbar_p.h>
#include <qdebug.h>
#ifndef QT_NO_ACCESSIBILITY
#include <qaccessible.h>
#endif
+#include <algorithm>
+
QT_BEGIN_NAMESPACE
extern bool qt_sendSpontaneousEvent(QObject *receiver, QEvent *event);
@@ -392,7 +401,7 @@ int QListView::spacing() const
void QListView::setBatchSize(int batchSize)
{
Q_D(QListView);
- if (batchSize <= 0) {
+ if (Q_UNLIKELY(batchSize <= 0)) {
qWarning("Invalid batchSize (%d)", batchSize);
return;
}
@@ -649,12 +658,13 @@ QItemViewPaintPairs QListViewPrivate::draggablePaintPairs(const QModelIndexList
QRect &rect = *r;
const QRect viewportRect = viewport->rect();
QItemViewPaintPairs ret;
- const QSet<QModelIndex> visibleIndexes = intersectingSet(viewportRect.translated(q->horizontalOffset(), q->verticalOffset())).toList().toSet();
- for (int i = 0; i < indexes.count(); ++i) {
- const QModelIndex &index = indexes.at(i);
- if (visibleIndexes.contains(index)) {
+ QVector<QModelIndex> visibleIndexes = intersectingSet(viewportRect.translated(q->horizontalOffset(), q->verticalOffset()));
+ std::sort(visibleIndexes.begin(), visibleIndexes.end());
+ for (const auto &index : indexes) {
+ if (std::binary_search(visibleIndexes.cbegin(), visibleIndexes.cend(), index)) {
const QRect current = q->visualRect(index);
- ret += qMakePair(current, index);
+ QItemViewPaintPair p = { current, index };
+ ret += p;
rect |= current;
}
}
@@ -812,7 +822,7 @@ void QListView::wheelEvent(QWheelEvent *e)
QPoint pixelDelta(e->pixelDelta().y(), e->pixelDelta().x());
QPoint angleDelta(e->angleDelta().y(), e->angleDelta().x());
QWheelEvent hwe(e->pos(), e->globalPos(), pixelDelta, angleDelta, e->delta(),
- Qt::Horizontal, e->buttons(), e->modifiers(), e->phase(), e->source());
+ Qt::Horizontal, e->buttons(), e->modifiers(), e->phase(), e->source(), e->inverted());
if (e->spontaneous())
qt_sendSpontaneousEvent(d->hbar, &hwe);
else
@@ -1005,10 +1015,7 @@ void QListView::paintEvent(QPaintEvent *e)
if (viewState == EditingState)
option.state |= QStyle::State_Editing;
}
- if (*it == hover)
- option.state |= QStyle::State_MouseOver;
- else
- option.state &= ~QStyle::State_MouseOver;
+ option.state.setFlag(QStyle::State_MouseOver, *it == hover);
if (alternate) {
int row = (*it).row();
@@ -1023,11 +1030,7 @@ void QListView::paintEvent(QPaintEvent *e)
alternateBase = (row & 1) != 0;
}
}
- if (alternateBase) {
- option.features |= QStyleOptionViewItem::Alternate;
- } else {
- option.features &= ~QStyleOptionViewItem::Alternate;
- }
+ option.features.setFlag(QStyleOptionViewItem::Alternate, alternateBase);
// draw background of the item (only alternate row). rest of the background
// is provided by the delegate
@@ -1396,16 +1399,16 @@ QRegion QListView::visualRegionForSelection(const QItemSelection &selection) con
int c = d->column;
QRegion selectionRegion;
const QRect &viewportRect = d->viewport->rect();
- for (int i = 0; i < selection.count(); ++i) {
- if (!selection.at(i).isValid())
+ for (const auto &elem : selection) {
+ if (!elem.isValid())
continue;
- QModelIndex parent = selection.at(i).topLeft().parent();
+ QModelIndex parent = elem.topLeft().parent();
//we only display the children of the root in a listview
//we're not interested in the other model indexes
if (parent != d->root)
continue;
- int t = selection.at(i).topLeft().row();
- int b = selection.at(i).bottomRight().row();
+ int t = elem.topLeft().row();
+ int b = elem.bottomRight().row();
if (d->viewMode == IconMode || d->isWrapping()) { // in non-static mode, we have to go through all selected items
for (int r = t; r <= b; ++r) {
const QRect &rect = visualRect(d->model->index(r, c, parent));
@@ -1437,13 +1440,11 @@ QModelIndexList QListView::selectedIndexes() const
return QModelIndexList();
QModelIndexList viewSelected = d->selectionModel->selectedIndexes();
- for (int i = 0; i < viewSelected.count();) {
- const QModelIndex &index = viewSelected.at(i);
- if (!isIndexHidden(index) && index.parent() == d->root && index.column() == d->column)
- ++i;
- else
- viewSelected.removeAt(i);
- }
+ auto ignorable = [this, d](const QModelIndex &index) {
+ return index.column() != d->column || index.parent() != d->root || isIndexHidden(index);
+ };
+ viewSelected.erase(std::remove_if(viewSelected.begin(), viewSelected.end(), ignorable),
+ viewSelected.end());
return viewSelected;
}
@@ -1842,6 +1843,16 @@ bool QListViewPrivate::dropOn(QDropEvent *event, int *dropRow, int *dropCol, QMo
}
#endif
+void QListViewPrivate::removeCurrentAndDisabled(QVector<QModelIndex> *indexes, const QModelIndex &current) const
+{
+ auto isCurrentOrDisabled = [=](const QModelIndex &index) {
+ return !isIndexEnabled(index) || index == current;
+ };
+ indexes->erase(std::remove_if(indexes->begin(), indexes->end(),
+ isCurrentOrDisabled),
+ indexes->end());
+}
+
/*
* Common ListView Implementation
*/
@@ -1867,7 +1878,7 @@ void QCommonListViewBase::paintDragDrop(QPainter *painter)
void QCommonListViewBase::updateHorizontalScrollBar(const QSize &step)
{
- horizontalScrollBar()->setSingleStep(step.width() + spacing());
+ horizontalScrollBar()->d_func()->itemviewChangeSingleStep(step.width() + spacing());
horizontalScrollBar()->setPageStep(viewport()->width());
// If both scroll bars are set to auto, we might end up in a situation with enough space
@@ -1897,7 +1908,7 @@ void QCommonListViewBase::updateHorizontalScrollBar(const QSize &step)
void QCommonListViewBase::updateVerticalScrollBar(const QSize &step)
{
- verticalScrollBar()->setSingleStep(step.height() + spacing());
+ verticalScrollBar()->d_func()->itemviewChangeSingleStep(step.height() + spacing());
verticalScrollBar()->setPageStep(viewport()->height());
// If both scroll bars are set to auto, we might end up in a situation with enough space
@@ -2774,9 +2785,8 @@ bool QIconModeViewBase::filterDropEvent(QDropEvent *e)
}
QPoint start = dd->pressedPosition;
QPoint delta = (dd->movement == QListView::Snap ? snapToGrid(end) - snapToGrid(start) : end - start);
- QList<QModelIndex> indexes = dd->selectionModel->selectedIndexes();
- for (int i = 0; i < indexes.count(); ++i) {
- QModelIndex index = indexes.at(i);
+ const QList<QModelIndex> indexes = dd->selectionModel->selectedIndexes();
+ for (const auto &index : indexes) {
QRect rect = dd->rectForIndex(index);
viewport()->update(dd->mapToViewport(rect, false));
QPoint dest = rect.topLeft() + delta;