X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/919d20115f9f3f085b1719ce51b2bd04240662bb..1e27cae3e58f0be65ea6c690f9144b0621829d2b:/src/kcategorizedview.cpp diff --git a/src/kcategorizedview.cpp b/src/kcategorizedview.cpp index 20fbdc757..42ee7b1ad 100644 --- a/src/kcategorizedview.cpp +++ b/src/kcategorizedview.cpp @@ -24,7 +24,6 @@ #include // trunc on C99 compliant systems #include // trunc for not C99 compliant systems -#include #include #include #include @@ -34,11 +33,18 @@ #include "kcategorydrawer.h" #include "kcategorizedsortfilterproxymodel.h" +// By defining DOLPHIN_DRAGANDDROP the custom drag and drop implementation of +// KCategorizedView is bypassed to have a consistent drag and drop look for all +// views. Hopefully transparent pixmaps for drag objects will be supported in +// Qt 4.4, so that this workaround can be skipped. +#define DOLPHIN_DRAGANDDROP + KCategorizedView::Private::Private(KCategorizedView *listView) : listView(listView) , categoryDrawer(0) , biggestItemSize(QSize(0, 0)) , mouseButtonPressed(false) + , rightMouseButtonPressed(false) , isDragging(false) , dragLeftViewport(false) , proxyModel(0) @@ -355,7 +361,7 @@ void KCategorizedView::Private::drawNewCategory(const QModelIndex &index, } QStyleOption optionCopy = option; - const QString category = proxyModel->data(index, KCategorizedSortFilterProxyModel::CategoryRole).toString(); + const QString category = proxyModel->data(index, KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString(); optionCopy.state &= ~QStyle::State_Selected; @@ -390,6 +396,8 @@ void KCategorizedView::Private::updateScrollbars() int lastItemBottom = cachedRectIndex(lastIndex).top() + listView->spacing() + (listView->gridSize().isEmpty() ? 0 : listView->gridSize().height()) - listView->viewport()->height(); + listView->horizontalScrollBar()->setRange(0, 0); + listView->verticalScrollBar()->setSingleStep(listView->viewport()->height() / 10); listView->verticalScrollBar()->setPageStep(listView->viewport()->height()); listView->verticalScrollBar()->setRange(0, lastItemBottom); @@ -418,18 +426,24 @@ void KCategorizedView::Private::layoutChanged(bool forceItemReload) { if ((listView->viewMode() == KCategorizedView::IconMode) && proxyModel && categoryDrawer && proxyModel->isCategorizedModel() && - (((modelSortRole != proxyModel->sortRole()) || + ((forceItemReload || + (modelSortRole != proxyModel->sortRole()) || (modelSortColumn != proxyModel->sortColumn()) || (modelSortOrder != proxyModel->sortOrder()) || - (modelCategorized != proxyModel->isCategorizedModel())) || forceItemReload)) + (modelLastRowCount != proxyModel->rowCount()) || + (modelCategorized != proxyModel->isCategorizedModel())))) { // Force the view to update all elements listView->rowsInsertedArtifficial(QModelIndex(), 0, proxyModel->rowCount() - 1); - modelSortRole = proxyModel->sortRole(); - modelSortColumn = proxyModel->sortColumn(); - modelCategorized = proxyModel->isCategorizedModel(); - modelSortOrder = proxyModel->sortOrder(); + if (!forceItemReload) + { + modelSortRole = proxyModel->sortRole(); + modelSortColumn = proxyModel->sortColumn(); + modelSortOrder = proxyModel->sortOrder(); + modelLastRowCount = proxyModel->rowCount(); + modelCategorized = proxyModel->isCategorizedModel(); + } } else if ((listView->viewMode() == KCategorizedView::IconMode) && proxyModel && categoryDrawer && proxyModel->isCategorizedModel()) @@ -486,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(); @@ -497,6 +510,7 @@ void KCategorizedView::setModel(QAbstractItemModel *model) d->modelIndexList.clear(); d->hovered = QModelIndex(); d->mouseButtonPressed = false; + d->rightMouseButtonPressed = false; if (d->proxyModel) { @@ -521,8 +535,9 @@ void KCategorizedView::setModel(QAbstractItemModel *model) { d->modelSortRole = d->proxyModel->sortRole(); d->modelSortColumn = d->proxyModel->sortColumn(); - d->modelCategorized = true; d->modelSortOrder = d->proxyModel->sortOrder(); + d->modelLastRowCount = d->proxyModel->rowCount(); + d->modelCategorized = d->proxyModel->isCategorizedModel(); QObject::connect(d->proxyModel, SIGNAL(layoutChanged()), @@ -538,7 +553,7 @@ void KCategorizedView::setModel(QAbstractItemModel *model) if (d->proxyModel->rowCount()) { - rowsInsertedArtifficial(QModelIndex(), 0, d->proxyModel->rowCount() - 1); + d->layoutChanged(true); } } else @@ -571,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(); @@ -582,6 +596,7 @@ void KCategorizedView::setCategoryDrawer(KCategoryDrawer *categoryDrawer) d->modelIndexList.clear(); d->hovered = QModelIndex(); d->mouseButtonPressed = false; + d->rightMouseButtonPressed = false; if (!categoryDrawer && d->proxyModel) { @@ -618,7 +633,10 @@ void KCategorizedView::setCategoryDrawer(KCategoryDrawer *categoryDrawer) { if (d->proxyModel) { - rowsInsertedArtifficial(QModelIndex(), 0, d->proxyModel->rowCount() - 1); + if (d->proxyModel->rowCount()) + { + d->layoutChanged(true); + } } } else @@ -644,8 +662,6 @@ QModelIndex KCategorizedView::indexAt(const QPoint &point) const index = item[0]; } - d->hovered = index; - return index; } @@ -654,7 +670,6 @@ void KCategorizedView::reset() QListView::reset(); d->lastSelection = QItemSelection(); - d->currentViewIndex = QModelIndex(); d->forcedSelectionPosition = 0; d->elementsInfo.clear(); d->elementsPosition.clear(); @@ -666,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) @@ -726,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; @@ -736,6 +755,7 @@ void KCategorizedView::paintEvent(QPaintEvent *event) // Redraw categories QStyleOptionViewItem otherOption; + bool intersectedInThePast = false; foreach (const QString &category, d->categories) { otherOption = option; @@ -744,11 +764,18 @@ void KCategorizedView::paintEvent(QPaintEvent *event) if (otherOption.rect.intersects(area)) { + intersectedInThePast = true; + QModelIndex indexToDraw = d->proxyModel->index(d->categoriesIndexes[category][0].row(), d->proxyModel->sortColumn()); d->drawNewCategory(indexToDraw, d->proxyModel->sortRole(), otherOption, &painter); } + else if (intersectedInThePast) + { + break; // the visible area has been finished, we don't need to keep asking, the rest won't intersect + // this is doable because we know that categories are correctly ordered on the list + } } if (d->mouseButtonPressed && !d->isDragging) @@ -824,65 +851,130 @@ void KCategorizedView::setSelection(const QRect &rect, if (flags & QItemSelectionModel::Clear) { - if (!rect.intersects(d->categoryVisualRect(d->hoveredCategory))) - { - d->lastSelection = QItemSelection(); - } + selectionModel()->clear(); + d->lastSelection.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, QItemSelectionModel::SelectCurrent); return; } - QItemSelection selection; + QModelIndex topLeft; + QModelIndex bottomRight; - if (!d->mouseButtonPressed) - { - selection = QItemSelection(dirtyIndexes[0], dirtyIndexes[0]); - d->currentViewIndex = dirtyIndexes[0]; - } - else + if (d->mouseButtonPressed || d->rightMouseButtonPressed) // selection with click + drag { - QModelIndex first = dirtyIndexes[0]; - QModelIndex last; + QItemSelection selection; + + 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; + // we have a different interval. non-contiguous items + if ((index.row() - prev.row()) > 1) { + selection << QItemSelectionRange(first, prev); first = index; } - last = index; + prev = index; } - if (last.isValid()) - selection << QItemSelectionRange(first, last); - } + selection << QItemSelectionRange(first, prev); + + if (flags & QItemSelectionModel::Current) + { + if (rect.topLeft() == rect.bottomRight()) + { + selectionModel()->setCurrentIndex(indexAt(rect.topLeft()), QItemSelectionModel::NoUpdate); + } - if (d->lastSelection.count()) - { - if ((selection.count() == 1) && (selection[0].indexes().count() == 1)) selection.merge(d->lastSelection, flags); + } else - selection.merge(d->lastSelection, QItemSelectionModel::Select); + { + selection.merge(selectionModel()->selection(), flags); + + selectionModel()->select(selection, QItemSelectionModel::SelectCurrent); + + return; + } + + selectionModel()->select(selection, flags); } + 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(); + } - selectionModel()->select(selection, flags); + 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()); + } + + QItemSelection selection(topLeft, bottomRight); + + selectionModel()->select(selection, flags); + } } void KCategorizedView::mouseMoveEvent(QMouseEvent *event) @@ -895,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(); @@ -938,10 +1041,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; } } @@ -959,15 +1058,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); @@ -981,39 +1087,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); break; } } } - d->lastSelection = selectionModel()->selection(); - if (d->hovered.isValid()) viewport()->update(visualRect(d->hovered)); else if (!d->hoveredCategory.isEmpty()) @@ -1035,10 +1131,13 @@ void KCategorizedView::startDrag(Qt::DropActions supportedActions) // ARGB window so it is no transparent. Use QAbstractItemView when // this is fixed on Qt. // QAbstractItemView::startDrag(supportedActions); +#if !defined(DOLPHIN_DRAGANDDROP) QListView::startDrag(supportedActions); +#endif d->isDragging = false; d->mouseButtonPressed = false; + d->rightMouseButtonPressed = false; viewport()->update(d->lastDraggedItemsRect); } @@ -1061,7 +1160,11 @@ void KCategorizedView::dragMoveEvent(QDragMoveEvent *event) if ((viewMode() != KCategorizedView::IconMode) || !d->proxyModel || !d->categoryDrawer || !d->proxyModel->isCategorizedModel()) { +#if defined(DOLPHIN_DRAGANDDROP) + QAbstractItemView::dragMoveEvent(event); +#else QListView::dragMoveEvent(event); +#endif return; } @@ -1072,7 +1175,20 @@ void KCategorizedView::dragLeaveEvent(QDragLeaveEvent *event) { d->dragLeftViewport = true; +#if defined(DOLPHIN_DRAGANDDROP) + QAbstractItemView::dragLeaveEvent(event); +#else QListView::dragLeaveEvent(event); +#endif +} + +void KCategorizedView::dropEvent(QDropEvent *event) +{ +#if defined(DOLPHIN_DRAGANDDROP) + QAbstractItemView::dropEvent(event); +#else + QListView::dropEvent(event); +#endif } QModelIndex KCategorizedView::moveCursor(CursorAction cursorAction, @@ -1088,7 +1204,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()); + selectionModel()->select(current, QItemSelectionModel::NoUpdate); + d->forcedSelectionPosition = 0; + + return current; + } int viewportWidth = viewport()->width() - spacing(); int itemWidth; @@ -1104,6 +1229,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(); @@ -1189,36 +1316,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); @@ -1238,8 +1385,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(); @@ -1251,6 +1396,7 @@ void KCategorizedView::rowsInserted(const QModelIndex &parent, d->hovered = QModelIndex(); d->biggestItemSize = QSize(0, 0); d->mouseButtonPressed = false; + d->rightMouseButtonPressed = false; return; } @@ -1264,8 +1410,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(); @@ -1277,6 +1421,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()) { @@ -1284,7 +1429,7 @@ void KCategorizedView::rowsInsertedArtifficial(const QModelIndex &parent, } // Add all elements mapped to the source model and explore categories - QString prevCategory = d->proxyModel->data(d->proxyModel->index(0, d->proxyModel->sortColumn()), KCategorizedSortFilterProxyModel::CategoryRole).toString(); + QString prevCategory = d->proxyModel->data(d->proxyModel->index(0, d->proxyModel->sortColumn()), KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString(); QString lastCategory = prevCategory; QModelIndexList modelIndexList; struct Private::ElementInfo elementInfo; @@ -1301,7 +1446,7 @@ void KCategorizedView::rowsInsertedArtifficial(const QModelIndex &parent, d->modelIndexList << index; - lastCategory = d->proxyModel->data(index, KCategorizedSortFilterProxyModel::CategoryRole).toString(); + lastCategory = d->proxyModel->data(index, KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString(); elementInfo.category = lastCategory; @@ -1329,6 +1474,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, @@ -1362,4 +1511,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"