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::drawDraggedItems()
421 foreach (const QModelIndex
&index
, listView
->selectionModel()->selectedIndexes())
423 int dx
= mousePosition
.x() - initialPressPosition
.x() + listView
->horizontalOffset();
424 int dy
= mousePosition
.y() - initialPressPosition
.y() + listView
->verticalOffset();
426 currentRect
= visualRect(index
);
427 currentRect
.adjust(dx
, dy
, dx
, dy
);
429 if (currentRect
.intersects(listView
->viewport()->rect()))
431 rectToUpdate
= rectToUpdate
.united(currentRect
);
435 listView
->viewport()->update(lastDraggedItemsRect
.united(rectToUpdate
));
437 lastDraggedItemsRect
= rectToUpdate
;
441 //==============================================================================
444 KCategorizedView::KCategorizedView(QWidget
*parent
)
446 , d(new Private(this))
450 KCategorizedView::~KCategorizedView()
455 void KCategorizedView::setGridSize(const QSize
&size
)
457 QListView::setGridSize(size
);
462 void KCategorizedView::setModel(QAbstractItemModel
*model
)
464 d
->lastSelection
= QItemSelection();
465 d
->currentViewIndex
= QModelIndex();
466 d
->forcedSelectionPosition
= 0;
467 d
->elementsInfo
.clear();
468 d
->elementsPosition
.clear();
469 d
->categoriesIndexes
.clear();
470 d
->categoriesPosition
.clear();
471 d
->categories
.clear();
472 d
->intersectedIndexes
.clear();
473 d
->modelIndexList
.clear();
474 d
->hovered
= QModelIndex();
475 d
->mouseButtonPressed
= false;
479 QObject::disconnect(d
->proxyModel
,
480 SIGNAL(layoutChanged()),
481 this, SLOT(slotLayoutChanged()));
483 QObject::disconnect(d
->proxyModel
,
484 SIGNAL(dataChanged(QModelIndex
,QModelIndex
)),
485 this, SLOT(slotLayoutChanged()));
488 QListView::setModel(model
);
490 d
->proxyModel
= dynamic_cast<KCategorizedSortFilterProxyModel
*>(model
);
494 QObject::connect(d
->proxyModel
,
495 SIGNAL(layoutChanged()),
496 this, SLOT(slotLayoutChanged()));
498 QObject::connect(d
->proxyModel
,
499 SIGNAL(dataChanged(QModelIndex
,QModelIndex
)),
500 this, SLOT(slotLayoutChanged()));
504 QRect
KCategorizedView::visualRect(const QModelIndex
&index
) const
506 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
507 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
509 return QListView::visualRect(index
);
512 if (!qobject_cast
<const QSortFilterProxyModel
*>(index
.model()))
514 return d
->visualRect(d
->proxyModel
->mapFromSource(index
));
517 return d
->visualRect(index
);
520 KCategoryDrawer
*KCategorizedView::categoryDrawer() const
522 return d
->categoryDrawer
;
525 void KCategorizedView::setCategoryDrawer(KCategoryDrawer
*categoryDrawer
)
527 d
->lastSelection
= QItemSelection();
528 d
->currentViewIndex
= QModelIndex();
529 d
->forcedSelectionPosition
= 0;
530 d
->elementsInfo
.clear();
531 d
->elementsPosition
.clear();
532 d
->categoriesIndexes
.clear();
533 d
->categoriesPosition
.clear();
534 d
->categories
.clear();
535 d
->intersectedIndexes
.clear();
536 d
->modelIndexList
.clear();
537 d
->hovered
= QModelIndex();
538 d
->mouseButtonPressed
= false;
540 if (!categoryDrawer
&& d
->proxyModel
)
542 QObject::disconnect(d
->proxyModel
,
543 SIGNAL(layoutChanged()),
544 this, SLOT(slotLayoutChanged()));
546 QObject::disconnect(d
->proxyModel
,
547 SIGNAL(dataChanged(QModelIndex
,QModelIndex
)),
548 this, SLOT(slotLayoutChanged()));
550 else if (categoryDrawer
&& d
->proxyModel
)
552 QObject::connect(d
->proxyModel
,
553 SIGNAL(layoutChanged()),
554 this, SLOT(slotLayoutChanged()));
556 QObject::connect(d
->proxyModel
,
557 SIGNAL(dataChanged(QModelIndex
,QModelIndex
)),
558 this, SLOT(slotLayoutChanged()));
561 d
->categoryDrawer
= categoryDrawer
;
567 rowsInserted(QModelIndex(), 0, d
->proxyModel
->rowCount() - 1);
576 QModelIndex
KCategorizedView::indexAt(const QPoint
&point
) const
578 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
579 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
581 return QListView::indexAt(point
);
586 QModelIndexList item
= d
->intersectionSet(QRect(point
, point
));
588 if (item
.count() == 1)
598 void KCategorizedView::reset()
602 d
->lastSelection
= QItemSelection();
603 d
->currentViewIndex
= QModelIndex();
604 d
->forcedSelectionPosition
= 0;
605 d
->elementsInfo
.clear();
606 d
->elementsPosition
.clear();
607 d
->categoriesIndexes
.clear();
608 d
->categoriesPosition
.clear();
609 d
->categories
.clear();
610 d
->intersectedIndexes
.clear();
611 d
->modelIndexList
.clear();
612 d
->hovered
= QModelIndex();
613 d
->biggestItemSize
= QSize(0, 0);
614 d
->mouseButtonPressed
= false;
617 void KCategorizedView::paintEvent(QPaintEvent
*event
)
619 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
620 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
622 QListView::paintEvent(event
);
626 QStyleOptionViewItemV3 option
= viewOptions();
627 option
.widget
= this;
630 option
.features
|= QStyleOptionViewItemV2::WrapText
;
633 QPainter
painter(viewport());
634 QRect area
= event
->rect();
635 const bool focus
= (hasFocus() || viewport()->hasFocus()) &&
636 currentIndex().isValid();
637 const QStyle::State state
= option
.state
;
638 const bool enabled
= (state
& QStyle::State_Enabled
) != 0;
642 QModelIndexList dirtyIndexes
= d
->intersectionSet(area
);
643 foreach (const QModelIndex
&index
, dirtyIndexes
)
645 option
.state
= state
;
646 option
.rect
= visualRect(index
);
648 if (selectionModel() && selectionModel()->isSelected(index
))
650 option
.state
|= QStyle::State_Selected
;
655 QPalette::ColorGroup cg
;
656 if ((d
->proxyModel
->flags(index
) & Qt::ItemIsEnabled
) == 0)
658 option
.state
&= ~QStyle::State_Enabled
;
659 cg
= QPalette::Disabled
;
663 cg
= QPalette::Normal
;
665 option
.palette
.setCurrentColorGroup(cg
);
668 if (focus
&& currentIndex() == index
)
670 option
.state
|= QStyle::State_HasFocus
;
671 if (this->state() == EditingState
)
672 option
.state
|= QStyle::State_Editing
;
675 if ((index
== d
->hovered
) && !d
->mouseButtonPressed
)
676 option
.state
|= QStyle::State_MouseOver
;
678 option
.state
&= ~QStyle::State_MouseOver
;
680 itemDelegate(index
)->paint(&painter
, option
, index
);
684 QStyleOptionViewItem otherOption
;
685 foreach (const QString
&category
, d
->categories
)
687 otherOption
= option
;
688 otherOption
.rect
= d
->categoryVisualRect(category
);
689 otherOption
.state
&= ~QStyle::State_MouseOver
;
691 if (otherOption
.rect
.intersects(area
))
693 QModelIndex indexToDraw
= d
->proxyModel
->index(d
->categoriesIndexes
[category
][0].row(), d
->proxyModel
->sortColumn());
695 d
->drawNewCategory(indexToDraw
,
696 d
->proxyModel
->sortRole(), otherOption
, &painter
);
700 if (d
->mouseButtonPressed
&& !d
->isDragging
)
702 QPoint start
, end
, initialPressPosition
;
704 initialPressPosition
= d
->initialPressPosition
;
706 initialPressPosition
.setY(initialPressPosition
.y() - verticalOffset());
707 initialPressPosition
.setX(initialPressPosition
.x() - horizontalOffset());
709 if (d
->initialPressPosition
.x() > d
->mousePosition
.x() ||
710 d
->initialPressPosition
.y() > d
->mousePosition
.y())
712 start
= d
->mousePosition
;
713 end
= initialPressPosition
;
717 start
= initialPressPosition
;
718 end
= d
->mousePosition
;
721 QStyleOptionRubberBand yetAnotherOption
;
722 yetAnotherOption
.initFrom(this);
723 yetAnotherOption
.shape
= QRubberBand::Rectangle
;
724 yetAnotherOption
.opaque
= false;
725 yetAnotherOption
.rect
= QRect(start
, end
).intersected(viewport()->rect().adjusted(-16, -16, 16, 16));
727 style()->drawControl(QStyle::CE_RubberBand
, &yetAnotherOption
, &painter
);
731 if (d
->isDragging
&& !d
->dragLeftViewport
)
733 painter
.setOpacity(0.5);
734 d
->drawDraggedItems(&painter
);
740 void KCategorizedView::resizeEvent(QResizeEvent
*event
)
742 QListView::resizeEvent(event
);
744 // Clear the items positions cache
745 d
->elementsPosition
.clear();
746 d
->categoriesPosition
.clear();
747 d
->forcedSelectionPosition
= 0;
749 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
750 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
755 d
->updateScrollbars();
758 void KCategorizedView::setSelection(const QRect
&rect
,
759 QItemSelectionModel::SelectionFlags flags
)
761 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
762 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
764 QListView::setSelection(rect
, flags
);
771 if (flags
& QItemSelectionModel::Clear
)
773 if (!rect
.intersects(d
->categoryVisualRect(d
->hoveredCategory
)))
775 d
->lastSelection
= QItemSelection();
779 selectionModel()->clear();
781 QModelIndexList dirtyIndexes
= d
->intersectionSet(rect
);
783 if (!dirtyIndexes
.count())
785 if (!d
->lastSelection
.isEmpty() &&
786 (rect
.intersects(d
->categoryVisualRect(d
->hoveredCategory
)) || d
->mouseButtonPressed
))
788 selectionModel()->select(d
->lastSelection
, flags
);
794 QItemSelection selection
;
796 if (!d
->mouseButtonPressed
)
798 selection
= QItemSelection(dirtyIndexes
[0], dirtyIndexes
[0]);
799 d
->currentViewIndex
= dirtyIndexes
[0];
803 QModelIndex first
= dirtyIndexes
[0];
805 foreach (const QModelIndex
&index
, dirtyIndexes
)
807 if (last
.isValid() && last
.row() + 1 != index
.row())
809 QItemSelectionRange
range(first
, last
);
820 selection
<< QItemSelectionRange(first
, last
);
823 if (d
->lastSelection
.count())
825 if ((selection
.count() == 1) && (selection
[0].indexes().count() == 1))
826 selection
.merge(d
->lastSelection
, flags
);
828 selection
.merge(d
->lastSelection
, QItemSelectionModel::Select
);
831 selectionModel()->select(selection
, flags
);
834 void KCategorizedView::mouseMoveEvent(QMouseEvent
*event
)
836 QListView::mouseMoveEvent(event
);
838 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
839 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
844 const QString previousHoveredCategory
= d
->hoveredCategory
;
846 d
->mousePosition
= event
->pos();
847 d
->hoveredCategory
= QString();
850 foreach (const QString
&category
, d
->categories
)
852 if (d
->categoryVisualRect(category
).intersects(QRect(event
->pos(), event
->pos())))
854 d
->hoveredCategory
= category
;
855 viewport()->update(d
->categoryVisualRect(category
));
857 else if ((category
== previousHoveredCategory
) &&
858 (!d
->categoryVisualRect(previousHoveredCategory
).intersects(QRect(event
->pos(), event
->pos()))))
860 viewport()->update(d
->categoryVisualRect(category
));
865 if (d
->mouseButtonPressed
&& !d
->isDragging
)
867 QPoint start
, end
, initialPressPosition
;
869 initialPressPosition
= d
->initialPressPosition
;
871 initialPressPosition
.setY(initialPressPosition
.y() - verticalOffset());
872 initialPressPosition
.setX(initialPressPosition
.x() - horizontalOffset());
874 if (d
->initialPressPosition
.x() > d
->mousePosition
.x() ||
875 d
->initialPressPosition
.y() > d
->mousePosition
.y())
877 start
= d
->mousePosition
;
878 end
= initialPressPosition
;
882 start
= initialPressPosition
;
883 end
= d
->mousePosition
;
886 rect
= QRect(start
, end
).intersected(viewport()->rect().adjusted(-16, -16, 16, 16));
888 //viewport()->update(rect.united(d->lastSelectionRect));
890 d
->lastSelectionRect
= rect
;
894 void KCategorizedView::mousePressEvent(QMouseEvent
*event
)
896 d
->dragLeftViewport
= false;
898 if (event
->button() == Qt::LeftButton
)
900 d
->mouseButtonPressed
= true;
902 d
->initialPressPosition
= event
->pos();
903 d
->initialPressPosition
.setY(d
->initialPressPosition
.y() +
905 d
->initialPressPosition
.setX(d
->initialPressPosition
.x() +
909 QListView::mousePressEvent(event
);
911 viewport()->update(d
->categoryVisualRect(d
->hoveredCategory
));
914 void KCategorizedView::mouseReleaseEvent(QMouseEvent
*event
)
916 d
->mouseButtonPressed
= false;
918 QListView::mouseReleaseEvent(event
);
920 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
921 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
926 QPoint initialPressPosition
= viewport()->mapFromGlobal(QCursor::pos());
927 initialPressPosition
.setY(initialPressPosition
.y() + verticalOffset());
928 initialPressPosition
.setX(initialPressPosition
.x() + horizontalOffset());
930 QItemSelection selection
;
931 QItemSelection deselection
;
933 if (initialPressPosition
== d
->initialPressPosition
)
935 foreach(const QString
&category
, d
->categories
)
937 if (d
->categoryVisualRect(category
).contains(event
->pos()))
939 foreach (const QModelIndex
&index
, d
->categoriesIndexes
[category
])
941 QModelIndex selectIndex
= index
.model()->index(index
.row(), 0);
943 if (!d
->lastSelection
.contains(selectIndex
))
945 selection
<< QItemSelectionRange(selectIndex
);
949 deselection
<< QItemSelectionRange(selectIndex
);
953 selectionModel()->select(selection
, QItemSelectionModel::Select
);
954 selectionModel()->select(deselection
, QItemSelectionModel::Deselect
);
961 d
->lastSelection
= selectionModel()->selection();
963 if (d
->hovered
.isValid())
964 viewport()->update(visualRect(d
->hovered
));
965 else if (!d
->hoveredCategory
.isEmpty())
966 viewport()->update(d
->categoryVisualRect(d
->hoveredCategory
));
969 void KCategorizedView::leaveEvent(QEvent
*event
)
971 d
->hovered
= QModelIndex();
972 d
->hoveredCategory
= QString();
974 QListView::leaveEvent(event
);
977 void KCategorizedView::startDrag(Qt::DropActions supportedActions
)
979 // FIXME: QAbstractItemView does far better here since it sets the
980 // pixmap of selected icons to the dragging cursor, but it sets a non
981 // ARGB window so it is no transparent. Use QAbstractItemView when
982 // this is fixed on Qt.
983 //QListView::startDrag(supportedActions);
984 QAbstractItemView::startDrag(supportedActions
);
986 d
->isDragging
= false;
987 d
->mouseButtonPressed
= false;
989 viewport()->update(d
->lastDraggedItemsRect
);
992 void KCategorizedView::dragMoveEvent(QDragMoveEvent
*event
)
994 d
->mousePosition
= event
->pos();
996 if (d
->mouseButtonPressed
)
998 d
->isDragging
= true;
1002 d
->isDragging
= false;
1005 d
->dragLeftViewport
= false;
1007 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
1008 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
1010 QListView::dragMoveEvent(event
);
1014 d
->drawDraggedItems();
1017 void KCategorizedView::dragLeaveEvent(QDragLeaveEvent
*event
)
1019 d
->dragLeftViewport
= true;
1021 QListView::dragLeaveEvent(event
);
1024 QModelIndex
KCategorizedView::moveCursor(CursorAction cursorAction
,
1025 Qt::KeyboardModifiers modifiers
)
1027 if ((viewMode() != KCategorizedView::IconMode
) ||
1029 !d
->categoryDrawer
||
1030 d
->categories
.isEmpty() ||
1031 !d
->proxyModel
->isCategorizedModel()
1034 return QListView::moveCursor(cursorAction
, modifiers
);
1037 const QModelIndex current
= selectionModel()->currentIndex();
1039 int viewportWidth
= viewport()->width() - spacing();
1042 if (gridSize().isEmpty())
1044 itemWidth
= d
->biggestItemSize
.width();
1048 itemWidth
= gridSize().width();
1051 int itemWidthPlusSeparation
= spacing() + itemWidth
;
1052 int elementsPerRow
= viewportWidth
/ itemWidthPlusSeparation
;
1054 QString lastCategory
= d
->categories
.first();
1055 QString theCategory
= d
->categories
.first();
1056 QString afterCategory
= d
->categories
.first();
1058 bool hasToBreak
= false;
1059 foreach (const QString
&category
, d
->categories
)
1063 afterCategory
= category
;
1068 if (category
== d
->elementsInfo
[current
.row()].category
)
1070 theCategory
= category
;
1077 lastCategory
= category
;
1081 switch (cursorAction
)
1083 case QAbstractItemView::MoveUp
: {
1084 if (d
->elementsInfo
[current
.row()].relativeOffsetToCategory
>= elementsPerRow
)
1086 int indexToMove
= current
.row();
1087 indexToMove
-= qMin(((d
->elementsInfo
[current
.row()].relativeOffsetToCategory
) + d
->forcedSelectionPosition
), elementsPerRow
- d
->forcedSelectionPosition
+ (d
->elementsInfo
[current
.row()].relativeOffsetToCategory
% elementsPerRow
));
1089 return d
->proxyModel
->index(indexToMove
, 0);
1093 int lastCategoryLastRow
= (d
->categoriesIndexes
[lastCategory
].count() - 1) % elementsPerRow
;
1094 int indexToMove
= current
.row() - d
->elementsInfo
[current
.row()].relativeOffsetToCategory
;
1096 if (d
->forcedSelectionPosition
>= lastCategoryLastRow
)
1102 indexToMove
-= qMin((lastCategoryLastRow
- d
->forcedSelectionPosition
+ 1), d
->forcedSelectionPosition
+ elementsPerRow
+ 1);
1105 return d
->proxyModel
->index(indexToMove
, 0);
1109 case QAbstractItemView::MoveDown
: {
1110 if (d
->elementsInfo
[current
.row()].relativeOffsetToCategory
< (d
->categoriesIndexes
[theCategory
].count() - 1 - ((d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
)))
1112 int indexToMove
= current
.row();
1113 indexToMove
+= qMin(elementsPerRow
, d
->categoriesIndexes
[theCategory
].count() - 1 - d
->elementsInfo
[current
.row()].relativeOffsetToCategory
);
1115 return d
->proxyModel
->index(indexToMove
, 0);
1119 int afterCategoryLastRow
= qMin(elementsPerRow
, d
->categoriesIndexes
[afterCategory
].count());
1120 int indexToMove
= current
.row() + (d
->categoriesIndexes
[theCategory
].count() - d
->elementsInfo
[current
.row()].relativeOffsetToCategory
);
1122 if (d
->forcedSelectionPosition
>= afterCategoryLastRow
)
1124 indexToMove
+= afterCategoryLastRow
- 1;
1128 indexToMove
+= qMin(d
->forcedSelectionPosition
, elementsPerRow
);
1131 return d
->proxyModel
->index(indexToMove
, 0);
1135 case QAbstractItemView::MoveLeft
:
1136 d
->forcedSelectionPosition
= d
->elementsInfo
[current
.row() - 1].relativeOffsetToCategory
% elementsPerRow
;
1138 if (d
->forcedSelectionPosition
< 0)
1139 d
->forcedSelectionPosition
= (d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
;
1141 return d
->proxyModel
->index(current
.row() - 1, 0);
1143 case QAbstractItemView::MoveRight
:
1144 d
->forcedSelectionPosition
= d
->elementsInfo
[current
.row() + 1].relativeOffsetToCategory
% elementsPerRow
;
1146 if (d
->forcedSelectionPosition
< 0)
1147 d
->forcedSelectionPosition
= (d
->categoriesIndexes
[theCategory
].count() - 1) % elementsPerRow
;
1149 return d
->proxyModel
->index(current
.row() + 1, 0);
1155 return QListView::moveCursor(cursorAction
, modifiers
);
1158 void KCategorizedView::rowsInserted(const QModelIndex
&parent
,
1162 QListView::rowsInserted(parent
, start
, end
);
1164 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
1165 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
1167 d
->lastSelection
= QItemSelection();
1168 d
->currentViewIndex
= QModelIndex();
1169 d
->forcedSelectionPosition
= 0;
1170 d
->elementsInfo
.clear();
1171 d
->elementsPosition
.clear();
1172 d
->categoriesIndexes
.clear();
1173 d
->categoriesPosition
.clear();
1174 d
->categories
.clear();
1175 d
->intersectedIndexes
.clear();
1176 d
->modelIndexList
.clear();
1177 d
->hovered
= QModelIndex();
1178 d
->biggestItemSize
= QSize(0, 0);
1179 d
->mouseButtonPressed
= false;
1184 rowsInsertedArtifficial(parent
, start
, end
);
1187 void KCategorizedView::rowsInsertedArtifficial(const QModelIndex
&parent
,
1193 d
->lastSelection
= QItemSelection();
1194 d
->currentViewIndex
= QModelIndex();
1195 d
->forcedSelectionPosition
= 0;
1196 d
->elementsInfo
.clear();
1197 d
->elementsPosition
.clear();
1198 d
->categoriesIndexes
.clear();
1199 d
->categoriesPosition
.clear();
1200 d
->categories
.clear();
1201 d
->intersectedIndexes
.clear();
1202 d
->modelIndexList
.clear();
1203 d
->hovered
= QModelIndex();
1204 d
->biggestItemSize
= QSize(0, 0);
1205 d
->mouseButtonPressed
= false;
1207 if (start
> end
|| end
< 0 || start
< 0 || !d
->proxyModel
->rowCount())
1212 // Add all elements mapped to the source model
1213 for (int k
= 0; k
< d
->proxyModel
->rowCount(); k
++)
1215 d
->biggestItemSize
= QSize(qMax(sizeHintForIndex(d
->proxyModel
->index(k
, 0)).width(),
1216 d
->biggestItemSize
.width()),
1217 qMax(sizeHintForIndex(d
->proxyModel
->index(k
, 0)).height(),
1218 d
->biggestItemSize
.height()));
1220 d
->modelIndexList
<< d
->proxyModel
->index(k
, d
->proxyModel
->sortColumn());
1223 // Explore categories
1224 QString prevCategory
= d
->proxyModel
->data(d
->modelIndexList
[0], KCategorizedSortFilterProxyModel::CategoryRole
).toString();
1225 QString lastCategory
= prevCategory
;
1226 QModelIndexList modelIndexList
;
1227 struct Private::ElementInfo elementInfo
;
1229 foreach (const QModelIndex
&index
, d
->modelIndexList
)
1231 lastCategory
= d
->proxyModel
->data(index
, KCategorizedSortFilterProxyModel::CategoryRole
).toString();
1233 elementInfo
.category
= lastCategory
;
1235 if (prevCategory
!= lastCategory
)
1238 d
->categoriesIndexes
.insert(prevCategory
, modelIndexList
);
1239 d
->categories
<< prevCategory
;
1240 modelIndexList
.clear();
1247 elementInfo
.relativeOffsetToCategory
= offset
;
1249 modelIndexList
<< index
;
1250 prevCategory
= lastCategory
;
1252 d
->elementsInfo
.insert(index
.row(), elementInfo
);
1255 d
->categoriesIndexes
.insert(prevCategory
, modelIndexList
);
1256 d
->categories
<< prevCategory
;
1258 d
->updateScrollbars();
1261 void KCategorizedView::rowsRemoved(const QModelIndex
&parent
,
1265 if ((viewMode() == KCategorizedView::IconMode
) && d
->proxyModel
&&
1266 d
->categoryDrawer
&& d
->proxyModel
->isCategorizedModel())
1268 // Force the view to update all elements
1269 rowsInsertedArtifficial(parent
, start
, end
);
1273 void KCategorizedView::updateGeometries()
1275 if ((viewMode() != KCategorizedView::IconMode
) || !d
->proxyModel
||
1276 !d
->categoryDrawer
|| !d
->proxyModel
->isCategorizedModel())
1278 QListView::updateGeometries();
1282 // Avoid QListView::updateGeometries(), since it will try to set another
1283 // range to our scroll bars, what we don't want (ereslibre)
1284 QAbstractItemView::updateGeometries();
1287 void KCategorizedView::slotLayoutChanged()
1289 if ((viewMode() == KCategorizedView::IconMode
) && d
->proxyModel
&&
1290 d
->categoryDrawer
&& d
->proxyModel
->isCategorizedModel())
1292 // Force the view to update all elements
1293 rowsInsertedArtifficial(QModelIndex(), 0, d
->proxyModel
->rowCount() - 1);
1297 #include "kcategorizedview.moc"