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 (modelCategorized
!= proxyModel
->isCategorizedModel()))))
427 // Force the view to update all elements
428 listView
->rowsInsertedArtifficial(QModelIndex(), 0, proxyModel
->rowCount() - 1);
430 if (!forceItemReload
)
432 modelSortRole
= proxyModel
->sortRole();
433 modelSortColumn
= proxyModel
->sortColumn();
434 modelSortOrder
= proxyModel
->sortOrder();
435 modelCategorized
= proxyModel
->isCategorizedModel();
438 else if ((listView
->viewMode() == KCategorizedView::IconMode
) && proxyModel
&&
439 categoryDrawer
&& proxyModel
->isCategorizedModel())
445 void KCategorizedView::Private::drawDraggedItems()
449 foreach (const QModelIndex
&index
, listView
->selectionModel()->selectedIndexes())
451 int dx
= mousePosition
.x() - initialPressPosition
.x() + listView
->horizontalOffset();
452 int dy
= mousePosition
.y() - initialPressPosition
.y() + listView
->verticalOffset();
454 currentRect
= visualRect(index
);
455 currentRect
.adjust(dx
, dy
, dx
, dy
);
457 if (currentRect
.intersects(listView
->viewport()->rect()))
459 rectToUpdate
= rectToUpdate
.united(currentRect
);
463 listView
->viewport()->update(lastDraggedItemsRect
.united(rectToUpdate
));
465 lastDraggedItemsRect
= rectToUpdate
;
469 //==============================================================================
472 KCategorizedView::KCategorizedView(QWidget
*parent
)
474 , d(new Private(this))
478 KCategorizedView::~KCategorizedView()
483 void KCategorizedView::setGridSize(const QSize
&size
)
485 QListView::setGridSize(size
);
487 d
->layoutChanged(true);
490 void KCategorizedView::setModel(QAbstractItemModel
*model
)
492 d
->lastSelection
= QItemSelection();
493 d
->currentViewIndex
= QModelIndex();
494 d
->forcedSelectionPosition
= 0;
495 d
->elementsInfo
.clear();
496 d
->elementsPosition
.clear();
497 d
->categoriesIndexes
.clear();
498 d
->categoriesPosition
.clear();
499 d
->categories
.clear();
500 d
->intersectedIndexes
.clear();
501 d
->modelIndexList
.clear();
502 d
->hovered
= QModelIndex();
503 d
->mouseButtonPressed
= false;
507 QObject::disconnect(d
->proxyModel
,
508 SIGNAL(layoutChanged()),
509 this, SLOT(slotLayoutChanged()));
511 QObject::disconnect(d
->proxyModel
,
512 SIGNAL(dataChanged(QModelIndex
,QModelIndex
)),
513 this, SLOT(slotLayoutChanged()));
515 QObject::disconnect(d
->proxyModel
,
516 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
517 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
520 QListView::setModel(model
);
522 d
->proxyModel
= dynamic_cast<KCategorizedSortFilterProxyModel
*>(model
);
526 d
->modelSortRole
= d
->proxyModel
->sortRole();
527 d
->modelSortColumn
= d
->proxyModel
->sortColumn();
528 d
->modelSortOrder
= d
->proxyModel
->sortOrder();
529 d
->modelCategorized
= d
->proxyModel
->isCategorizedModel();
531 QObject::connect(d
->proxyModel
,
532 SIGNAL(layoutChanged()),
533 this, SLOT(slotLayoutChanged()));
535 QObject::connect(d
->proxyModel
,
536 SIGNAL(dataChanged(QModelIndex
,QModelIndex
)),
537 this, SLOT(slotLayoutChanged()));
539 QObject::connect(d
->proxyModel
,
540 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
541 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
543 if (d
->proxyModel
->rowCount())
545 d
->layoutChanged(true);
550 d
->modelCategorized
= false;
554 QRect
KCategorizedView::visualRect(const QModelIndex
&index
) const
556 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
557 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
559 return QListView::visualRect(index
);
562 if (!qobject_cast
<const QSortFilterProxyModel
*>(index
.model()))
564 return d
->visualRect(d
->proxyModel
->mapFromSource(index
));
567 return d
->visualRect(index
);
570 KCategoryDrawer
*KCategorizedView::categoryDrawer() const
572 return d
->categoryDrawer
;
575 void KCategorizedView::setCategoryDrawer(KCategoryDrawer
*categoryDrawer
)
577 d
->lastSelection
= QItemSelection();
578 d
->currentViewIndex
= QModelIndex();
579 d
->forcedSelectionPosition
= 0;
580 d
->elementsInfo
.clear();
581 d
->elementsPosition
.clear();
582 d
->categoriesIndexes
.clear();
583 d
->categoriesPosition
.clear();
584 d
->categories
.clear();
585 d
->intersectedIndexes
.clear();
586 d
->modelIndexList
.clear();
587 d
->hovered
= QModelIndex();
588 d
->mouseButtonPressed
= false;
590 if (!categoryDrawer
&& d
->proxyModel
)
592 QObject::disconnect(d
->proxyModel
,
593 SIGNAL(layoutChanged()),
594 this, SLOT(slotLayoutChanged()));
596 QObject::disconnect(d
->proxyModel
,
597 SIGNAL(dataChanged(QModelIndex
,QModelIndex
)),
598 this, SLOT(slotLayoutChanged()));
600 QObject::disconnect(d
->proxyModel
,
601 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
602 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
604 else if (categoryDrawer
&& d
->proxyModel
)
606 QObject::connect(d
->proxyModel
,
607 SIGNAL(layoutChanged()),
608 this, SLOT(slotLayoutChanged()));
610 QObject::connect(d
->proxyModel
,
611 SIGNAL(dataChanged(QModelIndex
,QModelIndex
)),
612 this, SLOT(slotLayoutChanged()));
614 QObject::connect(d
->proxyModel
,
615 SIGNAL(rowsRemoved(QModelIndex
,int,int)),
616 this, SLOT(rowsRemoved(QModelIndex
,int,int)));
619 d
->categoryDrawer
= categoryDrawer
;
625 if (d
->proxyModel
->rowCount())
627 d
->layoutChanged(true);
637 QModelIndex
KCategorizedView::indexAt(const QPoint
&point
) const
639 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
640 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
642 return QListView::indexAt(point
);
647 QModelIndexList item
= d
->intersectionSet(QRect(point
, point
));
649 if (item
.count() == 1)
659 void KCategorizedView::reset()
663 d
->lastSelection
= QItemSelection();
664 d
->currentViewIndex
= QModelIndex();
665 d
->forcedSelectionPosition
= 0;
666 d
->elementsInfo
.clear();
667 d
->elementsPosition
.clear();
668 d
->categoriesIndexes
.clear();
669 d
->categoriesPosition
.clear();
670 d
->categories
.clear();
671 d
->intersectedIndexes
.clear();
672 d
->modelIndexList
.clear();
673 d
->hovered
= QModelIndex();
674 d
->biggestItemSize
= QSize(0, 0);
675 d
->mouseButtonPressed
= false;
678 void KCategorizedView::paintEvent(QPaintEvent
*event
)
680 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
681 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
683 QListView::paintEvent(event
);
687 QStyleOptionViewItemV3 option
= viewOptions();
688 option
.widget
= this;
691 option
.features
|= QStyleOptionViewItemV2::WrapText
;
694 QPainter
painter(viewport());
695 QRect area
= event
->rect();
696 const bool focus
= (hasFocus() || viewport()->hasFocus()) &&
697 currentIndex().isValid();
698 const QStyle::State state
= option
.state
;
699 const bool enabled
= (state
& QStyle::State_Enabled
) != 0;
703 QModelIndexList dirtyIndexes
= d
->intersectionSet(area
);
704 foreach (const QModelIndex
&index
, dirtyIndexes
)
706 option
.state
= state
;
707 option
.rect
= visualRect(index
);
709 if (selectionModel() && selectionModel()->isSelected(index
))
711 option
.state
|= QStyle::State_Selected
;
716 QPalette::ColorGroup cg
;
717 if ((d
->proxyModel
->flags(index
) & Qt::ItemIsEnabled
) == 0)
719 option
.state
&= ~QStyle::State_Enabled
;
720 cg
= QPalette::Disabled
;
724 cg
= QPalette::Normal
;
726 option
.palette
.setCurrentColorGroup(cg
);
729 if (focus
&& currentIndex() == index
)
731 option
.state
|= QStyle::State_HasFocus
;
732 if (this->state() == EditingState
)
733 option
.state
|= QStyle::State_Editing
;
736 if ((index
== d
->hovered
) && !d
->mouseButtonPressed
)
737 option
.state
|= QStyle::State_MouseOver
;
739 option
.state
&= ~QStyle::State_MouseOver
;
741 itemDelegate(index
)->paint(&painter
, option
, index
);
745 QStyleOptionViewItem otherOption
;
746 bool intersectedInThePast
= false;
747 foreach (const QString
&category
, d
->categories
)
749 otherOption
= option
;
750 otherOption
.rect
= d
->categoryVisualRect(category
);
751 otherOption
.state
&= ~QStyle::State_MouseOver
;
753 if (otherOption
.rect
.intersects(area
))
755 intersectedInThePast
= true;
757 QModelIndex indexToDraw
= d
->proxyModel
->index(d
->categoriesIndexes
[category
][0].row(), d
->proxyModel
->sortColumn());
759 d
->drawNewCategory(indexToDraw
,
760 d
->proxyModel
->sortRole(), otherOption
, &painter
);
762 else if (intersectedInThePast
)
764 break; // the visible area has been finished, we don't need to keep asking, the rest won't intersect
765 // this is doable because we know that categories are correctly ordered on the list
769 if (d
->mouseButtonPressed
&& !d
->isDragging
)
771 QPoint start
, end
, initialPressPosition
;
773 initialPressPosition
= d
->initialPressPosition
;
775 initialPressPosition
.setY(initialPressPosition
.y() - verticalOffset());
776 initialPressPosition
.setX(initialPressPosition
.x() - horizontalOffset());
778 if (d
->initialPressPosition
.x() > d
->mousePosition
.x() ||
779 d
->initialPressPosition
.y() > d
->mousePosition
.y())
781 start
= d
->mousePosition
;
782 end
= initialPressPosition
;
786 start
= initialPressPosition
;
787 end
= d
->mousePosition
;
790 QStyleOptionRubberBand yetAnotherOption
;
791 yetAnotherOption
.initFrom(this);
792 yetAnotherOption
.shape
= QRubberBand::Rectangle
;
793 yetAnotherOption
.opaque
= false;
794 yetAnotherOption
.rect
= QRect(start
, end
).intersected(viewport()->rect().adjusted(-16, -16, 16, 16));
796 style()->drawControl(QStyle::CE_RubberBand
, &yetAnotherOption
, &painter
);
800 if (d
->isDragging
&& !d
->dragLeftViewport
)
802 painter
.setOpacity(0.5);
803 d
->drawDraggedItems(&painter
);
809 void KCategorizedView::resizeEvent(QResizeEvent
*event
)
811 QListView::resizeEvent(event
);
813 // Clear the items positions cache
814 d
->elementsPosition
.clear();
815 d
->categoriesPosition
.clear();
816 d
->forcedSelectionPosition
= 0;
818 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
819 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
824 d
->updateScrollbars();
827 void KCategorizedView::setSelection(const QRect
&rect
,
828 QItemSelectionModel::SelectionFlags flags
)
830 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
831 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
833 QListView::setSelection(rect
, flags
);
840 if (flags
& QItemSelectionModel::Clear
)
842 if (!rect
.intersects(d
->categoryVisualRect(d
->hoveredCategory
)))
844 d
->lastSelection
= QItemSelection();
848 selectionModel()->clear();
850 QModelIndexList dirtyIndexes
= d
->intersectionSet(rect
);
852 if (!dirtyIndexes
.count())
854 if (!d
->lastSelection
.isEmpty() &&
855 (rect
.intersects(d
->categoryVisualRect(d
->hoveredCategory
)) || d
->mouseButtonPressed
))
857 selectionModel()->select(d
->lastSelection
, flags
);
863 QItemSelection selection
;
865 if (!d
->mouseButtonPressed
)
867 selection
= QItemSelection(dirtyIndexes
[0], dirtyIndexes
[0]);
868 d
->currentViewIndex
= dirtyIndexes
[0];
872 QModelIndex first
= dirtyIndexes
[0];
874 foreach (const QModelIndex
&index
, dirtyIndexes
)
876 if (last
.isValid() && last
.row() + 1 != index
.row())
878 QItemSelectionRange
range(first
, last
);
889 selection
<< QItemSelectionRange(first
, last
);
892 if (d
->lastSelection
.count())
894 if ((selection
.count() == 1) && (selection
[0].indexes().count() == 1))
895 selection
.merge(d
->lastSelection
, flags
);
897 selection
.merge(d
->lastSelection
, QItemSelectionModel::Select
);
900 selectionModel()->select(selection
, flags
);
903 void KCategorizedView::mouseMoveEvent(QMouseEvent
*event
)
905 QListView::mouseMoveEvent(event
);
907 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
908 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
913 const QString previousHoveredCategory
= d
->hoveredCategory
;
915 d
->mousePosition
= event
->pos();
916 d
->hoveredCategory
= QString();
919 foreach (const QString
&category
, d
->categories
)
921 if (d
->categoryVisualRect(category
).intersects(QRect(event
->pos(), event
->pos())))
923 d
->hoveredCategory
= category
;
924 viewport()->update(d
->categoryVisualRect(category
));
926 else if ((category
== previousHoveredCategory
) &&
927 (!d
->categoryVisualRect(previousHoveredCategory
).intersects(QRect(event
->pos(), event
->pos()))))
929 viewport()->update(d
->categoryVisualRect(category
));
934 if (d
->mouseButtonPressed
&& !d
->isDragging
)
936 QPoint start
, end
, initialPressPosition
;
938 initialPressPosition
= d
->initialPressPosition
;
940 initialPressPosition
.setY(initialPressPosition
.y() - verticalOffset());
941 initialPressPosition
.setX(initialPressPosition
.x() - horizontalOffset());
943 if (d
->initialPressPosition
.x() > d
->mousePosition
.x() ||
944 d
->initialPressPosition
.y() > d
->mousePosition
.y())
946 start
= d
->mousePosition
;
947 end
= initialPressPosition
;
951 start
= initialPressPosition
;
952 end
= d
->mousePosition
;
955 rect
= QRect(start
, end
).intersected(viewport()->rect().adjusted(-16, -16, 16, 16));
957 //viewport()->update(rect.united(d->lastSelectionRect));
959 d
->lastSelectionRect
= rect
;
963 void KCategorizedView::mousePressEvent(QMouseEvent
*event
)
965 d
->dragLeftViewport
= false;
967 if (event
->button() == Qt::LeftButton
)
969 d
->mouseButtonPressed
= true;
971 d
->initialPressPosition
= event
->pos();
972 d
->initialPressPosition
.setY(d
->initialPressPosition
.y() +
974 d
->initialPressPosition
.setX(d
->initialPressPosition
.x() +
978 QListView::mousePressEvent(event
);
980 viewport()->update(d
->categoryVisualRect(d
->hoveredCategory
));
983 void KCategorizedView::mouseReleaseEvent(QMouseEvent
*event
)
985 d
->mouseButtonPressed
= false;
987 QListView::mouseReleaseEvent(event
);
989 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
990 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
995 QPoint initialPressPosition
= viewport()->mapFromGlobal(QCursor::pos());
996 initialPressPosition
.setY(initialPressPosition
.y() + verticalOffset());
997 initialPressPosition
.setX(initialPressPosition
.x() + horizontalOffset());
999 QItemSelection selection
;
1000 QItemSelection deselection
;
1002 if (initialPressPosition
== d
->initialPressPosition
)
1004 foreach(const QString
&category
, d
->categories
)
1006 if (d
->categoryVisualRect(category
).contains(event
->pos()))
1008 foreach (const QModelIndex
&index
, d
->categoriesIndexes
[category
])
1010 QModelIndex selectIndex
= index
.model()->index(index
.row(), 0);
1012 if (!d
->lastSelection
.contains(selectIndex
))
1014 selection
<< QItemSelectionRange(selectIndex
);
1018 deselection
<< QItemSelectionRange(selectIndex
);
1022 selectionModel()->select(selection
, QItemSelectionModel::Select
);
1023 selectionModel()->select(deselection
, QItemSelectionModel::Deselect
);
1030 d
->lastSelection
= selectionModel()->selection();
1032 if (d
->hovered
.isValid())
1033 viewport()->update(visualRect(d
->hovered
));
1034 else if (!d
->hoveredCategory
.isEmpty())
1035 viewport()->update(d
->categoryVisualRect(d
->hoveredCategory
));
1038 void KCategorizedView::leaveEvent(QEvent
*event
)
1040 d
->hovered
= QModelIndex();
1041 d
->hoveredCategory
= QString();
1043 QListView::leaveEvent(event
);
1046 void KCategorizedView::startDrag(Qt::DropActions supportedActions
)
1048 // FIXME: QAbstractItemView does far better here since it sets the
1049 // pixmap of selected icons to the dragging cursor, but it sets a non
1050 // ARGB window so it is no transparent. Use QAbstractItemView when
1051 // this is fixed on Qt.
1052 // QAbstractItemView::startDrag(supportedActions);
1053 QListView::startDrag(supportedActions
);
1055 d
->isDragging
= false;
1056 d
->mouseButtonPressed
= false;
1058 viewport()->update(d
->lastDraggedItemsRect
);
1061 void KCategorizedView::dragMoveEvent(QDragMoveEvent
*event
)
1063 d
->mousePosition
= event
->pos();
1065 if (d
->mouseButtonPressed
)
1067 d
->isDragging
= true;
1071 d
->isDragging
= false;
1074 d
->dragLeftViewport
= false;
1076 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
1077 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
1079 QListView::dragMoveEvent(event
);
1083 d
->drawDraggedItems();
1086 void KCategorizedView::dragLeaveEvent(QDragLeaveEvent
*event
)
1088 d
->dragLeftViewport
= true;
1090 QListView::dragLeaveEvent(event
);
1093 QModelIndex
KCategorizedView::moveCursor(CursorAction cursorAction
,
1094 Qt::KeyboardModifiers modifiers
)
1096 if ((viewMode() != KCategorizedView::IconMode
) ||
1098 !d
->categoryDrawer
||
1099 d
->categories
.isEmpty() ||
1100 !d
->proxyModel
->isCategorizedModel()
1103 return QListView::moveCursor(cursorAction
, modifiers
);
1106 const QModelIndex current
= selectionModel()->currentIndex();
1108 int viewportWidth
= viewport()->width() - spacing();
1111 if (gridSize().isEmpty())
1113 itemWidth
= d
->biggestItemSize
.width();
1117 itemWidth
= gridSize().width();
1120 int itemWidthPlusSeparation
= spacing() + itemWidth
;
1121 int elementsPerRow
= viewportWidth
/ itemWidthPlusSeparation
;
1123 QString lastCategory
= d
->categories
.first();
1124 QString theCategory
= d
->categories
.first();
1125 QString afterCategory
= d
->categories
.first();
1127 bool hasToBreak
= false;
1128 foreach (const QString
&category
, d
->categories
)
1132 afterCategory
= category
;
1137 if (category
== d
->elementsInfo
[current
.row()].category
)
1139 theCategory
= category
;
1146 lastCategory
= category
;
1150 switch (cursorAction
)
1152 case QAbstractItemView::MoveUp
: {
1153 if (d
->elementsInfo
[current
.row()].relativeOffsetToCategory
>= elementsPerRow
)
1155 int indexToMove
= current
.row();
1156 indexToMove
-= qMin(((d
->elementsInfo
[current
.row()].relativeOffsetToCategory
) + d
->forcedSelectionPosition
), elementsPerRow
- d
->forcedSelectionPosition
+ (d
->elementsInfo
[current
.row()].relativeOffsetToCategory
% elementsPerRow
));
1158 return d
->proxyModel
->index(indexToMove
, 0);
1162 int lastCategoryLastRow
= (d
->categoriesIndexes
[lastCategory
].count() - 1) % elementsPerRow
;
1163 int indexToMove
= current
.row() - d
->elementsInfo
[current
.row()].relativeOffsetToCategory
;
1165 if (d
->forcedSelectionPosition
>= lastCategoryLastRow
)
1171 indexToMove
-= qMin((lastCategoryLastRow
- d
->forcedSelectionPosition
+ 1), d
->forcedSelectionPosition
+ elementsPerRow
+ 1);
1174 return d
->proxyModel
->index(indexToMove
, 0);
1178 case QAbstractItemView::MoveDown
: {
1179 if (d
->elementsInfo
[current
.row()].relativeOffsetToCategory
< (d
->categoriesIndexes
[theCategory
].count() - 1 - ((d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
)))
1181 int indexToMove
= current
.row();
1182 indexToMove
+= qMin(elementsPerRow
, d
->categoriesIndexes
[theCategory
].count() - 1 - d
->elementsInfo
[current
.row()].relativeOffsetToCategory
);
1184 return d
->proxyModel
->index(indexToMove
, 0);
1188 int afterCategoryLastRow
= qMin(elementsPerRow
, d
->categoriesIndexes
[afterCategory
].count());
1189 int indexToMove
= current
.row() + (d
->categoriesIndexes
[theCategory
].count() - d
->elementsInfo
[current
.row()].relativeOffsetToCategory
);
1191 if (d
->forcedSelectionPosition
>= afterCategoryLastRow
)
1193 indexToMove
+= afterCategoryLastRow
- 1;
1197 indexToMove
+= qMin(d
->forcedSelectionPosition
, elementsPerRow
);
1200 return d
->proxyModel
->index(indexToMove
, 0);
1204 case QAbstractItemView::MoveLeft
:
1205 if (layoutDirection() == Qt::RightToLeft
)
1207 d
->forcedSelectionPosition
= d
->elementsInfo
[current
.row() + 1].relativeOffsetToCategory
% elementsPerRow
;
1209 if (d
->forcedSelectionPosition
< 0)
1210 d
->forcedSelectionPosition
= (d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
;
1212 return d
->proxyModel
->index(current
.row() + 1, 0);
1215 d
->forcedSelectionPosition
= d
->elementsInfo
[current
.row() - 1].relativeOffsetToCategory
% elementsPerRow
;
1217 if (d
->forcedSelectionPosition
< 0)
1218 d
->forcedSelectionPosition
= (d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
;
1220 return d
->proxyModel
->index(current
.row() - 1, 0);
1222 case QAbstractItemView::MoveRight
:
1223 if (layoutDirection() == Qt::RightToLeft
)
1225 d
->forcedSelectionPosition
= d
->elementsInfo
[current
.row() - 1].relativeOffsetToCategory
% elementsPerRow
;
1227 if (d
->forcedSelectionPosition
< 0)
1228 d
->forcedSelectionPosition
= (d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
;
1230 return d
->proxyModel
->index(current
.row() - 1, 0);
1233 d
->forcedSelectionPosition
= d
->elementsInfo
[current
.row() + 1].relativeOffsetToCategory
% elementsPerRow
;
1235 if (d
->forcedSelectionPosition
< 0)
1236 d
->forcedSelectionPosition
= (d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
;
1238 return d
->proxyModel
->index(current
.row() + 1, 0);
1244 return QListView::moveCursor(cursorAction
, modifiers
);
1247 void KCategorizedView::rowsInserted(const QModelIndex
&parent
,
1251 QListView::rowsInserted(parent
, start
, end
);
1253 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
1254 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
1256 d
->lastSelection
= QItemSelection();
1257 d
->currentViewIndex
= QModelIndex();
1258 d
->forcedSelectionPosition
= 0;
1259 d
->elementsInfo
.clear();
1260 d
->elementsPosition
.clear();
1261 d
->categoriesIndexes
.clear();
1262 d
->categoriesPosition
.clear();
1263 d
->categories
.clear();
1264 d
->intersectedIndexes
.clear();
1265 d
->modelIndexList
.clear();
1266 d
->hovered
= QModelIndex();
1267 d
->biggestItemSize
= QSize(0, 0);
1268 d
->mouseButtonPressed
= false;
1273 rowsInsertedArtifficial(parent
, start
, end
);
1276 void KCategorizedView::rowsInsertedArtifficial(const QModelIndex
&parent
,
1282 d
->lastSelection
= QItemSelection();
1283 d
->currentViewIndex
= QModelIndex();
1284 d
->forcedSelectionPosition
= 0;
1285 d
->elementsInfo
.clear();
1286 d
->elementsPosition
.clear();
1287 d
->categoriesIndexes
.clear();
1288 d
->categoriesPosition
.clear();
1289 d
->categories
.clear();
1290 d
->intersectedIndexes
.clear();
1291 d
->modelIndexList
.clear();
1292 d
->hovered
= QModelIndex();
1293 d
->biggestItemSize
= QSize(0, 0);
1294 d
->mouseButtonPressed
= false;
1296 if (start
> end
|| end
< 0 || start
< 0 || !d
->proxyModel
->rowCount())
1301 // Add all elements mapped to the source model and explore categories
1302 QString prevCategory
= d
->proxyModel
->data(d
->proxyModel
->index(0, d
->proxyModel
->sortColumn()), KCategorizedSortFilterProxyModel::CategoryRole
).toString();
1303 QString lastCategory
= prevCategory
;
1304 QModelIndexList modelIndexList
;
1305 struct Private::ElementInfo elementInfo
;
1307 for (int k
= 0; k
< d
->proxyModel
->rowCount(); ++k
)
1309 QModelIndex index
= d
->proxyModel
->index(k
, d
->proxyModel
->sortColumn());
1310 QModelIndex indexSize
= d
->proxyModel
->index(k
, 0);
1312 d
->biggestItemSize
= QSize(qMax(sizeHintForIndex(indexSize
).width(),
1313 d
->biggestItemSize
.width()),
1314 qMax(sizeHintForIndex(indexSize
).height(),
1315 d
->biggestItemSize
.height()));
1317 d
->modelIndexList
<< index
;
1319 lastCategory
= d
->proxyModel
->data(index
, KCategorizedSortFilterProxyModel::CategoryRole
).toString();
1321 elementInfo
.category
= lastCategory
;
1323 if (prevCategory
!= lastCategory
)
1326 d
->categoriesIndexes
.insert(prevCategory
, modelIndexList
);
1327 d
->categories
<< prevCategory
;
1328 modelIndexList
.clear();
1335 elementInfo
.relativeOffsetToCategory
= offset
;
1337 modelIndexList
<< index
;
1338 prevCategory
= lastCategory
;
1340 d
->elementsInfo
.insert(index
.row(), elementInfo
);
1343 d
->categoriesIndexes
.insert(prevCategory
, modelIndexList
);
1344 d
->categories
<< prevCategory
;
1346 d
->updateScrollbars();
1349 void KCategorizedView::rowsRemoved(const QModelIndex
&parent
,
1353 if ((viewMode() == KCategorizedView::IconMode
) && d
->proxyModel
&&
1354 d
->categoryDrawer
&& d
->proxyModel
->isCategorizedModel())
1356 // Force the view to update all elements
1357 rowsInsertedArtifficial(QModelIndex(), 0, d
->proxyModel
->rowCount() - 1);
1361 void KCategorizedView::updateGeometries()
1363 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
1364 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
1366 QListView::updateGeometries();
1370 // Avoid QListView::updateGeometries(), since it will try to set another
1371 // range to our scroll bars, what we don't want (ereslibre)
1372 QAbstractItemView::updateGeometries();
1375 void KCategorizedView::slotLayoutChanged()
1380 #include "kcategorizedview.moc"