+ else // selection with click + keyboard keys
+ {
+ QModelIndex topLeftIndex = indexAt(QPoint(rect.topLeft().x(),
+ rect.topLeft().y()));
+ QModelIndex bottomRightIndex = indexAt(QPoint(rect.bottomRight().x(),
+ rect.bottomRight().y()));
+
+ // keyboard selection comes "upside down". Let's normalize it
+ if (topLeftIndex.row() > bottomRightIndex.row())
+ {
+ QModelIndex auxIndex = topLeftIndex;
+ topLeftIndex = bottomRightIndex;
+ bottomRightIndex = auxIndex;
+ }
+
+ int viewportWidth = viewport()->width() - spacing();
+ int itemWidth;
+
+ if (gridSize().isEmpty())
+ {
+ itemWidth = d->biggestItemSize.width();
+ }
+ else
+ {
+ itemWidth = gridSize().width();
+ }
+
+ int itemWidthPlusSeparation = spacing() + itemWidth;
+ int elementsPerRow = viewportWidth / itemWidthPlusSeparation;
+ if (!elementsPerRow)
+ elementsPerRow++;
+
+ QModelIndexList theoricDirty(dirtyIndexes);
+ dirtyIndexes.clear();
+ int first = model()->rowCount();
+ int last = 0;
+
+ foreach (const QModelIndex &index, theoricDirty)
+ {
+ if ((index.row() < first) &&
+ ((((topLeftIndex.row() / elementsPerRow) == (index.row() / elementsPerRow)) &&
+ ((topLeftIndex.row() % elementsPerRow) <= (index.row() % elementsPerRow))) ||
+ (topLeftIndex.row() / elementsPerRow) != (index.row() / elementsPerRow)))
+ {
+ first = index.row();
+ topLeft = index;
+ }
+
+ if ((index.row() > last) &&
+ ((((bottomRightIndex.row() / elementsPerRow) == (index.row() / elementsPerRow)) &&
+ ((bottomRightIndex.row() % elementsPerRow) >= (index.row() % elementsPerRow))) ||
+ (bottomRightIndex.row() / elementsPerRow) != (index.row() / elementsPerRow)))
+ {
+ last = index.row();
+ bottomRight = index;
+ }
+ }