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)
48 , dragLeftViewport(false)
53 KCategorizedView::Private::~Private()
57 const QModelIndexList
&KCategorizedView::Private::intersectionSet(const QRect
&rect
)
60 QRect indexVisualRect
;
62 intersectedIndexes
.clear();
66 if (listView
->gridSize().isEmpty())
68 itemHeight
= biggestItemSize
.height();
72 itemHeight
= listView
->gridSize().height();
75 // Lets find out where we should start
76 int top
= proxyModel
->rowCount() - 1;
78 int middle
= (top
+ bottom
) / 2;
81 middle
= (top
+ bottom
) / 2;
83 index
= proxyModel
->index(middle
, 0);
84 indexVisualRect
= visualRect(index
);
85 // We need the whole height (not only the visualRect). This will help us to update
86 // all needed indexes correctly (ereslibre)
87 indexVisualRect
.setHeight(indexVisualRect
.height() + (itemHeight
- indexVisualRect
.height()));
89 if (qMax(indexVisualRect
.topLeft().y(),
90 indexVisualRect
.bottomRight().y()) < qMin(rect
.topLeft().y(),
91 rect
.bottomRight().y()))
101 for (int i
= middle
; i
< proxyModel
->rowCount(); i
++)
103 index
= proxyModel
->index(i
, 0);
104 indexVisualRect
= visualRect(index
);
106 if (rect
.intersects(indexVisualRect
))
107 intersectedIndexes
.append(index
);
109 // If we passed next item, stop searching for hits
110 if (qMax(rect
.bottomRight().y(), rect
.topLeft().y()) <
111 qMin(indexVisualRect
.topLeft().y(),
112 indexVisualRect
.bottomRight().y()))
116 return intersectedIndexes
;
119 QRect
KCategorizedView::Private::visualRectInViewport(const QModelIndex
&index
) const
121 if (!index
.isValid())
124 QString curCategory
= elementsInfo
[index
.row()].category
;
128 if (listView
->layoutDirection() == Qt::LeftToRight
)
130 retRect
= QRect(listView
->spacing(), listView
->spacing() * 2 +
131 categoryDrawer
->categoryHeight(listView
->viewOptions()), 0, 0);
135 retRect
= QRect(listView
->viewport()->width() - listView
->spacing(), listView
->spacing() * 2 +
136 categoryDrawer
->categoryHeight(listView
->viewOptions()), 0, 0);
139 int viewportWidth
= listView
->viewport()->width() - listView
->spacing();
144 if (listView
->gridSize().isEmpty())
146 itemHeight
= biggestItemSize
.height();
147 itemWidth
= biggestItemSize
.width();
151 itemHeight
= listView
->gridSize().height();
152 itemWidth
= listView
->gridSize().width();
155 int itemWidthPlusSeparation
= listView
->spacing() + itemWidth
;
156 int elementsPerRow
= viewportWidth
/ itemWidthPlusSeparation
;
160 int column
= elementsInfo
[index
.row()].relativeOffsetToCategory
% elementsPerRow
;
161 int row
= elementsInfo
[index
.row()].relativeOffsetToCategory
/ elementsPerRow
;
163 if (listView
->layoutDirection() == Qt::LeftToRight
)
165 retRect
.setLeft(retRect
.left() + column
* listView
->spacing() +
170 retRect
.setLeft(retRect
.right() - column
* listView
->spacing() -
171 column
* itemWidth
- itemWidth
);
173 retRect
.setRight(retRect
.right() - column
* listView
->spacing() -
177 foreach (const QString
&category
, categories
)
179 if (category
== curCategory
)
182 float rows
= (float) ((float) categoriesIndexes
[category
].count() /
183 (float) elementsPerRow
);
185 int rowsInt
= categoriesIndexes
[category
].count() / elementsPerRow
;
187 if (rows
- trunc(rows
)) rowsInt
++;
189 retRect
.setTop(retRect
.top() +
190 (rowsInt
* itemHeight
) +
191 categoryDrawer
->categoryHeight(listView
->viewOptions()) +
192 listView
->spacing() * 2);
194 if (listView
->gridSize().isEmpty())
196 retRect
.setTop(retRect
.top() +
197 (rowsInt
* listView
->spacing()));
201 if (listView
->gridSize().isEmpty())
203 retRect
.setTop(retRect
.top() + row
* listView
->spacing() +
208 retRect
.setTop(retRect
.top() + (row
* itemHeight
));
211 retRect
.setWidth(itemWidth
);
213 QModelIndex heightIndex
= proxyModel
->index(index
.row(), 0);
214 if (listView
->gridSize().isEmpty())
216 retRect
.setHeight(listView
->sizeHintForIndex(heightIndex
).height());
220 retRect
.setHeight(qMin(listView
->sizeHintForIndex(heightIndex
).height(),
221 listView
->gridSize().height()));
227 QRect
KCategorizedView::Private::visualCategoryRectInViewport(const QString
&category
)
230 QRect
retRect(listView
->spacing(),
232 listView
->viewport()->width() - listView
->spacing() * 2,
235 if (!proxyModel
->rowCount() || !categories
.contains(category
))
238 QModelIndex index
= proxyModel
->index(0, 0, QModelIndex());
240 int viewportWidth
= listView
->viewport()->width() - listView
->spacing();
245 if (listView
->gridSize().isEmpty())
247 itemHeight
= biggestItemSize
.height();
248 itemWidth
= biggestItemSize
.width();
252 itemHeight
= listView
->gridSize().height();
253 itemWidth
= listView
->gridSize().width();
256 int itemWidthPlusSeparation
= listView
->spacing() + itemWidth
;
257 int elementsPerRow
= viewportWidth
/ itemWidthPlusSeparation
;
262 foreach (const QString
&itCategory
, categories
)
264 if (itCategory
== category
)
267 float rows
= (float) ((float) categoriesIndexes
[itCategory
].count() /
268 (float) elementsPerRow
);
269 int rowsInt
= categoriesIndexes
[itCategory
].count() / elementsPerRow
;
271 if (rows
- trunc(rows
)) rowsInt
++;
273 retRect
.setTop(retRect
.top() +
274 (rowsInt
* itemHeight
) +
275 categoryDrawer
->categoryHeight(listView
->viewOptions()) +
276 listView
->spacing() * 2);
278 if (listView
->gridSize().isEmpty())
280 retRect
.setTop(retRect
.top() +
281 (rowsInt
* listView
->spacing()));
285 retRect
.setHeight(categoryDrawer
->categoryHeight(listView
->viewOptions()));
290 // We're sure elementsPosition doesn't contain index
291 const QRect
&KCategorizedView::Private::cacheIndex(const QModelIndex
&index
)
293 QRect rect
= visualRectInViewport(index
);
294 elementsPosition
[index
.row()] = rect
;
296 return elementsPosition
[index
.row()];
299 // We're sure categoriesPosition doesn't contain category
300 const QRect
&KCategorizedView::Private::cacheCategory(const QString
&category
)
302 QRect rect
= visualCategoryRectInViewport(category
);
303 categoriesPosition
[category
] = rect
;
305 return categoriesPosition
[category
];
308 const QRect
&KCategorizedView::Private::cachedRectIndex(const QModelIndex
&index
)
310 if (elementsPosition
.contains(index
.row())) // If we have it cached
312 return elementsPosition
[index
.row()];
314 else // Otherwise, cache it
316 return cacheIndex(index
);
320 const QRect
&KCategorizedView::Private::cachedRectCategory(const QString
&category
)
322 if (categoriesPosition
.contains(category
)) // If we have it cached
324 return categoriesPosition
[category
];
326 else // Otherwise, cache it and
328 return cacheCategory(category
);
332 QRect
KCategorizedView::Private::visualRect(const QModelIndex
&index
)
334 QRect retRect
= cachedRectIndex(index
);
335 int dx
= -listView
->horizontalOffset();
336 int dy
= -listView
->verticalOffset();
337 retRect
.adjust(dx
, dy
, dx
, dy
);
342 QRect
KCategorizedView::Private::categoryVisualRect(const QString
&category
)
344 QRect retRect
= cachedRectCategory(category
);
345 int dx
= -listView
->horizontalOffset();
346 int dy
= -listView
->verticalOffset();
347 retRect
.adjust(dx
, dy
, dx
, dy
);
352 void KCategorizedView::Private::drawNewCategory(const QModelIndex
&index
,
354 const QStyleOption
&option
,
357 if (!index
.isValid())
362 QStyleOption optionCopy
= option
;
363 const QString category
= proxyModel
->data(index
, KCategorizedSortFilterProxyModel::CategoryDisplayRole
).toString();
365 optionCopy
.state
&= ~QStyle::State_Selected
;
367 if ((category
== hoveredCategory
) && !mouseButtonPressed
)
369 optionCopy
.state
|= QStyle::State_MouseOver
;
371 else if ((category
== hoveredCategory
) && mouseButtonPressed
)
373 QPoint initialPressPosition
= listView
->viewport()->mapFromGlobal(QCursor::pos());
374 initialPressPosition
.setY(initialPressPosition
.y() + listView
->verticalOffset());
375 initialPressPosition
.setX(initialPressPosition
.x() + listView
->horizontalOffset());
377 if (initialPressPosition
== this->initialPressPosition
)
379 optionCopy
.state
|= QStyle::State_Selected
;
383 categoryDrawer
->drawCategory(index
,
390 void KCategorizedView::Private::updateScrollbars()
392 // find the last index in the last category
393 QModelIndex lastIndex
= categoriesIndexes
.isEmpty() ? QModelIndex() : categoriesIndexes
[categories
.last()].last();
395 int lastItemBottom
= cachedRectIndex(lastIndex
).top() +
396 listView
->spacing() + (listView
->gridSize().isEmpty() ? 0 : listView
->gridSize().height()) - listView
->viewport()->height();
398 listView
->horizontalScrollBar()->setRange(0, 0);
400 listView
->verticalScrollBar()->setSingleStep(listView
->viewport()->height() / 10);
401 listView
->verticalScrollBar()->setPageStep(listView
->viewport()->height());
402 listView
->verticalScrollBar()->setRange(0, lastItemBottom
);
405 void KCategorizedView::Private::drawDraggedItems(QPainter
*painter
)
407 QStyleOptionViewItemV3 option
= listView
->viewOptions();
408 option
.state
&= ~QStyle::State_MouseOver
;
409 foreach (const QModelIndex
&index
, listView
->selectionModel()->selectedIndexes())
411 const int dx
= mousePosition
.x() - initialPressPosition
.x() + listView
->horizontalOffset();
412 const int dy
= mousePosition
.y() - initialPressPosition
.y() + listView
->verticalOffset();
414 option
.rect
= visualRect(index
);
415 option
.rect
.adjust(dx
, dy
, dx
, dy
);
417 if (option
.rect
.intersects(listView
->viewport()->rect()))
419 listView
->itemDelegate(index
)->paint(painter
, option
, index
);
424 void KCategorizedView::Private::layoutChanged(bool forceItemReload
)
426 if ((listView
->viewMode() == KCategorizedView::IconMode
) && proxyModel
&&
427 categoryDrawer
&& proxyModel
->isCategorizedModel() &&
429 (modelSortRole
!= proxyModel
->sortRole()) ||
430 (modelSortColumn
!= proxyModel
->sortColumn()) ||
431 (modelSortOrder
!= proxyModel
->sortOrder()) ||
432 (modelLastRowCount
!= proxyModel
->rowCount()) ||
433 (modelCategorized
!= proxyModel
->isCategorizedModel()))))
435 // Force the view to update all elements
436 listView
->rowsInsertedArtifficial(QModelIndex(), 0, proxyModel
->rowCount() - 1);
438 if (!forceItemReload
)
440 modelSortRole
= proxyModel
->sortRole();
441 modelSortColumn
= proxyModel
->sortColumn();
442 modelSortOrder
= proxyModel
->sortOrder();
443 modelLastRowCount
= proxyModel
->rowCount();
444 modelCategorized
= proxyModel
->isCategorizedModel();
447 else if ((listView
->viewMode() == KCategorizedView::IconMode
) && proxyModel
&&
448 categoryDrawer
&& proxyModel
->isCategorizedModel())
454 void KCategorizedView::Private::drawDraggedItems()
458 foreach (const QModelIndex
&index
, listView
->selectionModel()->selectedIndexes())
460 int dx
= mousePosition
.x() - initialPressPosition
.x() + listView
->horizontalOffset();
461 int dy
= mousePosition
.y() - initialPressPosition
.y() + listView
->verticalOffset();
463 currentRect
= visualRect(index
);
464 currentRect
.adjust(dx
, dy
, dx
, dy
);
466 if (currentRect
.intersects(listView
->viewport()->rect()))
468 rectToUpdate
= rectToUpdate
.united(currentRect
);
472 listView
->viewport()->update(lastDraggedItemsRect
.united(rectToUpdate
));
474 lastDraggedItemsRect
= rectToUpdate
;
478 //==============================================================================
481 KCategorizedView::KCategorizedView(QWidget
*parent
)
483 , d(new Private(this))
487 KCategorizedView::~KCategorizedView()
492 void KCategorizedView::setGridSize(const QSize
&size
)
494 QListView::setGridSize(size
);
496 d
->layoutChanged(true);
499 void KCategorizedView::setModel(QAbstractItemModel
*model
)
501 d
->lastSelection
= QItemSelection();
502 d
->currentViewIndex
= QModelIndex();
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;
516 QObject::disconnect(d
->proxyModel
,
517 SIGNAL(layoutChanged()),
518 this, SLOT(slotLayoutChanged()));
520 QObject::disconnect(d
->proxyModel
,
521 SIGNAL(dataChanged(QModelIndex
,QModelIndex
)),
522 this, SLOT(slotLayoutChanged()));
524 QObject::disconnect(d
->proxyModel
,
525 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
526 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
529 QListView::setModel(model
);
531 d
->proxyModel
= dynamic_cast<KCategorizedSortFilterProxyModel
*>(model
);
535 d
->modelSortRole
= d
->proxyModel
->sortRole();
536 d
->modelSortColumn
= d
->proxyModel
->sortColumn();
537 d
->modelSortOrder
= d
->proxyModel
->sortOrder();
538 d
->modelLastRowCount
= d
->proxyModel
->rowCount();
539 d
->modelCategorized
= d
->proxyModel
->isCategorizedModel();
541 QObject::connect(d
->proxyModel
,
542 SIGNAL(layoutChanged()),
543 this, SLOT(slotLayoutChanged()));
545 QObject::connect(d
->proxyModel
,
546 SIGNAL(dataChanged(QModelIndex
,QModelIndex
)),
547 this, SLOT(slotLayoutChanged()));
549 QObject::connect(d
->proxyModel
,
550 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
551 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
553 if (d
->proxyModel
->rowCount())
555 d
->layoutChanged(true);
560 d
->modelCategorized
= false;
564 QRect
KCategorizedView::visualRect(const QModelIndex
&index
) const
566 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
567 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
569 return QListView::visualRect(index
);
572 if (!qobject_cast
<const QSortFilterProxyModel
*>(index
.model()))
574 return d
->visualRect(d
->proxyModel
->mapFromSource(index
));
577 return d
->visualRect(index
);
580 KCategoryDrawer
*KCategorizedView::categoryDrawer() const
582 return d
->categoryDrawer
;
585 void KCategorizedView::setCategoryDrawer(KCategoryDrawer
*categoryDrawer
)
587 d
->lastSelection
= QItemSelection();
588 d
->currentViewIndex
= QModelIndex();
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;
600 if (!categoryDrawer
&& d
->proxyModel
)
602 QObject::disconnect(d
->proxyModel
,
603 SIGNAL(layoutChanged()),
604 this, SLOT(slotLayoutChanged()));
606 QObject::disconnect(d
->proxyModel
,
607 SIGNAL(dataChanged(QModelIndex
,QModelIndex
)),
608 this, SLOT(slotLayoutChanged()));
610 QObject::disconnect(d
->proxyModel
,
611 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
612 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
614 else if (categoryDrawer
&& d
->proxyModel
)
616 QObject::connect(d
->proxyModel
,
617 SIGNAL(layoutChanged()),
618 this, SLOT(slotLayoutChanged()));
620 QObject::connect(d
->proxyModel
,
621 SIGNAL(dataChanged(QModelIndex
,QModelIndex
)),
622 this, SLOT(slotLayoutChanged()));
624 QObject::connect(d
->proxyModel
,
625 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
626 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
629 d
->categoryDrawer
= categoryDrawer
;
635 if (d
->proxyModel
->rowCount())
637 d
->layoutChanged(true);
647 QModelIndex
KCategorizedView::indexAt(const QPoint
&point
) const
649 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
650 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
652 return QListView::indexAt(point
);
657 QModelIndexList item
= d
->intersectionSet(QRect(point
, point
));
659 if (item
.count() == 1)
669 void KCategorizedView::reset()
673 d
->lastSelection
= QItemSelection();
674 d
->currentViewIndex
= QModelIndex();
675 d
->forcedSelectionPosition
= 0;
676 d
->elementsInfo
.clear();
677 d
->elementsPosition
.clear();
678 d
->categoriesIndexes
.clear();
679 d
->categoriesPosition
.clear();
680 d
->categories
.clear();
681 d
->intersectedIndexes
.clear();
682 d
->modelIndexList
.clear();
683 d
->hovered
= QModelIndex();
684 d
->biggestItemSize
= QSize(0, 0);
685 d
->mouseButtonPressed
= false;
688 void KCategorizedView::paintEvent(QPaintEvent
*event
)
690 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
691 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
693 QListView::paintEvent(event
);
697 QStyleOptionViewItemV3 option
= viewOptions();
698 option
.palette
= palette(); // viewOptions() doesn't seem to return the correct palette
699 // if the app was already running and the color scheme was changed
701 option
.widget
= this;
704 option
.features
|= QStyleOptionViewItemV2::WrapText
;
707 QPainter
painter(viewport());
708 QRect area
= event
->rect();
709 const bool focus
= (hasFocus() || viewport()->hasFocus()) &&
710 currentIndex().isValid();
711 const QStyle::State state
= option
.state
;
712 const bool enabled
= (state
& QStyle::State_Enabled
) != 0;
716 QModelIndexList dirtyIndexes
= d
->intersectionSet(area
);
717 foreach (const QModelIndex
&index
, dirtyIndexes
)
719 option
.state
= state
;
720 option
.rect
= visualRect(index
);
722 if (selectionModel() && selectionModel()->isSelected(index
))
724 option
.state
|= QStyle::State_Selected
;
729 QPalette::ColorGroup cg
;
730 if ((d
->proxyModel
->flags(index
) & Qt::ItemIsEnabled
) == 0)
732 option
.state
&= ~QStyle::State_Enabled
;
733 cg
= QPalette::Disabled
;
737 cg
= QPalette::Normal
;
739 option
.palette
.setCurrentColorGroup(cg
);
742 if (focus
&& currentIndex() == index
)
744 option
.state
|= QStyle::State_HasFocus
;
745 if (this->state() == EditingState
)
746 option
.state
|= QStyle::State_Editing
;
749 if ((index
== d
->hovered
) && !d
->mouseButtonPressed
)
750 option
.state
|= QStyle::State_MouseOver
;
752 option
.state
&= ~QStyle::State_MouseOver
;
754 itemDelegate(index
)->paint(&painter
, option
, index
);
758 QStyleOptionViewItem otherOption
;
759 bool intersectedInThePast
= false;
760 foreach (const QString
&category
, d
->categories
)
762 otherOption
= option
;
763 otherOption
.rect
= d
->categoryVisualRect(category
);
764 otherOption
.state
&= ~QStyle::State_MouseOver
;
766 if (otherOption
.rect
.intersects(area
))
768 intersectedInThePast
= true;
770 QModelIndex indexToDraw
= d
->proxyModel
->index(d
->categoriesIndexes
[category
][0].row(), d
->proxyModel
->sortColumn());
772 d
->drawNewCategory(indexToDraw
,
773 d
->proxyModel
->sortRole(), otherOption
, &painter
);
775 else if (intersectedInThePast
)
777 break; // the visible area has been finished, we don't need to keep asking, the rest won't intersect
778 // this is doable because we know that categories are correctly ordered on the list
782 if (d
->mouseButtonPressed
&& !d
->isDragging
)
784 QPoint start
, end
, initialPressPosition
;
786 initialPressPosition
= d
->initialPressPosition
;
788 initialPressPosition
.setY(initialPressPosition
.y() - verticalOffset());
789 initialPressPosition
.setX(initialPressPosition
.x() - horizontalOffset());
791 if (d
->initialPressPosition
.x() > d
->mousePosition
.x() ||
792 d
->initialPressPosition
.y() > d
->mousePosition
.y())
794 start
= d
->mousePosition
;
795 end
= initialPressPosition
;
799 start
= initialPressPosition
;
800 end
= d
->mousePosition
;
803 QStyleOptionRubberBand yetAnotherOption
;
804 yetAnotherOption
.initFrom(this);
805 yetAnotherOption
.shape
= QRubberBand::Rectangle
;
806 yetAnotherOption
.opaque
= false;
807 yetAnotherOption
.rect
= QRect(start
, end
).intersected(viewport()->rect().adjusted(-16, -16, 16, 16));
809 style()->drawControl(QStyle::CE_RubberBand
, &yetAnotherOption
, &painter
);
813 if (d
->isDragging
&& !d
->dragLeftViewport
)
815 painter
.setOpacity(0.5);
816 d
->drawDraggedItems(&painter
);
822 void KCategorizedView::resizeEvent(QResizeEvent
*event
)
824 QListView::resizeEvent(event
);
826 // Clear the items positions cache
827 d
->elementsPosition
.clear();
828 d
->categoriesPosition
.clear();
829 d
->forcedSelectionPosition
= 0;
831 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
832 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
837 d
->updateScrollbars();
840 void KCategorizedView::setSelection(const QRect
&rect
,
841 QItemSelectionModel::SelectionFlags flags
)
843 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
844 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
846 QListView::setSelection(rect
, flags
);
853 if (flags
& QItemSelectionModel::Clear
)
855 if (!rect
.intersects(d
->categoryVisualRect(d
->hoveredCategory
)))
857 d
->lastSelection
= QItemSelection();
861 selectionModel()->clear();
863 QModelIndexList dirtyIndexes
= d
->intersectionSet(rect
);
865 if (!dirtyIndexes
.count())
867 if (!d
->lastSelection
.isEmpty() &&
868 (rect
.intersects(d
->categoryVisualRect(d
->hoveredCategory
)) || d
->mouseButtonPressed
))
870 selectionModel()->select(d
->lastSelection
, flags
);
876 QItemSelection selection
;
878 if (!d
->mouseButtonPressed
)
880 selection
= QItemSelection(dirtyIndexes
[0], dirtyIndexes
[0]);
881 d
->currentViewIndex
= dirtyIndexes
[0];
885 QModelIndex first
= dirtyIndexes
[0];
887 foreach (const QModelIndex
&index
, dirtyIndexes
)
889 if (last
.isValid() && last
.row() + 1 != index
.row())
891 QItemSelectionRange
range(first
, last
);
902 selection
<< QItemSelectionRange(first
, last
);
905 if (d
->lastSelection
.count())
907 if ((selection
.count() == 1) && (selection
[0].indexes().count() == 1))
908 selection
.merge(d
->lastSelection
, flags
);
910 selection
.merge(d
->lastSelection
, QItemSelectionModel::Select
);
913 selectionModel()->select(selection
, flags
);
916 void KCategorizedView::mouseMoveEvent(QMouseEvent
*event
)
918 QListView::mouseMoveEvent(event
);
920 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
921 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
926 const QString previousHoveredCategory
= d
->hoveredCategory
;
928 d
->mousePosition
= event
->pos();
929 d
->hoveredCategory
= QString();
932 foreach (const QString
&category
, d
->categories
)
934 if (d
->categoryVisualRect(category
).intersects(QRect(event
->pos(), event
->pos())))
936 d
->hoveredCategory
= category
;
937 viewport()->update(d
->categoryVisualRect(category
));
939 else if ((category
== previousHoveredCategory
) &&
940 (!d
->categoryVisualRect(previousHoveredCategory
).intersects(QRect(event
->pos(), event
->pos()))))
942 viewport()->update(d
->categoryVisualRect(category
));
947 if (d
->mouseButtonPressed
&& !d
->isDragging
)
949 QPoint start
, end
, initialPressPosition
;
951 initialPressPosition
= d
->initialPressPosition
;
953 initialPressPosition
.setY(initialPressPosition
.y() - verticalOffset());
954 initialPressPosition
.setX(initialPressPosition
.x() - horizontalOffset());
956 if (d
->initialPressPosition
.x() > d
->mousePosition
.x() ||
957 d
->initialPressPosition
.y() > d
->mousePosition
.y())
959 start
= d
->mousePosition
;
960 end
= initialPressPosition
;
964 start
= initialPressPosition
;
965 end
= d
->mousePosition
;
968 rect
= QRect(start
, end
).intersected(viewport()->rect().adjusted(-16, -16, 16, 16));
970 //viewport()->update(rect.united(d->lastSelectionRect));
972 d
->lastSelectionRect
= rect
;
976 void KCategorizedView::mousePressEvent(QMouseEvent
*event
)
978 d
->dragLeftViewport
= false;
980 if (event
->button() == Qt::LeftButton
)
982 d
->mouseButtonPressed
= true;
984 d
->initialPressPosition
= event
->pos();
985 d
->initialPressPosition
.setY(d
->initialPressPosition
.y() +
987 d
->initialPressPosition
.setX(d
->initialPressPosition
.x() +
991 QListView::mousePressEvent(event
);
993 viewport()->update(d
->categoryVisualRect(d
->hoveredCategory
));
996 void KCategorizedView::mouseReleaseEvent(QMouseEvent
*event
)
998 d
->mouseButtonPressed
= false;
1000 QListView::mouseReleaseEvent(event
);
1002 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
1003 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
1008 QPoint initialPressPosition
= viewport()->mapFromGlobal(QCursor::pos());
1009 initialPressPosition
.setY(initialPressPosition
.y() + verticalOffset());
1010 initialPressPosition
.setX(initialPressPosition
.x() + horizontalOffset());
1012 QItemSelection selection
;
1013 QItemSelection deselection
;
1015 if (initialPressPosition
== d
->initialPressPosition
)
1017 foreach(const QString
&category
, d
->categories
)
1019 if (d
->categoryVisualRect(category
).contains(event
->pos()))
1021 foreach (const QModelIndex
&index
, d
->categoriesIndexes
[category
])
1023 QModelIndex selectIndex
= index
.model()->index(index
.row(), 0);
1025 if (!d
->lastSelection
.contains(selectIndex
))
1027 selection
<< QItemSelectionRange(selectIndex
);
1031 deselection
<< QItemSelectionRange(selectIndex
);
1035 selectionModel()->select(selection
, QItemSelectionModel::Select
);
1036 selectionModel()->select(deselection
, QItemSelectionModel::Deselect
);
1043 d
->lastSelection
= selectionModel()->selection();
1045 if (d
->hovered
.isValid())
1046 viewport()->update(visualRect(d
->hovered
));
1047 else if (!d
->hoveredCategory
.isEmpty())
1048 viewport()->update(d
->categoryVisualRect(d
->hoveredCategory
));
1051 void KCategorizedView::leaveEvent(QEvent
*event
)
1053 d
->hovered
= QModelIndex();
1054 d
->hoveredCategory
= QString();
1056 QListView::leaveEvent(event
);
1059 void KCategorizedView::startDrag(Qt::DropActions supportedActions
)
1061 // FIXME: QAbstractItemView does far better here since it sets the
1062 // pixmap of selected icons to the dragging cursor, but it sets a non
1063 // ARGB window so it is no transparent. Use QAbstractItemView when
1064 // this is fixed on Qt.
1065 // QAbstractItemView::startDrag(supportedActions);
1066 #if !defined(DOLPHIN_DRAGANDDROP)
1067 QListView::startDrag(supportedActions
);
1070 d
->isDragging
= false;
1071 d
->mouseButtonPressed
= false;
1073 viewport()->update(d
->lastDraggedItemsRect
);
1076 void KCategorizedView::dragMoveEvent(QDragMoveEvent
*event
)
1078 d
->mousePosition
= event
->pos();
1080 if (d
->mouseButtonPressed
)
1082 d
->isDragging
= true;
1086 d
->isDragging
= false;
1089 d
->dragLeftViewport
= false;
1091 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
1092 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
1094 #if defined(DOLPHIN_DRAGANDDROP)
1095 QAbstractItemView::dragMoveEvent(event
);
1097 QListView::dragMoveEvent(event
);
1102 d
->drawDraggedItems();
1105 void KCategorizedView::dragLeaveEvent(QDragLeaveEvent
*event
)
1107 d
->dragLeftViewport
= true;
1109 #if defined(DOLPHIN_DRAGANDDROP)
1110 QAbstractItemView::dragLeaveEvent(event
);
1112 QListView::dragLeaveEvent(event
);
1116 void KCategorizedView::dropEvent(QDropEvent
*event
)
1118 #if defined(DOLPHIN_DRAGANDDROP)
1119 QAbstractItemView::dropEvent(event
);
1121 QListView::dropEvent(event
);
1125 QModelIndex
KCategorizedView::moveCursor(CursorAction cursorAction
,
1126 Qt::KeyboardModifiers modifiers
)
1128 if ((viewMode() != KCategorizedView::IconMode
) ||
1130 !d
->categoryDrawer
||
1131 d
->categories
.isEmpty() ||
1132 !d
->proxyModel
->isCategorizedModel()
1135 return QListView::moveCursor(cursorAction
, modifiers
);
1138 const QModelIndex current
= selectionModel()->currentIndex();
1140 int viewportWidth
= viewport()->width() - spacing();
1143 if (gridSize().isEmpty())
1145 itemWidth
= d
->biggestItemSize
.width();
1149 itemWidth
= gridSize().width();
1152 int itemWidthPlusSeparation
= spacing() + itemWidth
;
1153 int elementsPerRow
= viewportWidth
/ itemWidthPlusSeparation
;
1155 QString lastCategory
= d
->categories
.first();
1156 QString theCategory
= d
->categories
.first();
1157 QString afterCategory
= d
->categories
.first();
1159 bool hasToBreak
= false;
1160 foreach (const QString
&category
, d
->categories
)
1164 afterCategory
= category
;
1169 if (category
== d
->elementsInfo
[current
.row()].category
)
1171 theCategory
= category
;
1178 lastCategory
= category
;
1182 switch (cursorAction
)
1184 case QAbstractItemView::MoveUp
: {
1185 if (d
->elementsInfo
[current
.row()].relativeOffsetToCategory
>= elementsPerRow
)
1187 int indexToMove
= current
.row();
1188 indexToMove
-= qMin(((d
->elementsInfo
[current
.row()].relativeOffsetToCategory
) + d
->forcedSelectionPosition
), elementsPerRow
- d
->forcedSelectionPosition
+ (d
->elementsInfo
[current
.row()].relativeOffsetToCategory
% elementsPerRow
));
1190 return d
->proxyModel
->index(indexToMove
, 0);
1194 int lastCategoryLastRow
= (d
->categoriesIndexes
[lastCategory
].count() - 1) % elementsPerRow
;
1195 int indexToMove
= current
.row() - d
->elementsInfo
[current
.row()].relativeOffsetToCategory
;
1197 if (d
->forcedSelectionPosition
>= lastCategoryLastRow
)
1203 indexToMove
-= qMin((lastCategoryLastRow
- d
->forcedSelectionPosition
+ 1), d
->forcedSelectionPosition
+ elementsPerRow
+ 1);
1206 return d
->proxyModel
->index(indexToMove
, 0);
1210 case QAbstractItemView::MoveDown
: {
1211 if (d
->elementsInfo
[current
.row()].relativeOffsetToCategory
< (d
->categoriesIndexes
[theCategory
].count() - 1 - ((d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
)))
1213 int indexToMove
= current
.row();
1214 indexToMove
+= qMin(elementsPerRow
, d
->categoriesIndexes
[theCategory
].count() - 1 - d
->elementsInfo
[current
.row()].relativeOffsetToCategory
);
1216 return d
->proxyModel
->index(indexToMove
, 0);
1220 int afterCategoryLastRow
= qMin(elementsPerRow
, d
->categoriesIndexes
[afterCategory
].count());
1221 int indexToMove
= current
.row() + (d
->categoriesIndexes
[theCategory
].count() - d
->elementsInfo
[current
.row()].relativeOffsetToCategory
);
1223 if (d
->forcedSelectionPosition
>= afterCategoryLastRow
)
1225 indexToMove
+= afterCategoryLastRow
- 1;
1229 indexToMove
+= qMin(d
->forcedSelectionPosition
, elementsPerRow
);
1232 return d
->proxyModel
->index(indexToMove
, 0);
1236 case QAbstractItemView::MoveLeft
:
1237 if (layoutDirection() == Qt::RightToLeft
)
1239 d
->forcedSelectionPosition
= d
->elementsInfo
[current
.row() + 1].relativeOffsetToCategory
% elementsPerRow
;
1241 if (d
->forcedSelectionPosition
< 0)
1242 d
->forcedSelectionPosition
= (d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
;
1244 return d
->proxyModel
->index(current
.row() + 1, 0);
1247 d
->forcedSelectionPosition
= d
->elementsInfo
[current
.row() - 1].relativeOffsetToCategory
% elementsPerRow
;
1249 if (d
->forcedSelectionPosition
< 0)
1250 d
->forcedSelectionPosition
= (d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
;
1252 return d
->proxyModel
->index(current
.row() - 1, 0);
1254 case QAbstractItemView::MoveRight
:
1255 if (layoutDirection() == Qt::RightToLeft
)
1257 d
->forcedSelectionPosition
= d
->elementsInfo
[current
.row() - 1].relativeOffsetToCategory
% elementsPerRow
;
1259 if (d
->forcedSelectionPosition
< 0)
1260 d
->forcedSelectionPosition
= (d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
;
1262 return d
->proxyModel
->index(current
.row() - 1, 0);
1265 d
->forcedSelectionPosition
= d
->elementsInfo
[current
.row() + 1].relativeOffsetToCategory
% elementsPerRow
;
1267 if (d
->forcedSelectionPosition
< 0)
1268 d
->forcedSelectionPosition
= (d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
;
1270 return d
->proxyModel
->index(current
.row() + 1, 0);
1276 return QListView::moveCursor(cursorAction
, modifiers
);
1279 void KCategorizedView::rowsInserted(const QModelIndex
&parent
,
1283 QListView::rowsInserted(parent
, start
, end
);
1285 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
1286 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
1288 d
->lastSelection
= QItemSelection();
1289 d
->currentViewIndex
= QModelIndex();
1290 d
->forcedSelectionPosition
= 0;
1291 d
->elementsInfo
.clear();
1292 d
->elementsPosition
.clear();
1293 d
->categoriesIndexes
.clear();
1294 d
->categoriesPosition
.clear();
1295 d
->categories
.clear();
1296 d
->intersectedIndexes
.clear();
1297 d
->modelIndexList
.clear();
1298 d
->hovered
= QModelIndex();
1299 d
->biggestItemSize
= QSize(0, 0);
1300 d
->mouseButtonPressed
= false;
1305 rowsInsertedArtifficial(parent
, start
, end
);
1308 void KCategorizedView::rowsInsertedArtifficial(const QModelIndex
&parent
,
1314 d
->lastSelection
= QItemSelection();
1315 d
->currentViewIndex
= QModelIndex();
1316 d
->forcedSelectionPosition
= 0;
1317 d
->elementsInfo
.clear();
1318 d
->elementsPosition
.clear();
1319 d
->categoriesIndexes
.clear();
1320 d
->categoriesPosition
.clear();
1321 d
->categories
.clear();
1322 d
->intersectedIndexes
.clear();
1323 d
->modelIndexList
.clear();
1324 d
->hovered
= QModelIndex();
1325 d
->biggestItemSize
= QSize(0, 0);
1326 d
->mouseButtonPressed
= false;
1328 if (start
> end
|| end
< 0 || start
< 0 || !d
->proxyModel
->rowCount())
1333 // Add all elements mapped to the source model and explore categories
1334 QString prevCategory
= d
->proxyModel
->data(d
->proxyModel
->index(0, d
->proxyModel
->sortColumn()), KCategorizedSortFilterProxyModel::CategoryDisplayRole
).toString();
1335 QString lastCategory
= prevCategory
;
1336 QModelIndexList modelIndexList
;
1337 struct Private::ElementInfo elementInfo
;
1339 for (int k
= 0; k
< d
->proxyModel
->rowCount(); ++k
)
1341 QModelIndex index
= d
->proxyModel
->index(k
, d
->proxyModel
->sortColumn());
1342 QModelIndex indexSize
= d
->proxyModel
->index(k
, 0);
1344 d
->biggestItemSize
= QSize(qMax(sizeHintForIndex(indexSize
).width(),
1345 d
->biggestItemSize
.width()),
1346 qMax(sizeHintForIndex(indexSize
).height(),
1347 d
->biggestItemSize
.height()));
1349 d
->modelIndexList
<< index
;
1351 lastCategory
= d
->proxyModel
->data(index
, KCategorizedSortFilterProxyModel::CategoryDisplayRole
).toString();
1353 elementInfo
.category
= lastCategory
;
1355 if (prevCategory
!= lastCategory
)
1358 d
->categoriesIndexes
.insert(prevCategory
, modelIndexList
);
1359 d
->categories
<< prevCategory
;
1360 modelIndexList
.clear();
1367 elementInfo
.relativeOffsetToCategory
= offset
;
1369 modelIndexList
<< index
;
1370 prevCategory
= lastCategory
;
1372 d
->elementsInfo
.insert(index
.row(), elementInfo
);
1375 d
->categoriesIndexes
.insert(prevCategory
, modelIndexList
);
1376 d
->categories
<< prevCategory
;
1378 d
->updateScrollbars();
1381 void KCategorizedView::rowsRemoved(const QModelIndex
&parent
,
1385 if ((viewMode() == KCategorizedView::IconMode
) && d
->proxyModel
&&
1386 d
->categoryDrawer
&& d
->proxyModel
->isCategorizedModel())
1388 // Force the view to update all elements
1389 rowsInsertedArtifficial(QModelIndex(), 0, d
->proxyModel
->rowCount() - 1);
1393 void KCategorizedView::updateGeometries()
1395 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
1396 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
1398 QListView::updateGeometries();
1402 // Avoid QListView::updateGeometries(), since it will try to set another
1403 // range to our scroll bars, what we don't want (ereslibre)
1404 QAbstractItemView::updateGeometries();
1407 void KCategorizedView::slotLayoutChanged()
1412 #include "kcategorizedview.moc"