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();
103 if (listView
->gridSize().isEmpty())
105 itemHeight
= biggestItemSize
.height();
109 itemHeight
= listView
->gridSize().height();
112 // Lets find out where we should start
113 int top
= proxyModel
->rowCount() - 1;
115 int middle
= (top
+ bottom
) / 2;
116 while (bottom
<= top
)
118 middle
= (top
+ bottom
) / 2;
120 index
= elementDictionary
[proxyModel
->index(middle
, 0)];
121 indexVisualRect
= visualRect(index
);
122 // We need the whole height (not only the visualRect). This will help us to update
123 // all needed indexes correctly (ereslibre)
124 indexVisualRect
.setHeight(indexVisualRect
.height() + (itemHeight
- indexVisualRect
.height()));
126 if (qMax(indexVisualRect
.topLeft().y(),
127 indexVisualRect
.bottomRight().y()) < qMin(rect
.topLeft().y(),
128 rect
.bottomRight().y()))
138 for (int i
= middle
; i
< proxyModel
->rowCount(); i
++)
140 index
= elementDictionary
[proxyModel
->index(i
, 0)];
141 indexVisualRect
= visualRect(index
);
143 if (rect
.intersects(indexVisualRect
))
144 intersectedIndexes
.append(index
);
146 // If we passed next item, stop searching for hits
147 if (qMax(rect
.bottomRight().y(), rect
.topLeft().y()) <
148 qMin(indexVisualRect
.topLeft().y(),
149 indexVisualRect
.bottomRight().y()))
153 return intersectedIndexes
;
156 QRect
KCategorizedView::Private::visualRectInViewport(const QModelIndex
&index
) const
158 if (!index
.isValid())
161 QString curCategory
= elementsInfo
[index
].category
;
163 QRect
retRect(listView
->spacing(), listView
->spacing() * 2 +
164 itemCategorizer
->categoryHeight(listView
->viewOptions()), 0, 0);
166 int viewportWidth
= listView
->viewport()->width() - listView
->spacing();
171 if (listView
->gridSize().isEmpty())
173 itemHeight
= biggestItemSize
.height();
174 itemWidth
= biggestItemSize
.width();
178 itemHeight
= listView
->gridSize().height();
179 itemWidth
= listView
->gridSize().width();
182 int itemWidthPlusSeparation
= listView
->spacing() + itemWidth
;
183 int elementsPerRow
= viewportWidth
/ itemWidthPlusSeparation
;
187 int column
= elementsInfo
[index
].relativeOffsetToCategory
% elementsPerRow
;
188 int row
= elementsInfo
[index
].relativeOffsetToCategory
/ elementsPerRow
;
190 retRect
.setLeft(retRect
.left() + column
* listView
->spacing() +
193 foreach (const QString
&category
, categories
)
195 if (category
== curCategory
)
198 float rows
= (float) ((float) categoriesIndexes
[category
].count() /
199 (float) elementsPerRow
);
200 int rowsInt
= categoriesIndexes
[category
].count() / elementsPerRow
;
202 if (rows
- trunc(rows
)) rowsInt
++;
204 if (listView
->gridSize().isEmpty())
206 retRect
.setTop(retRect
.top() +
207 (rowsInt
* listView
->spacing()) +
208 (rowsInt
* itemHeight
) +
209 itemCategorizer
->categoryHeight(listView
->viewOptions()) +
210 listView
->spacing() * 2);
214 retRect
.setTop(retRect
.top() +
215 (rowsInt
* itemHeight
) +
216 itemCategorizer
->categoryHeight(listView
->viewOptions()) +
217 listView
->spacing());
222 if (listView
->gridSize().isEmpty())
224 retRect
.setTop(retRect
.top() + row
* listView
->spacing() +
229 retRect
.setTop(retRect
.top() + (row
* itemHeight
));
232 retRect
.setWidth(itemWidth
);
234 if (listView
->gridSize().isEmpty())
236 retRect
.setHeight(listView
->sizeHintForIndex(proxyModel
->mapFromSource(index
)).height());
240 retRect
.setHeight(qMin(listView
->sizeHintForIndex(proxyModel
->mapFromSource(index
)).height(),
241 listView
->gridSize().height()));
247 QRect
KCategorizedView::Private::visualCategoryRectInViewport(const QString
&category
)
250 QRect
retRect(listView
->spacing(),
252 listView
->viewport()->width() - listView
->spacing() * 2,
255 if (!proxyModel
->rowCount() || !categories
.contains(category
))
258 QModelIndex index
= proxyModel
->index(0, 0, QModelIndex());
260 int viewportWidth
= listView
->viewport()->width() - listView
->spacing();
265 if (listView
->gridSize().isEmpty())
267 itemHeight
= biggestItemSize
.height();
268 itemWidth
= biggestItemSize
.width();
272 itemHeight
= listView
->gridSize().height();
273 itemWidth
= listView
->gridSize().width();
276 int itemWidthPlusSeparation
= listView
->spacing() + itemWidth
;
277 int elementsPerRow
= viewportWidth
/ itemWidthPlusSeparation
;
282 foreach (const QString
&itCategory
, categories
)
284 if (itCategory
== category
)
287 float rows
= (float) ((float) categoriesIndexes
[itCategory
].count() /
288 (float) elementsPerRow
);
289 int rowsInt
= categoriesIndexes
[itCategory
].count() / elementsPerRow
;
291 if (rows
- trunc(rows
)) rowsInt
++;
293 if (listView
->gridSize().isEmpty())
295 retRect
.setTop(retRect
.top() +
296 (rowsInt
* listView
->spacing()) +
297 (rowsInt
* itemHeight
) +
298 itemCategorizer
->categoryHeight(listView
->viewOptions()) +
299 listView
->spacing() * 2);
303 retRect
.setTop(retRect
.top() +
304 (rowsInt
* itemHeight
) +
305 itemCategorizer
->categoryHeight(listView
->viewOptions()) +
306 listView
->spacing());
310 retRect
.setHeight(itemCategorizer
->categoryHeight(listView
->viewOptions()));
315 // We're sure elementsPosition doesn't contain index
316 const QRect
&KCategorizedView::Private::cacheIndex(const QModelIndex
&index
)
318 QRect rect
= visualRectInViewport(index
);
319 elementsPosition
[index
] = rect
;
321 return elementsPosition
[index
];
324 // We're sure categoriesPosition doesn't contain category
325 const QRect
&KCategorizedView::Private::cacheCategory(const QString
&category
)
327 QRect rect
= visualCategoryRectInViewport(category
);
328 categoriesPosition
[category
] = rect
;
330 return categoriesPosition
[category
];
333 const QRect
&KCategorizedView::Private::cachedRectIndex(const QModelIndex
&index
)
335 if (elementsPosition
.contains(index
)) // If we have it cached
337 return elementsPosition
[index
];
339 else // Otherwise, cache it
341 return cacheIndex(index
);
345 const QRect
&KCategorizedView::Private::cachedRectCategory(const QString
&category
)
347 if (categoriesPosition
.contains(category
)) // If we have it cached
349 return categoriesPosition
[category
];
351 else // Otherwise, cache it and
353 return cacheCategory(category
);
357 QRect
KCategorizedView::Private::visualRect(const QModelIndex
&index
)
359 QModelIndex mappedIndex
= proxyModel
->mapToSource(index
);
361 QRect retRect
= cachedRectIndex(mappedIndex
);
362 int dx
= -listView
->horizontalOffset();
363 int dy
= -listView
->verticalOffset();
364 retRect
.adjust(dx
, dy
, dx
, dy
);
369 QRect
KCategorizedView::Private::categoryVisualRect(const QString
&category
)
371 QRect retRect
= cachedRectCategory(category
);
372 int dx
= -listView
->horizontalOffset();
373 int dy
= -listView
->verticalOffset();
374 retRect
.adjust(dx
, dy
, dx
, dy
);
379 void KCategorizedView::Private::drawNewCategory(const QModelIndex
&index
,
381 const QStyleOption
&option
,
384 QStyleOption optionCopy
= option
;
385 const QString category
= itemCategorizer
->categoryForItem(index
, sortRole
);
387 if ((category
== hoveredCategory
) && !mouseButtonPressed
)
389 optionCopy
.state
|= QStyle::State_MouseOver
;
392 itemCategorizer
->drawCategory(index
,
399 void KCategorizedView::Private::updateScrollbars()
401 int lastItemBottom
= cachedRectIndex(lastIndex
).top() +
402 listView
->spacing() + (listView
->gridSize().isEmpty() ? 0 : listView
->gridSize().height()) - listView
->viewport()->height();
404 listView
->verticalScrollBar()->setSingleStep(listView
->viewport()->height() / 10);
405 listView
->verticalScrollBar()->setPageStep(listView
->viewport()->height());
406 listView
->verticalScrollBar()->setRange(0, lastItemBottom
);
409 void KCategorizedView::Private::drawDraggedItems(QPainter
*painter
)
411 QStyleOptionViewItemV3 option
= listView
->viewOptions();
412 option
.state
&= ~QStyle::State_MouseOver
;
413 foreach (const QModelIndex
&index
, listView
->selectionModel()->selectedIndexes())
415 const int dx
= mousePosition
.x() - initialPressPosition
.x() + listView
->horizontalOffset();
416 const int dy
= mousePosition
.y() - initialPressPosition
.y() + listView
->verticalOffset();
418 option
.rect
= visualRect(index
);
419 option
.rect
.adjust(dx
, dy
, dx
, dy
);
421 if (option
.rect
.intersects(listView
->viewport()->rect()))
423 listView
->itemDelegate(index
)->paint(painter
, option
, index
);
428 void KCategorizedView::Private::drawDraggedItems()
432 foreach (const QModelIndex
&index
, listView
->selectionModel()->selectedIndexes())
434 int dx
= mousePosition
.x() - initialPressPosition
.x() + listView
->horizontalOffset();
435 int dy
= mousePosition
.y() - initialPressPosition
.y() + listView
->verticalOffset();
437 currentRect
= visualRect(index
);
438 currentRect
.adjust(dx
, dy
, dx
, dy
);
440 if (currentRect
.intersects(listView
->viewport()->rect()))
442 rectToUpdate
= rectToUpdate
.united(currentRect
);
446 listView
->viewport()->update(lastDraggedItemsRect
.united(rectToUpdate
));
448 lastDraggedItemsRect
= rectToUpdate
;
452 //==============================================================================
455 KCategorizedView::KCategorizedView(QWidget
*parent
)
457 , d(new Private(this))
461 KCategorizedView::~KCategorizedView()
466 void KCategorizedView::setModel(QAbstractItemModel
*model
)
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;
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()));
494 QListView::setModel(model
);
496 d
->proxyModel
= dynamic_cast<KSortFilterProxyModel
*>(model
);
500 QObject::connect(d
->proxyModel
,
501 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
502 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
504 QObject::connect(d
->proxyModel
,
505 SIGNAL(sortingRoleChanged()),
506 this, SLOT(slotSortingRoleChanged()));
510 QRect
KCategorizedView::visualRect(const QModelIndex
&index
) const
512 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
515 return QListView::visualRect(index
);
518 if (!qobject_cast
<const QSortFilterProxyModel
*>(index
.model()))
520 return d
->visualRect(d
->proxyModel
->mapFromSource(index
));
523 return d
->visualRect(index
);
526 KItemCategorizer
*KCategorizedView::itemCategorizer() const
528 return d
->itemCategorizer
;
531 void KCategorizedView::setItemCategorizer(KItemCategorizer
*itemCategorizer
)
533 d
->lastSelection
= QItemSelection();
534 d
->currentViewIndex
= QModelIndex();
535 d
->forcedSelectionPosition
= 0;
536 d
->elementsInfo
.clear();
537 d
->elementsPosition
.clear();
538 d
->elementDictionary
.clear();
539 d
->invertedElementDictionary
.clear();
540 d
->categoriesIndexes
.clear();
541 d
->categoriesPosition
.clear();
542 d
->categories
.clear();
543 d
->intersectedIndexes
.clear();
544 d
->sourceModelIndexList
.clear();
545 d
->hovered
= QModelIndex();
546 d
->mouseButtonPressed
= false;
548 if (!itemCategorizer
&& d
->proxyModel
)
550 QObject::disconnect(d
->proxyModel
,
551 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
552 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
554 QObject::disconnect(d
->proxyModel
,
555 SIGNAL(sortingRoleChanged()),
556 this, SLOT(slotSortingRoleChanged()));
558 else if (itemCategorizer
&& d
->proxyModel
)
560 QObject::connect(d
->proxyModel
,
561 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
562 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
564 QObject::connect(d
->proxyModel
,
565 SIGNAL(sortingRoleChanged()),
566 this, SLOT(slotSortingRoleChanged()));
569 d
->itemCategorizer
= itemCategorizer
;
573 rowsInserted(QModelIndex(), 0, d
->proxyModel
->rowCount() - 1);
581 QModelIndex
KCategorizedView::indexAt(const QPoint
&point
) const
583 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
586 return QListView::indexAt(point
);
591 QModelIndexList item
= d
->intersectionSet(QRect(point
, point
));
593 if (item
.count() == 1)
603 void KCategorizedView::reset()
607 d
->lastSelection
= QItemSelection();
608 d
->currentViewIndex
= QModelIndex();
609 d
->forcedSelectionPosition
= 0;
610 d
->elementsInfo
.clear();
611 d
->elementsPosition
.clear();
612 d
->elementDictionary
.clear();
613 d
->invertedElementDictionary
.clear();
614 d
->categoriesIndexes
.clear();
615 d
->categoriesPosition
.clear();
616 d
->categories
.clear();
617 d
->intersectedIndexes
.clear();
618 d
->sourceModelIndexList
.clear();
619 d
->hovered
= QModelIndex();
620 d
->biggestItemSize
= QSize(0, 0);
621 d
->mouseButtonPressed
= false;
624 void KCategorizedView::paintEvent(QPaintEvent
*event
)
626 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
629 QListView::paintEvent(event
);
633 QStyleOptionViewItemV3 option
= viewOptions();
634 option
.widget
= this;
635 QPainter
painter(viewport());
636 QRect area
= event
->rect();
637 const bool focus
= (hasFocus() || viewport()->hasFocus()) &&
638 currentIndex().isValid();
639 const QStyle::State state
= option
.state
;
640 const bool enabled
= (state
& QStyle::State_Enabled
) != 0;
644 QModelIndexList dirtyIndexes
= d
->intersectionSet(area
);
645 foreach (const QModelIndex
&index
, dirtyIndexes
)
647 option
.state
= state
;
648 option
.rect
= d
->visualRect(index
);
650 if (selectionModel() && selectionModel()->isSelected(index
))
652 option
.state
|= QStyle::State_Selected
;
657 QPalette::ColorGroup cg
;
658 if ((d
->proxyModel
->flags(index
) & Qt::ItemIsEnabled
) == 0)
660 option
.state
&= ~QStyle::State_Enabled
;
661 cg
= QPalette::Disabled
;
665 cg
= QPalette::Normal
;
667 option
.palette
.setCurrentColorGroup(cg
);
670 if (focus
&& currentIndex() == index
)
672 option
.state
|= QStyle::State_HasFocus
;
673 if (this->state() == EditingState
)
674 option
.state
|= QStyle::State_Editing
;
677 if ((index
== d
->hovered
) && !d
->mouseButtonPressed
)
678 option
.state
|= QStyle::State_MouseOver
;
680 option
.state
&= ~QStyle::State_MouseOver
;
682 itemDelegate(index
)->paint(&painter
, option
, index
);
686 QStyleOptionViewItem otherOption
;
687 foreach (const QString
&category
, d
->categories
)
689 otherOption
= option
;
690 otherOption
.rect
= d
->categoryVisualRect(category
);
691 otherOption
.state
&= ~QStyle::State_MouseOver
;
693 if (otherOption
.rect
.intersects(area
))
695 d
->drawNewCategory(d
->categoriesIndexes
[category
][0],
696 d
->proxyModel
->sortRole(), otherOption
, &painter
);
700 if (d
->mouseButtonPressed
&& !d
->isDragging
)
702 QPoint start
, end
, initialPressPosition
;
704 initialPressPosition
= d
->initialPressPosition
;
706 initialPressPosition
.setY(initialPressPosition
.y() - verticalOffset());
707 initialPressPosition
.setX(initialPressPosition
.x() - horizontalOffset());
709 if (d
->initialPressPosition
.x() > d
->mousePosition
.x() ||
710 d
->initialPressPosition
.y() > d
->mousePosition
.y())
712 start
= d
->mousePosition
;
713 end
= initialPressPosition
;
717 start
= initialPressPosition
;
718 end
= d
->mousePosition
;
721 QStyleOptionRubberBand yetAnotherOption
;
722 yetAnotherOption
.initFrom(this);
723 yetAnotherOption
.shape
= QRubberBand::Rectangle
;
724 yetAnotherOption
.opaque
= false;
725 yetAnotherOption
.rect
= QRect(start
, end
).intersected(viewport()->rect().adjusted(-16, -16, 16, 16));
727 style()->drawControl(QStyle::CE_RubberBand
, &yetAnotherOption
, &painter
);
731 if (d
->isDragging
&& !d
->dragLeftViewport
)
733 painter
.setOpacity(0.5);
734 d
->drawDraggedItems(&painter
);
740 void KCategorizedView::resizeEvent(QResizeEvent
*event
)
742 QListView::resizeEvent(event
);
744 // Clear the items positions cache
745 d
->elementsPosition
.clear();
746 d
->categoriesPosition
.clear();
747 d
->forcedSelectionPosition
= 0;
749 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
755 d
->updateScrollbars();
758 void KCategorizedView::setSelection(const QRect
&rect
,
759 QItemSelectionModel::SelectionFlags flags
)
761 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
764 QListView::setSelection(rect
, flags
);
771 selectionModel()->clear();
773 if (flags
& QItemSelectionModel::Clear
)
775 d
->lastSelection
= QItemSelection();
778 QModelIndexList dirtyIndexes
= d
->intersectionSet(rect
);
780 QItemSelection selection
;
782 if (!dirtyIndexes
.count())
784 if (d
->lastSelection
.count())
786 selectionModel()->select(d
->lastSelection
, flags
);
792 if (!d
->mouseButtonPressed
)
794 selection
= QItemSelection(dirtyIndexes
[0], dirtyIndexes
[0]);
795 d
->currentViewIndex
= dirtyIndexes
[0];
799 QModelIndex first
= dirtyIndexes
[0];
801 foreach (const QModelIndex
&index
, dirtyIndexes
)
803 if (last
.isValid() && last
.row() + 1 != index
.row())
805 QItemSelectionRange
range(first
, last
);
816 selection
<< QItemSelectionRange(first
, last
);
819 if (d
->lastSelection
.count() && !d
->mouseButtonPressed
)
821 selection
.merge(d
->lastSelection
, flags
);
823 else if (d
->lastSelection
.count())
825 selection
.merge(d
->lastSelection
, QItemSelectionModel::Select
);
828 selectionModel()->select(selection
, flags
);
831 void KCategorizedView::mouseMoveEvent(QMouseEvent
*event
)
833 QListView::mouseMoveEvent(event
);
835 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
841 const QString previousHoveredCategory
= d
->hoveredCategory
;
843 d
->mousePosition
= event
->pos();
844 d
->hoveredCategory
= QString();
847 foreach (const QString
&category
, d
->categories
)
849 if (d
->categoryVisualRect(category
).intersects(QRect(event
->pos(), event
->pos())))
851 d
->hoveredCategory
= category
;
852 viewport()->update(d
->categoryVisualRect(category
));
854 else if ((category
== previousHoveredCategory
) &&
855 (!d
->categoryVisualRect(previousHoveredCategory
).intersects(QRect(event
->pos(), event
->pos()))))
857 viewport()->update(d
->categoryVisualRect(category
));
862 if (d
->mouseButtonPressed
&& !d
->isDragging
)
864 QPoint start
, end
, initialPressPosition
;
866 initialPressPosition
= d
->initialPressPosition
;
868 initialPressPosition
.setY(initialPressPosition
.y() - verticalOffset());
869 initialPressPosition
.setX(initialPressPosition
.x() - horizontalOffset());
871 if (d
->initialPressPosition
.x() > d
->mousePosition
.x() ||
872 d
->initialPressPosition
.y() > d
->mousePosition
.y())
874 start
= d
->mousePosition
;
875 end
= initialPressPosition
;
879 start
= initialPressPosition
;
880 end
= d
->mousePosition
;
883 rect
= QRect(start
, end
).intersected(viewport()->rect().adjusted(-16, -16, 16, 16));
885 //viewport()->update(rect.united(d->lastSelectionRect));
887 d
->lastSelectionRect
= rect
;
891 void KCategorizedView::mousePressEvent(QMouseEvent
*event
)
893 d
->dragLeftViewport
= false;
895 if (event
->button() == Qt::LeftButton
)
897 d
->mouseButtonPressed
= true;
899 d
->initialPressPosition
= event
->pos();
900 d
->initialPressPosition
.setY(d
->initialPressPosition
.y() +
902 d
->initialPressPosition
.setX(d
->initialPressPosition
.x() +
906 QListView::mousePressEvent(event
);
909 void KCategorizedView::mouseReleaseEvent(QMouseEvent
*event
)
911 d
->mouseButtonPressed
= false;
913 QListView::mouseReleaseEvent(event
);
915 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
921 QPoint initialPressPosition
= viewport()->mapFromGlobal(QCursor::pos());
922 initialPressPosition
.setY(initialPressPosition
.y() + verticalOffset());
923 initialPressPosition
.setX(initialPressPosition
.x() + horizontalOffset());
925 QItemSelection selection
;
927 if (initialPressPosition
== d
->initialPressPosition
)
929 foreach(const QString
&category
, d
->categories
)
931 if (d
->categoryVisualRect(category
).contains(event
->pos()))
933 foreach (const QModelIndex
&index
, d
->categoriesIndexes
[category
])
935 selection
<< QItemSelectionRange(d
->proxyModel
->mapFromSource(index
));
938 selectionModel()->select(selection
, QItemSelectionModel::Select
);
945 d
->lastSelection
= selectionModel()->selection();
947 if (d
->hovered
.isValid())
948 viewport()->update(d
->visualRect(d
->hovered
));
949 else if (!d
->hoveredCategory
.isEmpty())
950 viewport()->update(d
->categoryVisualRect(d
->hoveredCategory
));
953 void KCategorizedView::leaveEvent(QEvent
*event
)
955 d
->hovered
= QModelIndex();
956 d
->hoveredCategory
= QString();
958 QListView::leaveEvent(event
);
961 void KCategorizedView::startDrag(Qt::DropActions supportedActions
)
963 QListView::startDrag(supportedActions
);
965 d
->isDragging
= false;
966 d
->mouseButtonPressed
= false;
968 viewport()->update(d
->lastDraggedItemsRect
);
971 void KCategorizedView::dragMoveEvent(QDragMoveEvent
*event
)
973 d
->mousePosition
= event
->pos();
975 if (d
->mouseButtonPressed
)
977 d
->isDragging
= true;
981 d
->isDragging
= false;
984 d
->dragLeftViewport
= false;
986 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
989 QListView::dragMoveEvent(event
);
993 d
->drawDraggedItems();
996 void KCategorizedView::dragLeaveEvent(QDragLeaveEvent
*event
)
998 d
->dragLeftViewport
= true;
1000 QListView::dragLeaveEvent(event
);
1003 QModelIndex
KCategorizedView::moveCursor(CursorAction cursorAction
,
1004 Qt::KeyboardModifiers modifiers
)
1006 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
1007 !d
->itemCategorizer
)
1009 return QListView::moveCursor(cursorAction
, modifiers
);
1012 const QModelIndex current
= selectionModel()->currentIndex();
1014 int viewportWidth
= viewport()->width() - spacing();
1017 if (gridSize().isEmpty())
1019 itemWidth
= d
->biggestItemSize
.width();
1023 itemWidth
= gridSize().width();
1026 int itemWidthPlusSeparation
= spacing() + itemWidth
;
1027 int elementsPerRow
= viewportWidth
/ itemWidthPlusSeparation
;
1029 QString lastCategory
= d
->categories
[0];
1030 QString theCategory
= d
->categories
[0];
1031 QString afterCategory
= d
->categories
[0];
1032 bool hasToBreak
= false;
1033 foreach (const QString
&category
, d
->categories
)
1037 afterCategory
= category
;
1042 if (category
== d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].category
)
1044 theCategory
= category
;
1051 lastCategory
= category
;
1055 switch (cursorAction
)
1057 case QAbstractItemView::MoveUp
: {
1058 if (d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].relativeOffsetToCategory
>= elementsPerRow
)
1060 int indexToMove
= d
->invertedElementDictionary
[current
].row();
1061 indexToMove
-= qMin(((d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].relativeOffsetToCategory
) + d
->forcedSelectionPosition
), elementsPerRow
- d
->forcedSelectionPosition
+ (d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].relativeOffsetToCategory
% elementsPerRow
));
1063 return d
->elementDictionary
[d
->proxyModel
->index(indexToMove
, 0)];
1067 int lastCategoryLastRow
= (d
->categoriesIndexes
[lastCategory
].count() - 1) % elementsPerRow
;
1068 int indexToMove
= d
->invertedElementDictionary
[current
].row() - d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].relativeOffsetToCategory
;
1070 if (d
->forcedSelectionPosition
>= lastCategoryLastRow
)
1076 indexToMove
-= qMin((lastCategoryLastRow
- d
->forcedSelectionPosition
+ 1), d
->forcedSelectionPosition
+ elementsPerRow
+ 1);
1079 return d
->elementDictionary
[d
->proxyModel
->index(indexToMove
, 0)];
1083 case QAbstractItemView::MoveDown
: {
1084 if (d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].relativeOffsetToCategory
< (d
->categoriesIndexes
[theCategory
].count() - 1 - ((d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
)))
1086 int indexToMove
= d
->invertedElementDictionary
[current
].row();
1087 indexToMove
+= qMin(elementsPerRow
, d
->categoriesIndexes
[theCategory
].count() - 1 - d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].relativeOffsetToCategory
);
1089 return d
->elementDictionary
[d
->proxyModel
->index(indexToMove
, 0)];
1093 int afterCategoryLastRow
= qMin(elementsPerRow
, d
->categoriesIndexes
[afterCategory
].count());
1094 int indexToMove
= d
->invertedElementDictionary
[current
].row() + (d
->categoriesIndexes
[theCategory
].count() - d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].relativeOffsetToCategory
);
1096 if (d
->forcedSelectionPosition
>= afterCategoryLastRow
)
1098 indexToMove
+= afterCategoryLastRow
- 1;
1102 indexToMove
+= qMin(d
->forcedSelectionPosition
, elementsPerRow
);
1105 return d
->elementDictionary
[d
->proxyModel
->index(indexToMove
, 0)];
1109 case QAbstractItemView::MoveLeft
:
1110 d
->forcedSelectionPosition
= d
->elementsInfo
[d
->proxyModel
->mapToSource(d
->elementDictionary
[d
->proxyModel
->index(d
->invertedElementDictionary
[current
].row() - 1, 0)])].relativeOffsetToCategory
% elementsPerRow
;
1112 if (d
->forcedSelectionPosition
< 0)
1113 d
->forcedSelectionPosition
= (d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
;
1115 return d
->elementDictionary
[d
->proxyModel
->index(d
->invertedElementDictionary
[current
].row() - 1, 0)];
1117 case QAbstractItemView::MoveRight
:
1118 d
->forcedSelectionPosition
= d
->elementsInfo
[d
->proxyModel
->mapToSource(d
->elementDictionary
[d
->proxyModel
->index(d
->invertedElementDictionary
[current
].row() + 1, 0)])].relativeOffsetToCategory
% elementsPerRow
;
1120 if (d
->forcedSelectionPosition
< 0)
1121 d
->forcedSelectionPosition
= (d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
;
1123 return d
->elementDictionary
[d
->proxyModel
->index(d
->invertedElementDictionary
[current
].row() + 1, 0)];
1129 return QListView::moveCursor(cursorAction
, modifiers
);
1132 void KCategorizedView::rowsInserted(const QModelIndex
&parent
,
1136 QListView::rowsInserted(parent
, start
, end
);
1138 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
1139 !d
->itemCategorizer
)
1141 d
->lastSelection
= QItemSelection();
1142 d
->currentViewIndex
= QModelIndex();
1143 d
->forcedSelectionPosition
= 0;
1144 d
->elementsInfo
.clear();
1145 d
->elementsPosition
.clear();
1146 d
->elementDictionary
.clear();
1147 d
->invertedElementDictionary
.clear();
1148 d
->categoriesIndexes
.clear();
1149 d
->categoriesPosition
.clear();
1150 d
->categories
.clear();
1151 d
->intersectedIndexes
.clear();
1152 d
->sourceModelIndexList
.clear();
1153 d
->hovered
= QModelIndex();
1154 d
->biggestItemSize
= QSize(0, 0);
1155 d
->mouseButtonPressed
= false;
1160 rowsInsertedArtifficial(parent
, start
, end
);
1163 void KCategorizedView::rowsInsertedArtifficial(const QModelIndex
&parent
,
1169 d
->lastSelection
= QItemSelection();
1170 d
->currentViewIndex
= QModelIndex();
1171 d
->forcedSelectionPosition
= 0;
1172 d
->elementsInfo
.clear();
1173 d
->elementsPosition
.clear();
1174 d
->elementDictionary
.clear();
1175 d
->invertedElementDictionary
.clear();
1176 d
->categoriesIndexes
.clear();
1177 d
->categoriesPosition
.clear();
1178 d
->categories
.clear();
1179 d
->intersectedIndexes
.clear();
1180 d
->sourceModelIndexList
.clear();
1181 d
->hovered
= QModelIndex();
1182 d
->biggestItemSize
= QSize(0, 0);
1183 d
->mouseButtonPressed
= false;
1185 if (start
> end
|| end
< 0 || start
< 0 || !d
->proxyModel
->rowCount())
1190 // Add all elements mapped to the source model
1191 for (int k
= 0; k
< d
->proxyModel
->rowCount(); k
++)
1193 d
->biggestItemSize
= QSize(qMax(sizeHintForIndex(d
->proxyModel
->index(k
, 0)).width(),
1194 d
->biggestItemSize
.width()),
1195 qMax(sizeHintForIndex(d
->proxyModel
->index(k
, 0)).height(),
1196 d
->biggestItemSize
.height()));
1198 d
->sourceModelIndexList
<<
1199 d
->proxyModel
->mapToSource(d
->proxyModel
->index(k
, 0));
1202 // Sort them with the general purpose lessThan method
1203 LessThan
generalLessThan(d
->proxyModel
,
1204 LessThan::GeneralPurpose
);
1206 qStableSort(d
->sourceModelIndexList
.begin(), d
->sourceModelIndexList
.end(),
1209 // Explore categories
1210 QString prevCategory
=
1211 d
->itemCategorizer
->categoryForItem(d
->sourceModelIndexList
[0],
1212 d
->proxyModel
->sortRole());
1213 QString lastCategory
= prevCategory
;
1214 QModelIndexList modelIndexList
;
1215 struct Private::ElementInfo elementInfo
;
1216 foreach (const QModelIndex
&index
, d
->sourceModelIndexList
)
1218 lastCategory
= d
->itemCategorizer
->categoryForItem(index
,
1219 d
->proxyModel
->sortRole());
1221 elementInfo
.category
= lastCategory
;
1223 if (prevCategory
!= lastCategory
)
1225 d
->categoriesIndexes
.insert(prevCategory
, modelIndexList
);
1226 d
->categories
<< prevCategory
;
1227 modelIndexList
.clear();
1230 modelIndexList
<< index
;
1231 prevCategory
= lastCategory
;
1233 d
->elementsInfo
.insert(index
, elementInfo
);
1236 d
->categoriesIndexes
.insert(prevCategory
, modelIndexList
);
1237 d
->categories
<< prevCategory
;
1239 // Sort items locally in their respective categories with the category
1241 LessThan
categoryLessThan(d
->proxyModel
,
1242 LessThan::CategoryPurpose
);
1244 foreach (const QString
&key
, d
->categories
)
1246 QModelIndexList
&indexList
= d
->categoriesIndexes
[key
];
1248 qStableSort(indexList
.begin(), indexList
.end(), categoryLessThan
);
1251 d
->lastIndex
= d
->categoriesIndexes
[d
->categories
[d
->categories
.count() - 1]][d
->categoriesIndexes
[d
->categories
[d
->categories
.count() - 1]].count() - 1];
1253 // Finally, fill data information of items situation. This will help when
1254 // trying to compute an item place in the viewport
1255 int i
= 0; // position relative to the category beginning
1256 int j
= 0; // number of elements before current
1257 foreach (const QString
&key
, d
->categories
)
1259 foreach (const QModelIndex
&index
, d
->categoriesIndexes
[key
])
1261 struct Private::ElementInfo
&elementInfo
= d
->elementsInfo
[index
];
1263 elementInfo
.relativeOffsetToCategory
= i
;
1265 d
->elementDictionary
.insert(d
->proxyModel
->index(j
, 0),
1266 d
->proxyModel
->mapFromSource(index
));
1268 d
->invertedElementDictionary
.insert(d
->proxyModel
->mapFromSource(index
),
1269 d
->proxyModel
->index(j
, 0));
1278 d
->updateScrollbars();
1281 void KCategorizedView::rowsRemoved(const QModelIndex
&parent
,
1285 if ((viewMode() == KCategorizedView::IconMode
) && d
->proxyModel
&&
1288 // Force the view to update all elements
1289 rowsInsertedArtifficial(parent
, start
, end
);
1293 void KCategorizedView::updateGeometries()
1295 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
1296 !d
->itemCategorizer
)
1298 QListView::updateGeometries();
1302 // Avoid QListView::updateGeometries(), since it will try to set another
1303 // range to our scroll bars, what we don't want (ereslibre)
1304 QAbstractItemView::updateGeometries();
1307 void KCategorizedView::slotSortingRoleChanged()
1309 if ((viewMode() == KCategorizedView::IconMode
) && d
->proxyModel
&&
1312 // Force the view to update all elements
1313 rowsInsertedArtifficial(QModelIndex(), 0, d
->proxyModel
->rowCount() - 1);
1317 #include "kcategorizedview.moc"