X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/2df7bd34758cf02db0300ffb99b78e7dbf55a791..0cb23c8cfa1049cbd5ef98368ff1c200978b8ac7:/src/kcategorizedview.cpp diff --git a/src/kcategorizedview.cpp b/src/kcategorizedview.cpp index 6590c4b2a..6fa7632f2 100644 --- a/src/kcategorizedview.cpp +++ b/src/kcategorizedview.cpp @@ -44,6 +44,7 @@ KCategorizedView::Private::Private(KCategorizedView *listView) , categoryDrawer(0) , biggestItemSize(QSize(0, 0)) , mouseButtonPressed(false) + , rightMouseButtonPressed(false) , isDragging(false) , dragLeftViewport(false) , proxyModel(0) @@ -499,7 +500,6 @@ void KCategorizedView::setGridSize(const QSize &size) void KCategorizedView::setModel(QAbstractItemModel *model) { d->lastSelection = QItemSelection(); - d->currentViewIndex = QModelIndex(); d->forcedSelectionPosition = 0; d->elementsInfo.clear(); d->elementsPosition.clear(); @@ -510,6 +510,7 @@ void KCategorizedView::setModel(QAbstractItemModel *model) d->modelIndexList.clear(); d->hovered = QModelIndex(); d->mouseButtonPressed = false; + d->rightMouseButtonPressed = false; if (d->proxyModel) { @@ -585,7 +586,6 @@ KCategoryDrawer *KCategorizedView::categoryDrawer() const void KCategorizedView::setCategoryDrawer(KCategoryDrawer *categoryDrawer) { d->lastSelection = QItemSelection(); - d->currentViewIndex = QModelIndex(); d->forcedSelectionPosition = 0; d->elementsInfo.clear(); d->elementsPosition.clear(); @@ -596,6 +596,7 @@ void KCategorizedView::setCategoryDrawer(KCategoryDrawer *categoryDrawer) d->modelIndexList.clear(); d->hovered = QModelIndex(); d->mouseButtonPressed = false; + d->rightMouseButtonPressed = false; if (!categoryDrawer && d->proxyModel) { @@ -661,8 +662,6 @@ QModelIndex KCategorizedView::indexAt(const QPoint &point) const index = item[0]; } - d->hovered = index; - return index; } @@ -671,7 +670,6 @@ void KCategorizedView::reset() QListView::reset(); d->lastSelection = QItemSelection(); - d->currentViewIndex = QModelIndex(); d->forcedSelectionPosition = 0; d->elementsInfo.clear(); d->elementsPosition.clear(); @@ -683,6 +681,7 @@ void KCategorizedView::reset() d->hovered = QModelIndex(); d->biggestItemSize = QSize(0, 0); d->mouseButtonPressed = false; + d->rightMouseButtonPressed = false; } void KCategorizedView::paintEvent(QPaintEvent *event) @@ -743,7 +742,10 @@ void KCategorizedView::paintEvent(QPaintEvent *event) option.state |= QStyle::State_Editing; } - if ((index == d->hovered) && !d->mouseButtonPressed) + // we are only interested to give the mouse over feedback when no + // dragging is happening (ereslibre) + if ((index == d->hovered) && !d->mouseButtonPressed && + (this->state() == QAbstractItemView::NoState)) option.state |= QStyle::State_MouseOver; else option.state &= ~QStyle::State_MouseOver; @@ -850,6 +852,7 @@ void KCategorizedView::setSelection(const QRect &rect, if (flags & QItemSelectionModel::Clear) { selectionModel()->clear(); + d->lastSelection.clear(); } QModelIndexList dirtyIndexes = d->intersectionSet(rect); @@ -857,20 +860,7 @@ void KCategorizedView::setSelection(const QRect &rect, // no items affected, just leave if (!dirtyIndexes.count()) { - selectionModel()->select(d->lastSelection, flags); - - d->lastSelection.clear(); - - return; - } - - d->lastSelection.clear(); - - if (!(flags & QItemSelectionModel::Current)) - { - Q_ASSERT(dirtyIndexes.count() == 1); - - selectionModel()->select(dirtyIndexes[0], flags); + selectionModel()->select(d->lastSelection, QItemSelectionModel::SelectCurrent); return; } @@ -878,14 +868,17 @@ void KCategorizedView::setSelection(const QRect &rect, QModelIndex topLeft; QModelIndex bottomRight; - if (d->mouseButtonPressed) // selection with click + drag + if (d->mouseButtonPressed || d->rightMouseButtonPressed) // selection with click + drag { + QItemSelection selection; + QModelIndex prev = dirtyIndexes[0]; QModelIndex first = prev; foreach (const QModelIndex &index, dirtyIndexes) { + // we have a different interval. non-contiguous items if ((index.row() - prev.row()) > 1) { - d->lastSelection << QItemSelectionRange(first, prev); + selection << QItemSelectionRange(first, prev); first = index; } @@ -893,9 +886,27 @@ void KCategorizedView::setSelection(const QRect &rect, prev = index; } - d->lastSelection << QItemSelectionRange(first, prev); + selection << QItemSelectionRange(first, prev); - selectionModel()->select(d->lastSelection, flags); + if (flags & QItemSelectionModel::Current) + { + if (rect.topLeft() == rect.bottomRight()) + { + selectionModel()->setCurrentIndex(indexAt(rect.topLeft()), QItemSelectionModel::NoUpdate); + } + + selection.merge(d->lastSelection, flags); + } + else + { + selection.merge(selectionModel()->selection(), flags); + + selectionModel()->select(selection, QItemSelectionModel::SelectCurrent); + + return; + } + + selectionModel()->select(selection, flags); } else // selection with click + keyboard keys { @@ -960,10 +971,9 @@ void KCategorizedView::setSelection(const QRect &rect, dirtyIndexes << model()->index(i, theoricDirty[0].column(), theoricDirty[0].parent()); } - // our current selection will result modified - d->lastSelection = QItemSelection(topLeft, bottomRight); + QItemSelection selection(topLeft, bottomRight); - selectionModel()->select(d->lastSelection, flags); + selectionModel()->select(selection, flags); } } @@ -977,6 +987,17 @@ void KCategorizedView::mouseMoveEvent(QMouseEvent *event) return; } + QModelIndexList item = d->intersectionSet(QRect(event->pos(), event->pos())); + + if (item.count() == 1) + { + d->hovered = item[0]; + } + else + { + d->hovered = QModelIndex(); + } + const QString previousHoveredCategory = d->hoveredCategory; d->mousePosition = event->pos(); @@ -1019,7 +1040,10 @@ void KCategorizedView::mouseMoveEvent(QMouseEvent *event) end = d->mousePosition; } - rect = QRect(start, end).intersected(viewport()->rect().adjusted(-16, -16, 16, 16)); + rect = QRect(start, end).adjusted(-16, -16, 16, 16); + rect = rect.united(QRect(start, end).adjusted(16, 16, -16, -16)).intersected(viewport()->rect()); + + viewport()->update(rect); } } @@ -1037,15 +1061,22 @@ void KCategorizedView::mousePressEvent(QMouseEvent *event) d->initialPressPosition.setX(d->initialPressPosition.x() + horizontalOffset()); } + else if (event->button() == Qt::RightButton) + { + d->rightMouseButtonPressed = true; + } QListView::mousePressEvent(event); + d->lastSelection = selectionModel()->selection(); + viewport()->update(d->categoryVisualRect(d->hoveredCategory)); } void KCategorizedView::mouseReleaseEvent(QMouseEvent *event) { d->mouseButtonPressed = false; + d->rightMouseButtonPressed = false; QListView::mouseReleaseEvent(event); @@ -1075,13 +1106,41 @@ void KCategorizedView::mouseReleaseEvent(QMouseEvent *event) selection << QItemSelectionRange(selectIndex); } - selectionModel()->select(selection, QItemSelectionModel::SelectCurrent); + selectionModel()->select(selection, QItemSelectionModel::Select); break; } } } + QRect rect; + if (!d->isDragging) + { + QPoint start, end, initialPressPosition; + + initialPressPosition = d->initialPressPosition; + + initialPressPosition.setY(initialPressPosition.y() - verticalOffset()); + initialPressPosition.setX(initialPressPosition.x() - horizontalOffset()); + + if (d->initialPressPosition.x() > d->mousePosition.x() || + d->initialPressPosition.y() > d->mousePosition.y()) + { + start = d->mousePosition; + end = initialPressPosition; + } + else + { + start = initialPressPosition; + end = d->mousePosition; + } + + rect = QRect(start, end).adjusted(-16, -16, 16, 16); + rect = rect.united(QRect(start, end).adjusted(16, 16, -16, -16)).intersected(viewport()->rect()); + + viewport()->update(rect); + } + if (d->hovered.isValid()) viewport()->update(visualRect(d->hovered)); else if (!d->hoveredCategory.isEmpty()) @@ -1109,6 +1168,7 @@ void KCategorizedView::startDrag(Qt::DropActions supportedActions) d->isDragging = false; d->mouseButtonPressed = false; + d->rightMouseButtonPressed = false; viewport()->update(d->lastDraggedItemsRect); } @@ -1128,14 +1188,15 @@ void KCategorizedView::dragMoveEvent(QDragMoveEvent *event) d->dragLeftViewport = false; - if ((viewMode() != KCategorizedView::IconMode) || !d->proxyModel || - !d->categoryDrawer || !d->proxyModel->isCategorizedModel()) - { #if defined(DOLPHIN_DRAGANDDROP) - QAbstractItemView::dragMoveEvent(event); + QAbstractItemView::dragMoveEvent(event); #else - QListView::dragMoveEvent(event); + QListView::dragMoveEvent(event); #endif + + if ((viewMode() != KCategorizedView::IconMode) || !d->proxyModel || + !d->categoryDrawer || !d->proxyModel->isCategorizedModel()) + { return; } @@ -1180,7 +1241,7 @@ QModelIndex KCategorizedView::moveCursor(CursorAction cursorAction, if (!current.isValid()) { current = model()->index(0, 0, QModelIndex()); - setCurrentIndex(current); + selectionModel()->select(current, QItemSelectionModel::NoUpdate); d->forcedSelectionPosition = 0; return current; @@ -1356,8 +1417,6 @@ void KCategorizedView::rowsInserted(const QModelIndex &parent, if ((viewMode() != KCategorizedView::IconMode) || !d->proxyModel || !d->categoryDrawer || !d->proxyModel->isCategorizedModel()) { - d->lastSelection = QItemSelection(); - d->currentViewIndex = QModelIndex(); d->forcedSelectionPosition = 0; d->elementsInfo.clear(); d->elementsPosition.clear(); @@ -1369,6 +1428,7 @@ void KCategorizedView::rowsInserted(const QModelIndex &parent, d->hovered = QModelIndex(); d->biggestItemSize = QSize(0, 0); d->mouseButtonPressed = false; + d->rightMouseButtonPressed = false; return; } @@ -1382,8 +1442,6 @@ void KCategorizedView::rowsInsertedArtifficial(const QModelIndex &parent, { Q_UNUSED(parent); - d->lastSelection = QItemSelection(); - d->currentViewIndex = QModelIndex(); d->forcedSelectionPosition = 0; d->elementsInfo.clear(); d->elementsPosition.clear(); @@ -1395,6 +1453,7 @@ void KCategorizedView::rowsInsertedArtifficial(const QModelIndex &parent, d->hovered = QModelIndex(); d->biggestItemSize = QSize(0, 0); d->mouseButtonPressed = false; + d->rightMouseButtonPressed = false; if (start > end || end < 0 || start < 0 || !d->proxyModel->rowCount()) { @@ -1447,6 +1506,10 @@ void KCategorizedView::rowsInsertedArtifficial(const QModelIndex &parent, d->categories << prevCategory; d->updateScrollbars(); + + // FIXME: We need to safely save the last selection. This is on my TODO + // list (ereslibre). + selectionModel()->clear(); } void KCategorizedView::rowsRemoved(const QModelIndex &parent, @@ -1480,4 +1543,36 @@ void KCategorizedView::slotLayoutChanged() d->layoutChanged(); } +void KCategorizedView::currentChanged(const QModelIndex ¤t, + const QModelIndex &previous) +{ + // We need to update the forcedSelectionPosition property in order to correctly + // navigate after with keyboard using up & down keys + + int viewportWidth = viewport()->width() - spacing(); + + int itemHeight; + int itemWidth; + + if (gridSize().isEmpty()) + { + itemHeight = d->biggestItemSize.height(); + itemWidth = d->biggestItemSize.width(); + } + else + { + itemHeight = gridSize().height(); + itemWidth = gridSize().width(); + } + + int itemWidthPlusSeparation = spacing() + itemWidth; + int elementsPerRow = viewportWidth / itemWidthPlusSeparation; + if (!elementsPerRow) + elementsPerRow++; + + d->forcedSelectionPosition = d->elementsInfo[current.row()].relativeOffsetToCategory % elementsPerRow; + + QListView::currentChanged(current, previous); +} + #include "kcategorizedview.moc"