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();
154 int itemHeight
= biggestItemSize
.height();
155 int itemWidth
= biggestItemSize
.width();
156 int itemWidthPlusSeparation
= listView
->spacing() + itemWidth
;
157 int elementsPerRow
= viewportWidth
/ itemWidthPlusSeparation
;
161 int column
= elementsInfo
[index
].relativeOffsetToCategory
% elementsPerRow
;
162 int row
= elementsInfo
[index
].relativeOffsetToCategory
/ elementsPerRow
;
164 retRect
.setLeft(retRect
.left() + column
* listView
->spacing() +
167 foreach (const QString
&category
, categories
)
169 if (category
== curCategory
)
172 float rows
= (float) ((float) categoriesIndexes
[category
].count() /
173 (float) elementsPerRow
);
174 int rowsInt
= categoriesIndexes
[category
].count() / elementsPerRow
;
176 if (rows
- trunc(rows
)) rowsInt
++;
178 retRect
.setTop(retRect
.top() +
179 (rowsInt
* listView
->spacing()) +
180 (rowsInt
* itemHeight
) +
181 itemCategorizer
->categoryHeight(listView
->viewOptions()) +
182 listView
->spacing() * 2);
185 retRect
.setTop(retRect
.top() + row
* listView
->spacing() +
188 retRect
.setWidth(itemWidth
);
189 retRect
.setHeight(itemHeight
);
194 QRect
KCategorizedView::Private::visualCategoryRectInViewport(const QString
&category
)
197 QRect
retRect(listView
->spacing(),
199 listView
->viewport()->width() - listView
->spacing() * 2,
202 if (!proxyModel
->rowCount() || !categories
.contains(category
))
205 QModelIndex index
= proxyModel
->index(0, 0, QModelIndex());
207 int viewportWidth
= listView
->viewport()->width() - listView
->spacing();
209 int itemHeight
= biggestItemSize
.height();
210 int itemWidth
= biggestItemSize
.width();
211 int itemWidthPlusSeparation
= listView
->spacing() + itemWidth
;
212 int elementsPerRow
= viewportWidth
/ itemWidthPlusSeparation
;
217 foreach (const QString
&itCategory
, categories
)
219 if (itCategory
== category
)
222 float rows
= (float) ((float) categoriesIndexes
[itCategory
].count() /
223 (float) elementsPerRow
);
224 int rowsInt
= categoriesIndexes
[itCategory
].count() / elementsPerRow
;
226 if (rows
- trunc(rows
)) rowsInt
++;
228 retRect
.setTop(retRect
.top() +
229 (rowsInt
* listView
->spacing()) +
230 (rowsInt
* itemHeight
) +
231 itemCategorizer
->categoryHeight(listView
->viewOptions()) +
232 listView
->spacing() * 2);
235 retRect
.setHeight(itemCategorizer
->categoryHeight(listView
->viewOptions()));
240 // We're sure elementsPosition doesn't contain index
241 const QRect
&KCategorizedView::Private::cacheIndex(const QModelIndex
&index
)
243 QRect rect
= visualRectInViewport(index
);
244 elementsPosition
[index
] = rect
;
246 return elementsPosition
[index
];
249 // We're sure categoriesPosition doesn't contain category
250 const QRect
&KCategorizedView::Private::cacheCategory(const QString
&category
)
252 QRect rect
= visualCategoryRectInViewport(category
);
253 categoriesPosition
[category
] = rect
;
255 return categoriesPosition
[category
];
258 const QRect
&KCategorizedView::Private::cachedRectIndex(const QModelIndex
&index
)
260 if (elementsPosition
.contains(index
)) // If we have it cached
262 return elementsPosition
[index
];
264 else // Otherwise, cache it
266 return cacheIndex(index
);
270 const QRect
&KCategorizedView::Private::cachedRectCategory(const QString
&category
)
272 if (categoriesPosition
.contains(category
)) // If we have it cached
274 return categoriesPosition
[category
];
276 else // Otherwise, cache it and
278 return cacheCategory(category
);
282 QRect
KCategorizedView::Private::visualRect(const QModelIndex
&index
)
284 QModelIndex mappedIndex
= proxyModel
->mapToSource(index
);
286 QRect retRect
= cachedRectIndex(mappedIndex
);
287 int dx
= -listView
->horizontalOffset();
288 int dy
= -listView
->verticalOffset();
289 retRect
.adjust(dx
, dy
, dx
, dy
);
294 QRect
KCategorizedView::Private::categoryVisualRect(const QString
&category
)
296 QRect retRect
= cachedRectCategory(category
);
297 int dx
= -listView
->horizontalOffset();
298 int dy
= -listView
->verticalOffset();
299 retRect
.adjust(dx
, dy
, dx
, dy
);
304 void KCategorizedView::Private::drawNewCategory(const QModelIndex
&index
,
306 const QStyleOption
&option
,
309 QStyleOption optionCopy
= option
;
310 const QString category
= itemCategorizer
->categoryForItem(index
, sortRole
);
312 if ((category
== hoveredCategory
) && !mouseButtonPressed
)
314 optionCopy
.state
|= QStyle::State_MouseOver
;
317 itemCategorizer
->drawCategory(index
,
324 void KCategorizedView::Private::updateScrollbars()
326 int lastItemBottom
= cachedRectIndex(lastIndex
).bottom() +
327 listView
->spacing() - listView
->viewport()->height();
329 listView
->verticalScrollBar()->setSingleStep(listView
->viewport()->height() / 10);
330 listView
->verticalScrollBar()->setPageStep(listView
->viewport()->height());
331 listView
->verticalScrollBar()->setRange(0, lastItemBottom
);
334 void KCategorizedView::Private::drawDraggedItems(QPainter
*painter
)
336 QStyleOptionViewItemV3 option
= listView
->viewOptions();
337 option
.state
&= ~QStyle::State_MouseOver
;
338 foreach (const QModelIndex
&index
, listView
->selectionModel()->selectedIndexes())
340 const int dx
= mousePosition
.x() - initialPressPosition
.x() + listView
->horizontalOffset();
341 const int dy
= mousePosition
.y() - initialPressPosition
.y() + listView
->verticalOffset();
343 option
.rect
= visualRect(index
);
344 option
.rect
.adjust(dx
, dy
, dx
, dy
);
346 if (option
.rect
.intersects(listView
->viewport()->rect()))
348 listView
->itemDelegate(index
)->paint(painter
, option
, index
);
353 void KCategorizedView::Private::drawDraggedItems()
357 foreach (const QModelIndex
&index
, listView
->selectionModel()->selectedIndexes())
359 int dx
= mousePosition
.x() - initialPressPosition
.x() + listView
->horizontalOffset();
360 int dy
= mousePosition
.y() - initialPressPosition
.y() + listView
->verticalOffset();
362 currentRect
= visualRect(index
);
363 currentRect
.adjust(dx
, dy
, dx
, dy
);
365 if (currentRect
.intersects(listView
->viewport()->rect()))
367 rectToUpdate
= rectToUpdate
.united(currentRect
);
371 listView
->viewport()->update(lastDraggedItemsRect
.united(rectToUpdate
));
373 lastDraggedItemsRect
= rectToUpdate
;
377 //==============================================================================
380 KCategorizedView::KCategorizedView(QWidget
*parent
)
382 , d(new Private(this))
386 KCategorizedView::~KCategorizedView()
391 void KCategorizedView::setModel(QAbstractItemModel
*model
)
393 d
->lastSelection
= QItemSelection();
394 d
->currentViewIndex
= QModelIndex();
395 d
->forcedSelectionPosition
= 0;
396 d
->elementsInfo
.clear();
397 d
->elementsPosition
.clear();
398 d
->elementDictionary
.clear();
399 d
->invertedElementDictionary
.clear();
400 d
->categoriesIndexes
.clear();
401 d
->categoriesPosition
.clear();
402 d
->categories
.clear();
403 d
->intersectedIndexes
.clear();
404 d
->sourceModelIndexList
.clear();
405 d
->hovered
= QModelIndex();
406 d
->mouseButtonPressed
= false;
410 QObject::disconnect(d
->proxyModel
,
411 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
412 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
414 QObject::disconnect(d
->proxyModel
,
415 SIGNAL(sortingRoleChanged()),
416 this, SLOT(slotSortingRoleChanged()));
419 QListView::setModel(model
);
421 d
->proxyModel
= dynamic_cast<KSortFilterProxyModel
*>(model
);
425 QObject::connect(d
->proxyModel
,
426 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
427 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
429 QObject::connect(d
->proxyModel
,
430 SIGNAL(sortingRoleChanged()),
431 this, SLOT(slotSortingRoleChanged()));
435 QRect
KCategorizedView::visualRect(const QModelIndex
&index
) const
437 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
440 return QListView::visualRect(index
);
443 if (!qobject_cast
<const QSortFilterProxyModel
*>(index
.model()))
445 return d
->visualRect(d
->proxyModel
->mapFromSource(index
));
448 return d
->visualRect(index
);
451 KItemCategorizer
*KCategorizedView::itemCategorizer() const
453 return d
->itemCategorizer
;
456 void KCategorizedView::setItemCategorizer(KItemCategorizer
*itemCategorizer
)
458 d
->lastSelection
= QItemSelection();
459 d
->currentViewIndex
= QModelIndex();
460 d
->forcedSelectionPosition
= 0;
461 d
->elementsInfo
.clear();
462 d
->elementsPosition
.clear();
463 d
->elementDictionary
.clear();
464 d
->invertedElementDictionary
.clear();
465 d
->categoriesIndexes
.clear();
466 d
->categoriesPosition
.clear();
467 d
->categories
.clear();
468 d
->intersectedIndexes
.clear();
469 d
->sourceModelIndexList
.clear();
470 d
->hovered
= QModelIndex();
471 d
->mouseButtonPressed
= false;
473 if (!itemCategorizer
&& d
->proxyModel
)
475 QObject::disconnect(d
->proxyModel
,
476 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
477 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
479 QObject::disconnect(d
->proxyModel
,
480 SIGNAL(sortingRoleChanged()),
481 this, SLOT(slotSortingRoleChanged()));
483 else if (itemCategorizer
&& d
->proxyModel
)
485 QObject::connect(d
->proxyModel
,
486 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
487 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
489 QObject::connect(d
->proxyModel
,
490 SIGNAL(sortingRoleChanged()),
491 this, SLOT(slotSortingRoleChanged()));
494 d
->itemCategorizer
= itemCategorizer
;
498 rowsInserted(QModelIndex(), 0, d
->proxyModel
->rowCount() - 1);
506 QModelIndex
KCategorizedView::indexAt(const QPoint
&point
) const
508 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
511 return QListView::indexAt(point
);
516 QModelIndexList item
= d
->intersectionSet(QRect(point
, point
));
518 if (item
.count() == 1)
528 void KCategorizedView::reset()
532 d
->lastSelection
= QItemSelection();
533 d
->currentViewIndex
= QModelIndex();
534 d
->forcedSelectionPosition
= 0;
535 d
->elementsInfo
.clear();
536 d
->elementsPosition
.clear();
537 d
->elementDictionary
.clear();
538 d
->invertedElementDictionary
.clear();
539 d
->categoriesIndexes
.clear();
540 d
->categoriesPosition
.clear();
541 d
->categories
.clear();
542 d
->intersectedIndexes
.clear();
543 d
->sourceModelIndexList
.clear();
544 d
->hovered
= QModelIndex();
545 d
->mouseButtonPressed
= false;
548 void KCategorizedView::paintEvent(QPaintEvent
*event
)
550 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
553 QListView::paintEvent(event
);
557 QStyleOptionViewItemV3 option
= viewOptions();
558 option
.widget
= this;
559 QPainter
painter(viewport());
560 QRect area
= event
->rect();
561 const bool focus
= (hasFocus() || viewport()->hasFocus()) &&
562 currentIndex().isValid();
563 const QStyle::State state
= option
.state
;
564 const bool enabled
= (state
& QStyle::State_Enabled
) != 0;
568 QModelIndexList dirtyIndexes
= d
->intersectionSet(area
);
569 foreach (const QModelIndex
&index
, dirtyIndexes
)
571 option
.state
= state
;
572 option
.rect
= d
->visualRect(index
);
574 if (selectionModel() && selectionModel()->isSelected(index
))
576 option
.state
|= QStyle::State_Selected
;
581 QPalette::ColorGroup cg
;
582 if ((d
->proxyModel
->flags(index
) & Qt::ItemIsEnabled
) == 0)
584 option
.state
&= ~QStyle::State_Enabled
;
585 cg
= QPalette::Disabled
;
589 cg
= QPalette::Normal
;
591 option
.palette
.setCurrentColorGroup(cg
);
594 if (focus
&& currentIndex() == index
)
596 option
.state
|= QStyle::State_HasFocus
;
597 if (this->state() == EditingState
)
598 option
.state
|= QStyle::State_Editing
;
601 if ((index
== d
->hovered
) && !d
->mouseButtonPressed
)
602 option
.state
|= QStyle::State_MouseOver
;
604 option
.state
&= ~QStyle::State_MouseOver
;
606 itemDelegate(index
)->paint(&painter
, option
, index
);
611 QStyleOptionViewItem otherOption
;
612 foreach (const QString
&category
, d
->categories
)
614 otherOption
= option
;
615 otherOption
.rect
= d
->categoryVisualRect(category
);
616 otherOption
.state
&= ~QStyle::State_MouseOver
;
618 if (otherOption
.rect
.intersects(area
))
620 d
->drawNewCategory(d
->categoriesIndexes
[category
][0],
621 d
->proxyModel
->sortRole(), otherOption
, &painter
);
625 if (d
->mouseButtonPressed
&& !d
->isDragging
)
627 QPoint start
, end
, initialPressPosition
;
629 initialPressPosition
= d
->initialPressPosition
;
631 initialPressPosition
.setY(initialPressPosition
.y() - verticalOffset());
632 initialPressPosition
.setX(initialPressPosition
.x() - horizontalOffset());
634 if (d
->initialPressPosition
.x() > d
->mousePosition
.x() ||
635 d
->initialPressPosition
.y() > d
->mousePosition
.y())
637 start
= d
->mousePosition
;
638 end
= initialPressPosition
;
642 start
= initialPressPosition
;
643 end
= d
->mousePosition
;
646 QStyleOptionRubberBand yetAnotherOption
;
647 yetAnotherOption
.initFrom(this);
648 yetAnotherOption
.shape
= QRubberBand::Rectangle
;
649 yetAnotherOption
.opaque
= false;
650 yetAnotherOption
.rect
= QRect(start
, end
).intersected(viewport()->rect().adjusted(-16, -16, 16, 16));
652 style()->drawControl(QStyle::CE_RubberBand
, &yetAnotherOption
, &painter
);
656 if (d
->isDragging
&& !d
->dragLeftViewport
)
658 painter
.setOpacity(0.5);
659 d
->drawDraggedItems(&painter
);
665 void KCategorizedView::resizeEvent(QResizeEvent
*event
)
667 QListView::resizeEvent(event
);
669 // Clear the items positions cache
670 d
->elementsPosition
.clear();
671 d
->categoriesPosition
.clear();
672 d
->forcedSelectionPosition
= 0;
674 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
680 d
->updateScrollbars();
683 void KCategorizedView::setSelection(const QRect
&rect
,
684 QItemSelectionModel::SelectionFlags flags
)
686 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
689 QListView::setSelection(rect
, flags
);
696 selectionModel()->clear();
698 if (flags
& QItemSelectionModel::Clear
)
700 d
->lastSelection
= QItemSelection();
703 QModelIndexList dirtyIndexes
= d
->intersectionSet(rect
);
705 QItemSelection selection
;
707 if (!dirtyIndexes
.count())
709 if (d
->lastSelection
.count())
711 selectionModel()->select(d
->lastSelection
, flags
);
717 if (!d
->mouseButtonPressed
)
719 selection
= QItemSelection(dirtyIndexes
[0], dirtyIndexes
[0]);
720 d
->currentViewIndex
= dirtyIndexes
[0];
724 QModelIndex first
= dirtyIndexes
[0];
726 foreach (const QModelIndex
&index
, dirtyIndexes
)
728 if (last
.isValid() && last
.row() + 1 != index
.row())
730 QItemSelectionRange
range(first
, last
);
741 selection
<< QItemSelectionRange(first
, last
);
744 if (d
->lastSelection
.count() && !d
->mouseButtonPressed
)
746 selection
.merge(d
->lastSelection
, flags
);
748 else if (d
->lastSelection
.count())
750 selection
.merge(d
->lastSelection
, QItemSelectionModel::Select
);
753 selectionModel()->select(selection
, flags
);
756 void KCategorizedView::mouseMoveEvent(QMouseEvent
*event
)
758 QListView::mouseMoveEvent(event
);
760 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
766 const QString previousHoveredCategory
= d
->hoveredCategory
;
768 d
->mousePosition
= event
->pos();
769 d
->hoveredCategory
= QString();
772 foreach (const QString
&category
, d
->categories
)
774 if (d
->categoryVisualRect(category
).intersects(QRect(event
->pos(), event
->pos())))
776 d
->hoveredCategory
= category
;
777 viewport()->update(d
->categoryVisualRect(category
));
779 else if ((category
== previousHoveredCategory
) &&
780 (!d
->categoryVisualRect(previousHoveredCategory
).intersects(QRect(event
->pos(), event
->pos()))))
782 viewport()->update(d
->categoryVisualRect(category
));
787 if (d
->mouseButtonPressed
&& !d
->isDragging
)
789 QPoint start
, end
, initialPressPosition
;
791 initialPressPosition
= d
->initialPressPosition
;
793 initialPressPosition
.setY(initialPressPosition
.y() - verticalOffset());
794 initialPressPosition
.setX(initialPressPosition
.x() - horizontalOffset());
796 if (d
->initialPressPosition
.x() > d
->mousePosition
.x() ||
797 d
->initialPressPosition
.y() > d
->mousePosition
.y())
799 start
= d
->mousePosition
;
800 end
= initialPressPosition
;
804 start
= initialPressPosition
;
805 end
= d
->mousePosition
;
808 rect
= QRect(start
, end
).intersected(viewport()->rect().adjusted(-16, -16, 16, 16));
810 //viewport()->update(rect.united(d->lastSelectionRect));
812 d
->lastSelectionRect
= rect
;
816 void KCategorizedView::mousePressEvent(QMouseEvent
*event
)
818 d
->dragLeftViewport
= false;
820 if (event
->button() == Qt::LeftButton
)
822 d
->mouseButtonPressed
= true;
824 d
->initialPressPosition
= event
->pos();
825 d
->initialPressPosition
.setY(d
->initialPressPosition
.y() +
827 d
->initialPressPosition
.setX(d
->initialPressPosition
.x() +
831 QListView::mousePressEvent(event
);
834 void KCategorizedView::mouseReleaseEvent(QMouseEvent
*event
)
836 d
->mouseButtonPressed
= false;
838 QListView::mouseReleaseEvent(event
);
840 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
846 QPoint initialPressPosition
= viewport()->mapFromGlobal(QCursor::pos());
847 initialPressPosition
.setY(initialPressPosition
.y() + verticalOffset());
848 initialPressPosition
.setX(initialPressPosition
.x() + horizontalOffset());
850 QItemSelection selection
;
852 if (initialPressPosition
== d
->initialPressPosition
)
854 foreach(const QString
&category
, d
->categories
)
856 if (d
->categoryVisualRect(category
).contains(event
->pos()))
858 foreach (const QModelIndex
&index
, d
->categoriesIndexes
[category
])
860 selection
<< QItemSelectionRange(d
->proxyModel
->mapFromSource(index
));
863 selectionModel()->select(selection
, QItemSelectionModel::Select
);
870 d
->lastSelection
= selectionModel()->selection();
872 if (d
->hovered
.isValid())
873 viewport()->update(d
->visualRect(d
->hovered
));
874 else if (!d
->hoveredCategory
.isEmpty())
875 viewport()->update(d
->categoryVisualRect(d
->hoveredCategory
));
878 void KCategorizedView::leaveEvent(QEvent
*event
)
880 d
->hovered
= QModelIndex();
881 d
->hoveredCategory
= QString();
883 QListView::leaveEvent(event
);
886 void KCategorizedView::startDrag(Qt::DropActions supportedActions
)
888 QListView::startDrag(supportedActions
);
890 d
->isDragging
= false;
891 d
->mouseButtonPressed
= false;
893 viewport()->update(d
->lastDraggedItemsRect
);
896 void KCategorizedView::dragMoveEvent(QDragMoveEvent
*event
)
898 d
->mousePosition
= event
->pos();
900 if (d
->mouseButtonPressed
)
902 d
->isDragging
= true;
906 d
->isDragging
= false;
909 d
->dragLeftViewport
= false;
911 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
914 QListView::dragMoveEvent(event
);
918 d
->drawDraggedItems();
921 void KCategorizedView::dragLeaveEvent(QDragLeaveEvent
*event
)
923 d
->dragLeftViewport
= true;
925 QListView::dragLeaveEvent(event
);
928 QModelIndex
KCategorizedView::moveCursor(CursorAction cursorAction
,
929 Qt::KeyboardModifiers modifiers
)
931 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
934 return QListView::moveCursor(cursorAction
, modifiers
);
937 const QModelIndex current
= selectionModel()->currentIndex();
939 int viewportWidth
= viewport()->width() - spacing();
940 int itemHeight
= d
->biggestItemSize
.height();
941 int itemWidth
= d
->biggestItemSize
.width();
942 int itemWidthPlusSeparation
= spacing() + itemWidth
;
943 int elementsPerRow
= viewportWidth
/ itemWidthPlusSeparation
;
945 QString lastCategory
= d
->categories
[0];
946 QString theCategory
= d
->categories
[0];
947 QString afterCategory
= d
->categories
[0];
948 bool hasToBreak
= false;
949 foreach (const QString
&category
, d
->categories
)
953 afterCategory
= category
;
958 if (category
== d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].category
)
960 theCategory
= category
;
967 lastCategory
= category
;
971 switch (cursorAction
)
973 case QAbstractItemView::MoveUp
: {
974 if (d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].relativeOffsetToCategory
>= elementsPerRow
)
976 int indexToMove
= d
->invertedElementDictionary
[current
].row();
977 indexToMove
-= qMin(((d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].relativeOffsetToCategory
) + d
->forcedSelectionPosition
), elementsPerRow
- d
->forcedSelectionPosition
+ (d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].relativeOffsetToCategory
% elementsPerRow
));
979 return d
->elementDictionary
[d
->proxyModel
->index(indexToMove
, 0)];
983 int lastCategoryLastRow
= (d
->categoriesIndexes
[lastCategory
].count() - 1) % elementsPerRow
;
984 int indexToMove
= d
->invertedElementDictionary
[current
].row() - d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].relativeOffsetToCategory
;
986 if (d
->forcedSelectionPosition
>= lastCategoryLastRow
)
992 indexToMove
-= qMin((lastCategoryLastRow
- d
->forcedSelectionPosition
+ 1), d
->forcedSelectionPosition
+ elementsPerRow
+ 1);
995 return d
->elementDictionary
[d
->proxyModel
->index(indexToMove
, 0)];
999 case QAbstractItemView::MoveDown
: {
1000 if (d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].relativeOffsetToCategory
< (d
->categoriesIndexes
[theCategory
].count() - 1 - ((d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
)))
1002 int indexToMove
= d
->invertedElementDictionary
[current
].row();
1003 indexToMove
+= qMin(elementsPerRow
, d
->categoriesIndexes
[theCategory
].count() - 1 - d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].relativeOffsetToCategory
);
1005 return d
->elementDictionary
[d
->proxyModel
->index(indexToMove
, 0)];
1009 int afterCategoryLastRow
= qMin(elementsPerRow
, d
->categoriesIndexes
[afterCategory
].count());
1010 int indexToMove
= d
->invertedElementDictionary
[current
].row() + (d
->categoriesIndexes
[theCategory
].count() - d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].relativeOffsetToCategory
);
1012 if (d
->forcedSelectionPosition
>= afterCategoryLastRow
)
1014 indexToMove
+= afterCategoryLastRow
- 1;
1018 indexToMove
+= qMin(d
->forcedSelectionPosition
, elementsPerRow
);
1021 return d
->elementDictionary
[d
->proxyModel
->index(indexToMove
, 0)];
1025 case QAbstractItemView::MoveLeft
:
1026 d
->forcedSelectionPosition
= d
->elementsInfo
[d
->proxyModel
->mapToSource(d
->elementDictionary
[d
->proxyModel
->index(d
->invertedElementDictionary
[current
].row() - 1, 0)])].relativeOffsetToCategory
% elementsPerRow
;
1028 if (d
->forcedSelectionPosition
< 0)
1029 d
->forcedSelectionPosition
= (d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
;
1031 return d
->elementDictionary
[d
->proxyModel
->index(d
->invertedElementDictionary
[current
].row() - 1, 0)];
1033 case QAbstractItemView::MoveRight
:
1034 d
->forcedSelectionPosition
= d
->elementsInfo
[d
->proxyModel
->mapToSource(d
->elementDictionary
[d
->proxyModel
->index(d
->invertedElementDictionary
[current
].row() + 1, 0)])].relativeOffsetToCategory
% elementsPerRow
;
1036 if (d
->forcedSelectionPosition
< 0)
1037 d
->forcedSelectionPosition
= (d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
;
1039 return d
->elementDictionary
[d
->proxyModel
->index(d
->invertedElementDictionary
[current
].row() + 1, 0)];
1045 return QListView::moveCursor(cursorAction
, modifiers
);
1048 void KCategorizedView::rowsInserted(const QModelIndex
&parent
,
1052 QListView::rowsInserted(parent
, start
, end
);
1054 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
1055 !d
->itemCategorizer
)
1057 d
->lastSelection
= QItemSelection();
1058 d
->currentViewIndex
= QModelIndex();
1059 d
->forcedSelectionPosition
= 0;
1060 d
->elementsInfo
.clear();
1061 d
->elementsPosition
.clear();
1062 d
->elementDictionary
.clear();
1063 d
->invertedElementDictionary
.clear();
1064 d
->categoriesIndexes
.clear();
1065 d
->categoriesPosition
.clear();
1066 d
->categories
.clear();
1067 d
->intersectedIndexes
.clear();
1068 d
->sourceModelIndexList
.clear();
1069 d
->hovered
= QModelIndex();
1070 d
->mouseButtonPressed
= false;
1075 rowsInsertedArtifficial(parent
, start
, end
);
1078 void KCategorizedView::rowsInsertedArtifficial(const QModelIndex
&parent
,
1084 d
->lastSelection
= QItemSelection();
1085 d
->currentViewIndex
= QModelIndex();
1086 d
->forcedSelectionPosition
= 0;
1087 d
->elementsInfo
.clear();
1088 d
->elementsPosition
.clear();
1089 d
->elementDictionary
.clear();
1090 d
->invertedElementDictionary
.clear();
1091 d
->categoriesIndexes
.clear();
1092 d
->categoriesPosition
.clear();
1093 d
->categories
.clear();
1094 d
->intersectedIndexes
.clear();
1095 d
->sourceModelIndexList
.clear();
1096 d
->hovered
= QModelIndex();
1097 d
->mouseButtonPressed
= false;
1099 if (start
> end
|| end
< 0 || start
< 0 || !d
->proxyModel
->rowCount())
1104 // Add all elements mapped to the source model
1105 for (int k
= 0; k
< d
->proxyModel
->rowCount(); k
++)
1107 d
->biggestItemSize
= QSize(qMax(sizeHintForIndex(d
->proxyModel
->index(k
, 0)).width(),
1108 d
->biggestItemSize
.width()),
1109 qMax(sizeHintForIndex(d
->proxyModel
->index(k
, 0)).height(),
1110 d
->biggestItemSize
.height()));
1112 d
->sourceModelIndexList
<<
1113 d
->proxyModel
->mapToSource(d
->proxyModel
->index(k
, 0));
1116 // Sort them with the general purpose lessThan method
1117 LessThan
generalLessThan(d
->proxyModel
,
1118 LessThan::GeneralPurpose
);
1120 qStableSort(d
->sourceModelIndexList
.begin(), d
->sourceModelIndexList
.end(),
1123 // Explore categories
1124 QString prevCategory
=
1125 d
->itemCategorizer
->categoryForItem(d
->sourceModelIndexList
[0],
1126 d
->proxyModel
->sortRole());
1127 QString lastCategory
= prevCategory
;
1128 QModelIndexList modelIndexList
;
1129 struct Private::ElementInfo elementInfo
;
1130 foreach (const QModelIndex
&index
, d
->sourceModelIndexList
)
1132 lastCategory
= d
->itemCategorizer
->categoryForItem(index
,
1133 d
->proxyModel
->sortRole());
1135 elementInfo
.category
= lastCategory
;
1137 if (prevCategory
!= lastCategory
)
1139 d
->categoriesIndexes
.insert(prevCategory
, modelIndexList
);
1140 d
->categories
<< prevCategory
;
1141 modelIndexList
.clear();
1144 modelIndexList
<< index
;
1145 prevCategory
= lastCategory
;
1147 d
->elementsInfo
.insert(index
, elementInfo
);
1150 d
->categoriesIndexes
.insert(prevCategory
, modelIndexList
);
1151 d
->categories
<< prevCategory
;
1153 // Sort items locally in their respective categories with the category
1155 LessThan
categoryLessThan(d
->proxyModel
,
1156 LessThan::CategoryPurpose
);
1158 foreach (const QString
&key
, d
->categories
)
1160 QModelIndexList
&indexList
= d
->categoriesIndexes
[key
];
1162 qStableSort(indexList
.begin(), indexList
.end(), categoryLessThan
);
1165 d
->lastIndex
= d
->categoriesIndexes
[d
->categories
[d
->categories
.count() - 1]][d
->categoriesIndexes
[d
->categories
[d
->categories
.count() - 1]].count() - 1];
1167 // Finally, fill data information of items situation. This will help when
1168 // trying to compute an item place in the viewport
1169 int i
= 0; // position relative to the category beginning
1170 int j
= 0; // number of elements before current
1171 foreach (const QString
&key
, d
->categories
)
1173 foreach (const QModelIndex
&index
, d
->categoriesIndexes
[key
])
1175 struct Private::ElementInfo
&elementInfo
= d
->elementsInfo
[index
];
1177 elementInfo
.relativeOffsetToCategory
= i
;
1179 d
->elementDictionary
.insert(d
->proxyModel
->index(j
, 0),
1180 d
->proxyModel
->mapFromSource(index
));
1182 d
->invertedElementDictionary
.insert(d
->proxyModel
->mapFromSource(index
),
1183 d
->proxyModel
->index(j
, 0));
1192 d
->updateScrollbars();
1195 void KCategorizedView::rowsRemoved(const QModelIndex
&parent
,
1199 if ((viewMode() == KCategorizedView::IconMode
) && d
->proxyModel
&&
1202 // Force the view to update all elements
1203 rowsInsertedArtifficial(parent
, start
, end
);
1207 void KCategorizedView::updateGeometries()
1209 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
1210 !d
->itemCategorizer
)
1212 QListView::updateGeometries();
1216 // Avoid QListView::updateGeometries(), since it will try to set another
1217 // range to our scroll bars, what we don't want (ereslibre)
1218 QAbstractItemView::updateGeometries();
1221 void KCategorizedView::slotSortingRoleChanged()
1223 if ((viewMode() == KCategorizedView::IconMode
) && d
->proxyModel
&&
1226 // Force the view to update all elements
1227 rowsInsertedArtifficial(QModelIndex(), 0, d
->proxyModel
->rowCount() - 1);
1231 #include "kcategorizedview.moc"