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() &&
422 (modelSortRole
!= proxyModel
->sortRole()) ||
423 (modelSortColumn
!= proxyModel
->sortColumn()) ||
424 (modelSortOrder
!= proxyModel
->sortOrder()) ||
425 (modelLastRowCount
!= proxyModel
->rowCount()) ||
426 (modelCategorized
!= proxyModel
->isCategorizedModel()))))
428 // Force the view to update all elements
429 listView
->rowsInsertedArtifficial(QModelIndex(), 0, proxyModel
->rowCount() - 1);
431 if (!forceItemReload
)
433 modelSortRole
= proxyModel
->sortRole();
434 modelSortColumn
= proxyModel
->sortColumn();
435 modelSortOrder
= proxyModel
->sortOrder();
436 modelLastRowCount
= proxyModel
->rowCount();
437 modelCategorized
= proxyModel
->isCategorizedModel();
440 else if ((listView
->viewMode() == KCategorizedView::IconMode
) && proxyModel
&&
441 categoryDrawer
&& proxyModel
->isCategorizedModel())
447 void KCategorizedView::Private::drawDraggedItems()
451 foreach (const QModelIndex
&index
, listView
->selectionModel()->selectedIndexes())
453 int dx
= mousePosition
.x() - initialPressPosition
.x() + listView
->horizontalOffset();
454 int dy
= mousePosition
.y() - initialPressPosition
.y() + listView
->verticalOffset();
456 currentRect
= visualRect(index
);
457 currentRect
.adjust(dx
, dy
, dx
, dy
);
459 if (currentRect
.intersects(listView
->viewport()->rect()))
461 rectToUpdate
= rectToUpdate
.united(currentRect
);
465 listView
->viewport()->update(lastDraggedItemsRect
.united(rectToUpdate
));
467 lastDraggedItemsRect
= rectToUpdate
;
471 //==============================================================================
474 KCategorizedView::KCategorizedView(QWidget
*parent
)
476 , d(new Private(this))
480 KCategorizedView::~KCategorizedView()
485 void KCategorizedView::setGridSize(const QSize
&size
)
487 QListView::setGridSize(size
);
489 d
->layoutChanged(true);
492 void KCategorizedView::setModel(QAbstractItemModel
*model
)
494 d
->lastSelection
= QItemSelection();
495 d
->currentViewIndex
= QModelIndex();
496 d
->forcedSelectionPosition
= 0;
497 d
->elementsInfo
.clear();
498 d
->elementsPosition
.clear();
499 d
->categoriesIndexes
.clear();
500 d
->categoriesPosition
.clear();
501 d
->categories
.clear();
502 d
->intersectedIndexes
.clear();
503 d
->modelIndexList
.clear();
504 d
->hovered
= QModelIndex();
505 d
->mouseButtonPressed
= false;
509 QObject::disconnect(d
->proxyModel
,
510 SIGNAL(layoutChanged()),
511 this, SLOT(slotLayoutChanged()));
513 QObject::disconnect(d
->proxyModel
,
514 SIGNAL(dataChanged(QModelIndex
,QModelIndex
)),
515 this, SLOT(slotLayoutChanged()));
517 QObject::disconnect(d
->proxyModel
,
518 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
519 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
522 QListView::setModel(model
);
524 d
->proxyModel
= dynamic_cast<KCategorizedSortFilterProxyModel
*>(model
);
528 d
->modelSortRole
= d
->proxyModel
->sortRole();
529 d
->modelSortColumn
= d
->proxyModel
->sortColumn();
530 d
->modelSortOrder
= d
->proxyModel
->sortOrder();
531 d
->modelLastRowCount
= d
->proxyModel
->rowCount();
532 d
->modelCategorized
= d
->proxyModel
->isCategorizedModel();
534 QObject::connect(d
->proxyModel
,
535 SIGNAL(layoutChanged()),
536 this, SLOT(slotLayoutChanged()));
538 QObject::connect(d
->proxyModel
,
539 SIGNAL(dataChanged(QModelIndex
,QModelIndex
)),
540 this, SLOT(slotLayoutChanged()));
542 QObject::connect(d
->proxyModel
,
543 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
544 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
546 if (d
->proxyModel
->rowCount())
548 d
->layoutChanged(true);
553 d
->modelCategorized
= false;
557 QRect
KCategorizedView::visualRect(const QModelIndex
&index
) const
559 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
560 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
562 return QListView::visualRect(index
);
565 if (!qobject_cast
<const QSortFilterProxyModel
*>(index
.model()))
567 return d
->visualRect(d
->proxyModel
->mapFromSource(index
));
570 return d
->visualRect(index
);
573 KCategoryDrawer
*KCategorizedView::categoryDrawer() const
575 return d
->categoryDrawer
;
578 void KCategorizedView::setCategoryDrawer(KCategoryDrawer
*categoryDrawer
)
580 d
->lastSelection
= QItemSelection();
581 d
->currentViewIndex
= QModelIndex();
582 d
->forcedSelectionPosition
= 0;
583 d
->elementsInfo
.clear();
584 d
->elementsPosition
.clear();
585 d
->categoriesIndexes
.clear();
586 d
->categoriesPosition
.clear();
587 d
->categories
.clear();
588 d
->intersectedIndexes
.clear();
589 d
->modelIndexList
.clear();
590 d
->hovered
= QModelIndex();
591 d
->mouseButtonPressed
= false;
593 if (!categoryDrawer
&& d
->proxyModel
)
595 QObject::disconnect(d
->proxyModel
,
596 SIGNAL(layoutChanged()),
597 this, SLOT(slotLayoutChanged()));
599 QObject::disconnect(d
->proxyModel
,
600 SIGNAL(dataChanged(QModelIndex
,QModelIndex
)),
601 this, SLOT(slotLayoutChanged()));
603 QObject::disconnect(d
->proxyModel
,
604 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
605 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
607 else if (categoryDrawer
&& d
->proxyModel
)
609 QObject::connect(d
->proxyModel
,
610 SIGNAL(layoutChanged()),
611 this, SLOT(slotLayoutChanged()));
613 QObject::connect(d
->proxyModel
,
614 SIGNAL(dataChanged(QModelIndex
,QModelIndex
)),
615 this, SLOT(slotLayoutChanged()));
617 QObject::connect(d
->proxyModel
,
618 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
619 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
622 d
->categoryDrawer
= categoryDrawer
;
628 if (d
->proxyModel
->rowCount())
630 d
->layoutChanged(true);
640 QModelIndex
KCategorizedView::indexAt(const QPoint
&point
) const
642 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
643 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
645 return QListView::indexAt(point
);
650 QModelIndexList item
= d
->intersectionSet(QRect(point
, point
));
652 if (item
.count() == 1)
662 void KCategorizedView::reset()
666 d
->lastSelection
= QItemSelection();
667 d
->currentViewIndex
= QModelIndex();
668 d
->forcedSelectionPosition
= 0;
669 d
->elementsInfo
.clear();
670 d
->elementsPosition
.clear();
671 d
->categoriesIndexes
.clear();
672 d
->categoriesPosition
.clear();
673 d
->categories
.clear();
674 d
->intersectedIndexes
.clear();
675 d
->modelIndexList
.clear();
676 d
->hovered
= QModelIndex();
677 d
->biggestItemSize
= QSize(0, 0);
678 d
->mouseButtonPressed
= false;
681 void KCategorizedView::paintEvent(QPaintEvent
*event
)
683 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
684 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
686 QListView::paintEvent(event
);
690 QStyleOptionViewItemV3 option
= viewOptions();
691 option
.widget
= this;
694 option
.features
|= QStyleOptionViewItemV2::WrapText
;
697 QPainter
painter(viewport());
698 QRect area
= event
->rect();
699 const bool focus
= (hasFocus() || viewport()->hasFocus()) &&
700 currentIndex().isValid();
701 const QStyle::State state
= option
.state
;
702 const bool enabled
= (state
& QStyle::State_Enabled
) != 0;
706 QModelIndexList dirtyIndexes
= d
->intersectionSet(area
);
707 foreach (const QModelIndex
&index
, dirtyIndexes
)
709 option
.state
= state
;
710 option
.rect
= visualRect(index
);
712 if (selectionModel() && selectionModel()->isSelected(index
))
714 option
.state
|= QStyle::State_Selected
;
719 QPalette::ColorGroup cg
;
720 if ((d
->proxyModel
->flags(index
) & Qt::ItemIsEnabled
) == 0)
722 option
.state
&= ~QStyle::State_Enabled
;
723 cg
= QPalette::Disabled
;
727 cg
= QPalette::Normal
;
729 option
.palette
.setCurrentColorGroup(cg
);
732 if (focus
&& currentIndex() == index
)
734 option
.state
|= QStyle::State_HasFocus
;
735 if (this->state() == EditingState
)
736 option
.state
|= QStyle::State_Editing
;
739 if ((index
== d
->hovered
) && !d
->mouseButtonPressed
)
740 option
.state
|= QStyle::State_MouseOver
;
742 option
.state
&= ~QStyle::State_MouseOver
;
744 itemDelegate(index
)->paint(&painter
, option
, index
);
748 QStyleOptionViewItem otherOption
;
749 bool intersectedInThePast
= false;
750 foreach (const QString
&category
, d
->categories
)
752 otherOption
= option
;
753 otherOption
.rect
= d
->categoryVisualRect(category
);
754 otherOption
.state
&= ~QStyle::State_MouseOver
;
756 if (otherOption
.rect
.intersects(area
))
758 intersectedInThePast
= true;
760 QModelIndex indexToDraw
= d
->proxyModel
->index(d
->categoriesIndexes
[category
][0].row(), d
->proxyModel
->sortColumn());
762 d
->drawNewCategory(indexToDraw
,
763 d
->proxyModel
->sortRole(), otherOption
, &painter
);
765 else if (intersectedInThePast
)
767 break; // the visible area has been finished, we don't need to keep asking, the rest won't intersect
768 // this is doable because we know that categories are correctly ordered on the list
772 if (d
->mouseButtonPressed
&& !d
->isDragging
)
774 QPoint start
, end
, initialPressPosition
;
776 initialPressPosition
= d
->initialPressPosition
;
778 initialPressPosition
.setY(initialPressPosition
.y() - verticalOffset());
779 initialPressPosition
.setX(initialPressPosition
.x() - horizontalOffset());
781 if (d
->initialPressPosition
.x() > d
->mousePosition
.x() ||
782 d
->initialPressPosition
.y() > d
->mousePosition
.y())
784 start
= d
->mousePosition
;
785 end
= initialPressPosition
;
789 start
= initialPressPosition
;
790 end
= d
->mousePosition
;
793 QStyleOptionRubberBand yetAnotherOption
;
794 yetAnotherOption
.initFrom(this);
795 yetAnotherOption
.shape
= QRubberBand::Rectangle
;
796 yetAnotherOption
.opaque
= false;
797 yetAnotherOption
.rect
= QRect(start
, end
).intersected(viewport()->rect().adjusted(-16, -16, 16, 16));
799 style()->drawControl(QStyle::CE_RubberBand
, &yetAnotherOption
, &painter
);
803 if (d
->isDragging
&& !d
->dragLeftViewport
)
805 painter
.setOpacity(0.5);
806 d
->drawDraggedItems(&painter
);
812 void KCategorizedView::resizeEvent(QResizeEvent
*event
)
814 QListView::resizeEvent(event
);
816 // Clear the items positions cache
817 d
->elementsPosition
.clear();
818 d
->categoriesPosition
.clear();
819 d
->forcedSelectionPosition
= 0;
821 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
822 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
827 d
->updateScrollbars();
830 void KCategorizedView::setSelection(const QRect
&rect
,
831 QItemSelectionModel::SelectionFlags flags
)
833 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
834 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
836 QListView::setSelection(rect
, flags
);
843 if (flags
& QItemSelectionModel::Clear
)
845 if (!rect
.intersects(d
->categoryVisualRect(d
->hoveredCategory
)))
847 d
->lastSelection
= QItemSelection();
851 selectionModel()->clear();
853 QModelIndexList dirtyIndexes
= d
->intersectionSet(rect
);
855 if (!dirtyIndexes
.count())
857 if (!d
->lastSelection
.isEmpty() &&
858 (rect
.intersects(d
->categoryVisualRect(d
->hoveredCategory
)) || d
->mouseButtonPressed
))
860 selectionModel()->select(d
->lastSelection
, flags
);
866 QItemSelection selection
;
868 if (!d
->mouseButtonPressed
)
870 selection
= QItemSelection(dirtyIndexes
[0], dirtyIndexes
[0]);
871 d
->currentViewIndex
= dirtyIndexes
[0];
875 QModelIndex first
= dirtyIndexes
[0];
877 foreach (const QModelIndex
&index
, dirtyIndexes
)
879 if (last
.isValid() && last
.row() + 1 != index
.row())
881 QItemSelectionRange
range(first
, last
);
892 selection
<< QItemSelectionRange(first
, last
);
895 if (d
->lastSelection
.count())
897 if ((selection
.count() == 1) && (selection
[0].indexes().count() == 1))
898 selection
.merge(d
->lastSelection
, flags
);
900 selection
.merge(d
->lastSelection
, QItemSelectionModel::Select
);
903 selectionModel()->select(selection
, flags
);
906 void KCategorizedView::mouseMoveEvent(QMouseEvent
*event
)
908 QListView::mouseMoveEvent(event
);
910 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
911 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
916 const QString previousHoveredCategory
= d
->hoveredCategory
;
918 d
->mousePosition
= event
->pos();
919 d
->hoveredCategory
= QString();
922 foreach (const QString
&category
, d
->categories
)
924 if (d
->categoryVisualRect(category
).intersects(QRect(event
->pos(), event
->pos())))
926 d
->hoveredCategory
= category
;
927 viewport()->update(d
->categoryVisualRect(category
));
929 else if ((category
== previousHoveredCategory
) &&
930 (!d
->categoryVisualRect(previousHoveredCategory
).intersects(QRect(event
->pos(), event
->pos()))))
932 viewport()->update(d
->categoryVisualRect(category
));
937 if (d
->mouseButtonPressed
&& !d
->isDragging
)
939 QPoint start
, end
, initialPressPosition
;
941 initialPressPosition
= d
->initialPressPosition
;
943 initialPressPosition
.setY(initialPressPosition
.y() - verticalOffset());
944 initialPressPosition
.setX(initialPressPosition
.x() - horizontalOffset());
946 if (d
->initialPressPosition
.x() > d
->mousePosition
.x() ||
947 d
->initialPressPosition
.y() > d
->mousePosition
.y())
949 start
= d
->mousePosition
;
950 end
= initialPressPosition
;
954 start
= initialPressPosition
;
955 end
= d
->mousePosition
;
958 rect
= QRect(start
, end
).intersected(viewport()->rect().adjusted(-16, -16, 16, 16));
960 //viewport()->update(rect.united(d->lastSelectionRect));
962 d
->lastSelectionRect
= rect
;
966 void KCategorizedView::mousePressEvent(QMouseEvent
*event
)
968 d
->dragLeftViewport
= false;
970 if (event
->button() == Qt::LeftButton
)
972 d
->mouseButtonPressed
= true;
974 d
->initialPressPosition
= event
->pos();
975 d
->initialPressPosition
.setY(d
->initialPressPosition
.y() +
977 d
->initialPressPosition
.setX(d
->initialPressPosition
.x() +
981 QListView::mousePressEvent(event
);
983 viewport()->update(d
->categoryVisualRect(d
->hoveredCategory
));
986 void KCategorizedView::mouseReleaseEvent(QMouseEvent
*event
)
988 d
->mouseButtonPressed
= false;
990 QListView::mouseReleaseEvent(event
);
992 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
993 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
998 QPoint initialPressPosition
= viewport()->mapFromGlobal(QCursor::pos());
999 initialPressPosition
.setY(initialPressPosition
.y() + verticalOffset());
1000 initialPressPosition
.setX(initialPressPosition
.x() + horizontalOffset());
1002 QItemSelection selection
;
1003 QItemSelection deselection
;
1005 if (initialPressPosition
== d
->initialPressPosition
)
1007 foreach(const QString
&category
, d
->categories
)
1009 if (d
->categoryVisualRect(category
).contains(event
->pos()))
1011 foreach (const QModelIndex
&index
, d
->categoriesIndexes
[category
])
1013 QModelIndex selectIndex
= index
.model()->index(index
.row(), 0);
1015 if (!d
->lastSelection
.contains(selectIndex
))
1017 selection
<< QItemSelectionRange(selectIndex
);
1021 deselection
<< QItemSelectionRange(selectIndex
);
1025 selectionModel()->select(selection
, QItemSelectionModel::Select
);
1026 selectionModel()->select(deselection
, QItemSelectionModel::Deselect
);
1033 d
->lastSelection
= selectionModel()->selection();
1035 if (d
->hovered
.isValid())
1036 viewport()->update(visualRect(d
->hovered
));
1037 else if (!d
->hoveredCategory
.isEmpty())
1038 viewport()->update(d
->categoryVisualRect(d
->hoveredCategory
));
1041 void KCategorizedView::leaveEvent(QEvent
*event
)
1043 d
->hovered
= QModelIndex();
1044 d
->hoveredCategory
= QString();
1046 QListView::leaveEvent(event
);
1049 void KCategorizedView::startDrag(Qt::DropActions supportedActions
)
1051 // FIXME: QAbstractItemView does far better here since it sets the
1052 // pixmap of selected icons to the dragging cursor, but it sets a non
1053 // ARGB window so it is no transparent. Use QAbstractItemView when
1054 // this is fixed on Qt.
1055 // QAbstractItemView::startDrag(supportedActions);
1056 QListView::startDrag(supportedActions
);
1058 d
->isDragging
= false;
1059 d
->mouseButtonPressed
= false;
1061 viewport()->update(d
->lastDraggedItemsRect
);
1064 void KCategorizedView::dragMoveEvent(QDragMoveEvent
*event
)
1066 d
->mousePosition
= event
->pos();
1068 if (d
->mouseButtonPressed
)
1070 d
->isDragging
= true;
1074 d
->isDragging
= false;
1077 d
->dragLeftViewport
= false;
1079 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
1080 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
1082 QListView::dragMoveEvent(event
);
1086 d
->drawDraggedItems();
1089 void KCategorizedView::dragLeaveEvent(QDragLeaveEvent
*event
)
1091 d
->dragLeftViewport
= true;
1093 QListView::dragLeaveEvent(event
);
1096 QModelIndex
KCategorizedView::moveCursor(CursorAction cursorAction
,
1097 Qt::KeyboardModifiers modifiers
)
1099 if ((viewMode() != KCategorizedView::IconMode
) ||
1101 !d
->categoryDrawer
||
1102 d
->categories
.isEmpty() ||
1103 !d
->proxyModel
->isCategorizedModel()
1106 return QListView::moveCursor(cursorAction
, modifiers
);
1109 const QModelIndex current
= selectionModel()->currentIndex();
1111 int viewportWidth
= viewport()->width() - spacing();
1114 if (gridSize().isEmpty())
1116 itemWidth
= d
->biggestItemSize
.width();
1120 itemWidth
= gridSize().width();
1123 int itemWidthPlusSeparation
= spacing() + itemWidth
;
1124 int elementsPerRow
= viewportWidth
/ itemWidthPlusSeparation
;
1126 QString lastCategory
= d
->categories
.first();
1127 QString theCategory
= d
->categories
.first();
1128 QString afterCategory
= d
->categories
.first();
1130 bool hasToBreak
= false;
1131 foreach (const QString
&category
, d
->categories
)
1135 afterCategory
= category
;
1140 if (category
== d
->elementsInfo
[current
.row()].category
)
1142 theCategory
= category
;
1149 lastCategory
= category
;
1153 switch (cursorAction
)
1155 case QAbstractItemView::MoveUp
: {
1156 if (d
->elementsInfo
[current
.row()].relativeOffsetToCategory
>= elementsPerRow
)
1158 int indexToMove
= current
.row();
1159 indexToMove
-= qMin(((d
->elementsInfo
[current
.row()].relativeOffsetToCategory
) + d
->forcedSelectionPosition
), elementsPerRow
- d
->forcedSelectionPosition
+ (d
->elementsInfo
[current
.row()].relativeOffsetToCategory
% elementsPerRow
));
1161 return d
->proxyModel
->index(indexToMove
, 0);
1165 int lastCategoryLastRow
= (d
->categoriesIndexes
[lastCategory
].count() - 1) % elementsPerRow
;
1166 int indexToMove
= current
.row() - d
->elementsInfo
[current
.row()].relativeOffsetToCategory
;
1168 if (d
->forcedSelectionPosition
>= lastCategoryLastRow
)
1174 indexToMove
-= qMin((lastCategoryLastRow
- d
->forcedSelectionPosition
+ 1), d
->forcedSelectionPosition
+ elementsPerRow
+ 1);
1177 return d
->proxyModel
->index(indexToMove
, 0);
1181 case QAbstractItemView::MoveDown
: {
1182 if (d
->elementsInfo
[current
.row()].relativeOffsetToCategory
< (d
->categoriesIndexes
[theCategory
].count() - 1 - ((d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
)))
1184 int indexToMove
= current
.row();
1185 indexToMove
+= qMin(elementsPerRow
, d
->categoriesIndexes
[theCategory
].count() - 1 - d
->elementsInfo
[current
.row()].relativeOffsetToCategory
);
1187 return d
->proxyModel
->index(indexToMove
, 0);
1191 int afterCategoryLastRow
= qMin(elementsPerRow
, d
->categoriesIndexes
[afterCategory
].count());
1192 int indexToMove
= current
.row() + (d
->categoriesIndexes
[theCategory
].count() - d
->elementsInfo
[current
.row()].relativeOffsetToCategory
);
1194 if (d
->forcedSelectionPosition
>= afterCategoryLastRow
)
1196 indexToMove
+= afterCategoryLastRow
- 1;
1200 indexToMove
+= qMin(d
->forcedSelectionPosition
, elementsPerRow
);
1203 return d
->proxyModel
->index(indexToMove
, 0);
1207 case QAbstractItemView::MoveLeft
:
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);
1225 case QAbstractItemView::MoveRight
:
1226 if (layoutDirection() == Qt::RightToLeft
)
1228 d
->forcedSelectionPosition
= d
->elementsInfo
[current
.row() - 1].relativeOffsetToCategory
% elementsPerRow
;
1230 if (d
->forcedSelectionPosition
< 0)
1231 d
->forcedSelectionPosition
= (d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
;
1233 return d
->proxyModel
->index(current
.row() - 1, 0);
1236 d
->forcedSelectionPosition
= d
->elementsInfo
[current
.row() + 1].relativeOffsetToCategory
% elementsPerRow
;
1238 if (d
->forcedSelectionPosition
< 0)
1239 d
->forcedSelectionPosition
= (d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
;
1241 return d
->proxyModel
->index(current
.row() + 1, 0);
1247 return QListView::moveCursor(cursorAction
, modifiers
);
1250 void KCategorizedView::rowsInserted(const QModelIndex
&parent
,
1254 QListView::rowsInserted(parent
, start
, end
);
1256 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
1257 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
1259 d
->lastSelection
= QItemSelection();
1260 d
->currentViewIndex
= QModelIndex();
1261 d
->forcedSelectionPosition
= 0;
1262 d
->elementsInfo
.clear();
1263 d
->elementsPosition
.clear();
1264 d
->categoriesIndexes
.clear();
1265 d
->categoriesPosition
.clear();
1266 d
->categories
.clear();
1267 d
->intersectedIndexes
.clear();
1268 d
->modelIndexList
.clear();
1269 d
->hovered
= QModelIndex();
1270 d
->biggestItemSize
= QSize(0, 0);
1271 d
->mouseButtonPressed
= false;
1276 rowsInsertedArtifficial(parent
, start
, end
);
1279 void KCategorizedView::rowsInsertedArtifficial(const QModelIndex
&parent
,
1285 d
->lastSelection
= QItemSelection();
1286 d
->currentViewIndex
= QModelIndex();
1287 d
->forcedSelectionPosition
= 0;
1288 d
->elementsInfo
.clear();
1289 d
->elementsPosition
.clear();
1290 d
->categoriesIndexes
.clear();
1291 d
->categoriesPosition
.clear();
1292 d
->categories
.clear();
1293 d
->intersectedIndexes
.clear();
1294 d
->modelIndexList
.clear();
1295 d
->hovered
= QModelIndex();
1296 d
->biggestItemSize
= QSize(0, 0);
1297 d
->mouseButtonPressed
= false;
1299 if (start
> end
|| end
< 0 || start
< 0 || !d
->proxyModel
->rowCount())
1304 // Add all elements mapped to the source model and explore categories
1305 QString prevCategory
= d
->proxyModel
->data(d
->proxyModel
->index(0, d
->proxyModel
->sortColumn()), KCategorizedSortFilterProxyModel::CategoryRole
).toString();
1306 QString lastCategory
= prevCategory
;
1307 QModelIndexList modelIndexList
;
1308 struct Private::ElementInfo elementInfo
;
1310 for (int k
= 0; k
< d
->proxyModel
->rowCount(); ++k
)
1312 QModelIndex index
= d
->proxyModel
->index(k
, d
->proxyModel
->sortColumn());
1313 QModelIndex indexSize
= d
->proxyModel
->index(k
, 0);
1315 d
->biggestItemSize
= QSize(qMax(sizeHintForIndex(indexSize
).width(),
1316 d
->biggestItemSize
.width()),
1317 qMax(sizeHintForIndex(indexSize
).height(),
1318 d
->biggestItemSize
.height()));
1320 d
->modelIndexList
<< index
;
1322 lastCategory
= d
->proxyModel
->data(index
, KCategorizedSortFilterProxyModel::CategoryRole
).toString();
1324 elementInfo
.category
= lastCategory
;
1326 if (prevCategory
!= lastCategory
)
1329 d
->categoriesIndexes
.insert(prevCategory
, modelIndexList
);
1330 d
->categories
<< prevCategory
;
1331 modelIndexList
.clear();
1338 elementInfo
.relativeOffsetToCategory
= offset
;
1340 modelIndexList
<< index
;
1341 prevCategory
= lastCategory
;
1343 d
->elementsInfo
.insert(index
.row(), elementInfo
);
1346 d
->categoriesIndexes
.insert(prevCategory
, modelIndexList
);
1347 d
->categories
<< prevCategory
;
1349 d
->updateScrollbars();
1352 void KCategorizedView::rowsRemoved(const QModelIndex
&parent
,
1356 if ((viewMode() == KCategorizedView::IconMode
) && d
->proxyModel
&&
1357 d
->categoryDrawer
&& d
->proxyModel
->isCategorizedModel())
1359 // Force the view to update all elements
1360 rowsInsertedArtifficial(QModelIndex(), 0, d
->proxyModel
->rowCount() - 1);
1364 void KCategorizedView::updateGeometries()
1366 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
1367 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
1369 QListView::updateGeometries();
1373 // Avoid QListView::updateGeometries(), since it will try to set another
1374 // range to our scroll bars, what we don't want (ereslibre)
1375 QAbstractItemView::updateGeometries();
1378 void KCategorizedView::slotLayoutChanged()
1383 #include "kcategorizedview.moc"