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 "kcategorizedview.h"
22 #include "kcategorizedview_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 KCategorizedView::Private::Private(KCategorizedView
*listView
)
81 , biggestItemSize(QSize(0, 0))
82 , mouseButtonPressed(false)
84 , dragLeftViewport(false)
86 , lastIndex(QModelIndex())
90 KCategorizedView::Private::~Private()
94 const QModelIndexList
&KCategorizedView::Private::intersectionSet(const QRect
&rect
)
97 QRect indexVisualRect
;
99 intersectedIndexes
.clear();
101 // Lets find out where we should start
102 int top
= proxyModel
->rowCount() - 1;
104 int middle
= (top
+ bottom
) / 2;
105 while (bottom
<= top
)
107 middle
= (top
+ bottom
) / 2;
109 index
= elementDictionary
[proxyModel
->index(middle
, 0)];
110 indexVisualRect
= visualRect(index
);
112 if (qMax(indexVisualRect
.topLeft().y(),
113 indexVisualRect
.bottomRight().y()) < qMin(rect
.topLeft().y(),
114 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 qMin(indexVisualRect
.topLeft().y(),
135 indexVisualRect
.bottomRight().y()))
139 return intersectedIndexes
;
142 QRect
KCategorizedView::Private::visualRectInViewport(const QModelIndex
&index
) const
144 if (!index
.isValid())
147 QString curCategory
= elementsInfo
[index
].category
;
149 QRect
retRect(listView
->spacing(), listView
->spacing() * 2 +
150 itemCategorizer
->categoryHeight(listView
->viewOptions()), 0, 0);
152 int viewportWidth
= listView
->viewport()->width() - listView
->spacing();
157 if (listView
->gridSize().isEmpty())
159 itemHeight
= biggestItemSize
.height();
160 itemWidth
= biggestItemSize
.width();
164 itemHeight
= listView
->gridSize().height();
165 itemWidth
= listView
->gridSize().width();
168 int itemWidthPlusSeparation
= listView
->spacing() + itemWidth
;
169 int elementsPerRow
= viewportWidth
/ itemWidthPlusSeparation
;
173 int column
= elementsInfo
[index
].relativeOffsetToCategory
% elementsPerRow
;
174 int row
= elementsInfo
[index
].relativeOffsetToCategory
/ elementsPerRow
;
176 retRect
.setLeft(retRect
.left() + column
* listView
->spacing() +
179 foreach (const QString
&category
, categories
)
181 if (category
== curCategory
)
184 float rows
= (float) ((float) categoriesIndexes
[category
].count() /
185 (float) elementsPerRow
);
186 int rowsInt
= categoriesIndexes
[category
].count() / elementsPerRow
;
188 if (rows
- trunc(rows
)) rowsInt
++;
190 retRect
.setTop(retRect
.top() +
191 (rowsInt
* listView
->spacing()) +
192 (rowsInt
* itemHeight
) +
193 itemCategorizer
->categoryHeight(listView
->viewOptions()) +
194 listView
->spacing() * 2);
197 retRect
.setTop(retRect
.top() + row
* listView
->spacing() +
200 retRect
.setWidth(itemWidth
);
201 retRect
.setHeight(itemHeight
);
206 QRect
KCategorizedView::Private::visualCategoryRectInViewport(const QString
&category
)
209 QRect
retRect(listView
->spacing(),
211 listView
->viewport()->width() - listView
->spacing() * 2,
214 if (!proxyModel
->rowCount() || !categories
.contains(category
))
217 QModelIndex index
= proxyModel
->index(0, 0, QModelIndex());
219 int viewportWidth
= listView
->viewport()->width() - listView
->spacing();
224 if (listView
->gridSize().isEmpty())
226 itemHeight
= biggestItemSize
.height();
227 itemWidth
= biggestItemSize
.width();
231 itemHeight
= listView
->gridSize().height();
232 itemWidth
= listView
->gridSize().width();
235 int itemWidthPlusSeparation
= listView
->spacing() + itemWidth
;
236 int elementsPerRow
= viewportWidth
/ itemWidthPlusSeparation
;
241 foreach (const QString
&itCategory
, categories
)
243 if (itCategory
== category
)
246 float rows
= (float) ((float) categoriesIndexes
[itCategory
].count() /
247 (float) elementsPerRow
);
248 int rowsInt
= categoriesIndexes
[itCategory
].count() / elementsPerRow
;
250 if (rows
- trunc(rows
)) rowsInt
++;
252 retRect
.setTop(retRect
.top() +
253 (rowsInt
* listView
->spacing()) +
254 (rowsInt
* itemHeight
) +
255 itemCategorizer
->categoryHeight(listView
->viewOptions()) +
256 listView
->spacing() * 2);
259 retRect
.setHeight(itemCategorizer
->categoryHeight(listView
->viewOptions()));
264 // We're sure elementsPosition doesn't contain index
265 const QRect
&KCategorizedView::Private::cacheIndex(const QModelIndex
&index
)
267 QRect rect
= visualRectInViewport(index
);
268 elementsPosition
[index
] = rect
;
270 return elementsPosition
[index
];
273 // We're sure categoriesPosition doesn't contain category
274 const QRect
&KCategorizedView::Private::cacheCategory(const QString
&category
)
276 QRect rect
= visualCategoryRectInViewport(category
);
277 categoriesPosition
[category
] = rect
;
279 return categoriesPosition
[category
];
282 const QRect
&KCategorizedView::Private::cachedRectIndex(const QModelIndex
&index
)
284 if (elementsPosition
.contains(index
)) // If we have it cached
286 return elementsPosition
[index
];
288 else // Otherwise, cache it
290 return cacheIndex(index
);
294 const QRect
&KCategorizedView::Private::cachedRectCategory(const QString
&category
)
296 if (categoriesPosition
.contains(category
)) // If we have it cached
298 return categoriesPosition
[category
];
300 else // Otherwise, cache it and
302 return cacheCategory(category
);
306 QRect
KCategorizedView::Private::visualRect(const QModelIndex
&index
)
308 QModelIndex mappedIndex
= proxyModel
->mapToSource(index
);
310 QRect retRect
= cachedRectIndex(mappedIndex
);
311 int dx
= -listView
->horizontalOffset();
312 int dy
= -listView
->verticalOffset();
313 retRect
.adjust(dx
, dy
, dx
, dy
);
318 QRect
KCategorizedView::Private::categoryVisualRect(const QString
&category
)
320 QRect retRect
= cachedRectCategory(category
);
321 int dx
= -listView
->horizontalOffset();
322 int dy
= -listView
->verticalOffset();
323 retRect
.adjust(dx
, dy
, dx
, dy
);
328 void KCategorizedView::Private::drawNewCategory(const QModelIndex
&index
,
330 const QStyleOption
&option
,
333 QStyleOption optionCopy
= option
;
334 const QString category
= itemCategorizer
->categoryForItem(index
, sortRole
);
336 if ((category
== hoveredCategory
) && !mouseButtonPressed
)
338 optionCopy
.state
|= QStyle::State_MouseOver
;
341 itemCategorizer
->drawCategory(index
,
348 void KCategorizedView::Private::updateScrollbars()
350 int lastItemBottom
= cachedRectIndex(lastIndex
).bottom() +
351 listView
->spacing() - listView
->viewport()->height();
353 listView
->verticalScrollBar()->setSingleStep(listView
->viewport()->height() / 10);
354 listView
->verticalScrollBar()->setPageStep(listView
->viewport()->height());
355 listView
->verticalScrollBar()->setRange(0, lastItemBottom
);
358 void KCategorizedView::Private::drawDraggedItems(QPainter
*painter
)
360 QStyleOptionViewItemV3 option
= listView
->viewOptions();
361 option
.state
&= ~QStyle::State_MouseOver
;
362 foreach (const QModelIndex
&index
, listView
->selectionModel()->selectedIndexes())
364 const int dx
= mousePosition
.x() - initialPressPosition
.x() + listView
->horizontalOffset();
365 const int dy
= mousePosition
.y() - initialPressPosition
.y() + listView
->verticalOffset();
367 option
.rect
= visualRect(index
);
368 option
.rect
.adjust(dx
, dy
, dx
, dy
);
370 if (option
.rect
.intersects(listView
->viewport()->rect()))
372 listView
->itemDelegate(index
)->paint(painter
, option
, index
);
377 void KCategorizedView::Private::drawDraggedItems()
381 foreach (const QModelIndex
&index
, listView
->selectionModel()->selectedIndexes())
383 int dx
= mousePosition
.x() - initialPressPosition
.x() + listView
->horizontalOffset();
384 int dy
= mousePosition
.y() - initialPressPosition
.y() + listView
->verticalOffset();
386 currentRect
= visualRect(index
);
387 currentRect
.adjust(dx
, dy
, dx
, dy
);
389 if (currentRect
.intersects(listView
->viewport()->rect()))
391 rectToUpdate
= rectToUpdate
.united(currentRect
);
395 listView
->viewport()->update(lastDraggedItemsRect
.united(rectToUpdate
));
397 lastDraggedItemsRect
= rectToUpdate
;
401 //==============================================================================
404 KCategorizedView::KCategorizedView(QWidget
*parent
)
406 , d(new Private(this))
410 KCategorizedView::~KCategorizedView()
415 void KCategorizedView::setModel(QAbstractItemModel
*model
)
417 d
->lastSelection
= QItemSelection();
418 d
->currentViewIndex
= QModelIndex();
419 d
->forcedSelectionPosition
= 0;
420 d
->elementsInfo
.clear();
421 d
->elementsPosition
.clear();
422 d
->elementDictionary
.clear();
423 d
->invertedElementDictionary
.clear();
424 d
->categoriesIndexes
.clear();
425 d
->categoriesPosition
.clear();
426 d
->categories
.clear();
427 d
->intersectedIndexes
.clear();
428 d
->sourceModelIndexList
.clear();
429 d
->hovered
= QModelIndex();
430 d
->mouseButtonPressed
= false;
434 QObject::disconnect(d
->proxyModel
,
435 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
436 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
438 QObject::disconnect(d
->proxyModel
,
439 SIGNAL(sortingRoleChanged()),
440 this, SLOT(slotSortingRoleChanged()));
443 QListView::setModel(model
);
445 d
->proxyModel
= dynamic_cast<KSortFilterProxyModel
*>(model
);
449 QObject::connect(d
->proxyModel
,
450 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
451 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
453 QObject::connect(d
->proxyModel
,
454 SIGNAL(sortingRoleChanged()),
455 this, SLOT(slotSortingRoleChanged()));
459 QRect
KCategorizedView::visualRect(const QModelIndex
&index
) const
461 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
464 return QListView::visualRect(index
);
467 if (!qobject_cast
<const QSortFilterProxyModel
*>(index
.model()))
469 return d
->visualRect(d
->proxyModel
->mapFromSource(index
));
472 return d
->visualRect(index
);
475 KItemCategorizer
*KCategorizedView::itemCategorizer() const
477 return d
->itemCategorizer
;
480 void KCategorizedView::setItemCategorizer(KItemCategorizer
*itemCategorizer
)
482 d
->lastSelection
= QItemSelection();
483 d
->currentViewIndex
= QModelIndex();
484 d
->forcedSelectionPosition
= 0;
485 d
->elementsInfo
.clear();
486 d
->elementsPosition
.clear();
487 d
->elementDictionary
.clear();
488 d
->invertedElementDictionary
.clear();
489 d
->categoriesIndexes
.clear();
490 d
->categoriesPosition
.clear();
491 d
->categories
.clear();
492 d
->intersectedIndexes
.clear();
493 d
->sourceModelIndexList
.clear();
494 d
->hovered
= QModelIndex();
495 d
->mouseButtonPressed
= false;
497 if (!itemCategorizer
&& d
->proxyModel
)
499 QObject::disconnect(d
->proxyModel
,
500 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
501 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
503 QObject::disconnect(d
->proxyModel
,
504 SIGNAL(sortingRoleChanged()),
505 this, SLOT(slotSortingRoleChanged()));
507 else if (itemCategorizer
&& d
->proxyModel
)
509 QObject::connect(d
->proxyModel
,
510 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
511 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
513 QObject::connect(d
->proxyModel
,
514 SIGNAL(sortingRoleChanged()),
515 this, SLOT(slotSortingRoleChanged()));
518 d
->itemCategorizer
= itemCategorizer
;
522 rowsInserted(QModelIndex(), 0, d
->proxyModel
->rowCount() - 1);
530 QModelIndex
KCategorizedView::indexAt(const QPoint
&point
) const
532 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
535 return QListView::indexAt(point
);
540 QModelIndexList item
= d
->intersectionSet(QRect(point
, point
));
542 if (item
.count() == 1)
552 void KCategorizedView::reset()
556 d
->lastSelection
= QItemSelection();
557 d
->currentViewIndex
= QModelIndex();
558 d
->forcedSelectionPosition
= 0;
559 d
->elementsInfo
.clear();
560 d
->elementsPosition
.clear();
561 d
->elementDictionary
.clear();
562 d
->invertedElementDictionary
.clear();
563 d
->categoriesIndexes
.clear();
564 d
->categoriesPosition
.clear();
565 d
->categories
.clear();
566 d
->intersectedIndexes
.clear();
567 d
->sourceModelIndexList
.clear();
568 d
->hovered
= QModelIndex();
569 d
->biggestItemSize
= QSize(0, 0);
570 d
->mouseButtonPressed
= false;
573 void KCategorizedView::paintEvent(QPaintEvent
*event
)
575 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
578 QListView::paintEvent(event
);
582 QStyleOptionViewItemV3 option
= viewOptions();
583 option
.widget
= this;
584 QPainter
painter(viewport());
585 QRect area
= event
->rect();
586 const bool focus
= (hasFocus() || viewport()->hasFocus()) &&
587 currentIndex().isValid();
588 const QStyle::State state
= option
.state
;
589 const bool enabled
= (state
& QStyle::State_Enabled
) != 0;
593 QModelIndexList dirtyIndexes
= d
->intersectionSet(area
);
594 foreach (const QModelIndex
&index
, dirtyIndexes
)
596 option
.state
= state
;
597 option
.rect
= d
->visualRect(index
);
599 if (selectionModel() && selectionModel()->isSelected(index
))
601 option
.state
|= QStyle::State_Selected
;
606 QPalette::ColorGroup cg
;
607 if ((d
->proxyModel
->flags(index
) & Qt::ItemIsEnabled
) == 0)
609 option
.state
&= ~QStyle::State_Enabled
;
610 cg
= QPalette::Disabled
;
614 cg
= QPalette::Normal
;
616 option
.palette
.setCurrentColorGroup(cg
);
619 if (focus
&& currentIndex() == index
)
621 option
.state
|= QStyle::State_HasFocus
;
622 if (this->state() == EditingState
)
623 option
.state
|= QStyle::State_Editing
;
626 if ((index
== d
->hovered
) && !d
->mouseButtonPressed
)
627 option
.state
|= QStyle::State_MouseOver
;
629 option
.state
&= ~QStyle::State_MouseOver
;
631 itemDelegate(index
)->paint(&painter
, option
, index
);
635 QStyleOptionViewItem otherOption
;
636 foreach (const QString
&category
, d
->categories
)
638 otherOption
= option
;
639 otherOption
.rect
= d
->categoryVisualRect(category
);
640 otherOption
.state
&= ~QStyle::State_MouseOver
;
642 if (otherOption
.rect
.intersects(area
))
644 d
->drawNewCategory(d
->categoriesIndexes
[category
][0],
645 d
->proxyModel
->sortRole(), otherOption
, &painter
);
649 if (d
->mouseButtonPressed
&& !d
->isDragging
)
651 QPoint start
, end
, initialPressPosition
;
653 initialPressPosition
= d
->initialPressPosition
;
655 initialPressPosition
.setY(initialPressPosition
.y() - verticalOffset());
656 initialPressPosition
.setX(initialPressPosition
.x() - horizontalOffset());
658 if (d
->initialPressPosition
.x() > d
->mousePosition
.x() ||
659 d
->initialPressPosition
.y() > d
->mousePosition
.y())
661 start
= d
->mousePosition
;
662 end
= initialPressPosition
;
666 start
= initialPressPosition
;
667 end
= d
->mousePosition
;
670 QStyleOptionRubberBand yetAnotherOption
;
671 yetAnotherOption
.initFrom(this);
672 yetAnotherOption
.shape
= QRubberBand::Rectangle
;
673 yetAnotherOption
.opaque
= false;
674 yetAnotherOption
.rect
= QRect(start
, end
).intersected(viewport()->rect().adjusted(-16, -16, 16, 16));
676 style()->drawControl(QStyle::CE_RubberBand
, &yetAnotherOption
, &painter
);
680 if (d
->isDragging
&& !d
->dragLeftViewport
)
682 painter
.setOpacity(0.5);
683 d
->drawDraggedItems(&painter
);
689 void KCategorizedView::resizeEvent(QResizeEvent
*event
)
691 QListView::resizeEvent(event
);
693 // Clear the items positions cache
694 d
->elementsPosition
.clear();
695 d
->categoriesPosition
.clear();
696 d
->forcedSelectionPosition
= 0;
698 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
704 d
->updateScrollbars();
707 void KCategorizedView::setSelection(const QRect
&rect
,
708 QItemSelectionModel::SelectionFlags flags
)
710 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
713 QListView::setSelection(rect
, flags
);
720 selectionModel()->clear();
722 if (flags
& QItemSelectionModel::Clear
)
724 d
->lastSelection
= QItemSelection();
727 QModelIndexList dirtyIndexes
= d
->intersectionSet(rect
);
729 QItemSelection selection
;
731 if (!dirtyIndexes
.count())
733 if (d
->lastSelection
.count())
735 selectionModel()->select(d
->lastSelection
, flags
);
741 if (!d
->mouseButtonPressed
)
743 selection
= QItemSelection(dirtyIndexes
[0], dirtyIndexes
[0]);
744 d
->currentViewIndex
= dirtyIndexes
[0];
748 QModelIndex first
= dirtyIndexes
[0];
750 foreach (const QModelIndex
&index
, dirtyIndexes
)
752 if (last
.isValid() && last
.row() + 1 != index
.row())
754 QItemSelectionRange
range(first
, last
);
765 selection
<< QItemSelectionRange(first
, last
);
768 if (d
->lastSelection
.count() && !d
->mouseButtonPressed
)
770 selection
.merge(d
->lastSelection
, flags
);
772 else if (d
->lastSelection
.count())
774 selection
.merge(d
->lastSelection
, QItemSelectionModel::Select
);
777 selectionModel()->select(selection
, flags
);
780 void KCategorizedView::mouseMoveEvent(QMouseEvent
*event
)
782 QListView::mouseMoveEvent(event
);
784 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
790 const QString previousHoveredCategory
= d
->hoveredCategory
;
792 d
->mousePosition
= event
->pos();
793 d
->hoveredCategory
= QString();
796 foreach (const QString
&category
, d
->categories
)
798 if (d
->categoryVisualRect(category
).intersects(QRect(event
->pos(), event
->pos())))
800 d
->hoveredCategory
= category
;
801 viewport()->update(d
->categoryVisualRect(category
));
803 else if ((category
== previousHoveredCategory
) &&
804 (!d
->categoryVisualRect(previousHoveredCategory
).intersects(QRect(event
->pos(), event
->pos()))))
806 viewport()->update(d
->categoryVisualRect(category
));
811 if (d
->mouseButtonPressed
&& !d
->isDragging
)
813 QPoint start
, end
, initialPressPosition
;
815 initialPressPosition
= d
->initialPressPosition
;
817 initialPressPosition
.setY(initialPressPosition
.y() - verticalOffset());
818 initialPressPosition
.setX(initialPressPosition
.x() - horizontalOffset());
820 if (d
->initialPressPosition
.x() > d
->mousePosition
.x() ||
821 d
->initialPressPosition
.y() > d
->mousePosition
.y())
823 start
= d
->mousePosition
;
824 end
= initialPressPosition
;
828 start
= initialPressPosition
;
829 end
= d
->mousePosition
;
832 rect
= QRect(start
, end
).intersected(viewport()->rect().adjusted(-16, -16, 16, 16));
834 //viewport()->update(rect.united(d->lastSelectionRect));
836 d
->lastSelectionRect
= rect
;
840 void KCategorizedView::mousePressEvent(QMouseEvent
*event
)
842 d
->dragLeftViewport
= false;
844 if (event
->button() == Qt::LeftButton
)
846 d
->mouseButtonPressed
= true;
848 d
->initialPressPosition
= event
->pos();
849 d
->initialPressPosition
.setY(d
->initialPressPosition
.y() +
851 d
->initialPressPosition
.setX(d
->initialPressPosition
.x() +
855 QListView::mousePressEvent(event
);
858 void KCategorizedView::mouseReleaseEvent(QMouseEvent
*event
)
860 d
->mouseButtonPressed
= false;
862 QListView::mouseReleaseEvent(event
);
864 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
870 QPoint initialPressPosition
= viewport()->mapFromGlobal(QCursor::pos());
871 initialPressPosition
.setY(initialPressPosition
.y() + verticalOffset());
872 initialPressPosition
.setX(initialPressPosition
.x() + horizontalOffset());
874 QItemSelection selection
;
876 if (initialPressPosition
== d
->initialPressPosition
)
878 foreach(const QString
&category
, d
->categories
)
880 if (d
->categoryVisualRect(category
).contains(event
->pos()))
882 foreach (const QModelIndex
&index
, d
->categoriesIndexes
[category
])
884 selection
<< QItemSelectionRange(d
->proxyModel
->mapFromSource(index
));
887 selectionModel()->select(selection
, QItemSelectionModel::Select
);
894 d
->lastSelection
= selectionModel()->selection();
896 if (d
->hovered
.isValid())
897 viewport()->update(d
->visualRect(d
->hovered
));
898 else if (!d
->hoveredCategory
.isEmpty())
899 viewport()->update(d
->categoryVisualRect(d
->hoveredCategory
));
902 void KCategorizedView::leaveEvent(QEvent
*event
)
904 d
->hovered
= QModelIndex();
905 d
->hoveredCategory
= QString();
907 QListView::leaveEvent(event
);
910 void KCategorizedView::startDrag(Qt::DropActions supportedActions
)
912 QListView::startDrag(supportedActions
);
914 d
->isDragging
= false;
915 d
->mouseButtonPressed
= false;
917 viewport()->update(d
->lastDraggedItemsRect
);
920 void KCategorizedView::dragMoveEvent(QDragMoveEvent
*event
)
922 d
->mousePosition
= event
->pos();
924 if (d
->mouseButtonPressed
)
926 d
->isDragging
= true;
930 d
->isDragging
= false;
933 d
->dragLeftViewport
= false;
935 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
938 QListView::dragMoveEvent(event
);
942 d
->drawDraggedItems();
945 void KCategorizedView::dragLeaveEvent(QDragLeaveEvent
*event
)
947 d
->dragLeftViewport
= true;
949 QListView::dragLeaveEvent(event
);
952 QModelIndex
KCategorizedView::moveCursor(CursorAction cursorAction
,
953 Qt::KeyboardModifiers modifiers
)
955 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
958 return QListView::moveCursor(cursorAction
, modifiers
);
961 const QModelIndex current
= selectionModel()->currentIndex();
963 int viewportWidth
= viewport()->width() - spacing();
966 if (gridSize().isEmpty())
968 itemWidth
= d
->biggestItemSize
.width();
972 itemWidth
= gridSize().width();
975 int itemWidthPlusSeparation
= spacing() + itemWidth
;
976 int elementsPerRow
= viewportWidth
/ itemWidthPlusSeparation
;
978 QString lastCategory
= d
->categories
[0];
979 QString theCategory
= d
->categories
[0];
980 QString afterCategory
= d
->categories
[0];
981 bool hasToBreak
= false;
982 foreach (const QString
&category
, d
->categories
)
986 afterCategory
= category
;
991 if (category
== d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].category
)
993 theCategory
= category
;
1000 lastCategory
= category
;
1004 switch (cursorAction
)
1006 case QAbstractItemView::MoveUp
: {
1007 if (d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].relativeOffsetToCategory
>= elementsPerRow
)
1009 int indexToMove
= d
->invertedElementDictionary
[current
].row();
1010 indexToMove
-= qMin(((d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].relativeOffsetToCategory
) + d
->forcedSelectionPosition
), elementsPerRow
- d
->forcedSelectionPosition
+ (d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].relativeOffsetToCategory
% elementsPerRow
));
1012 return d
->elementDictionary
[d
->proxyModel
->index(indexToMove
, 0)];
1016 int lastCategoryLastRow
= (d
->categoriesIndexes
[lastCategory
].count() - 1) % elementsPerRow
;
1017 int indexToMove
= d
->invertedElementDictionary
[current
].row() - d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].relativeOffsetToCategory
;
1019 if (d
->forcedSelectionPosition
>= lastCategoryLastRow
)
1025 indexToMove
-= qMin((lastCategoryLastRow
- d
->forcedSelectionPosition
+ 1), d
->forcedSelectionPosition
+ elementsPerRow
+ 1);
1028 return d
->elementDictionary
[d
->proxyModel
->index(indexToMove
, 0)];
1032 case QAbstractItemView::MoveDown
: {
1033 if (d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].relativeOffsetToCategory
< (d
->categoriesIndexes
[theCategory
].count() - 1 - ((d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
)))
1035 int indexToMove
= d
->invertedElementDictionary
[current
].row();
1036 indexToMove
+= qMin(elementsPerRow
, d
->categoriesIndexes
[theCategory
].count() - 1 - d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].relativeOffsetToCategory
);
1038 return d
->elementDictionary
[d
->proxyModel
->index(indexToMove
, 0)];
1042 int afterCategoryLastRow
= qMin(elementsPerRow
, d
->categoriesIndexes
[afterCategory
].count());
1043 int indexToMove
= d
->invertedElementDictionary
[current
].row() + (d
->categoriesIndexes
[theCategory
].count() - d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].relativeOffsetToCategory
);
1045 if (d
->forcedSelectionPosition
>= afterCategoryLastRow
)
1047 indexToMove
+= afterCategoryLastRow
- 1;
1051 indexToMove
+= qMin(d
->forcedSelectionPosition
, elementsPerRow
);
1054 return d
->elementDictionary
[d
->proxyModel
->index(indexToMove
, 0)];
1058 case QAbstractItemView::MoveLeft
:
1059 d
->forcedSelectionPosition
= d
->elementsInfo
[d
->proxyModel
->mapToSource(d
->elementDictionary
[d
->proxyModel
->index(d
->invertedElementDictionary
[current
].row() - 1, 0)])].relativeOffsetToCategory
% elementsPerRow
;
1061 if (d
->forcedSelectionPosition
< 0)
1062 d
->forcedSelectionPosition
= (d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
;
1064 return d
->elementDictionary
[d
->proxyModel
->index(d
->invertedElementDictionary
[current
].row() - 1, 0)];
1066 case QAbstractItemView::MoveRight
:
1067 d
->forcedSelectionPosition
= d
->elementsInfo
[d
->proxyModel
->mapToSource(d
->elementDictionary
[d
->proxyModel
->index(d
->invertedElementDictionary
[current
].row() + 1, 0)])].relativeOffsetToCategory
% elementsPerRow
;
1069 if (d
->forcedSelectionPosition
< 0)
1070 d
->forcedSelectionPosition
= (d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
;
1072 return d
->elementDictionary
[d
->proxyModel
->index(d
->invertedElementDictionary
[current
].row() + 1, 0)];
1078 return QListView::moveCursor(cursorAction
, modifiers
);
1081 void KCategorizedView::rowsInserted(const QModelIndex
&parent
,
1085 QListView::rowsInserted(parent
, start
, end
);
1087 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
1088 !d
->itemCategorizer
)
1090 d
->lastSelection
= QItemSelection();
1091 d
->currentViewIndex
= QModelIndex();
1092 d
->forcedSelectionPosition
= 0;
1093 d
->elementsInfo
.clear();
1094 d
->elementsPosition
.clear();
1095 d
->elementDictionary
.clear();
1096 d
->invertedElementDictionary
.clear();
1097 d
->categoriesIndexes
.clear();
1098 d
->categoriesPosition
.clear();
1099 d
->categories
.clear();
1100 d
->intersectedIndexes
.clear();
1101 d
->sourceModelIndexList
.clear();
1102 d
->hovered
= QModelIndex();
1103 d
->biggestItemSize
= QSize(0, 0);
1104 d
->mouseButtonPressed
= false;
1109 rowsInsertedArtifficial(parent
, start
, end
);
1112 void KCategorizedView::rowsInsertedArtifficial(const QModelIndex
&parent
,
1118 d
->lastSelection
= QItemSelection();
1119 d
->currentViewIndex
= QModelIndex();
1120 d
->forcedSelectionPosition
= 0;
1121 d
->elementsInfo
.clear();
1122 d
->elementsPosition
.clear();
1123 d
->elementDictionary
.clear();
1124 d
->invertedElementDictionary
.clear();
1125 d
->categoriesIndexes
.clear();
1126 d
->categoriesPosition
.clear();
1127 d
->categories
.clear();
1128 d
->intersectedIndexes
.clear();
1129 d
->sourceModelIndexList
.clear();
1130 d
->hovered
= QModelIndex();
1131 d
->biggestItemSize
= QSize(0, 0);
1132 d
->mouseButtonPressed
= false;
1134 if (start
> end
|| end
< 0 || start
< 0 || !d
->proxyModel
->rowCount())
1139 // Add all elements mapped to the source model
1140 for (int k
= 0; k
< d
->proxyModel
->rowCount(); k
++)
1142 d
->biggestItemSize
= QSize(qMax(sizeHintForIndex(d
->proxyModel
->index(k
, 0)).width(),
1143 d
->biggestItemSize
.width()),
1144 qMax(sizeHintForIndex(d
->proxyModel
->index(k
, 0)).height(),
1145 d
->biggestItemSize
.height()));
1147 d
->sourceModelIndexList
<<
1148 d
->proxyModel
->mapToSource(d
->proxyModel
->index(k
, 0));
1151 // Sort them with the general purpose lessThan method
1152 LessThan
generalLessThan(d
->proxyModel
,
1153 LessThan::GeneralPurpose
);
1155 qStableSort(d
->sourceModelIndexList
.begin(), d
->sourceModelIndexList
.end(),
1158 // Explore categories
1159 QString prevCategory
=
1160 d
->itemCategorizer
->categoryForItem(d
->sourceModelIndexList
[0],
1161 d
->proxyModel
->sortRole());
1162 QString lastCategory
= prevCategory
;
1163 QModelIndexList modelIndexList
;
1164 struct Private::ElementInfo elementInfo
;
1165 foreach (const QModelIndex
&index
, d
->sourceModelIndexList
)
1167 lastCategory
= d
->itemCategorizer
->categoryForItem(index
,
1168 d
->proxyModel
->sortRole());
1170 elementInfo
.category
= lastCategory
;
1172 if (prevCategory
!= lastCategory
)
1174 d
->categoriesIndexes
.insert(prevCategory
, modelIndexList
);
1175 d
->categories
<< prevCategory
;
1176 modelIndexList
.clear();
1179 modelIndexList
<< index
;
1180 prevCategory
= lastCategory
;
1182 d
->elementsInfo
.insert(index
, elementInfo
);
1185 d
->categoriesIndexes
.insert(prevCategory
, modelIndexList
);
1186 d
->categories
<< prevCategory
;
1188 // Sort items locally in their respective categories with the category
1190 LessThan
categoryLessThan(d
->proxyModel
,
1191 LessThan::CategoryPurpose
);
1193 foreach (const QString
&key
, d
->categories
)
1195 QModelIndexList
&indexList
= d
->categoriesIndexes
[key
];
1197 qStableSort(indexList
.begin(), indexList
.end(), categoryLessThan
);
1200 d
->lastIndex
= d
->categoriesIndexes
[d
->categories
[d
->categories
.count() - 1]][d
->categoriesIndexes
[d
->categories
[d
->categories
.count() - 1]].count() - 1];
1202 // Finally, fill data information of items situation. This will help when
1203 // trying to compute an item place in the viewport
1204 int i
= 0; // position relative to the category beginning
1205 int j
= 0; // number of elements before current
1206 foreach (const QString
&key
, d
->categories
)
1208 foreach (const QModelIndex
&index
, d
->categoriesIndexes
[key
])
1210 struct Private::ElementInfo
&elementInfo
= d
->elementsInfo
[index
];
1212 elementInfo
.relativeOffsetToCategory
= i
;
1214 d
->elementDictionary
.insert(d
->proxyModel
->index(j
, 0),
1215 d
->proxyModel
->mapFromSource(index
));
1217 d
->invertedElementDictionary
.insert(d
->proxyModel
->mapFromSource(index
),
1218 d
->proxyModel
->index(j
, 0));
1227 d
->updateScrollbars();
1230 void KCategorizedView::rowsRemoved(const QModelIndex
&parent
,
1234 if ((viewMode() == KCategorizedView::IconMode
) && d
->proxyModel
&&
1237 // Force the view to update all elements
1238 rowsInsertedArtifficial(parent
, start
, end
);
1242 void KCategorizedView::updateGeometries()
1244 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
1245 !d
->itemCategorizer
)
1247 QListView::updateGeometries();
1251 // Avoid QListView::updateGeometries(), since it will try to set another
1252 // range to our scroll bars, what we don't want (ereslibre)
1253 QAbstractItemView::updateGeometries();
1256 void KCategorizedView::slotSortingRoleChanged()
1258 if ((viewMode() == KCategorizedView::IconMode
) && d
->proxyModel
&&
1261 // Force the view to update all elements
1262 rowsInsertedArtifficial(QModelIndex(), 0, d
->proxyModel
->rowCount() - 1);
1266 #include "kcategorizedview.moc"