X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/292e732ff91843c0c60c35f6cd2445ad23c96503..b607d3cc4bbce0ef496730b25dfe2ec7bf00ab7a:/src/kcategorizedview.cpp diff --git a/src/kcategorizedview.cpp b/src/kcategorizedview.cpp index 39e2e4b3f..e178760ee 100644 --- a/src/kcategorizedview.cpp +++ b/src/kcategorizedview.cpp @@ -33,7 +33,7 @@ #include #include "kitemcategorizer.h" -#include "ksortfilterproxymodel.h" +#include "dolphinsortfilterproxymodel.h" class LessThan { @@ -44,7 +44,7 @@ public: CategoryPurpose }; - inline LessThan(const KSortFilterProxyModel *proxyModel, + inline LessThan(const DolphinSortFilterProxyModel *proxyModel, Purpose purpose) : proxyModel(proxyModel) , purpose(purpose) @@ -67,7 +67,7 @@ public: } private: - const KSortFilterProxyModel *proxyModel; + const DolphinSortFilterProxyModel *proxyModel; const Purpose purpose; }; @@ -78,11 +78,11 @@ private: KCategorizedView::Private::Private(KCategorizedView *listView) : listView(listView) , itemCategorizer(0) + , biggestItemSize(QSize(0, 0)) , mouseButtonPressed(false) , isDragging(false) , dragLeftViewport(false) , proxyModel(0) - , lastIndex(QModelIndex()) { } @@ -97,6 +97,17 @@ const QModelIndexList &KCategorizedView::Private::intersectionSet(const QRect &r intersectedIndexes.clear(); + int itemHeight; + + if (listView->gridSize().isEmpty()) + { + itemHeight = biggestItemSize.height(); + } + else + { + itemHeight = listView->gridSize().height(); + } + // Lets find out where we should start int top = proxyModel->rowCount() - 1; int bottom = 0; @@ -107,10 +118,13 @@ const QModelIndexList &KCategorizedView::Private::intersectionSet(const QRect &r index = elementDictionary[proxyModel->index(middle, 0)]; indexVisualRect = visualRect(index); + // We need the whole height (not only the visualRect). This will help us to update + // all needed indexes correctly (ereslibre) + indexVisualRect.setHeight(indexVisualRect.height() + (itemHeight - indexVisualRect.height())); if (qMax(indexVisualRect.topLeft().y(), indexVisualRect.bottomRight().y()) < qMin(rect.topLeft().y(), - rect.bottomRight().y())) + rect.bottomRight().y())) { bottom = middle + 1; } @@ -150,14 +164,20 @@ QRect KCategorizedView::Private::visualRectInViewport(const QModelIndex &index) int viewportWidth = listView->viewport()->width() - listView->spacing(); - // We really need all items to be of same size. Otherwise we cannot do this - // (ereslibre) - // QSize itemSize = - // listView->sizeHintForIndex(proxyModel->mapFromSource(index)); - // int itemHeight = itemSize.height(); - // int itemWidth = itemSize.width();*/ - int itemHeight = 107; - int itemWidth = 130; + int itemHeight; + int itemWidth; + + if (listView->gridSize().isEmpty()) + { + itemHeight = biggestItemSize.height(); + itemWidth = biggestItemSize.width(); + } + else + { + itemHeight = listView->gridSize().height(); + itemWidth = listView->gridSize().width(); + } + int itemWidthPlusSeparation = listView->spacing() + itemWidth; int elementsPerRow = viewportWidth / itemWidthPlusSeparation; if (!elementsPerRow) @@ -181,17 +201,39 @@ QRect KCategorizedView::Private::visualRectInViewport(const QModelIndex &index) if (rows - trunc(rows)) rowsInt++; retRect.setTop(retRect.top() + - (rowsInt * listView->spacing()) + (rowsInt * itemHeight) + itemCategorizer->categoryHeight(listView->viewOptions()) + listView->spacing() * 2); + + if (listView->gridSize().isEmpty()) + { + retRect.setTop(retRect.top() + + (rowsInt * listView->spacing())); + } } - retRect.setTop(retRect.top() + row * listView->spacing() + - row * itemHeight); + + if (listView->gridSize().isEmpty()) + { + retRect.setTop(retRect.top() + row * listView->spacing() + + (row * itemHeight)); + } + else + { + retRect.setTop(retRect.top() + (row * itemHeight)); + } retRect.setWidth(itemWidth); - retRect.setHeight(itemHeight); + + if (listView->gridSize().isEmpty()) + { + retRect.setHeight(listView->sizeHintForIndex(proxyModel->mapFromSource(index)).height()); + } + else + { + retRect.setHeight(qMin(listView->sizeHintForIndex(proxyModel->mapFromSource(index)).height(), + listView->gridSize().height())); + } return retRect; } @@ -211,13 +253,20 @@ QRect KCategorizedView::Private::visualCategoryRectInViewport(const QString &cat int viewportWidth = listView->viewport()->width() - listView->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 itemHeight; + int itemWidth; + + if (listView->gridSize().isEmpty()) + { + itemHeight = biggestItemSize.height(); + itemWidth = biggestItemSize.width(); + } + else + { + itemHeight = listView->gridSize().height(); + itemWidth = listView->gridSize().width(); + } + int itemWidthPlusSeparation = listView->spacing() + itemWidth; int elementsPerRow = viewportWidth / itemWidthPlusSeparation; @@ -236,10 +285,15 @@ QRect KCategorizedView::Private::visualCategoryRectInViewport(const QString &cat if (rows - trunc(rows)) rowsInt++; retRect.setTop(retRect.top() + - (rowsInt * listView->spacing()) + (rowsInt * itemHeight) + itemCategorizer->categoryHeight(listView->viewOptions()) + listView->spacing() * 2); + + if (listView->gridSize().isEmpty()) + { + retRect.setTop(retRect.top() + + (rowsInt * listView->spacing())); + } } retRect.setHeight(itemCategorizer->categoryHeight(listView->viewOptions())); @@ -333,8 +387,11 @@ void KCategorizedView::Private::drawNewCategory(const QModelIndex &index, void KCategorizedView::Private::updateScrollbars() { - int lastItemBottom = cachedRectIndex(lastIndex).bottom() + - listView->spacing() - listView->viewport()->height(); + // find the last index in the last category + QModelIndex lastIndex = categoriesIndexes.isEmpty() ? QModelIndex() : categoriesIndexes[categories.last()].last(); + + int lastItemBottom = cachedRectIndex(lastIndex).top() + + listView->spacing() + (listView->gridSize().isEmpty() ? 0 : listView->gridSize().height()) - listView->viewport()->height(); listView->verticalScrollBar()->setSingleStep(listView->viewport()->height() / 10); listView->verticalScrollBar()->setPageStep(listView->viewport()->height()); @@ -428,7 +485,7 @@ void KCategorizedView::setModel(QAbstractItemModel *model) QListView::setModel(model); - d->proxyModel = dynamic_cast(model); + d->proxyModel = dynamic_cast(model); if (d->proxyModel) { @@ -552,6 +609,7 @@ void KCategorizedView::reset() d->intersectedIndexes.clear(); d->sourceModelIndexList.clear(); d->hovered = QModelIndex(); + d->biggestItemSize = QSize(0, 0); d->mouseButtonPressed = false; } @@ -566,6 +624,11 @@ void KCategorizedView::paintEvent(QPaintEvent *event) QStyleOptionViewItemV3 option = viewOptions(); option.widget = this; + if (wordWrap()) + { + option.features |= QStyleOptionViewItemV2::WrapText; + } + QPainter painter(viewport()); QRect area = event->rect(); const bool focus = (hasFocus() || viewport()->hasFocus()) && @@ -579,7 +642,7 @@ void KCategorizedView::paintEvent(QPaintEvent *event) foreach (const QModelIndex &index, dirtyIndexes) { option.state = state; - option.rect = d->visualRect(index); + option.rect = visualRect(index); if (selectionModel() && selectionModel()->isSelected(index)) { @@ -617,7 +680,6 @@ void KCategorizedView::paintEvent(QPaintEvent *event) } // Redraw categories - int i = 0; QStyleOptionViewItem otherOption; foreach (const QString &category, d->categories) { @@ -691,7 +753,7 @@ void KCategorizedView::resizeEvent(QResizeEvent *event) } void KCategorizedView::setSelection(const QRect &rect, - QItemSelectionModel::SelectionFlags flags) + QItemSelectionModel::SelectionFlags flags) { if ((viewMode() != KCategorizedView::IconMode) || !d->proxyModel || !d->itemCategorizer) @@ -865,10 +927,10 @@ void KCategorizedView::mouseReleaseEvent(QMouseEvent *event) { if (d->categoryVisualRect(category).contains(event->pos())) { - QItemSelectionRange selectionRange(d->proxyModel->mapFromSource(d->categoriesIndexes[category][0]), - d->proxyModel->mapFromSource(d->categoriesIndexes[category][d->categoriesIndexes[category].count() - 1])); - - selection << selectionRange; + foreach (const QModelIndex &index, d->categoriesIndexes[category]) + { + selection << QItemSelectionRange(d->proxyModel->mapFromSource(index)); + } selectionModel()->select(selection, QItemSelectionModel::Select); @@ -880,7 +942,7 @@ void KCategorizedView::mouseReleaseEvent(QMouseEvent *event) d->lastSelection = selectionModel()->selection(); if (d->hovered.isValid()) - viewport()->update(d->visualRect(d->hovered)); + viewport()->update(visualRect(d->hovered)); else if (!d->hoveredCategory.isEmpty()) viewport()->update(d->categoryVisualRect(d->hoveredCategory)); } @@ -938,8 +1000,11 @@ void KCategorizedView::dragLeaveEvent(QDragLeaveEvent *event) QModelIndex KCategorizedView::moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers) { - if ((viewMode() != KCategorizedView::IconMode) || !d->proxyModel || - !d->itemCategorizer) + if ( (viewMode() != KCategorizedView::IconMode) || + !d->proxyModel || + !d->itemCategorizer || + d->categories.isEmpty() + ) { return QListView::moveCursor(cursorAction, modifiers); } @@ -947,19 +1012,24 @@ QModelIndex KCategorizedView::moveCursor(CursorAction cursorAction, 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 itemWidth; + + if (gridSize().isEmpty()) + { + itemWidth = d->biggestItemSize.width(); + } + else + { + itemWidth = gridSize().width(); + } + int itemWidthPlusSeparation = spacing() + itemWidth; int elementsPerRow = viewportWidth / itemWidthPlusSeparation; - QString lastCategory = d->categories[0]; - QString theCategory = d->categories[0]; - QString afterCategory = d->categories[0]; + QString lastCategory = d->categories.first(); + QString theCategory = d->categories.first(); + QString afterCategory = d->categories.first(); + bool hasToBreak = false; foreach (const QString &category, d->categories) { @@ -1082,6 +1152,7 @@ void KCategorizedView::rowsInserted(const QModelIndex &parent, d->intersectedIndexes.clear(); d->sourceModelIndexList.clear(); d->hovered = QModelIndex(); + d->biggestItemSize = QSize(0, 0); d->mouseButtonPressed = false; return; @@ -1109,6 +1180,7 @@ void KCategorizedView::rowsInsertedArtifficial(const QModelIndex &parent, d->intersectedIndexes.clear(); d->sourceModelIndexList.clear(); d->hovered = QModelIndex(); + d->biggestItemSize = QSize(0, 0); d->mouseButtonPressed = false; if (start > end || end < 0 || start < 0 || !d->proxyModel->rowCount()) @@ -1119,6 +1191,11 @@ void KCategorizedView::rowsInsertedArtifficial(const QModelIndex &parent, // Add all elements mapped to the source model for (int k = 0; k < d->proxyModel->rowCount(); k++) { + d->biggestItemSize = QSize(qMax(sizeHintForIndex(d->proxyModel->index(k, 0)).width(), + d->biggestItemSize.width()), + qMax(sizeHintForIndex(d->proxyModel->index(k, 0)).height(), + d->biggestItemSize.height())); + d->sourceModelIndexList << d->proxyModel->mapToSource(d->proxyModel->index(k, 0)); } @@ -1172,8 +1249,6 @@ void KCategorizedView::rowsInsertedArtifficial(const QModelIndex &parent, qStableSort(indexList.begin(), indexList.end(), categoryLessThan); } - d->lastIndex = d->categoriesIndexes[d->categories[d->categories.count() - 1]][d->categoriesIndexes[d->categories[d->categories.count() - 1]].count() - 1]; - // Finally, fill data information of items situation. This will help when // trying to compute an item place in the viewport int i = 0; // position relative to the category beginning