X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/0abbaf9e11d166da461bb33bc6abeb5ddfb5fa2c..94cfa325caef933e833bf8a14ac323cec0f2d499:/src/klistview.cpp diff --git a/src/klistview.cpp b/src/klistview.cpp index df8150a11..e3ea89e41 100644 --- a/src/klistview.cpp +++ b/src/klistview.cpp @@ -80,6 +80,7 @@ KListView::Private::Private(KListView *listView) , itemCategorizer(0) , mouseButtonPressed(false) , isDragging(false) + , dragLeftViewport(false) , proxyModel(0) , lastIndex(QModelIndex()) { @@ -119,7 +120,6 @@ const QModelIndexList &KListView::Private::intersectionSet(const QRect &rect) } } - int j = 0; for (int i = middle; i < proxyModel->rowCount(); i++) { index = elementDictionary[proxyModel->index(i, 0)]; @@ -130,10 +130,9 @@ const QModelIndexList &KListView::Private::intersectionSet(const QRect &rect) // If we passed next item, stop searching for hits if (qMax(rect.bottomRight().y(), rect.topLeft().y()) < - indexVisualRect.topLeft().y()) + qMin(indexVisualRect.topLeft().y(), + indexVisualRect.bottomRight().y())) break; - - j++; } return intersectedIndexes; @@ -170,16 +169,14 @@ QRect KListView::Private::visualRectInViewport(const QModelIndex &index) const retRect.setLeft(retRect.left() + column * listView->spacing() + column * itemWidth); - float rows; - int rowsInt; foreach (const QString &category, categories) { if (category == curCategory) break; - rows = (float) ((float) categoriesIndexes[category].count() / - (float) elementsPerRow); - rowsInt = categoriesIndexes[category].count() / elementsPerRow; + float rows = (float) ((float) categoriesIndexes[category].count() / + (float) elementsPerRow); + int rowsInt = categoriesIndexes[category].count() / elementsPerRow; if (rows - trunc(rows)) rowsInt++; @@ -227,16 +224,14 @@ QRect KListView::Private::visualCategoryRectInViewport(const QString &category) if (!elementsPerRow) elementsPerRow++; - float rows; - int rowsInt; foreach (const QString &itCategory, categories) { if (itCategory == category) break; - rows = (float) ((float) categoriesIndexes[itCategory].count() / - (float) elementsPerRow); - rowsInt = categoriesIndexes[itCategory].count() / elementsPerRow; + float rows = (float) ((float) categoriesIndexes[itCategory].count() / + (float) elementsPerRow); + int rowsInt = categoriesIndexes[itCategory].count() / elementsPerRow; if (rows - trunc(rows)) rowsInt++; @@ -332,8 +327,7 @@ void KListView::Private::drawNewCategory(const QString &category, opt.direction = option.direction; opt.text = category; - if (option.rect.contains(listView->viewport()->mapFromGlobal(QCursor::pos())) && - !mouseButtonPressed) + if ((category == hoveredCategory) && !mouseButtonPressed) { const QPalette::ColorGroup group = option.state & QStyle::State_Enabled ? @@ -399,14 +393,42 @@ void KListView::Private::drawDraggedItems(QPainter *painter) option.state &= ~QStyle::State_MouseOver; foreach (const QModelIndex &index, listView->selectionModel()->selectedIndexes()) { - int dx = mousePosition.x() - initialPressPosition.x(); - int dy = mousePosition.y() - initialPressPosition.y(); + const int dx = mousePosition.x() - initialPressPosition.x() + listView->horizontalOffset(); + const int dy = mousePosition.y() - initialPressPosition.y() + listView->verticalOffset(); option.rect = visualRect(index); option.rect.adjust(dx, dy, dx, dy); - listView->itemDelegate(index)->paint(painter, option, index); + if (option.rect.intersects(listView->viewport()->rect())) + { + listView->itemDelegate(index)->paint(painter, option, index); + } + } +} + +void KListView::Private::drawDraggedItems() +{ + QRect rectToUpdate; + QRect currentRect; + foreach (const QModelIndex &index, listView->selectionModel()->selectedIndexes()) + { + int dx = mousePosition.x() - initialPressPosition.x() + listView->horizontalOffset(); + int dy = mousePosition.y() - initialPressPosition.y() + listView->verticalOffset(); + + currentRect = visualRect(index); + currentRect.adjust(dx, dy, dx, dy); + + if (currentRect.intersects(listView->viewport()->rect())) + { + rectToUpdate = rectToUpdate.united(currentRect); + } } + + listView->viewport()->update(lastDraggedItemsRect); + + lastDraggedItemsRect = rectToUpdate; + + listView->viewport()->update(rectToUpdate); } @@ -546,7 +568,6 @@ void KListView::reset() d->elementDictionary.clear(); d->categoriesIndexes.clear(); d->categoriesPosition.clear(); - d->isIndexSelected.clear(); // selection cache d->categories.clear(); d->intersectedIndexes.clear(); d->sourceModelIndexList.clear(); @@ -614,19 +635,6 @@ void KListView::paintEvent(QPaintEvent *event) itemDelegate(index)->paint(&painter, option, index); } - // Redraw categories - QStyleOptionViewItem otherOption; - foreach (const QString &category, d->categories) - { - otherOption = option; - otherOption.rect = d->categoryVisualRect(category); - - if (otherOption.rect.intersects(area)) - { - d->drawNewCategory(category, otherOption, &painter); - } - } - if (d->mouseButtonPressed && !d->isDragging) { QPoint start, end, initialPressPosition; @@ -658,8 +666,24 @@ void KListView::paintEvent(QPaintEvent *event) painter.restore(); } - if (d->isDragging) + // Redraw categories + QStyleOptionViewItem otherOption; + foreach (const QString &category, d->categories) + { + otherOption = option; + otherOption.rect = d->categoryVisualRect(category); + + if (otherOption.rect.intersects(area)) + { + d->drawNewCategory(category, otherOption, &painter); + } + } + + if (d->isDragging && !d->dragLeftViewport) + { + painter.setOpacity(0.5); d->drawDraggedItems(&painter); + } painter.restore(); } @@ -691,100 +715,121 @@ void KListView::setSelection(const QRect &rect, return; } + if (!flags) + return; + + selectionModel()->clear(); + if (flags & QItemSelectionModel::Clear) { - selectionModel()->clear(); - d->isIndexSelected.clear(); - d->isTemporarySelected.clear(); + d->lastSelection = QItemSelection(); } - QItemSelection selection; QModelIndexList dirtyIndexes = d->intersectionSet(rect); - foreach (const QModelIndex &index, dirtyIndexes) + QItemSelection selection; + + if (!dirtyIndexes.count()) { - if (!d->mouseButtonPressed && rect.intersects(visualRect(index))) + if (d->lastSelection.count()) { - if (d->isIndexSelected.contains(index)) - { - if (!d->isIndexSelected[index]) - selection.select(index, index); - - d->isIndexSelected[index] = true; - } - else - { - d->isIndexSelected.insert(index, true); - selection.select(index, index); - } + selectionModel()->select(d->lastSelection, flags); } - else if (d->mouseButtonPressed) // selection cache - { - if (!d->isIndexSelected.contains(index) || - (d->isIndexSelected.contains(index) && !d->isIndexSelected[index])) - { - if (d->isTemporarySelected.contains(index)) - { - d->isTemporarySelected[index] = true; - } - else - { - d->isTemporarySelected.insert(index, true); - } - } - if (d->isIndexSelected.contains(index)) - { - if (!d->isIndexSelected[index]) - selection.select(index, index); - - d->isIndexSelected[index] = true; - } - else - { - d->isIndexSelected.insert(index, true); - selection.select(index, index); - } - } + return; } - QItemSelection deselect; - - foreach (const QModelIndex &index, d->isIndexSelected.keys()) + if (!d->mouseButtonPressed) { - if (!rect.intersects(visualRect(index))) + selection = QItemSelection(dirtyIndexes[0], dirtyIndexes[0]); + } + else + { + QModelIndex first = dirtyIndexes[0]; + QModelIndex last; + foreach (const QModelIndex &index, dirtyIndexes) { - if (d->isTemporarySelected.contains(index) && - d->isTemporarySelected[index]) + if (last.isValid() && last.row() + 1 != index.row()) { - deselect.select(index, index); - d->isTemporarySelected[index] = false; - d->isIndexSelected[index] = false; + QItemSelectionRange range(first, last); + + selection << range; + + first = index; } + + last = index; } + + if (last.isValid()) + selection << QItemSelectionRange(first, last); } - if (selection.count()) - selectionModel()->select(selection, QItemSelectionModel::Select); + if (d->lastSelection.count() && !d->mouseButtonPressed) + { + selection.merge(d->lastSelection, flags); + } + else if (d->lastSelection.count()) + { + selection.merge(d->lastSelection, QItemSelectionModel::Select); + } - if (deselect.count()) - selectionModel()->select(deselect, QItemSelectionModel::Deselect); + selectionModel()->select(selection, flags); } void KListView::mouseMoveEvent(QMouseEvent *event) { QListView::mouseMoveEvent(event); - d->mousePosition = event->pos(); - if ((viewMode() != KListView::IconMode) || !d->proxyModel || !d->itemCategorizer) { return; } - event->accept(); + d->mousePosition = event->pos(); + d->hoveredCategory = QString(); + + // Redraw categories + foreach (const QString &category, d->categories) + { + if (d->categoryVisualRect(category).intersects(QRect(event->pos(), event->pos()))) + { + d->hoveredCategory = category; + } + + viewport()->update(d->categoryVisualRect(category)); + } + + QRect rect; + if (d->mouseButtonPressed && !d->isDragging) + { + QPoint start, end, initialPressPosition; + + initialPressPosition = d->initialPressPosition; - viewport()->update(); + 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; + } + + viewport()->update(d->lastSelectionRect); + + rect = QRect(start, end).intersected(viewport()->rect().adjusted(-16, -16, 16, 16)); + + viewport()->update(rect); + + d->lastSelectionRect = rect; + } } void KListView::mousePressEvent(QMouseEvent *event) @@ -797,7 +842,7 @@ void KListView::mousePressEvent(QMouseEvent *event) return; } - event->accept(); + d->dragLeftViewport = false; if (event->button() == Qt::LeftButton) { @@ -809,82 +854,64 @@ void KListView::mousePressEvent(QMouseEvent *event) d->initialPressPosition.setX(d->initialPressPosition.x() + horizontalOffset()); } - - viewport()->update(); } void KListView::mouseReleaseEvent(QMouseEvent *event) { QListView::mouseReleaseEvent(event); - d->mouseButtonPressed = false; - if ((viewMode() != KListView::IconMode) || !d->proxyModel || !d->itemCategorizer) { return; } - event->accept(); - - d->isTemporarySelected.clear(); // selection cache + d->mouseButtonPressed = false; QPoint initialPressPosition = viewport()->mapFromGlobal(QCursor::pos()); initialPressPosition.setY(initialPressPosition.y() + verticalOffset()); initialPressPosition.setX(initialPressPosition.x() + horizontalOffset()); + QItemSelection selection; + if (initialPressPosition == d->initialPressPosition) { - QItemSelection selection; foreach(const QString &category, d->categories) { if (d->categoryVisualRect(category).contains(event->pos())) { - QModelIndex index; - foreach (const QModelIndex &mappedIndex, - d->categoriesIndexes[category]) - { - index = d->proxyModel->mapFromSource(mappedIndex); - - if (d->isIndexSelected.contains(index)) - { - if (!d->isIndexSelected[index]) - selection.select(index, index); - - d->isIndexSelected[index] = true; - } - else - { - d->isIndexSelected.insert(index, true); - selection.select(index, index); - } - } - - selectionModel()->select(selection, QItemSelectionModel::Toggle); + QItemSelectionRange selectionRange(d->proxyModel->mapFromSource(d->categoriesIndexes[category][0]), + d->proxyModel->mapFromSource(d->categoriesIndexes[category][d->categoriesIndexes[category].count() - 1])); + + selection << selectionRange; + + selectionModel()->select(selection, QItemSelectionModel::Select); break; } } } - viewport()->update(); + d->lastSelection = selectionModel()->selection(); + + if (d->hovered.isValid()) + viewport()->update(d->visualRect(d->hovered)); + else if (!d->hoveredCategory.isEmpty()) + viewport()->update(d->categoryVisualRect(d->hoveredCategory)); } void KListView::leaveEvent(QEvent *event) { QListView::leaveEvent(event); - d->hovered = QModelIndex(); - if ((viewMode() != KListView::IconMode) || !d->proxyModel || !d->itemCategorizer) { return; } - event->accept(); - - viewport()->update(); + d->hovered = QModelIndex(); + d->hoveredCategory = QString(); } void KListView::startDrag(Qt::DropActions supportedActions) @@ -897,16 +924,15 @@ void KListView::startDrag(Qt::DropActions supportedActions) void KListView::dragMoveEvent(QDragMoveEvent *event) { - QListView::dragMoveEvent(event); - - d->mousePosition = event->pos(); - if ((viewMode() != KListView::IconMode) || !d->proxyModel || !d->itemCategorizer) { + QListView::dragMoveEvent(event); return; } + d->mousePosition = event->pos(); + if (d->mouseButtonPressed) { d->isDragging = true; @@ -916,9 +942,34 @@ void KListView::dragMoveEvent(QDragMoveEvent *event) d->isDragging = false; } - event->accept(); + d->dragLeftViewport = false; - viewport()->update(); + d->drawDraggedItems(); +} + +void KListView::dragLeaveEvent(QDragLeaveEvent *event) +{ + QListView::dragLeaveEvent(event); + + if ((viewMode() != KListView::IconMode) || !d->proxyModel || + !d->itemCategorizer) + { + return; + } + + d->dragLeftViewport = true; +} + +QModelIndex KListView::moveCursor(CursorAction cursorAction, + Qt::KeyboardModifiers modifiers) +{ + if ((viewMode() != KListView::IconMode) || !d->proxyModel || + !d->itemCategorizer) + { + return QListView::moveCursor(cursorAction, modifiers); + } + + return QListView::moveCursor(cursorAction, modifiers); } void KListView::rowsInserted(const QModelIndex &parent, @@ -940,12 +991,14 @@ void KListView::rowsInsertedArtifficial(const QModelIndex &parent, int start, int end) { + Q_UNUSED(parent); + + d->lastSelection = QItemSelection(); d->elementsInfo.clear(); d->elementsPosition.clear(); d->elementDictionary.clear(); d->categoriesIndexes.clear(); d->categoriesPosition.clear(); - d->isIndexSelected.clear(); // selection cache d->categories.clear(); d->intersectedIndexes.clear(); d->sourceModelIndexList.clear(); @@ -1044,7 +1097,8 @@ void KListView::rowsRemoved(const QModelIndex &parent, int start, int end) { - if (d->proxyModel) + if ((viewMode() == KListView::IconMode) && d->proxyModel && + d->itemCategorizer) { // Force the view to update all elements rowsInsertedArtifficial(parent, start, end);