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
29 #include <QPaintEvent>
33 #include "kcategorydrawer.h"
34 #include "kcategorizedsortfilterproxymodel.h"
36 // By defining DOLPHIN_DRAGANDDROP the custom drag and drop implementation of
37 // KCategorizedView is bypassed to have a consistent drag and drop look for all
38 // views. Hopefully transparent pixmaps for drag objects will be supported in
39 // Qt 4.4, so that this workaround can be skipped.
40 #define DOLPHIN_DRAGANDDROP
42 KCategorizedView::Private::Private(KCategorizedView
*listView
)
45 , biggestItemSize(QSize(0, 0))
46 , mouseButtonPressed(false)
47 , rightMouseButtonPressed(false)
49 , dragLeftViewport(false)
54 KCategorizedView::Private::~Private()
58 const QModelIndexList
&KCategorizedView::Private::intersectionSet(const QRect
&rect
)
61 QRect indexVisualRect
;
63 intersectedIndexes
.clear();
67 if (listView
->gridSize().isEmpty())
69 itemHeight
= biggestItemSize
.height();
73 itemHeight
= listView
->gridSize().height();
76 // Lets find out where we should start
77 int top
= proxyModel
->rowCount() - 1;
79 int middle
= (top
+ bottom
) / 2;
82 middle
= (top
+ bottom
) / 2;
84 index
= proxyModel
->index(middle
, 0);
85 indexVisualRect
= visualRect(index
);
86 // We need the whole height (not only the visualRect). This will help us to update
87 // all needed indexes correctly (ereslibre)
88 indexVisualRect
.setHeight(indexVisualRect
.height() + (itemHeight
- indexVisualRect
.height()));
90 if (qMax(indexVisualRect
.topLeft().y(),
91 indexVisualRect
.bottomRight().y()) < qMin(rect
.topLeft().y(),
92 rect
.bottomRight().y()))
102 for (int i
= middle
; i
< proxyModel
->rowCount(); i
++)
104 index
= proxyModel
->index(i
, 0);
105 indexVisualRect
= visualRect(index
);
107 if (rect
.intersects(indexVisualRect
))
108 intersectedIndexes
.append(index
);
110 // If we passed next item, stop searching for hits
111 if (qMax(rect
.bottomRight().y(), rect
.topLeft().y()) <
112 qMin(indexVisualRect
.topLeft().y(),
113 indexVisualRect
.bottomRight().y()))
117 return intersectedIndexes
;
120 QRect
KCategorizedView::Private::visualRectInViewport(const QModelIndex
&index
) const
122 if (!index
.isValid())
125 QString curCategory
= elementsInfo
[index
.row()].category
;
129 if (listView
->layoutDirection() == Qt::LeftToRight
)
131 retRect
= QRect(listView
->spacing(), listView
->spacing() * 2 +
132 categoryDrawer
->categoryHeight(listView
->viewOptions()), 0, 0);
136 retRect
= QRect(listView
->viewport()->width() - listView
->spacing(), listView
->spacing() * 2 +
137 categoryDrawer
->categoryHeight(listView
->viewOptions()), 0, 0);
140 int viewportWidth
= listView
->viewport()->width() - listView
->spacing();
145 if (listView
->gridSize().isEmpty())
147 itemHeight
= biggestItemSize
.height();
148 itemWidth
= biggestItemSize
.width();
152 itemHeight
= listView
->gridSize().height();
153 itemWidth
= listView
->gridSize().width();
156 int itemWidthPlusSeparation
= listView
->spacing() + itemWidth
;
157 int elementsPerRow
= viewportWidth
/ itemWidthPlusSeparation
;
161 int column
= elementsInfo
[index
.row()].relativeOffsetToCategory
% elementsPerRow
;
162 int row
= elementsInfo
[index
.row()].relativeOffsetToCategory
/ elementsPerRow
;
164 if (listView
->layoutDirection() == Qt::LeftToRight
)
166 retRect
.setLeft(retRect
.left() + column
* listView
->spacing() +
171 retRect
.setLeft(retRect
.right() - column
* listView
->spacing() -
172 column
* itemWidth
- itemWidth
);
174 retRect
.setRight(retRect
.right() - column
* listView
->spacing() -
178 foreach (const QString
&category
, categories
)
180 if (category
== curCategory
)
183 float rows
= (float) ((float) categoriesIndexes
[category
].count() /
184 (float) elementsPerRow
);
186 int rowsInt
= categoriesIndexes
[category
].count() / elementsPerRow
;
188 if (rows
- trunc(rows
)) rowsInt
++;
190 retRect
.setTop(retRect
.top() +
191 (rowsInt
* itemHeight
) +
192 categoryDrawer
->categoryHeight(listView
->viewOptions()) +
193 listView
->spacing() * 2);
195 if (listView
->gridSize().isEmpty())
197 retRect
.setTop(retRect
.top() +
198 (rowsInt
* listView
->spacing()));
202 if (listView
->gridSize().isEmpty())
204 retRect
.setTop(retRect
.top() + row
* listView
->spacing() +
209 retRect
.setTop(retRect
.top() + (row
* itemHeight
));
212 retRect
.setWidth(itemWidth
);
214 QModelIndex heightIndex
= proxyModel
->index(index
.row(), 0);
215 if (listView
->gridSize().isEmpty())
217 retRect
.setHeight(listView
->sizeHintForIndex(heightIndex
).height());
221 retRect
.setHeight(qMin(listView
->sizeHintForIndex(heightIndex
).height(),
222 listView
->gridSize().height()));
228 QRect
KCategorizedView::Private::visualCategoryRectInViewport(const QString
&category
)
231 QRect
retRect(listView
->spacing(),
233 listView
->viewport()->width() - listView
->spacing() * 2,
236 if (!proxyModel
->rowCount() || !categories
.contains(category
))
239 QModelIndex index
= proxyModel
->index(0, 0, QModelIndex());
241 int viewportWidth
= listView
->viewport()->width() - listView
->spacing();
246 if (listView
->gridSize().isEmpty())
248 itemHeight
= biggestItemSize
.height();
249 itemWidth
= biggestItemSize
.width();
253 itemHeight
= listView
->gridSize().height();
254 itemWidth
= listView
->gridSize().width();
257 int itemWidthPlusSeparation
= listView
->spacing() + itemWidth
;
258 int elementsPerRow
= viewportWidth
/ itemWidthPlusSeparation
;
263 foreach (const QString
&itCategory
, categories
)
265 if (itCategory
== category
)
268 float rows
= (float) ((float) categoriesIndexes
[itCategory
].count() /
269 (float) elementsPerRow
);
270 int rowsInt
= categoriesIndexes
[itCategory
].count() / elementsPerRow
;
272 if (rows
- trunc(rows
)) rowsInt
++;
274 retRect
.setTop(retRect
.top() +
275 (rowsInt
* itemHeight
) +
276 categoryDrawer
->categoryHeight(listView
->viewOptions()) +
277 listView
->spacing() * 2);
279 if (listView
->gridSize().isEmpty())
281 retRect
.setTop(retRect
.top() +
282 (rowsInt
* listView
->spacing()));
286 retRect
.setHeight(categoryDrawer
->categoryHeight(listView
->viewOptions()));
291 // We're sure elementsPosition doesn't contain index
292 const QRect
&KCategorizedView::Private::cacheIndex(const QModelIndex
&index
)
294 QRect rect
= visualRectInViewport(index
);
295 elementsPosition
[index
.row()] = rect
;
297 return elementsPosition
[index
.row()];
300 // We're sure categoriesPosition doesn't contain category
301 const QRect
&KCategorizedView::Private::cacheCategory(const QString
&category
)
303 QRect rect
= visualCategoryRectInViewport(category
);
304 categoriesPosition
[category
] = rect
;
306 return categoriesPosition
[category
];
309 const QRect
&KCategorizedView::Private::cachedRectIndex(const QModelIndex
&index
)
311 if (elementsPosition
.contains(index
.row())) // If we have it cached
313 return elementsPosition
[index
.row()];
315 else // Otherwise, cache it
317 return cacheIndex(index
);
321 const QRect
&KCategorizedView::Private::cachedRectCategory(const QString
&category
)
323 if (categoriesPosition
.contains(category
)) // If we have it cached
325 return categoriesPosition
[category
];
327 else // Otherwise, cache it and
329 return cacheCategory(category
);
333 QRect
KCategorizedView::Private::visualRect(const QModelIndex
&index
)
335 QRect retRect
= cachedRectIndex(index
);
336 int dx
= -listView
->horizontalOffset();
337 int dy
= -listView
->verticalOffset();
338 retRect
.adjust(dx
, dy
, dx
, dy
);
343 QRect
KCategorizedView::Private::categoryVisualRect(const QString
&category
)
345 QRect retRect
= cachedRectCategory(category
);
346 int dx
= -listView
->horizontalOffset();
347 int dy
= -listView
->verticalOffset();
348 retRect
.adjust(dx
, dy
, dx
, dy
);
353 void KCategorizedView::Private::drawNewCategory(const QModelIndex
&index
,
355 const QStyleOption
&option
,
358 if (!index
.isValid())
363 QStyleOption optionCopy
= option
;
364 const QString category
= proxyModel
->data(index
, KCategorizedSortFilterProxyModel::CategoryDisplayRole
).toString();
366 optionCopy
.state
&= ~QStyle::State_Selected
;
368 if ((category
== hoveredCategory
) && !mouseButtonPressed
)
370 optionCopy
.state
|= QStyle::State_MouseOver
;
372 else if ((category
== hoveredCategory
) && mouseButtonPressed
)
374 QPoint initialPressPosition
= listView
->viewport()->mapFromGlobal(QCursor::pos());
375 initialPressPosition
.setY(initialPressPosition
.y() + listView
->verticalOffset());
376 initialPressPosition
.setX(initialPressPosition
.x() + listView
->horizontalOffset());
378 if (initialPressPosition
== this->initialPressPosition
)
380 optionCopy
.state
|= QStyle::State_Selected
;
384 categoryDrawer
->drawCategory(index
,
391 void KCategorizedView::Private::updateScrollbars()
393 // find the last index in the last category
394 QModelIndex lastIndex
= categoriesIndexes
.isEmpty() ? QModelIndex() : categoriesIndexes
[categories
.last()].last();
396 int lastItemBottom
= cachedRectIndex(lastIndex
).top() +
397 listView
->spacing() + (listView
->gridSize().isEmpty() ? 0 : listView
->gridSize().height()) - listView
->viewport()->height();
399 listView
->horizontalScrollBar()->setRange(0, 0);
401 listView
->verticalScrollBar()->setSingleStep(listView
->viewport()->height() / 10);
402 listView
->verticalScrollBar()->setPageStep(listView
->viewport()->height());
403 listView
->verticalScrollBar()->setRange(0, lastItemBottom
);
406 void KCategorizedView::Private::drawDraggedItems(QPainter
*painter
)
408 QStyleOptionViewItemV3 option
= listView
->viewOptions();
409 option
.state
&= ~QStyle::State_MouseOver
;
410 foreach (const QModelIndex
&index
, listView
->selectionModel()->selectedIndexes())
412 const int dx
= mousePosition
.x() - initialPressPosition
.x() + listView
->horizontalOffset();
413 const int dy
= mousePosition
.y() - initialPressPosition
.y() + listView
->verticalOffset();
415 option
.rect
= visualRect(index
);
416 option
.rect
.adjust(dx
, dy
, dx
, dy
);
418 if (option
.rect
.intersects(listView
->viewport()->rect()))
420 listView
->itemDelegate(index
)->paint(painter
, option
, index
);
425 void KCategorizedView::Private::layoutChanged(bool forceItemReload
)
427 if ((listView
->viewMode() == KCategorizedView::IconMode
) && proxyModel
&&
428 categoryDrawer
&& proxyModel
->isCategorizedModel() &&
430 (modelSortRole
!= proxyModel
->sortRole()) ||
431 (modelSortColumn
!= proxyModel
->sortColumn()) ||
432 (modelSortOrder
!= proxyModel
->sortOrder()) ||
433 (modelLastRowCount
!= proxyModel
->rowCount()) ||
434 (modelCategorized
!= proxyModel
->isCategorizedModel()))))
436 // Force the view to update all elements
437 listView
->rowsInsertedArtifficial(QModelIndex(), 0, proxyModel
->rowCount() - 1);
439 if (!forceItemReload
)
441 modelSortRole
= proxyModel
->sortRole();
442 modelSortColumn
= proxyModel
->sortColumn();
443 modelSortOrder
= proxyModel
->sortOrder();
444 modelLastRowCount
= proxyModel
->rowCount();
445 modelCategorized
= proxyModel
->isCategorizedModel();
448 else if ((listView
->viewMode() == KCategorizedView::IconMode
) && proxyModel
&&
449 categoryDrawer
&& proxyModel
->isCategorizedModel())
455 void KCategorizedView::Private::drawDraggedItems()
459 foreach (const QModelIndex
&index
, listView
->selectionModel()->selectedIndexes())
461 int dx
= mousePosition
.x() - initialPressPosition
.x() + listView
->horizontalOffset();
462 int dy
= mousePosition
.y() - initialPressPosition
.y() + listView
->verticalOffset();
464 currentRect
= visualRect(index
);
465 currentRect
.adjust(dx
, dy
, dx
, dy
);
467 if (currentRect
.intersects(listView
->viewport()->rect()))
469 rectToUpdate
= rectToUpdate
.united(currentRect
);
473 listView
->viewport()->update(lastDraggedItemsRect
.united(rectToUpdate
));
475 lastDraggedItemsRect
= rectToUpdate
;
479 //==============================================================================
482 KCategorizedView::KCategorizedView(QWidget
*parent
)
484 , d(new Private(this))
488 KCategorizedView::~KCategorizedView()
493 void KCategorizedView::setGridSize(const QSize
&size
)
495 QListView::setGridSize(size
);
497 d
->layoutChanged(true);
500 void KCategorizedView::setModel(QAbstractItemModel
*model
)
502 d
->lastSelection
= QItemSelection();
503 d
->forcedSelectionPosition
= 0;
504 d
->elementsInfo
.clear();
505 d
->elementsPosition
.clear();
506 d
->categoriesIndexes
.clear();
507 d
->categoriesPosition
.clear();
508 d
->categories
.clear();
509 d
->intersectedIndexes
.clear();
510 d
->modelIndexList
.clear();
511 d
->hovered
= QModelIndex();
512 d
->mouseButtonPressed
= false;
513 d
->rightMouseButtonPressed
= false;
517 QObject::disconnect(d
->proxyModel
,
518 SIGNAL(layoutChanged()),
519 this, SLOT(slotLayoutChanged()));
521 QObject::disconnect(d
->proxyModel
,
522 SIGNAL(dataChanged(QModelIndex
,QModelIndex
)),
523 this, SLOT(slotLayoutChanged()));
525 QObject::disconnect(d
->proxyModel
,
526 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
527 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
530 QListView::setModel(model
);
532 d
->proxyModel
= dynamic_cast<KCategorizedSortFilterProxyModel
*>(model
);
536 d
->modelSortRole
= d
->proxyModel
->sortRole();
537 d
->modelSortColumn
= d
->proxyModel
->sortColumn();
538 d
->modelSortOrder
= d
->proxyModel
->sortOrder();
539 d
->modelLastRowCount
= d
->proxyModel
->rowCount();
540 d
->modelCategorized
= d
->proxyModel
->isCategorizedModel();
542 QObject::connect(d
->proxyModel
,
543 SIGNAL(layoutChanged()),
544 this, SLOT(slotLayoutChanged()));
546 QObject::connect(d
->proxyModel
,
547 SIGNAL(dataChanged(QModelIndex
,QModelIndex
)),
548 this, SLOT(slotLayoutChanged()));
550 QObject::connect(d
->proxyModel
,
551 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
552 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
554 if (d
->proxyModel
->rowCount())
556 d
->layoutChanged(true);
561 d
->modelCategorized
= false;
565 QRect
KCategorizedView::visualRect(const QModelIndex
&index
) const
567 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
568 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
570 return QListView::visualRect(index
);
573 if (!qobject_cast
<const QSortFilterProxyModel
*>(index
.model()))
575 return d
->visualRect(d
->proxyModel
->mapFromSource(index
));
578 return d
->visualRect(index
);
581 KCategoryDrawer
*KCategorizedView::categoryDrawer() const
583 return d
->categoryDrawer
;
586 void KCategorizedView::setCategoryDrawer(KCategoryDrawer
*categoryDrawer
)
588 d
->lastSelection
= QItemSelection();
589 d
->forcedSelectionPosition
= 0;
590 d
->elementsInfo
.clear();
591 d
->elementsPosition
.clear();
592 d
->categoriesIndexes
.clear();
593 d
->categoriesPosition
.clear();
594 d
->categories
.clear();
595 d
->intersectedIndexes
.clear();
596 d
->modelIndexList
.clear();
597 d
->hovered
= QModelIndex();
598 d
->mouseButtonPressed
= false;
599 d
->rightMouseButtonPressed
= false;
601 if (!categoryDrawer
&& d
->proxyModel
)
603 QObject::disconnect(d
->proxyModel
,
604 SIGNAL(layoutChanged()),
605 this, SLOT(slotLayoutChanged()));
607 QObject::disconnect(d
->proxyModel
,
608 SIGNAL(dataChanged(QModelIndex
,QModelIndex
)),
609 this, SLOT(slotLayoutChanged()));
611 QObject::disconnect(d
->proxyModel
,
612 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
613 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
615 else if (categoryDrawer
&& d
->proxyModel
)
617 QObject::connect(d
->proxyModel
,
618 SIGNAL(layoutChanged()),
619 this, SLOT(slotLayoutChanged()));
621 QObject::connect(d
->proxyModel
,
622 SIGNAL(dataChanged(QModelIndex
,QModelIndex
)),
623 this, SLOT(slotLayoutChanged()));
625 QObject::connect(d
->proxyModel
,
626 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
627 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
630 d
->categoryDrawer
= categoryDrawer
;
636 if (d
->proxyModel
->rowCount())
638 d
->layoutChanged(true);
648 QModelIndex
KCategorizedView::indexAt(const QPoint
&point
) const
650 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
651 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
653 return QListView::indexAt(point
);
658 QModelIndexList item
= d
->intersectionSet(QRect(point
, point
));
660 if (item
.count() == 1)
668 void KCategorizedView::reset()
672 d
->lastSelection
= QItemSelection();
673 d
->forcedSelectionPosition
= 0;
674 d
->elementsInfo
.clear();
675 d
->elementsPosition
.clear();
676 d
->categoriesIndexes
.clear();
677 d
->categoriesPosition
.clear();
678 d
->categories
.clear();
679 d
->intersectedIndexes
.clear();
680 d
->modelIndexList
.clear();
681 d
->hovered
= QModelIndex();
682 d
->biggestItemSize
= QSize(0, 0);
683 d
->mouseButtonPressed
= false;
684 d
->rightMouseButtonPressed
= false;
687 void KCategorizedView::paintEvent(QPaintEvent
*event
)
689 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
690 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
692 QListView::paintEvent(event
);
696 QStyleOptionViewItemV3 option
= viewOptions();
697 option
.widget
= this;
700 option
.features
|= QStyleOptionViewItemV2::WrapText
;
703 QPainter
painter(viewport());
704 QRect area
= event
->rect();
705 const bool focus
= (hasFocus() || viewport()->hasFocus()) &&
706 currentIndex().isValid();
707 const QStyle::State state
= option
.state
;
708 const bool enabled
= (state
& QStyle::State_Enabled
) != 0;
712 QModelIndexList dirtyIndexes
= d
->intersectionSet(area
);
713 foreach (const QModelIndex
&index
, dirtyIndexes
)
715 option
.state
= state
;
716 option
.rect
= visualRect(index
);
718 if (selectionModel() && selectionModel()->isSelected(index
))
720 option
.state
|= QStyle::State_Selected
;
725 QPalette::ColorGroup cg
;
726 if ((d
->proxyModel
->flags(index
) & Qt::ItemIsEnabled
) == 0)
728 option
.state
&= ~QStyle::State_Enabled
;
729 cg
= QPalette::Disabled
;
733 cg
= QPalette::Normal
;
735 option
.palette
.setCurrentColorGroup(cg
);
738 if (focus
&& currentIndex() == index
)
740 option
.state
|= QStyle::State_HasFocus
;
741 if (this->state() == EditingState
)
742 option
.state
|= QStyle::State_Editing
;
745 // we are only interested to give the mouse over feedback when no
746 // dragging is happening (ereslibre)
747 if ((index
== d
->hovered
) && !d
->mouseButtonPressed
&&
748 (this->state() == QAbstractItemView::NoState
))
749 option
.state
|= QStyle::State_MouseOver
;
751 option
.state
&= ~QStyle::State_MouseOver
;
753 itemDelegate(index
)->paint(&painter
, option
, index
);
757 QStyleOptionViewItem otherOption
;
758 bool intersectedInThePast
= false;
759 foreach (const QString
&category
, d
->categories
)
761 otherOption
= option
;
762 otherOption
.rect
= d
->categoryVisualRect(category
);
763 otherOption
.state
&= ~QStyle::State_MouseOver
;
765 if (otherOption
.rect
.intersects(area
))
767 intersectedInThePast
= true;
769 QModelIndex indexToDraw
= d
->proxyModel
->index(d
->categoriesIndexes
[category
][0].row(), d
->proxyModel
->sortColumn());
771 d
->drawNewCategory(indexToDraw
,
772 d
->proxyModel
->sortRole(), otherOption
, &painter
);
774 else if (intersectedInThePast
)
776 break; // the visible area has been finished, we don't need to keep asking, the rest won't intersect
777 // this is doable because we know that categories are correctly ordered on the list
781 if (d
->mouseButtonPressed
&& !d
->isDragging
)
783 QPoint start
, end
, initialPressPosition
;
785 initialPressPosition
= d
->initialPressPosition
;
787 initialPressPosition
.setY(initialPressPosition
.y() - verticalOffset());
788 initialPressPosition
.setX(initialPressPosition
.x() - horizontalOffset());
790 if (d
->initialPressPosition
.x() > d
->mousePosition
.x() ||
791 d
->initialPressPosition
.y() > d
->mousePosition
.y())
793 start
= d
->mousePosition
;
794 end
= initialPressPosition
;
798 start
= initialPressPosition
;
799 end
= d
->mousePosition
;
802 QStyleOptionRubberBand yetAnotherOption
;
803 yetAnotherOption
.initFrom(this);
804 yetAnotherOption
.shape
= QRubberBand::Rectangle
;
805 yetAnotherOption
.opaque
= false;
806 yetAnotherOption
.rect
= QRect(start
, end
).intersected(viewport()->rect().adjusted(-16, -16, 16, 16));
808 style()->drawControl(QStyle::CE_RubberBand
, &yetAnotherOption
, &painter
);
812 if (d
->isDragging
&& !d
->dragLeftViewport
)
814 painter
.setOpacity(0.5);
815 d
->drawDraggedItems(&painter
);
821 void KCategorizedView::resizeEvent(QResizeEvent
*event
)
823 QListView::resizeEvent(event
);
825 // Clear the items positions cache
826 d
->elementsPosition
.clear();
827 d
->categoriesPosition
.clear();
828 d
->forcedSelectionPosition
= 0;
830 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
831 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
836 d
->updateScrollbars();
839 void KCategorizedView::setSelection(const QRect
&rect
,
840 QItemSelectionModel::SelectionFlags flags
)
842 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
843 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
845 QListView::setSelection(rect
, flags
);
852 if (flags
& QItemSelectionModel::Clear
)
854 selectionModel()->clear();
855 d
->lastSelection
.clear();
858 QModelIndexList dirtyIndexes
= d
->intersectionSet(rect
);
860 // no items affected, just leave
861 if (!dirtyIndexes
.count())
863 selectionModel()->select(d
->lastSelection
, QItemSelectionModel::SelectCurrent
);
869 QModelIndex bottomRight
;
871 if (d
->mouseButtonPressed
|| d
->rightMouseButtonPressed
) // selection with click + drag
873 QItemSelection selection
;
875 QModelIndex prev
= dirtyIndexes
[0];
876 QModelIndex first
= prev
;
877 foreach (const QModelIndex
&index
, dirtyIndexes
)
879 // we have a different interval. non-contiguous items
880 if ((index
.row() - prev
.row()) > 1) {
881 selection
<< QItemSelectionRange(first
, prev
);
889 selection
<< QItemSelectionRange(first
, prev
);
891 if (flags
& QItemSelectionModel::Current
)
893 if (rect
.topLeft() == rect
.bottomRight())
895 selectionModel()->setCurrentIndex(indexAt(rect
.topLeft()), QItemSelectionModel::NoUpdate
);
898 selection
.merge(d
->lastSelection
, flags
);
902 selection
.merge(selectionModel()->selection(), flags
);
904 selectionModel()->select(selection
, QItemSelectionModel::SelectCurrent
);
909 selectionModel()->select(selection
, flags
);
911 else // selection with click + keyboard keys
913 QModelIndex topLeftIndex
= indexAt(QPoint(rect
.topLeft().x(),
914 rect
.topLeft().y()));
915 QModelIndex bottomRightIndex
= indexAt(QPoint(rect
.bottomRight().x(),
916 rect
.bottomRight().y()));
918 // keyboard selection comes "upside down". Let's normalize it
919 if (topLeftIndex
.row() > bottomRightIndex
.row())
921 QModelIndex auxIndex
= topLeftIndex
;
922 topLeftIndex
= bottomRightIndex
;
923 bottomRightIndex
= auxIndex
;
926 int viewportWidth
= viewport()->width() - spacing();
929 if (gridSize().isEmpty())
931 itemWidth
= d
->biggestItemSize
.width();
935 itemWidth
= gridSize().width();
938 int itemWidthPlusSeparation
= spacing() + itemWidth
;
939 int elementsPerRow
= viewportWidth
/ itemWidthPlusSeparation
;
943 QModelIndexList
theoricDirty(dirtyIndexes
);
944 dirtyIndexes
.clear();
945 int first
= model()->rowCount();
948 foreach (const QModelIndex
&index
, theoricDirty
)
950 if ((index
.row() < first
) &&
951 ((((topLeftIndex
.row() / elementsPerRow
) == (index
.row() / elementsPerRow
)) &&
952 ((topLeftIndex
.row() % elementsPerRow
) <= (index
.row() % elementsPerRow
))) ||
953 (topLeftIndex
.row() / elementsPerRow
) != (index
.row() / elementsPerRow
)))
959 if ((index
.row() > last
) &&
960 ((((bottomRightIndex
.row() / elementsPerRow
) == (index
.row() / elementsPerRow
)) &&
961 ((bottomRightIndex
.row() % elementsPerRow
) >= (index
.row() % elementsPerRow
))) ||
962 (bottomRightIndex
.row() / elementsPerRow
) != (index
.row() / elementsPerRow
)))
969 for (int i
= first
; i
<= last
; i
++)
971 dirtyIndexes
<< model()->index(i
, theoricDirty
[0].column(), theoricDirty
[0].parent());
974 QItemSelection
selection(topLeft
, bottomRight
);
976 selectionModel()->select(selection
, flags
);
980 void KCategorizedView::mouseMoveEvent(QMouseEvent
*event
)
982 QListView::mouseMoveEvent(event
);
984 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
985 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
990 QModelIndexList item
= d
->intersectionSet(QRect(event
->pos(), event
->pos()));
992 if (item
.count() == 1)
994 d
->hovered
= item
[0];
998 d
->hovered
= QModelIndex();
1001 const QString previousHoveredCategory
= d
->hoveredCategory
;
1003 d
->mousePosition
= event
->pos();
1004 d
->hoveredCategory
= QString();
1006 // Redraw categories
1007 foreach (const QString
&category
, d
->categories
)
1009 if (d
->categoryVisualRect(category
).intersects(QRect(event
->pos(), event
->pos())))
1011 d
->hoveredCategory
= category
;
1012 viewport()->update(d
->categoryVisualRect(category
));
1014 else if ((category
== previousHoveredCategory
) &&
1015 (!d
->categoryVisualRect(previousHoveredCategory
).intersects(QRect(event
->pos(), event
->pos()))))
1017 viewport()->update(d
->categoryVisualRect(category
));
1022 if (d
->mouseButtonPressed
&& !d
->isDragging
)
1024 QPoint start
, end
, initialPressPosition
;
1026 initialPressPosition
= d
->initialPressPosition
;
1028 initialPressPosition
.setY(initialPressPosition
.y() - verticalOffset());
1029 initialPressPosition
.setX(initialPressPosition
.x() - horizontalOffset());
1031 if (d
->initialPressPosition
.x() > d
->mousePosition
.x() ||
1032 d
->initialPressPosition
.y() > d
->mousePosition
.y())
1034 start
= d
->mousePosition
;
1035 end
= initialPressPosition
;
1039 start
= initialPressPosition
;
1040 end
= d
->mousePosition
;
1043 rect
= QRect(start
, end
).adjusted(-16, -16, 16, 16);
1044 rect
= rect
.united(QRect(start
, end
).adjusted(16, 16, -16, -16)).intersected(viewport()->rect());
1046 viewport()->update(rect
);
1050 void KCategorizedView::mousePressEvent(QMouseEvent
*event
)
1052 d
->dragLeftViewport
= false;
1054 if (event
->button() == Qt::LeftButton
)
1056 d
->mouseButtonPressed
= true;
1058 d
->initialPressPosition
= event
->pos();
1059 d
->initialPressPosition
.setY(d
->initialPressPosition
.y() +
1061 d
->initialPressPosition
.setX(d
->initialPressPosition
.x() +
1062 horizontalOffset());
1064 else if (event
->button() == Qt::RightButton
)
1066 d
->rightMouseButtonPressed
= true;
1069 QListView::mousePressEvent(event
);
1071 d
->lastSelection
= selectionModel()->selection();
1073 viewport()->update(d
->categoryVisualRect(d
->hoveredCategory
));
1076 void KCategorizedView::mouseReleaseEvent(QMouseEvent
*event
)
1078 d
->mouseButtonPressed
= false;
1079 d
->rightMouseButtonPressed
= false;
1081 QListView::mouseReleaseEvent(event
);
1083 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
1084 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
1089 QPoint initialPressPosition
= viewport()->mapFromGlobal(QCursor::pos());
1090 initialPressPosition
.setY(initialPressPosition
.y() + verticalOffset());
1091 initialPressPosition
.setX(initialPressPosition
.x() + horizontalOffset());
1093 if (initialPressPosition
== d
->initialPressPosition
)
1095 foreach(const QString
&category
, d
->categories
)
1097 if (d
->categoryVisualRect(category
).contains(event
->pos()))
1099 QItemSelection selection
;
1100 QModelIndexList indexList
= d
->categoriesIndexes
[category
];
1102 foreach (const QModelIndex
&index
, indexList
)
1104 QModelIndex selectIndex
= index
.model()->index(index
.row(), 0);
1106 selection
<< QItemSelectionRange(selectIndex
);
1109 selectionModel()->select(selection
, QItemSelectionModel::Select
);
1119 QPoint start
, end
, initialPressPosition
;
1121 initialPressPosition
= d
->initialPressPosition
;
1123 initialPressPosition
.setY(initialPressPosition
.y() - verticalOffset());
1124 initialPressPosition
.setX(initialPressPosition
.x() - horizontalOffset());
1126 if (d
->initialPressPosition
.x() > d
->mousePosition
.x() ||
1127 d
->initialPressPosition
.y() > d
->mousePosition
.y())
1129 start
= d
->mousePosition
;
1130 end
= initialPressPosition
;
1134 start
= initialPressPosition
;
1135 end
= d
->mousePosition
;
1138 rect
= QRect(start
, end
).adjusted(-16, -16, 16, 16);
1139 rect
= rect
.united(QRect(start
, end
).adjusted(16, 16, -16, -16)).intersected(viewport()->rect());
1141 viewport()->update(rect
);
1144 if (d
->hovered
.isValid())
1145 viewport()->update(visualRect(d
->hovered
));
1146 else if (!d
->hoveredCategory
.isEmpty())
1147 viewport()->update(d
->categoryVisualRect(d
->hoveredCategory
));
1150 void KCategorizedView::leaveEvent(QEvent
*event
)
1152 d
->hovered
= QModelIndex();
1153 d
->hoveredCategory
= QString();
1155 QListView::leaveEvent(event
);
1158 void KCategorizedView::startDrag(Qt::DropActions supportedActions
)
1160 // FIXME: QAbstractItemView does far better here since it sets the
1161 // pixmap of selected icons to the dragging cursor, but it sets a non
1162 // ARGB window so it is no transparent. Use QAbstractItemView when
1163 // this is fixed on Qt.
1164 // QAbstractItemView::startDrag(supportedActions);
1165 #if !defined(DOLPHIN_DRAGANDDROP)
1166 QListView::startDrag(supportedActions
);
1169 d
->isDragging
= false;
1170 d
->mouseButtonPressed
= false;
1171 d
->rightMouseButtonPressed
= false;
1173 viewport()->update(d
->lastDraggedItemsRect
);
1176 void KCategorizedView::dragMoveEvent(QDragMoveEvent
*event
)
1178 d
->mousePosition
= event
->pos();
1180 if (d
->mouseButtonPressed
)
1182 d
->isDragging
= true;
1186 d
->isDragging
= false;
1189 d
->dragLeftViewport
= false;
1191 #if defined(DOLPHIN_DRAGANDDROP)
1192 QAbstractItemView::dragMoveEvent(event
);
1194 QListView::dragMoveEvent(event
);
1197 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
1198 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
1203 d
->drawDraggedItems();
1206 void KCategorizedView::dragLeaveEvent(QDragLeaveEvent
*event
)
1208 d
->dragLeftViewport
= true;
1210 #if defined(DOLPHIN_DRAGANDDROP)
1211 QAbstractItemView::dragLeaveEvent(event
);
1213 QListView::dragLeaveEvent(event
);
1217 void KCategorizedView::dropEvent(QDropEvent
*event
)
1219 #if defined(DOLPHIN_DRAGANDDROP)
1220 QAbstractItemView::dropEvent(event
);
1222 QListView::dropEvent(event
);
1226 QModelIndex
KCategorizedView::moveCursor(CursorAction cursorAction
,
1227 Qt::KeyboardModifiers modifiers
)
1229 if ((viewMode() != KCategorizedView::IconMode
) ||
1231 !d
->categoryDrawer
||
1232 d
->categories
.isEmpty() ||
1233 !d
->proxyModel
->isCategorizedModel()
1236 return QListView::moveCursor(cursorAction
, modifiers
);
1239 QModelIndex current
= selectionModel()->currentIndex();
1241 if (!current
.isValid())
1243 current
= model()->index(0, 0, QModelIndex());
1244 selectionModel()->select(current
, QItemSelectionModel::NoUpdate
);
1245 d
->forcedSelectionPosition
= 0;
1250 int viewportWidth
= viewport()->width() - spacing();
1253 if (gridSize().isEmpty())
1255 itemWidth
= d
->biggestItemSize
.width();
1259 itemWidth
= gridSize().width();
1262 int itemWidthPlusSeparation
= spacing() + itemWidth
;
1263 int elementsPerRow
= viewportWidth
/ itemWidthPlusSeparation
;
1264 if (!elementsPerRow
)
1267 QString lastCategory
= d
->categories
.first();
1268 QString theCategory
= d
->categories
.first();
1269 QString afterCategory
= d
->categories
.first();
1271 bool hasToBreak
= false;
1272 foreach (const QString
&category
, d
->categories
)
1276 afterCategory
= category
;
1281 if (category
== d
->elementsInfo
[current
.row()].category
)
1283 theCategory
= category
;
1290 lastCategory
= category
;
1294 switch (cursorAction
)
1296 case QAbstractItemView::MoveUp
: {
1297 if (d
->elementsInfo
[current
.row()].relativeOffsetToCategory
>= elementsPerRow
)
1299 int indexToMove
= current
.row();
1300 indexToMove
-= qMin(((d
->elementsInfo
[current
.row()].relativeOffsetToCategory
) + d
->forcedSelectionPosition
), elementsPerRow
- d
->forcedSelectionPosition
+ (d
->elementsInfo
[current
.row()].relativeOffsetToCategory
% elementsPerRow
));
1302 return d
->proxyModel
->index(indexToMove
, 0);
1306 int lastCategoryLastRow
= (d
->categoriesIndexes
[lastCategory
].count() - 1) % elementsPerRow
;
1307 int indexToMove
= current
.row() - d
->elementsInfo
[current
.row()].relativeOffsetToCategory
;
1309 if (d
->forcedSelectionPosition
>= lastCategoryLastRow
)
1315 indexToMove
-= qMin((lastCategoryLastRow
- d
->forcedSelectionPosition
+ 1), d
->forcedSelectionPosition
+ elementsPerRow
+ 1);
1318 return d
->proxyModel
->index(indexToMove
, 0);
1322 case QAbstractItemView::MoveDown
: {
1323 if (d
->elementsInfo
[current
.row()].relativeOffsetToCategory
< (d
->categoriesIndexes
[theCategory
].count() - 1 - ((d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
)))
1325 int indexToMove
= current
.row();
1326 indexToMove
+= qMin(elementsPerRow
, d
->categoriesIndexes
[theCategory
].count() - 1 - d
->elementsInfo
[current
.row()].relativeOffsetToCategory
);
1328 return d
->proxyModel
->index(indexToMove
, 0);
1332 int afterCategoryLastRow
= qMin(elementsPerRow
, d
->categoriesIndexes
[afterCategory
].count());
1333 int indexToMove
= current
.row() + (d
->categoriesIndexes
[theCategory
].count() - d
->elementsInfo
[current
.row()].relativeOffsetToCategory
);
1335 if (d
->forcedSelectionPosition
>= afterCategoryLastRow
)
1337 indexToMove
+= afterCategoryLastRow
- 1;
1341 indexToMove
+= qMin(d
->forcedSelectionPosition
, elementsPerRow
);
1344 return d
->proxyModel
->index(indexToMove
, 0);
1348 case QAbstractItemView::MoveLeft
:
1349 if (layoutDirection() == Qt::RightToLeft
)
1351 if (!(d
->elementsInfo
[current
.row() + 1].relativeOffsetToCategory
% elementsPerRow
))
1354 d
->forcedSelectionPosition
= d
->elementsInfo
[current
.row() + 1].relativeOffsetToCategory
% elementsPerRow
;
1356 #if 0 //follow qt view behavior. lateral movements won't change visual row
1357 if (d
->forcedSelectionPosition
< 0)
1358 d
->forcedSelectionPosition
= (d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
;
1361 return d
->proxyModel
->index(current
.row() + 1, 0);
1364 if (!(d
->elementsInfo
[current
.row()].relativeOffsetToCategory
% elementsPerRow
))
1367 d
->forcedSelectionPosition
= d
->elementsInfo
[current
.row() - 1].relativeOffsetToCategory
% elementsPerRow
;
1369 #if 0 //follow qt view behavior. lateral movements won't change visual row
1370 if (d
->forcedSelectionPosition
< 0)
1371 d
->forcedSelectionPosition
= (d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
;
1374 return d
->proxyModel
->index(current
.row() - 1, 0);
1376 case QAbstractItemView::MoveRight
:
1377 if (layoutDirection() == Qt::RightToLeft
)
1379 if (!(d
->elementsInfo
[current
.row()].relativeOffsetToCategory
% elementsPerRow
))
1382 d
->forcedSelectionPosition
= d
->elementsInfo
[current
.row() - 1].relativeOffsetToCategory
% elementsPerRow
;
1384 #if 0 //follow qt view behavior. lateral movements won't change visual row
1385 if (d
->forcedSelectionPosition
< 0)
1386 d
->forcedSelectionPosition
= (d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
;
1389 return d
->proxyModel
->index(current
.row() - 1, 0);
1392 if (!(d
->elementsInfo
[current
.row() + 1].relativeOffsetToCategory
% elementsPerRow
))
1395 d
->forcedSelectionPosition
= d
->elementsInfo
[current
.row() + 1].relativeOffsetToCategory
% elementsPerRow
;
1397 #if 0 //follow qt view behavior. lateral movements won't change visual row
1398 if (d
->forcedSelectionPosition
< 0)
1399 d
->forcedSelectionPosition
= (d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
;
1402 return d
->proxyModel
->index(current
.row() + 1, 0);
1408 return QListView::moveCursor(cursorAction
, modifiers
);
1411 void KCategorizedView::rowsInserted(const QModelIndex
&parent
,
1415 QListView::rowsInserted(parent
, start
, end
);
1417 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
1418 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
1420 d
->forcedSelectionPosition
= 0;
1421 d
->elementsInfo
.clear();
1422 d
->elementsPosition
.clear();
1423 d
->categoriesIndexes
.clear();
1424 d
->categoriesPosition
.clear();
1425 d
->categories
.clear();
1426 d
->intersectedIndexes
.clear();
1427 d
->modelIndexList
.clear();
1428 d
->hovered
= QModelIndex();
1429 d
->biggestItemSize
= QSize(0, 0);
1430 d
->mouseButtonPressed
= false;
1431 d
->rightMouseButtonPressed
= false;
1436 rowsInsertedArtifficial(parent
, start
, end
);
1439 void KCategorizedView::rowsInsertedArtifficial(const QModelIndex
&parent
,
1445 d
->forcedSelectionPosition
= 0;
1446 d
->elementsInfo
.clear();
1447 d
->elementsPosition
.clear();
1448 d
->categoriesIndexes
.clear();
1449 d
->categoriesPosition
.clear();
1450 d
->categories
.clear();
1451 d
->intersectedIndexes
.clear();
1452 d
->modelIndexList
.clear();
1453 d
->hovered
= QModelIndex();
1454 d
->biggestItemSize
= QSize(0, 0);
1455 d
->mouseButtonPressed
= false;
1456 d
->rightMouseButtonPressed
= false;
1458 if (start
> end
|| end
< 0 || start
< 0 || !d
->proxyModel
->rowCount())
1463 // Add all elements mapped to the source model and explore categories
1464 QString prevCategory
= d
->proxyModel
->data(d
->proxyModel
->index(0, d
->proxyModel
->sortColumn()), KCategorizedSortFilterProxyModel::CategoryDisplayRole
).toString();
1465 QString lastCategory
= prevCategory
;
1466 QModelIndexList modelIndexList
;
1467 struct Private::ElementInfo elementInfo
;
1469 for (int k
= 0; k
< d
->proxyModel
->rowCount(); ++k
)
1471 QModelIndex index
= d
->proxyModel
->index(k
, d
->proxyModel
->sortColumn());
1472 QModelIndex indexSize
= d
->proxyModel
->index(k
, 0);
1474 d
->biggestItemSize
= QSize(qMax(sizeHintForIndex(indexSize
).width(),
1475 d
->biggestItemSize
.width()),
1476 qMax(sizeHintForIndex(indexSize
).height(),
1477 d
->biggestItemSize
.height()));
1479 d
->modelIndexList
<< index
;
1481 lastCategory
= d
->proxyModel
->data(index
, KCategorizedSortFilterProxyModel::CategoryDisplayRole
).toString();
1483 elementInfo
.category
= lastCategory
;
1485 if (prevCategory
!= lastCategory
)
1488 d
->categoriesIndexes
.insert(prevCategory
, modelIndexList
);
1489 d
->categories
<< prevCategory
;
1490 modelIndexList
.clear();
1497 elementInfo
.relativeOffsetToCategory
= offset
;
1499 modelIndexList
<< index
;
1500 prevCategory
= lastCategory
;
1502 d
->elementsInfo
.insert(index
.row(), elementInfo
);
1505 d
->categoriesIndexes
.insert(prevCategory
, modelIndexList
);
1506 d
->categories
<< prevCategory
;
1508 d
->updateScrollbars();
1510 // FIXME: We need to safely save the last selection. This is on my TODO
1511 // list (ereslibre).
1512 selectionModel()->clear();
1515 void KCategorizedView::rowsRemoved(const QModelIndex
&parent
,
1519 if ((viewMode() == KCategorizedView::IconMode
) && d
->proxyModel
&&
1520 d
->categoryDrawer
&& d
->proxyModel
->isCategorizedModel())
1522 // Force the view to update all elements
1523 rowsInsertedArtifficial(QModelIndex(), 0, d
->proxyModel
->rowCount() - 1);
1527 void KCategorizedView::updateGeometries()
1529 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
1530 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
1532 QListView::updateGeometries();
1536 // Avoid QListView::updateGeometries(), since it will try to set another
1537 // range to our scroll bars, what we don't want (ereslibre)
1538 QAbstractItemView::updateGeometries();
1541 void KCategorizedView::slotLayoutChanged()
1546 void KCategorizedView::currentChanged(const QModelIndex
¤t
,
1547 const QModelIndex
&previous
)
1549 // We need to update the forcedSelectionPosition property in order to correctly
1550 // navigate after with keyboard using up & down keys
1552 int viewportWidth
= viewport()->width() - spacing();
1557 if (gridSize().isEmpty())
1559 itemHeight
= d
->biggestItemSize
.height();
1560 itemWidth
= d
->biggestItemSize
.width();
1564 itemHeight
= gridSize().height();
1565 itemWidth
= gridSize().width();
1568 int itemWidthPlusSeparation
= spacing() + itemWidth
;
1569 int elementsPerRow
= viewportWidth
/ itemWidthPlusSeparation
;
1570 if (!elementsPerRow
)
1573 d
->forcedSelectionPosition
= d
->elementsInfo
[current
.row()].relativeOffsetToCategory
% elementsPerRow
;
1575 QListView::currentChanged(current
, previous
);
1578 #include "kcategorizedview.moc"