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
);
412 void KListView::Private::drawDraggedItems()
418 foreach (const QModelIndex
&index
, listView
->selectionModel()->selectedIndexes())
420 dx
= mousePosition
.x() - initialPressPosition
.x() + listView
->horizontalOffset();
421 dy
= mousePosition
.y() - initialPressPosition
.y() + listView
->verticalOffset();
423 currentRect
= visualRect(index
);
424 currentRect
.adjust(dx
, dy
, dx
, dy
);
426 rectToUpdate
= rectToUpdate
.united(currentRect
);
429 listView
->viewport()->update(lastDraggedItemsRect
);
431 lastDraggedItemsRect
= rectToUpdate
;
433 listView
->viewport()->update(rectToUpdate
);
437 //==============================================================================
440 KListView::KListView(QWidget
*parent
)
442 , d(new Private(this))
446 KListView::~KListView()
451 void KListView::setModel(QAbstractItemModel
*model
)
455 QObject::disconnect(d
->proxyModel
,
456 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
457 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
459 QObject::disconnect(d
->proxyModel
,
460 SIGNAL(sortingRoleChanged()),
461 this, SLOT(slotSortingRoleChanged()));
464 QListView::setModel(model
);
466 d
->proxyModel
= dynamic_cast<KSortFilterProxyModel
*>(model
);
470 QObject::connect(d
->proxyModel
,
471 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
472 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
474 QObject::connect(d
->proxyModel
,
475 SIGNAL(sortingRoleChanged()),
476 this, SLOT(slotSortingRoleChanged()));
480 QRect
KListView::visualRect(const QModelIndex
&index
) const
482 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
485 return QListView::visualRect(index
);
488 if (!qobject_cast
<const QSortFilterProxyModel
*>(index
.model()))
490 return d
->visualRect(d
->proxyModel
->mapFromSource(index
));
493 return d
->visualRect(index
);
496 KItemCategorizer
*KListView::itemCategorizer() const
498 return d
->itemCategorizer
;
501 void KListView::setItemCategorizer(KItemCategorizer
*itemCategorizer
)
503 if (!itemCategorizer
&& d
->proxyModel
)
505 QObject::disconnect(d
->proxyModel
,
506 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
507 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
509 QObject::disconnect(d
->proxyModel
,
510 SIGNAL(sortingRoleChanged()),
511 this, SLOT(slotSortingRoleChanged()));
513 else if (itemCategorizer
&& d
->proxyModel
)
515 QObject::connect(d
->proxyModel
,
516 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
517 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
519 QObject::connect(d
->proxyModel
,
520 SIGNAL(sortingRoleChanged()),
521 this, SLOT(slotSortingRoleChanged()));
524 d
->itemCategorizer
= itemCategorizer
;
528 rowsInserted(QModelIndex(), 0, d
->proxyModel
->rowCount() - 1);
536 QModelIndex
KListView::indexAt(const QPoint
&point
) const
538 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
541 return QListView::indexAt(point
);
546 QModelIndexList item
= d
->intersectionSet(QRect(point
, point
));
548 if (item
.count() == 1)
558 void KListView::reset()
562 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
568 d
->elementsInfo
.clear();
569 d
->elementsPosition
.clear();
570 d
->elementDictionary
.clear();
571 d
->categoriesIndexes
.clear();
572 d
->categoriesPosition
.clear();
573 d
->categories
.clear();
574 d
->intersectedIndexes
.clear();
575 d
->sourceModelIndexList
.clear();
576 d
->hovered
= QModelIndex();
577 d
->mouseButtonPressed
= false;
580 void KListView::paintEvent(QPaintEvent
*event
)
582 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
585 QListView::paintEvent(event
);
589 QStyleOptionViewItemV3 option
= viewOptions();
590 QPainter
painter(viewport());
591 QRect area
= event
->rect();
592 const bool focus
= (hasFocus() || viewport()->hasFocus()) &&
593 currentIndex().isValid();
594 const QStyle::State state
= option
.state
;
595 const bool enabled
= (state
& QStyle::State_Enabled
) != 0;
599 QModelIndexList dirtyIndexes
= d
->intersectionSet(area
);
600 foreach (const QModelIndex
&index
, dirtyIndexes
)
602 option
.state
= state
;
603 option
.rect
= d
->visualRect(index
);
605 if (selectionModel() && selectionModel()->isSelected(index
))
607 option
.state
|= QStyle::State_Selected
;
612 QPalette::ColorGroup cg
;
613 if ((d
->proxyModel
->flags(index
) & Qt::ItemIsEnabled
) == 0)
615 option
.state
&= ~QStyle::State_Enabled
;
616 cg
= QPalette::Disabled
;
620 cg
= QPalette::Normal
;
622 option
.palette
.setCurrentColorGroup(cg
);
625 if (focus
&& currentIndex() == index
)
627 option
.state
|= QStyle::State_HasFocus
;
628 if (this->state() == EditingState
)
629 option
.state
|= QStyle::State_Editing
;
632 if ((index
== d
->hovered
) && !d
->mouseButtonPressed
)
633 option
.state
|= QStyle::State_MouseOver
;
635 option
.state
&= ~QStyle::State_MouseOver
;
637 itemDelegate(index
)->paint(&painter
, option
, index
);
640 if (d
->mouseButtonPressed
&& !d
->isDragging
)
642 QPoint start
, end
, initialPressPosition
;
644 initialPressPosition
= d
->initialPressPosition
;
646 initialPressPosition
.setY(initialPressPosition
.y() - verticalOffset());
647 initialPressPosition
.setX(initialPressPosition
.x() - horizontalOffset());
649 if (d
->initialPressPosition
.x() > d
->mousePosition
.x() ||
650 d
->initialPressPosition
.y() > d
->mousePosition
.y())
652 start
= d
->mousePosition
;
653 end
= initialPressPosition
;
657 start
= initialPressPosition
;
658 end
= d
->mousePosition
;
661 QStyleOptionRubberBand yetAnotherOption
;
662 yetAnotherOption
.initFrom(this);
663 yetAnotherOption
.shape
= QRubberBand::Rectangle
;
664 yetAnotherOption
.opaque
= false;
665 yetAnotherOption
.rect
= QRect(start
, end
).intersected(viewport()->rect().adjusted(-16, -16, 16, 16));
667 style()->drawControl(QStyle::CE_RubberBand
, &yetAnotherOption
, &painter
);
672 QStyleOptionViewItem otherOption
;
673 foreach (const QString
&category
, d
->categories
)
675 otherOption
= option
;
676 otherOption
.rect
= d
->categoryVisualRect(category
);
678 if (otherOption
.rect
.intersects(area
))
680 d
->drawNewCategory(category
, otherOption
, &painter
);
684 if (d
->isDragging
&& !d
->dragLeftViewport
)
686 painter
.setOpacity(0.5);
687 d
->drawDraggedItems(&painter
);
693 void KListView::resizeEvent(QResizeEvent
*event
)
695 QListView::resizeEvent(event
);
697 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
703 // Clear the items positions cache
704 d
->elementsPosition
.clear();
705 d
->categoriesPosition
.clear();
707 d
->updateScrollbars();
710 void KListView::setSelection(const QRect
&rect
,
711 QItemSelectionModel::SelectionFlags flags
)
713 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
716 QListView::setSelection(rect
, flags
);
723 selectionModel()->clear();
725 if (flags
& QItemSelectionModel::Clear
)
727 d
->lastSelection
= QItemSelection();
730 QModelIndexList dirtyIndexes
= d
->intersectionSet(rect
);
731 QItemSelection selection
;
733 if (!dirtyIndexes
.count())
735 if (d
->lastSelection
.count())
737 selectionModel()->select(d
->lastSelection
, flags
);
743 if (!d
->mouseButtonPressed
)
745 selection
= QItemSelection(dirtyIndexes
[0], dirtyIndexes
[0]);
749 QModelIndex first
= dirtyIndexes
[0];
751 foreach (const QModelIndex
&index
, dirtyIndexes
)
753 if (last
.isValid() && last
.row() + 1 != index
.row())
755 QItemSelectionRange
range(first
, last
);
766 selection
<< QItemSelectionRange(first
, last
);
769 if (d
->lastSelection
.count() && !d
->mouseButtonPressed
)
771 selection
.merge(d
->lastSelection
, flags
);
773 else if (d
->lastSelection
.count())
775 selection
.merge(d
->lastSelection
, QItemSelectionModel::Select
);
778 selectionModel()->select(selection
, flags
);
781 void KListView::mouseMoveEvent(QMouseEvent
*event
)
783 QListView::mouseMoveEvent(event
);
785 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
791 d
->mousePosition
= event
->pos();
792 d
->hoveredCategory
= QString();
795 foreach (const QString
&category
, d
->categories
)
797 if (d
->categoryVisualRect(category
).intersects(QRect(event
->pos(), event
->pos())))
799 d
->hoveredCategory
= category
;
802 viewport()->update(d
->categoryVisualRect(category
));
806 if (d
->mouseButtonPressed
&& !d
->isDragging
)
808 QPoint start
, end
, initialPressPosition
;
810 initialPressPosition
= d
->initialPressPosition
;
812 initialPressPosition
.setY(initialPressPosition
.y() - verticalOffset());
813 initialPressPosition
.setX(initialPressPosition
.x() - horizontalOffset());
815 if (d
->initialPressPosition
.x() > d
->mousePosition
.x() ||
816 d
->initialPressPosition
.y() > d
->mousePosition
.y())
818 start
= d
->mousePosition
;
819 end
= initialPressPosition
;
823 start
= initialPressPosition
;
824 end
= d
->mousePosition
;
827 viewport()->update(d
->lastSelectionRect
);
829 rect
= QRect(start
, end
).intersected(viewport()->rect().adjusted(-16, -16, 16, 16));
831 viewport()->update(rect
);
833 d
->lastSelectionRect
= rect
;
837 void KListView::mousePressEvent(QMouseEvent
*event
)
839 QListView::mousePressEvent(event
);
841 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
847 d
->dragLeftViewport
= false;
849 if (event
->button() == Qt::LeftButton
)
851 d
->mouseButtonPressed
= true;
853 d
->initialPressPosition
= event
->pos();
854 d
->initialPressPosition
.setY(d
->initialPressPosition
.y() +
856 d
->initialPressPosition
.setX(d
->initialPressPosition
.x() +
861 void KListView::mouseReleaseEvent(QMouseEvent
*event
)
863 QListView::mouseReleaseEvent(event
);
865 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
871 d
->mouseButtonPressed
= false;
873 QPoint initialPressPosition
= viewport()->mapFromGlobal(QCursor::pos());
874 initialPressPosition
.setY(initialPressPosition
.y() + verticalOffset());
875 initialPressPosition
.setX(initialPressPosition
.x() + horizontalOffset());
877 QItemSelection selection
;
879 if (initialPressPosition
== d
->initialPressPosition
)
881 foreach(const QString
&category
, d
->categories
)
883 if (d
->categoryVisualRect(category
).contains(event
->pos()))
885 QItemSelectionRange
selectionRange(d
->proxyModel
->mapFromSource(d
->categoriesIndexes
[category
][0]),
886 d
->proxyModel
->mapFromSource(d
->categoriesIndexes
[category
][d
->categoriesIndexes
[category
].count() - 1]));
888 selection
<< selectionRange
;
890 selectionModel()->select(selection
, QItemSelectionModel::Select
);
897 d
->lastSelection
= selectionModel()->selection();
899 if (d
->hovered
.isValid())
900 viewport()->update(d
->visualRect(d
->hovered
));
901 else if (!d
->hoveredCategory
.isEmpty())
902 viewport()->update(d
->categoryVisualRect(d
->hoveredCategory
));
905 void KListView::leaveEvent(QEvent
*event
)
907 QListView::leaveEvent(event
);
909 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
915 d
->hovered
= QModelIndex();
916 d
->hoveredCategory
= QString();
919 void KListView::startDrag(Qt::DropActions supportedActions
)
921 QListView::startDrag(supportedActions
);
923 d
->isDragging
= false;
924 d
->mouseButtonPressed
= false;
927 void KListView::dragMoveEvent(QDragMoveEvent
*event
)
929 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
932 QListView::dragMoveEvent(event
);
936 d
->mousePosition
= event
->pos();
938 if (d
->mouseButtonPressed
)
940 d
->isDragging
= true;
944 d
->isDragging
= false;
947 d
->dragLeftViewport
= false;
949 d
->drawDraggedItems();
952 void KListView::dragLeaveEvent(QDragLeaveEvent
*event
)
954 QListView::dragLeaveEvent(event
);
956 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
962 d
->dragLeftViewport
= true;
965 QModelIndex
KListView::moveCursor(CursorAction cursorAction
,
966 Qt::KeyboardModifiers modifiers
)
968 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
971 return QListView::moveCursor(cursorAction
, modifiers
);
974 return QListView::moveCursor(cursorAction
, modifiers
);
977 void KListView::rowsInserted(const QModelIndex
&parent
,
981 QListView::rowsInserted(parent
, start
, end
);
983 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
989 rowsInsertedArtifficial(parent
, start
, end
);
992 void KListView::rowsInsertedArtifficial(const QModelIndex
&parent
,
996 d
->lastSelection
= QItemSelection();
997 d
->elementsInfo
.clear();
998 d
->elementsPosition
.clear();
999 d
->elementDictionary
.clear();
1000 d
->categoriesIndexes
.clear();
1001 d
->categoriesPosition
.clear();
1002 d
->categories
.clear();
1003 d
->intersectedIndexes
.clear();
1004 d
->sourceModelIndexList
.clear();
1005 d
->hovered
= QModelIndex();
1006 d
->mouseButtonPressed
= false;
1008 if (start
> end
|| end
< 0 || start
< 0 || !d
->proxyModel
->rowCount())
1013 // Add all elements mapped to the source model
1014 for (int k
= 0; k
< d
->proxyModel
->rowCount(); k
++)
1016 d
->sourceModelIndexList
<<
1017 d
->proxyModel
->mapToSource(d
->proxyModel
->index(k
, 0));
1020 // Sort them with the general purpose lessThan method
1021 LessThan
generalLessThan(d
->proxyModel
,
1022 LessThan::GeneralPurpose
);
1024 qStableSort(d
->sourceModelIndexList
.begin(), d
->sourceModelIndexList
.end(),
1027 // Explore categories
1028 QString prevCategory
=
1029 d
->itemCategorizer
->categoryForItem(d
->sourceModelIndexList
[0],
1030 d
->proxyModel
->sortRole());
1031 QString lastCategory
= prevCategory
;
1032 QModelIndexList modelIndexList
;
1033 struct Private::ElementInfo elementInfo
;
1034 foreach (const QModelIndex
&index
, d
->sourceModelIndexList
)
1036 lastCategory
= d
->itemCategorizer
->categoryForItem(index
,
1037 d
->proxyModel
->sortRole());
1039 elementInfo
.category
= lastCategory
;
1041 if (prevCategory
!= lastCategory
)
1043 d
->categoriesIndexes
.insert(prevCategory
, modelIndexList
);
1044 d
->categories
<< prevCategory
;
1045 modelIndexList
.clear();
1048 modelIndexList
<< index
;
1049 prevCategory
= lastCategory
;
1051 d
->elementsInfo
.insert(index
, elementInfo
);
1054 d
->categoriesIndexes
.insert(prevCategory
, modelIndexList
);
1055 d
->categories
<< prevCategory
;
1057 // Sort items locally in their respective categories with the category
1059 LessThan
categoryLessThan(d
->proxyModel
,
1060 LessThan::CategoryPurpose
);
1062 foreach (const QString
&key
, d
->categories
)
1064 QModelIndexList
&indexList
= d
->categoriesIndexes
[key
];
1066 qStableSort(indexList
.begin(), indexList
.end(), categoryLessThan
);
1069 d
->lastIndex
= d
->categoriesIndexes
[d
->categories
[d
->categories
.count() - 1]][d
->categoriesIndexes
[d
->categories
[d
->categories
.count() - 1]].count() - 1];
1071 // Finally, fill data information of items situation. This will help when
1072 // trying to compute an item place in the viewport
1073 int i
= 0; // position relative to the category beginning
1074 int j
= 0; // number of elements before current
1075 foreach (const QString
&key
, d
->categories
)
1077 foreach (const QModelIndex
&index
, d
->categoriesIndexes
[key
])
1079 struct Private::ElementInfo
&elementInfo
= d
->elementsInfo
[index
];
1081 elementInfo
.relativeOffsetToCategory
= i
;
1083 d
->elementDictionary
.insert(d
->proxyModel
->index(j
, 0),
1084 d
->proxyModel
->mapFromSource(index
));
1093 d
->updateScrollbars();
1096 void KListView::rowsRemoved(const QModelIndex
&parent
,
1100 if ((viewMode() == KListView::IconMode
) && d
->proxyModel
&&
1103 // Force the view to update all elements
1104 rowsInsertedArtifficial(parent
, start
, end
);
1108 void KListView::updateGeometries()
1110 if ((viewMode() != KListView::IconMode
) || !d
->proxyModel
||
1111 !d
->itemCategorizer
)
1113 QListView::updateGeometries();
1117 // Avoid QListView::updateGeometries(), since it will try to set another
1118 // range to our scroll bars, what we don't want (ereslibre)
1119 QAbstractItemView::updateGeometries();
1122 void KListView::slotSortingRoleChanged()
1124 if ((viewMode() == KListView::IconMode
) && d
->proxyModel
&&
1127 // Force the view to update all elements
1128 rowsInsertedArtifficial(QModelIndex(), 0, d
->proxyModel
->rowCount() - 1);
1132 #include "klistview.moc"