summaryrefslogtreecommitdiffstats
path: root/src/widgets/itemviews
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2015-11-30 12:19:31 +0100
committerMarc Mutz <marc.mutz@kdab.com>2015-12-16 17:41:50 +0000
commit0516487237145ad41b2fd13ecb5f63ba4325c02f (patch)
tree8dbc353c28857742882610cc5d810c8c64052f0f /src/widgets/itemviews
parentb69751863472b186aaad08db6b9b08de81e95dc4 (diff)
QtWidgets: replace some index-based for loops with C++11 range-for
This needs to be handled a bit carefully, because Qt containers will detach upon being iteratoed over using range-for. In the cases of this patch, that cannot happen, because all containers are marked as const (either by this patch or before). Separate patches will deal with other situations. Apart from being more readable, range-for loops are also the most efficient for loop. This patch shaves almost 2K of text size off an optimized Linux AMD64 GCC 4.9 build. Change-Id: I53810c7b25420b4fd449d20c90c07503c5e76a66 Reviewed-by: Edward Welbourne <edward.welbourne@theqtcompany.com> Reviewed-by: Lars Knoll <lars.knoll@theqtcompany.com>
Diffstat (limited to 'src/widgets/itemviews')
-rw-r--r--src/widgets/itemviews/qabstractitemview.cpp3
-rw-r--r--src/widgets/itemviews/qabstractitemview_p.h5
-rw-r--r--src/widgets/itemviews/qdirmodel.cpp4
-rw-r--r--src/widgets/itemviews/qheaderview.cpp6
-rw-r--r--src/widgets/itemviews/qheaderview_p.h4
-rw-r--r--src/widgets/itemviews/qlistview.cpp18
-rw-r--r--src/widgets/itemviews/qtableview.cpp21
-rw-r--r--src/widgets/itemviews/qtablewidget.cpp27
-rw-r--r--src/widgets/itemviews/qtreeview.cpp3
-rw-r--r--src/widgets/itemviews/qtreewidget.cpp42
10 files changed, 61 insertions, 72 deletions
diff --git a/src/widgets/itemviews/qabstractitemview.cpp b/src/widgets/itemviews/qabstractitemview.cpp
index fe98fae17a..cbf734203a 100644
--- a/src/widgets/itemviews/qabstractitemview.cpp
+++ b/src/widgets/itemviews/qabstractitemview.cpp
@@ -4351,8 +4351,7 @@ QItemViewPaintPairs QAbstractItemViewPrivate::draggablePaintPairs(const QModelIn
QRect &rect = *r;
const QRect viewportRect = viewport->rect();
QItemViewPaintPairs ret;
- for (int i = 0; i < indexes.count(); ++i) {
- const QModelIndex &index = indexes.at(i);
+ for (const auto &index : indexes) {
const QRect current = q->visualRect(index);
if (current.intersects(viewportRect)) {
QItemViewPaintPair p = { current, index };
diff --git a/src/widgets/itemviews/qabstractitemview_p.h b/src/widgets/itemviews/qabstractitemview_p.h
index cb74586fab..2b28d8db84 100644
--- a/src/widgets/itemviews/qabstractitemview_p.h
+++ b/src/widgets/itemviews/qabstractitemview_p.h
@@ -177,9 +177,10 @@ public:
// Whether the data can actually be dropped will be checked in drag move.
if (event->type() == QEvent::DragEnter && (event->dropAction() & model->supportedDropActions())) {
const QStringList modelTypes = model->mimeTypes();
- for (int i = 0; i < modelTypes.count(); ++i)
- if (mime->hasFormat(modelTypes.at(i)))
+ for (const auto &modelType : modelTypes) {
+ if (mime->hasFormat(modelType))
return true;
+ }
}
QModelIndex index;
diff --git a/src/widgets/itemviews/qdirmodel.cpp b/src/widgets/itemviews/qdirmodel.cpp
index daf00e9362..f18233b64c 100644
--- a/src/widgets/itemviews/qdirmodel.cpp
+++ b/src/widgets/itemviews/qdirmodel.cpp
@@ -169,8 +169,8 @@ void QDirModelPrivate::invalidate()
const QDirNode *current = nodes.pop();
current->stat = false;
const QVector<QDirNode> &children = current->children;
- for (int i = 0; i < children.count(); ++i)
- nodes.push(&children.at(i));
+ for (const auto &child : children)
+ nodes.push(&child);
}
}
diff --git a/src/widgets/itemviews/qheaderview.cpp b/src/widgets/itemviews/qheaderview.cpp
index 4cb28d0804..496d36015e 100644
--- a/src/widgets/itemviews/qheaderview.cpp
+++ b/src/widgets/itemviews/qheaderview.cpp
@@ -2958,8 +2958,7 @@ QRegion QHeaderView::visualRegionForSelection(const QItemSelection &selection) c
int right = 0;
int rangeLeft, rangeRight;
- for (int i = 0; i < selection.count(); ++i) {
- QItemSelectionRange r = selection.at(i);
+ for (const auto &r : selection) {
if (r.parent().isValid() || !r.isValid())
continue; // we only know about toplevel items and we don't want invalid ranges
// FIXME an item inside the range may be the leftmost or rightmost
@@ -2992,8 +2991,7 @@ QRegion QHeaderView::visualRegionForSelection(const QItemSelection &selection) c
int bottom = 0;
int rangeTop, rangeBottom;
- for (int i = 0; i < selection.count(); ++i) {
- QItemSelectionRange r = selection.at(i);
+ for (const auto &r : selection) {
if (r.parent().isValid() || !r.isValid())
continue; // we only know about toplevel items
// FIXME an item inside the range may be the leftmost or rightmost
diff --git a/src/widgets/itemviews/qheaderview_p.h b/src/widgets/itemviews/qheaderview_p.h
index 7f92d2a81b..60542e297a 100644
--- a/src/widgets/itemviews/qheaderview_p.h
+++ b/src/widgets/itemviews/qheaderview_p.h
@@ -327,8 +327,8 @@ public:
inline int headerLength() const { // for debugging
int len = 0;
- for (int i = 0; i < sectionItems.count(); ++i)
- len += sectionItems.at(i).size;
+ for (const auto &section : sectionItems)
+ len += section.size;
return len;
}
diff --git a/src/widgets/itemviews/qlistview.cpp b/src/widgets/itemviews/qlistview.cpp
index 2dbbd211c0..7cd8e890db 100644
--- a/src/widgets/itemviews/qlistview.cpp
+++ b/src/widgets/itemviews/qlistview.cpp
@@ -651,8 +651,7 @@ QItemViewPaintPairs QListViewPrivate::draggablePaintPairs(const QModelIndexList
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);
+ for (const auto &index : indexes) {
if (visibleIndexes.contains(index)) {
const QRect current = q->visualRect(index);
QItemViewPaintPair p = { current, index };
@@ -1398,16 +1397,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));
@@ -2771,9 +2770,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;
diff --git a/src/widgets/itemviews/qtableview.cpp b/src/widgets/itemviews/qtableview.cpp
index 2e88310b2f..88090f1e45 100644
--- a/src/widgets/itemviews/qtableview.cpp
+++ b/src/widgets/itemviews/qtableview.cpp
@@ -1370,9 +1370,6 @@ void QTableView::paintEvent(QPaintEvent *event)
uint x = horizontalHeader->length() - horizontalHeader->offset() - (rightToLeft ? 0 : 1);
uint y = verticalHeader->length() - verticalHeader->offset() - 1;
- const QRegion region = event->region().translated(offset);
- const QVector<QRect> rects = region.rects();
-
//firstVisualRow is the visual index of the first visible row. lastVisualRow is the visual index of the last visible Row.
//same goes for ...VisualColumn
int firstVisualRow = qMax(verticalHeader->visualIndexAt(0),0);
@@ -1391,13 +1388,15 @@ void QTableView::paintEvent(QPaintEvent *event)
QBitArray drawn((lastVisualRow - firstVisualRow + 1) * (lastVisualColumn - firstVisualColumn + 1));
+ const QRegion region = event->region().translated(offset);
+
if (d->hasSpans()) {
d->drawAndClipSpans(region, &painter, option, &drawn,
firstVisualRow, lastVisualRow, firstVisualColumn, lastVisualColumn);
}
- for (int i = 0; i < rects.size(); ++i) {
- QRect dirtyArea = rects.at(i);
+ const QVector<QRect> rects = region.rects();
+ for (auto dirtyArea : rects) {
dirtyArea.setBottom(qMin(dirtyArea.bottom(), int(y)));
if (rightToLeft) {
dirtyArea.setLeft(qMax(dirtyArea.left(), d->viewport->width() - int(x)));
@@ -1952,8 +1951,7 @@ QRegion QTableView::visualRegionForSelection(const QItemSelection &selection) co
bool horizontalMoved = horizontalHeader()->sectionsMoved();
if ((verticalMoved && horizontalMoved) || (d->hasSpans() && (verticalMoved || horizontalMoved))) {
- for (int i = 0; i < selection.count(); ++i) {
- QItemSelectionRange range = selection.at(i);
+ for (const auto &range : selection) {
if (range.parent() != d->root || !range.isValid())
continue;
for (int r = range.top(); r <= range.bottom(); ++r)
@@ -1964,8 +1962,7 @@ QRegion QTableView::visualRegionForSelection(const QItemSelection &selection) co
}
}
} else if (horizontalMoved) {
- for (int i = 0; i < selection.count(); ++i) {
- QItemSelectionRange range = selection.at(i);
+ for (const auto &range : selection) {
if (range.parent() != d->root || !range.isValid())
continue;
int top = rowViewportPosition(range.top());
@@ -1980,8 +1977,7 @@ QRegion QTableView::visualRegionForSelection(const QItemSelection &selection) co
}
}
} else if (verticalMoved) {
- for (int i = 0; i < selection.count(); ++i) {
- QItemSelectionRange range = selection.at(i);
+ for (const auto &range : selection) {
if (range.parent() != d->root || !range.isValid())
continue;
int left = columnViewportPosition(range.left());
@@ -1997,8 +1993,7 @@ QRegion QTableView::visualRegionForSelection(const QItemSelection &selection) co
}
} else { // nothing moved
const int gridAdjust = showGrid() ? 1 : 0;
- for (int i = 0; i < selection.count(); ++i) {
- QItemSelectionRange range = selection.at(i);
+ for (auto range : selection) {
if (range.parent() != d->root || !range.isValid())
continue;
d->trimHiddenSelections(&range);
diff --git a/src/widgets/itemviews/qtablewidget.cpp b/src/widgets/itemviews/qtablewidget.cpp
index 568b52f901..6238835f04 100644
--- a/src/widgets/itemviews/qtablewidget.cpp
+++ b/src/widgets/itemviews/qtablewidget.cpp
@@ -1388,9 +1388,10 @@ void QTableWidgetItem::setData(int role, const QVariant &value)
QVariant QTableWidgetItem::data(int role) const
{
role = (role == Qt::EditRole ? Qt::DisplayRole : role);
- for (int i = 0; i < values.count(); ++i)
- if (values.at(i).role == role)
- return values.at(i).value;
+ for (const auto &value : values) {
+ if (value.role == role)
+ return value.value;
+ }
return QVariant();
}
@@ -2359,10 +2360,9 @@ QList<QTableWidgetSelectionRange> QTableWidget::selectedRanges() const
QList<QTableWidgetItem*> QTableWidget::selectedItems() const
{
Q_D(const QTableWidget);
- QModelIndexList indexes = selectionModel()->selectedIndexes();
+ const QModelIndexList indexes = selectionModel()->selectedIndexes();
QList<QTableWidgetItem*> items;
- for (int i = 0; i < indexes.count(); ++i) {
- QModelIndex index = indexes.at(i);
+ for (const auto &index : indexes) {
if (isIndexHidden(index))
continue;
QTableWidgetItem *item = d->tableModel()->item(index);
@@ -2682,22 +2682,21 @@ void QTableWidget::dropEvent(QDropEvent *event) {
int col = -1;
int row = -1;
if (d->dropOn(event, &row, &col, &topIndex)) {
- QModelIndexList indexes = selectedIndexes();
+ const QModelIndexList indexes = selectedIndexes();
int top = INT_MAX;
int left = INT_MAX;
- for (int i = 0; i < indexes.count(); ++i) {
- top = qMin(indexes.at(i).row(), top);
- left = qMin(indexes.at(i).column(), left);
+ for (const auto &index : indexes) {
+ top = qMin(index.row(), top);
+ left = qMin(index.column(), left);
}
QList<QTableWidgetItem *> taken;
const int indexesCount = indexes.count();
taken.reserve(indexesCount);
- for (int i = 0; i < indexesCount; ++i)
- taken.append(takeItem(indexes.at(i).row(), indexes.at(i).column()));
+ for (const auto &index : indexes)
+ taken.append(takeItem(index.row(), index.column()));
- for (int i = 0; i < indexes.count(); ++i) {
- QModelIndex index = indexes.at(i);
+ for (const auto &index : indexes) {
int r = index.row() - top + topIndex.row();
int c = index.column() - left + topIndex.column();
setItem(r, c, taken.takeFirst());
diff --git a/src/widgets/itemviews/qtreeview.cpp b/src/widgets/itemviews/qtreeview.cpp
index 41f1ca7502..b11c54ffb8 100644
--- a/src/widgets/itemviews/qtreeview.cpp
+++ b/src/widgets/itemviews/qtreeview.cpp
@@ -2393,8 +2393,7 @@ QRegion QTreeView::visualRegionForSelection(const QItemSelection &selection) con
QRegion selectionRegion;
const QRect &viewportRect = d->viewport->rect();
- for (int i = 0; i < selection.count(); ++i) {
- QItemSelectionRange range = selection.at(i);
+ for (const auto &range : selection) {
if (!range.isValid())
continue;
QModelIndex parent = range.parent();
diff --git a/src/widgets/itemviews/qtreewidget.cpp b/src/widgets/itemviews/qtreewidget.cpp
index 6d3162d387..9dbf7df5fb 100644
--- a/src/widgets/itemviews/qtreewidget.cpp
+++ b/src/widgets/itemviews/qtreewidget.cpp
@@ -725,9 +725,9 @@ QMimeData *QTreeModel::internalMimeData() const
QMimeData *QTreeModel::mimeData(const QModelIndexList &indexes) const
{
QList<QTreeWidgetItem*> items;
- for (int i = 0; i < indexes.count(); ++i) {
- if (indexes.at(i).column() == 0) // only one item per row
- items << item(indexes.at(i));
+ for (const auto &index : indexes) {
+ if (index.column() == 0) // only one item per row
+ items << item(index);
}
// cachedIndexes is a little hack to avoid copying from QModelIndexList to
@@ -1735,7 +1735,7 @@ void QTreeWidgetItem::setData(int column, int role, const QVariant &value)
default:
if (column < values.count()) {
bool found = false;
- QVector<QWidgetItemData> column_values = values.at(column);
+ const QVector<QWidgetItemData> column_values = values.at(column);
for (int i = 0; i < column_values.count(); ++i) {
if (column_values.at(i).role == role) {
if (column_values.at(i).value == value)
@@ -1785,9 +1785,10 @@ QVariant QTreeWidgetItem::data(int column, int role) const
default:
if (column >= 0 && column < values.size()) {
const QVector<QWidgetItemData> &column_values = values.at(column);
- for (int i = 0; i < column_values.count(); ++i)
- if (column_values.at(i).role == role)
- return column_values.at(i).value;
+ for (const auto &column_value : column_values) {
+ if (column_value.role == role)
+ return column_value.value;
+ }
}
}
return QVariant();
@@ -2136,8 +2137,8 @@ QVariant QTreeWidgetItem::childrenCheckState(int column) const
return QVariant();
bool checkedChildren = false;
bool uncheckedChildren = false;
- for (int i = 0; i < children.count(); ++i) {
- QVariant value = children.at(i)->data(column, Qt::CheckStateRole);
+ for (const auto *child : children) {
+ QVariant value = child->data(column, Qt::CheckStateRole);
if (!value.isValid())
return QVariant();
@@ -3018,13 +3019,13 @@ void QTreeWidget::setItemSelected(const QTreeWidgetItem *item, bool select)
QList<QTreeWidgetItem*> QTreeWidget::selectedItems() const
{
Q_D(const QTreeWidget);
- QModelIndexList indexes = selectionModel()->selectedIndexes();
+ const QModelIndexList indexes = selectionModel()->selectedIndexes();
QList<QTreeWidgetItem*> items;
items.reserve(indexes.count());
QSet<QTreeWidgetItem *> seen;
seen.reserve(indexes.count());
- for (int i = 0; i < indexes.count(); ++i) {
- QTreeWidgetItem *item = d->item(indexes.at(i));
+ for (const auto &index : indexes) {
+ QTreeWidgetItem *item = d->item(index);
if (isItemHidden(item) || seen.contains(item))
continue;
seen.insert(item);
@@ -3278,8 +3279,7 @@ QMimeData *QTreeWidget::mimeData(const QList<QTreeWidgetItem*> items) const
Q_D(const QTreeWidget);
if (d->treeModel()->cachedIndexes.isEmpty()) {
QList<QModelIndex> indexes;
- for (int i = 0; i < items.count(); ++i) {
- QTreeWidgetItem *item = items.at(i);
+ for (const auto *item : items) {
if (Q_UNLIKELY(!item)) {
qWarning("QTreeWidget::mimeData: Null-item passed");
return 0;
@@ -3382,12 +3382,12 @@ void QTreeWidget::dropEvent(QDropEvent *event) {
int col = -1;
int row = -1;
if (d->dropOn(event, &row, &col, &topIndex)) {
- QList<QModelIndex> idxs = selectedIndexes();
+ const QList<QModelIndex> idxs = selectedIndexes();
QList<QPersistentModelIndex> indexes;
const int indexesCount = idxs.count();
indexes.reserve(indexesCount);
- for (int i = 0; i < indexesCount; i++)
- indexes.append(idxs.at(i));
+ for (const auto &idx : idxs)
+ indexes.append(idx);
if (indexes.contains(topIndex))
return;
@@ -3397,12 +3397,12 @@ void QTreeWidget::dropEvent(QDropEvent *event) {
// Remove the items
QList<QTreeWidgetItem *> taken;
- for (int i = 0; i < indexes.count(); ++i) {
- QTreeWidgetItem *parent = itemFromIndex(indexes.at(i));
+ for (const auto &index : indexes) {
+ QTreeWidgetItem *parent = itemFromIndex(index);
if (!parent || !parent->parent()) {
- taken.append(takeTopLevelItem(indexes.at(i).row()));
+ taken.append(takeTopLevelItem(index.row()));
} else {
- taken.append(parent->parent()->takeChild(indexes.at(i).row()));
+ taken.append(parent->parent()->takeChild(index.row()));
}
}