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 , mouseButtonPressed(false)
83 , dragLeftViewport(false)
85 , lastIndex(QModelIndex())
89 KCategorizedView::Private::~Private()
93 const QModelIndexList
&KCategorizedView::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
KCategorizedView::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 itemCategorizer
->categoryHeight(listView
->viewOptions()), 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() +
172 foreach (const QString
&category
, categories
)
174 if (category
== curCategory
)
177 float rows
= (float) ((float) categoriesIndexes
[category
].count() /
178 (float) elementsPerRow
);
179 int rowsInt
= categoriesIndexes
[category
].count() / elementsPerRow
;
181 if (rows
- trunc(rows
)) rowsInt
++;
183 retRect
.setTop(retRect
.top() +
184 (rowsInt
* listView
->spacing()) +
185 (rowsInt
* itemHeight
) +
186 itemCategorizer
->categoryHeight(listView
->viewOptions()) +
187 listView
->spacing() * 2);
190 retRect
.setTop(retRect
.top() + row
* listView
->spacing() +
193 retRect
.setWidth(itemWidth
);
194 retRect
.setHeight(itemHeight
);
199 QRect
KCategorizedView::Private::visualCategoryRectInViewport(const QString
&category
)
202 QRect
retRect(listView
->spacing(),
204 listView
->viewport()->width() - listView
->spacing() * 2,
207 if (!proxyModel
->rowCount() || !categories
.contains(category
))
210 QModelIndex index
= proxyModel
->index(0, 0, QModelIndex());
212 int viewportWidth
= listView
->viewport()->width() - listView
->spacing();
214 // We really need all items to be of same size. Otherwise we cannot do this
216 // QSize itemSize = listView->sizeHintForIndex(index);
217 // int itemHeight = itemSize.height();
218 // int itemWidth = itemSize.width();
219 int itemHeight
= 107;
221 int itemWidthPlusSeparation
= listView
->spacing() + itemWidth
;
222 int elementsPerRow
= viewportWidth
/ itemWidthPlusSeparation
;
227 foreach (const QString
&itCategory
, categories
)
229 if (itCategory
== category
)
232 float rows
= (float) ((float) categoriesIndexes
[itCategory
].count() /
233 (float) elementsPerRow
);
234 int rowsInt
= categoriesIndexes
[itCategory
].count() / elementsPerRow
;
236 if (rows
- trunc(rows
)) rowsInt
++;
238 retRect
.setTop(retRect
.top() +
239 (rowsInt
* listView
->spacing()) +
240 (rowsInt
* itemHeight
) +
241 itemCategorizer
->categoryHeight(listView
->viewOptions()) +
242 listView
->spacing() * 2);
245 retRect
.setHeight(itemCategorizer
->categoryHeight(listView
->viewOptions()));
250 // We're sure elementsPosition doesn't contain index
251 const QRect
&KCategorizedView::Private::cacheIndex(const QModelIndex
&index
)
253 QRect rect
= visualRectInViewport(index
);
254 elementsPosition
[index
] = rect
;
256 return elementsPosition
[index
];
259 // We're sure categoriesPosition doesn't contain category
260 const QRect
&KCategorizedView::Private::cacheCategory(const QString
&category
)
262 QRect rect
= visualCategoryRectInViewport(category
);
263 categoriesPosition
[category
] = rect
;
265 return categoriesPosition
[category
];
268 const QRect
&KCategorizedView::Private::cachedRectIndex(const QModelIndex
&index
)
270 if (elementsPosition
.contains(index
)) // If we have it cached
272 return elementsPosition
[index
];
274 else // Otherwise, cache it
276 return cacheIndex(index
);
280 const QRect
&KCategorizedView::Private::cachedRectCategory(const QString
&category
)
282 if (categoriesPosition
.contains(category
)) // If we have it cached
284 return categoriesPosition
[category
];
286 else // Otherwise, cache it and
288 return cacheCategory(category
);
292 QRect
KCategorizedView::Private::visualRect(const QModelIndex
&index
)
294 QModelIndex mappedIndex
= proxyModel
->mapToSource(index
);
296 QRect retRect
= cachedRectIndex(mappedIndex
);
297 int dx
= -listView
->horizontalOffset();
298 int dy
= -listView
->verticalOffset();
299 retRect
.adjust(dx
, dy
, dx
, dy
);
304 QRect
KCategorizedView::Private::categoryVisualRect(const QString
&category
)
306 QRect retRect
= cachedRectCategory(category
);
307 int dx
= -listView
->horizontalOffset();
308 int dy
= -listView
->verticalOffset();
309 retRect
.adjust(dx
, dy
, dx
, dy
);
314 void KCategorizedView::Private::drawNewCategory(const QModelIndex
&index
,
316 const QStyleOption
&option
,
319 QStyleOption optionCopy
= option
;
320 const QString category
= itemCategorizer
->categoryForItem(index
, sortRole
);
322 if ((category
== hoveredCategory
) && !mouseButtonPressed
)
324 optionCopy
.state
|= QStyle::State_MouseOver
;
327 itemCategorizer
->drawCategory(index
,
334 void KCategorizedView::Private::updateScrollbars()
336 int lastItemBottom
= cachedRectIndex(lastIndex
).bottom() +
337 listView
->spacing() - listView
->viewport()->height();
339 listView
->verticalScrollBar()->setSingleStep(listView
->viewport()->height() / 10);
340 listView
->verticalScrollBar()->setPageStep(listView
->viewport()->height());
341 listView
->verticalScrollBar()->setRange(0, lastItemBottom
);
344 void KCategorizedView::Private::drawDraggedItems(QPainter
*painter
)
346 QStyleOptionViewItemV3 option
= listView
->viewOptions();
347 option
.state
&= ~QStyle::State_MouseOver
;
348 foreach (const QModelIndex
&index
, listView
->selectionModel()->selectedIndexes())
350 const int dx
= mousePosition
.x() - initialPressPosition
.x() + listView
->horizontalOffset();
351 const int dy
= mousePosition
.y() - initialPressPosition
.y() + listView
->verticalOffset();
353 option
.rect
= visualRect(index
);
354 option
.rect
.adjust(dx
, dy
, dx
, dy
);
356 if (option
.rect
.intersects(listView
->viewport()->rect()))
358 listView
->itemDelegate(index
)->paint(painter
, option
, index
);
363 void KCategorizedView::Private::drawDraggedItems()
367 foreach (const QModelIndex
&index
, listView
->selectionModel()->selectedIndexes())
369 int dx
= mousePosition
.x() - initialPressPosition
.x() + listView
->horizontalOffset();
370 int dy
= mousePosition
.y() - initialPressPosition
.y() + listView
->verticalOffset();
372 currentRect
= visualRect(index
);
373 currentRect
.adjust(dx
, dy
, dx
, dy
);
375 if (currentRect
.intersects(listView
->viewport()->rect()))
377 rectToUpdate
= rectToUpdate
.united(currentRect
);
381 listView
->viewport()->update(lastDraggedItemsRect
.united(rectToUpdate
));
383 lastDraggedItemsRect
= rectToUpdate
;
387 //==============================================================================
390 KCategorizedView::KCategorizedView(QWidget
*parent
)
392 , d(new Private(this))
396 KCategorizedView::~KCategorizedView()
401 void KCategorizedView::setModel(QAbstractItemModel
*model
)
403 d
->lastSelection
= QItemSelection();
404 d
->currentViewIndex
= QModelIndex();
405 d
->forcedSelectionPosition
= 0;
406 d
->elementsInfo
.clear();
407 d
->elementsPosition
.clear();
408 d
->elementDictionary
.clear();
409 d
->invertedElementDictionary
.clear();
410 d
->categoriesIndexes
.clear();
411 d
->categoriesPosition
.clear();
412 d
->categories
.clear();
413 d
->intersectedIndexes
.clear();
414 d
->sourceModelIndexList
.clear();
415 d
->hovered
= QModelIndex();
416 d
->mouseButtonPressed
= false;
420 QObject::disconnect(d
->proxyModel
,
421 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
422 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
424 QObject::disconnect(d
->proxyModel
,
425 SIGNAL(sortingRoleChanged()),
426 this, SLOT(slotSortingRoleChanged()));
429 QListView::setModel(model
);
431 d
->proxyModel
= dynamic_cast<KSortFilterProxyModel
*>(model
);
435 QObject::connect(d
->proxyModel
,
436 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
437 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
439 QObject::connect(d
->proxyModel
,
440 SIGNAL(sortingRoleChanged()),
441 this, SLOT(slotSortingRoleChanged()));
445 QRect
KCategorizedView::visualRect(const QModelIndex
&index
) const
447 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
450 return QListView::visualRect(index
);
453 if (!qobject_cast
<const QSortFilterProxyModel
*>(index
.model()))
455 return d
->visualRect(d
->proxyModel
->mapFromSource(index
));
458 return d
->visualRect(index
);
461 KItemCategorizer
*KCategorizedView::itemCategorizer() const
463 return d
->itemCategorizer
;
466 void KCategorizedView::setItemCategorizer(KItemCategorizer
*itemCategorizer
)
468 d
->lastSelection
= QItemSelection();
469 d
->currentViewIndex
= QModelIndex();
470 d
->forcedSelectionPosition
= 0;
471 d
->elementsInfo
.clear();
472 d
->elementsPosition
.clear();
473 d
->elementDictionary
.clear();
474 d
->invertedElementDictionary
.clear();
475 d
->categoriesIndexes
.clear();
476 d
->categoriesPosition
.clear();
477 d
->categories
.clear();
478 d
->intersectedIndexes
.clear();
479 d
->sourceModelIndexList
.clear();
480 d
->hovered
= QModelIndex();
481 d
->mouseButtonPressed
= false;
483 if (!itemCategorizer
&& d
->proxyModel
)
485 QObject::disconnect(d
->proxyModel
,
486 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
487 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
489 QObject::disconnect(d
->proxyModel
,
490 SIGNAL(sortingRoleChanged()),
491 this, SLOT(slotSortingRoleChanged()));
493 else if (itemCategorizer
&& d
->proxyModel
)
495 QObject::connect(d
->proxyModel
,
496 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
497 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
499 QObject::connect(d
->proxyModel
,
500 SIGNAL(sortingRoleChanged()),
501 this, SLOT(slotSortingRoleChanged()));
504 d
->itemCategorizer
= itemCategorizer
;
508 rowsInserted(QModelIndex(), 0, d
->proxyModel
->rowCount() - 1);
516 QModelIndex
KCategorizedView::indexAt(const QPoint
&point
) const
518 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
521 return QListView::indexAt(point
);
526 QModelIndexList item
= d
->intersectionSet(QRect(point
, point
));
528 if (item
.count() == 1)
538 void KCategorizedView::reset()
542 d
->lastSelection
= QItemSelection();
543 d
->currentViewIndex
= QModelIndex();
544 d
->forcedSelectionPosition
= 0;
545 d
->elementsInfo
.clear();
546 d
->elementsPosition
.clear();
547 d
->elementDictionary
.clear();
548 d
->invertedElementDictionary
.clear();
549 d
->categoriesIndexes
.clear();
550 d
->categoriesPosition
.clear();
551 d
->categories
.clear();
552 d
->intersectedIndexes
.clear();
553 d
->sourceModelIndexList
.clear();
554 d
->hovered
= QModelIndex();
555 d
->mouseButtonPressed
= false;
558 void KCategorizedView::paintEvent(QPaintEvent
*event
)
560 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
563 QListView::paintEvent(event
);
567 QStyleOptionViewItemV3 option
= viewOptions();
568 QPainter
painter(viewport());
569 QRect area
= event
->rect();
570 const bool focus
= (hasFocus() || viewport()->hasFocus()) &&
571 currentIndex().isValid();
572 const QStyle::State state
= option
.state
;
573 const bool enabled
= (state
& QStyle::State_Enabled
) != 0;
577 QModelIndexList dirtyIndexes
= d
->intersectionSet(area
);
578 foreach (const QModelIndex
&index
, dirtyIndexes
)
580 option
.state
= state
;
581 option
.rect
= d
->visualRect(index
);
583 if (selectionModel() && selectionModel()->isSelected(index
))
585 option
.state
|= QStyle::State_Selected
;
590 QPalette::ColorGroup cg
;
591 if ((d
->proxyModel
->flags(index
) & Qt::ItemIsEnabled
) == 0)
593 option
.state
&= ~QStyle::State_Enabled
;
594 cg
= QPalette::Disabled
;
598 cg
= QPalette::Normal
;
600 option
.palette
.setCurrentColorGroup(cg
);
603 if (focus
&& currentIndex() == index
)
605 option
.state
|= QStyle::State_HasFocus
;
606 if (this->state() == EditingState
)
607 option
.state
|= QStyle::State_Editing
;
610 if ((index
== d
->hovered
) && !d
->mouseButtonPressed
)
611 option
.state
|= QStyle::State_MouseOver
;
613 option
.state
&= ~QStyle::State_MouseOver
;
615 itemDelegate(index
)->paint(&painter
, option
, index
);
620 QStyleOptionViewItem otherOption
;
621 foreach (const QString
&category
, d
->categories
)
623 otherOption
= option
;
624 otherOption
.rect
= d
->categoryVisualRect(category
);
625 otherOption
.state
&= ~QStyle::State_MouseOver
;
627 if (otherOption
.rect
.intersects(area
))
629 d
->drawNewCategory(d
->categoriesIndexes
[category
][0],
630 d
->proxyModel
->sortRole(), otherOption
, &painter
);
634 if (d
->mouseButtonPressed
&& !d
->isDragging
)
636 QPoint start
, end
, initialPressPosition
;
638 initialPressPosition
= d
->initialPressPosition
;
640 initialPressPosition
.setY(initialPressPosition
.y() - verticalOffset());
641 initialPressPosition
.setX(initialPressPosition
.x() - horizontalOffset());
643 if (d
->initialPressPosition
.x() > d
->mousePosition
.x() ||
644 d
->initialPressPosition
.y() > d
->mousePosition
.y())
646 start
= d
->mousePosition
;
647 end
= initialPressPosition
;
651 start
= initialPressPosition
;
652 end
= d
->mousePosition
;
655 QStyleOptionRubberBand yetAnotherOption
;
656 yetAnotherOption
.initFrom(this);
657 yetAnotherOption
.shape
= QRubberBand::Rectangle
;
658 yetAnotherOption
.opaque
= false;
659 yetAnotherOption
.rect
= QRect(start
, end
).intersected(viewport()->rect().adjusted(-16, -16, 16, 16));
661 style()->drawControl(QStyle::CE_RubberBand
, &yetAnotherOption
, &painter
);
665 if (d
->isDragging
&& !d
->dragLeftViewport
)
667 painter
.setOpacity(0.5);
668 d
->drawDraggedItems(&painter
);
674 void KCategorizedView::resizeEvent(QResizeEvent
*event
)
676 QListView::resizeEvent(event
);
678 // Clear the items positions cache
679 d
->elementsPosition
.clear();
680 d
->categoriesPosition
.clear();
681 d
->forcedSelectionPosition
= 0;
683 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
689 d
->updateScrollbars();
692 void KCategorizedView::setSelection(const QRect
&rect
,
693 QItemSelectionModel::SelectionFlags flags
)
695 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
698 QListView::setSelection(rect
, flags
);
705 selectionModel()->clear();
707 if (flags
& QItemSelectionModel::Clear
)
709 d
->lastSelection
= QItemSelection();
712 QModelIndexList dirtyIndexes
= d
->intersectionSet(rect
);
714 QItemSelection selection
;
716 if (!dirtyIndexes
.count())
718 if (d
->lastSelection
.count())
720 selectionModel()->select(d
->lastSelection
, flags
);
726 if (!d
->mouseButtonPressed
)
728 selection
= QItemSelection(dirtyIndexes
[0], dirtyIndexes
[0]);
729 d
->currentViewIndex
= dirtyIndexes
[0];
733 QModelIndex first
= dirtyIndexes
[0];
735 foreach (const QModelIndex
&index
, dirtyIndexes
)
737 if (last
.isValid() && last
.row() + 1 != index
.row())
739 QItemSelectionRange
range(first
, last
);
750 selection
<< QItemSelectionRange(first
, last
);
753 if (d
->lastSelection
.count() && !d
->mouseButtonPressed
)
755 selection
.merge(d
->lastSelection
, flags
);
757 else if (d
->lastSelection
.count())
759 selection
.merge(d
->lastSelection
, QItemSelectionModel::Select
);
762 selectionModel()->select(selection
, flags
);
765 void KCategorizedView::mouseMoveEvent(QMouseEvent
*event
)
767 QListView::mouseMoveEvent(event
);
769 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
775 const QString previousHoveredCategory
= d
->hoveredCategory
;
777 d
->mousePosition
= event
->pos();
778 d
->hoveredCategory
= QString();
781 foreach (const QString
&category
, d
->categories
)
783 if (d
->categoryVisualRect(category
).intersects(QRect(event
->pos(), event
->pos())))
785 d
->hoveredCategory
= category
;
786 viewport()->update(d
->categoryVisualRect(category
));
788 else if ((category
== previousHoveredCategory
) &&
789 (!d
->categoryVisualRect(previousHoveredCategory
).intersects(QRect(event
->pos(), event
->pos()))))
791 viewport()->update(d
->categoryVisualRect(category
));
796 if (d
->mouseButtonPressed
&& !d
->isDragging
)
798 QPoint start
, end
, initialPressPosition
;
800 initialPressPosition
= d
->initialPressPosition
;
802 initialPressPosition
.setY(initialPressPosition
.y() - verticalOffset());
803 initialPressPosition
.setX(initialPressPosition
.x() - horizontalOffset());
805 if (d
->initialPressPosition
.x() > d
->mousePosition
.x() ||
806 d
->initialPressPosition
.y() > d
->mousePosition
.y())
808 start
= d
->mousePosition
;
809 end
= initialPressPosition
;
813 start
= initialPressPosition
;
814 end
= d
->mousePosition
;
817 rect
= QRect(start
, end
).intersected(viewport()->rect().adjusted(-16, -16, 16, 16));
819 //viewport()->update(rect.united(d->lastSelectionRect));
821 d
->lastSelectionRect
= rect
;
825 void KCategorizedView::mousePressEvent(QMouseEvent
*event
)
827 d
->dragLeftViewport
= false;
829 if (event
->button() == Qt::LeftButton
)
831 d
->mouseButtonPressed
= true;
833 d
->initialPressPosition
= event
->pos();
834 d
->initialPressPosition
.setY(d
->initialPressPosition
.y() +
836 d
->initialPressPosition
.setX(d
->initialPressPosition
.x() +
840 QListView::mousePressEvent(event
);
843 void KCategorizedView::mouseReleaseEvent(QMouseEvent
*event
)
845 d
->mouseButtonPressed
= false;
847 QListView::mouseReleaseEvent(event
);
849 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
855 QPoint initialPressPosition
= viewport()->mapFromGlobal(QCursor::pos());
856 initialPressPosition
.setY(initialPressPosition
.y() + verticalOffset());
857 initialPressPosition
.setX(initialPressPosition
.x() + horizontalOffset());
859 QItemSelection selection
;
861 if (initialPressPosition
== d
->initialPressPosition
)
863 foreach(const QString
&category
, d
->categories
)
865 if (d
->categoryVisualRect(category
).contains(event
->pos()))
867 QItemSelectionRange
selectionRange(d
->proxyModel
->mapFromSource(d
->categoriesIndexes
[category
][0]),
868 d
->proxyModel
->mapFromSource(d
->categoriesIndexes
[category
][d
->categoriesIndexes
[category
].count() - 1]));
870 selection
<< selectionRange
;
872 selectionModel()->select(selection
, QItemSelectionModel::Select
);
879 d
->lastSelection
= selectionModel()->selection();
881 if (d
->hovered
.isValid())
882 viewport()->update(d
->visualRect(d
->hovered
));
883 else if (!d
->hoveredCategory
.isEmpty())
884 viewport()->update(d
->categoryVisualRect(d
->hoveredCategory
));
887 void KCategorizedView::leaveEvent(QEvent
*event
)
889 d
->hovered
= QModelIndex();
890 d
->hoveredCategory
= QString();
892 QListView::leaveEvent(event
);
895 void KCategorizedView::startDrag(Qt::DropActions supportedActions
)
897 QListView::startDrag(supportedActions
);
899 d
->isDragging
= false;
900 d
->mouseButtonPressed
= false;
902 viewport()->update(d
->lastDraggedItemsRect
);
905 void KCategorizedView::dragMoveEvent(QDragMoveEvent
*event
)
907 d
->mousePosition
= event
->pos();
909 if (d
->mouseButtonPressed
)
911 d
->isDragging
= true;
915 d
->isDragging
= false;
918 d
->dragLeftViewport
= false;
920 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
923 QListView::dragMoveEvent(event
);
927 d
->drawDraggedItems();
930 void KCategorizedView::dragLeaveEvent(QDragLeaveEvent
*event
)
932 d
->dragLeftViewport
= true;
934 QListView::dragLeaveEvent(event
);
937 QModelIndex
KCategorizedView::moveCursor(CursorAction cursorAction
,
938 Qt::KeyboardModifiers modifiers
)
940 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
943 return QListView::moveCursor(cursorAction
, modifiers
);
946 const QModelIndex current
= selectionModel()->currentIndex();
948 int viewportWidth
= viewport()->width() - spacing();
949 // We really need all items to be of same size. Otherwise we cannot do this
951 // QSize itemSize = listView->sizeHintForIndex(index);
952 // int itemHeight = itemSize.height();
953 // int itemWidth = itemSize.width();
954 int itemHeight
= 107;
956 int itemWidthPlusSeparation
= spacing() + itemWidth
;
957 int elementsPerRow
= viewportWidth
/ itemWidthPlusSeparation
;
959 QString lastCategory
= d
->categories
[0];
960 QString theCategory
= d
->categories
[0];
961 QString afterCategory
= d
->categories
[0];
962 bool hasToBreak
= false;
963 foreach (const QString
&category
, d
->categories
)
967 afterCategory
= category
;
972 if (category
== d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].category
)
974 theCategory
= category
;
981 lastCategory
= category
;
985 switch (cursorAction
)
987 case QAbstractItemView::MoveUp
: {
988 if (d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].relativeOffsetToCategory
>= elementsPerRow
)
990 int indexToMove
= d
->invertedElementDictionary
[current
].row();
991 indexToMove
-= qMin(((d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].relativeOffsetToCategory
) + d
->forcedSelectionPosition
), elementsPerRow
- d
->forcedSelectionPosition
+ (d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].relativeOffsetToCategory
% elementsPerRow
));
993 return d
->elementDictionary
[d
->proxyModel
->index(indexToMove
, 0)];
997 int lastCategoryLastRow
= (d
->categoriesIndexes
[lastCategory
].count() - 1) % elementsPerRow
;
998 int indexToMove
= d
->invertedElementDictionary
[current
].row() - d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].relativeOffsetToCategory
;
1000 if (d
->forcedSelectionPosition
>= lastCategoryLastRow
)
1006 indexToMove
-= qMin((lastCategoryLastRow
- d
->forcedSelectionPosition
+ 1), d
->forcedSelectionPosition
+ elementsPerRow
+ 1);
1009 return d
->elementDictionary
[d
->proxyModel
->index(indexToMove
, 0)];
1013 case QAbstractItemView::MoveDown
: {
1014 if (d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].relativeOffsetToCategory
< (d
->categoriesIndexes
[theCategory
].count() - 1 - ((d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
)))
1016 int indexToMove
= d
->invertedElementDictionary
[current
].row();
1017 indexToMove
+= qMin(elementsPerRow
, d
->categoriesIndexes
[theCategory
].count() - 1 - d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].relativeOffsetToCategory
);
1019 return d
->elementDictionary
[d
->proxyModel
->index(indexToMove
, 0)];
1023 int afterCategoryLastRow
= qMin(elementsPerRow
, d
->categoriesIndexes
[afterCategory
].count());
1024 int indexToMove
= d
->invertedElementDictionary
[current
].row() + (d
->categoriesIndexes
[theCategory
].count() - d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].relativeOffsetToCategory
);
1026 if (d
->forcedSelectionPosition
>= afterCategoryLastRow
)
1028 indexToMove
+= afterCategoryLastRow
- 1;
1032 indexToMove
+= qMin(d
->forcedSelectionPosition
, elementsPerRow
);
1035 return d
->elementDictionary
[d
->proxyModel
->index(indexToMove
, 0)];
1039 case QAbstractItemView::MoveLeft
:
1040 d
->forcedSelectionPosition
= d
->elementsInfo
[d
->proxyModel
->mapToSource(d
->elementDictionary
[d
->proxyModel
->index(d
->invertedElementDictionary
[current
].row() - 1, 0)])].relativeOffsetToCategory
% elementsPerRow
;
1042 if (d
->forcedSelectionPosition
< 0)
1043 d
->forcedSelectionPosition
= (d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
;
1045 return d
->elementDictionary
[d
->proxyModel
->index(d
->invertedElementDictionary
[current
].row() - 1, 0)];
1047 case QAbstractItemView::MoveRight
:
1048 d
->forcedSelectionPosition
= d
->elementsInfo
[d
->proxyModel
->mapToSource(d
->elementDictionary
[d
->proxyModel
->index(d
->invertedElementDictionary
[current
].row() + 1, 0)])].relativeOffsetToCategory
% elementsPerRow
;
1050 if (d
->forcedSelectionPosition
< 0)
1051 d
->forcedSelectionPosition
= (d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
;
1053 return d
->elementDictionary
[d
->proxyModel
->index(d
->invertedElementDictionary
[current
].row() + 1, 0)];
1059 return QListView::moveCursor(cursorAction
, modifiers
);
1062 void KCategorizedView::rowsInserted(const QModelIndex
&parent
,
1066 QListView::rowsInserted(parent
, start
, end
);
1068 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
1069 !d
->itemCategorizer
)
1071 d
->lastSelection
= QItemSelection();
1072 d
->currentViewIndex
= QModelIndex();
1073 d
->forcedSelectionPosition
= 0;
1074 d
->elementsInfo
.clear();
1075 d
->elementsPosition
.clear();
1076 d
->elementDictionary
.clear();
1077 d
->invertedElementDictionary
.clear();
1078 d
->categoriesIndexes
.clear();
1079 d
->categoriesPosition
.clear();
1080 d
->categories
.clear();
1081 d
->intersectedIndexes
.clear();
1082 d
->sourceModelIndexList
.clear();
1083 d
->hovered
= QModelIndex();
1084 d
->mouseButtonPressed
= false;
1089 rowsInsertedArtifficial(parent
, start
, end
);
1092 void KCategorizedView::rowsInsertedArtifficial(const QModelIndex
&parent
,
1098 d
->lastSelection
= QItemSelection();
1099 d
->currentViewIndex
= QModelIndex();
1100 d
->forcedSelectionPosition
= 0;
1101 d
->elementsInfo
.clear();
1102 d
->elementsPosition
.clear();
1103 d
->elementDictionary
.clear();
1104 d
->invertedElementDictionary
.clear();
1105 d
->categoriesIndexes
.clear();
1106 d
->categoriesPosition
.clear();
1107 d
->categories
.clear();
1108 d
->intersectedIndexes
.clear();
1109 d
->sourceModelIndexList
.clear();
1110 d
->hovered
= QModelIndex();
1111 d
->mouseButtonPressed
= false;
1113 if (start
> end
|| end
< 0 || start
< 0 || !d
->proxyModel
->rowCount())
1118 // Add all elements mapped to the source model
1119 for (int k
= 0; k
< d
->proxyModel
->rowCount(); k
++)
1121 d
->sourceModelIndexList
<<
1122 d
->proxyModel
->mapToSource(d
->proxyModel
->index(k
, 0));
1125 // Sort them with the general purpose lessThan method
1126 LessThan
generalLessThan(d
->proxyModel
,
1127 LessThan::GeneralPurpose
);
1129 qStableSort(d
->sourceModelIndexList
.begin(), d
->sourceModelIndexList
.end(),
1132 // Explore categories
1133 QString prevCategory
=
1134 d
->itemCategorizer
->categoryForItem(d
->sourceModelIndexList
[0],
1135 d
->proxyModel
->sortRole());
1136 QString lastCategory
= prevCategory
;
1137 QModelIndexList modelIndexList
;
1138 struct Private::ElementInfo elementInfo
;
1139 foreach (const QModelIndex
&index
, d
->sourceModelIndexList
)
1141 lastCategory
= d
->itemCategorizer
->categoryForItem(index
,
1142 d
->proxyModel
->sortRole());
1144 elementInfo
.category
= lastCategory
;
1146 if (prevCategory
!= lastCategory
)
1148 d
->categoriesIndexes
.insert(prevCategory
, modelIndexList
);
1149 d
->categories
<< prevCategory
;
1150 modelIndexList
.clear();
1153 modelIndexList
<< index
;
1154 prevCategory
= lastCategory
;
1156 d
->elementsInfo
.insert(index
, elementInfo
);
1159 d
->categoriesIndexes
.insert(prevCategory
, modelIndexList
);
1160 d
->categories
<< prevCategory
;
1162 // Sort items locally in their respective categories with the category
1164 LessThan
categoryLessThan(d
->proxyModel
,
1165 LessThan::CategoryPurpose
);
1167 foreach (const QString
&key
, d
->categories
)
1169 QModelIndexList
&indexList
= d
->categoriesIndexes
[key
];
1171 qStableSort(indexList
.begin(), indexList
.end(), categoryLessThan
);
1174 d
->lastIndex
= d
->categoriesIndexes
[d
->categories
[d
->categories
.count() - 1]][d
->categoriesIndexes
[d
->categories
[d
->categories
.count() - 1]].count() - 1];
1176 // Finally, fill data information of items situation. This will help when
1177 // trying to compute an item place in the viewport
1178 int i
= 0; // position relative to the category beginning
1179 int j
= 0; // number of elements before current
1180 foreach (const QString
&key
, d
->categories
)
1182 foreach (const QModelIndex
&index
, d
->categoriesIndexes
[key
])
1184 struct Private::ElementInfo
&elementInfo
= d
->elementsInfo
[index
];
1186 elementInfo
.relativeOffsetToCategory
= i
;
1188 d
->elementDictionary
.insert(d
->proxyModel
->index(j
, 0),
1189 d
->proxyModel
->mapFromSource(index
));
1191 d
->invertedElementDictionary
.insert(d
->proxyModel
->mapFromSource(index
),
1192 d
->proxyModel
->index(j
, 0));
1201 d
->updateScrollbars();
1204 void KCategorizedView::rowsRemoved(const QModelIndex
&parent
,
1208 if ((viewMode() == KCategorizedView::IconMode
) && d
->proxyModel
&&
1211 // Force the view to update all elements
1212 rowsInsertedArtifficial(parent
, start
, end
);
1216 void KCategorizedView::updateGeometries()
1218 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
1219 !d
->itemCategorizer
)
1221 QListView::updateGeometries();
1225 // Avoid QListView::updateGeometries(), since it will try to set another
1226 // range to our scroll bars, what we don't want (ereslibre)
1227 QAbstractItemView::updateGeometries();
1230 void KCategorizedView::slotSortingRoleChanged()
1232 if ((viewMode() == KCategorizedView::IconMode
) && d
->proxyModel
&&
1235 // Force the view to update all elements
1236 rowsInsertedArtifficial(QModelIndex(), 0, d
->proxyModel
->rowCount() - 1);
1240 #include "kcategorizedview.moc"