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 retRect
.setTop(retRect
.top() +
205 (rowsInt
* itemHeight
) +
206 itemCategorizer
->categoryHeight(listView
->viewOptions()) +
207 listView
->spacing() * 2);
209 if (listView
->gridSize().isEmpty())
211 retRect
.setTop(retRect
.top() +
212 (rowsInt
* listView
->spacing()));
217 if (listView
->gridSize().isEmpty())
219 retRect
.setTop(retRect
.top() + row
* listView
->spacing() +
224 retRect
.setTop(retRect
.top() + (row
* itemHeight
));
227 retRect
.setWidth(itemWidth
);
229 if (listView
->gridSize().isEmpty())
231 retRect
.setHeight(listView
->sizeHintForIndex(proxyModel
->mapFromSource(index
)).height());
235 retRect
.setHeight(qMin(listView
->sizeHintForIndex(proxyModel
->mapFromSource(index
)).height(),
236 listView
->gridSize().height()));
242 QRect
KCategorizedView::Private::visualCategoryRectInViewport(const QString
&category
)
245 QRect
retRect(listView
->spacing(),
247 listView
->viewport()->width() - listView
->spacing() * 2,
250 if (!proxyModel
->rowCount() || !categories
.contains(category
))
253 QModelIndex index
= proxyModel
->index(0, 0, QModelIndex());
255 int viewportWidth
= listView
->viewport()->width() - listView
->spacing();
260 if (listView
->gridSize().isEmpty())
262 itemHeight
= biggestItemSize
.height();
263 itemWidth
= biggestItemSize
.width();
267 itemHeight
= listView
->gridSize().height();
268 itemWidth
= listView
->gridSize().width();
271 int itemWidthPlusSeparation
= listView
->spacing() + itemWidth
;
272 int elementsPerRow
= viewportWidth
/ itemWidthPlusSeparation
;
277 foreach (const QString
&itCategory
, categories
)
279 if (itCategory
== category
)
282 float rows
= (float) ((float) categoriesIndexes
[itCategory
].count() /
283 (float) elementsPerRow
);
284 int rowsInt
= categoriesIndexes
[itCategory
].count() / elementsPerRow
;
286 if (rows
- trunc(rows
)) rowsInt
++;
288 retRect
.setTop(retRect
.top() +
289 (rowsInt
* itemHeight
) +
290 itemCategorizer
->categoryHeight(listView
->viewOptions()) +
291 listView
->spacing() * 2);
293 if (listView
->gridSize().isEmpty())
295 retRect
.setTop(retRect
.top() +
296 (rowsInt
* listView
->spacing()));
300 retRect
.setHeight(itemCategorizer
->categoryHeight(listView
->viewOptions()));
305 // We're sure elementsPosition doesn't contain index
306 const QRect
&KCategorizedView::Private::cacheIndex(const QModelIndex
&index
)
308 QRect rect
= visualRectInViewport(index
);
309 elementsPosition
[index
] = rect
;
311 return elementsPosition
[index
];
314 // We're sure categoriesPosition doesn't contain category
315 const QRect
&KCategorizedView::Private::cacheCategory(const QString
&category
)
317 QRect rect
= visualCategoryRectInViewport(category
);
318 categoriesPosition
[category
] = rect
;
320 return categoriesPosition
[category
];
323 const QRect
&KCategorizedView::Private::cachedRectIndex(const QModelIndex
&index
)
325 if (elementsPosition
.contains(index
)) // If we have it cached
327 return elementsPosition
[index
];
329 else // Otherwise, cache it
331 return cacheIndex(index
);
335 const QRect
&KCategorizedView::Private::cachedRectCategory(const QString
&category
)
337 if (categoriesPosition
.contains(category
)) // If we have it cached
339 return categoriesPosition
[category
];
341 else // Otherwise, cache it and
343 return cacheCategory(category
);
347 QRect
KCategorizedView::Private::visualRect(const QModelIndex
&index
)
349 QModelIndex mappedIndex
= proxyModel
->mapToSource(index
);
351 QRect retRect
= cachedRectIndex(mappedIndex
);
352 int dx
= -listView
->horizontalOffset();
353 int dy
= -listView
->verticalOffset();
354 retRect
.adjust(dx
, dy
, dx
, dy
);
359 QRect
KCategorizedView::Private::categoryVisualRect(const QString
&category
)
361 QRect retRect
= cachedRectCategory(category
);
362 int dx
= -listView
->horizontalOffset();
363 int dy
= -listView
->verticalOffset();
364 retRect
.adjust(dx
, dy
, dx
, dy
);
369 void KCategorizedView::Private::drawNewCategory(const QModelIndex
&index
,
371 const QStyleOption
&option
,
374 QStyleOption optionCopy
= option
;
375 const QString category
= itemCategorizer
->categoryForItem(index
, sortRole
);
377 if ((category
== hoveredCategory
) && !mouseButtonPressed
)
379 optionCopy
.state
|= QStyle::State_MouseOver
;
382 itemCategorizer
->drawCategory(index
,
389 void KCategorizedView::Private::updateScrollbars()
391 int lastItemBottom
= cachedRectIndex(lastIndex
).top() +
392 listView
->spacing() + (listView
->gridSize().isEmpty() ? 0 : listView
->gridSize().height()) - listView
->viewport()->height();
394 listView
->verticalScrollBar()->setSingleStep(listView
->viewport()->height() / 10);
395 listView
->verticalScrollBar()->setPageStep(listView
->viewport()->height());
396 listView
->verticalScrollBar()->setRange(0, lastItemBottom
);
399 void KCategorizedView::Private::drawDraggedItems(QPainter
*painter
)
401 QStyleOptionViewItemV3 option
= listView
->viewOptions();
402 option
.state
&= ~QStyle::State_MouseOver
;
403 foreach (const QModelIndex
&index
, listView
->selectionModel()->selectedIndexes())
405 const int dx
= mousePosition
.x() - initialPressPosition
.x() + listView
->horizontalOffset();
406 const int dy
= mousePosition
.y() - initialPressPosition
.y() + listView
->verticalOffset();
408 option
.rect
= visualRect(index
);
409 option
.rect
.adjust(dx
, dy
, dx
, dy
);
411 if (option
.rect
.intersects(listView
->viewport()->rect()))
413 listView
->itemDelegate(index
)->paint(painter
, option
, index
);
418 void KCategorizedView::Private::drawDraggedItems()
422 foreach (const QModelIndex
&index
, listView
->selectionModel()->selectedIndexes())
424 int dx
= mousePosition
.x() - initialPressPosition
.x() + listView
->horizontalOffset();
425 int dy
= mousePosition
.y() - initialPressPosition
.y() + listView
->verticalOffset();
427 currentRect
= visualRect(index
);
428 currentRect
.adjust(dx
, dy
, dx
, dy
);
430 if (currentRect
.intersects(listView
->viewport()->rect()))
432 rectToUpdate
= rectToUpdate
.united(currentRect
);
436 listView
->viewport()->update(lastDraggedItemsRect
.united(rectToUpdate
));
438 lastDraggedItemsRect
= rectToUpdate
;
442 //==============================================================================
445 KCategorizedView::KCategorizedView(QWidget
*parent
)
447 , d(new Private(this))
451 KCategorizedView::~KCategorizedView()
456 void KCategorizedView::setModel(QAbstractItemModel
*model
)
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;
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()));
484 QListView::setModel(model
);
486 d
->proxyModel
= dynamic_cast<KSortFilterProxyModel
*>(model
);
490 QObject::connect(d
->proxyModel
,
491 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
492 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
494 QObject::connect(d
->proxyModel
,
495 SIGNAL(sortingRoleChanged()),
496 this, SLOT(slotSortingRoleChanged()));
500 QRect
KCategorizedView::visualRect(const QModelIndex
&index
) const
502 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
505 return QListView::visualRect(index
);
508 if (!qobject_cast
<const QSortFilterProxyModel
*>(index
.model()))
510 return d
->visualRect(d
->proxyModel
->mapFromSource(index
));
513 return d
->visualRect(index
);
516 KItemCategorizer
*KCategorizedView::itemCategorizer() const
518 return d
->itemCategorizer
;
521 void KCategorizedView::setItemCategorizer(KItemCategorizer
*itemCategorizer
)
523 d
->lastSelection
= QItemSelection();
524 d
->currentViewIndex
= QModelIndex();
525 d
->forcedSelectionPosition
= 0;
526 d
->elementsInfo
.clear();
527 d
->elementsPosition
.clear();
528 d
->elementDictionary
.clear();
529 d
->invertedElementDictionary
.clear();
530 d
->categoriesIndexes
.clear();
531 d
->categoriesPosition
.clear();
532 d
->categories
.clear();
533 d
->intersectedIndexes
.clear();
534 d
->sourceModelIndexList
.clear();
535 d
->hovered
= QModelIndex();
536 d
->mouseButtonPressed
= false;
538 if (!itemCategorizer
&& d
->proxyModel
)
540 QObject::disconnect(d
->proxyModel
,
541 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
542 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
544 QObject::disconnect(d
->proxyModel
,
545 SIGNAL(sortingRoleChanged()),
546 this, SLOT(slotSortingRoleChanged()));
548 else if (itemCategorizer
&& d
->proxyModel
)
550 QObject::connect(d
->proxyModel
,
551 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
552 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
554 QObject::connect(d
->proxyModel
,
555 SIGNAL(sortingRoleChanged()),
556 this, SLOT(slotSortingRoleChanged()));
559 d
->itemCategorizer
= itemCategorizer
;
563 rowsInserted(QModelIndex(), 0, d
->proxyModel
->rowCount() - 1);
571 QModelIndex
KCategorizedView::indexAt(const QPoint
&point
) const
573 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
576 return QListView::indexAt(point
);
581 QModelIndexList item
= d
->intersectionSet(QRect(point
, point
));
583 if (item
.count() == 1)
593 void KCategorizedView::reset()
597 d
->lastSelection
= QItemSelection();
598 d
->currentViewIndex
= QModelIndex();
599 d
->forcedSelectionPosition
= 0;
600 d
->elementsInfo
.clear();
601 d
->elementsPosition
.clear();
602 d
->elementDictionary
.clear();
603 d
->invertedElementDictionary
.clear();
604 d
->categoriesIndexes
.clear();
605 d
->categoriesPosition
.clear();
606 d
->categories
.clear();
607 d
->intersectedIndexes
.clear();
608 d
->sourceModelIndexList
.clear();
609 d
->hovered
= QModelIndex();
610 d
->biggestItemSize
= QSize(0, 0);
611 d
->mouseButtonPressed
= false;
614 void KCategorizedView::paintEvent(QPaintEvent
*event
)
616 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
619 QListView::paintEvent(event
);
623 QStyleOptionViewItemV3 option
= viewOptions();
624 option
.widget
= this;
627 option
.features
|= QStyleOptionViewItemV2::WrapText
;
630 QPainter
painter(viewport());
631 QRect area
= event
->rect();
632 const bool focus
= (hasFocus() || viewport()->hasFocus()) &&
633 currentIndex().isValid();
634 const QStyle::State state
= option
.state
;
635 const bool enabled
= (state
& QStyle::State_Enabled
) != 0;
639 QModelIndexList dirtyIndexes
= d
->intersectionSet(area
);
640 foreach (const QModelIndex
&index
, dirtyIndexes
)
642 option
.state
= state
;
643 option
.rect
= visualRect(index
);
645 if (selectionModel() && selectionModel()->isSelected(index
))
647 option
.state
|= QStyle::State_Selected
;
652 QPalette::ColorGroup cg
;
653 if ((d
->proxyModel
->flags(index
) & Qt::ItemIsEnabled
) == 0)
655 option
.state
&= ~QStyle::State_Enabled
;
656 cg
= QPalette::Disabled
;
660 cg
= QPalette::Normal
;
662 option
.palette
.setCurrentColorGroup(cg
);
665 if (focus
&& currentIndex() == index
)
667 option
.state
|= QStyle::State_HasFocus
;
668 if (this->state() == EditingState
)
669 option
.state
|= QStyle::State_Editing
;
672 if ((index
== d
->hovered
) && !d
->mouseButtonPressed
)
673 option
.state
|= QStyle::State_MouseOver
;
675 option
.state
&= ~QStyle::State_MouseOver
;
677 itemDelegate(index
)->paint(&painter
, option
, index
);
681 QStyleOptionViewItem otherOption
;
682 foreach (const QString
&category
, d
->categories
)
684 otherOption
= option
;
685 otherOption
.rect
= d
->categoryVisualRect(category
);
686 otherOption
.state
&= ~QStyle::State_MouseOver
;
688 if (otherOption
.rect
.intersects(area
))
690 d
->drawNewCategory(d
->categoriesIndexes
[category
][0],
691 d
->proxyModel
->sortRole(), otherOption
, &painter
);
695 if (d
->mouseButtonPressed
&& !d
->isDragging
)
697 QPoint start
, end
, initialPressPosition
;
699 initialPressPosition
= d
->initialPressPosition
;
701 initialPressPosition
.setY(initialPressPosition
.y() - verticalOffset());
702 initialPressPosition
.setX(initialPressPosition
.x() - horizontalOffset());
704 if (d
->initialPressPosition
.x() > d
->mousePosition
.x() ||
705 d
->initialPressPosition
.y() > d
->mousePosition
.y())
707 start
= d
->mousePosition
;
708 end
= initialPressPosition
;
712 start
= initialPressPosition
;
713 end
= d
->mousePosition
;
716 QStyleOptionRubberBand yetAnotherOption
;
717 yetAnotherOption
.initFrom(this);
718 yetAnotherOption
.shape
= QRubberBand::Rectangle
;
719 yetAnotherOption
.opaque
= false;
720 yetAnotherOption
.rect
= QRect(start
, end
).intersected(viewport()->rect().adjusted(-16, -16, 16, 16));
722 style()->drawControl(QStyle::CE_RubberBand
, &yetAnotherOption
, &painter
);
726 if (d
->isDragging
&& !d
->dragLeftViewport
)
728 painter
.setOpacity(0.5);
729 d
->drawDraggedItems(&painter
);
735 void KCategorizedView::resizeEvent(QResizeEvent
*event
)
737 QListView::resizeEvent(event
);
739 // Clear the items positions cache
740 d
->elementsPosition
.clear();
741 d
->categoriesPosition
.clear();
742 d
->forcedSelectionPosition
= 0;
744 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
750 d
->updateScrollbars();
753 void KCategorizedView::setSelection(const QRect
&rect
,
754 QItemSelectionModel::SelectionFlags flags
)
756 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
759 QListView::setSelection(rect
, flags
);
766 selectionModel()->clear();
768 if (flags
& QItemSelectionModel::Clear
)
770 d
->lastSelection
= QItemSelection();
773 QModelIndexList dirtyIndexes
= d
->intersectionSet(rect
);
775 QItemSelection selection
;
777 if (!dirtyIndexes
.count())
779 if (d
->lastSelection
.count())
781 selectionModel()->select(d
->lastSelection
, flags
);
787 if (!d
->mouseButtonPressed
)
789 selection
= QItemSelection(dirtyIndexes
[0], dirtyIndexes
[0]);
790 d
->currentViewIndex
= dirtyIndexes
[0];
794 QModelIndex first
= dirtyIndexes
[0];
796 foreach (const QModelIndex
&index
, dirtyIndexes
)
798 if (last
.isValid() && last
.row() + 1 != index
.row())
800 QItemSelectionRange
range(first
, last
);
811 selection
<< QItemSelectionRange(first
, last
);
814 if (d
->lastSelection
.count() && !d
->mouseButtonPressed
)
816 selection
.merge(d
->lastSelection
, flags
);
818 else if (d
->lastSelection
.count())
820 selection
.merge(d
->lastSelection
, QItemSelectionModel::Select
);
823 selectionModel()->select(selection
, flags
);
826 void KCategorizedView::mouseMoveEvent(QMouseEvent
*event
)
828 QListView::mouseMoveEvent(event
);
830 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
836 const QString previousHoveredCategory
= d
->hoveredCategory
;
838 d
->mousePosition
= event
->pos();
839 d
->hoveredCategory
= QString();
842 foreach (const QString
&category
, d
->categories
)
844 if (d
->categoryVisualRect(category
).intersects(QRect(event
->pos(), event
->pos())))
846 d
->hoveredCategory
= category
;
847 viewport()->update(d
->categoryVisualRect(category
));
849 else if ((category
== previousHoveredCategory
) &&
850 (!d
->categoryVisualRect(previousHoveredCategory
).intersects(QRect(event
->pos(), event
->pos()))))
852 viewport()->update(d
->categoryVisualRect(category
));
857 if (d
->mouseButtonPressed
&& !d
->isDragging
)
859 QPoint start
, end
, initialPressPosition
;
861 initialPressPosition
= d
->initialPressPosition
;
863 initialPressPosition
.setY(initialPressPosition
.y() - verticalOffset());
864 initialPressPosition
.setX(initialPressPosition
.x() - horizontalOffset());
866 if (d
->initialPressPosition
.x() > d
->mousePosition
.x() ||
867 d
->initialPressPosition
.y() > d
->mousePosition
.y())
869 start
= d
->mousePosition
;
870 end
= initialPressPosition
;
874 start
= initialPressPosition
;
875 end
= d
->mousePosition
;
878 rect
= QRect(start
, end
).intersected(viewport()->rect().adjusted(-16, -16, 16, 16));
880 //viewport()->update(rect.united(d->lastSelectionRect));
882 d
->lastSelectionRect
= rect
;
886 void KCategorizedView::mousePressEvent(QMouseEvent
*event
)
888 d
->dragLeftViewport
= false;
890 if (event
->button() == Qt::LeftButton
)
892 d
->mouseButtonPressed
= true;
894 d
->initialPressPosition
= event
->pos();
895 d
->initialPressPosition
.setY(d
->initialPressPosition
.y() +
897 d
->initialPressPosition
.setX(d
->initialPressPosition
.x() +
901 QListView::mousePressEvent(event
);
904 void KCategorizedView::mouseReleaseEvent(QMouseEvent
*event
)
906 d
->mouseButtonPressed
= false;
908 QListView::mouseReleaseEvent(event
);
910 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
916 QPoint initialPressPosition
= viewport()->mapFromGlobal(QCursor::pos());
917 initialPressPosition
.setY(initialPressPosition
.y() + verticalOffset());
918 initialPressPosition
.setX(initialPressPosition
.x() + horizontalOffset());
920 QItemSelection selection
;
922 if (initialPressPosition
== d
->initialPressPosition
)
924 foreach(const QString
&category
, d
->categories
)
926 if (d
->categoryVisualRect(category
).contains(event
->pos()))
928 foreach (const QModelIndex
&index
, d
->categoriesIndexes
[category
])
930 selection
<< QItemSelectionRange(d
->proxyModel
->mapFromSource(index
));
933 selectionModel()->select(selection
, QItemSelectionModel::Select
);
940 d
->lastSelection
= selectionModel()->selection();
942 if (d
->hovered
.isValid())
943 viewport()->update(visualRect(d
->hovered
));
944 else if (!d
->hoveredCategory
.isEmpty())
945 viewport()->update(d
->categoryVisualRect(d
->hoveredCategory
));
948 void KCategorizedView::leaveEvent(QEvent
*event
)
950 d
->hovered
= QModelIndex();
951 d
->hoveredCategory
= QString();
953 QListView::leaveEvent(event
);
956 void KCategorizedView::startDrag(Qt::DropActions supportedActions
)
958 QListView::startDrag(supportedActions
);
960 d
->isDragging
= false;
961 d
->mouseButtonPressed
= false;
963 viewport()->update(d
->lastDraggedItemsRect
);
966 void KCategorizedView::dragMoveEvent(QDragMoveEvent
*event
)
968 d
->mousePosition
= event
->pos();
970 if (d
->mouseButtonPressed
)
972 d
->isDragging
= true;
976 d
->isDragging
= false;
979 d
->dragLeftViewport
= false;
981 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
984 QListView::dragMoveEvent(event
);
988 d
->drawDraggedItems();
991 void KCategorizedView::dragLeaveEvent(QDragLeaveEvent
*event
)
993 d
->dragLeftViewport
= true;
995 QListView::dragLeaveEvent(event
);
998 QModelIndex
KCategorizedView::moveCursor(CursorAction cursorAction
,
999 Qt::KeyboardModifiers modifiers
)
1001 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
1002 !d
->itemCategorizer
)
1004 return QListView::moveCursor(cursorAction
, modifiers
);
1007 const QModelIndex current
= selectionModel()->currentIndex();
1009 int viewportWidth
= viewport()->width() - spacing();
1012 if (gridSize().isEmpty())
1014 itemWidth
= d
->biggestItemSize
.width();
1018 itemWidth
= gridSize().width();
1021 int itemWidthPlusSeparation
= spacing() + itemWidth
;
1022 int elementsPerRow
= viewportWidth
/ itemWidthPlusSeparation
;
1024 QString lastCategory
= d
->categories
[0];
1025 QString theCategory
= d
->categories
[0];
1026 QString afterCategory
= d
->categories
[0];
1027 bool hasToBreak
= false;
1028 foreach (const QString
&category
, d
->categories
)
1032 afterCategory
= category
;
1037 if (category
== d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].category
)
1039 theCategory
= category
;
1046 lastCategory
= category
;
1050 switch (cursorAction
)
1052 case QAbstractItemView::MoveUp
: {
1053 if (d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].relativeOffsetToCategory
>= elementsPerRow
)
1055 int indexToMove
= d
->invertedElementDictionary
[current
].row();
1056 indexToMove
-= qMin(((d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].relativeOffsetToCategory
) + d
->forcedSelectionPosition
), elementsPerRow
- d
->forcedSelectionPosition
+ (d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].relativeOffsetToCategory
% elementsPerRow
));
1058 return d
->elementDictionary
[d
->proxyModel
->index(indexToMove
, 0)];
1062 int lastCategoryLastRow
= (d
->categoriesIndexes
[lastCategory
].count() - 1) % elementsPerRow
;
1063 int indexToMove
= d
->invertedElementDictionary
[current
].row() - d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].relativeOffsetToCategory
;
1065 if (d
->forcedSelectionPosition
>= lastCategoryLastRow
)
1071 indexToMove
-= qMin((lastCategoryLastRow
- d
->forcedSelectionPosition
+ 1), d
->forcedSelectionPosition
+ elementsPerRow
+ 1);
1074 return d
->elementDictionary
[d
->proxyModel
->index(indexToMove
, 0)];
1078 case QAbstractItemView::MoveDown
: {
1079 if (d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].relativeOffsetToCategory
< (d
->categoriesIndexes
[theCategory
].count() - 1 - ((d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
)))
1081 int indexToMove
= d
->invertedElementDictionary
[current
].row();
1082 indexToMove
+= qMin(elementsPerRow
, d
->categoriesIndexes
[theCategory
].count() - 1 - d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].relativeOffsetToCategory
);
1084 return d
->elementDictionary
[d
->proxyModel
->index(indexToMove
, 0)];
1088 int afterCategoryLastRow
= qMin(elementsPerRow
, d
->categoriesIndexes
[afterCategory
].count());
1089 int indexToMove
= d
->invertedElementDictionary
[current
].row() + (d
->categoriesIndexes
[theCategory
].count() - d
->elementsInfo
[d
->proxyModel
->mapToSource(current
)].relativeOffsetToCategory
);
1091 if (d
->forcedSelectionPosition
>= afterCategoryLastRow
)
1093 indexToMove
+= afterCategoryLastRow
- 1;
1097 indexToMove
+= qMin(d
->forcedSelectionPosition
, elementsPerRow
);
1100 return d
->elementDictionary
[d
->proxyModel
->index(indexToMove
, 0)];
1104 case QAbstractItemView::MoveLeft
:
1105 d
->forcedSelectionPosition
= d
->elementsInfo
[d
->proxyModel
->mapToSource(d
->elementDictionary
[d
->proxyModel
->index(d
->invertedElementDictionary
[current
].row() - 1, 0)])].relativeOffsetToCategory
% elementsPerRow
;
1107 if (d
->forcedSelectionPosition
< 0)
1108 d
->forcedSelectionPosition
= (d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
;
1110 return d
->elementDictionary
[d
->proxyModel
->index(d
->invertedElementDictionary
[current
].row() - 1, 0)];
1112 case QAbstractItemView::MoveRight
:
1113 d
->forcedSelectionPosition
= d
->elementsInfo
[d
->proxyModel
->mapToSource(d
->elementDictionary
[d
->proxyModel
->index(d
->invertedElementDictionary
[current
].row() + 1, 0)])].relativeOffsetToCategory
% elementsPerRow
;
1115 if (d
->forcedSelectionPosition
< 0)
1116 d
->forcedSelectionPosition
= (d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
;
1118 return d
->elementDictionary
[d
->proxyModel
->index(d
->invertedElementDictionary
[current
].row() + 1, 0)];
1124 return QListView::moveCursor(cursorAction
, modifiers
);
1127 void KCategorizedView::rowsInserted(const QModelIndex
&parent
,
1131 QListView::rowsInserted(parent
, start
, end
);
1133 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
1134 !d
->itemCategorizer
)
1136 d
->lastSelection
= QItemSelection();
1137 d
->currentViewIndex
= QModelIndex();
1138 d
->forcedSelectionPosition
= 0;
1139 d
->elementsInfo
.clear();
1140 d
->elementsPosition
.clear();
1141 d
->elementDictionary
.clear();
1142 d
->invertedElementDictionary
.clear();
1143 d
->categoriesIndexes
.clear();
1144 d
->categoriesPosition
.clear();
1145 d
->categories
.clear();
1146 d
->intersectedIndexes
.clear();
1147 d
->sourceModelIndexList
.clear();
1148 d
->hovered
= QModelIndex();
1149 d
->biggestItemSize
= QSize(0, 0);
1150 d
->mouseButtonPressed
= false;
1155 rowsInsertedArtifficial(parent
, start
, end
);
1158 void KCategorizedView::rowsInsertedArtifficial(const QModelIndex
&parent
,
1164 d
->lastSelection
= QItemSelection();
1165 d
->currentViewIndex
= QModelIndex();
1166 d
->forcedSelectionPosition
= 0;
1167 d
->elementsInfo
.clear();
1168 d
->elementsPosition
.clear();
1169 d
->elementDictionary
.clear();
1170 d
->invertedElementDictionary
.clear();
1171 d
->categoriesIndexes
.clear();
1172 d
->categoriesPosition
.clear();
1173 d
->categories
.clear();
1174 d
->intersectedIndexes
.clear();
1175 d
->sourceModelIndexList
.clear();
1176 d
->hovered
= QModelIndex();
1177 d
->biggestItemSize
= QSize(0, 0);
1178 d
->mouseButtonPressed
= false;
1180 if (start
> end
|| end
< 0 || start
< 0 || !d
->proxyModel
->rowCount())
1185 // Add all elements mapped to the source model
1186 for (int k
= 0; k
< d
->proxyModel
->rowCount(); k
++)
1188 d
->biggestItemSize
= QSize(qMax(sizeHintForIndex(d
->proxyModel
->index(k
, 0)).width(),
1189 d
->biggestItemSize
.width()),
1190 qMax(sizeHintForIndex(d
->proxyModel
->index(k
, 0)).height(),
1191 d
->biggestItemSize
.height()));
1193 d
->sourceModelIndexList
<<
1194 d
->proxyModel
->mapToSource(d
->proxyModel
->index(k
, 0));
1197 // Sort them with the general purpose lessThan method
1198 LessThan
generalLessThan(d
->proxyModel
,
1199 LessThan::GeneralPurpose
);
1201 qStableSort(d
->sourceModelIndexList
.begin(), d
->sourceModelIndexList
.end(),
1204 // Explore categories
1205 QString prevCategory
=
1206 d
->itemCategorizer
->categoryForItem(d
->sourceModelIndexList
[0],
1207 d
->proxyModel
->sortRole());
1208 QString lastCategory
= prevCategory
;
1209 QModelIndexList modelIndexList
;
1210 struct Private::ElementInfo elementInfo
;
1211 foreach (const QModelIndex
&index
, d
->sourceModelIndexList
)
1213 lastCategory
= d
->itemCategorizer
->categoryForItem(index
,
1214 d
->proxyModel
->sortRole());
1216 elementInfo
.category
= lastCategory
;
1218 if (prevCategory
!= lastCategory
)
1220 d
->categoriesIndexes
.insert(prevCategory
, modelIndexList
);
1221 d
->categories
<< prevCategory
;
1222 modelIndexList
.clear();
1225 modelIndexList
<< index
;
1226 prevCategory
= lastCategory
;
1228 d
->elementsInfo
.insert(index
, elementInfo
);
1231 d
->categoriesIndexes
.insert(prevCategory
, modelIndexList
);
1232 d
->categories
<< prevCategory
;
1234 // Sort items locally in their respective categories with the category
1236 LessThan
categoryLessThan(d
->proxyModel
,
1237 LessThan::CategoryPurpose
);
1239 foreach (const QString
&key
, d
->categories
)
1241 QModelIndexList
&indexList
= d
->categoriesIndexes
[key
];
1243 qStableSort(indexList
.begin(), indexList
.end(), categoryLessThan
);
1246 d
->lastIndex
= d
->categoriesIndexes
[d
->categories
[d
->categories
.count() - 1]][d
->categoriesIndexes
[d
->categories
[d
->categories
.count() - 1]].count() - 1];
1248 // Finally, fill data information of items situation. This will help when
1249 // trying to compute an item place in the viewport
1250 int i
= 0; // position relative to the category beginning
1251 int j
= 0; // number of elements before current
1252 foreach (const QString
&key
, d
->categories
)
1254 foreach (const QModelIndex
&index
, d
->categoriesIndexes
[key
])
1256 struct Private::ElementInfo
&elementInfo
= d
->elementsInfo
[index
];
1258 elementInfo
.relativeOffsetToCategory
= i
;
1260 d
->elementDictionary
.insert(d
->proxyModel
->index(j
, 0),
1261 d
->proxyModel
->mapFromSource(index
));
1263 d
->invertedElementDictionary
.insert(d
->proxyModel
->mapFromSource(index
),
1264 d
->proxyModel
->index(j
, 0));
1273 d
->updateScrollbars();
1276 void KCategorizedView::rowsRemoved(const QModelIndex
&parent
,
1280 if ((viewMode() == KCategorizedView::IconMode
) && d
->proxyModel
&&
1283 // Force the view to update all elements
1284 rowsInsertedArtifficial(parent
, start
, end
);
1288 void KCategorizedView::updateGeometries()
1290 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
1291 !d
->itemCategorizer
)
1293 QListView::updateGeometries();
1297 // Avoid QListView::updateGeometries(), since it will try to set another
1298 // range to our scroll bars, what we don't want (ereslibre)
1299 QAbstractItemView::updateGeometries();
1302 void KCategorizedView::slotSortingRoleChanged()
1304 if ((viewMode() == KCategorizedView::IconMode
) && d
->proxyModel
&&
1307 // Force the view to update all elements
1308 rowsInsertedArtifficial(QModelIndex(), 0, d
->proxyModel
->rowCount() - 1);
1312 #include "kcategorizedview.moc"