2 * This file is part of the KDE project
3 * Copyright (C) 2007 Rafael Fernández López <ereslibre@gmail.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
21 #include "klistview.h"
22 #include "klistview_p.h"
24 #include <math.h> // trunc on C99 compliant systems
25 #include <kdefakes.h> // trunc for not C99 compliant systems
27 #include <QApplication>
30 #include <QPaintEvent>
35 #include "kitemcategorizer.h"
36 #include "ksortfilterproxymodel.h"
47 inline LessThan(const KSortFilterProxyModel
*proxyModel
,
49 : proxyModel(proxyModel
)
54 inline bool operator()(const QModelIndex
&left
,
55 const QModelIndex
&right
) const
57 if (purpose
== GeneralPurpose
)
59 return proxyModel
->sortOrder() == Qt::AscendingOrder
?
60 proxyModel
->lessThanGeneralPurpose(left
, right
) :
61 !proxyModel
->lessThanGeneralPurpose(left
, right
);
64 return proxyModel
->sortOrder() == Qt::AscendingOrder
?
65 proxyModel
->lessThanCategoryPurpose(left
, right
) :
66 !proxyModel
->lessThanCategoryPurpose(left
, right
);
70 const KSortFilterProxyModel
*proxyModel
;
71 const Purpose purpose
;
75 //==============================================================================
78 KListView::Private::Private(KListView
*listView
)
81 , mouseButtonPressed(false)
83 , dragLeftViewport(false)
85 , lastIndex(QModelIndex())
89 KListView::Private::~Private()
93 const QModelIndexList
&KListView::Private::intersectionSet(const QRect
&rect
)
96 QRect indexVisualRect
;
98 intersectedIndexes
.clear();
100 // Lets find out where we should start
101 int top
= proxyModel
->rowCount() - 1;
103 int middle
= (top
+ bottom
) / 2;
104 while (bottom
<= top
)
106 middle
= (top
+ bottom
) / 2;
108 index
= elementDictionary
[proxyModel
->index(middle
, 0)];
109 indexVisualRect
= visualRect(index
);
111 if (qMax(indexVisualRect
.topLeft().y(),
112 indexVisualRect
.bottomRight().y()) < qMin(rect
.topLeft().y(),
113 rect
.bottomRight().y()))
123 for (int i
= middle
; i
< proxyModel
->rowCount(); i
++)
125 index
= elementDictionary
[proxyModel
->index(i
, 0)];
126 indexVisualRect
= visualRect(index
);
128 if (rect
.intersects(indexVisualRect
))
129 intersectedIndexes
.append(index
);
131 // If we passed next item, stop searching for hits
132 if (qMax(rect
.bottomRight().y(), rect
.topLeft().y()) <
133 qMin(indexVisualRect
.topLeft().y(),
134 indexVisualRect
.bottomRight().y()))
138 return intersectedIndexes
;
141 QRect
KListView::Private::visualRectInViewport(const QModelIndex
&index
) const
143 if (!index
.isValid())
146 QString curCategory
= elementsInfo
[index
].category
;
148 QRect
retRect(listView
->spacing(), listView
->spacing() * 2 +
149 30 /* categoryHeight */, 0, 0);
151 int viewportWidth
= listView
->viewport()->width() - listView
->spacing();
153 // We really need all items to be of same size. Otherwise we cannot do this
156 // listView->sizeHintForIndex(proxyModel->mapFromSource(index));
157 // int itemHeight = itemSize.height();
158 // int itemWidth = itemSize.width();*/
159 int itemHeight
= 107;
161 int itemWidthPlusSeparation
= listView
->spacing() + itemWidth
;
162 int elementsPerRow
= viewportWidth
/ itemWidthPlusSeparation
;
166 int column
= elementsInfo
[index
].relativeOffsetToCategory
% elementsPerRow
;
167 int row
= elementsInfo
[index
].relativeOffsetToCategory
/ elementsPerRow
;
169 retRect
.setLeft(retRect
.left() + column
* listView
->spacing() +
174 foreach (const QString
&category
, categories
)
176 if (category
== curCategory
)
179 rows
= (float) ((float) categoriesIndexes
[category
].count() /
180 (float) elementsPerRow
);
181 rowsInt
= categoriesIndexes
[category
].count() / elementsPerRow
;
183 if (rows
- trunc(rows
)) rowsInt
++;
185 retRect
.setTop(retRect
.top() +
186 (rowsInt
* listView
->spacing()) +
187 (rowsInt
* itemHeight
) +
188 30 /* categoryHeight */ +
189 listView
->spacing() * 2);
192 retRect
.setTop(retRect
.top() + row
* listView
->spacing() +
195 retRect
.setWidth(itemWidth
);
196 retRect
.setHeight(itemHeight
);
201 QRect
KListView::Private::visualCategoryRectInViewport(const QString
&category
)
204 QRect
retRect(listView
->spacing(),
206 listView
->viewport()->width() - listView
->spacing() * 2,
209 if (!proxyModel
->rowCount() || !categories
.contains(category
))
212 QModelIndex index
= proxyModel
->index(0, 0, QModelIndex());
214 int viewportWidth
= listView
->viewport()->width() - listView
->spacing();
216 // We really need all items to be of same size. Otherwise we cannot do this
218 // QSize itemSize = listView->sizeHintForIndex(index);
219 // int itemHeight = itemSize.height();
220 // int itemWidth = itemSize.width();
221 int itemHeight
= 107;
223 int itemWidthPlusSeparation
= listView
->spacing() + itemWidth
;
224 int elementsPerRow
= viewportWidth
/ itemWidthPlusSeparation
;
231 foreach (const QString
&itCategory
, categories
)
233 if (itCategory
== category
)
236 rows
= (float) ((float) categoriesIndexes
[itCategory
].count() /
237 (float) elementsPerRow
);
238 rowsInt
= categoriesIndexes
[itCategory
].count() / elementsPerRow
;
240 if (rows
- trunc(rows
)) rowsInt
++;
242 retRect
.setTop(retRect
.top() +
243 (rowsInt
* listView
->spacing()) +
244 (rowsInt
* itemHeight
) +
245 30 /* categoryHeight */ +
246 listView
->spacing() * 2);
249 retRect
.setHeight(30 /* categoryHeight */);
254 // We're sure elementsPosition doesn't contain index
255 const QRect
&KListView::Private::cacheIndex(const QModelIndex
&index
)
257 QRect rect
= visualRectInViewport(index
);
258 elementsPosition
[index
] = rect
;
260 return elementsPosition
[index
];
263 // We're sure categoriesPosition doesn't contain category
264 const QRect
&KListView::Private::cacheCategory(const QString
&category
)
266 QRect rect
= visualCategoryRectInViewport(category
);
267 categoriesPosition
[category
] = rect
;
269 return categoriesPosition
[category
];
272 const QRect
&KListView::Private::cachedRectIndex(const QModelIndex
&index
)
274 if (elementsPosition
.contains(index
)) // If we have it cached
276 return elementsPosition
[index
];
278 else // Otherwise, cache it
280 return cacheIndex(index
);
284 const QRect
&KListView::Private::cachedRectCategory(const QString
&category
)
286 if (categoriesPosition
.contains(category
)) // If we have it cached
288 return categoriesPosition
[category
];
290 else // Otherwise, cache it and
292 return cacheCategory(category
);
296 QRect
KListView::Private::visualRect(const QModelIndex
&index
)
298 QModelIndex mappedIndex
= proxyModel
->mapToSource(index
);
300 QRect retRect
= cachedRectIndex(mappedIndex
);
301 int dx
= -listView
->horizontalOffset();
302 int dy
= -listView
->verticalOffset();
303 retRect
.adjust(dx
, dy
, dx
, dy
);
308 QRect
KListView::Private::categoryVisualRect(const QString
&category
)
310 QRect retRect
= cachedRectCategory(category
);
311 int dx
= -listView
->horizontalOffset();
312 int dy
= -listView
->verticalOffset();
313 retRect
.adjust(dx
, dy
, dx
, dy
);
318 void KListView::Private::drawNewCategory(const QString
&category
,
319 const QStyleOption
&option
,
322 QColor color
= option
.palette
.color(QPalette::Text
);
325 painter
->setRenderHint(QPainter::Antialiasing
);
327 QStyleOptionButton opt
;
329 opt
.rect
= option
.rect
;
330 opt
.palette
= option
.palette
;
331 opt
.direction
= option
.direction
;
334 if ((category
== hoveredCategory
) && !mouseButtonPressed
)
336 const QPalette::ColorGroup group
=
337 option
.state
& QStyle::State_Enabled
?
338 QPalette::Normal
: QPalette::Disabled
;
340 QLinearGradient
gradient(option
.rect
.topLeft(),
341 option
.rect
.bottomRight());
342 gradient
.setColorAt(0,
343 option
.palette
.color(group
,
344 QPalette::Highlight
).light());
345 gradient
.setColorAt(1, Qt::transparent
);
347 painter
->fillRect(option
.rect
, gradient
);
350 /*if (const KStyle *style = dynamic_cast<const KStyle*>(QApplication::style()))
352 style->drawControl(KStyle::CE_Category, &opt, painter, this);
356 QFont painterFont
= painter
->font();
357 painterFont
.setWeight(QFont::Bold
);
358 QFontMetrics
metrics(painterFont
);
359 painter
->setFont(painterFont
);
362 path
.addRect(option
.rect
.left(),
363 option
.rect
.bottom() - 2,
367 QLinearGradient
gradient(option
.rect
.topLeft(),
368 option
.rect
.bottomRight());
369 gradient
.setColorAt(0, color
);
370 gradient
.setColorAt(1, Qt::transparent
);
372 painter
->setBrush(gradient
);
373 painter
->fillPath(path
, gradient
);
375 painter
->setPen(color
);
377 painter
->drawText(option
.rect
, Qt::AlignVCenter
| Qt::AlignLeft
,
378 metrics
.elidedText(category
, Qt::ElideRight
, option
.rect
.width()));
384 void KListView::Private::updateScrollbars()
386 int lastItemBottom
= cachedRectIndex(lastIndex
).bottom() +
387 listView
->spacing() - listView
->viewport()->height();
389 listView
->verticalScrollBar()->setSingleStep(listView
->viewport()->height() / 10);
390 listView
->verticalScrollBar()->setPageStep(listView
->viewport()->height());
391 listView
->verticalScrollBar()->setRange(0, lastItemBottom
);
394 void KListView::Private::drawDraggedItems(QPainter
*painter
)
396 QStyleOptionViewItemV3 option
= listView
->viewOptions();
397 option
.state
&= ~QStyle::State_MouseOver
;
400 foreach (const QModelIndex
&index
, listView
->selectionModel()->selectedIndexes())
402 dx
= mousePosition
.x() - initialPressPosition
.x() + listView
->horizontalOffset();
403 dy
= mousePosition
.y() - initialPressPosition
.y() + listView
->verticalOffset();
405 option
.rect
= visualRect(index
);
406 option
.rect
.adjust(dx
, dy
, dx
, dy
);
408 listView
->itemDelegate(index
)->paint(painter
, option
, index
);
413 //==============================================================================
416 KListView::KListView(QWidget
*parent
)
418 , d(new Private(this))
422 KListView::~KListView()
427 void KListView::setModel(QAbstractItemModel
*model
)
431 QObject::disconnect(d
->proxyModel
,
432 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
433 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
435 QObject::disconnect(d
->proxyModel
,
436 SIGNAL(sortingRoleChanged()),
437 this, SLOT(slotSortingRoleChanged()));
440 QListView::setModel(model
);
442 d
->proxyModel
= dynamic_cast<KSortFilterProxyModel
*>(model
);
446 QObject::connect(d
->proxyModel
,
447 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
448 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
450 QObject::connect(d
->proxyModel
,
451 SIGNAL(sortingRoleChanged()),
452 this, SLOT(slotSortingRoleChanged()));
456 QRect
KListView::visualRect(const QModelIndex
&index
) const
458 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
461 return QListView::visualRect(index
);
464 if (!qobject_cast
<const QSortFilterProxyModel
*>(index
.model()))
466 return d
->visualRect(d
->proxyModel
->mapFromSource(index
));
469 return d
->visualRect(index
);
472 KItemCategorizer
*KListView::itemCategorizer() const
474 return d
->itemCategorizer
;
477 void KListView::setItemCategorizer(KItemCategorizer
*itemCategorizer
)
479 if (!itemCategorizer
&& d
->proxyModel
)
481 QObject::disconnect(d
->proxyModel
,
482 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
483 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
485 QObject::disconnect(d
->proxyModel
,
486 SIGNAL(sortingRoleChanged()),
487 this, SLOT(slotSortingRoleChanged()));
489 else if (itemCategorizer
&& d
->proxyModel
)
491 QObject::connect(d
->proxyModel
,
492 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
493 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
495 QObject::connect(d
->proxyModel
,
496 SIGNAL(sortingRoleChanged()),
497 this, SLOT(slotSortingRoleChanged()));
500 d
->itemCategorizer
= itemCategorizer
;
504 rowsInserted(QModelIndex(), 0, d
->proxyModel
->rowCount() - 1);
512 QModelIndex
KListView::indexAt(const QPoint
&point
) const
514 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
517 return QListView::indexAt(point
);
522 QModelIndexList item
= d
->intersectionSet(QRect(point
, point
));
524 if (item
.count() == 1)
534 void KListView::reset()
538 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
544 d
->elementsInfo
.clear();
545 d
->elementsPosition
.clear();
546 d
->elementDictionary
.clear();
547 d
->categoriesIndexes
.clear();
548 d
->categoriesPosition
.clear();
549 d
->categories
.clear();
550 d
->intersectedIndexes
.clear();
551 d
->sourceModelIndexList
.clear();
552 d
->hovered
= QModelIndex();
553 d
->mouseButtonPressed
= false;
556 void KListView::paintEvent(QPaintEvent
*event
)
558 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
561 QListView::paintEvent(event
);
565 QStyleOptionViewItemV3 option
= viewOptions();
566 QPainter
painter(viewport());
567 QRect area
= event
->rect();
568 const bool focus
= (hasFocus() || viewport()->hasFocus()) &&
569 currentIndex().isValid();
570 const QStyle::State state
= option
.state
;
571 const bool enabled
= (state
& QStyle::State_Enabled
) != 0;
575 QModelIndexList dirtyIndexes
= d
->intersectionSet(area
);
576 foreach (const QModelIndex
&index
, dirtyIndexes
)
578 option
.state
= state
;
579 option
.rect
= d
->visualRect(index
);
581 if (selectionModel() && selectionModel()->isSelected(index
))
583 option
.state
|= QStyle::State_Selected
;
588 QPalette::ColorGroup cg
;
589 if ((d
->proxyModel
->flags(index
) & Qt::ItemIsEnabled
) == 0)
591 option
.state
&= ~QStyle::State_Enabled
;
592 cg
= QPalette::Disabled
;
596 cg
= QPalette::Normal
;
598 option
.palette
.setCurrentColorGroup(cg
);
601 if (focus
&& currentIndex() == index
)
603 option
.state
|= QStyle::State_HasFocus
;
604 if (this->state() == EditingState
)
605 option
.state
|= QStyle::State_Editing
;
608 if ((index
== d
->hovered
) && !d
->mouseButtonPressed
)
609 option
.state
|= QStyle::State_MouseOver
;
611 option
.state
&= ~QStyle::State_MouseOver
;
613 itemDelegate(index
)->paint(&painter
, option
, index
);
617 QStyleOptionViewItem otherOption
;
618 foreach (const QString
&category
, d
->categories
)
620 otherOption
= option
;
621 otherOption
.rect
= d
->categoryVisualRect(category
);
623 if (otherOption
.rect
.intersects(area
))
625 d
->drawNewCategory(category
, otherOption
, &painter
);
629 if (d
->mouseButtonPressed
&& !d
->isDragging
)
631 QPoint start
, end
, initialPressPosition
;
633 initialPressPosition
= d
->initialPressPosition
;
635 initialPressPosition
.setY(initialPressPosition
.y() - verticalOffset());
636 initialPressPosition
.setX(initialPressPosition
.x() - horizontalOffset());
638 if (d
->initialPressPosition
.x() > d
->mousePosition
.x() ||
639 d
->initialPressPosition
.y() > d
->mousePosition
.y())
641 start
= d
->mousePosition
;
642 end
= initialPressPosition
;
646 start
= initialPressPosition
;
647 end
= d
->mousePosition
;
650 QStyleOptionRubberBand yetAnotherOption
;
651 yetAnotherOption
.initFrom(this);
652 yetAnotherOption
.shape
= QRubberBand::Rectangle
;
653 yetAnotherOption
.opaque
= false;
654 yetAnotherOption
.rect
= QRect(start
, end
).intersected(viewport()->rect().adjusted(-16, -16, 16, 16));
656 style()->drawControl(QStyle::CE_RubberBand
, &yetAnotherOption
, &painter
);
660 if (d
->isDragging
&& !d
->dragLeftViewport
)
662 painter
.setOpacity(0.5);
663 d
->drawDraggedItems(&painter
);
669 void KListView::resizeEvent(QResizeEvent
*event
)
671 QListView::resizeEvent(event
);
673 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
679 // Clear the items positions cache
680 d
->elementsPosition
.clear();
681 d
->categoriesPosition
.clear();
683 d
->updateScrollbars();
686 void KListView::setSelection(const QRect
&rect
,
687 QItemSelectionModel::SelectionFlags flags
)
689 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
692 QListView::setSelection(rect
, flags
);
699 selectionModel()->clear();
701 if (flags
& QItemSelectionModel::Clear
)
703 d
->lastSelection
= QItemSelection();
706 QModelIndexList dirtyIndexes
= d
->intersectionSet(rect
);
707 QItemSelection selection
;
709 if (!dirtyIndexes
.count())
711 if (d
->lastSelection
.count())
713 selectionModel()->select(d
->lastSelection
, flags
);
719 if (!d
->mouseButtonPressed
)
721 selection
= QItemSelection(dirtyIndexes
[0], dirtyIndexes
[0]);
725 QModelIndex first
= dirtyIndexes
[0];
727 foreach (const QModelIndex
&index
, dirtyIndexes
)
729 if (last
.isValid() && last
.row() + 1 != index
.row())
731 QItemSelectionRange
range(first
, last
);
742 selection
<< QItemSelectionRange(first
, last
);
745 if (d
->lastSelection
.count() && !d
->mouseButtonPressed
)
747 selection
.merge(d
->lastSelection
, flags
);
749 else if (d
->lastSelection
.count())
751 selection
.merge(d
->lastSelection
, QItemSelectionModel::Select
);
754 selectionModel()->select(selection
, flags
);
757 void KListView::mouseMoveEvent(QMouseEvent
*event
)
759 QListView::mouseMoveEvent(event
);
761 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
767 d
->mousePosition
= event
->pos();
768 d
->hoveredCategory
= QString();
771 foreach (const QString
&category
, d
->categories
)
773 if (d
->categoryVisualRect(category
).intersects(QRect(event
->pos(), event
->pos())))
775 d
->hoveredCategory
= category
;
778 viewport()->update(d
->categoryVisualRect(category
));
782 void KListView::mousePressEvent(QMouseEvent
*event
)
784 QListView::mousePressEvent(event
);
786 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
792 d
->dragLeftViewport
= false;
794 if (event
->button() == Qt::LeftButton
)
796 d
->mouseButtonPressed
= true;
798 d
->initialPressPosition
= event
->pos();
799 d
->initialPressPosition
.setY(d
->initialPressPosition
.y() +
801 d
->initialPressPosition
.setX(d
->initialPressPosition
.x() +
806 void KListView::mouseReleaseEvent(QMouseEvent
*event
)
808 QListView::mouseReleaseEvent(event
);
810 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
816 d
->mouseButtonPressed
= false;
818 QPoint initialPressPosition
= viewport()->mapFromGlobal(QCursor::pos());
819 initialPressPosition
.setY(initialPressPosition
.y() + verticalOffset());
820 initialPressPosition
.setX(initialPressPosition
.x() + horizontalOffset());
822 QItemSelection selection
;
824 if (initialPressPosition
== d
->initialPressPosition
)
826 foreach(const QString
&category
, d
->categories
)
828 if (d
->categoryVisualRect(category
).contains(event
->pos()))
830 QItemSelectionRange
selectionRange(d
->proxyModel
->mapFromSource(d
->categoriesIndexes
[category
][0]),
831 d
->proxyModel
->mapFromSource(d
->categoriesIndexes
[category
][d
->categoriesIndexes
[category
].count() - 1]));
833 selection
<< selectionRange
;
835 selectionModel()->select(selection
, QItemSelectionModel::Select
);
842 d
->lastSelection
= selectionModel()->selection();
844 if (d
->hovered
.isValid())
845 viewport()->update(d
->visualRect(d
->hovered
));
846 else if (!d
->hoveredCategory
.isEmpty())
847 viewport()->update(d
->categoryVisualRect(d
->hoveredCategory
));
850 void KListView::leaveEvent(QEvent
*event
)
852 QListView::leaveEvent(event
);
854 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
860 d
->hovered
= QModelIndex();
861 d
->hoveredCategory
= QString();
864 void KListView::startDrag(Qt::DropActions supportedActions
)
866 QListView::startDrag(supportedActions
);
868 d
->isDragging
= false;
869 d
->mouseButtonPressed
= false;
872 void KListView::dragMoveEvent(QDragMoveEvent
*event
)
874 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
877 QListView::dragMoveEvent(event
);
881 d
->mousePosition
= event
->pos();
883 if (d
->mouseButtonPressed
)
885 d
->isDragging
= true;
889 d
->isDragging
= false;
892 d
->dragLeftViewport
= false;
895 void KListView::dragLeaveEvent(QDragLeaveEvent
*event
)
897 QListView::dragLeaveEvent(event
);
899 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
905 d
->dragLeftViewport
= true;
908 QModelIndex
KListView::moveCursor(CursorAction cursorAction
,
909 Qt::KeyboardModifiers modifiers
)
911 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
914 return QListView::moveCursor(cursorAction
, modifiers
);
917 return QListView::moveCursor(cursorAction
, modifiers
);
920 void KListView::rowsInserted(const QModelIndex
&parent
,
924 QListView::rowsInserted(parent
, start
, end
);
926 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
932 rowsInsertedArtifficial(parent
, start
, end
);
935 void KListView::rowsInsertedArtifficial(const QModelIndex
&parent
,
939 d
->lastSelection
= QItemSelection();
940 d
->elementsInfo
.clear();
941 d
->elementsPosition
.clear();
942 d
->elementDictionary
.clear();
943 d
->categoriesIndexes
.clear();
944 d
->categoriesPosition
.clear();
945 d
->categories
.clear();
946 d
->intersectedIndexes
.clear();
947 d
->sourceModelIndexList
.clear();
948 d
->hovered
= QModelIndex();
949 d
->mouseButtonPressed
= false;
951 if (start
> end
|| end
< 0 || start
< 0 || !d
->proxyModel
->rowCount())
956 // Add all elements mapped to the source model
957 for (int k
= 0; k
< d
->proxyModel
->rowCount(); k
++)
959 d
->sourceModelIndexList
<<
960 d
->proxyModel
->mapToSource(d
->proxyModel
->index(k
, 0));
963 // Sort them with the general purpose lessThan method
964 LessThan
generalLessThan(d
->proxyModel
,
965 LessThan::GeneralPurpose
);
967 qStableSort(d
->sourceModelIndexList
.begin(), d
->sourceModelIndexList
.end(),
970 // Explore categories
971 QString prevCategory
=
972 d
->itemCategorizer
->categoryForItem(d
->sourceModelIndexList
[0],
973 d
->proxyModel
->sortRole());
974 QString lastCategory
= prevCategory
;
975 QModelIndexList modelIndexList
;
976 struct Private::ElementInfo elementInfo
;
977 foreach (const QModelIndex
&index
, d
->sourceModelIndexList
)
979 lastCategory
= d
->itemCategorizer
->categoryForItem(index
,
980 d
->proxyModel
->sortRole());
982 elementInfo
.category
= lastCategory
;
984 if (prevCategory
!= lastCategory
)
986 d
->categoriesIndexes
.insert(prevCategory
, modelIndexList
);
987 d
->categories
<< prevCategory
;
988 modelIndexList
.clear();
991 modelIndexList
<< index
;
992 prevCategory
= lastCategory
;
994 d
->elementsInfo
.insert(index
, elementInfo
);
997 d
->categoriesIndexes
.insert(prevCategory
, modelIndexList
);
998 d
->categories
<< prevCategory
;
1000 // Sort items locally in their respective categories with the category
1002 LessThan
categoryLessThan(d
->proxyModel
,
1003 LessThan::CategoryPurpose
);
1005 foreach (const QString
&key
, d
->categories
)
1007 QModelIndexList
&indexList
= d
->categoriesIndexes
[key
];
1009 qStableSort(indexList
.begin(), indexList
.end(), categoryLessThan
);
1012 d
->lastIndex
= d
->categoriesIndexes
[d
->categories
[d
->categories
.count() - 1]][d
->categoriesIndexes
[d
->categories
[d
->categories
.count() - 1]].count() - 1];
1014 // Finally, fill data information of items situation. This will help when
1015 // trying to compute an item place in the viewport
1016 int i
= 0; // position relative to the category beginning
1017 int j
= 0; // number of elements before current
1018 foreach (const QString
&key
, d
->categories
)
1020 foreach (const QModelIndex
&index
, d
->categoriesIndexes
[key
])
1022 struct Private::ElementInfo
&elementInfo
= d
->elementsInfo
[index
];
1024 elementInfo
.relativeOffsetToCategory
= i
;
1026 d
->elementDictionary
.insert(d
->proxyModel
->index(j
, 0),
1027 d
->proxyModel
->mapFromSource(index
));
1036 d
->updateScrollbars();
1039 void KListView::rowsRemoved(const QModelIndex
&parent
,
1043 if ((viewMode() == KListView::IconMode
) && d
->proxyModel
&&
1046 // Force the view to update all elements
1047 rowsInsertedArtifficial(parent
, start
, end
);
1051 void KListView::updateGeometries()
1053 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
1054 !d
->itemCategorizer
)
1056 QListView::updateGeometries();
1060 // Avoid QListView::updateGeometries(), since it will try to set another
1061 // range to our scroll bars, what we don't want (ereslibre)
1062 QAbstractItemView::updateGeometries();
1065 void KListView::slotSortingRoleChanged()
1067 if ((viewMode() == KListView::IconMode
) && d
->proxyModel
&&
1070 // Force the view to update all elements
1071 rowsInsertedArtifficial(QModelIndex(), 0, d
->proxyModel
->rowCount() - 1);
1075 #include "klistview.moc"