X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/1ec3860a264df856b206336f412e95ada4fb524a..737b74aa294e05cebc2cd1e48a4fa66cf62f222e:/src/kcategorizedview.cpp diff --git a/src/kcategorizedview.cpp b/src/kcategorizedview.cpp index c062b12b3..152573214 100644 --- a/src/kcategorizedview.cpp +++ b/src/kcategorizedview.cpp @@ -39,6 +39,11 @@ // Qt 4.4, so that this workaround can be skipped. #define DOLPHIN_DRAGANDDROP +// By defining KDE_WORKAROUND_FOR_QT_VIEW_BUG we save the selection being held +// before mousePressEvent happens. On Qt there is something clearing the last +// selection made, what make it impossible to save our very last selection. +#define KDE_WORKAROUND_FOR_QT_VIEW_BUG + KCategorizedView::Private::Private(KCategorizedView *listView) : listView(listView) , categoryDrawer(0) @@ -849,65 +854,122 @@ void KCategorizedView::setSelection(const QRect &rect, if (flags & QItemSelectionModel::Clear) { - if (!rect.intersects(d->categoryVisualRect(d->hoveredCategory))) - { - d->lastSelection = QItemSelection(); - } + selectionModel()->clear(); } - selectionModel()->clear(); - QModelIndexList dirtyIndexes = d->intersectionSet(rect); + // no items affected, just leave if (!dirtyIndexes.count()) { - if (!d->lastSelection.isEmpty() && - (rect.intersects(d->categoryVisualRect(d->hoveredCategory)) || d->mouseButtonPressed)) - { - selectionModel()->select(d->lastSelection, flags); - } + selectionModel()->select(d->lastSelection, flags); + + d->lastSelection.clear(); return; } - QItemSelection selection; + d->lastSelection.clear(); - if (!d->mouseButtonPressed) + if (!(flags & QItemSelectionModel::Current)) { - selection = QItemSelection(dirtyIndexes[0], dirtyIndexes[0]); - d->currentViewIndex = dirtyIndexes[0]; + Q_ASSERT(dirtyIndexes.count() == 1); + + selectionModel()->select(dirtyIndexes[0], flags); + + return; } - else + + QModelIndex topLeft; + QModelIndex bottomRight; + + if (d->mouseButtonPressed) // selection with click + drag { - QModelIndex first = dirtyIndexes[0]; - QModelIndex last; + QModelIndex prev = dirtyIndexes[0]; + QModelIndex first = prev; foreach (const QModelIndex &index, dirtyIndexes) { - if (last.isValid() && last.row() + 1 != index.row()) - { - QItemSelectionRange range(first, last); - - selection << range; + if ((index.row() - prev.row()) > 1) { + d->lastSelection << QItemSelectionRange(first, prev); first = index; } - last = index; + prev = index; } - if (last.isValid()) - selection << QItemSelectionRange(first, last); - } + d->lastSelection << QItemSelectionRange(first, prev); - if (d->lastSelection.count()) + selectionModel()->select(d->lastSelection, flags); + } + else // selection with click + keyboard keys { - if ((selection.count() == 1) && (selection[0].indexes().count() == 1)) - selection.merge(d->lastSelection, flags); + 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 - selection.merge(d->lastSelection, QItemSelectionModel::Select); - } + { + 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; + } + } + + for (int i = first; i <= last; i++) + { + dirtyIndexes << model()->index(i, theoricDirty[0].column(), theoricDirty[0].parent()); + } - selectionModel()->select(selection, flags); + // our current selection will result modified + d->lastSelection = QItemSelection(topLeft, bottomRight); + + selectionModel()->select(d->lastSelection, flags); + } } void KCategorizedView::mouseMoveEvent(QMouseEvent *event) @@ -963,10 +1025,6 @@ void KCategorizedView::mouseMoveEvent(QMouseEvent *event) } rect = QRect(start, end).intersected(viewport()->rect().adjusted(-16, -16, 16, 16)); - - //viewport()->update(rect.united(d->lastSelectionRect)); - - d->lastSelectionRect = rect; } } @@ -985,7 +1043,13 @@ void KCategorizedView::mousePressEvent(QMouseEvent *event) horizontalOffset()); } +#ifdef KDE_WORKAROUND_FOR_QT_VIEW_BUG + QItemSelection prevSelection = selectionModel()->selection(); +#endif QListView::mousePressEvent(event); +#ifdef KDE_WORKAROUND_FOR_QT_VIEW_BUG + selectionModel()->select(prevSelection, QItemSelectionModel::Select); +#endif viewport()->update(d->categoryVisualRect(d->hoveredCategory)); } @@ -1006,39 +1070,29 @@ void KCategorizedView::mouseReleaseEvent(QMouseEvent *event) initialPressPosition.setY(initialPressPosition.y() + verticalOffset()); initialPressPosition.setX(initialPressPosition.x() + horizontalOffset()); - QItemSelection selection; - QItemSelection deselection; - if (initialPressPosition == d->initialPressPosition) { foreach(const QString &category, d->categories) { if (d->categoryVisualRect(category).contains(event->pos())) { - foreach (const QModelIndex &index, d->categoriesIndexes[category]) + QItemSelection selection; + QModelIndexList indexList = d->categoriesIndexes[category]; + + foreach (const QModelIndex &index, indexList) { QModelIndex selectIndex = index.model()->index(index.row(), 0); - if (!d->lastSelection.contains(selectIndex)) - { - selection << QItemSelectionRange(selectIndex); - } - else - { - deselection << QItemSelectionRange(selectIndex); - } + selection << QItemSelectionRange(selectIndex); } - selectionModel()->select(selection, QItemSelectionModel::Select); - selectionModel()->select(deselection, QItemSelectionModel::Deselect); + selectionModel()->select(selection, QItemSelectionModel::SelectCurrent); break; } } } - d->lastSelection = selectionModel()->selection(); - if (d->hovered.isValid()) viewport()->update(visualRect(d->hovered)); else if (!d->hoveredCategory.isEmpty()) @@ -1132,7 +1186,16 @@ QModelIndex KCategorizedView::moveCursor(CursorAction cursorAction, return QListView::moveCursor(cursorAction, modifiers); } - const QModelIndex current = selectionModel()->currentIndex(); + QModelIndex current = selectionModel()->currentIndex(); + + if (!current.isValid()) + { + current = model()->index(0, 0, QModelIndex()); + setCurrentIndex(current); + d->forcedSelectionPosition = 0; + + return current; + } int viewportWidth = viewport()->width() - spacing(); int itemWidth; @@ -1148,6 +1211,8 @@ QModelIndex KCategorizedView::moveCursor(CursorAction cursorAction, int itemWidthPlusSeparation = spacing() + itemWidth; int elementsPerRow = viewportWidth / itemWidthPlusSeparation; + if (!elementsPerRow) + elementsPerRow++; QString lastCategory = d->categories.first(); QString theCategory = d->categories.first(); @@ -1233,36 +1298,56 @@ QModelIndex KCategorizedView::moveCursor(CursorAction cursorAction, case QAbstractItemView::MoveLeft: if (layoutDirection() == Qt::RightToLeft) { + if (!(d->elementsInfo[current.row() + 1].relativeOffsetToCategory % elementsPerRow)) + return current; + d->forcedSelectionPosition = d->elementsInfo[current.row() + 1].relativeOffsetToCategory % elementsPerRow; +#if 0 //follow qt view behavior. lateral movements won't change visual row if (d->forcedSelectionPosition < 0) d->forcedSelectionPosition = (d->categoriesIndexes[theCategory].count() - 1) % elementsPerRow; +#endif return d->proxyModel->index(current.row() + 1, 0); } + if (!(d->elementsInfo[current.row()].relativeOffsetToCategory % elementsPerRow)) + return current; + d->forcedSelectionPosition = d->elementsInfo[current.row() - 1].relativeOffsetToCategory % elementsPerRow; +#if 0 //follow qt view behavior. lateral movements won't change visual row if (d->forcedSelectionPosition < 0) d->forcedSelectionPosition = (d->categoriesIndexes[theCategory].count() - 1) % elementsPerRow; +#endif return d->proxyModel->index(current.row() - 1, 0); case QAbstractItemView::MoveRight: if (layoutDirection() == Qt::RightToLeft) { + if (!(d->elementsInfo[current.row()].relativeOffsetToCategory % elementsPerRow)) + return current; + d->forcedSelectionPosition = d->elementsInfo[current.row() - 1].relativeOffsetToCategory % elementsPerRow; +#if 0 //follow qt view behavior. lateral movements won't change visual row if (d->forcedSelectionPosition < 0) d->forcedSelectionPosition = (d->categoriesIndexes[theCategory].count() - 1) % elementsPerRow; +#endif return d->proxyModel->index(current.row() - 1, 0); } + if (!(d->elementsInfo[current.row() + 1].relativeOffsetToCategory % elementsPerRow)) + return current; + d->forcedSelectionPosition = d->elementsInfo[current.row() + 1].relativeOffsetToCategory % elementsPerRow; +#if 0 //follow qt view behavior. lateral movements won't change visual row if (d->forcedSelectionPosition < 0) d->forcedSelectionPosition = (d->categoriesIndexes[theCategory].count() - 1) % elementsPerRow; +#endif return d->proxyModel->index(current.row() + 1, 0);