2 * This file is part of the KDE project
3 * Copyright (C) 2007 Rafael Fernández López <ereslibre@gmail.com>
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 if ((category
== hoveredCategory
) && !mouseButtonPressed
)
362 optionCopy
.state
|= QStyle::State_MouseOver
;
365 categoryDrawer
->drawCategory(index
,
372 void KCategorizedView::Private::updateScrollbars()
374 // find the last index in the last category
375 QModelIndex lastIndex
= categoriesIndexes
.isEmpty() ? QModelIndex() : categoriesIndexes
[categories
.last()].last();
377 int lastItemBottom
= cachedRectIndex(lastIndex
).top() +
378 listView
->spacing() + (listView
->gridSize().isEmpty() ? 0 : listView
->gridSize().height()) - listView
->viewport()->height();
380 listView
->verticalScrollBar()->setSingleStep(listView
->viewport()->height() / 10);
381 listView
->verticalScrollBar()->setPageStep(listView
->viewport()->height());
382 listView
->verticalScrollBar()->setRange(0, lastItemBottom
);
385 void KCategorizedView::Private::drawDraggedItems(QPainter
*painter
)
387 QStyleOptionViewItemV3 option
= listView
->viewOptions();
388 option
.state
&= ~QStyle::State_MouseOver
;
389 foreach (const QModelIndex
&index
, listView
->selectionModel()->selectedIndexes())
391 const int dx
= mousePosition
.x() - initialPressPosition
.x() + listView
->horizontalOffset();
392 const int dy
= mousePosition
.y() - initialPressPosition
.y() + listView
->verticalOffset();
394 option
.rect
= visualRect(index
);
395 option
.rect
.adjust(dx
, dy
, dx
, dy
);
397 if (option
.rect
.intersects(listView
->viewport()->rect()))
399 listView
->itemDelegate(index
)->paint(painter
, option
, index
);
404 void KCategorizedView::Private::drawDraggedItems()
408 foreach (const QModelIndex
&index
, listView
->selectionModel()->selectedIndexes())
410 int dx
= mousePosition
.x() - initialPressPosition
.x() + listView
->horizontalOffset();
411 int dy
= mousePosition
.y() - initialPressPosition
.y() + listView
->verticalOffset();
413 currentRect
= visualRect(index
);
414 currentRect
.adjust(dx
, dy
, dx
, dy
);
416 if (currentRect
.intersects(listView
->viewport()->rect()))
418 rectToUpdate
= rectToUpdate
.united(currentRect
);
422 listView
->viewport()->update(lastDraggedItemsRect
.united(rectToUpdate
));
424 lastDraggedItemsRect
= rectToUpdate
;
428 //==============================================================================
431 KCategorizedView::KCategorizedView(QWidget
*parent
)
433 , d(new Private(this))
437 KCategorizedView::~KCategorizedView()
442 void KCategorizedView::setGridSize(const QSize
&size
)
444 QListView::setGridSize(size
);
449 void KCategorizedView::setModel(QAbstractItemModel
*model
)
451 d
->lastSelection
= QItemSelection();
452 d
->currentViewIndex
= QModelIndex();
453 d
->forcedSelectionPosition
= 0;
454 d
->elementsInfo
.clear();
455 d
->elementsPosition
.clear();
456 d
->categoriesIndexes
.clear();
457 d
->categoriesPosition
.clear();
458 d
->categories
.clear();
459 d
->intersectedIndexes
.clear();
460 d
->modelIndexList
.clear();
461 d
->hovered
= QModelIndex();
462 d
->mouseButtonPressed
= false;
466 QObject::disconnect(d
->proxyModel
,
467 SIGNAL(layoutChanged()),
468 this, SLOT(slotLayoutChanged()));
470 QObject::disconnect(d
->proxyModel
,
471 SIGNAL(dataChanged(QModelIndex
,QModelIndex
)),
472 this, SLOT(slotLayoutChanged()));
475 QListView::setModel(model
);
477 d
->proxyModel
= dynamic_cast<KCategorizedSortFilterProxyModel
*>(model
);
481 QObject::connect(d
->proxyModel
,
482 SIGNAL(layoutChanged()),
483 this, SLOT(slotLayoutChanged()));
485 QObject::connect(d
->proxyModel
,
486 SIGNAL(dataChanged(QModelIndex
,QModelIndex
)),
487 this, SLOT(slotLayoutChanged()));
491 QRect
KCategorizedView::visualRect(const QModelIndex
&index
) const
493 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
494 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
496 return QListView::visualRect(index
);
499 if (!qobject_cast
<const QSortFilterProxyModel
*>(index
.model()))
501 return d
->visualRect(d
->proxyModel
->mapFromSource(index
));
504 return d
->visualRect(index
);
507 KCategoryDrawer
*KCategorizedView::categoryDrawer() const
509 return d
->categoryDrawer
;
512 void KCategorizedView::setCategoryDrawer(KCategoryDrawer
*categoryDrawer
)
514 d
->lastSelection
= QItemSelection();
515 d
->currentViewIndex
= QModelIndex();
516 d
->forcedSelectionPosition
= 0;
517 d
->elementsInfo
.clear();
518 d
->elementsPosition
.clear();
519 d
->categoriesIndexes
.clear();
520 d
->categoriesPosition
.clear();
521 d
->categories
.clear();
522 d
->intersectedIndexes
.clear();
523 d
->modelIndexList
.clear();
524 d
->hovered
= QModelIndex();
525 d
->mouseButtonPressed
= false;
527 if (!categoryDrawer
&& d
->proxyModel
)
529 QObject::disconnect(d
->proxyModel
,
530 SIGNAL(layoutChanged()),
531 this, SLOT(slotLayoutChanged()));
533 QObject::disconnect(d
->proxyModel
,
534 SIGNAL(dataChanged(QModelIndex
,QModelIndex
)),
535 this, SLOT(slotLayoutChanged()));
537 else if (categoryDrawer
&& d
->proxyModel
)
539 QObject::connect(d
->proxyModel
,
540 SIGNAL(layoutChanged()),
541 this, SLOT(slotLayoutChanged()));
543 QObject::connect(d
->proxyModel
,
544 SIGNAL(dataChanged(QModelIndex
,QModelIndex
)),
545 this, SLOT(slotLayoutChanged()));
548 d
->categoryDrawer
= categoryDrawer
;
554 rowsInserted(QModelIndex(), 0, d
->proxyModel
->rowCount() - 1);
563 QModelIndex
KCategorizedView::indexAt(const QPoint
&point
) const
565 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
566 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
568 return QListView::indexAt(point
);
573 QModelIndexList item
= d
->intersectionSet(QRect(point
, point
));
575 if (item
.count() == 1)
585 void KCategorizedView::reset()
589 d
->lastSelection
= QItemSelection();
590 d
->currentViewIndex
= QModelIndex();
591 d
->forcedSelectionPosition
= 0;
592 d
->elementsInfo
.clear();
593 d
->elementsPosition
.clear();
594 d
->categoriesIndexes
.clear();
595 d
->categoriesPosition
.clear();
596 d
->categories
.clear();
597 d
->intersectedIndexes
.clear();
598 d
->modelIndexList
.clear();
599 d
->hovered
= QModelIndex();
600 d
->biggestItemSize
= QSize(0, 0);
601 d
->mouseButtonPressed
= false;
604 void KCategorizedView::paintEvent(QPaintEvent
*event
)
606 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
607 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
609 QListView::paintEvent(event
);
613 QStyleOptionViewItemV3 option
= viewOptions();
614 option
.widget
= this;
617 option
.features
|= QStyleOptionViewItemV2::WrapText
;
620 QPainter
painter(viewport());
621 QRect area
= event
->rect();
622 const bool focus
= (hasFocus() || viewport()->hasFocus()) &&
623 currentIndex().isValid();
624 const QStyle::State state
= option
.state
;
625 const bool enabled
= (state
& QStyle::State_Enabled
) != 0;
629 QModelIndexList dirtyIndexes
= d
->intersectionSet(area
);
630 foreach (const QModelIndex
&index
, dirtyIndexes
)
632 option
.state
= state
;
633 option
.rect
= visualRect(index
);
635 if (selectionModel() && selectionModel()->isSelected(index
))
637 option
.state
|= QStyle::State_Selected
;
642 QPalette::ColorGroup cg
;
643 if ((d
->proxyModel
->flags(index
) & Qt::ItemIsEnabled
) == 0)
645 option
.state
&= ~QStyle::State_Enabled
;
646 cg
= QPalette::Disabled
;
650 cg
= QPalette::Normal
;
652 option
.palette
.setCurrentColorGroup(cg
);
655 if (focus
&& currentIndex() == index
)
657 option
.state
|= QStyle::State_HasFocus
;
658 if (this->state() == EditingState
)
659 option
.state
|= QStyle::State_Editing
;
662 if ((index
== d
->hovered
) && !d
->mouseButtonPressed
)
663 option
.state
|= QStyle::State_MouseOver
;
665 option
.state
&= ~QStyle::State_MouseOver
;
667 itemDelegate(index
)->paint(&painter
, option
, index
);
671 QStyleOptionViewItem otherOption
;
672 foreach (const QString
&category
, d
->categories
)
674 otherOption
= option
;
675 otherOption
.rect
= d
->categoryVisualRect(category
);
676 otherOption
.state
&= ~QStyle::State_MouseOver
;
678 if (otherOption
.rect
.intersects(area
))
680 QModelIndex indexToDraw
= d
->proxyModel
->index(d
->categoriesIndexes
[category
][0].row(), d
->proxyModel
->sortColumn());
682 d
->drawNewCategory(indexToDraw
,
683 d
->proxyModel
->sortRole(), otherOption
, &painter
);
687 if (d
->mouseButtonPressed
&& !d
->isDragging
)
689 QPoint start
, end
, initialPressPosition
;
691 initialPressPosition
= d
->initialPressPosition
;
693 initialPressPosition
.setY(initialPressPosition
.y() - verticalOffset());
694 initialPressPosition
.setX(initialPressPosition
.x() - horizontalOffset());
696 if (d
->initialPressPosition
.x() > d
->mousePosition
.x() ||
697 d
->initialPressPosition
.y() > d
->mousePosition
.y())
699 start
= d
->mousePosition
;
700 end
= initialPressPosition
;
704 start
= initialPressPosition
;
705 end
= d
->mousePosition
;
708 QStyleOptionRubberBand yetAnotherOption
;
709 yetAnotherOption
.initFrom(this);
710 yetAnotherOption
.shape
= QRubberBand::Rectangle
;
711 yetAnotherOption
.opaque
= false;
712 yetAnotherOption
.rect
= QRect(start
, end
).intersected(viewport()->rect().adjusted(-16, -16, 16, 16));
714 style()->drawControl(QStyle::CE_RubberBand
, &yetAnotherOption
, &painter
);
718 if (d
->isDragging
&& !d
->dragLeftViewport
)
720 painter
.setOpacity(0.5);
721 d
->drawDraggedItems(&painter
);
727 void KCategorizedView::resizeEvent(QResizeEvent
*event
)
729 QListView::resizeEvent(event
);
731 // Clear the items positions cache
732 d
->elementsPosition
.clear();
733 d
->categoriesPosition
.clear();
734 d
->forcedSelectionPosition
= 0;
736 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
737 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
742 d
->updateScrollbars();
745 void KCategorizedView::setSelection(const QRect
&rect
,
746 QItemSelectionModel::SelectionFlags flags
)
748 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
749 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
751 QListView::setSelection(rect
, flags
);
758 selectionModel()->clear();
760 if (flags
& QItemSelectionModel::Clear
)
762 d
->lastSelection
= QItemSelection();
765 QModelIndexList dirtyIndexes
= d
->intersectionSet(rect
);
767 QItemSelection selection
;
769 if (!dirtyIndexes
.count())
771 if (d
->lastSelection
.count())
773 selectionModel()->select(d
->lastSelection
, flags
);
779 if (!d
->mouseButtonPressed
)
781 selection
= QItemSelection(dirtyIndexes
[0], dirtyIndexes
[0]);
782 d
->currentViewIndex
= dirtyIndexes
[0];
786 QModelIndex first
= dirtyIndexes
[0];
788 foreach (const QModelIndex
&index
, dirtyIndexes
)
790 if (last
.isValid() && last
.row() + 1 != index
.row())
792 QItemSelectionRange
range(first
, last
);
803 selection
<< QItemSelectionRange(first
, last
);
806 if (d
->lastSelection
.count() && !d
->mouseButtonPressed
)
808 selection
.merge(d
->lastSelection
, flags
);
810 else if (d
->lastSelection
.count())
812 selection
.merge(d
->lastSelection
, QItemSelectionModel::Select
);
815 selectionModel()->select(selection
, flags
);
818 void KCategorizedView::mouseMoveEvent(QMouseEvent
*event
)
820 QListView::mouseMoveEvent(event
);
822 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
823 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
828 const QString previousHoveredCategory
= d
->hoveredCategory
;
830 d
->mousePosition
= event
->pos();
831 d
->hoveredCategory
= QString();
834 foreach (const QString
&category
, d
->categories
)
836 if (d
->categoryVisualRect(category
).intersects(QRect(event
->pos(), event
->pos())))
838 d
->hoveredCategory
= category
;
839 viewport()->update(d
->categoryVisualRect(category
));
841 else if ((category
== previousHoveredCategory
) &&
842 (!d
->categoryVisualRect(previousHoveredCategory
).intersects(QRect(event
->pos(), event
->pos()))))
844 viewport()->update(d
->categoryVisualRect(category
));
849 if (d
->mouseButtonPressed
&& !d
->isDragging
)
851 QPoint start
, end
, initialPressPosition
;
853 initialPressPosition
= d
->initialPressPosition
;
855 initialPressPosition
.setY(initialPressPosition
.y() - verticalOffset());
856 initialPressPosition
.setX(initialPressPosition
.x() - horizontalOffset());
858 if (d
->initialPressPosition
.x() > d
->mousePosition
.x() ||
859 d
->initialPressPosition
.y() > d
->mousePosition
.y())
861 start
= d
->mousePosition
;
862 end
= initialPressPosition
;
866 start
= initialPressPosition
;
867 end
= d
->mousePosition
;
870 rect
= QRect(start
, end
).intersected(viewport()->rect().adjusted(-16, -16, 16, 16));
872 //viewport()->update(rect.united(d->lastSelectionRect));
874 d
->lastSelectionRect
= rect
;
878 void KCategorizedView::mousePressEvent(QMouseEvent
*event
)
880 d
->dragLeftViewport
= false;
882 if (event
->button() == Qt::LeftButton
)
884 d
->mouseButtonPressed
= true;
886 d
->initialPressPosition
= event
->pos();
887 d
->initialPressPosition
.setY(d
->initialPressPosition
.y() +
889 d
->initialPressPosition
.setX(d
->initialPressPosition
.x() +
893 QListView::mousePressEvent(event
);
896 void KCategorizedView::mouseReleaseEvent(QMouseEvent
*event
)
898 d
->mouseButtonPressed
= false;
900 QListView::mouseReleaseEvent(event
);
902 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
903 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
908 QPoint initialPressPosition
= viewport()->mapFromGlobal(QCursor::pos());
909 initialPressPosition
.setY(initialPressPosition
.y() + verticalOffset());
910 initialPressPosition
.setX(initialPressPosition
.x() + horizontalOffset());
912 QItemSelection selection
;
914 if (initialPressPosition
== d
->initialPressPosition
)
916 foreach(const QString
&category
, d
->categories
)
918 if (d
->categoryVisualRect(category
).contains(event
->pos()))
920 foreach (const QModelIndex
&index
, d
->categoriesIndexes
[category
])
922 QModelIndex selectIndex
= index
.model()->index(index
.row(), 0);
924 selection
<< QItemSelectionRange(selectIndex
);
927 selectionModel()->select(selection
, QItemSelectionModel::Select
);
934 d
->lastSelection
= selectionModel()->selection();
936 if (d
->hovered
.isValid())
937 viewport()->update(visualRect(d
->hovered
));
938 else if (!d
->hoveredCategory
.isEmpty())
939 viewport()->update(d
->categoryVisualRect(d
->hoveredCategory
));
942 void KCategorizedView::leaveEvent(QEvent
*event
)
944 d
->hovered
= QModelIndex();
945 d
->hoveredCategory
= QString();
947 QListView::leaveEvent(event
);
950 void KCategorizedView::startDrag(Qt::DropActions supportedActions
)
952 QListView::startDrag(supportedActions
);
954 d
->isDragging
= false;
955 d
->mouseButtonPressed
= false;
957 viewport()->update(d
->lastDraggedItemsRect
);
960 void KCategorizedView::dragMoveEvent(QDragMoveEvent
*event
)
962 d
->mousePosition
= event
->pos();
964 if (d
->mouseButtonPressed
)
966 d
->isDragging
= true;
970 d
->isDragging
= false;
973 d
->dragLeftViewport
= false;
975 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
976 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
978 QListView::dragMoveEvent(event
);
982 d
->drawDraggedItems();
985 void KCategorizedView::dragLeaveEvent(QDragLeaveEvent
*event
)
987 d
->dragLeftViewport
= true;
989 QListView::dragLeaveEvent(event
);
992 QModelIndex
KCategorizedView::moveCursor(CursorAction cursorAction
,
993 Qt::KeyboardModifiers modifiers
)
995 if ((viewMode() != KCategorizedView::IconMode
) ||
997 !d
->categoryDrawer
||
998 d
->categories
.isEmpty() ||
999 !d
->proxyModel
->isCategorizedModel()
1002 return QListView::moveCursor(cursorAction
, modifiers
);
1005 const QModelIndex current
= selectionModel()->currentIndex();
1007 int viewportWidth
= viewport()->width() - spacing();
1010 if (gridSize().isEmpty())
1012 itemWidth
= d
->biggestItemSize
.width();
1016 itemWidth
= gridSize().width();
1019 int itemWidthPlusSeparation
= spacing() + itemWidth
;
1020 int elementsPerRow
= viewportWidth
/ itemWidthPlusSeparation
;
1022 QString lastCategory
= d
->categories
.first();
1023 QString theCategory
= d
->categories
.first();
1024 QString afterCategory
= d
->categories
.first();
1026 bool hasToBreak
= false;
1027 foreach (const QString
&category
, d
->categories
)
1031 afterCategory
= category
;
1036 if (category
== d
->elementsInfo
[d
->proxyModel
->mapToSource(current
).row()].category
)
1038 theCategory
= category
;
1045 lastCategory
= category
;
1050 switch (cursorAction
)
1052 case QAbstractItemView::MoveUp
: {
1053 if (d
->elementsInfo
[d
->proxyModel
->mapToSource(current
).row()].relativeOffsetToCategory
>= elementsPerRow
)
1055 int indexToMove
= d
->invertedElementDictionary
[current
.row()].row();
1056 indexToMove
-= qMin(((d
->elementsInfo
[d
->proxyModel
->mapToSource(current
).row()].relativeOffsetToCategory
) + d
->forcedSelectionPosition
), elementsPerRow
- d
->forcedSelectionPosition
+ (d
->elementsInfo
[d
->proxyModel
->mapToSource(current
).row()].relativeOffsetToCategory
% elementsPerRow
));
1058 return d
->elementDictionary
[indexToMove
];
1062 int lastCategoryLastRow
= (d
->categoriesIndexes
[lastCategory
].count() - 1) % elementsPerRow
;
1063 int indexToMove
= d
->invertedElementDictionary
[current
.row()].row() - d
->elementsInfo
[d
->proxyModel
->mapToSource(current
).row()].relativeOffsetToCategory
;
1065 if (d
->forcedSelectionPosition
>= lastCategoryLastRow
)
1071 indexToMove
-= qMin((lastCategoryLastRow
- d
->forcedSelectionPosition
+ 1), d
->forcedSelectionPosition
+ elementsPerRow
+ 1);
1074 return d
->elementDictionary
[indexToMove
];
1078 case QAbstractItemView::MoveDown
: {
1079 if (d
->elementsInfo
[d
->proxyModel
->mapToSource(current
).row()].relativeOffsetToCategory
< (d
->categoriesIndexes
[theCategory
].count() - 1 - ((d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
)))
1081 int indexToMove
= d
->invertedElementDictionary
[current
.row()].row();
1082 indexToMove
+= qMin(elementsPerRow
, d
->categoriesIndexes
[theCategory
].count() - 1 - d
->elementsInfo
[d
->proxyModel
->mapToSource(current
).row()].relativeOffsetToCategory
);
1084 return d
->elementDictionary
[indexToMove
];
1088 int afterCategoryLastRow
= qMin(elementsPerRow
, d
->categoriesIndexes
[afterCategory
].count());
1089 int indexToMove
= d
->invertedElementDictionary
[current
.row()].row() + (d
->categoriesIndexes
[theCategory
].count() - d
->elementsInfo
[d
->proxyModel
->mapToSource(current
).row()].relativeOffsetToCategory
);
1091 if (d
->forcedSelectionPosition
>= afterCategoryLastRow
)
1093 indexToMove
+= afterCategoryLastRow
- 1;
1097 indexToMove
+= qMin(d
->forcedSelectionPosition
, elementsPerRow
);
1100 return d
->elementDictionary
[indexToMove
];
1104 case QAbstractItemView::MoveLeft
:
1105 d
->forcedSelectionPosition
= d
->elementsInfo
[d
->proxyModel
->mapToSource(d
->elementDictionary
[d
->proxyModel
->index(d
->invertedElementDictionary
[current
.row()].row() - 1, 0).row()]).row()].relativeOffsetToCategory
% elementsPerRow
;
1107 if (d
->forcedSelectionPosition
< 0)
1108 d
->forcedSelectionPosition
= (d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
;
1110 return d
->elementDictionary
[d
->proxyModel
->index(d
->invertedElementDictionary
[current
.row()].row() - 1, 0).row()];
1112 case QAbstractItemView::MoveRight
:
1113 d
->forcedSelectionPosition
= d
->elementsInfo
[d
->proxyModel
->mapToSource(d
->elementDictionary
[d
->proxyModel
->index(d
->invertedElementDictionary
[current
.row()].row() + 1, 0).row()]).row()].relativeOffsetToCategory
% elementsPerRow
;
1115 if (d
->forcedSelectionPosition
< 0)
1116 d
->forcedSelectionPosition
= (d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
;
1118 return d
->elementDictionary
[d
->proxyModel
->index(d
->invertedElementDictionary
[current
.row()].row() + 1, 0).row()];
1124 return QListView::moveCursor(cursorAction
, modifiers
);
1127 void KCategorizedView::rowsInserted(const QModelIndex
&parent
,
1131 QListView::rowsInserted(parent
, start
, end
);
1133 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
1134 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
1136 d
->lastSelection
= QItemSelection();
1137 d
->currentViewIndex
= QModelIndex();
1138 d
->forcedSelectionPosition
= 0;
1139 d
->elementsInfo
.clear();
1140 d
->elementsPosition
.clear();
1141 d
->categoriesIndexes
.clear();
1142 d
->categoriesPosition
.clear();
1143 d
->categories
.clear();
1144 d
->intersectedIndexes
.clear();
1145 d
->modelIndexList
.clear();
1146 d
->hovered
= QModelIndex();
1147 d
->biggestItemSize
= QSize(0, 0);
1148 d
->mouseButtonPressed
= false;
1153 rowsInsertedArtifficial(parent
, start
, end
);
1156 void KCategorizedView::rowsInsertedArtifficial(const QModelIndex
&parent
,
1162 d
->lastSelection
= QItemSelection();
1163 d
->currentViewIndex
= QModelIndex();
1164 d
->forcedSelectionPosition
= 0;
1165 d
->elementsInfo
.clear();
1166 d
->elementsPosition
.clear();
1167 d
->categoriesIndexes
.clear();
1168 d
->categoriesPosition
.clear();
1169 d
->categories
.clear();
1170 d
->intersectedIndexes
.clear();
1171 d
->modelIndexList
.clear();
1172 d
->hovered
= QModelIndex();
1173 d
->biggestItemSize
= QSize(0, 0);
1174 d
->mouseButtonPressed
= false;
1176 if (start
> end
|| end
< 0 || start
< 0 || !d
->proxyModel
->rowCount())
1181 // Add all elements mapped to the source model
1182 for (int k
= 0; k
< d
->proxyModel
->rowCount(); k
++)
1184 d
->biggestItemSize
= QSize(qMax(sizeHintForIndex(d
->proxyModel
->index(k
, 0)).width(),
1185 d
->biggestItemSize
.width()),
1186 qMax(sizeHintForIndex(d
->proxyModel
->index(k
, 0)).height(),
1187 d
->biggestItemSize
.height()));
1189 d
->modelIndexList
<< d
->proxyModel
->index(k
, d
->proxyModel
->sortColumn());
1192 // Explore categories
1193 QString prevCategory
= d
->proxyModel
->data(d
->modelIndexList
[0], KCategorizedSortFilterProxyModel::CategoryRole
).toString();
1194 QString lastCategory
= prevCategory
;
1195 QModelIndexList modelIndexList
;
1196 struct Private::ElementInfo elementInfo
;
1198 foreach (const QModelIndex
&index
, d
->modelIndexList
)
1200 lastCategory
= d
->proxyModel
->data(index
, KCategorizedSortFilterProxyModel::CategoryRole
).toString();
1202 elementInfo
.category
= lastCategory
;
1204 if (prevCategory
!= lastCategory
)
1207 d
->categoriesIndexes
.insert(prevCategory
, modelIndexList
);
1208 d
->categories
<< prevCategory
;
1209 modelIndexList
.clear();
1216 elementInfo
.relativeOffsetToCategory
= offset
;
1218 modelIndexList
<< index
;
1219 prevCategory
= lastCategory
;
1221 d
->elementsInfo
.insert(index
.row(), elementInfo
);
1224 d
->categoriesIndexes
.insert(prevCategory
, modelIndexList
);
1225 d
->categories
<< prevCategory
;
1227 d
->updateScrollbars();
1230 void KCategorizedView::rowsRemoved(const QModelIndex
&parent
,
1234 if ((viewMode() == KCategorizedView::IconMode
) && d
->proxyModel
&&
1235 d
->categoryDrawer
&& d
->proxyModel
->isCategorizedModel())
1237 // Force the view to update all elements
1238 rowsInsertedArtifficial(parent
, start
, end
);
1242 void KCategorizedView::updateGeometries()
1244 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
1245 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
1247 QListView::updateGeometries();
1251 // Avoid QListView::updateGeometries(), since it will try to set another
1252 // range to our scroll bars, what we don't want (ereslibre)
1253 QAbstractItemView::updateGeometries();
1256 void KCategorizedView::slotLayoutChanged()
1258 if ((viewMode() == KCategorizedView::IconMode
) && d
->proxyModel
&&
1259 d
->categoryDrawer
&& d
->proxyModel
->isCategorizedModel())
1261 // Force the view to update all elements
1262 rowsInsertedArtifficial(QModelIndex(), 0, d
->proxyModel
->rowCount() - 1);
1266 #include "kcategorizedview.moc"