X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/ba6c853219c78e4e2a491d416b94e4e4c1aef591..90ca3c31dc3961ff907d7600f2ff7bec14322b30:/src/klistview.cpp diff --git a/src/klistview.cpp b/src/klistview.cpp index e484c3fb3..4db18e1f4 100644 --- a/src/klistview.cpp +++ b/src/klistview.cpp @@ -120,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)]; @@ -131,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; @@ -148,7 +146,7 @@ QRect KListView::Private::visualRectInViewport(const QModelIndex &index) const QString curCategory = elementsInfo[index].category; QRect retRect(listView->spacing(), listView->spacing() * 2 + - 30 /* categoryHeight */, 0, 0); + itemCategorizer->categoryHeight(listView->viewOptions()), 0, 0); int viewportWidth = listView->viewport()->width() - listView->spacing(); @@ -171,23 +169,21 @@ 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++; retRect.setTop(retRect.top() + (rowsInt * listView->spacing()) + (rowsInt * itemHeight) + - 30 /* categoryHeight */ + + itemCategorizer->categoryHeight(listView->viewOptions()) + listView->spacing() * 2); } @@ -228,27 +224,25 @@ 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++; retRect.setTop(retRect.top() + (rowsInt * listView->spacing()) + (rowsInt * itemHeight) + - 30 /* categoryHeight */ + + itemCategorizer->categoryHeight(listView->viewOptions()) + listView->spacing() * 2); } - retRect.setHeight(30 /* categoryHeight */); + retRect.setHeight(itemCategorizer->categoryHeight(listView->viewOptions())); return retRect; } @@ -317,70 +311,23 @@ QRect KListView::Private::categoryVisualRect(const QString &category) return retRect; } -void KListView::Private::drawNewCategory(const QString &category, +void KListView::Private::drawNewCategory(const QModelIndex &index, + int sortRole, const QStyleOption &option, QPainter *painter) { - QColor color = option.palette.color(QPalette::Text); - - painter->save(); - painter->setRenderHint(QPainter::Antialiasing); - - QStyleOptionButton opt; - - opt.rect = option.rect; - opt.palette = option.palette; - opt.direction = option.direction; - opt.text = category; + QStyleOption optionCopy = option; + const QString category = itemCategorizer->categoryForItem(index, sortRole); - if (option.rect.contains(listView->viewport()->mapFromGlobal(QCursor::pos())) && - !mouseButtonPressed) + if ((category == hoveredCategory) && !mouseButtonPressed) { - const QPalette::ColorGroup group = - option.state & QStyle::State_Enabled ? - QPalette::Normal : QPalette::Disabled; - - QLinearGradient gradient(option.rect.topLeft(), - option.rect.bottomRight()); - gradient.setColorAt(0, - option.palette.color(group, - QPalette::Highlight).light()); - gradient.setColorAt(1, Qt::transparent); - - painter->fillRect(option.rect, gradient); + optionCopy.state |= QStyle::State_MouseOver; } - /*if (const KStyle *style = dynamic_cast(QApplication::style())) - { - style->drawControl(KStyle::CE_Category, &opt, painter, this); - } - else - {*/ - QFont painterFont = painter->font(); - painterFont.setWeight(QFont::Bold); - QFontMetrics metrics(painterFont); - painter->setFont(painterFont); - - QPainterPath path; - path.addRect(option.rect.left(), - option.rect.bottom() - 2, - option.rect.width(), - 2); - - QLinearGradient gradient(option.rect.topLeft(), - option.rect.bottomRight()); - gradient.setColorAt(0, color); - gradient.setColorAt(1, Qt::transparent); - - painter->setBrush(gradient); - painter->fillPath(path, gradient); - - painter->setPen(color); - - painter->drawText(option.rect, Qt::AlignVCenter | Qt::AlignLeft, - metrics.elidedText(category, Qt::ElideRight, option.rect.width())); - //} - painter->restore(); + itemCategorizer->drawCategory(index, + sortRole, + optionCopy, + painter); } @@ -400,14 +347,40 @@ void KListView::Private::drawDraggedItems(QPainter *painter) option.state &= ~QStyle::State_MouseOver; foreach (const QModelIndex &index, listView->selectionModel()->selectedIndexes()) { - int dx = mousePosition.x() - initialPressPosition.x() + listView->horizontalOffset(); - int dy = mousePosition.y() - initialPressPosition.y() + listView->verticalOffset(); + 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.united(rectToUpdate)); + + lastDraggedItemsRect = rectToUpdate; } @@ -427,6 +400,21 @@ KListView::~KListView() void KListView::setModel(QAbstractItemModel *model) { + d->lastSelection = QItemSelection(); + d->currentViewIndex = QModelIndex(); + d->forcedSelectionPosition = 0; + d->elementsInfo.clear(); + d->elementsPosition.clear(); + d->elementDictionary.clear(); + d->invertedElementDictionary.clear(); + d->categoriesIndexes.clear(); + d->categoriesPosition.clear(); + d->categories.clear(); + d->intersectedIndexes.clear(); + d->sourceModelIndexList.clear(); + d->hovered = QModelIndex(); + d->mouseButtonPressed = false; + if (d->proxyModel) { QObject::disconnect(d->proxyModel, @@ -477,6 +465,21 @@ KItemCategorizer *KListView::itemCategorizer() const void KListView::setItemCategorizer(KItemCategorizer *itemCategorizer) { + d->lastSelection = QItemSelection(); + d->currentViewIndex = QModelIndex(); + d->forcedSelectionPosition = 0; + d->elementsInfo.clear(); + d->elementsPosition.clear(); + d->elementDictionary.clear(); + d->invertedElementDictionary.clear(); + d->categoriesIndexes.clear(); + d->categoriesPosition.clear(); + d->categories.clear(); + d->intersectedIndexes.clear(); + d->sourceModelIndexList.clear(); + d->hovered = QModelIndex(); + d->mouseButtonPressed = false; + if (!itemCategorizer && d->proxyModel) { QObject::disconnect(d->proxyModel, @@ -536,15 +539,13 @@ void KListView::reset() { QListView::reset(); - if ((viewMode() != KListView::IconMode) || !d->proxyModel || - !d->itemCategorizer) - { - return; - } - + d->lastSelection = QItemSelection(); + d->currentViewIndex = QModelIndex(); + d->forcedSelectionPosition = 0; d->elementsInfo.clear(); d->elementsPosition.clear(); d->elementDictionary.clear(); + d->invertedElementDictionary.clear(); d->categoriesIndexes.clear(); d->categoriesPosition.clear(); d->categories.clear(); @@ -615,15 +616,18 @@ void KListView::paintEvent(QPaintEvent *event) } // Redraw categories + int i = 0; QStyleOptionViewItem otherOption; foreach (const QString &category, d->categories) { otherOption = option; otherOption.rect = d->categoryVisualRect(category); + otherOption.state &= ~QStyle::State_MouseOver; if (otherOption.rect.intersects(area)) { - d->drawNewCategory(category, otherOption, &painter); + d->drawNewCategory(d->categoriesIndexes[category][0], + d->proxyModel->sortRole(), otherOption, &painter); } } @@ -671,16 +675,17 @@ void KListView::resizeEvent(QResizeEvent *event) { QListView::resizeEvent(event); + // Clear the items positions cache + d->elementsPosition.clear(); + d->categoriesPosition.clear(); + d->forcedSelectionPosition = 0; + if ((viewMode() != KListView::IconMode) || !d->proxyModel || !d->itemCategorizer) { return; } - // Clear the items positions cache - d->elementsPosition.clear(); - d->categoriesPosition.clear(); - d->updateScrollbars(); } @@ -705,6 +710,7 @@ void KListView::setSelection(const QRect &rect, } QModelIndexList dirtyIndexes = d->intersectionSet(rect); + QItemSelection selection; if (!dirtyIndexes.count()) @@ -720,6 +726,7 @@ void KListView::setSelection(const QRect &rect, if (!d->mouseButtonPressed) { selection = QItemSelection(dirtyIndexes[0], dirtyIndexes[0]); + d->currentViewIndex = dirtyIndexes[0]; } else { @@ -753,14 +760,10 @@ void KListView::setSelection(const QRect &rect, } selectionModel()->select(selection, flags); - - viewport()->update(); } void KListView::mouseMoveEvent(QMouseEvent *event) { - d->mousePosition = event->pos(); - QListView::mouseMoveEvent(event); if ((viewMode() != KListView::IconMode) || !d->proxyModel || @@ -769,23 +772,58 @@ void KListView::mouseMoveEvent(QMouseEvent *event) return; } - event->accept(); - - viewport()->update(); -} + const QString previousHoveredCategory = d->hoveredCategory; -void KListView::mousePressEvent(QMouseEvent *event) -{ - QListView::mousePressEvent(event); + d->mousePosition = event->pos(); + d->hoveredCategory = QString(); - if ((viewMode() != KListView::IconMode) || !d->proxyModel || - !d->itemCategorizer) + // Redraw categories + foreach (const QString &category, d->categories) { - return; + if (d->categoryVisualRect(category).intersects(QRect(event->pos(), event->pos()))) + { + d->hoveredCategory = category; + viewport()->update(d->categoryVisualRect(category)); + } + else if ((category == previousHoveredCategory) && + (!d->categoryVisualRect(previousHoveredCategory).intersects(QRect(event->pos(), event->pos())))) + { + viewport()->update(d->categoryVisualRect(category)); + } } - event->accept(); + QRect rect; + if (d->mouseButtonPressed && !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).intersected(viewport()->rect().adjusted(-16, -16, 16, 16)); + + //viewport()->update(rect.united(d->lastSelectionRect)); + + d->lastSelectionRect = rect; + } +} +void KListView::mousePressEvent(QMouseEvent *event) +{ d->dragLeftViewport = false; if (event->button() == Qt::LeftButton) @@ -799,11 +837,13 @@ void KListView::mousePressEvent(QMouseEvent *event) horizontalOffset()); } - viewport()->update(); + QListView::mousePressEvent(event); } void KListView::mouseReleaseEvent(QMouseEvent *event) { + d->mouseButtonPressed = false; + QListView::mouseReleaseEvent(event); if ((viewMode() != KListView::IconMode) || !d->proxyModel || @@ -812,10 +852,6 @@ void KListView::mouseReleaseEvent(QMouseEvent *event) return; } - event->accept(); - - d->mouseButtonPressed = false; - QPoint initialPressPosition = viewport()->mapFromGlobal(QCursor::pos()); initialPressPosition.setY(initialPressPosition.y() + verticalOffset()); initialPressPosition.setX(initialPressPosition.x() + horizontalOffset()); @@ -842,24 +878,18 @@ void KListView::mouseReleaseEvent(QMouseEvent *event) d->lastSelection = selectionModel()->selection(); - viewport()->update(); + 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(); + d->hoveredCategory = QString(); - if ((viewMode() != KListView::IconMode) || !d->proxyModel || - !d->itemCategorizer) - { - return; - } - - event->accept(); - - viewport()->update(); + QListView::leaveEvent(event); } void KListView::startDrag(Qt::DropActions supportedActions) @@ -868,20 +898,14 @@ void KListView::startDrag(Qt::DropActions supportedActions) d->isDragging = false; d->mouseButtonPressed = false; + + viewport()->update(d->lastDraggedItemsRect); } void KListView::dragMoveEvent(QDragMoveEvent *event) { - QListView::dragMoveEvent(event); - d->mousePosition = event->pos(); - if ((viewMode() != KListView::IconMode) || !d->proxyModel || - !d->itemCategorizer) - { - return; - } - if (d->mouseButtonPressed) { d->isDragging = true; @@ -893,26 +917,21 @@ void KListView::dragMoveEvent(QDragMoveEvent *event) d->dragLeftViewport = false; - event->accept(); - - viewport()->update(); -} - -void KListView::dragLeaveEvent(QDragLeaveEvent *event) -{ - QListView::dragLeaveEvent(event); - if ((viewMode() != KListView::IconMode) || !d->proxyModel || !d->itemCategorizer) { + QListView::dragMoveEvent(event); return; } - d->dragLeftViewport = true; + d->drawDraggedItems(); +} - event->accept(); +void KListView::dragLeaveEvent(QDragLeaveEvent *event) +{ + d->dragLeftViewport = true; - viewport()->update(); + QListView::dragLeaveEvent(event); } QModelIndex KListView::moveCursor(CursorAction cursorAction, @@ -924,6 +943,119 @@ QModelIndex KListView::moveCursor(CursorAction cursorAction, return QListView::moveCursor(cursorAction, modifiers); } + const QModelIndex current = selectionModel()->currentIndex(); + + int viewportWidth = viewport()->width() - spacing(); + // We really need all items to be of same size. Otherwise we cannot do this + // (ereslibre) + // QSize itemSize = listView->sizeHintForIndex(index); + // int itemHeight = itemSize.height(); + // int itemWidth = itemSize.width(); + int itemHeight = 107; + int itemWidth = 130; + int itemWidthPlusSeparation = spacing() + itemWidth; + int elementsPerRow = viewportWidth / itemWidthPlusSeparation; + + QString lastCategory = d->categories[0]; + QString theCategory = d->categories[0]; + QString afterCategory = d->categories[0]; + bool hasToBreak = false; + foreach (const QString &category, d->categories) + { + if (hasToBreak) + { + afterCategory = category; + + break; + } + + if (category == d->elementsInfo[d->proxyModel->mapToSource(current)].category) + { + theCategory = category; + + hasToBreak = true; + } + + if (!hasToBreak) + { + lastCategory = category; + } + } + + switch (cursorAction) + { + case QAbstractItemView::MoveUp: { + if (d->elementsInfo[d->proxyModel->mapToSource(current)].relativeOffsetToCategory >= elementsPerRow) + { + int indexToMove = d->invertedElementDictionary[current].row(); + indexToMove -= qMin(((d->elementsInfo[d->proxyModel->mapToSource(current)].relativeOffsetToCategory) + d->forcedSelectionPosition), elementsPerRow - d->forcedSelectionPosition + (d->elementsInfo[d->proxyModel->mapToSource(current)].relativeOffsetToCategory % elementsPerRow)); + + return d->elementDictionary[d->proxyModel->index(indexToMove, 0)]; + } + else + { + int lastCategoryLastRow = (d->categoriesIndexes[lastCategory].count() - 1) % elementsPerRow; + int indexToMove = d->invertedElementDictionary[current].row() - d->elementsInfo[d->proxyModel->mapToSource(current)].relativeOffsetToCategory; + + if (d->forcedSelectionPosition >= lastCategoryLastRow) + { + indexToMove -= 1; + } + else + { + indexToMove -= qMin((lastCategoryLastRow - d->forcedSelectionPosition + 1), d->forcedSelectionPosition + elementsPerRow + 1); + } + + return d->elementDictionary[d->proxyModel->index(indexToMove, 0)]; + } + } + + case QAbstractItemView::MoveDown: { + if (d->elementsInfo[d->proxyModel->mapToSource(current)].relativeOffsetToCategory < (d->categoriesIndexes[theCategory].count() - 1 - ((d->categoriesIndexes[theCategory].count() - 1) % elementsPerRow))) + { + int indexToMove = d->invertedElementDictionary[current].row(); + indexToMove += qMin(elementsPerRow, d->categoriesIndexes[theCategory].count() - 1 - d->elementsInfo[d->proxyModel->mapToSource(current)].relativeOffsetToCategory); + + return d->elementDictionary[d->proxyModel->index(indexToMove, 0)]; + } + else + { + int afterCategoryLastRow = qMin(elementsPerRow, d->categoriesIndexes[afterCategory].count()); + int indexToMove = d->invertedElementDictionary[current].row() + (d->categoriesIndexes[theCategory].count() - d->elementsInfo[d->proxyModel->mapToSource(current)].relativeOffsetToCategory); + + if (d->forcedSelectionPosition >= afterCategoryLastRow) + { + indexToMove += afterCategoryLastRow - 1; + } + else + { + indexToMove += qMin(d->forcedSelectionPosition, elementsPerRow); + } + + return d->elementDictionary[d->proxyModel->index(indexToMove, 0)]; + } + } + + case QAbstractItemView::MoveLeft: + d->forcedSelectionPosition = d->elementsInfo[d->proxyModel->mapToSource(d->elementDictionary[d->proxyModel->index(d->invertedElementDictionary[current].row() - 1, 0)])].relativeOffsetToCategory % elementsPerRow; + + if (d->forcedSelectionPosition < 0) + d->forcedSelectionPosition = (d->categoriesIndexes[theCategory].count() - 1) % elementsPerRow; + + return d->elementDictionary[d->proxyModel->index(d->invertedElementDictionary[current].row() - 1, 0)]; + + case QAbstractItemView::MoveRight: + d->forcedSelectionPosition = d->elementsInfo[d->proxyModel->mapToSource(d->elementDictionary[d->proxyModel->index(d->invertedElementDictionary[current].row() + 1, 0)])].relativeOffsetToCategory % elementsPerRow; + + if (d->forcedSelectionPosition < 0) + d->forcedSelectionPosition = (d->categoriesIndexes[theCategory].count() - 1) % elementsPerRow; + + return d->elementDictionary[d->proxyModel->index(d->invertedElementDictionary[current].row() + 1, 0)]; + + default: + break; + } + return QListView::moveCursor(cursorAction, modifiers); } @@ -936,6 +1068,21 @@ void KListView::rowsInserted(const QModelIndex &parent, if ((viewMode() != KListView::IconMode) || !d->proxyModel || !d->itemCategorizer) { + d->lastSelection = QItemSelection(); + d->currentViewIndex = QModelIndex(); + d->forcedSelectionPosition = 0; + d->elementsInfo.clear(); + d->elementsPosition.clear(); + d->elementDictionary.clear(); + d->invertedElementDictionary.clear(); + d->categoriesIndexes.clear(); + d->categoriesPosition.clear(); + d->categories.clear(); + d->intersectedIndexes.clear(); + d->sourceModelIndexList.clear(); + d->hovered = QModelIndex(); + d->mouseButtonPressed = false; + return; } @@ -946,10 +1093,15 @@ void KListView::rowsInsertedArtifficial(const QModelIndex &parent, int start, int end) { + Q_UNUSED(parent); + d->lastSelection = QItemSelection(); + d->currentViewIndex = QModelIndex(); + d->forcedSelectionPosition = 0; d->elementsInfo.clear(); d->elementsPosition.clear(); d->elementDictionary.clear(); + d->invertedElementDictionary.clear(); d->categoriesIndexes.clear(); d->categoriesPosition.clear(); d->categories.clear(); @@ -1036,6 +1188,9 @@ void KListView::rowsInsertedArtifficial(const QModelIndex &parent, d->elementDictionary.insert(d->proxyModel->index(j, 0), d->proxyModel->mapFromSource(index)); + d->invertedElementDictionary.insert(d->proxyModel->mapFromSource(index), + d->proxyModel->index(j, 0)); + i++; j++; }