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()))
124 for (int i
= middle
; i
< proxyModel
->rowCount(); i
++)
126 index
= elementDictionary
[proxyModel
->index(i
, 0)];
127 indexVisualRect
= visualRect(index
);
129 if (rect
.intersects(indexVisualRect
))
130 intersectedIndexes
.append(index
);
132 // If we passed next item, stop searching for hits
133 if (qMax(rect
.bottomRight().y(), rect
.topLeft().y()) <
134 indexVisualRect
.topLeft().y())
140 return intersectedIndexes
;
143 QRect
KListView::Private::visualRectInViewport(const QModelIndex
&index
) const
145 if (!index
.isValid())
148 QString curCategory
= elementsInfo
[index
].category
;
150 QRect
retRect(listView
->spacing(), listView
->spacing() * 2 +
151 30 /* categoryHeight */, 0, 0);
153 int viewportWidth
= listView
->viewport()->width() - listView
->spacing();
155 // We really need all items to be of same size. Otherwise we cannot do this
158 // listView->sizeHintForIndex(proxyModel->mapFromSource(index));
159 // int itemHeight = itemSize.height();
160 // int itemWidth = itemSize.width();*/
161 int itemHeight
= 107;
163 int itemWidthPlusSeparation
= listView
->spacing() + itemWidth
;
164 int elementsPerRow
= viewportWidth
/ itemWidthPlusSeparation
;
168 int column
= elementsInfo
[index
].relativeOffsetToCategory
% elementsPerRow
;
169 int row
= elementsInfo
[index
].relativeOffsetToCategory
/ elementsPerRow
;
171 retRect
.setLeft(retRect
.left() + column
* listView
->spacing() +
176 foreach (const QString
&category
, categories
)
178 if (category
== curCategory
)
181 rows
= (float) ((float) categoriesIndexes
[category
].count() /
182 (float) elementsPerRow
);
183 rowsInt
= categoriesIndexes
[category
].count() / elementsPerRow
;
185 if (rows
- trunc(rows
)) rowsInt
++;
187 retRect
.setTop(retRect
.top() +
188 (rowsInt
* listView
->spacing()) +
189 (rowsInt
* itemHeight
) +
190 30 /* categoryHeight */ +
191 listView
->spacing() * 2);
194 retRect
.setTop(retRect
.top() + row
* listView
->spacing() +
197 retRect
.setWidth(itemWidth
);
198 retRect
.setHeight(itemHeight
);
203 QRect
KListView::Private::visualCategoryRectInViewport(const QString
&category
)
206 QRect
retRect(listView
->spacing(),
208 listView
->viewport()->width() - listView
->spacing() * 2,
211 if (!proxyModel
->rowCount() || !categories
.contains(category
))
214 QModelIndex index
= proxyModel
->index(0, 0, QModelIndex());
216 int viewportWidth
= listView
->viewport()->width() - listView
->spacing();
218 // We really need all items to be of same size. Otherwise we cannot do this
220 // QSize itemSize = listView->sizeHintForIndex(index);
221 // int itemHeight = itemSize.height();
222 // int itemWidth = itemSize.width();
223 int itemHeight
= 107;
225 int itemWidthPlusSeparation
= listView
->spacing() + itemWidth
;
226 int elementsPerRow
= viewportWidth
/ itemWidthPlusSeparation
;
233 foreach (const QString
&itCategory
, categories
)
235 if (itCategory
== category
)
238 rows
= (float) ((float) categoriesIndexes
[itCategory
].count() /
239 (float) elementsPerRow
);
240 rowsInt
= categoriesIndexes
[itCategory
].count() / elementsPerRow
;
242 if (rows
- trunc(rows
)) rowsInt
++;
244 retRect
.setTop(retRect
.top() +
245 (rowsInt
* listView
->spacing()) +
246 (rowsInt
* itemHeight
) +
247 30 /* categoryHeight */ +
248 listView
->spacing() * 2);
251 retRect
.setHeight(30 /* categoryHeight */);
256 // We're sure elementsPosition doesn't contain index
257 const QRect
&KListView::Private::cacheIndex(const QModelIndex
&index
)
259 QRect rect
= visualRectInViewport(index
);
260 elementsPosition
[index
] = rect
;
262 return elementsPosition
[index
];
265 // We're sure categoriesPosition doesn't contain category
266 const QRect
&KListView::Private::cacheCategory(const QString
&category
)
268 QRect rect
= visualCategoryRectInViewport(category
);
269 categoriesPosition
[category
] = rect
;
271 return categoriesPosition
[category
];
274 const QRect
&KListView::Private::cachedRectIndex(const QModelIndex
&index
)
276 if (elementsPosition
.contains(index
)) // If we have it cached
278 return elementsPosition
[index
];
280 else // Otherwise, cache it
282 return cacheIndex(index
);
286 const QRect
&KListView::Private::cachedRectCategory(const QString
&category
)
288 if (categoriesPosition
.contains(category
)) // If we have it cached
290 return categoriesPosition
[category
];
292 else // Otherwise, cache it and
294 return cacheCategory(category
);
298 QRect
KListView::Private::visualRect(const QModelIndex
&index
)
300 QModelIndex mappedIndex
= proxyModel
->mapToSource(index
);
302 QRect retRect
= cachedRectIndex(mappedIndex
);
303 int dx
= -listView
->horizontalOffset();
304 int dy
= -listView
->verticalOffset();
305 retRect
.adjust(dx
, dy
, dx
, dy
);
310 QRect
KListView::Private::categoryVisualRect(const QString
&category
)
312 QRect retRect
= cachedRectCategory(category
);
313 int dx
= -listView
->horizontalOffset();
314 int dy
= -listView
->verticalOffset();
315 retRect
.adjust(dx
, dy
, dx
, dy
);
320 void KListView::Private::drawNewCategory(const QString
&category
,
321 const QStyleOption
&option
,
324 QColor color
= option
.palette
.color(QPalette::Text
);
327 painter
->setRenderHint(QPainter::Antialiasing
);
329 QStyleOptionButton opt
;
331 opt
.rect
= option
.rect
;
332 opt
.palette
= option
.palette
;
333 opt
.direction
= option
.direction
;
336 if (option
.rect
.contains(listView
->viewport()->mapFromGlobal(QCursor::pos())) &&
339 const QPalette::ColorGroup group
=
340 option
.state
& QStyle::State_Enabled
?
341 QPalette::Normal
: QPalette::Disabled
;
343 QLinearGradient
gradient(option
.rect
.topLeft(),
344 option
.rect
.bottomRight());
345 gradient
.setColorAt(0,
346 option
.palette
.color(group
,
347 QPalette::Highlight
).light());
348 gradient
.setColorAt(1, Qt::transparent
);
350 painter
->fillRect(option
.rect
, gradient
);
353 /*if (const KStyle *style = dynamic_cast<const KStyle*>(QApplication::style()))
355 style->drawControl(KStyle::CE_Category, &opt, painter, this);
359 QFont painterFont
= painter
->font();
360 painterFont
.setWeight(QFont::Bold
);
361 QFontMetrics
metrics(painterFont
);
362 painter
->setFont(painterFont
);
365 path
.addRect(option
.rect
.left(),
366 option
.rect
.bottom() - 2,
370 QLinearGradient
gradient(option
.rect
.topLeft(),
371 option
.rect
.bottomRight());
372 gradient
.setColorAt(0, color
);
373 gradient
.setColorAt(1, Qt::transparent
);
375 painter
->setBrush(gradient
);
376 painter
->fillPath(path
, gradient
);
378 painter
->setPen(color
);
380 painter
->drawText(option
.rect
, Qt::AlignVCenter
| Qt::AlignLeft
,
381 metrics
.elidedText(category
, Qt::ElideRight
, option
.rect
.width()));
387 void KListView::Private::updateScrollbars()
389 int lastItemBottom
= cachedRectIndex(lastIndex
).bottom() +
390 listView
->spacing() - listView
->viewport()->height();
392 listView
->verticalScrollBar()->setSingleStep(listView
->viewport()->height() / 10);
393 listView
->verticalScrollBar()->setPageStep(listView
->viewport()->height());
394 listView
->verticalScrollBar()->setRange(0, lastItemBottom
);
397 void KListView::Private::drawDraggedItems(QPainter
*painter
)
399 QStyleOptionViewItemV3 option
= listView
->viewOptions();
400 option
.state
&= ~QStyle::State_MouseOver
;
401 foreach (const QModelIndex
&index
, listView
->selectionModel()->selectedIndexes())
403 int dx
= mousePosition
.x() - initialPressPosition
.x() + listView
->horizontalOffset();
404 int dy
= mousePosition
.y() - initialPressPosition
.y() + listView
->verticalOffset();
406 option
.rect
= visualRect(index
);
407 option
.rect
.adjust(dx
, dy
, dx
, dy
);
409 listView
->itemDelegate(index
)->paint(painter
, option
, index
);
414 //==============================================================================
417 KListView::KListView(QWidget
*parent
)
419 , d(new Private(this))
423 KListView::~KListView()
428 void KListView::setModel(QAbstractItemModel
*model
)
432 QObject::disconnect(d
->proxyModel
,
433 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
434 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
436 QObject::disconnect(d
->proxyModel
,
437 SIGNAL(sortingRoleChanged()),
438 this, SLOT(slotSortingRoleChanged()));
441 QListView::setModel(model
);
443 d
->proxyModel
= dynamic_cast<KSortFilterProxyModel
*>(model
);
447 QObject::connect(d
->proxyModel
,
448 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
449 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
451 QObject::connect(d
->proxyModel
,
452 SIGNAL(sortingRoleChanged()),
453 this, SLOT(slotSortingRoleChanged()));
457 QRect
KListView::visualRect(const QModelIndex
&index
) const
459 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
462 return QListView::visualRect(index
);
465 if (!qobject_cast
<const QSortFilterProxyModel
*>(index
.model()))
467 return d
->visualRect(d
->proxyModel
->mapFromSource(index
));
470 return d
->visualRect(index
);
473 KItemCategorizer
*KListView::itemCategorizer() const
475 return d
->itemCategorizer
;
478 void KListView::setItemCategorizer(KItemCategorizer
*itemCategorizer
)
480 if (!itemCategorizer
&& d
->proxyModel
)
482 QObject::disconnect(d
->proxyModel
,
483 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
484 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
486 QObject::disconnect(d
->proxyModel
,
487 SIGNAL(sortingRoleChanged()),
488 this, SLOT(slotSortingRoleChanged()));
490 else if (itemCategorizer
&& d
->proxyModel
)
492 QObject::connect(d
->proxyModel
,
493 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
494 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
496 QObject::connect(d
->proxyModel
,
497 SIGNAL(sortingRoleChanged()),
498 this, SLOT(slotSortingRoleChanged()));
501 d
->itemCategorizer
= itemCategorizer
;
505 rowsInserted(QModelIndex(), 0, d
->proxyModel
->rowCount() - 1);
513 QModelIndex
KListView::indexAt(const QPoint
&point
) const
515 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
518 return QListView::indexAt(point
);
523 QModelIndexList item
= d
->intersectionSet(QRect(point
, point
));
525 if (item
.count() == 1)
535 void KListView::reset()
539 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
545 d
->elementsInfo
.clear();
546 d
->elementsPosition
.clear();
547 d
->elementDictionary
.clear();
548 d
->categoriesIndexes
.clear();
549 d
->categoriesPosition
.clear();
550 d
->categories
.clear();
551 d
->intersectedIndexes
.clear();
552 d
->sourceModelIndexList
.clear();
553 d
->hovered
= QModelIndex();
554 d
->mouseButtonPressed
= false;
557 void KListView::paintEvent(QPaintEvent
*event
)
559 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
562 QListView::paintEvent(event
);
566 QStyleOptionViewItemV3 option
= viewOptions();
567 QPainter
painter(viewport());
568 QRect area
= event
->rect();
569 const bool focus
= (hasFocus() || viewport()->hasFocus()) &&
570 currentIndex().isValid();
571 const QStyle::State state
= option
.state
;
572 const bool enabled
= (state
& QStyle::State_Enabled
) != 0;
576 QModelIndexList dirtyIndexes
= d
->intersectionSet(area
);
577 foreach (const QModelIndex
&index
, dirtyIndexes
)
579 option
.state
= state
;
580 option
.rect
= d
->visualRect(index
);
582 if (selectionModel() && selectionModel()->isSelected(index
))
584 option
.state
|= QStyle::State_Selected
;
589 QPalette::ColorGroup cg
;
590 if ((d
->proxyModel
->flags(index
) & Qt::ItemIsEnabled
) == 0)
592 option
.state
&= ~QStyle::State_Enabled
;
593 cg
= QPalette::Disabled
;
597 cg
= QPalette::Normal
;
599 option
.palette
.setCurrentColorGroup(cg
);
602 if (focus
&& currentIndex() == index
)
604 option
.state
|= QStyle::State_HasFocus
;
605 if (this->state() == EditingState
)
606 option
.state
|= QStyle::State_Editing
;
609 if ((index
== d
->hovered
) && !d
->mouseButtonPressed
)
610 option
.state
|= QStyle::State_MouseOver
;
612 option
.state
&= ~QStyle::State_MouseOver
;
614 itemDelegate(index
)->paint(&painter
, option
, index
);
618 QStyleOptionViewItem otherOption
;
619 foreach (const QString
&category
, d
->categories
)
621 otherOption
= option
;
622 otherOption
.rect
= d
->categoryVisualRect(category
);
624 if (otherOption
.rect
.intersects(area
))
626 d
->drawNewCategory(category
, otherOption
, &painter
);
630 if (d
->mouseButtonPressed
&& !d
->isDragging
)
632 QPoint start
, end
, initialPressPosition
;
634 initialPressPosition
= d
->initialPressPosition
;
636 initialPressPosition
.setY(initialPressPosition
.y() - verticalOffset());
637 initialPressPosition
.setX(initialPressPosition
.x() - horizontalOffset());
639 if (d
->initialPressPosition
.x() > d
->mousePosition
.x() ||
640 d
->initialPressPosition
.y() > d
->mousePosition
.y())
642 start
= d
->mousePosition
;
643 end
= initialPressPosition
;
647 start
= initialPressPosition
;
648 end
= d
->mousePosition
;
651 QStyleOptionRubberBand yetAnotherOption
;
652 yetAnotherOption
.initFrom(this);
653 yetAnotherOption
.shape
= QRubberBand::Rectangle
;
654 yetAnotherOption
.opaque
= false;
655 yetAnotherOption
.rect
= QRect(start
, end
).intersected(viewport()->rect().adjusted(-16, -16, 16, 16));
657 style()->drawControl(QStyle::CE_RubberBand
, &yetAnotherOption
, &painter
);
661 if (d
->isDragging
&& !d
->dragLeftViewport
)
663 painter
.setOpacity(0.5);
664 d
->drawDraggedItems(&painter
);
670 void KListView::resizeEvent(QResizeEvent
*event
)
672 QListView::resizeEvent(event
);
674 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
680 // Clear the items positions cache
681 d
->elementsPosition
.clear();
682 d
->categoriesPosition
.clear();
684 d
->updateScrollbars();
687 void KListView::setSelection(const QRect
&rect
,
688 QItemSelectionModel::SelectionFlags flags
)
690 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
693 QListView::setSelection(rect
, flags
);
700 selectionModel()->clear();
702 if (flags
& QItemSelectionModel::Clear
)
704 d
->lastSelection
= QItemSelection();
707 QModelIndexList dirtyIndexes
= d
->intersectionSet(rect
);
708 QItemSelection selection
;
710 if (!dirtyIndexes
.count())
712 if (d
->lastSelection
.count())
714 selectionModel()->select(d
->lastSelection
, flags
);
720 if (!d
->mouseButtonPressed
)
722 selection
= QItemSelection(dirtyIndexes
[0], dirtyIndexes
[0]);
726 QModelIndex first
= dirtyIndexes
[0];
728 foreach (const QModelIndex
&index
, dirtyIndexes
)
730 if (last
.isValid() && last
.row() + 1 != index
.row())
732 QItemSelectionRange
range(first
, last
);
743 selection
<< QItemSelectionRange(first
, last
);
746 if (d
->lastSelection
.count() && !d
->mouseButtonPressed
)
748 selection
.merge(d
->lastSelection
, flags
);
750 else if (d
->lastSelection
.count())
752 selection
.merge(d
->lastSelection
, QItemSelectionModel::Select
);
755 selectionModel()->select(selection
, flags
);
757 viewport()->update();
760 void KListView::mouseMoveEvent(QMouseEvent
*event
)
762 d
->mousePosition
= event
->pos();
764 QListView::mouseMoveEvent(event
);
766 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
774 viewport()->update();
777 void KListView::mousePressEvent(QMouseEvent
*event
)
779 QListView::mousePressEvent(event
);
781 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
789 d
->dragLeftViewport
= false;
791 if (event
->button() == Qt::LeftButton
)
793 d
->mouseButtonPressed
= true;
795 d
->initialPressPosition
= event
->pos();
796 d
->initialPressPosition
.setY(d
->initialPressPosition
.y() +
798 d
->initialPressPosition
.setX(d
->initialPressPosition
.x() +
802 viewport()->update();
805 void KListView::mouseReleaseEvent(QMouseEvent
*event
)
807 QListView::mouseReleaseEvent(event
);
809 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
817 d
->mouseButtonPressed
= false;
819 QPoint initialPressPosition
= viewport()->mapFromGlobal(QCursor::pos());
820 initialPressPosition
.setY(initialPressPosition
.y() + verticalOffset());
821 initialPressPosition
.setX(initialPressPosition
.x() + horizontalOffset());
823 QItemSelection selection
;
825 if (initialPressPosition
== d
->initialPressPosition
)
827 foreach(const QString
&category
, d
->categories
)
829 if (d
->categoryVisualRect(category
).contains(event
->pos()))
831 QItemSelectionRange
selectionRange(d
->proxyModel
->mapFromSource(d
->categoriesIndexes
[category
][0]),
832 d
->proxyModel
->mapFromSource(d
->categoriesIndexes
[category
][d
->categoriesIndexes
[category
].count() - 1]));
834 selection
<< selectionRange
;
836 selectionModel()->select(selection
, QItemSelectionModel::Select
);
843 d
->lastSelection
= selectionModel()->selection();
845 viewport()->update();
848 void KListView::leaveEvent(QEvent
*event
)
850 QListView::leaveEvent(event
);
852 d
->hovered
= QModelIndex();
854 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
862 viewport()->update();
865 void KListView::startDrag(Qt::DropActions supportedActions
)
867 QListView::startDrag(supportedActions
);
869 d
->isDragging
= false;
870 d
->mouseButtonPressed
= false;
873 void KListView::dragMoveEvent(QDragMoveEvent
*event
)
875 QListView::dragMoveEvent(event
);
877 d
->mousePosition
= event
->pos();
879 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
885 if (d
->mouseButtonPressed
)
887 d
->isDragging
= true;
891 d
->isDragging
= false;
894 d
->dragLeftViewport
= false;
898 viewport()->update();
901 void KListView::dragLeaveEvent(QDragLeaveEvent
*event
)
903 QListView::dragLeaveEvent(event
);
905 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
911 d
->dragLeftViewport
= true;
915 viewport()->update();
918 QModelIndex
KListView::moveCursor(CursorAction cursorAction
,
919 Qt::KeyboardModifiers modifiers
)
921 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
924 return QListView::moveCursor(cursorAction
, modifiers
);
927 return QListView::moveCursor(cursorAction
, modifiers
);
930 void KListView::rowsInserted(const QModelIndex
&parent
,
934 QListView::rowsInserted(parent
, start
, end
);
936 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
942 rowsInsertedArtifficial(parent
, start
, end
);
945 void KListView::rowsInsertedArtifficial(const QModelIndex
&parent
,
949 d
->lastSelection
= QItemSelection();
950 d
->elementsInfo
.clear();
951 d
->elementsPosition
.clear();
952 d
->elementDictionary
.clear();
953 d
->categoriesIndexes
.clear();
954 d
->categoriesPosition
.clear();
955 d
->categories
.clear();
956 d
->intersectedIndexes
.clear();
957 d
->sourceModelIndexList
.clear();
958 d
->hovered
= QModelIndex();
959 d
->mouseButtonPressed
= false;
961 if (start
> end
|| end
< 0 || start
< 0 || !d
->proxyModel
->rowCount())
966 // Add all elements mapped to the source model
967 for (int k
= 0; k
< d
->proxyModel
->rowCount(); k
++)
969 d
->sourceModelIndexList
<<
970 d
->proxyModel
->mapToSource(d
->proxyModel
->index(k
, 0));
973 // Sort them with the general purpose lessThan method
974 LessThan
generalLessThan(d
->proxyModel
,
975 LessThan::GeneralPurpose
);
977 qStableSort(d
->sourceModelIndexList
.begin(), d
->sourceModelIndexList
.end(),
980 // Explore categories
981 QString prevCategory
=
982 d
->itemCategorizer
->categoryForItem(d
->sourceModelIndexList
[0],
983 d
->proxyModel
->sortRole());
984 QString lastCategory
= prevCategory
;
985 QModelIndexList modelIndexList
;
986 struct Private::ElementInfo elementInfo
;
987 foreach (const QModelIndex
&index
, d
->sourceModelIndexList
)
989 lastCategory
= d
->itemCategorizer
->categoryForItem(index
,
990 d
->proxyModel
->sortRole());
992 elementInfo
.category
= lastCategory
;
994 if (prevCategory
!= lastCategory
)
996 d
->categoriesIndexes
.insert(prevCategory
, modelIndexList
);
997 d
->categories
<< prevCategory
;
998 modelIndexList
.clear();
1001 modelIndexList
<< index
;
1002 prevCategory
= lastCategory
;
1004 d
->elementsInfo
.insert(index
, elementInfo
);
1007 d
->categoriesIndexes
.insert(prevCategory
, modelIndexList
);
1008 d
->categories
<< prevCategory
;
1010 // Sort items locally in their respective categories with the category
1012 LessThan
categoryLessThan(d
->proxyModel
,
1013 LessThan::CategoryPurpose
);
1015 foreach (const QString
&key
, d
->categories
)
1017 QModelIndexList
&indexList
= d
->categoriesIndexes
[key
];
1019 qStableSort(indexList
.begin(), indexList
.end(), categoryLessThan
);
1022 d
->lastIndex
= d
->categoriesIndexes
[d
->categories
[d
->categories
.count() - 1]][d
->categoriesIndexes
[d
->categories
[d
->categories
.count() - 1]].count() - 1];
1024 // Finally, fill data information of items situation. This will help when
1025 // trying to compute an item place in the viewport
1026 int i
= 0; // position relative to the category beginning
1027 int j
= 0; // number of elements before current
1028 foreach (const QString
&key
, d
->categories
)
1030 foreach (const QModelIndex
&index
, d
->categoriesIndexes
[key
])
1032 struct Private::ElementInfo
&elementInfo
= d
->elementsInfo
[index
];
1034 elementInfo
.relativeOffsetToCategory
= i
;
1036 d
->elementDictionary
.insert(d
->proxyModel
->index(j
, 0),
1037 d
->proxyModel
->mapFromSource(index
));
1046 d
->updateScrollbars();
1049 void KListView::rowsRemoved(const QModelIndex
&parent
,
1053 if ((viewMode() == KListView::IconMode
) && d
->proxyModel
&&
1056 // Force the view to update all elements
1057 rowsInsertedArtifficial(parent
, start
, end
);
1061 void KListView::updateGeometries()
1063 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
1064 !d
->itemCategorizer
)
1066 QListView::updateGeometries();
1070 // Avoid QListView::updateGeometries(), since it will try to set another
1071 // range to our scroll bars, what we don't want (ereslibre)
1072 QAbstractItemView::updateGeometries();
1075 void KListView::slotSortingRoleChanged()
1077 if ((viewMode() == KListView::IconMode
) && d
->proxyModel
&&
1080 // Force the view to update all elements
1081 rowsInsertedArtifficial(QModelIndex(), 0, d
->proxyModel
->rowCount() - 1);
1085 #include "klistview.moc"