2 * This file is part of the KDE project
3 * Copyright (C) 2007 Rafael Fernández López <ereslibre@kde.org>
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>
34 #include "kcategorydrawer.h"
35 #include "kcategorizedsortfilterproxymodel.h"
37 KCategorizedView::Private::Private(KCategorizedView
*listView
)
40 , biggestItemSize(QSize(0, 0))
41 , mouseButtonPressed(false)
43 , dragLeftViewport(false)
48 KCategorizedView::Private::~Private()
52 const QModelIndexList
&KCategorizedView::Private::intersectionSet(const QRect
&rect
)
55 QRect indexVisualRect
;
57 intersectedIndexes
.clear();
61 if (listView
->gridSize().isEmpty())
63 itemHeight
= biggestItemSize
.height();
67 itemHeight
= listView
->gridSize().height();
70 // Lets find out where we should start
71 int top
= proxyModel
->rowCount() - 1;
73 int middle
= (top
+ bottom
) / 2;
76 middle
= (top
+ bottom
) / 2;
78 index
= proxyModel
->index(middle
, 0);
79 indexVisualRect
= visualRect(index
);
80 // We need the whole height (not only the visualRect). This will help us to update
81 // all needed indexes correctly (ereslibre)
82 indexVisualRect
.setHeight(indexVisualRect
.height() + (itemHeight
- indexVisualRect
.height()));
84 if (qMax(indexVisualRect
.topLeft().y(),
85 indexVisualRect
.bottomRight().y()) < qMin(rect
.topLeft().y(),
86 rect
.bottomRight().y()))
96 for (int i
= middle
; i
< proxyModel
->rowCount(); i
++)
98 index
= proxyModel
->index(i
, 0);
99 indexVisualRect
= visualRect(index
);
101 if (rect
.intersects(indexVisualRect
))
102 intersectedIndexes
.append(index
);
104 // If we passed next item, stop searching for hits
105 if (qMax(rect
.bottomRight().y(), rect
.topLeft().y()) <
106 qMin(indexVisualRect
.topLeft().y(),
107 indexVisualRect
.bottomRight().y()))
111 return intersectedIndexes
;
114 QRect
KCategorizedView::Private::visualRectInViewport(const QModelIndex
&index
) const
116 if (!index
.isValid())
119 QString curCategory
= elementsInfo
[index
.row()].category
;
123 if (listView
->layoutDirection() == Qt::LeftToRight
)
125 retRect
= QRect(listView
->spacing(), listView
->spacing() * 2 +
126 categoryDrawer
->categoryHeight(listView
->viewOptions()), 0, 0);
130 retRect
= QRect(listView
->viewport()->width() - listView
->spacing(), listView
->spacing() * 2 +
131 categoryDrawer
->categoryHeight(listView
->viewOptions()), 0, 0);
134 int viewportWidth
= listView
->viewport()->width() - listView
->spacing();
139 if (listView
->gridSize().isEmpty())
141 itemHeight
= biggestItemSize
.height();
142 itemWidth
= biggestItemSize
.width();
146 itemHeight
= listView
->gridSize().height();
147 itemWidth
= listView
->gridSize().width();
150 int itemWidthPlusSeparation
= listView
->spacing() + itemWidth
;
151 int elementsPerRow
= viewportWidth
/ itemWidthPlusSeparation
;
155 int column
= elementsInfo
[index
.row()].relativeOffsetToCategory
% elementsPerRow
;
156 int row
= elementsInfo
[index
.row()].relativeOffsetToCategory
/ elementsPerRow
;
158 if (listView
->layoutDirection() == Qt::LeftToRight
)
160 retRect
.setLeft(retRect
.left() + column
* listView
->spacing() +
165 retRect
.setLeft(retRect
.right() - column
* listView
->spacing() -
166 column
* itemWidth
- itemWidth
);
168 retRect
.setRight(retRect
.right() - column
* listView
->spacing() -
172 foreach (const QString
&category
, categories
)
174 if (category
== curCategory
)
177 float rows
= (float) ((float) categoriesIndexes
[category
].count() /
178 (float) elementsPerRow
);
180 int rowsInt
= categoriesIndexes
[category
].count() / elementsPerRow
;
182 if (rows
- trunc(rows
)) rowsInt
++;
184 retRect
.setTop(retRect
.top() +
185 (rowsInt
* itemHeight
) +
186 categoryDrawer
->categoryHeight(listView
->viewOptions()) +
187 listView
->spacing() * 2);
189 if (listView
->gridSize().isEmpty())
191 retRect
.setTop(retRect
.top() +
192 (rowsInt
* listView
->spacing()));
196 if (listView
->gridSize().isEmpty())
198 retRect
.setTop(retRect
.top() + row
* listView
->spacing() +
203 retRect
.setTop(retRect
.top() + (row
* itemHeight
));
206 retRect
.setWidth(itemWidth
);
208 QModelIndex heightIndex
= proxyModel
->index(index
.row(), 0);
209 if (listView
->gridSize().isEmpty())
211 retRect
.setHeight(listView
->sizeHintForIndex(heightIndex
).height());
215 retRect
.setHeight(qMin(listView
->sizeHintForIndex(heightIndex
).height(),
216 listView
->gridSize().height()));
222 QRect
KCategorizedView::Private::visualCategoryRectInViewport(const QString
&category
)
225 QRect
retRect(listView
->spacing(),
227 listView
->viewport()->width() - listView
->spacing() * 2,
230 if (!proxyModel
->rowCount() || !categories
.contains(category
))
233 QModelIndex index
= proxyModel
->index(0, 0, QModelIndex());
235 int viewportWidth
= listView
->viewport()->width() - listView
->spacing();
240 if (listView
->gridSize().isEmpty())
242 itemHeight
= biggestItemSize
.height();
243 itemWidth
= biggestItemSize
.width();
247 itemHeight
= listView
->gridSize().height();
248 itemWidth
= listView
->gridSize().width();
251 int itemWidthPlusSeparation
= listView
->spacing() + itemWidth
;
252 int elementsPerRow
= viewportWidth
/ itemWidthPlusSeparation
;
257 foreach (const QString
&itCategory
, categories
)
259 if (itCategory
== category
)
262 float rows
= (float) ((float) categoriesIndexes
[itCategory
].count() /
263 (float) elementsPerRow
);
264 int rowsInt
= categoriesIndexes
[itCategory
].count() / elementsPerRow
;
266 if (rows
- trunc(rows
)) rowsInt
++;
268 retRect
.setTop(retRect
.top() +
269 (rowsInt
* itemHeight
) +
270 categoryDrawer
->categoryHeight(listView
->viewOptions()) +
271 listView
->spacing() * 2);
273 if (listView
->gridSize().isEmpty())
275 retRect
.setTop(retRect
.top() +
276 (rowsInt
* listView
->spacing()));
280 retRect
.setHeight(categoryDrawer
->categoryHeight(listView
->viewOptions()));
285 // We're sure elementsPosition doesn't contain index
286 const QRect
&KCategorizedView::Private::cacheIndex(const QModelIndex
&index
)
288 QRect rect
= visualRectInViewport(index
);
289 elementsPosition
[index
.row()] = rect
;
291 return elementsPosition
[index
.row()];
294 // We're sure categoriesPosition doesn't contain category
295 const QRect
&KCategorizedView::Private::cacheCategory(const QString
&category
)
297 QRect rect
= visualCategoryRectInViewport(category
);
298 categoriesPosition
[category
] = rect
;
300 return categoriesPosition
[category
];
303 const QRect
&KCategorizedView::Private::cachedRectIndex(const QModelIndex
&index
)
305 if (elementsPosition
.contains(index
.row())) // If we have it cached
307 return elementsPosition
[index
.row()];
309 else // Otherwise, cache it
311 return cacheIndex(index
);
315 const QRect
&KCategorizedView::Private::cachedRectCategory(const QString
&category
)
317 if (categoriesPosition
.contains(category
)) // If we have it cached
319 return categoriesPosition
[category
];
321 else // Otherwise, cache it and
323 return cacheCategory(category
);
327 QRect
KCategorizedView::Private::visualRect(const QModelIndex
&index
)
329 QRect retRect
= cachedRectIndex(index
);
330 int dx
= -listView
->horizontalOffset();
331 int dy
= -listView
->verticalOffset();
332 retRect
.adjust(dx
, dy
, dx
, dy
);
337 QRect
KCategorizedView::Private::categoryVisualRect(const QString
&category
)
339 QRect retRect
= cachedRectCategory(category
);
340 int dx
= -listView
->horizontalOffset();
341 int dy
= -listView
->verticalOffset();
342 retRect
.adjust(dx
, dy
, dx
, dy
);
347 void KCategorizedView::Private::drawNewCategory(const QModelIndex
&index
,
349 const QStyleOption
&option
,
352 if (!index
.isValid())
357 QStyleOption optionCopy
= option
;
358 const QString category
= proxyModel
->data(index
, KCategorizedSortFilterProxyModel::CategoryRole
).toString();
360 optionCopy
.state
&= ~QStyle::State_Selected
;
362 if ((category
== hoveredCategory
) && !mouseButtonPressed
)
364 optionCopy
.state
|= QStyle::State_MouseOver
;
366 else if ((category
== hoveredCategory
) && mouseButtonPressed
)
368 QPoint initialPressPosition
= listView
->viewport()->mapFromGlobal(QCursor::pos());
369 initialPressPosition
.setY(initialPressPosition
.y() + listView
->verticalOffset());
370 initialPressPosition
.setX(initialPressPosition
.x() + listView
->horizontalOffset());
372 if (initialPressPosition
== this->initialPressPosition
)
374 optionCopy
.state
|= QStyle::State_Selected
;
378 categoryDrawer
->drawCategory(index
,
385 void KCategorizedView::Private::updateScrollbars()
387 // find the last index in the last category
388 QModelIndex lastIndex
= categoriesIndexes
.isEmpty() ? QModelIndex() : categoriesIndexes
[categories
.last()].last();
390 int lastItemBottom
= cachedRectIndex(lastIndex
).top() +
391 listView
->spacing() + (listView
->gridSize().isEmpty() ? 0 : listView
->gridSize().height()) - listView
->viewport()->height();
393 listView
->verticalScrollBar()->setSingleStep(listView
->viewport()->height() / 10);
394 listView
->verticalScrollBar()->setPageStep(listView
->viewport()->height());
395 listView
->verticalScrollBar()->setRange(0, lastItemBottom
);
398 void KCategorizedView::Private::drawDraggedItems(QPainter
*painter
)
400 QStyleOptionViewItemV3 option
= listView
->viewOptions();
401 option
.state
&= ~QStyle::State_MouseOver
;
402 foreach (const QModelIndex
&index
, listView
->selectionModel()->selectedIndexes())
404 const int dx
= mousePosition
.x() - initialPressPosition
.x() + listView
->horizontalOffset();
405 const int dy
= mousePosition
.y() - initialPressPosition
.y() + listView
->verticalOffset();
407 option
.rect
= visualRect(index
);
408 option
.rect
.adjust(dx
, dy
, dx
, dy
);
410 if (option
.rect
.intersects(listView
->viewport()->rect()))
412 listView
->itemDelegate(index
)->paint(painter
, option
, index
);
417 void KCategorizedView::Private::layoutChanged(bool forceItemReload
)
419 if ((listView
->viewMode() == KCategorizedView::IconMode
) && proxyModel
&&
420 categoryDrawer
&& proxyModel
->isCategorizedModel() &&
421 (((modelSortRole
!= proxyModel
->sortRole()) ||
422 (modelSortColumn
!= proxyModel
->sortColumn()) ||
423 (modelSortOrder
!= proxyModel
->sortOrder()) ||
424 (modelCategorized
!= proxyModel
->isCategorizedModel())) || forceItemReload
))
426 // Force the view to update all elements
427 listView
->rowsInsertedArtifficial(QModelIndex(), 0, proxyModel
->rowCount() - 1);
429 modelSortRole
= proxyModel
->sortRole();
430 modelSortColumn
= proxyModel
->sortColumn();
431 modelCategorized
= proxyModel
->isCategorizedModel();
432 modelSortOrder
= proxyModel
->sortOrder();
434 else if ((listView
->viewMode() == KCategorizedView::IconMode
) && proxyModel
&&
435 categoryDrawer
&& proxyModel
->isCategorizedModel())
441 void KCategorizedView::Private::drawDraggedItems()
445 foreach (const QModelIndex
&index
, listView
->selectionModel()->selectedIndexes())
447 int dx
= mousePosition
.x() - initialPressPosition
.x() + listView
->horizontalOffset();
448 int dy
= mousePosition
.y() - initialPressPosition
.y() + listView
->verticalOffset();
450 currentRect
= visualRect(index
);
451 currentRect
.adjust(dx
, dy
, dx
, dy
);
453 if (currentRect
.intersects(listView
->viewport()->rect()))
455 rectToUpdate
= rectToUpdate
.united(currentRect
);
459 listView
->viewport()->update(lastDraggedItemsRect
.united(rectToUpdate
));
461 lastDraggedItemsRect
= rectToUpdate
;
465 //==============================================================================
468 KCategorizedView::KCategorizedView(QWidget
*parent
)
470 , d(new Private(this))
474 KCategorizedView::~KCategorizedView()
479 void KCategorizedView::setGridSize(const QSize
&size
)
481 QListView::setGridSize(size
);
483 d
->layoutChanged(true);
486 void KCategorizedView::setModel(QAbstractItemModel
*model
)
488 d
->lastSelection
= QItemSelection();
489 d
->currentViewIndex
= QModelIndex();
490 d
->forcedSelectionPosition
= 0;
491 d
->elementsInfo
.clear();
492 d
->elementsPosition
.clear();
493 d
->categoriesIndexes
.clear();
494 d
->categoriesPosition
.clear();
495 d
->categories
.clear();
496 d
->intersectedIndexes
.clear();
497 d
->modelIndexList
.clear();
498 d
->hovered
= QModelIndex();
499 d
->mouseButtonPressed
= false;
503 QObject::disconnect(d
->proxyModel
,
504 SIGNAL(layoutChanged()),
505 this, SLOT(slotLayoutChanged()));
507 QObject::disconnect(d
->proxyModel
,
508 SIGNAL(dataChanged(QModelIndex
,QModelIndex
)),
509 this, SLOT(slotLayoutChanged()));
511 QObject::disconnect(d
->proxyModel
,
512 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
513 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
516 QListView::setModel(model
);
518 d
->proxyModel
= dynamic_cast<KCategorizedSortFilterProxyModel
*>(model
);
522 d
->modelSortRole
= d
->proxyModel
->sortRole();
523 d
->modelSortColumn
= d
->proxyModel
->sortColumn();
524 d
->modelCategorized
= true;
525 d
->modelSortOrder
= d
->proxyModel
->sortOrder();
527 QObject::connect(d
->proxyModel
,
528 SIGNAL(layoutChanged()),
529 this, SLOT(slotLayoutChanged()));
531 QObject::connect(d
->proxyModel
,
532 SIGNAL(dataChanged(QModelIndex
,QModelIndex
)),
533 this, SLOT(slotLayoutChanged()));
535 QObject::connect(d
->proxyModel
,
536 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
537 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
539 if (d
->proxyModel
->rowCount())
541 rowsInsertedArtifficial(QModelIndex(), 0, d
->proxyModel
->rowCount() - 1);
546 d
->modelCategorized
= false;
550 QRect
KCategorizedView::visualRect(const QModelIndex
&index
) const
552 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
553 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
555 return QListView::visualRect(index
);
558 if (!qobject_cast
<const QSortFilterProxyModel
*>(index
.model()))
560 return d
->visualRect(d
->proxyModel
->mapFromSource(index
));
563 return d
->visualRect(index
);
566 KCategoryDrawer
*KCategorizedView::categoryDrawer() const
568 return d
->categoryDrawer
;
571 void KCategorizedView::setCategoryDrawer(KCategoryDrawer
*categoryDrawer
)
573 d
->lastSelection
= QItemSelection();
574 d
->currentViewIndex
= QModelIndex();
575 d
->forcedSelectionPosition
= 0;
576 d
->elementsInfo
.clear();
577 d
->elementsPosition
.clear();
578 d
->categoriesIndexes
.clear();
579 d
->categoriesPosition
.clear();
580 d
->categories
.clear();
581 d
->intersectedIndexes
.clear();
582 d
->modelIndexList
.clear();
583 d
->hovered
= QModelIndex();
584 d
->mouseButtonPressed
= false;
586 if (!categoryDrawer
&& d
->proxyModel
)
588 QObject::disconnect(d
->proxyModel
,
589 SIGNAL(layoutChanged()),
590 this, SLOT(slotLayoutChanged()));
592 QObject::disconnect(d
->proxyModel
,
593 SIGNAL(dataChanged(QModelIndex
,QModelIndex
)),
594 this, SLOT(slotLayoutChanged()));
596 QObject::disconnect(d
->proxyModel
,
597 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
598 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
600 else if (categoryDrawer
&& d
->proxyModel
)
602 QObject::connect(d
->proxyModel
,
603 SIGNAL(layoutChanged()),
604 this, SLOT(slotLayoutChanged()));
606 QObject::connect(d
->proxyModel
,
607 SIGNAL(dataChanged(QModelIndex
,QModelIndex
)),
608 this, SLOT(slotLayoutChanged()));
610 QObject::connect(d
->proxyModel
,
611 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
612 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
615 d
->categoryDrawer
= categoryDrawer
;
621 rowsInsertedArtifficial(QModelIndex(), 0, d
->proxyModel
->rowCount() - 1);
630 QModelIndex
KCategorizedView::indexAt(const QPoint
&point
) const
632 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
633 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
635 return QListView::indexAt(point
);
640 QModelIndexList item
= d
->intersectionSet(QRect(point
, point
));
642 if (item
.count() == 1)
652 void KCategorizedView::reset()
656 d
->lastSelection
= QItemSelection();
657 d
->currentViewIndex
= QModelIndex();
658 d
->forcedSelectionPosition
= 0;
659 d
->elementsInfo
.clear();
660 d
->elementsPosition
.clear();
661 d
->categoriesIndexes
.clear();
662 d
->categoriesPosition
.clear();
663 d
->categories
.clear();
664 d
->intersectedIndexes
.clear();
665 d
->modelIndexList
.clear();
666 d
->hovered
= QModelIndex();
667 d
->biggestItemSize
= QSize(0, 0);
668 d
->mouseButtonPressed
= false;
671 void KCategorizedView::paintEvent(QPaintEvent
*event
)
673 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
674 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
676 QListView::paintEvent(event
);
680 QStyleOptionViewItemV3 option
= viewOptions();
681 option
.widget
= this;
684 option
.features
|= QStyleOptionViewItemV2::WrapText
;
687 QPainter
painter(viewport());
688 QRect area
= event
->rect();
689 const bool focus
= (hasFocus() || viewport()->hasFocus()) &&
690 currentIndex().isValid();
691 const QStyle::State state
= option
.state
;
692 const bool enabled
= (state
& QStyle::State_Enabled
) != 0;
696 QModelIndexList dirtyIndexes
= d
->intersectionSet(area
);
697 foreach (const QModelIndex
&index
, dirtyIndexes
)
699 option
.state
= state
;
700 option
.rect
= visualRect(index
);
702 if (selectionModel() && selectionModel()->isSelected(index
))
704 option
.state
|= QStyle::State_Selected
;
709 QPalette::ColorGroup cg
;
710 if ((d
->proxyModel
->flags(index
) & Qt::ItemIsEnabled
) == 0)
712 option
.state
&= ~QStyle::State_Enabled
;
713 cg
= QPalette::Disabled
;
717 cg
= QPalette::Normal
;
719 option
.palette
.setCurrentColorGroup(cg
);
722 if (focus
&& currentIndex() == index
)
724 option
.state
|= QStyle::State_HasFocus
;
725 if (this->state() == EditingState
)
726 option
.state
|= QStyle::State_Editing
;
729 if ((index
== d
->hovered
) && !d
->mouseButtonPressed
)
730 option
.state
|= QStyle::State_MouseOver
;
732 option
.state
&= ~QStyle::State_MouseOver
;
734 itemDelegate(index
)->paint(&painter
, option
, index
);
738 QStyleOptionViewItem otherOption
;
739 foreach (const QString
&category
, d
->categories
)
741 otherOption
= option
;
742 otherOption
.rect
= d
->categoryVisualRect(category
);
743 otherOption
.state
&= ~QStyle::State_MouseOver
;
745 if (otherOption
.rect
.intersects(area
))
747 QModelIndex indexToDraw
= d
->proxyModel
->index(d
->categoriesIndexes
[category
][0].row(), d
->proxyModel
->sortColumn());
749 d
->drawNewCategory(indexToDraw
,
750 d
->proxyModel
->sortRole(), otherOption
, &painter
);
754 if (d
->mouseButtonPressed
&& !d
->isDragging
)
756 QPoint start
, end
, initialPressPosition
;
758 initialPressPosition
= d
->initialPressPosition
;
760 initialPressPosition
.setY(initialPressPosition
.y() - verticalOffset());
761 initialPressPosition
.setX(initialPressPosition
.x() - horizontalOffset());
763 if (d
->initialPressPosition
.x() > d
->mousePosition
.x() ||
764 d
->initialPressPosition
.y() > d
->mousePosition
.y())
766 start
= d
->mousePosition
;
767 end
= initialPressPosition
;
771 start
= initialPressPosition
;
772 end
= d
->mousePosition
;
775 QStyleOptionRubberBand yetAnotherOption
;
776 yetAnotherOption
.initFrom(this);
777 yetAnotherOption
.shape
= QRubberBand::Rectangle
;
778 yetAnotherOption
.opaque
= false;
779 yetAnotherOption
.rect
= QRect(start
, end
).intersected(viewport()->rect().adjusted(-16, -16, 16, 16));
781 style()->drawControl(QStyle::CE_RubberBand
, &yetAnotherOption
, &painter
);
785 if (d
->isDragging
&& !d
->dragLeftViewport
)
787 painter
.setOpacity(0.5);
788 d
->drawDraggedItems(&painter
);
794 void KCategorizedView::resizeEvent(QResizeEvent
*event
)
796 QListView::resizeEvent(event
);
798 // Clear the items positions cache
799 d
->elementsPosition
.clear();
800 d
->categoriesPosition
.clear();
801 d
->forcedSelectionPosition
= 0;
803 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
804 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
809 d
->updateScrollbars();
812 void KCategorizedView::setSelection(const QRect
&rect
,
813 QItemSelectionModel::SelectionFlags flags
)
815 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
816 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
818 QListView::setSelection(rect
, flags
);
825 if (flags
& QItemSelectionModel::Clear
)
827 if (!rect
.intersects(d
->categoryVisualRect(d
->hoveredCategory
)))
829 d
->lastSelection
= QItemSelection();
833 selectionModel()->clear();
835 QModelIndexList dirtyIndexes
= d
->intersectionSet(rect
);
837 if (!dirtyIndexes
.count())
839 if (!d
->lastSelection
.isEmpty() &&
840 (rect
.intersects(d
->categoryVisualRect(d
->hoveredCategory
)) || d
->mouseButtonPressed
))
842 selectionModel()->select(d
->lastSelection
, flags
);
848 QItemSelection selection
;
850 if (!d
->mouseButtonPressed
)
852 selection
= QItemSelection(dirtyIndexes
[0], dirtyIndexes
[0]);
853 d
->currentViewIndex
= dirtyIndexes
[0];
857 QModelIndex first
= dirtyIndexes
[0];
859 foreach (const QModelIndex
&index
, dirtyIndexes
)
861 if (last
.isValid() && last
.row() + 1 != index
.row())
863 QItemSelectionRange
range(first
, last
);
874 selection
<< QItemSelectionRange(first
, last
);
877 if (d
->lastSelection
.count())
879 if ((selection
.count() == 1) && (selection
[0].indexes().count() == 1))
880 selection
.merge(d
->lastSelection
, flags
);
882 selection
.merge(d
->lastSelection
, QItemSelectionModel::Select
);
885 selectionModel()->select(selection
, flags
);
888 void KCategorizedView::mouseMoveEvent(QMouseEvent
*event
)
890 QListView::mouseMoveEvent(event
);
892 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
893 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
898 const QString previousHoveredCategory
= d
->hoveredCategory
;
900 d
->mousePosition
= event
->pos();
901 d
->hoveredCategory
= QString();
904 foreach (const QString
&category
, d
->categories
)
906 if (d
->categoryVisualRect(category
).intersects(QRect(event
->pos(), event
->pos())))
908 d
->hoveredCategory
= category
;
909 viewport()->update(d
->categoryVisualRect(category
));
911 else if ((category
== previousHoveredCategory
) &&
912 (!d
->categoryVisualRect(previousHoveredCategory
).intersects(QRect(event
->pos(), event
->pos()))))
914 viewport()->update(d
->categoryVisualRect(category
));
919 if (d
->mouseButtonPressed
&& !d
->isDragging
)
921 QPoint start
, end
, initialPressPosition
;
923 initialPressPosition
= d
->initialPressPosition
;
925 initialPressPosition
.setY(initialPressPosition
.y() - verticalOffset());
926 initialPressPosition
.setX(initialPressPosition
.x() - horizontalOffset());
928 if (d
->initialPressPosition
.x() > d
->mousePosition
.x() ||
929 d
->initialPressPosition
.y() > d
->mousePosition
.y())
931 start
= d
->mousePosition
;
932 end
= initialPressPosition
;
936 start
= initialPressPosition
;
937 end
= d
->mousePosition
;
940 rect
= QRect(start
, end
).intersected(viewport()->rect().adjusted(-16, -16, 16, 16));
942 //viewport()->update(rect.united(d->lastSelectionRect));
944 d
->lastSelectionRect
= rect
;
948 void KCategorizedView::mousePressEvent(QMouseEvent
*event
)
950 d
->dragLeftViewport
= false;
952 if (event
->button() == Qt::LeftButton
)
954 d
->mouseButtonPressed
= true;
956 d
->initialPressPosition
= event
->pos();
957 d
->initialPressPosition
.setY(d
->initialPressPosition
.y() +
959 d
->initialPressPosition
.setX(d
->initialPressPosition
.x() +
963 QListView::mousePressEvent(event
);
965 viewport()->update(d
->categoryVisualRect(d
->hoveredCategory
));
968 void KCategorizedView::mouseReleaseEvent(QMouseEvent
*event
)
970 d
->mouseButtonPressed
= false;
972 QListView::mouseReleaseEvent(event
);
974 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
975 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
980 QPoint initialPressPosition
= viewport()->mapFromGlobal(QCursor::pos());
981 initialPressPosition
.setY(initialPressPosition
.y() + verticalOffset());
982 initialPressPosition
.setX(initialPressPosition
.x() + horizontalOffset());
984 QItemSelection selection
;
985 QItemSelection deselection
;
987 if (initialPressPosition
== d
->initialPressPosition
)
989 foreach(const QString
&category
, d
->categories
)
991 if (d
->categoryVisualRect(category
).contains(event
->pos()))
993 foreach (const QModelIndex
&index
, d
->categoriesIndexes
[category
])
995 QModelIndex selectIndex
= index
.model()->index(index
.row(), 0);
997 if (!d
->lastSelection
.contains(selectIndex
))
999 selection
<< QItemSelectionRange(selectIndex
);
1003 deselection
<< QItemSelectionRange(selectIndex
);
1007 selectionModel()->select(selection
, QItemSelectionModel::Select
);
1008 selectionModel()->select(deselection
, QItemSelectionModel::Deselect
);
1015 d
->lastSelection
= selectionModel()->selection();
1017 if (d
->hovered
.isValid())
1018 viewport()->update(visualRect(d
->hovered
));
1019 else if (!d
->hoveredCategory
.isEmpty())
1020 viewport()->update(d
->categoryVisualRect(d
->hoveredCategory
));
1023 void KCategorizedView::leaveEvent(QEvent
*event
)
1025 d
->hovered
= QModelIndex();
1026 d
->hoveredCategory
= QString();
1028 QListView::leaveEvent(event
);
1031 void KCategorizedView::startDrag(Qt::DropActions supportedActions
)
1033 // FIXME: QAbstractItemView does far better here since it sets the
1034 // pixmap of selected icons to the dragging cursor, but it sets a non
1035 // ARGB window so it is no transparent. Use QAbstractItemView when
1036 // this is fixed on Qt.
1037 // QAbstractItemView::startDrag(supportedActions);
1038 QListView::startDrag(supportedActions
);
1040 d
->isDragging
= false;
1041 d
->mouseButtonPressed
= false;
1043 viewport()->update(d
->lastDraggedItemsRect
);
1046 void KCategorizedView::dragMoveEvent(QDragMoveEvent
*event
)
1048 d
->mousePosition
= event
->pos();
1050 if (d
->mouseButtonPressed
)
1052 d
->isDragging
= true;
1056 d
->isDragging
= false;
1059 d
->dragLeftViewport
= false;
1061 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
1062 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
1064 QListView::dragMoveEvent(event
);
1068 d
->drawDraggedItems();
1071 void KCategorizedView::dragLeaveEvent(QDragLeaveEvent
*event
)
1073 d
->dragLeftViewport
= true;
1075 QListView::dragLeaveEvent(event
);
1078 QModelIndex
KCategorizedView::moveCursor(CursorAction cursorAction
,
1079 Qt::KeyboardModifiers modifiers
)
1081 if ((viewMode() != KCategorizedView::IconMode
) ||
1083 !d
->categoryDrawer
||
1084 d
->categories
.isEmpty() ||
1085 !d
->proxyModel
->isCategorizedModel()
1088 return QListView::moveCursor(cursorAction
, modifiers
);
1091 const QModelIndex current
= selectionModel()->currentIndex();
1093 int viewportWidth
= viewport()->width() - spacing();
1096 if (gridSize().isEmpty())
1098 itemWidth
= d
->biggestItemSize
.width();
1102 itemWidth
= gridSize().width();
1105 int itemWidthPlusSeparation
= spacing() + itemWidth
;
1106 int elementsPerRow
= viewportWidth
/ itemWidthPlusSeparation
;
1108 QString lastCategory
= d
->categories
.first();
1109 QString theCategory
= d
->categories
.first();
1110 QString afterCategory
= d
->categories
.first();
1112 bool hasToBreak
= false;
1113 foreach (const QString
&category
, d
->categories
)
1117 afterCategory
= category
;
1122 if (category
== d
->elementsInfo
[current
.row()].category
)
1124 theCategory
= category
;
1131 lastCategory
= category
;
1135 switch (cursorAction
)
1137 case QAbstractItemView::MoveUp
: {
1138 if (d
->elementsInfo
[current
.row()].relativeOffsetToCategory
>= elementsPerRow
)
1140 int indexToMove
= current
.row();
1141 indexToMove
-= qMin(((d
->elementsInfo
[current
.row()].relativeOffsetToCategory
) + d
->forcedSelectionPosition
), elementsPerRow
- d
->forcedSelectionPosition
+ (d
->elementsInfo
[current
.row()].relativeOffsetToCategory
% elementsPerRow
));
1143 return d
->proxyModel
->index(indexToMove
, 0);
1147 int lastCategoryLastRow
= (d
->categoriesIndexes
[lastCategory
].count() - 1) % elementsPerRow
;
1148 int indexToMove
= current
.row() - d
->elementsInfo
[current
.row()].relativeOffsetToCategory
;
1150 if (d
->forcedSelectionPosition
>= lastCategoryLastRow
)
1156 indexToMove
-= qMin((lastCategoryLastRow
- d
->forcedSelectionPosition
+ 1), d
->forcedSelectionPosition
+ elementsPerRow
+ 1);
1159 return d
->proxyModel
->index(indexToMove
, 0);
1163 case QAbstractItemView::MoveDown
: {
1164 if (d
->elementsInfo
[current
.row()].relativeOffsetToCategory
< (d
->categoriesIndexes
[theCategory
].count() - 1 - ((d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
)))
1166 int indexToMove
= current
.row();
1167 indexToMove
+= qMin(elementsPerRow
, d
->categoriesIndexes
[theCategory
].count() - 1 - d
->elementsInfo
[current
.row()].relativeOffsetToCategory
);
1169 return d
->proxyModel
->index(indexToMove
, 0);
1173 int afterCategoryLastRow
= qMin(elementsPerRow
, d
->categoriesIndexes
[afterCategory
].count());
1174 int indexToMove
= current
.row() + (d
->categoriesIndexes
[theCategory
].count() - d
->elementsInfo
[current
.row()].relativeOffsetToCategory
);
1176 if (d
->forcedSelectionPosition
>= afterCategoryLastRow
)
1178 indexToMove
+= afterCategoryLastRow
- 1;
1182 indexToMove
+= qMin(d
->forcedSelectionPosition
, elementsPerRow
);
1185 return d
->proxyModel
->index(indexToMove
, 0);
1189 case QAbstractItemView::MoveLeft
:
1190 if (layoutDirection() == Qt::RightToLeft
)
1192 d
->forcedSelectionPosition
= d
->elementsInfo
[current
.row() + 1].relativeOffsetToCategory
% elementsPerRow
;
1194 if (d
->forcedSelectionPosition
< 0)
1195 d
->forcedSelectionPosition
= (d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
;
1197 return d
->proxyModel
->index(current
.row() + 1, 0);
1200 d
->forcedSelectionPosition
= d
->elementsInfo
[current
.row() - 1].relativeOffsetToCategory
% elementsPerRow
;
1202 if (d
->forcedSelectionPosition
< 0)
1203 d
->forcedSelectionPosition
= (d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
;
1205 return d
->proxyModel
->index(current
.row() - 1, 0);
1207 case QAbstractItemView::MoveRight
:
1208 if (layoutDirection() == Qt::RightToLeft
)
1210 d
->forcedSelectionPosition
= d
->elementsInfo
[current
.row() - 1].relativeOffsetToCategory
% elementsPerRow
;
1212 if (d
->forcedSelectionPosition
< 0)
1213 d
->forcedSelectionPosition
= (d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
;
1215 return d
->proxyModel
->index(current
.row() - 1, 0);
1218 d
->forcedSelectionPosition
= d
->elementsInfo
[current
.row() + 1].relativeOffsetToCategory
% elementsPerRow
;
1220 if (d
->forcedSelectionPosition
< 0)
1221 d
->forcedSelectionPosition
= (d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
;
1223 return d
->proxyModel
->index(current
.row() + 1, 0);
1229 return QListView::moveCursor(cursorAction
, modifiers
);
1232 void KCategorizedView::rowsInserted(const QModelIndex
&parent
,
1236 QListView::rowsInserted(parent
, start
, end
);
1238 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
1239 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
1241 d
->lastSelection
= QItemSelection();
1242 d
->currentViewIndex
= QModelIndex();
1243 d
->forcedSelectionPosition
= 0;
1244 d
->elementsInfo
.clear();
1245 d
->elementsPosition
.clear();
1246 d
->categoriesIndexes
.clear();
1247 d
->categoriesPosition
.clear();
1248 d
->categories
.clear();
1249 d
->intersectedIndexes
.clear();
1250 d
->modelIndexList
.clear();
1251 d
->hovered
= QModelIndex();
1252 d
->biggestItemSize
= QSize(0, 0);
1253 d
->mouseButtonPressed
= false;
1258 rowsInsertedArtifficial(parent
, start
, end
);
1261 void KCategorizedView::rowsInsertedArtifficial(const QModelIndex
&parent
,
1267 d
->lastSelection
= QItemSelection();
1268 d
->currentViewIndex
= QModelIndex();
1269 d
->forcedSelectionPosition
= 0;
1270 d
->elementsInfo
.clear();
1271 d
->elementsPosition
.clear();
1272 d
->categoriesIndexes
.clear();
1273 d
->categoriesPosition
.clear();
1274 d
->categories
.clear();
1275 d
->intersectedIndexes
.clear();
1276 d
->modelIndexList
.clear();
1277 d
->hovered
= QModelIndex();
1278 d
->biggestItemSize
= QSize(0, 0);
1279 d
->mouseButtonPressed
= false;
1281 if (start
> end
|| end
< 0 || start
< 0 || !d
->proxyModel
->rowCount())
1286 // Add all elements mapped to the source model and explore categories
1287 QString prevCategory
= d
->proxyModel
->data(d
->proxyModel
->index(0, d
->proxyModel
->sortColumn()), KCategorizedSortFilterProxyModel::CategoryRole
).toString();
1288 QString lastCategory
= prevCategory
;
1289 QModelIndexList modelIndexList
;
1290 struct Private::ElementInfo elementInfo
;
1292 for (int k
= 0; k
< d
->proxyModel
->rowCount(); ++k
)
1294 QModelIndex index
= d
->proxyModel
->index(k
, d
->proxyModel
->sortColumn());
1295 QModelIndex indexSize
= d
->proxyModel
->index(k
, 0);
1297 d
->biggestItemSize
= QSize(qMax(sizeHintForIndex(indexSize
).width(),
1298 d
->biggestItemSize
.width()),
1299 qMax(sizeHintForIndex(indexSize
).height(),
1300 d
->biggestItemSize
.height()));
1302 d
->modelIndexList
<< index
;
1304 lastCategory
= d
->proxyModel
->data(index
, KCategorizedSortFilterProxyModel::CategoryRole
).toString();
1306 elementInfo
.category
= lastCategory
;
1308 if (prevCategory
!= lastCategory
)
1311 d
->categoriesIndexes
.insert(prevCategory
, modelIndexList
);
1312 d
->categories
<< prevCategory
;
1313 modelIndexList
.clear();
1320 elementInfo
.relativeOffsetToCategory
= offset
;
1322 modelIndexList
<< index
;
1323 prevCategory
= lastCategory
;
1325 d
->elementsInfo
.insert(index
.row(), elementInfo
);
1328 d
->categoriesIndexes
.insert(prevCategory
, modelIndexList
);
1329 d
->categories
<< prevCategory
;
1331 d
->updateScrollbars();
1334 void KCategorizedView::rowsRemoved(const QModelIndex
&parent
,
1338 if ((viewMode() == KCategorizedView::IconMode
) && d
->proxyModel
&&
1339 d
->categoryDrawer
&& d
->proxyModel
->isCategorizedModel())
1341 // Force the view to update all elements
1342 rowsInsertedArtifficial(QModelIndex(), 0, d
->proxyModel
->rowCount() - 1);
1346 void KCategorizedView::updateGeometries()
1348 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
1349 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
1351 QListView::updateGeometries();
1355 // Avoid QListView::updateGeometries(), since it will try to set another
1356 // range to our scroll bars, what we don't want (ereslibre)
1357 QAbstractItemView::updateGeometries();
1360 void KCategorizedView::slotLayoutChanged()
1365 #include "kcategorizedview.moc"