]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistview.cpp
Update the layout asynchronously when items are changed
[dolphin.git] / src / kitemviews / kitemlistview.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * Based on the Itemviews NG project from Trolltech Labs: *
5 * http://qt.gitorious.org/qt-labs/itemviews-ng *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
21 ***************************************************************************/
22
23 #include "kitemlistview.h"
24
25 #include "kitemlistcontroller.h"
26 #include "kitemlistgroupheader.h"
27 #include "kitemlistheader_p.h"
28 #include "kitemlistrubberband_p.h"
29 #include "kitemlistselectionmanager.h"
30 #include "kitemlistsizehintresolver_p.h"
31 #include "kitemlistviewlayouter_p.h"
32 #include "kitemlistviewanimation_p.h"
33 #include "kitemlistwidget.h"
34
35 #include <KDebug>
36
37 #include <QCursor>
38 #include <QGraphicsSceneMouseEvent>
39 #include <QPainter>
40 #include <QPropertyAnimation>
41 #include <QStyle>
42 #include <QStyleOptionRubberBand>
43 #include <QTimer>
44
45 namespace {
46 // Time in ms until reaching the autoscroll margin triggers
47 // an initial autoscrolling
48 const int InitialAutoScrollDelay = 700;
49
50 // Delay in ms for triggering the next autoscroll
51 const int RepeatingAutoScrollDelay = 1000 / 60;
52 }
53
54 KItemListView::KItemListView(QGraphicsWidget* parent) :
55 QGraphicsWidget(parent),
56 m_grouped(false),
57 m_activeTransactions(0),
58 m_itemSize(),
59 m_controller(0),
60 m_model(0),
61 m_visibleRoles(),
62 m_visibleRolesSizes(),
63 m_widgetCreator(0),
64 m_groupHeaderCreator(0),
65 m_styleOption(),
66 m_visibleItems(),
67 m_visibleGroups(),
68 m_sizeHintResolver(0),
69 m_layouter(0),
70 m_animation(0),
71 m_layoutTimer(0),
72 m_oldOffset(0),
73 m_oldMaximumOffset(0),
74 m_skipAutoScrollForRubberBand(false),
75 m_rubberBand(0),
76 m_mousePos(),
77 m_autoScrollIncrement(0),
78 m_autoScrollTimer(0),
79 m_header(0),
80 m_useHeaderWidths(false)
81 {
82 setAcceptHoverEvents(true);
83
84 m_sizeHintResolver = new KItemListSizeHintResolver(this);
85
86 m_layouter = new KItemListViewLayouter(this);
87 m_layouter->setSizeHintResolver(m_sizeHintResolver);
88
89 m_animation = new KItemListViewAnimation(this);
90 connect(m_animation, SIGNAL(finished(QGraphicsWidget*,KItemListViewAnimation::AnimationType)),
91 this, SLOT(slotAnimationFinished(QGraphicsWidget*,KItemListViewAnimation::AnimationType)));
92
93 m_layoutTimer = new QTimer(this);
94 m_layoutTimer->setInterval(300);
95 m_layoutTimer->setSingleShot(true);
96 connect(m_layoutTimer, SIGNAL(timeout()), this, SLOT(slotLayoutTimerFinished()));
97
98 m_rubberBand = new KItemListRubberBand(this);
99 connect(m_rubberBand, SIGNAL(activationChanged(bool)), this, SLOT(slotRubberBandActivationChanged(bool)));
100 }
101
102 KItemListView::~KItemListView()
103 {
104 delete m_sizeHintResolver;
105 m_sizeHintResolver = 0;
106 }
107
108 void KItemListView::setScrollOrientation(Qt::Orientation orientation)
109 {
110 const Qt::Orientation previousOrientation = m_layouter->scrollOrientation();
111 if (orientation == previousOrientation) {
112 return;
113 }
114
115 m_layouter->setScrollOrientation(orientation);
116 m_animation->setScrollOrientation(orientation);
117 m_sizeHintResolver->clearCache();
118 updateLayout();
119 onScrollOrientationChanged(orientation, previousOrientation);
120 }
121
122 Qt::Orientation KItemListView::scrollOrientation() const
123 {
124 return m_layouter->scrollOrientation();
125 }
126
127 void KItemListView::setItemSize(const QSizeF& itemSize)
128 {
129 const QSizeF previousSize = m_itemSize;
130 if (itemSize == previousSize) {
131 return;
132 }
133
134 m_itemSize = itemSize;
135
136 if (!markVisibleRolesSizesAsDirty()) {
137 if (itemSize.width() < previousSize.width() || itemSize.height() < previousSize.height()) {
138 prepareLayoutForIncreasedItemCount(itemSize, ItemSize);
139 } else {
140 m_layouter->setItemSize(itemSize);
141 }
142 }
143
144 m_sizeHintResolver->clearCache();
145 updateLayout();
146 onItemSizeChanged(itemSize, previousSize);
147 }
148
149 QSizeF KItemListView::itemSize() const
150 {
151 return m_itemSize;
152 }
153
154 void KItemListView::setOffset(qreal offset)
155 {
156 if (offset < 0) {
157 offset = 0;
158 }
159
160 const qreal previousOffset = m_layouter->offset();
161 if (offset == previousOffset) {
162 return;
163 }
164
165 m_layouter->setOffset(offset);
166 m_animation->setOffset(offset);
167 if (!m_layoutTimer->isActive()) {
168 doLayout(NoAnimation, 0, 0);
169 update();
170 }
171 onOffsetChanged(offset, previousOffset);
172 }
173
174 qreal KItemListView::offset() const
175 {
176 return m_layouter->offset();
177 }
178
179 qreal KItemListView::maximumOffset() const
180 {
181 return m_layouter->maximumOffset();
182 }
183
184 void KItemListView::setVisibleRoles(const QList<QByteArray>& roles)
185 {
186 const QList<QByteArray> previousRoles = m_visibleRoles;
187 m_visibleRoles = roles;
188
189 QHashIterator<int, KItemListWidget*> it(m_visibleItems);
190 while (it.hasNext()) {
191 it.next();
192 KItemListWidget* widget = it.value();
193 widget->setVisibleRoles(roles);
194 widget->setVisibleRolesSizes(m_visibleRolesSizes);
195 }
196
197 m_sizeHintResolver->clearCache();
198 m_layouter->markAsDirty();
199 onVisibleRolesChanged(roles, previousRoles);
200
201 markVisibleRolesSizesAsDirty();
202 updateLayout();
203
204 if (m_header) {
205 m_header->setVisibleRoles(roles);
206 m_header->setVisibleRolesWidths(headerRolesWidths());
207 m_useHeaderWidths = false;
208 }
209 }
210
211 QList<QByteArray> KItemListView::visibleRoles() const
212 {
213 return m_visibleRoles;
214 }
215
216 void KItemListView::setAutoScroll(bool enabled)
217 {
218 if (enabled && !m_autoScrollTimer) {
219 m_autoScrollTimer = new QTimer(this);
220 m_autoScrollTimer->setSingleShot(false);
221 connect(m_autoScrollTimer, SIGNAL(timeout()), this, SLOT(triggerAutoScrolling()));
222 m_autoScrollTimer->start(InitialAutoScrollDelay);
223 } else if (!enabled && m_autoScrollTimer) {
224 delete m_autoScrollTimer;
225 m_autoScrollTimer = 0;
226 }
227
228 }
229
230 bool KItemListView::autoScroll() const
231 {
232 return m_autoScrollTimer != 0;
233 }
234
235 void KItemListView::setHeaderShown(bool show)
236 {
237 if (show && !m_header) {
238 m_header = new KItemListHeader(this);
239 m_header->setPos(0, 0);
240 m_header->setModel(m_model);
241 m_header->setVisibleRoles(m_visibleRoles);
242 m_header->setVisibleRolesWidths(headerRolesWidths());
243 m_header->setZValue(1);
244
245 m_useHeaderWidths = false;
246 updateHeaderWidth();
247
248 connect(m_header, SIGNAL(visibleRoleWidthChanged(QByteArray,qreal,qreal)),
249 this, SLOT(slotVisibleRoleWidthChanged(QByteArray,qreal,qreal)));
250
251 m_layouter->setHeaderHeight(m_header->size().height());
252 } else if (!show && m_header) {
253 delete m_header;
254 m_header = 0;
255 m_useHeaderWidths = false;
256 m_layouter->setHeaderHeight(0);
257 }
258 }
259
260 bool KItemListView::isHeaderShown() const
261 {
262 return m_header != 0;
263 }
264
265 KItemListController* KItemListView::controller() const
266 {
267 return m_controller;
268 }
269
270 KItemModelBase* KItemListView::model() const
271 {
272 return m_model;
273 }
274
275 void KItemListView::setWidgetCreator(KItemListWidgetCreatorBase* widgetCreator)
276 {
277 m_widgetCreator = widgetCreator;
278 }
279
280 KItemListWidgetCreatorBase* KItemListView::widgetCreator() const
281 {
282 return m_widgetCreator;
283 }
284
285 void KItemListView::setGroupHeaderCreator(KItemListGroupHeaderCreatorBase* groupHeaderCreator)
286 {
287 m_groupHeaderCreator = groupHeaderCreator;
288 }
289
290 KItemListGroupHeaderCreatorBase* KItemListView::groupHeaderCreator() const
291 {
292 return m_groupHeaderCreator;
293 }
294
295 void KItemListView::setStyleOption(const KItemListStyleOption& option)
296 {
297 const KItemListStyleOption previousOption = m_styleOption;
298 m_styleOption = option;
299
300 QHashIterator<int, KItemListWidget*> it(m_visibleItems);
301 while (it.hasNext()) {
302 it.next();
303 it.value()->setStyleOption(option);
304 }
305
306 m_sizeHintResolver->clearCache();
307 updateLayout();
308 onStyleOptionChanged(option, previousOption);
309 }
310
311 const KItemListStyleOption& KItemListView::styleOption() const
312 {
313 return m_styleOption;
314 }
315
316 void KItemListView::setGeometry(const QRectF& rect)
317 {
318 QGraphicsWidget::setGeometry(rect);
319 if (!m_model) {
320 return;
321 }
322
323 if (m_itemSize.isEmpty()) {
324 m_layouter->setItemSize(QSizeF());
325 }
326
327 if (m_model->count() > 0) {
328 prepareLayoutForIncreasedItemCount(rect.size(), LayouterSize);
329 } else {
330 m_layouter->setSize(rect.size());
331 }
332
333 if (!m_layoutTimer->isActive()) {
334 m_layoutTimer->start();
335 }
336 }
337
338 int KItemListView::itemAt(const QPointF& pos) const
339 {
340 QHashIterator<int, KItemListWidget*> it(m_visibleItems);
341 while (it.hasNext()) {
342 it.next();
343
344 const KItemListWidget* widget = it.value();
345 const QPointF mappedPos = widget->mapFromItem(this, pos);
346 if (widget->contains(mappedPos)) {
347 return it.key();
348 }
349 }
350
351 return -1;
352 }
353
354 bool KItemListView::isAboveSelectionToggle(int index, const QPointF& pos) const
355 {
356 Q_UNUSED(index);
357 Q_UNUSED(pos);
358 return false;
359 }
360
361 bool KItemListView::isAboveExpansionToggle(int index, const QPointF& pos) const
362 {
363 const KItemListWidget* widget = m_visibleItems.value(index);
364 if (widget) {
365 const QRectF expansionToggleRect = widget->expansionToggleRect();
366 if (!expansionToggleRect.isEmpty()) {
367 const QPointF mappedPos = widget->mapFromItem(this, pos);
368 return expansionToggleRect.contains(mappedPos);
369 }
370 }
371 return false;
372 }
373
374 int KItemListView::firstVisibleIndex() const
375 {
376 return m_layouter->firstVisibleIndex();
377 }
378
379 int KItemListView::lastVisibleIndex() const
380 {
381 return m_layouter->lastVisibleIndex();
382 }
383
384 QSizeF KItemListView::itemSizeHint(int index) const
385 {
386 Q_UNUSED(index);
387 return itemSize();
388 }
389
390 QHash<QByteArray, QSizeF> KItemListView::visibleRoleSizes() const
391 {
392 return QHash<QByteArray, QSizeF>();
393 }
394
395 QRectF KItemListView::itemBoundingRect(int index) const
396 {
397 return m_layouter->itemBoundingRect(index);
398 }
399
400 int KItemListView::itemsPerOffset() const
401 {
402 return m_layouter->itemsPerOffset();
403 }
404
405 void KItemListView::beginTransaction()
406 {
407 ++m_activeTransactions;
408 if (m_activeTransactions == 1) {
409 onTransactionBegin();
410 }
411 }
412
413 void KItemListView::endTransaction()
414 {
415 --m_activeTransactions;
416 if (m_activeTransactions < 0) {
417 m_activeTransactions = 0;
418 kWarning() << "Mismatch between beginTransaction()/endTransaction()";
419 }
420
421 if (m_activeTransactions == 0) {
422 onTransactionEnd();
423 updateLayout();
424 }
425 }
426
427 bool KItemListView::isTransactionActive() const
428 {
429 return m_activeTransactions > 0;
430 }
431
432 QPixmap KItemListView::createDragPixmap(const QSet<int>& indexes) const
433 {
434 Q_UNUSED(indexes);
435 return QPixmap();
436 }
437
438 void KItemListView::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
439 {
440 QGraphicsWidget::paint(painter, option, widget);
441
442 if (m_rubberBand->isActive()) {
443 QRectF rubberBandRect = QRectF(m_rubberBand->startPosition(),
444 m_rubberBand->endPosition()).normalized();
445
446 const QPointF topLeft = rubberBandRect.topLeft();
447 if (scrollOrientation() == Qt::Vertical) {
448 rubberBandRect.moveTo(topLeft.x(), topLeft.y() - offset());
449 } else {
450 rubberBandRect.moveTo(topLeft.x() - offset(), topLeft.y());
451 }
452
453 QStyleOptionRubberBand opt;
454 opt.initFrom(widget);
455 opt.shape = QRubberBand::Rectangle;
456 opt.opaque = false;
457 opt.rect = rubberBandRect.toRect();
458 style()->drawControl(QStyle::CE_RubberBand, &opt, painter);
459 }
460 }
461
462 void KItemListView::initializeItemListWidget(KItemListWidget* item)
463 {
464 Q_UNUSED(item);
465 }
466
467 bool KItemListView::itemSizeHintUpdateRequired(const QSet<QByteArray>& changedRoles) const
468 {
469 Q_UNUSED(changedRoles);
470 return true;
471 }
472
473 void KItemListView::onControllerChanged(KItemListController* current, KItemListController* previous)
474 {
475 Q_UNUSED(current);
476 Q_UNUSED(previous);
477 }
478
479 void KItemListView::onModelChanged(KItemModelBase* current, KItemModelBase* previous)
480 {
481 Q_UNUSED(current);
482 Q_UNUSED(previous);
483 }
484
485 void KItemListView::onScrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous)
486 {
487 Q_UNUSED(current);
488 Q_UNUSED(previous);
489 }
490
491 void KItemListView::onItemSizeChanged(const QSizeF& current, const QSizeF& previous)
492 {
493 Q_UNUSED(current);
494 Q_UNUSED(previous);
495 }
496
497 void KItemListView::onOffsetChanged(qreal current, qreal previous)
498 {
499 Q_UNUSED(current);
500 Q_UNUSED(previous);
501 }
502
503 void KItemListView::onVisibleRolesChanged(const QList<QByteArray>& current, const QList<QByteArray>& previous)
504 {
505 Q_UNUSED(current);
506 Q_UNUSED(previous);
507 }
508
509 void KItemListView::onStyleOptionChanged(const KItemListStyleOption& current, const KItemListStyleOption& previous)
510 {
511 Q_UNUSED(current);
512 Q_UNUSED(previous);
513 }
514
515 void KItemListView::onTransactionBegin()
516 {
517 }
518
519 void KItemListView::onTransactionEnd()
520 {
521 }
522
523 bool KItemListView::event(QEvent* event)
524 {
525 // Forward all events to the controller and handle them there
526 if (m_controller && m_controller->processEvent(event, transform())) {
527 event->accept();
528 return true;
529 }
530 return QGraphicsWidget::event(event);
531 }
532
533 void KItemListView::mousePressEvent(QGraphicsSceneMouseEvent* event)
534 {
535 m_mousePos = transform().map(event->pos());
536 event->accept();
537 }
538
539 void KItemListView::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
540 {
541 QGraphicsWidget::mouseMoveEvent(event);
542
543 m_mousePos = transform().map(event->pos());
544 if (m_autoScrollTimer && !m_autoScrollTimer->isActive()) {
545 m_autoScrollTimer->start(InitialAutoScrollDelay);
546 }
547 }
548
549 void KItemListView::dragEnterEvent(QGraphicsSceneDragDropEvent* event)
550 {
551 event->setAccepted(true);
552 setAutoScroll(true);
553 }
554
555 void KItemListView::dragMoveEvent(QGraphicsSceneDragDropEvent *event)
556 {
557 QGraphicsWidget::dragMoveEvent(event);
558
559 m_mousePos = transform().map(event->pos());
560 if (m_autoScrollTimer && !m_autoScrollTimer->isActive()) {
561 m_autoScrollTimer->start(InitialAutoScrollDelay);
562 }
563 }
564
565 void KItemListView::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
566 {
567 QGraphicsWidget::dragLeaveEvent(event);
568 setAutoScroll(false);
569 }
570
571 void KItemListView::dropEvent(QGraphicsSceneDragDropEvent* event)
572 {
573 QGraphicsWidget::dropEvent(event);
574 setAutoScroll(false);
575 }
576
577 QList<KItemListWidget*> KItemListView::visibleItemListWidgets() const
578 {
579 return m_visibleItems.values();
580 }
581
582 void KItemListView::resizeEvent(QGraphicsSceneResizeEvent* event)
583 {
584 QGraphicsWidget::resizeEvent(event);
585 updateHeaderWidth();
586 }
587
588 bool KItemListView::markVisibleRolesSizesAsDirty()
589 {
590 const bool dirty = m_itemSize.isEmpty();
591 if (dirty && !m_useHeaderWidths) {
592 m_visibleRolesSizes.clear();
593 m_layouter->setItemSize(QSizeF());
594 }
595 return dirty;
596 }
597
598 void KItemListView::slotItemsInserted(const KItemRangeList& itemRanges)
599 {
600 markVisibleRolesSizesAsDirty();
601
602 const bool hasMultipleRanges = (itemRanges.count() > 1);
603 if (hasMultipleRanges) {
604 beginTransaction();
605 }
606
607 int previouslyInsertedCount = 0;
608 foreach (const KItemRange& range, itemRanges) {
609 // range.index is related to the model before anything has been inserted.
610 // As in each loop the current item-range gets inserted the index must
611 // be increased by the already previously inserted items.
612 const int index = range.index + previouslyInsertedCount;
613 const int count = range.count;
614 if (index < 0 || count <= 0) {
615 kWarning() << "Invalid item range (index:" << index << ", count:" << count << ")";
616 continue;
617 }
618 previouslyInsertedCount += count;
619
620 m_sizeHintResolver->itemsInserted(index, count);
621
622 // Determine which visible items must be moved
623 QList<int> itemsToMove;
624 QHashIterator<int, KItemListWidget*> it(m_visibleItems);
625 while (it.hasNext()) {
626 it.next();
627 const int visibleItemIndex = it.key();
628 if (visibleItemIndex >= index) {
629 itemsToMove.append(visibleItemIndex);
630 }
631 }
632
633 // Update the indexes of all KItemListWidget instances that are located
634 // after the inserted items. It is important to adjust the indexes in the order
635 // from the highest index to the lowest index to prevent overlaps when setting the new index.
636 qSort(itemsToMove);
637 for (int i = itemsToMove.count() - 1; i >= 0; --i) {
638 KItemListWidget* widget = m_visibleItems.value(itemsToMove[i]);
639 Q_ASSERT(widget);
640 setWidgetIndex(widget, widget->index() + count);
641 }
642
643 m_layouter->markAsDirty();
644 if (m_model->count() == count && maximumOffset() > size().height()) {
645 kDebug() << "Scrollbar required, skipping layout";
646 const int scrollBarExtent = style()->pixelMetric(QStyle::PM_ScrollBarExtent);
647 QSizeF layouterSize = m_layouter->size();
648 if (scrollOrientation() == Qt::Vertical) {
649 layouterSize.rwidth() -= scrollBarExtent;
650 } else {
651 layouterSize.rheight() -= scrollBarExtent;
652 }
653 m_layouter->setSize(layouterSize);
654 }
655
656 if (!hasMultipleRanges) {
657 doLayout(Animation, index, count);
658 update();
659 }
660 }
661
662 if (m_controller) {
663 m_controller->selectionManager()->itemsInserted(itemRanges);
664 }
665
666 if (hasMultipleRanges) {
667 endTransaction();
668 }
669 }
670
671 void KItemListView::slotItemsRemoved(const KItemRangeList& itemRanges)
672 {
673 markVisibleRolesSizesAsDirty();
674
675 const bool hasMultipleRanges = (itemRanges.count() > 1);
676 if (hasMultipleRanges) {
677 beginTransaction();
678 }
679
680 for (int i = itemRanges.count() - 1; i >= 0; --i) {
681 const KItemRange& range = itemRanges.at(i);
682 const int index = range.index;
683 const int count = range.count;
684 if (index < 0 || count <= 0) {
685 kWarning() << "Invalid item range (index:" << index << ", count:" << count << ")";
686 continue;
687 }
688
689 m_sizeHintResolver->itemsRemoved(index, count);
690
691 const int firstRemovedIndex = index;
692 const int lastRemovedIndex = index + count - 1;
693 const int lastIndex = m_model->count() + count - 1;
694
695 // Remove all KItemListWidget instances that got deleted
696 for (int i = firstRemovedIndex; i <= lastRemovedIndex; ++i) {
697 KItemListWidget* widget = m_visibleItems.value(i);
698 if (!widget) {
699 continue;
700 }
701
702 m_animation->stop(widget);
703 // Stopping the animation might lead to recycling the widget if
704 // it is invisible (see slotAnimationFinished()).
705 // Check again whether it is still visible:
706 if (!m_visibleItems.contains(i)) {
707 continue;
708 }
709
710 if (m_model->count() == 0) {
711 // For performance reasons no animation is done when all items have
712 // been removed.
713 recycleWidget(widget);
714 } else {
715 // Animate the removing of the items. Special case: When removing an item there
716 // is no valid model index available anymore. For the
717 // remove-animation the item gets removed from m_visibleItems but the widget
718 // will stay alive until the animation has been finished and will
719 // be recycled (deleted) in KItemListView::slotAnimationFinished().
720 m_visibleItems.remove(i);
721 widget->setIndex(-1);
722 m_animation->start(widget, KItemListViewAnimation::DeleteAnimation);
723 }
724 }
725
726 // Update the indexes of all KItemListWidget instances that are located
727 // after the deleted items
728 for (int i = lastRemovedIndex + 1; i <= lastIndex; ++i) {
729 KItemListWidget* widget = m_visibleItems.value(i);
730 if (widget) {
731 const int newIndex = i - count;
732 setWidgetIndex(widget, newIndex);
733 }
734 }
735
736 m_layouter->markAsDirty();
737 if (!hasMultipleRanges) {
738 doLayout(Animation, index, -count);
739 update();
740 }
741 }
742
743 if (m_controller) {
744 m_controller->selectionManager()->itemsRemoved(itemRanges);
745 }
746
747 if (hasMultipleRanges) {
748 endTransaction();
749 }
750 }
751
752 void KItemListView::slotItemsChanged(const KItemRangeList& itemRanges,
753 const QSet<QByteArray>& roles)
754 {
755 const bool updateSizeHints = itemSizeHintUpdateRequired(roles);
756 if (updateSizeHints) {
757 markVisibleRolesSizesAsDirty();
758 }
759
760 foreach (const KItemRange& itemRange, itemRanges) {
761 const int index = itemRange.index;
762 const int count = itemRange.count;
763
764 if (updateSizeHints) {
765 m_sizeHintResolver->itemsChanged(index, count, roles);
766 m_layouter->markAsDirty();
767 if (!m_layoutTimer->isActive()) {
768 m_layoutTimer->start();
769 }
770 }
771
772 // Apply the changed roles to the visible item-widgets
773 const int lastIndex = index + count - 1;
774 for (int i = index; i <= lastIndex; ++i) {
775 KItemListWidget* widget = m_visibleItems.value(i);
776 if (widget) {
777 widget->setData(m_model->data(i), roles);
778 }
779 }
780
781 }
782 }
783
784 void KItemListView::slotCurrentChanged(int current, int previous)
785 {
786 Q_UNUSED(previous);
787
788 KItemListWidget* previousWidget = m_visibleItems.value(previous, 0);
789 if (previousWidget) {
790 Q_ASSERT(previousWidget->isCurrent());
791 previousWidget->setCurrent(false);
792 }
793
794 KItemListWidget* currentWidget = m_visibleItems.value(current, 0);
795 if (currentWidget) {
796 Q_ASSERT(!currentWidget->isCurrent());
797 currentWidget->setCurrent(true);
798 }
799
800 const QRectF viewGeometry = geometry();
801 const QRectF currentBoundingRect = itemBoundingRect(current);
802
803 if (!viewGeometry.contains(currentBoundingRect)) {
804 // Make sure that the new current item is fully visible in the view.
805 qreal newOffset = offset();
806 if (currentBoundingRect.top() < viewGeometry.top()) {
807 Q_ASSERT(scrollOrientation() == Qt::Vertical);
808 newOffset += currentBoundingRect.top() - viewGeometry.top();
809 } else if ((currentBoundingRect.bottom() > viewGeometry.bottom())) {
810 Q_ASSERT(scrollOrientation() == Qt::Vertical);
811 newOffset += currentBoundingRect.bottom() - viewGeometry.bottom();
812 } else if (currentBoundingRect.left() < viewGeometry.left()) {
813 if (scrollOrientation() == Qt::Horizontal) {
814 newOffset += currentBoundingRect.left() - viewGeometry.left();
815 }
816 } else if ((currentBoundingRect.right() > viewGeometry.right())) {
817 if (scrollOrientation() == Qt::Horizontal) {
818 newOffset += currentBoundingRect.right() - viewGeometry.right();
819 }
820 }
821
822 if (newOffset != offset()) {
823 emit scrollTo(newOffset);
824 }
825 }
826 }
827
828 void KItemListView::slotSelectionChanged(const QSet<int>& current, const QSet<int>& previous)
829 {
830 Q_UNUSED(previous);
831
832 QHashIterator<int, KItemListWidget*> it(m_visibleItems);
833 while (it.hasNext()) {
834 it.next();
835 const int index = it.key();
836 KItemListWidget* widget = it.value();
837 widget->setSelected(current.contains(index));
838 }
839 }
840
841 void KItemListView::slotAnimationFinished(QGraphicsWidget* widget,
842 KItemListViewAnimation::AnimationType type)
843 {
844 KItemListWidget* itemListWidget = qobject_cast<KItemListWidget*>(widget);
845 Q_ASSERT(itemListWidget);
846
847 switch (type) {
848 case KItemListViewAnimation::DeleteAnimation: {
849 // As we recycle the widget in this case it is important to assure that no
850 // other animation has been started. This is a convention in KItemListView and
851 // not a requirement defined by KItemListViewAnimation.
852 Q_ASSERT(!m_animation->isStarted(itemListWidget));
853
854 // All KItemListWidgets that are animated by the DeleteAnimation are not maintained
855 // by m_visibleWidgets and must be deleted manually after the animation has
856 // been finished.
857 KItemListGroupHeader* header = m_visibleGroups.value(itemListWidget);
858 if (header) {
859 m_groupHeaderCreator->recycle(header);
860 m_visibleGroups.remove(itemListWidget);
861 }
862 m_widgetCreator->recycle(itemListWidget);
863 break;
864 }
865
866 case KItemListViewAnimation::CreateAnimation:
867 case KItemListViewAnimation::MovingAnimation:
868 case KItemListViewAnimation::ResizeAnimation: {
869 const int index = itemListWidget->index();
870 const bool invisible = (index < m_layouter->firstVisibleIndex()) ||
871 (index > m_layouter->lastVisibleIndex());
872 if (invisible && !m_animation->isStarted(itemListWidget)) {
873 recycleWidget(itemListWidget);
874 }
875 break;
876 }
877
878 default: break;
879 }
880 }
881
882 void KItemListView::slotLayoutTimerFinished()
883 {
884 m_layouter->setSize(geometry().size());
885 doLayout(Animation, 0, 0);
886 }
887
888 void KItemListView::slotRubberBandPosChanged()
889 {
890 update();
891 }
892
893 void KItemListView::slotRubberBandActivationChanged(bool active)
894 {
895 if (active) {
896 connect(m_rubberBand, SIGNAL(startPositionChanged(QPointF,QPointF)), this, SLOT(slotRubberBandPosChanged()));
897 connect(m_rubberBand, SIGNAL(endPositionChanged(QPointF,QPointF)), this, SLOT(slotRubberBandPosChanged()));
898 m_skipAutoScrollForRubberBand = true;
899 } else {
900 disconnect(m_rubberBand, SIGNAL(startPositionChanged(QPointF,QPointF)), this, SLOT(slotRubberBandPosChanged()));
901 disconnect(m_rubberBand, SIGNAL(endPositionChanged(QPointF,QPointF)), this, SLOT(slotRubberBandPosChanged()));
902 m_skipAutoScrollForRubberBand = false;
903 }
904
905 update();
906 }
907
908 void KItemListView::slotVisibleRoleWidthChanged(const QByteArray& role,
909 qreal currentWidth,
910 qreal previousWidth)
911 {
912 Q_UNUSED(previousWidth);
913
914 m_useHeaderWidths = true;
915
916 if (m_visibleRolesSizes.contains(role)) {
917 QSizeF roleSize = m_visibleRolesSizes.value(role);
918 roleSize.setWidth(currentWidth);
919 m_visibleRolesSizes.insert(role, roleSize);
920 }
921
922 m_layouter->setItemSize(QSizeF()); // Forces an update in applyDynamicItemSize()
923 updateLayout();
924 }
925
926 void KItemListView::triggerAutoScrolling()
927 {
928 if (!m_autoScrollTimer) {
929 return;
930 }
931
932 int pos = 0;
933 int visibleSize = 0;
934 if (scrollOrientation() == Qt::Vertical) {
935 pos = m_mousePos.y();
936 visibleSize = size().height();
937 } else {
938 pos = m_mousePos.x();
939 visibleSize = size().width();
940 }
941
942 if (m_autoScrollTimer->interval() == InitialAutoScrollDelay) {
943 m_autoScrollIncrement = 0;
944 }
945
946 m_autoScrollIncrement = calculateAutoScrollingIncrement(pos, visibleSize, m_autoScrollIncrement);
947 if (m_autoScrollIncrement == 0) {
948 // The mouse position is not above an autoscroll margin (the autoscroll timer
949 // will be restarted in mouseMoveEvent())
950 m_autoScrollTimer->stop();
951 return;
952 }
953
954 if (m_rubberBand->isActive() && m_skipAutoScrollForRubberBand) {
955 // If a rubberband selection is ongoing the autoscrolling may only get triggered
956 // if the direction of the rubberband is similar to the autoscroll direction. This
957 // prevents that starting to create a rubberband within the autoscroll margins starts
958 // an autoscrolling.
959
960 const qreal minDiff = 4; // Ignore any autoscrolling if the rubberband is very small
961 const qreal diff = (scrollOrientation() == Qt::Vertical)
962 ? m_rubberBand->endPosition().y() - m_rubberBand->startPosition().y()
963 : m_rubberBand->endPosition().x() - m_rubberBand->startPosition().x();
964 if (qAbs(diff) < minDiff || (m_autoScrollIncrement < 0 && diff > 0) || (m_autoScrollIncrement > 0 && diff < 0)) {
965 // The rubberband direction is different from the scroll direction (e.g. the rubberband has
966 // been moved up although the autoscroll direction might be down)
967 m_autoScrollTimer->stop();
968 return;
969 }
970 }
971
972 // As soon as the autoscrolling has been triggered at least once despite having an active rubberband,
973 // the autoscrolling may not get skipped anymore until a new rubberband is created
974 m_skipAutoScrollForRubberBand = false;
975
976 setOffset(offset() + m_autoScrollIncrement);
977
978 // Trigger the autoscroll timer which will periodically call
979 // triggerAutoScrolling()
980 m_autoScrollTimer->start(RepeatingAutoScrollDelay);
981 }
982
983 void KItemListView::setController(KItemListController* controller)
984 {
985 if (m_controller != controller) {
986 KItemListController* previous = m_controller;
987 if (previous) {
988 KItemListSelectionManager* selectionManager = previous->selectionManager();
989 disconnect(selectionManager, SIGNAL(currentChanged(int,int)), this, SLOT(slotCurrentChanged(int,int)));
990 disconnect(selectionManager, SIGNAL(selectionChanged(QSet<int>,QSet<int>)), this, SLOT(slotSelectionChanged(QSet<int>,QSet<int>)));
991 }
992
993 m_controller = controller;
994
995 if (controller) {
996 KItemListSelectionManager* selectionManager = controller->selectionManager();
997 connect(selectionManager, SIGNAL(currentChanged(int,int)), this, SLOT(slotCurrentChanged(int,int)));
998 connect(selectionManager, SIGNAL(selectionChanged(QSet<int>,QSet<int>)), this, SLOT(slotSelectionChanged(QSet<int>,QSet<int>)));
999 }
1000
1001 onControllerChanged(controller, previous);
1002 }
1003 }
1004
1005 void KItemListView::setModel(KItemModelBase* model)
1006 {
1007 if (m_model == model) {
1008 return;
1009 }
1010
1011 KItemModelBase* previous = m_model;
1012
1013 if (m_model) {
1014 disconnect(m_model, SIGNAL(itemsChanged(KItemRangeList,QSet<QByteArray>)),
1015 this, SLOT(slotItemsChanged(KItemRangeList,QSet<QByteArray>)));
1016 disconnect(m_model, SIGNAL(itemsInserted(KItemRangeList)),
1017 this, SLOT(slotItemsInserted(KItemRangeList)));
1018 disconnect(m_model, SIGNAL(itemsRemoved(KItemRangeList)),
1019 this, SLOT(slotItemsRemoved(KItemRangeList)));
1020 }
1021
1022 m_model = model;
1023 m_layouter->setModel(model);
1024 m_grouped = !model->groupRole().isEmpty();
1025
1026 if (m_model) {
1027 connect(m_model, SIGNAL(itemsChanged(KItemRangeList,QSet<QByteArray>)),
1028 this, SLOT(slotItemsChanged(KItemRangeList,QSet<QByteArray>)));
1029 connect(m_model, SIGNAL(itemsInserted(KItemRangeList)),
1030 this, SLOT(slotItemsInserted(KItemRangeList)));
1031 connect(m_model, SIGNAL(itemsRemoved(KItemRangeList)),
1032 this, SLOT(slotItemsRemoved(KItemRangeList)));
1033 }
1034
1035 onModelChanged(model, previous);
1036 }
1037
1038 KItemListRubberBand* KItemListView::rubberBand() const
1039 {
1040 return m_rubberBand;
1041 }
1042
1043 void KItemListView::updateLayout()
1044 {
1045 doLayout(Animation, 0, 0);
1046 update();
1047 }
1048
1049 void KItemListView::doLayout(LayoutAnimationHint hint, int changedIndex, int changedCount)
1050 {
1051 if (m_layoutTimer->isActive()) {
1052 kDebug() << "Stopping layout timer, synchronous layout requested";
1053 m_layoutTimer->stop();
1054 }
1055
1056 if (m_model->count() < 0 || m_activeTransactions > 0) {
1057 return;
1058 }
1059
1060 applyDynamicItemSize();
1061
1062 const int firstVisibleIndex = m_layouter->firstVisibleIndex();
1063 const int lastVisibleIndex = m_layouter->lastVisibleIndex();
1064 if (firstVisibleIndex < 0) {
1065 emitOffsetChanges();
1066 return;
1067 }
1068
1069 // Do a sanity check of the offset-property: When properties of the itemlist-view have been changed
1070 // it might be possible that the maximum offset got changed too. Assure that the full visible range
1071 // is still shown if the maximum offset got decreased.
1072 const qreal visibleOffsetRange = (scrollOrientation() == Qt::Horizontal) ? size().width() : size().height();
1073 const qreal maxOffsetToShowFullRange = maximumOffset() - visibleOffsetRange;
1074 if (offset() > maxOffsetToShowFullRange) {
1075 m_layouter->setOffset(qMax(qreal(0), maxOffsetToShowFullRange));
1076 }
1077
1078 // Determine all items that are completely invisible and might be
1079 // reused for items that just got (at least partly) visible.
1080 // Items that do e.g. an animated moving of their position are not
1081 // marked as invisible: This assures that a scrolling inside the view
1082 // can be done without breaking an animation.
1083 QList<int> reusableItems;
1084 QHashIterator<int, KItemListWidget*> it(m_visibleItems);
1085 while (it.hasNext()) {
1086 it.next();
1087 KItemListWidget* widget = it.value();
1088 const int index = widget->index();
1089 const bool invisible = (index < firstVisibleIndex) || (index > lastVisibleIndex);
1090 if (invisible && !m_animation->isStarted(widget)) {
1091 widget->setVisible(false);
1092 reusableItems.append(index);
1093 }
1094 }
1095
1096 // Assure that for each visible item a KItemListWidget is available. KItemListWidget
1097 // instances from invisible items are reused. If no reusable items are
1098 // found then new KItemListWidget instances get created.
1099 const bool animate = (hint == Animation);
1100 for (int i = firstVisibleIndex; i <= lastVisibleIndex; ++i) {
1101 bool applyNewPos = true;
1102 bool wasHidden = false;
1103
1104 const QRectF itemBounds = m_layouter->itemBoundingRect(i);
1105 const QPointF newPos = itemBounds.topLeft();
1106 KItemListWidget* widget = m_visibleItems.value(i);
1107 if (!widget) {
1108 wasHidden = true;
1109 if (!reusableItems.isEmpty()) {
1110 // Reuse a KItemListWidget instance from an invisible item
1111 const int oldIndex = reusableItems.takeLast();
1112 widget = m_visibleItems.value(oldIndex);
1113 setWidgetIndex(widget, i);
1114 } else {
1115 // No reusable KItemListWidget instance is available, create a new one
1116 widget = createWidget(i);
1117 }
1118 widget->resize(itemBounds.size());
1119
1120 if (animate && changedCount < 0) {
1121 // Items have been deleted, move the created item to the
1122 // imaginary old position.
1123 const QRectF itemBoundingRect = m_layouter->itemBoundingRect(i - changedCount);
1124 if (itemBoundingRect.isEmpty()) {
1125 const QPointF invisibleOldPos = (scrollOrientation() == Qt::Vertical)
1126 ? QPointF(0, size().height()) : QPointF(size().width(), 0);
1127 widget->setPos(invisibleOldPos);
1128 } else {
1129 widget->setPos(itemBoundingRect.topLeft());
1130 }
1131 applyNewPos = false;
1132 }
1133 } else if (m_animation->isStarted(widget, KItemListViewAnimation::MovingAnimation)) {
1134 applyNewPos = false;
1135 }
1136
1137 if (animate) {
1138 const bool itemsRemoved = (changedCount < 0);
1139 const bool itemsInserted = (changedCount > 0);
1140
1141 if (itemsRemoved && (i >= changedIndex + changedCount + 1)) {
1142 // The item is located after the removed items. Animate the moving of the position.
1143 m_animation->start(widget, KItemListViewAnimation::MovingAnimation, newPos);
1144 applyNewPos = false;
1145 } else if (itemsInserted && i >= changedIndex) {
1146 // The item is located after the first inserted item
1147 if (i <= changedIndex + changedCount - 1) {
1148 // The item is an inserted item. Animate the appearing of the item.
1149 // For performance reasons no animation is done when changedCount is equal
1150 // to all available items.
1151 if (changedCount < m_model->count()) {
1152 m_animation->start(widget, KItemListViewAnimation::CreateAnimation);
1153 }
1154 } else if (!m_animation->isStarted(widget, KItemListViewAnimation::CreateAnimation)) {
1155 // The item was already there before, so animate the moving of the position.
1156 // No moving animation is done if the item is animated by a create animation: This
1157 // prevents a "move animation mess" when inserting several ranges in parallel.
1158 m_animation->start(widget, KItemListViewAnimation::MovingAnimation, newPos);
1159 applyNewPos = false;
1160 }
1161 } else if (!itemsRemoved && !itemsInserted && !wasHidden) {
1162 // The size of the view might have been changed. Animate the moving of the position.
1163 m_animation->start(widget, KItemListViewAnimation::MovingAnimation, newPos);
1164 applyNewPos = false;
1165 }
1166 }
1167
1168 if (applyNewPos) {
1169 widget->setPos(newPos);
1170 }
1171
1172 Q_ASSERT(widget->index() == i);
1173 widget->setVisible(true);
1174
1175 if (widget->size() != itemBounds.size()) {
1176 m_animation->start(widget, KItemListViewAnimation::ResizeAnimation, itemBounds.size());
1177 }
1178 }
1179
1180 // Delete invisible KItemListWidget instances that have not been reused
1181 foreach (int index, reusableItems) {
1182 recycleWidget(m_visibleItems.value(index));
1183 }
1184
1185 emitOffsetChanges();
1186 }
1187
1188 void KItemListView::emitOffsetChanges()
1189 {
1190 const qreal newOffset = m_layouter->offset();
1191 if (m_oldOffset != newOffset) {
1192 emit offsetChanged(newOffset, m_oldOffset);
1193 m_oldOffset = newOffset;
1194 }
1195
1196 const qreal newMaximumOffset = m_layouter->maximumOffset();
1197 if (m_oldMaximumOffset != newMaximumOffset) {
1198 emit maximumOffsetChanged(newMaximumOffset, m_oldMaximumOffset);
1199 m_oldMaximumOffset = newMaximumOffset;
1200 }
1201 }
1202
1203 KItemListWidget* KItemListView::createWidget(int index)
1204 {
1205 KItemListWidget* widget = m_widgetCreator->create(this);
1206 updateWidgetProperties(widget, index);
1207 m_visibleItems.insert(index, widget);
1208
1209 if (m_grouped) {
1210 if (m_layouter->isFirstGroupItem(index)) {
1211 KItemListGroupHeader* header = m_groupHeaderCreator->create(widget);
1212 header->setPos(0, -50);
1213 header->resize(50, 50);
1214 m_visibleGroups.insert(widget, header);
1215 }
1216 }
1217
1218 initializeItemListWidget(widget);
1219 return widget;
1220 }
1221
1222 void KItemListView::recycleWidget(KItemListWidget* widget)
1223 {
1224 if (m_grouped) {
1225 KItemListGroupHeader* header = m_visibleGroups.value(widget);
1226 if (header) {
1227 m_groupHeaderCreator->recycle(header);
1228 m_visibleGroups.remove(widget);
1229 }
1230 }
1231
1232 m_visibleItems.remove(widget->index());
1233 m_widgetCreator->recycle(widget);
1234 }
1235
1236 void KItemListView::setWidgetIndex(KItemListWidget* widget, int index)
1237 {
1238 if (m_grouped) {
1239 bool createHeader = m_layouter->isFirstGroupItem(index);
1240 KItemListGroupHeader* header = m_visibleGroups.value(widget);
1241 if (header) {
1242 if (createHeader) {
1243 createHeader = false;
1244 } else {
1245 m_groupHeaderCreator->recycle(header);
1246 m_visibleGroups.remove(widget);
1247 }
1248 }
1249
1250 if (createHeader) {
1251 KItemListGroupHeader* header = m_groupHeaderCreator->create(widget);
1252 header->setPos(0, -50);
1253 header->resize(50, 50);
1254 m_visibleGroups.insert(widget, header);
1255 }
1256 }
1257
1258 const int oldIndex = widget->index();
1259 m_visibleItems.remove(oldIndex);
1260 updateWidgetProperties(widget, index);
1261 m_visibleItems.insert(index, widget);
1262
1263 initializeItemListWidget(widget);
1264 }
1265
1266 void KItemListView::prepareLayoutForIncreasedItemCount(const QSizeF& size, SizeType sizeType)
1267 {
1268 // Calculate the first visible index and last visible index for the current size
1269 const int currentFirst = m_layouter->firstVisibleIndex();
1270 const int currentLast = m_layouter->lastVisibleIndex();
1271
1272 const QSizeF currentSize = (sizeType == LayouterSize) ? m_layouter->size() : m_layouter->itemSize();
1273
1274 // Calculate the first visible index and last visible index for the new size
1275 setLayouterSize(size, sizeType);
1276 const int newFirst = m_layouter->firstVisibleIndex();
1277 const int newLast = m_layouter->lastVisibleIndex();
1278
1279 if ((currentFirst != newFirst) || (currentLast != newLast)) {
1280 // At least one index has been changed. Assure that widgets for all possible
1281 // visible items get created so that a move-animation can be started later.
1282 const int maxVisibleItems = m_layouter->maximumVisibleItems();
1283 int minFirst = qMin(newFirst, currentFirst);
1284 const int maxLast = qMax(newLast, currentLast);
1285
1286 if (maxLast - minFirst + 1 < maxVisibleItems) {
1287 // Increasing the size might result in a smaller KItemListView::offset().
1288 // Decrease the first visible index in a way that at least the maximum
1289 // visible items are shown.
1290 minFirst = qMax(0, maxLast - maxVisibleItems + 1);
1291 }
1292
1293 if (maxLast - minFirst > maxVisibleItems + maxVisibleItems / 2) {
1294 // The creating of widgets is quite expensive. Assure that never more
1295 // than 50 % of the maximum visible items get created for the animations.
1296 return;
1297 }
1298
1299 setLayouterSize(currentSize, sizeType);
1300 for (int i = minFirst; i <= maxLast; ++i) {
1301 if (!m_visibleItems.contains(i)) {
1302 KItemListWidget* widget = createWidget(i);
1303 const QPointF pos = m_layouter->itemBoundingRect(i).topLeft();
1304 widget->setPos(pos);
1305 }
1306 }
1307 setLayouterSize(size, sizeType);
1308 }
1309 }
1310
1311 void KItemListView::setLayouterSize(const QSizeF& size, SizeType sizeType)
1312 {
1313 switch (sizeType) {
1314 case LayouterSize: m_layouter->setSize(size); break;
1315 case ItemSize: m_layouter->setItemSize(size); break;
1316 default: break;
1317 }
1318 }
1319
1320 void KItemListView::applyDynamicItemSize()
1321 {
1322 if (!m_itemSize.isEmpty()) {
1323 return;
1324 }
1325
1326 if (m_visibleRolesSizes.isEmpty()) {
1327 m_visibleRolesSizes = visibleRoleSizes();
1328 if (m_header) {
1329 m_header->setVisibleRolesWidths(headerRolesWidths());
1330 }
1331 }
1332
1333 if (m_layouter->itemSize().isEmpty()) {
1334 // Calculate the maximum size of an item by considering the
1335 // visible role sizes and apply them to the layouter.
1336 qreal requiredWidth = 0;
1337 qreal requiredHeight = 0;
1338
1339 QHashIterator<QByteArray, QSizeF> it(m_visibleRolesSizes);
1340 while (it.hasNext()) {
1341 it.next();
1342 const QSizeF& visibleRoleSize = it.value();
1343 requiredWidth += visibleRoleSize.width();
1344 requiredHeight += visibleRoleSize.height();
1345 }
1346
1347 QSizeF dynamicItemSize = m_itemSize;
1348 if (dynamicItemSize.width() <= 0) {
1349 dynamicItemSize.setWidth(qMax(requiredWidth, size().width()));
1350 }
1351 if (dynamicItemSize.height() <= 0) {
1352 dynamicItemSize.setHeight(qMax(requiredHeight, size().height()));
1353 }
1354
1355 m_layouter->setItemSize(dynamicItemSize);
1356
1357 // Update the role sizes for all visible widgets
1358 foreach (KItemListWidget* widget, visibleItemListWidgets()) {
1359 widget->setVisibleRolesSizes(m_visibleRolesSizes);
1360 }
1361 }
1362 }
1363
1364 void KItemListView::updateWidgetProperties(KItemListWidget* widget, int index)
1365 {
1366 widget->setVisibleRoles(m_visibleRoles);
1367 widget->setVisibleRolesSizes(m_visibleRolesSizes);
1368 widget->setStyleOption(m_styleOption);
1369
1370 const KItemListSelectionManager* selectionManager = m_controller->selectionManager();
1371 widget->setCurrent(index == selectionManager->currentItem());
1372
1373 if (selectionManager->hasSelection()) {
1374 const QSet<int> selectedItems = selectionManager->selectedItems();
1375 widget->setSelected(selectedItems.contains(index));
1376 } else {
1377 widget->setSelected(false);
1378 }
1379
1380 widget->setHovered(false);
1381
1382 widget->setIndex(index);
1383 widget->setData(m_model->data(index));
1384 }
1385
1386 void KItemListView::updateHeaderWidth()
1387 {
1388 if (!m_header) {
1389 return;
1390 }
1391
1392 // TODO 1: Use the required width of all roles
1393 m_header->resize(size().width(), m_header->size().height());
1394 }
1395
1396 QHash<QByteArray, qreal> KItemListView::headerRolesWidths() const
1397 {
1398 QHash<QByteArray, qreal> rolesWidths;
1399
1400 QHashIterator<QByteArray, QSizeF> it(m_visibleRolesSizes);
1401 while (it.hasNext()) {
1402 it.next();
1403 rolesWidths.insert(it.key(), it.value().width());
1404 }
1405
1406 return rolesWidths;
1407 }
1408
1409 int KItemListView::calculateAutoScrollingIncrement(int pos, int range, int oldInc)
1410 {
1411 int inc = 0;
1412
1413 const int minSpeed = 4;
1414 const int maxSpeed = 128;
1415 const int speedLimiter = 96;
1416 const int autoScrollBorder = 64;
1417
1418 // Limit the increment that is allowed to be added in comparison to 'oldInc'.
1419 // This assures that the autoscrolling speed grows gradually.
1420 const int incLimiter = 1;
1421
1422 if (pos < autoScrollBorder) {
1423 inc = -minSpeed + qAbs(pos - autoScrollBorder) * (pos - autoScrollBorder) / speedLimiter;
1424 inc = qMax(inc, -maxSpeed);
1425 inc = qMax(inc, oldInc - incLimiter);
1426 } else if (pos > range - autoScrollBorder) {
1427 inc = minSpeed + qAbs(pos - range + autoScrollBorder) * (pos - range + autoScrollBorder) / speedLimiter;
1428 inc = qMin(inc, maxSpeed);
1429 inc = qMin(inc, oldInc + incLimiter);
1430 }
1431
1432 return inc;
1433 }
1434
1435
1436 KItemListCreatorBase::~KItemListCreatorBase()
1437 {
1438 qDeleteAll(m_recycleableWidgets);
1439 qDeleteAll(m_createdWidgets);
1440 }
1441
1442 void KItemListCreatorBase::addCreatedWidget(QGraphicsWidget* widget)
1443 {
1444 m_createdWidgets.insert(widget);
1445 }
1446
1447 void KItemListCreatorBase::pushRecycleableWidget(QGraphicsWidget* widget)
1448 {
1449 Q_ASSERT(m_createdWidgets.contains(widget));
1450 m_createdWidgets.remove(widget);
1451
1452 if (m_recycleableWidgets.count() < 100) {
1453 m_recycleableWidgets.append(widget);
1454 widget->setVisible(false);
1455 } else {
1456 delete widget;
1457 }
1458 }
1459
1460 QGraphicsWidget* KItemListCreatorBase::popRecycleableWidget()
1461 {
1462 if (m_recycleableWidgets.isEmpty()) {
1463 return 0;
1464 }
1465
1466 QGraphicsWidget* widget = m_recycleableWidgets.takeLast();
1467 m_createdWidgets.insert(widget);
1468 return widget;
1469 }
1470
1471 KItemListWidgetCreatorBase::~KItemListWidgetCreatorBase()
1472 {
1473 }
1474
1475 void KItemListWidgetCreatorBase::recycle(KItemListWidget* widget)
1476 {
1477 widget->setOpacity(1.0);
1478 pushRecycleableWidget(widget);
1479 }
1480
1481 KItemListGroupHeaderCreatorBase::~KItemListGroupHeaderCreatorBase()
1482 {
1483 }
1484
1485 void KItemListGroupHeaderCreatorBase::recycle(KItemListGroupHeader* header)
1486 {
1487 header->setOpacity(1.0);
1488 pushRecycleableWidget(header);
1489 }
1490
1491 #include "kitemlistview.moc"