]> cloud.milkyroute.net Git - dolphin.git/blob - src/kcategorizedview.cpp
Allow turning off the feature that additional information might be shown in the icons...
[dolphin.git] / src / kcategorizedview.cpp
1 /**
2 * This file is part of the KDE project
3 * Copyright (C) 2007 Rafael Fernández López <ereslibre@kde.org>
4 *
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.
9 *
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.
14 *
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.
19 */
20
21 #include "kcategorizedview.h"
22 #include "kcategorizedview_p.h"
23
24 #include <math.h> // trunc on C99 compliant systems
25 #include <kdefakes.h> // trunc for not C99 compliant systems
26
27 #include <QApplication>
28 #include <QPainter>
29 #include <QScrollBar>
30 #include <QPaintEvent>
31
32 #include <kstyle.h>
33
34 #include "kcategorydrawer.h"
35 #include "kcategorizedsortfilterproxymodel.h"
36
37 // By defining DOLPHIN_DRAGANDDROP the custom drag and drop implementation of
38 // KCategorizedView is bypassed to have a consistent drag and drop look for all
39 // views. Hopefully transparent pixmaps for drag objects will be supported in
40 // Qt 4.4, so that this workaround can be skipped.
41 #define DOLPHIN_DRAGANDDROP
42
43 KCategorizedView::Private::Private(KCategorizedView *listView)
44 : listView(listView)
45 , categoryDrawer(0)
46 , biggestItemSize(QSize(0, 0))
47 , mouseButtonPressed(false)
48 , isDragging(false)
49 , dragLeftViewport(false)
50 , proxyModel(0)
51 {
52 }
53
54 KCategorizedView::Private::~Private()
55 {
56 }
57
58 const QModelIndexList &KCategorizedView::Private::intersectionSet(const QRect &rect)
59 {
60 QModelIndex index;
61 QRect indexVisualRect;
62
63 intersectedIndexes.clear();
64
65 int itemHeight;
66
67 if (listView->gridSize().isEmpty())
68 {
69 itemHeight = biggestItemSize.height();
70 }
71 else
72 {
73 itemHeight = listView->gridSize().height();
74 }
75
76 // Lets find out where we should start
77 int top = proxyModel->rowCount() - 1;
78 int bottom = 0;
79 int middle = (top + bottom) / 2;
80 while (bottom <= top)
81 {
82 middle = (top + bottom) / 2;
83
84 index = proxyModel->index(middle, 0);
85 indexVisualRect = visualRect(index);
86 // We need the whole height (not only the visualRect). This will help us to update
87 // all needed indexes correctly (ereslibre)
88 indexVisualRect.setHeight(indexVisualRect.height() + (itemHeight - indexVisualRect.height()));
89
90 if (qMax(indexVisualRect.topLeft().y(),
91 indexVisualRect.bottomRight().y()) < qMin(rect.topLeft().y(),
92 rect.bottomRight().y()))
93 {
94 bottom = middle + 1;
95 }
96 else
97 {
98 top = middle - 1;
99 }
100 }
101
102 for (int i = middle; i < proxyModel->rowCount(); i++)
103 {
104 index = proxyModel->index(i, 0);
105 indexVisualRect = visualRect(index);
106
107 if (rect.intersects(indexVisualRect))
108 intersectedIndexes.append(index);
109
110 // If we passed next item, stop searching for hits
111 if (qMax(rect.bottomRight().y(), rect.topLeft().y()) <
112 qMin(indexVisualRect.topLeft().y(),
113 indexVisualRect.bottomRight().y()))
114 break;
115 }
116
117 return intersectedIndexes;
118 }
119
120 QRect KCategorizedView::Private::visualRectInViewport(const QModelIndex &index) const
121 {
122 if (!index.isValid())
123 return QRect();
124
125 QString curCategory = elementsInfo[index.row()].category;
126
127 QRect retRect;
128
129 if (listView->layoutDirection() == Qt::LeftToRight)
130 {
131 retRect = QRect(listView->spacing(), listView->spacing() * 2 +
132 categoryDrawer->categoryHeight(listView->viewOptions()), 0, 0);
133 }
134 else
135 {
136 retRect = QRect(listView->viewport()->width() - listView->spacing(), listView->spacing() * 2 +
137 categoryDrawer->categoryHeight(listView->viewOptions()), 0, 0);
138 }
139
140 int viewportWidth = listView->viewport()->width() - listView->spacing();
141
142 int itemHeight;
143 int itemWidth;
144
145 if (listView->gridSize().isEmpty())
146 {
147 itemHeight = biggestItemSize.height();
148 itemWidth = biggestItemSize.width();
149 }
150 else
151 {
152 itemHeight = listView->gridSize().height();
153 itemWidth = listView->gridSize().width();
154 }
155
156 int itemWidthPlusSeparation = listView->spacing() + itemWidth;
157 int elementsPerRow = viewportWidth / itemWidthPlusSeparation;
158 if (!elementsPerRow)
159 elementsPerRow++;
160
161 int column = elementsInfo[index.row()].relativeOffsetToCategory % elementsPerRow;
162 int row = elementsInfo[index.row()].relativeOffsetToCategory / elementsPerRow;
163
164 if (listView->layoutDirection() == Qt::LeftToRight)
165 {
166 retRect.setLeft(retRect.left() + column * listView->spacing() +
167 column * itemWidth);
168 }
169 else
170 {
171 retRect.setLeft(retRect.right() - column * listView->spacing() -
172 column * itemWidth - itemWidth);
173
174 retRect.setRight(retRect.right() - column * listView->spacing() -
175 column * itemWidth);
176 }
177
178 foreach (const QString &category, categories)
179 {
180 if (category == curCategory)
181 break;
182
183 float rows = (float) ((float) categoriesIndexes[category].count() /
184 (float) elementsPerRow);
185
186 int rowsInt = categoriesIndexes[category].count() / elementsPerRow;
187
188 if (rows - trunc(rows)) rowsInt++;
189
190 retRect.setTop(retRect.top() +
191 (rowsInt * itemHeight) +
192 categoryDrawer->categoryHeight(listView->viewOptions()) +
193 listView->spacing() * 2);
194
195 if (listView->gridSize().isEmpty())
196 {
197 retRect.setTop(retRect.top() +
198 (rowsInt * listView->spacing()));
199 }
200 }
201
202 if (listView->gridSize().isEmpty())
203 {
204 retRect.setTop(retRect.top() + row * listView->spacing() +
205 (row * itemHeight));
206 }
207 else
208 {
209 retRect.setTop(retRect.top() + (row * itemHeight));
210 }
211
212 retRect.setWidth(itemWidth);
213
214 QModelIndex heightIndex = proxyModel->index(index.row(), 0);
215 if (listView->gridSize().isEmpty())
216 {
217 retRect.setHeight(listView->sizeHintForIndex(heightIndex).height());
218 }
219 else
220 {
221 retRect.setHeight(qMin(listView->sizeHintForIndex(heightIndex).height(),
222 listView->gridSize().height()));
223 }
224
225 return retRect;
226 }
227
228 QRect KCategorizedView::Private::visualCategoryRectInViewport(const QString &category)
229 const
230 {
231 QRect retRect(listView->spacing(),
232 listView->spacing(),
233 listView->viewport()->width() - listView->spacing() * 2,
234 0);
235
236 if (!proxyModel->rowCount() || !categories.contains(category))
237 return QRect();
238
239 QModelIndex index = proxyModel->index(0, 0, QModelIndex());
240
241 int viewportWidth = listView->viewport()->width() - listView->spacing();
242
243 int itemHeight;
244 int itemWidth;
245
246 if (listView->gridSize().isEmpty())
247 {
248 itemHeight = biggestItemSize.height();
249 itemWidth = biggestItemSize.width();
250 }
251 else
252 {
253 itemHeight = listView->gridSize().height();
254 itemWidth = listView->gridSize().width();
255 }
256
257 int itemWidthPlusSeparation = listView->spacing() + itemWidth;
258 int elementsPerRow = viewportWidth / itemWidthPlusSeparation;
259
260 if (!elementsPerRow)
261 elementsPerRow++;
262
263 foreach (const QString &itCategory, categories)
264 {
265 if (itCategory == category)
266 break;
267
268 float rows = (float) ((float) categoriesIndexes[itCategory].count() /
269 (float) elementsPerRow);
270 int rowsInt = categoriesIndexes[itCategory].count() / elementsPerRow;
271
272 if (rows - trunc(rows)) rowsInt++;
273
274 retRect.setTop(retRect.top() +
275 (rowsInt * itemHeight) +
276 categoryDrawer->categoryHeight(listView->viewOptions()) +
277 listView->spacing() * 2);
278
279 if (listView->gridSize().isEmpty())
280 {
281 retRect.setTop(retRect.top() +
282 (rowsInt * listView->spacing()));
283 }
284 }
285
286 retRect.setHeight(categoryDrawer->categoryHeight(listView->viewOptions()));
287
288 return retRect;
289 }
290
291 // We're sure elementsPosition doesn't contain index
292 const QRect &KCategorizedView::Private::cacheIndex(const QModelIndex &index)
293 {
294 QRect rect = visualRectInViewport(index);
295 elementsPosition[index.row()] = rect;
296
297 return elementsPosition[index.row()];
298 }
299
300 // We're sure categoriesPosition doesn't contain category
301 const QRect &KCategorizedView::Private::cacheCategory(const QString &category)
302 {
303 QRect rect = visualCategoryRectInViewport(category);
304 categoriesPosition[category] = rect;
305
306 return categoriesPosition[category];
307 }
308
309 const QRect &KCategorizedView::Private::cachedRectIndex(const QModelIndex &index)
310 {
311 if (elementsPosition.contains(index.row())) // If we have it cached
312 { // return it
313 return elementsPosition[index.row()];
314 }
315 else // Otherwise, cache it
316 { // and return it
317 return cacheIndex(index);
318 }
319 }
320
321 const QRect &KCategorizedView::Private::cachedRectCategory(const QString &category)
322 {
323 if (categoriesPosition.contains(category)) // If we have it cached
324 { // return it
325 return categoriesPosition[category];
326 }
327 else // Otherwise, cache it and
328 { // return it
329 return cacheCategory(category);
330 }
331 }
332
333 QRect KCategorizedView::Private::visualRect(const QModelIndex &index)
334 {
335 QRect retRect = cachedRectIndex(index);
336 int dx = -listView->horizontalOffset();
337 int dy = -listView->verticalOffset();
338 retRect.adjust(dx, dy, dx, dy);
339
340 return retRect;
341 }
342
343 QRect KCategorizedView::Private::categoryVisualRect(const QString &category)
344 {
345 QRect retRect = cachedRectCategory(category);
346 int dx = -listView->horizontalOffset();
347 int dy = -listView->verticalOffset();
348 retRect.adjust(dx, dy, dx, dy);
349
350 return retRect;
351 }
352
353 void KCategorizedView::Private::drawNewCategory(const QModelIndex &index,
354 int sortRole,
355 const QStyleOption &option,
356 QPainter *painter)
357 {
358 if (!index.isValid())
359 {
360 return;
361 }
362
363 QStyleOption optionCopy = option;
364 const QString category = proxyModel->data(index, KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString();
365
366 optionCopy.state &= ~QStyle::State_Selected;
367
368 if ((category == hoveredCategory) && !mouseButtonPressed)
369 {
370 optionCopy.state |= QStyle::State_MouseOver;
371 }
372 else if ((category == hoveredCategory) && mouseButtonPressed)
373 {
374 QPoint initialPressPosition = listView->viewport()->mapFromGlobal(QCursor::pos());
375 initialPressPosition.setY(initialPressPosition.y() + listView->verticalOffset());
376 initialPressPosition.setX(initialPressPosition.x() + listView->horizontalOffset());
377
378 if (initialPressPosition == this->initialPressPosition)
379 {
380 optionCopy.state |= QStyle::State_Selected;
381 }
382 }
383
384 categoryDrawer->drawCategory(index,
385 sortRole,
386 optionCopy,
387 painter);
388 }
389
390
391 void KCategorizedView::Private::updateScrollbars()
392 {
393 // find the last index in the last category
394 QModelIndex lastIndex = categoriesIndexes.isEmpty() ? QModelIndex() : categoriesIndexes[categories.last()].last();
395
396 int lastItemBottom = cachedRectIndex(lastIndex).top() +
397 listView->spacing() + (listView->gridSize().isEmpty() ? 0 : listView->gridSize().height()) - listView->viewport()->height();
398
399 listView->horizontalScrollBar()->setRange(0, 0);
400
401 listView->verticalScrollBar()->setSingleStep(listView->viewport()->height() / 10);
402 listView->verticalScrollBar()->setPageStep(listView->viewport()->height());
403 listView->verticalScrollBar()->setRange(0, lastItemBottom);
404 }
405
406 void KCategorizedView::Private::drawDraggedItems(QPainter *painter)
407 {
408 QStyleOptionViewItemV3 option = listView->viewOptions();
409 option.state &= ~QStyle::State_MouseOver;
410 foreach (const QModelIndex &index, listView->selectionModel()->selectedIndexes())
411 {
412 const int dx = mousePosition.x() - initialPressPosition.x() + listView->horizontalOffset();
413 const int dy = mousePosition.y() - initialPressPosition.y() + listView->verticalOffset();
414
415 option.rect = visualRect(index);
416 option.rect.adjust(dx, dy, dx, dy);
417
418 if (option.rect.intersects(listView->viewport()->rect()))
419 {
420 listView->itemDelegate(index)->paint(painter, option, index);
421 }
422 }
423 }
424
425 void KCategorizedView::Private::layoutChanged(bool forceItemReload)
426 {
427 if ((listView->viewMode() == KCategorizedView::IconMode) && proxyModel &&
428 categoryDrawer && proxyModel->isCategorizedModel() &&
429 ((forceItemReload ||
430 (modelSortRole != proxyModel->sortRole()) ||
431 (modelSortColumn != proxyModel->sortColumn()) ||
432 (modelSortOrder != proxyModel->sortOrder()) ||
433 (modelLastRowCount != proxyModel->rowCount()) ||
434 (modelCategorized != proxyModel->isCategorizedModel()))))
435 {
436 // Force the view to update all elements
437 listView->rowsInsertedArtifficial(QModelIndex(), 0, proxyModel->rowCount() - 1);
438
439 if (!forceItemReload)
440 {
441 modelSortRole = proxyModel->sortRole();
442 modelSortColumn = proxyModel->sortColumn();
443 modelSortOrder = proxyModel->sortOrder();
444 modelLastRowCount = proxyModel->rowCount();
445 modelCategorized = proxyModel->isCategorizedModel();
446 }
447 }
448 else if ((listView->viewMode() == KCategorizedView::IconMode) && proxyModel &&
449 categoryDrawer && proxyModel->isCategorizedModel())
450 {
451 updateScrollbars();
452 }
453 }
454
455 void KCategorizedView::Private::drawDraggedItems()
456 {
457 QRect rectToUpdate;
458 QRect currentRect;
459 foreach (const QModelIndex &index, listView->selectionModel()->selectedIndexes())
460 {
461 int dx = mousePosition.x() - initialPressPosition.x() + listView->horizontalOffset();
462 int dy = mousePosition.y() - initialPressPosition.y() + listView->verticalOffset();
463
464 currentRect = visualRect(index);
465 currentRect.adjust(dx, dy, dx, dy);
466
467 if (currentRect.intersects(listView->viewport()->rect()))
468 {
469 rectToUpdate = rectToUpdate.united(currentRect);
470 }
471 }
472
473 listView->viewport()->update(lastDraggedItemsRect.united(rectToUpdate));
474
475 lastDraggedItemsRect = rectToUpdate;
476 }
477
478
479 //==============================================================================
480
481
482 KCategorizedView::KCategorizedView(QWidget *parent)
483 : QListView(parent)
484 , d(new Private(this))
485 {
486 }
487
488 KCategorizedView::~KCategorizedView()
489 {
490 delete d;
491 }
492
493 void KCategorizedView::setGridSize(const QSize &size)
494 {
495 QListView::setGridSize(size);
496
497 d->layoutChanged(true);
498 }
499
500 void KCategorizedView::setModel(QAbstractItemModel *model)
501 {
502 d->lastSelection = QItemSelection();
503 d->currentViewIndex = QModelIndex();
504 d->forcedSelectionPosition = 0;
505 d->elementsInfo.clear();
506 d->elementsPosition.clear();
507 d->categoriesIndexes.clear();
508 d->categoriesPosition.clear();
509 d->categories.clear();
510 d->intersectedIndexes.clear();
511 d->modelIndexList.clear();
512 d->hovered = QModelIndex();
513 d->mouseButtonPressed = false;
514
515 if (d->proxyModel)
516 {
517 QObject::disconnect(d->proxyModel,
518 SIGNAL(layoutChanged()),
519 this, SLOT(slotLayoutChanged()));
520
521 QObject::disconnect(d->proxyModel,
522 SIGNAL(dataChanged(QModelIndex,QModelIndex)),
523 this, SLOT(slotLayoutChanged()));
524
525 QObject::disconnect(d->proxyModel,
526 SIGNAL(rowsRemoved(QModelIndex,int,int)),
527 this, SLOT(rowsRemoved(QModelIndex,int,int)));
528 }
529
530 QListView::setModel(model);
531
532 d->proxyModel = dynamic_cast<KCategorizedSortFilterProxyModel*>(model);
533
534 if (d->proxyModel)
535 {
536 d->modelSortRole = d->proxyModel->sortRole();
537 d->modelSortColumn = d->proxyModel->sortColumn();
538 d->modelSortOrder = d->proxyModel->sortOrder();
539 d->modelLastRowCount = d->proxyModel->rowCount();
540 d->modelCategorized = d->proxyModel->isCategorizedModel();
541
542 QObject::connect(d->proxyModel,
543 SIGNAL(layoutChanged()),
544 this, SLOT(slotLayoutChanged()));
545
546 QObject::connect(d->proxyModel,
547 SIGNAL(dataChanged(QModelIndex,QModelIndex)),
548 this, SLOT(slotLayoutChanged()));
549
550 QObject::connect(d->proxyModel,
551 SIGNAL(rowsRemoved(QModelIndex,int,int)),
552 this, SLOT(rowsRemoved(QModelIndex,int,int)));
553
554 if (d->proxyModel->rowCount())
555 {
556 d->layoutChanged(true);
557 }
558 }
559 else
560 {
561 d->modelCategorized = false;
562 }
563 }
564
565 QRect KCategorizedView::visualRect(const QModelIndex &index) const
566 {
567 if ((viewMode() != KCategorizedView::IconMode) || !d->proxyModel ||
568 !d->categoryDrawer || !d->proxyModel->isCategorizedModel())
569 {
570 return QListView::visualRect(index);
571 }
572
573 if (!qobject_cast<const QSortFilterProxyModel*>(index.model()))
574 {
575 return d->visualRect(d->proxyModel->mapFromSource(index));
576 }
577
578 return d->visualRect(index);
579 }
580
581 KCategoryDrawer *KCategorizedView::categoryDrawer() const
582 {
583 return d->categoryDrawer;
584 }
585
586 void KCategorizedView::setCategoryDrawer(KCategoryDrawer *categoryDrawer)
587 {
588 d->lastSelection = QItemSelection();
589 d->currentViewIndex = QModelIndex();
590 d->forcedSelectionPosition = 0;
591 d->elementsInfo.clear();
592 d->elementsPosition.clear();
593 d->categoriesIndexes.clear();
594 d->categoriesPosition.clear();
595 d->categories.clear();
596 d->intersectedIndexes.clear();
597 d->modelIndexList.clear();
598 d->hovered = QModelIndex();
599 d->mouseButtonPressed = false;
600
601 if (!categoryDrawer && d->proxyModel)
602 {
603 QObject::disconnect(d->proxyModel,
604 SIGNAL(layoutChanged()),
605 this, SLOT(slotLayoutChanged()));
606
607 QObject::disconnect(d->proxyModel,
608 SIGNAL(dataChanged(QModelIndex,QModelIndex)),
609 this, SLOT(slotLayoutChanged()));
610
611 QObject::disconnect(d->proxyModel,
612 SIGNAL(rowsRemoved(QModelIndex,int,int)),
613 this, SLOT(rowsRemoved(QModelIndex,int,int)));
614 }
615 else if (categoryDrawer && d->proxyModel)
616 {
617 QObject::connect(d->proxyModel,
618 SIGNAL(layoutChanged()),
619 this, SLOT(slotLayoutChanged()));
620
621 QObject::connect(d->proxyModel,
622 SIGNAL(dataChanged(QModelIndex,QModelIndex)),
623 this, SLOT(slotLayoutChanged()));
624
625 QObject::connect(d->proxyModel,
626 SIGNAL(rowsRemoved(QModelIndex,int,int)),
627 this, SLOT(rowsRemoved(QModelIndex,int,int)));
628 }
629
630 d->categoryDrawer = categoryDrawer;
631
632 if (categoryDrawer)
633 {
634 if (d->proxyModel)
635 {
636 if (d->proxyModel->rowCount())
637 {
638 d->layoutChanged(true);
639 }
640 }
641 }
642 else
643 {
644 updateGeometries();
645 }
646 }
647
648 QModelIndex KCategorizedView::indexAt(const QPoint &point) const
649 {
650 if ((viewMode() != KCategorizedView::IconMode) || !d->proxyModel ||
651 !d->categoryDrawer || !d->proxyModel->isCategorizedModel())
652 {
653 return QListView::indexAt(point);
654 }
655
656 QModelIndex index;
657
658 QModelIndexList item = d->intersectionSet(QRect(point, point));
659
660 if (item.count() == 1)
661 {
662 index = item[0];
663 }
664
665 d->hovered = index;
666
667 return index;
668 }
669
670 void KCategorizedView::reset()
671 {
672 QListView::reset();
673
674 d->lastSelection = QItemSelection();
675 d->currentViewIndex = QModelIndex();
676 d->forcedSelectionPosition = 0;
677 d->elementsInfo.clear();
678 d->elementsPosition.clear();
679 d->categoriesIndexes.clear();
680 d->categoriesPosition.clear();
681 d->categories.clear();
682 d->intersectedIndexes.clear();
683 d->modelIndexList.clear();
684 d->hovered = QModelIndex();
685 d->biggestItemSize = QSize(0, 0);
686 d->mouseButtonPressed = false;
687 }
688
689 void KCategorizedView::paintEvent(QPaintEvent *event)
690 {
691 if ((viewMode() != KCategorizedView::IconMode) || !d->proxyModel ||
692 !d->categoryDrawer || !d->proxyModel->isCategorizedModel())
693 {
694 QListView::paintEvent(event);
695 return;
696 }
697
698 QStyleOptionViewItemV3 option = viewOptions();
699 option.widget = this;
700 if (wordWrap())
701 {
702 option.features |= QStyleOptionViewItemV2::WrapText;
703 }
704
705 QPainter painter(viewport());
706 QRect area = event->rect();
707 const bool focus = (hasFocus() || viewport()->hasFocus()) &&
708 currentIndex().isValid();
709 const QStyle::State state = option.state;
710 const bool enabled = (state & QStyle::State_Enabled) != 0;
711
712 painter.save();
713
714 QModelIndexList dirtyIndexes = d->intersectionSet(area);
715 foreach (const QModelIndex &index, dirtyIndexes)
716 {
717 option.state = state;
718 option.rect = visualRect(index);
719
720 if (selectionModel() && selectionModel()->isSelected(index))
721 {
722 option.state |= QStyle::State_Selected;
723 }
724
725 if (enabled)
726 {
727 QPalette::ColorGroup cg;
728 if ((d->proxyModel->flags(index) & Qt::ItemIsEnabled) == 0)
729 {
730 option.state &= ~QStyle::State_Enabled;
731 cg = QPalette::Disabled;
732 }
733 else
734 {
735 cg = QPalette::Normal;
736 }
737 option.palette.setCurrentColorGroup(cg);
738 }
739
740 if (focus && currentIndex() == index)
741 {
742 option.state |= QStyle::State_HasFocus;
743 if (this->state() == EditingState)
744 option.state |= QStyle::State_Editing;
745 }
746
747 if ((index == d->hovered) && !d->mouseButtonPressed)
748 option.state |= QStyle::State_MouseOver;
749 else
750 option.state &= ~QStyle::State_MouseOver;
751
752 itemDelegate(index)->paint(&painter, option, index);
753 }
754
755 // Redraw categories
756 QStyleOptionViewItem otherOption;
757 bool intersectedInThePast = false;
758 foreach (const QString &category, d->categories)
759 {
760 otherOption = option;
761 otherOption.rect = d->categoryVisualRect(category);
762 otherOption.state &= ~QStyle::State_MouseOver;
763
764 if (otherOption.rect.intersects(area))
765 {
766 intersectedInThePast = true;
767
768 QModelIndex indexToDraw = d->proxyModel->index(d->categoriesIndexes[category][0].row(), d->proxyModel->sortColumn());
769
770 d->drawNewCategory(indexToDraw,
771 d->proxyModel->sortRole(), otherOption, &painter);
772 }
773 else if (intersectedInThePast)
774 {
775 break; // the visible area has been finished, we don't need to keep asking, the rest won't intersect
776 // this is doable because we know that categories are correctly ordered on the list
777 }
778 }
779
780 if (d->mouseButtonPressed && !d->isDragging)
781 {
782 QPoint start, end, initialPressPosition;
783
784 initialPressPosition = d->initialPressPosition;
785
786 initialPressPosition.setY(initialPressPosition.y() - verticalOffset());
787 initialPressPosition.setX(initialPressPosition.x() - horizontalOffset());
788
789 if (d->initialPressPosition.x() > d->mousePosition.x() ||
790 d->initialPressPosition.y() > d->mousePosition.y())
791 {
792 start = d->mousePosition;
793 end = initialPressPosition;
794 }
795 else
796 {
797 start = initialPressPosition;
798 end = d->mousePosition;
799 }
800
801 QStyleOptionRubberBand yetAnotherOption;
802 yetAnotherOption.initFrom(this);
803 yetAnotherOption.shape = QRubberBand::Rectangle;
804 yetAnotherOption.opaque = false;
805 yetAnotherOption.rect = QRect(start, end).intersected(viewport()->rect().adjusted(-16, -16, 16, 16));
806 painter.save();
807 style()->drawControl(QStyle::CE_RubberBand, &yetAnotherOption, &painter);
808 painter.restore();
809 }
810
811 if (d->isDragging && !d->dragLeftViewport)
812 {
813 painter.setOpacity(0.5);
814 d->drawDraggedItems(&painter);
815 }
816
817 painter.restore();
818 }
819
820 void KCategorizedView::resizeEvent(QResizeEvent *event)
821 {
822 QListView::resizeEvent(event);
823
824 // Clear the items positions cache
825 d->elementsPosition.clear();
826 d->categoriesPosition.clear();
827 d->forcedSelectionPosition = 0;
828
829 if ((viewMode() != KCategorizedView::IconMode) || !d->proxyModel ||
830 !d->categoryDrawer || !d->proxyModel->isCategorizedModel())
831 {
832 return;
833 }
834
835 d->updateScrollbars();
836 }
837
838 void KCategorizedView::setSelection(const QRect &rect,
839 QItemSelectionModel::SelectionFlags flags)
840 {
841 if ((viewMode() != KCategorizedView::IconMode) || !d->proxyModel ||
842 !d->categoryDrawer || !d->proxyModel->isCategorizedModel())
843 {
844 QListView::setSelection(rect, flags);
845 return;
846 }
847
848 if (!flags)
849 return;
850
851 if (flags & QItemSelectionModel::Clear)
852 {
853 if (!rect.intersects(d->categoryVisualRect(d->hoveredCategory)))
854 {
855 d->lastSelection = QItemSelection();
856 }
857 }
858
859 selectionModel()->clear();
860
861 QModelIndexList dirtyIndexes = d->intersectionSet(rect);
862
863 if (!dirtyIndexes.count())
864 {
865 if (!d->lastSelection.isEmpty() &&
866 (rect.intersects(d->categoryVisualRect(d->hoveredCategory)) || d->mouseButtonPressed))
867 {
868 selectionModel()->select(d->lastSelection, flags);
869 }
870
871 return;
872 }
873
874 QItemSelection selection;
875
876 if (!d->mouseButtonPressed)
877 {
878 selection = QItemSelection(dirtyIndexes[0], dirtyIndexes[0]);
879 d->currentViewIndex = dirtyIndexes[0];
880 }
881 else
882 {
883 QModelIndex first = dirtyIndexes[0];
884 QModelIndex last;
885 foreach (const QModelIndex &index, dirtyIndexes)
886 {
887 if (last.isValid() && last.row() + 1 != index.row())
888 {
889 QItemSelectionRange range(first, last);
890
891 selection << range;
892
893 first = index;
894 }
895
896 last = index;
897 }
898
899 if (last.isValid())
900 selection << QItemSelectionRange(first, last);
901 }
902
903 if (d->lastSelection.count())
904 {
905 if ((selection.count() == 1) && (selection[0].indexes().count() == 1))
906 selection.merge(d->lastSelection, flags);
907 else
908 selection.merge(d->lastSelection, QItemSelectionModel::Select);
909 }
910
911 selectionModel()->select(selection, flags);
912 }
913
914 void KCategorizedView::mouseMoveEvent(QMouseEvent *event)
915 {
916 QListView::mouseMoveEvent(event);
917
918 if ((viewMode() != KCategorizedView::IconMode) || !d->proxyModel ||
919 !d->categoryDrawer || !d->proxyModel->isCategorizedModel())
920 {
921 return;
922 }
923
924 const QString previousHoveredCategory = d->hoveredCategory;
925
926 d->mousePosition = event->pos();
927 d->hoveredCategory = QString();
928
929 // Redraw categories
930 foreach (const QString &category, d->categories)
931 {
932 if (d->categoryVisualRect(category).intersects(QRect(event->pos(), event->pos())))
933 {
934 d->hoveredCategory = category;
935 viewport()->update(d->categoryVisualRect(category));
936 }
937 else if ((category == previousHoveredCategory) &&
938 (!d->categoryVisualRect(previousHoveredCategory).intersects(QRect(event->pos(), event->pos()))))
939 {
940 viewport()->update(d->categoryVisualRect(category));
941 }
942 }
943
944 QRect rect;
945 if (d->mouseButtonPressed && !d->isDragging)
946 {
947 QPoint start, end, initialPressPosition;
948
949 initialPressPosition = d->initialPressPosition;
950
951 initialPressPosition.setY(initialPressPosition.y() - verticalOffset());
952 initialPressPosition.setX(initialPressPosition.x() - horizontalOffset());
953
954 if (d->initialPressPosition.x() > d->mousePosition.x() ||
955 d->initialPressPosition.y() > d->mousePosition.y())
956 {
957 start = d->mousePosition;
958 end = initialPressPosition;
959 }
960 else
961 {
962 start = initialPressPosition;
963 end = d->mousePosition;
964 }
965
966 rect = QRect(start, end).intersected(viewport()->rect().adjusted(-16, -16, 16, 16));
967
968 //viewport()->update(rect.united(d->lastSelectionRect));
969
970 d->lastSelectionRect = rect;
971 }
972 }
973
974 void KCategorizedView::mousePressEvent(QMouseEvent *event)
975 {
976 d->dragLeftViewport = false;
977
978 if (event->button() == Qt::LeftButton)
979 {
980 d->mouseButtonPressed = true;
981
982 d->initialPressPosition = event->pos();
983 d->initialPressPosition.setY(d->initialPressPosition.y() +
984 verticalOffset());
985 d->initialPressPosition.setX(d->initialPressPosition.x() +
986 horizontalOffset());
987 }
988
989 QListView::mousePressEvent(event);
990
991 viewport()->update(d->categoryVisualRect(d->hoveredCategory));
992 }
993
994 void KCategorizedView::mouseReleaseEvent(QMouseEvent *event)
995 {
996 d->mouseButtonPressed = false;
997
998 QListView::mouseReleaseEvent(event);
999
1000 if ((viewMode() != KCategorizedView::IconMode) || !d->proxyModel ||
1001 !d->categoryDrawer || !d->proxyModel->isCategorizedModel())
1002 {
1003 return;
1004 }
1005
1006 QPoint initialPressPosition = viewport()->mapFromGlobal(QCursor::pos());
1007 initialPressPosition.setY(initialPressPosition.y() + verticalOffset());
1008 initialPressPosition.setX(initialPressPosition.x() + horizontalOffset());
1009
1010 QItemSelection selection;
1011 QItemSelection deselection;
1012
1013 if (initialPressPosition == d->initialPressPosition)
1014 {
1015 foreach(const QString &category, d->categories)
1016 {
1017 if (d->categoryVisualRect(category).contains(event->pos()))
1018 {
1019 foreach (const QModelIndex &index, d->categoriesIndexes[category])
1020 {
1021 QModelIndex selectIndex = index.model()->index(index.row(), 0);
1022
1023 if (!d->lastSelection.contains(selectIndex))
1024 {
1025 selection << QItemSelectionRange(selectIndex);
1026 }
1027 else
1028 {
1029 deselection << QItemSelectionRange(selectIndex);
1030 }
1031 }
1032
1033 selectionModel()->select(selection, QItemSelectionModel::Select);
1034 selectionModel()->select(deselection, QItemSelectionModel::Deselect);
1035
1036 break;
1037 }
1038 }
1039 }
1040
1041 d->lastSelection = selectionModel()->selection();
1042
1043 if (d->hovered.isValid())
1044 viewport()->update(visualRect(d->hovered));
1045 else if (!d->hoveredCategory.isEmpty())
1046 viewport()->update(d->categoryVisualRect(d->hoveredCategory));
1047 }
1048
1049 void KCategorizedView::leaveEvent(QEvent *event)
1050 {
1051 d->hovered = QModelIndex();
1052 d->hoveredCategory = QString();
1053
1054 QListView::leaveEvent(event);
1055 }
1056
1057 void KCategorizedView::startDrag(Qt::DropActions supportedActions)
1058 {
1059 // FIXME: QAbstractItemView does far better here since it sets the
1060 // pixmap of selected icons to the dragging cursor, but it sets a non
1061 // ARGB window so it is no transparent. Use QAbstractItemView when
1062 // this is fixed on Qt.
1063 // QAbstractItemView::startDrag(supportedActions);
1064 #if !defined(DOLPHIN_DRAGANDDROP)
1065 QListView::startDrag(supportedActions);
1066 #endif
1067
1068 d->isDragging = false;
1069 d->mouseButtonPressed = false;
1070
1071 viewport()->update(d->lastDraggedItemsRect);
1072 }
1073
1074 void KCategorizedView::dragMoveEvent(QDragMoveEvent *event)
1075 {
1076 d->mousePosition = event->pos();
1077
1078 if (d->mouseButtonPressed)
1079 {
1080 d->isDragging = true;
1081 }
1082 else
1083 {
1084 d->isDragging = false;
1085 }
1086
1087 d->dragLeftViewport = false;
1088
1089 if ((viewMode() != KCategorizedView::IconMode) || !d->proxyModel ||
1090 !d->categoryDrawer || !d->proxyModel->isCategorizedModel())
1091 {
1092 #if defined(DOLPHIN_DRAGANDDROP)
1093 QAbstractItemView::dragMoveEvent(event);
1094 #else
1095 QListView::dragMoveEvent(event);
1096 #endif
1097 return;
1098 }
1099
1100 d->drawDraggedItems();
1101 }
1102
1103 void KCategorizedView::dragLeaveEvent(QDragLeaveEvent *event)
1104 {
1105 d->dragLeftViewport = true;
1106
1107 #if defined(DOLPHIN_DRAGANDDROP)
1108 QAbstractItemView::dragLeaveEvent(event);
1109 #else
1110 QListView::dragLeaveEvent(event);
1111 #endif
1112 }
1113
1114 void KCategorizedView::dropEvent(QDropEvent *event)
1115 {
1116 #if defined(DOLPHIN_DRAGANDDROP)
1117 QAbstractItemView::dropEvent(event);
1118 #else
1119 QListView::dropEvent(event);
1120 #endif
1121 }
1122
1123 QModelIndex KCategorizedView::moveCursor(CursorAction cursorAction,
1124 Qt::KeyboardModifiers modifiers)
1125 {
1126 if ((viewMode() != KCategorizedView::IconMode) ||
1127 !d->proxyModel ||
1128 !d->categoryDrawer ||
1129 d->categories.isEmpty() ||
1130 !d->proxyModel->isCategorizedModel()
1131 )
1132 {
1133 return QListView::moveCursor(cursorAction, modifiers);
1134 }
1135
1136 const QModelIndex current = selectionModel()->currentIndex();
1137
1138 int viewportWidth = viewport()->width() - spacing();
1139 int itemWidth;
1140
1141 if (gridSize().isEmpty())
1142 {
1143 itemWidth = d->biggestItemSize.width();
1144 }
1145 else
1146 {
1147 itemWidth = gridSize().width();
1148 }
1149
1150 int itemWidthPlusSeparation = spacing() + itemWidth;
1151 int elementsPerRow = viewportWidth / itemWidthPlusSeparation;
1152
1153 QString lastCategory = d->categories.first();
1154 QString theCategory = d->categories.first();
1155 QString afterCategory = d->categories.first();
1156
1157 bool hasToBreak = false;
1158 foreach (const QString &category, d->categories)
1159 {
1160 if (hasToBreak)
1161 {
1162 afterCategory = category;
1163
1164 break;
1165 }
1166
1167 if (category == d->elementsInfo[current.row()].category)
1168 {
1169 theCategory = category;
1170
1171 hasToBreak = true;
1172 }
1173
1174 if (!hasToBreak)
1175 {
1176 lastCategory = category;
1177 }
1178 }
1179
1180 switch (cursorAction)
1181 {
1182 case QAbstractItemView::MoveUp: {
1183 if (d->elementsInfo[current.row()].relativeOffsetToCategory >= elementsPerRow)
1184 {
1185 int indexToMove = current.row();
1186 indexToMove -= qMin(((d->elementsInfo[current.row()].relativeOffsetToCategory) + d->forcedSelectionPosition), elementsPerRow - d->forcedSelectionPosition + (d->elementsInfo[current.row()].relativeOffsetToCategory % elementsPerRow));
1187
1188 return d->proxyModel->index(indexToMove, 0);
1189 }
1190 else
1191 {
1192 int lastCategoryLastRow = (d->categoriesIndexes[lastCategory].count() - 1) % elementsPerRow;
1193 int indexToMove = current.row() - d->elementsInfo[current.row()].relativeOffsetToCategory;
1194
1195 if (d->forcedSelectionPosition >= lastCategoryLastRow)
1196 {
1197 indexToMove -= 1;
1198 }
1199 else
1200 {
1201 indexToMove -= qMin((lastCategoryLastRow - d->forcedSelectionPosition + 1), d->forcedSelectionPosition + elementsPerRow + 1);
1202 }
1203
1204 return d->proxyModel->index(indexToMove, 0);
1205 }
1206 }
1207
1208 case QAbstractItemView::MoveDown: {
1209 if (d->elementsInfo[current.row()].relativeOffsetToCategory < (d->categoriesIndexes[theCategory].count() - 1 - ((d->categoriesIndexes[theCategory].count() - 1) % elementsPerRow)))
1210 {
1211 int indexToMove = current.row();
1212 indexToMove += qMin(elementsPerRow, d->categoriesIndexes[theCategory].count() - 1 - d->elementsInfo[current.row()].relativeOffsetToCategory);
1213
1214 return d->proxyModel->index(indexToMove, 0);
1215 }
1216 else
1217 {
1218 int afterCategoryLastRow = qMin(elementsPerRow, d->categoriesIndexes[afterCategory].count());
1219 int indexToMove = current.row() + (d->categoriesIndexes[theCategory].count() - d->elementsInfo[current.row()].relativeOffsetToCategory);
1220
1221 if (d->forcedSelectionPosition >= afterCategoryLastRow)
1222 {
1223 indexToMove += afterCategoryLastRow - 1;
1224 }
1225 else
1226 {
1227 indexToMove += qMin(d->forcedSelectionPosition, elementsPerRow);
1228 }
1229
1230 return d->proxyModel->index(indexToMove, 0);
1231 }
1232 }
1233
1234 case QAbstractItemView::MoveLeft:
1235 if (layoutDirection() == Qt::RightToLeft)
1236 {
1237 d->forcedSelectionPosition = d->elementsInfo[current.row() + 1].relativeOffsetToCategory % elementsPerRow;
1238
1239 if (d->forcedSelectionPosition < 0)
1240 d->forcedSelectionPosition = (d->categoriesIndexes[theCategory].count() - 1) % elementsPerRow;
1241
1242 return d->proxyModel->index(current.row() + 1, 0);
1243 }
1244
1245 d->forcedSelectionPosition = d->elementsInfo[current.row() - 1].relativeOffsetToCategory % elementsPerRow;
1246
1247 if (d->forcedSelectionPosition < 0)
1248 d->forcedSelectionPosition = (d->categoriesIndexes[theCategory].count() - 1) % elementsPerRow;
1249
1250 return d->proxyModel->index(current.row() - 1, 0);
1251
1252 case QAbstractItemView::MoveRight:
1253 if (layoutDirection() == Qt::RightToLeft)
1254 {
1255 d->forcedSelectionPosition = d->elementsInfo[current.row() - 1].relativeOffsetToCategory % elementsPerRow;
1256
1257 if (d->forcedSelectionPosition < 0)
1258 d->forcedSelectionPosition = (d->categoriesIndexes[theCategory].count() - 1) % elementsPerRow;
1259
1260 return d->proxyModel->index(current.row() - 1, 0);
1261 }
1262
1263 d->forcedSelectionPosition = d->elementsInfo[current.row() + 1].relativeOffsetToCategory % elementsPerRow;
1264
1265 if (d->forcedSelectionPosition < 0)
1266 d->forcedSelectionPosition = (d->categoriesIndexes[theCategory].count() - 1) % elementsPerRow;
1267
1268 return d->proxyModel->index(current.row() + 1, 0);
1269
1270 default:
1271 break;
1272 }
1273
1274 return QListView::moveCursor(cursorAction, modifiers);
1275 }
1276
1277 void KCategorizedView::rowsInserted(const QModelIndex &parent,
1278 int start,
1279 int end)
1280 {
1281 QListView::rowsInserted(parent, start, end);
1282
1283 if ((viewMode() != KCategorizedView::IconMode) || !d->proxyModel ||
1284 !d->categoryDrawer || !d->proxyModel->isCategorizedModel())
1285 {
1286 d->lastSelection = QItemSelection();
1287 d->currentViewIndex = QModelIndex();
1288 d->forcedSelectionPosition = 0;
1289 d->elementsInfo.clear();
1290 d->elementsPosition.clear();
1291 d->categoriesIndexes.clear();
1292 d->categoriesPosition.clear();
1293 d->categories.clear();
1294 d->intersectedIndexes.clear();
1295 d->modelIndexList.clear();
1296 d->hovered = QModelIndex();
1297 d->biggestItemSize = QSize(0, 0);
1298 d->mouseButtonPressed = false;
1299
1300 return;
1301 }
1302
1303 rowsInsertedArtifficial(parent, start, end);
1304 }
1305
1306 void KCategorizedView::rowsInsertedArtifficial(const QModelIndex &parent,
1307 int start,
1308 int end)
1309 {
1310 Q_UNUSED(parent);
1311
1312 d->lastSelection = QItemSelection();
1313 d->currentViewIndex = QModelIndex();
1314 d->forcedSelectionPosition = 0;
1315 d->elementsInfo.clear();
1316 d->elementsPosition.clear();
1317 d->categoriesIndexes.clear();
1318 d->categoriesPosition.clear();
1319 d->categories.clear();
1320 d->intersectedIndexes.clear();
1321 d->modelIndexList.clear();
1322 d->hovered = QModelIndex();
1323 d->biggestItemSize = QSize(0, 0);
1324 d->mouseButtonPressed = false;
1325
1326 if (start > end || end < 0 || start < 0 || !d->proxyModel->rowCount())
1327 {
1328 return;
1329 }
1330
1331 // Add all elements mapped to the source model and explore categories
1332 QString prevCategory = d->proxyModel->data(d->proxyModel->index(0, d->proxyModel->sortColumn()), KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString();
1333 QString lastCategory = prevCategory;
1334 QModelIndexList modelIndexList;
1335 struct Private::ElementInfo elementInfo;
1336 int offset = -1;
1337 for (int k = 0; k < d->proxyModel->rowCount(); ++k)
1338 {
1339 QModelIndex index = d->proxyModel->index(k, d->proxyModel->sortColumn());
1340 QModelIndex indexSize = d->proxyModel->index(k, 0);
1341
1342 d->biggestItemSize = QSize(qMax(sizeHintForIndex(indexSize).width(),
1343 d->biggestItemSize.width()),
1344 qMax(sizeHintForIndex(indexSize).height(),
1345 d->biggestItemSize.height()));
1346
1347 d->modelIndexList << index;
1348
1349 lastCategory = d->proxyModel->data(index, KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString();
1350
1351 elementInfo.category = lastCategory;
1352
1353 if (prevCategory != lastCategory)
1354 {
1355 offset = 0;
1356 d->categoriesIndexes.insert(prevCategory, modelIndexList);
1357 d->categories << prevCategory;
1358 modelIndexList.clear();
1359 }
1360 else
1361 {
1362 offset++;
1363 }
1364
1365 elementInfo.relativeOffsetToCategory = offset;
1366
1367 modelIndexList << index;
1368 prevCategory = lastCategory;
1369
1370 d->elementsInfo.insert(index.row(), elementInfo);
1371 }
1372
1373 d->categoriesIndexes.insert(prevCategory, modelIndexList);
1374 d->categories << prevCategory;
1375
1376 d->updateScrollbars();
1377 }
1378
1379 void KCategorizedView::rowsRemoved(const QModelIndex &parent,
1380 int start,
1381 int end)
1382 {
1383 if ((viewMode() == KCategorizedView::IconMode) && d->proxyModel &&
1384 d->categoryDrawer && d->proxyModel->isCategorizedModel())
1385 {
1386 // Force the view to update all elements
1387 rowsInsertedArtifficial(QModelIndex(), 0, d->proxyModel->rowCount() - 1);
1388 }
1389 }
1390
1391 void KCategorizedView::updateGeometries()
1392 {
1393 if ((viewMode() != KCategorizedView::IconMode) || !d->proxyModel ||
1394 !d->categoryDrawer || !d->proxyModel->isCategorizedModel())
1395 {
1396 QListView::updateGeometries();
1397 return;
1398 }
1399
1400 // Avoid QListView::updateGeometries(), since it will try to set another
1401 // range to our scroll bars, what we don't want (ereslibre)
1402 QAbstractItemView::updateGeometries();
1403 }
1404
1405 void KCategorizedView::slotLayoutChanged()
1406 {
1407 d->layoutChanged();
1408 }
1409
1410 #include "kcategorizedview.moc"