]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistview.cpp
Merge branch for accessibility
[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 <KDebug>
26 #include "kitemlistcontroller.h"
27 #include "kitemlistheader.h"
28 #include "kitemlistselectionmanager.h"
29 #include "kitemlistwidget.h"
30
31 #include "private/kitemlistheaderwidget.h"
32 #include "private/kitemlistrubberband.h"
33 #include "private/kitemlistsizehintresolver.h"
34 #include "private/kitemlistviewlayouter.h"
35 #include "private/kitemlistviewanimation.h"
36
37 #include <QCursor>
38 #include <QGraphicsSceneMouseEvent>
39 #include <QGraphicsView>
40 #include <QPainter>
41 #include <QPropertyAnimation>
42 #include <QStyle>
43 #include <QStyleOptionRubberBand>
44 #include <QTimer>
45
46 #include "kitemlistviewaccessible.h"
47
48 namespace {
49 // Time in ms until reaching the autoscroll margin triggers
50 // an initial autoscrolling
51 const int InitialAutoScrollDelay = 700;
52
53 // Delay in ms for triggering the next autoscroll
54 const int RepeatingAutoScrollDelay = 1000 / 60;
55 }
56
57 #ifndef QT_NO_ACCESSIBILITY
58 QAccessibleInterface* accessibleInterfaceFactory(const QString &key, QObject *object)
59 {
60 Q_UNUSED(key)
61 if (KItemListContainer*view = qobject_cast<KItemListContainer*>(object)) {
62 return new KItemListContainerAccessible(view);
63 }
64 if (KItemListView *view = qobject_cast<KItemListView*>(object)) {
65 return new KItemListViewAccessible(view);
66 }
67 return 0;
68 }
69 #endif
70
71 KItemListView::KItemListView(QGraphicsWidget* parent) :
72 QGraphicsWidget(parent),
73 m_enabledSelectionToggles(false),
74 m_grouped(false),
75 m_supportsItemExpanding(false),
76 m_editingRole(false),
77 m_activeTransactions(0),
78 m_endTransactionAnimationHint(Animation),
79 m_itemSize(),
80 m_controller(0),
81 m_model(0),
82 m_visibleRoles(),
83 m_widgetCreator(0),
84 m_groupHeaderCreator(0),
85 m_styleOption(),
86 m_visibleItems(),
87 m_visibleGroups(),
88 m_visibleCells(),
89 m_sizeHintResolver(0),
90 m_layouter(0),
91 m_animation(0),
92 m_layoutTimer(0),
93 m_oldScrollOffset(0),
94 m_oldMaximumScrollOffset(0),
95 m_oldItemOffset(0),
96 m_oldMaximumItemOffset(0),
97 m_skipAutoScrollForRubberBand(false),
98 m_rubberBand(0),
99 m_mousePos(),
100 m_autoScrollIncrement(0),
101 m_autoScrollTimer(0),
102 m_header(0),
103 m_headerWidget(0),
104 m_dropIndicator()
105 {
106 setAcceptHoverEvents(true);
107
108 m_sizeHintResolver = new KItemListSizeHintResolver(this);
109
110 m_layouter = new KItemListViewLayouter(this);
111 m_layouter->setSizeHintResolver(m_sizeHintResolver);
112
113 m_animation = new KItemListViewAnimation(this);
114 connect(m_animation, SIGNAL(finished(QGraphicsWidget*,KItemListViewAnimation::AnimationType)),
115 this, SLOT(slotAnimationFinished(QGraphicsWidget*,KItemListViewAnimation::AnimationType)));
116
117 m_layoutTimer = new QTimer(this);
118 m_layoutTimer->setInterval(300);
119 m_layoutTimer->setSingleShot(true);
120 connect(m_layoutTimer, SIGNAL(timeout()), this, SLOT(slotLayoutTimerFinished()));
121
122 m_rubberBand = new KItemListRubberBand(this);
123 connect(m_rubberBand, SIGNAL(activationChanged(bool)), this, SLOT(slotRubberBandActivationChanged(bool)));
124
125 m_headerWidget = new KItemListHeaderWidget(this);
126 m_headerWidget->setVisible(false);
127
128 m_header = new KItemListHeader(this);
129
130 #ifndef QT_NO_ACCESSIBILITY
131 QAccessible::installFactory(accessibleInterfaceFactory);
132 #endif
133
134 }
135
136 KItemListView::~KItemListView()
137 {
138 // The group headers are children of the widgets created by
139 // widgetCreator(). So it is mandatory to delete the group headers
140 // first.
141 delete m_groupHeaderCreator;
142 m_groupHeaderCreator = 0;
143
144 delete m_widgetCreator;
145 m_widgetCreator = 0;
146
147 delete m_sizeHintResolver;
148 m_sizeHintResolver = 0;
149 }
150
151 void KItemListView::setScrollOffset(qreal offset)
152 {
153 if (offset < 0) {
154 offset = 0;
155 }
156
157 const qreal previousOffset = m_layouter->scrollOffset();
158 if (offset == previousOffset) {
159 return;
160 }
161
162 m_layouter->setScrollOffset(offset);
163 m_animation->setScrollOffset(offset);
164
165 // Don't check whether the m_layoutTimer is active: Changing the
166 // scroll offset must always trigger a synchronous layout, otherwise
167 // the smooth-scrolling might get jerky.
168 doLayout(NoAnimation);
169 onScrollOffsetChanged(offset, previousOffset);
170 }
171
172 qreal KItemListView::scrollOffset() const
173 {
174 return m_layouter->scrollOffset();
175 }
176
177 qreal KItemListView::maximumScrollOffset() const
178 {
179 return m_layouter->maximumScrollOffset();
180 }
181
182 void KItemListView::setItemOffset(qreal offset)
183 {
184 if (m_layouter->itemOffset() == offset) {
185 return;
186 }
187
188 m_layouter->setItemOffset(offset);
189 if (m_headerWidget->isVisible()) {
190 m_headerWidget->setOffset(offset);
191 }
192
193 // Don't check whether the m_layoutTimer is active: Changing the
194 // item offset must always trigger a synchronous layout, otherwise
195 // the smooth-scrolling might get jerky.
196 doLayout(NoAnimation);
197 }
198
199 qreal KItemListView::itemOffset() const
200 {
201 return m_layouter->itemOffset();
202 }
203
204 qreal KItemListView::maximumItemOffset() const
205 {
206 return m_layouter->maximumItemOffset();
207 }
208
209 void KItemListView::setVisibleRoles(const QList<QByteArray>& roles)
210 {
211 const QList<QByteArray> previousRoles = m_visibleRoles;
212 m_visibleRoles = roles;
213 onVisibleRolesChanged(roles, previousRoles);
214
215 m_sizeHintResolver->clearCache();
216 m_layouter->markAsDirty();
217
218 if (m_itemSize.isEmpty()) {
219 m_headerWidget->setColumns(roles);
220 updatePreferredColumnWidths();
221 if (!m_headerWidget->automaticColumnResizing()) {
222 // The column-width of new roles are still 0. Apply the preferred
223 // column-width as default with.
224 foreach (const QByteArray& role, m_visibleRoles) {
225 if (m_headerWidget->columnWidth(role) == 0) {
226 const qreal width = m_headerWidget->preferredColumnWidth(role);
227 m_headerWidget->setColumnWidth(role, width);
228 }
229 }
230
231 applyColumnWidthsFromHeader();
232 }
233 }
234
235 const bool alternateBackgroundsChanged = m_itemSize.isEmpty() &&
236 ((roles.count() > 1 && previousRoles.count() <= 1) ||
237 (roles.count() <= 1 && previousRoles.count() > 1));
238
239 QHashIterator<int, KItemListWidget*> it(m_visibleItems);
240 while (it.hasNext()) {
241 it.next();
242 KItemListWidget* widget = it.value();
243 widget->setVisibleRoles(roles);
244 if (alternateBackgroundsChanged) {
245 updateAlternateBackgroundForWidget(widget);
246 }
247 }
248
249 doLayout(NoAnimation);
250 }
251
252 QList<QByteArray> KItemListView::visibleRoles() const
253 {
254 return m_visibleRoles;
255 }
256
257 void KItemListView::setAutoScroll(bool enabled)
258 {
259 if (enabled && !m_autoScrollTimer) {
260 m_autoScrollTimer = new QTimer(this);
261 m_autoScrollTimer->setSingleShot(true);
262 connect(m_autoScrollTimer, SIGNAL(timeout()), this, SLOT(triggerAutoScrolling()));
263 m_autoScrollTimer->start(InitialAutoScrollDelay);
264 } else if (!enabled && m_autoScrollTimer) {
265 delete m_autoScrollTimer;
266 m_autoScrollTimer = 0;
267 }
268 }
269
270 bool KItemListView::autoScroll() const
271 {
272 return m_autoScrollTimer != 0;
273 }
274
275 void KItemListView::setEnabledSelectionToggles(bool enabled)
276 {
277 if (m_enabledSelectionToggles != enabled) {
278 m_enabledSelectionToggles = enabled;
279
280 QHashIterator<int, KItemListWidget*> it(m_visibleItems);
281 while (it.hasNext()) {
282 it.next();
283 it.value()->setEnabledSelectionToggle(enabled);
284 }
285 }
286 }
287
288 bool KItemListView::enabledSelectionToggles() const
289 {
290 return m_enabledSelectionToggles;
291 }
292
293 KItemListController* KItemListView::controller() const
294 {
295 return m_controller;
296 }
297
298 KItemModelBase* KItemListView::model() const
299 {
300 return m_model;
301 }
302
303 void KItemListView::setWidgetCreator(KItemListWidgetCreatorBase* widgetCreator)
304 {
305 if (m_widgetCreator) {
306 delete m_widgetCreator;
307 }
308 m_widgetCreator = widgetCreator;
309 }
310
311 KItemListWidgetCreatorBase* KItemListView::widgetCreator() const
312 {
313 if (!m_widgetCreator) {
314 m_widgetCreator = defaultWidgetCreator();
315 }
316 return m_widgetCreator;
317 }
318
319 void KItemListView::setGroupHeaderCreator(KItemListGroupHeaderCreatorBase* groupHeaderCreator)
320 {
321 if (m_groupHeaderCreator) {
322 delete m_groupHeaderCreator;
323 }
324 m_groupHeaderCreator = groupHeaderCreator;
325 }
326
327 KItemListGroupHeaderCreatorBase* KItemListView::groupHeaderCreator() const
328 {
329 if (!m_groupHeaderCreator) {
330 m_groupHeaderCreator = defaultGroupHeaderCreator();
331 }
332 return m_groupHeaderCreator;
333 }
334
335 QSizeF KItemListView::itemSize() const
336 {
337 return m_itemSize;
338 }
339
340 const KItemListStyleOption& KItemListView::styleOption() const
341 {
342 return m_styleOption;
343 }
344
345 void KItemListView::setGeometry(const QRectF& rect)
346 {
347 QGraphicsWidget::setGeometry(rect);
348
349 if (!m_model) {
350 return;
351 }
352
353 const QSizeF newSize = rect.size();
354 if (m_itemSize.isEmpty()) {
355 m_headerWidget->resize(rect.width(), m_headerWidget->size().height());
356 if (m_headerWidget->automaticColumnResizing()) {
357 applyAutomaticColumnWidths();
358 } else {
359 const qreal requiredWidth = columnWidthsSum();
360 const QSizeF dynamicItemSize(qMax(newSize.width(), requiredWidth),
361 m_itemSize.height());
362 m_layouter->setItemSize(dynamicItemSize);
363 }
364
365 // Triggering a synchronous layout is fine from a performance point of view,
366 // as with dynamic item sizes no moving animation must be done.
367 m_layouter->setSize(newSize);
368 doLayout(NoAnimation);
369 } else {
370 const bool animate = !changesItemGridLayout(newSize,
371 m_layouter->itemSize(),
372 m_layouter->itemMargin());
373 m_layouter->setSize(newSize);
374
375 if (animate) {
376 // Trigger an asynchronous relayout with m_layoutTimer to prevent
377 // performance bottlenecks. If the timer is exceeded, an animated layout
378 // will be triggered.
379 if (!m_layoutTimer->isActive()) {
380 m_layoutTimer->start();
381 }
382 } else {
383 m_layoutTimer->stop();
384 doLayout(NoAnimation);
385 }
386 }
387 }
388
389 int KItemListView::itemAt(const QPointF& pos) const
390 {
391 QHashIterator<int, KItemListWidget*> it(m_visibleItems);
392 while (it.hasNext()) {
393 it.next();
394
395 const KItemListWidget* widget = it.value();
396 const QPointF mappedPos = widget->mapFromItem(this, pos);
397 if (widget->contains(mappedPos)) {
398 return it.key();
399 }
400 }
401
402 return -1;
403 }
404
405 bool KItemListView::isAboveSelectionToggle(int index, const QPointF& pos) const
406 {
407 if (!m_enabledSelectionToggles) {
408 return false;
409 }
410
411 const KItemListWidget* widget = m_visibleItems.value(index);
412 if (widget) {
413 const QRectF selectionToggleRect = widget->selectionToggleRect();
414 if (!selectionToggleRect.isEmpty()) {
415 const QPointF mappedPos = widget->mapFromItem(this, pos);
416 return selectionToggleRect.contains(mappedPos);
417 }
418 }
419 return false;
420 }
421
422 bool KItemListView::isAboveExpansionToggle(int index, const QPointF& pos) const
423 {
424 const KItemListWidget* widget = m_visibleItems.value(index);
425 if (widget) {
426 const QRectF expansionToggleRect = widget->expansionToggleRect();
427 if (!expansionToggleRect.isEmpty()) {
428 const QPointF mappedPos = widget->mapFromItem(this, pos);
429 return expansionToggleRect.contains(mappedPos);
430 }
431 }
432 return false;
433 }
434
435 int KItemListView::firstVisibleIndex() const
436 {
437 return m_layouter->firstVisibleIndex();
438 }
439
440 int KItemListView::lastVisibleIndex() const
441 {
442 return m_layouter->lastVisibleIndex();
443 }
444
445 QSizeF KItemListView::itemSizeHint(int index) const
446 {
447 return widgetCreator()->itemSizeHint(index, this);
448 }
449
450 void KItemListView::setSupportsItemExpanding(bool supportsExpanding)
451 {
452 if (m_supportsItemExpanding != supportsExpanding) {
453 m_supportsItemExpanding = supportsExpanding;
454 updateSiblingsInformation();
455 onSupportsItemExpandingChanged(supportsExpanding);
456 }
457 }
458
459 bool KItemListView::supportsItemExpanding() const
460 {
461 return m_supportsItemExpanding;
462 }
463
464 QRectF KItemListView::itemRect(int index) const
465 {
466 return m_layouter->itemRect(index);
467 }
468
469 QRectF KItemListView::itemContextRect(int index) const
470 {
471 QRectF contextRect;
472
473 const KItemListWidget* widget = m_visibleItems.value(index);
474 if (widget) {
475 contextRect = widget->iconRect() | widget->textRect();
476 contextRect.translate(itemRect(index).topLeft());
477 }
478
479 return contextRect;
480 }
481
482 void KItemListView::scrollToItem(int index)
483 {
484 QRectF viewGeometry = geometry();
485 if (m_headerWidget->isVisible()) {
486 const qreal headerHeight = m_headerWidget->size().height();
487 viewGeometry.adjust(0, headerHeight, 0, 0);
488 }
489 const QRectF currentRect = itemRect(index);
490
491 if (!viewGeometry.contains(currentRect)) {
492 qreal newOffset = scrollOffset();
493 if (scrollOrientation() == Qt::Vertical) {
494 if (currentRect.top() < viewGeometry.top()) {
495 newOffset += currentRect.top() - viewGeometry.top();
496 } else if (currentRect.bottom() > viewGeometry.bottom()) {
497 newOffset += currentRect.bottom() - viewGeometry.bottom();
498 }
499 } else {
500 if (currentRect.left() < viewGeometry.left()) {
501 newOffset += currentRect.left() - viewGeometry.left();
502 } else if (currentRect.right() > viewGeometry.right()) {
503 newOffset += currentRect.right() - viewGeometry.right();
504 }
505 }
506
507 if (newOffset != scrollOffset()) {
508 emit scrollTo(newOffset);
509 }
510 }
511 }
512
513 void KItemListView::beginTransaction()
514 {
515 ++m_activeTransactions;
516 if (m_activeTransactions == 1) {
517 onTransactionBegin();
518 }
519 }
520
521 void KItemListView::endTransaction()
522 {
523 --m_activeTransactions;
524 if (m_activeTransactions < 0) {
525 m_activeTransactions = 0;
526 kWarning() << "Mismatch between beginTransaction()/endTransaction()";
527 }
528
529 if (m_activeTransactions == 0) {
530 onTransactionEnd();
531 doLayout(m_endTransactionAnimationHint);
532 m_endTransactionAnimationHint = Animation;
533 }
534 }
535
536 bool KItemListView::isTransactionActive() const
537 {
538 return m_activeTransactions > 0;
539 }
540
541 void KItemListView::setHeaderVisible(bool visible)
542 {
543 if (visible && !m_headerWidget->isVisible()) {
544 QStyleOptionHeader option;
545 const QSize headerSize = style()->sizeFromContents(QStyle::CT_HeaderSection,
546 &option, QSize());
547
548 m_headerWidget->setPos(0, 0);
549 m_headerWidget->resize(size().width(), headerSize.height());
550 m_headerWidget->setModel(m_model);
551 m_headerWidget->setColumns(m_visibleRoles);
552 m_headerWidget->setZValue(1);
553
554 connect(m_headerWidget, SIGNAL(columnWidthChanged(QByteArray,qreal,qreal)),
555 this, SLOT(slotHeaderColumnWidthChanged(QByteArray,qreal,qreal)));
556 connect(m_headerWidget, SIGNAL(columnMoved(QByteArray,int,int)),
557 this, SLOT(slotHeaderColumnMoved(QByteArray,int,int)));
558 connect(m_headerWidget, SIGNAL(sortOrderChanged(Qt::SortOrder,Qt::SortOrder)),
559 this, SIGNAL(sortOrderChanged(Qt::SortOrder,Qt::SortOrder)));
560 connect(m_headerWidget, SIGNAL(sortRoleChanged(QByteArray,QByteArray)),
561 this, SIGNAL(sortRoleChanged(QByteArray,QByteArray)));
562
563 m_layouter->setHeaderHeight(headerSize.height());
564 m_headerWidget->setVisible(true);
565 } else if (!visible && m_headerWidget->isVisible()) {
566 disconnect(m_headerWidget, SIGNAL(columnWidthChanged(QByteArray,qreal,qreal)),
567 this, SLOT(slotHeaderColumnWidthChanged(QByteArray,qreal,qreal)));
568 disconnect(m_headerWidget, SIGNAL(columnMoved(QByteArray,int,int)),
569 this, SLOT(slotHeaderColumnMoved(QByteArray,int,int)));
570 disconnect(m_headerWidget, SIGNAL(sortOrderChanged(Qt::SortOrder,Qt::SortOrder)),
571 this, SIGNAL(sortOrderChanged(Qt::SortOrder,Qt::SortOrder)));
572 disconnect(m_headerWidget, SIGNAL(sortRoleChanged(QByteArray,QByteArray)),
573 this, SIGNAL(sortRoleChanged(QByteArray,QByteArray)));
574
575 m_layouter->setHeaderHeight(0);
576 m_headerWidget->setVisible(false);
577 }
578 }
579
580 bool KItemListView::isHeaderVisible() const
581 {
582 return m_headerWidget->isVisible();
583 }
584
585 KItemListHeader* KItemListView::header() const
586 {
587 return m_header;
588 }
589
590 QPixmap KItemListView::createDragPixmap(const QSet<int>& indexes) const
591 {
592 QPixmap pixmap;
593
594 if (indexes.count() == 1) {
595 KItemListWidget* item = m_visibleItems.value(indexes.toList().first());
596 QGraphicsView* graphicsView = scene()->views()[0];
597 if (item && graphicsView) {
598 pixmap = item->createDragPixmap(0, graphicsView);
599 }
600 } else {
601 // TODO: Not implemented yet. Probably extend the interface
602 // from KItemListWidget::createDragPixmap() to return a pixmap
603 // that can be used for multiple indexes.
604 }
605
606 return pixmap;
607 }
608
609 void KItemListView::editRole(int index, const QByteArray& role)
610 {
611 KItemListWidget* widget = m_visibleItems.value(index);
612 if (!widget || m_editingRole) {
613 return;
614 }
615
616 m_editingRole = true;
617 widget->setEditedRole(role);
618
619 connect(widget, SIGNAL(roleEditingCanceled(int,QByteArray,QVariant)),
620 this, SLOT(slotRoleEditingCanceled(int,QByteArray,QVariant)));
621 connect(widget, SIGNAL(roleEditingFinished(int,QByteArray,QVariant)),
622 this, SLOT(slotRoleEditingFinished(int,QByteArray,QVariant)));
623 }
624
625 void KItemListView::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
626 {
627 QGraphicsWidget::paint(painter, option, widget);
628
629 if (m_rubberBand->isActive()) {
630 QRectF rubberBandRect = QRectF(m_rubberBand->startPosition(),
631 m_rubberBand->endPosition()).normalized();
632
633 const QPointF topLeft = rubberBandRect.topLeft();
634 if (scrollOrientation() == Qt::Vertical) {
635 rubberBandRect.moveTo(topLeft.x(), topLeft.y() - scrollOffset());
636 } else {
637 rubberBandRect.moveTo(topLeft.x() - scrollOffset(), topLeft.y());
638 }
639
640 QStyleOptionRubberBand opt;
641 opt.initFrom(widget);
642 opt.shape = QRubberBand::Rectangle;
643 opt.opaque = false;
644 opt.rect = rubberBandRect.toRect();
645 style()->drawControl(QStyle::CE_RubberBand, &opt, painter);
646 }
647
648 if (!m_dropIndicator.isEmpty()) {
649 const QRectF r = m_dropIndicator.toRect();
650
651 QColor color = palette().brush(QPalette::Normal, QPalette::Highlight).color();
652 painter->setPen(color);
653
654 // TODO: The following implementation works only for a vertical scroll-orientation
655 // and assumes a height of the m_draggingInsertIndicator of 1.
656 Q_ASSERT(r.height() == 1);
657 painter->drawLine(r.left() + 1, r.top(), r.right() - 1, r.top());
658
659 color.setAlpha(128);
660 painter->setPen(color);
661 painter->drawRect(r.left(), r.top() - 1, r.width() - 1, 2);
662 }
663 }
664
665 KItemListViewLayouter* KItemListView::layouter() const
666 {
667 return m_layouter;
668 }
669
670 void KItemListView::setItemSize(const QSizeF& size)
671 {
672 const QSizeF previousSize = m_itemSize;
673 if (size == previousSize) {
674 return;
675 }
676
677 // Skip animations when the number of rows or columns
678 // are changed in the grid layout. Although the animation
679 // engine can handle this usecase, it looks obtrusive.
680 const bool animate = !changesItemGridLayout(m_layouter->size(),
681 size,
682 m_layouter->itemMargin());
683
684 const bool alternateBackgroundsChanged = (m_visibleRoles.count() > 1) &&
685 (( m_itemSize.isEmpty() && !size.isEmpty()) ||
686 (!m_itemSize.isEmpty() && size.isEmpty()));
687
688 m_itemSize = size;
689
690 if (alternateBackgroundsChanged) {
691 // For an empty item size alternate backgrounds are drawn if more than
692 // one role is shown. Assure that the backgrounds for visible items are
693 // updated when changing the size in this context.
694 updateAlternateBackgrounds();
695 }
696
697 if (size.isEmpty()) {
698 if (m_headerWidget->automaticColumnResizing()) {
699 updatePreferredColumnWidths();
700 } else {
701 // Only apply the changed height and respect the header widths
702 // set by the user
703 const qreal currentWidth = m_layouter->itemSize().width();
704 const QSizeF newSize(currentWidth, size.height());
705 m_layouter->setItemSize(newSize);
706 }
707 } else {
708 m_layouter->setItemSize(size);
709 }
710
711 m_sizeHintResolver->clearCache();
712 doLayout(animate ? Animation : NoAnimation);
713 onItemSizeChanged(size, previousSize);
714 }
715
716 void KItemListView::setStyleOption(const KItemListStyleOption& option)
717 {
718 const KItemListStyleOption previousOption = m_styleOption;
719 m_styleOption = option;
720
721 bool animate = true;
722 const QSizeF margin(option.horizontalMargin, option.verticalMargin);
723 if (margin != m_layouter->itemMargin()) {
724 // Skip animations when the number of rows or columns
725 // are changed in the grid layout. Although the animation
726 // engine can handle this usecase, it looks obtrusive.
727 animate = !changesItemGridLayout(m_layouter->size(),
728 m_layouter->itemSize(),
729 margin);
730 m_layouter->setItemMargin(margin);
731 }
732
733 if (m_grouped) {
734 updateGroupHeaderHeight();
735 }
736
737 if (animate && previousOption.maxTextSize != option.maxTextSize) {
738 // Animating a change of the maximum text size just results in expensive
739 // temporary eliding and clipping operations and does not look good visually.
740 animate = false;
741 }
742
743 QHashIterator<int, KItemListWidget*> it(m_visibleItems);
744 while (it.hasNext()) {
745 it.next();
746 it.value()->setStyleOption(option);
747 }
748
749 m_sizeHintResolver->clearCache();
750 m_layouter->markAsDirty();
751 doLayout(animate ? Animation : NoAnimation);
752
753 if (m_itemSize.isEmpty()) {
754 updatePreferredColumnWidths();
755 }
756
757 onStyleOptionChanged(option, previousOption);
758 }
759
760 void KItemListView::setScrollOrientation(Qt::Orientation orientation)
761 {
762 const Qt::Orientation previousOrientation = m_layouter->scrollOrientation();
763 if (orientation == previousOrientation) {
764 return;
765 }
766
767 m_layouter->setScrollOrientation(orientation);
768 m_animation->setScrollOrientation(orientation);
769 m_sizeHintResolver->clearCache();
770
771 if (m_grouped) {
772 QMutableHashIterator<KItemListWidget*, KItemListGroupHeader*> it (m_visibleGroups);
773 while (it.hasNext()) {
774 it.next();
775 it.value()->setScrollOrientation(orientation);
776 }
777 updateGroupHeaderHeight();
778
779 }
780
781 doLayout(NoAnimation);
782
783 onScrollOrientationChanged(orientation, previousOrientation);
784 emit scrollOrientationChanged(orientation, previousOrientation);
785 }
786
787 Qt::Orientation KItemListView::scrollOrientation() const
788 {
789 return m_layouter->scrollOrientation();
790 }
791
792 KItemListWidgetCreatorBase* KItemListView::defaultWidgetCreator() const
793 {
794 return 0;
795 }
796
797 KItemListGroupHeaderCreatorBase* KItemListView::defaultGroupHeaderCreator() const
798 {
799 return 0;
800 }
801
802 void KItemListView::initializeItemListWidget(KItemListWidget* item)
803 {
804 Q_UNUSED(item);
805 }
806
807 bool KItemListView::itemSizeHintUpdateRequired(const QSet<QByteArray>& changedRoles) const
808 {
809 Q_UNUSED(changedRoles);
810 return true;
811 }
812
813 void KItemListView::onControllerChanged(KItemListController* current, KItemListController* previous)
814 {
815 Q_UNUSED(current);
816 Q_UNUSED(previous);
817 }
818
819 void KItemListView::onModelChanged(KItemModelBase* current, KItemModelBase* previous)
820 {
821 Q_UNUSED(current);
822 Q_UNUSED(previous);
823 }
824
825 void KItemListView::onScrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous)
826 {
827 Q_UNUSED(current);
828 Q_UNUSED(previous);
829 }
830
831 void KItemListView::onItemSizeChanged(const QSizeF& current, const QSizeF& previous)
832 {
833 Q_UNUSED(current);
834 Q_UNUSED(previous);
835 }
836
837 void KItemListView::onScrollOffsetChanged(qreal current, qreal previous)
838 {
839 Q_UNUSED(current);
840 Q_UNUSED(previous);
841 }
842
843 void KItemListView::onVisibleRolesChanged(const QList<QByteArray>& current, const QList<QByteArray>& previous)
844 {
845 Q_UNUSED(current);
846 Q_UNUSED(previous);
847 }
848
849 void KItemListView::onStyleOptionChanged(const KItemListStyleOption& current, const KItemListStyleOption& previous)
850 {
851 Q_UNUSED(current);
852 Q_UNUSED(previous);
853 }
854
855 void KItemListView::onSupportsItemExpandingChanged(bool supportsExpanding)
856 {
857 Q_UNUSED(supportsExpanding);
858 }
859
860 void KItemListView::onTransactionBegin()
861 {
862 }
863
864 void KItemListView::onTransactionEnd()
865 {
866 }
867
868 bool KItemListView::event(QEvent* event)
869 {
870 // Forward all events to the controller and handle them there
871 if (!m_editingRole && m_controller && m_controller->processEvent(event, transform())) {
872 event->accept();
873 return true;
874 }
875 return QGraphicsWidget::event(event);
876 }
877
878 void KItemListView::mousePressEvent(QGraphicsSceneMouseEvent* event)
879 {
880 m_mousePos = transform().map(event->pos());
881 event->accept();
882 }
883
884 void KItemListView::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
885 {
886 QGraphicsWidget::mouseMoveEvent(event);
887
888 m_mousePos = transform().map(event->pos());
889 if (m_autoScrollTimer && !m_autoScrollTimer->isActive()) {
890 m_autoScrollTimer->start(InitialAutoScrollDelay);
891 }
892 }
893
894 void KItemListView::dragEnterEvent(QGraphicsSceneDragDropEvent* event)
895 {
896 event->setAccepted(true);
897 setAutoScroll(true);
898 }
899
900 void KItemListView::dragMoveEvent(QGraphicsSceneDragDropEvent *event)
901 {
902 QGraphicsWidget::dragMoveEvent(event);
903
904 m_mousePos = transform().map(event->pos());
905 if (m_autoScrollTimer && !m_autoScrollTimer->isActive()) {
906 m_autoScrollTimer->start(InitialAutoScrollDelay);
907 }
908 }
909
910 void KItemListView::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
911 {
912 QGraphicsWidget::dragLeaveEvent(event);
913 setAutoScroll(false);
914 }
915
916 void KItemListView::dropEvent(QGraphicsSceneDragDropEvent* event)
917 {
918 QGraphicsWidget::dropEvent(event);
919 setAutoScroll(false);
920 }
921
922 QList<KItemListWidget*> KItemListView::visibleItemListWidgets() const
923 {
924 return m_visibleItems.values();
925 }
926
927 void KItemListView::slotItemsInserted(const KItemRangeList& itemRanges)
928 {
929 if (m_itemSize.isEmpty()) {
930 updatePreferredColumnWidths(itemRanges);
931 }
932
933 const bool hasMultipleRanges = (itemRanges.count() > 1);
934 if (hasMultipleRanges) {
935 beginTransaction();
936 }
937
938 m_layouter->markAsDirty();
939
940 int previouslyInsertedCount = 0;
941 foreach (const KItemRange& range, itemRanges) {
942 // range.index is related to the model before anything has been inserted.
943 // As in each loop the current item-range gets inserted the index must
944 // be increased by the already previously inserted items.
945 const int index = range.index + previouslyInsertedCount;
946 const int count = range.count;
947 if (index < 0 || count <= 0) {
948 kWarning() << "Invalid item range (index:" << index << ", count:" << count << ")";
949 continue;
950 }
951 previouslyInsertedCount += count;
952
953 m_sizeHintResolver->itemsInserted(index, count);
954
955 // Determine which visible items must be moved
956 QList<int> itemsToMove;
957 QHashIterator<int, KItemListWidget*> it(m_visibleItems);
958 while (it.hasNext()) {
959 it.next();
960 const int visibleItemIndex = it.key();
961 if (visibleItemIndex >= index) {
962 itemsToMove.append(visibleItemIndex);
963 }
964 }
965
966 // Update the indexes of all KItemListWidget instances that are located
967 // after the inserted items. It is important to adjust the indexes in the order
968 // from the highest index to the lowest index to prevent overlaps when setting the new index.
969 qSort(itemsToMove);
970 for (int i = itemsToMove.count() - 1; i >= 0; --i) {
971 KItemListWidget* widget = m_visibleItems.value(itemsToMove[i]);
972 Q_ASSERT(widget);
973 const int newIndex = widget->index() + count;
974 if (hasMultipleRanges) {
975 setWidgetIndex(widget, newIndex);
976 } else {
977 // Try to animate the moving of the item
978 moveWidgetToIndex(widget, newIndex);
979 }
980 }
981
982 if (m_model->count() == count && m_activeTransactions == 0) {
983 // Check whether a scrollbar is required to show the inserted items. In this case
984 // the size of the layouter will be decreased before calling doLayout(): This prevents
985 // an unnecessary temporary animation due to the geometry change of the inserted scrollbar.
986 const bool verticalScrollOrientation = (scrollOrientation() == Qt::Vertical);
987 const bool decreaseLayouterSize = ( verticalScrollOrientation && maximumScrollOffset() > size().height()) ||
988 (!verticalScrollOrientation && maximumScrollOffset() > size().width());
989 if (decreaseLayouterSize) {
990 const int scrollBarExtent = style()->pixelMetric(QStyle::PM_ScrollBarExtent);
991 QSizeF layouterSize = m_layouter->size();
992 if (verticalScrollOrientation) {
993 layouterSize.rwidth() -= scrollBarExtent;
994 } else {
995 layouterSize.rheight() -= scrollBarExtent;
996 }
997 m_layouter->setSize(layouterSize);
998 }
999 }
1000
1001 if (!hasMultipleRanges) {
1002 doLayout(animateChangedItemCount(count) ? Animation : NoAnimation, index, count);
1003 updateSiblingsInformation();
1004 }
1005 }
1006
1007 if (m_controller) {
1008 m_controller->selectionManager()->itemsInserted(itemRanges);
1009 }
1010
1011 if (hasMultipleRanges) {
1012 #ifndef QT_NO_DEBUG
1013 // Important: Don't read any m_layouter-property inside the for-loop in case if
1014 // multiple ranges are given! m_layouter accesses m_sizeHintResolver which is
1015 // updated in each loop-cycle and has only a consistent state after the loop.
1016 Q_ASSERT(m_layouter->isDirty());
1017 #endif
1018 m_endTransactionAnimationHint = NoAnimation;
1019 endTransaction();
1020
1021 updateSiblingsInformation();
1022 }
1023
1024 if (m_grouped && (hasMultipleRanges || itemRanges.first().count < m_model->count())) {
1025 // In case if items of the same group have been inserted before an item that
1026 // currently represents the first item of the group, the group header of
1027 // this item must be removed.
1028 updateVisibleGroupHeaders();
1029 }
1030
1031 if (useAlternateBackgrounds()) {
1032 updateAlternateBackgrounds();
1033 }
1034 }
1035
1036 void KItemListView::slotItemsRemoved(const KItemRangeList& itemRanges)
1037 {
1038 if (m_itemSize.isEmpty()) {
1039 // Don't pass the item-range: The preferred column-widths of
1040 // all items must be adjusted when removing items.
1041 updatePreferredColumnWidths();
1042 }
1043
1044 const bool hasMultipleRanges = (itemRanges.count() > 1);
1045 if (hasMultipleRanges) {
1046 beginTransaction();
1047 }
1048
1049 m_layouter->markAsDirty();
1050
1051 int removedItemsCount = 0;
1052 for (int i = 0; i < itemRanges.count(); ++i) {
1053 removedItemsCount += itemRanges[i].count;
1054 }
1055
1056 for (int i = itemRanges.count() - 1; i >= 0; --i) {
1057 const KItemRange& range = itemRanges[i];
1058 const int index = range.index;
1059 const int count = range.count;
1060 if (index < 0 || count <= 0) {
1061 kWarning() << "Invalid item range (index:" << index << ", count:" << count << ")";
1062 continue;
1063 }
1064
1065 m_sizeHintResolver->itemsRemoved(index, count);
1066
1067 const int firstRemovedIndex = index;
1068 const int lastRemovedIndex = index + count - 1;
1069 const int lastIndex = m_model->count() - 1 + removedItemsCount;
1070 removedItemsCount -= count;
1071
1072 // Remove all KItemListWidget instances that got deleted
1073 for (int i = firstRemovedIndex; i <= lastRemovedIndex; ++i) {
1074 KItemListWidget* widget = m_visibleItems.value(i);
1075 if (!widget) {
1076 continue;
1077 }
1078
1079 m_animation->stop(widget);
1080 // Stopping the animation might lead to recycling the widget if
1081 // it is invisible (see slotAnimationFinished()).
1082 // Check again whether it is still visible:
1083 if (!m_visibleItems.contains(i)) {
1084 continue;
1085 }
1086
1087 if (m_model->count() == 0 || hasMultipleRanges || !animateChangedItemCount(count)) {
1088 // Remove the widget without animation
1089 recycleWidget(widget);
1090 } else {
1091 // Animate the removing of the items. Special case: When removing an item there
1092 // is no valid model index available anymore. For the
1093 // remove-animation the item gets removed from m_visibleItems but the widget
1094 // will stay alive until the animation has been finished and will
1095 // be recycled (deleted) in KItemListView::slotAnimationFinished().
1096 m_visibleItems.remove(i);
1097 widget->setIndex(-1);
1098 m_animation->start(widget, KItemListViewAnimation::DeleteAnimation);
1099 }
1100 }
1101
1102 // Update the indexes of all KItemListWidget instances that are located
1103 // after the deleted items
1104 for (int i = lastRemovedIndex + 1; i <= lastIndex; ++i) {
1105 KItemListWidget* widget = m_visibleItems.value(i);
1106 if (widget) {
1107 const int newIndex = i - count;
1108 if (hasMultipleRanges) {
1109 setWidgetIndex(widget, newIndex);
1110 } else {
1111 // Try to animate the moving of the item
1112 moveWidgetToIndex(widget, newIndex);
1113 }
1114 }
1115 }
1116
1117 if (!hasMultipleRanges) {
1118 // The decrease-layout-size optimization in KItemListView::slotItemsInserted()
1119 // assumes an updated geometry. If items are removed during an active transaction,
1120 // the transaction will be temporary deactivated so that doLayout() triggers a
1121 // geometry update if necessary.
1122 const int activeTransactions = m_activeTransactions;
1123 m_activeTransactions = 0;
1124 doLayout(animateChangedItemCount(count) ? Animation : NoAnimation, index, -count);
1125 m_activeTransactions = activeTransactions;
1126 updateSiblingsInformation();
1127 }
1128 }
1129
1130 if (m_controller) {
1131 m_controller->selectionManager()->itemsRemoved(itemRanges);
1132 }
1133
1134 if (hasMultipleRanges) {
1135 #ifndef QT_NO_DEBUG
1136 // Important: Don't read any m_layouter-property inside the for-loop in case if
1137 // multiple ranges are given! m_layouter accesses m_sizeHintResolver which is
1138 // updated in each loop-cycle and has only a consistent state after the loop.
1139 Q_ASSERT(m_layouter->isDirty());
1140 #endif
1141 m_endTransactionAnimationHint = NoAnimation;
1142 endTransaction();
1143 updateSiblingsInformation();
1144 }
1145
1146 if (m_grouped && (hasMultipleRanges || m_model->count() > 0)) {
1147 // In case if the first item of a group has been removed, the group header
1148 // must be applied to the next visible item.
1149 updateVisibleGroupHeaders();
1150 }
1151
1152 if (useAlternateBackgrounds()) {
1153 updateAlternateBackgrounds();
1154 }
1155 }
1156
1157 void KItemListView::slotItemsMoved(const KItemRange& itemRange, const QList<int>& movedToIndexes)
1158 {
1159 m_sizeHintResolver->itemsMoved(itemRange.index, itemRange.count);
1160 m_layouter->markAsDirty();
1161
1162 if (m_controller) {
1163 m_controller->selectionManager()->itemsMoved(itemRange, movedToIndexes);
1164 }
1165
1166 const int firstVisibleMovedIndex = qMax(firstVisibleIndex(), itemRange.index);
1167 const int lastVisibleMovedIndex = qMin(lastVisibleIndex(), itemRange.index + itemRange.count - 1);
1168
1169 for (int index = firstVisibleMovedIndex; index <= lastVisibleMovedIndex; ++index) {
1170 KItemListWidget* widget = m_visibleItems.value(index);
1171 if (widget) {
1172 updateWidgetProperties(widget, index);
1173 initializeItemListWidget(widget);
1174 }
1175 }
1176
1177 doLayout(NoAnimation);
1178 updateSiblingsInformation();
1179 }
1180
1181 void KItemListView::slotItemsChanged(const KItemRangeList& itemRanges,
1182 const QSet<QByteArray>& roles)
1183 {
1184 const bool updateSizeHints = itemSizeHintUpdateRequired(roles);
1185 if (updateSizeHints && m_itemSize.isEmpty()) {
1186 updatePreferredColumnWidths(itemRanges);
1187 }
1188
1189 foreach (const KItemRange& itemRange, itemRanges) {
1190 const int index = itemRange.index;
1191 const int count = itemRange.count;
1192
1193 if (updateSizeHints) {
1194 m_sizeHintResolver->itemsChanged(index, count, roles);
1195 m_layouter->markAsDirty();
1196
1197 if (!m_layoutTimer->isActive()) {
1198 m_layoutTimer->start();
1199 }
1200 }
1201
1202 // Apply the changed roles to the visible item-widgets
1203 const int lastIndex = index + count - 1;
1204 for (int i = index; i <= lastIndex; ++i) {
1205 KItemListWidget* widget = m_visibleItems.value(i);
1206 if (widget) {
1207 widget->setData(m_model->data(i), roles);
1208 }
1209 }
1210
1211 if (m_grouped && roles.contains(m_model->sortRole())) {
1212 // The sort-role has been changed which might result
1213 // in modified group headers
1214 updateVisibleGroupHeaders();
1215 doLayout(NoAnimation);
1216 }
1217 }
1218 }
1219
1220 void KItemListView::slotGroupedSortingChanged(bool current)
1221 {
1222 m_grouped = current;
1223 m_layouter->markAsDirty();
1224
1225 if (m_grouped) {
1226 updateGroupHeaderHeight();
1227 } else {
1228 // Clear all visible headers
1229 QMutableHashIterator<KItemListWidget*, KItemListGroupHeader*> it (m_visibleGroups);
1230 while (it.hasNext()) {
1231 it.next();
1232 recycleGroupHeaderForWidget(it.key());
1233 }
1234 Q_ASSERT(m_visibleGroups.isEmpty());
1235 }
1236
1237 if (useAlternateBackgrounds()) {
1238 // Changing the group mode requires to update the alternate backgrounds
1239 // as with the enabled group mode the altering is done on base of the first
1240 // group item.
1241 updateAlternateBackgrounds();
1242 }
1243 updateSiblingsInformation();
1244 doLayout(NoAnimation);
1245 }
1246
1247 void KItemListView::slotSortOrderChanged(Qt::SortOrder current, Qt::SortOrder previous)
1248 {
1249 Q_UNUSED(current);
1250 Q_UNUSED(previous);
1251 if (m_grouped) {
1252 updateVisibleGroupHeaders();
1253 doLayout(NoAnimation);
1254 }
1255 }
1256
1257 void KItemListView::slotSortRoleChanged(const QByteArray& current, const QByteArray& previous)
1258 {
1259 Q_UNUSED(current);
1260 Q_UNUSED(previous);
1261 if (m_grouped) {
1262 updateVisibleGroupHeaders();
1263 doLayout(NoAnimation);
1264 }
1265 }
1266
1267 void KItemListView::slotCurrentChanged(int current, int previous)
1268 {
1269 Q_UNUSED(previous);
1270
1271 KItemListWidget* previousWidget = m_visibleItems.value(previous, 0);
1272 if (previousWidget) {
1273 previousWidget->setCurrent(false);
1274 }
1275
1276 KItemListWidget* currentWidget = m_visibleItems.value(current, 0);
1277 if (currentWidget) {
1278 currentWidget->setCurrent(true);
1279 }
1280 QAccessible::updateAccessibility(this, current+1, QAccessible::Focus);
1281 }
1282
1283 void KItemListView::slotSelectionChanged(const QSet<int>& current, const QSet<int>& previous)
1284 {
1285 Q_UNUSED(previous);
1286
1287 QHashIterator<int, KItemListWidget*> it(m_visibleItems);
1288 while (it.hasNext()) {
1289 it.next();
1290 const int index = it.key();
1291 KItemListWidget* widget = it.value();
1292 widget->setSelected(current.contains(index));
1293 }
1294 }
1295
1296 void KItemListView::slotAnimationFinished(QGraphicsWidget* widget,
1297 KItemListViewAnimation::AnimationType type)
1298 {
1299 KItemListWidget* itemListWidget = qobject_cast<KItemListWidget*>(widget);
1300 Q_ASSERT(itemListWidget);
1301
1302 switch (type) {
1303 case KItemListViewAnimation::DeleteAnimation: {
1304 // As we recycle the widget in this case it is important to assure that no
1305 // other animation has been started. This is a convention in KItemListView and
1306 // not a requirement defined by KItemListViewAnimation.
1307 Q_ASSERT(!m_animation->isStarted(itemListWidget));
1308
1309 // All KItemListWidgets that are animated by the DeleteAnimation are not maintained
1310 // by m_visibleWidgets and must be deleted manually after the animation has
1311 // been finished.
1312 recycleGroupHeaderForWidget(itemListWidget);
1313 widgetCreator()->recycle(itemListWidget);
1314 break;
1315 }
1316
1317 case KItemListViewAnimation::CreateAnimation:
1318 case KItemListViewAnimation::MovingAnimation:
1319 case KItemListViewAnimation::ResizeAnimation: {
1320 const int index = itemListWidget->index();
1321 const bool invisible = (index < m_layouter->firstVisibleIndex()) ||
1322 (index > m_layouter->lastVisibleIndex());
1323 if (invisible && !m_animation->isStarted(itemListWidget)) {
1324 recycleWidget(itemListWidget);
1325 }
1326 break;
1327 }
1328
1329 default: break;
1330 }
1331 }
1332
1333 void KItemListView::slotLayoutTimerFinished()
1334 {
1335 m_layouter->setSize(geometry().size());
1336 doLayout(Animation);
1337 }
1338
1339 void KItemListView::slotRubberBandPosChanged()
1340 {
1341 update();
1342 }
1343
1344 void KItemListView::slotRubberBandActivationChanged(bool active)
1345 {
1346 if (active) {
1347 connect(m_rubberBand, SIGNAL(startPositionChanged(QPointF,QPointF)), this, SLOT(slotRubberBandPosChanged()));
1348 connect(m_rubberBand, SIGNAL(endPositionChanged(QPointF,QPointF)), this, SLOT(slotRubberBandPosChanged()));
1349 m_skipAutoScrollForRubberBand = true;
1350 } else {
1351 disconnect(m_rubberBand, SIGNAL(startPositionChanged(QPointF,QPointF)), this, SLOT(slotRubberBandPosChanged()));
1352 disconnect(m_rubberBand, SIGNAL(endPositionChanged(QPointF,QPointF)), this, SLOT(slotRubberBandPosChanged()));
1353 m_skipAutoScrollForRubberBand = false;
1354 }
1355
1356 update();
1357 }
1358
1359 void KItemListView::slotHeaderColumnWidthChanged(const QByteArray& role,
1360 qreal currentWidth,
1361 qreal previousWidth)
1362 {
1363 Q_UNUSED(role);
1364 Q_UNUSED(currentWidth);
1365 Q_UNUSED(previousWidth);
1366
1367 m_headerWidget->setAutomaticColumnResizing(false);
1368 applyColumnWidthsFromHeader();
1369 doLayout(NoAnimation);
1370 }
1371
1372 void KItemListView::slotHeaderColumnMoved(const QByteArray& role,
1373 int currentIndex,
1374 int previousIndex)
1375 {
1376 Q_ASSERT(m_visibleRoles[previousIndex] == role);
1377
1378 const QList<QByteArray> previous = m_visibleRoles;
1379
1380 QList<QByteArray> current = m_visibleRoles;
1381 current.removeAt(previousIndex);
1382 current.insert(currentIndex, role);
1383
1384 setVisibleRoles(current);
1385
1386 emit visibleRolesChanged(current, previous);
1387 }
1388
1389 void KItemListView::triggerAutoScrolling()
1390 {
1391 if (!m_autoScrollTimer) {
1392 return;
1393 }
1394
1395 int pos = 0;
1396 int visibleSize = 0;
1397 if (scrollOrientation() == Qt::Vertical) {
1398 pos = m_mousePos.y();
1399 visibleSize = size().height();
1400 } else {
1401 pos = m_mousePos.x();
1402 visibleSize = size().width();
1403 }
1404
1405 if (m_autoScrollTimer->interval() == InitialAutoScrollDelay) {
1406 m_autoScrollIncrement = 0;
1407 }
1408
1409 m_autoScrollIncrement = calculateAutoScrollingIncrement(pos, visibleSize, m_autoScrollIncrement);
1410 if (m_autoScrollIncrement == 0) {
1411 // The mouse position is not above an autoscroll margin (the autoscroll timer
1412 // will be restarted in mouseMoveEvent())
1413 m_autoScrollTimer->stop();
1414 return;
1415 }
1416
1417 if (m_rubberBand->isActive() && m_skipAutoScrollForRubberBand) {
1418 // If a rubberband selection is ongoing the autoscrolling may only get triggered
1419 // if the direction of the rubberband is similar to the autoscroll direction. This
1420 // prevents that starting to create a rubberband within the autoscroll margins starts
1421 // an autoscrolling.
1422
1423 const qreal minDiff = 4; // Ignore any autoscrolling if the rubberband is very small
1424 const qreal diff = (scrollOrientation() == Qt::Vertical)
1425 ? m_rubberBand->endPosition().y() - m_rubberBand->startPosition().y()
1426 : m_rubberBand->endPosition().x() - m_rubberBand->startPosition().x();
1427 if (qAbs(diff) < minDiff || (m_autoScrollIncrement < 0 && diff > 0) || (m_autoScrollIncrement > 0 && diff < 0)) {
1428 // The rubberband direction is different from the scroll direction (e.g. the rubberband has
1429 // been moved up although the autoscroll direction might be down)
1430 m_autoScrollTimer->stop();
1431 return;
1432 }
1433 }
1434
1435 // As soon as the autoscrolling has been triggered at least once despite having an active rubberband,
1436 // the autoscrolling may not get skipped anymore until a new rubberband is created
1437 m_skipAutoScrollForRubberBand = false;
1438
1439 const qreal maxVisibleOffset = qMax(qreal(0), maximumScrollOffset() - visibleSize);
1440 const qreal newScrollOffset = qMin(scrollOffset() + m_autoScrollIncrement, maxVisibleOffset);
1441 setScrollOffset(newScrollOffset);
1442
1443 // Trigger the autoscroll timer which will periodically call
1444 // triggerAutoScrolling()
1445 m_autoScrollTimer->start(RepeatingAutoScrollDelay);
1446 }
1447
1448 void KItemListView::slotGeometryOfGroupHeaderParentChanged()
1449 {
1450 KItemListWidget* widget = qobject_cast<KItemListWidget*>(sender());
1451 Q_ASSERT(widget);
1452 KItemListGroupHeader* groupHeader = m_visibleGroups.value(widget);
1453 Q_ASSERT(groupHeader);
1454 updateGroupHeaderLayout(widget);
1455 }
1456
1457 void KItemListView::slotRoleEditingCanceled(int index, const QByteArray& role, const QVariant& value)
1458 {
1459 emit roleEditingCanceled(index, role, value);
1460 m_editingRole = false;
1461 }
1462
1463 void KItemListView::slotRoleEditingFinished(int index, const QByteArray& role, const QVariant& value)
1464 {
1465 emit roleEditingFinished(index, role, value);
1466 m_editingRole = false;
1467 }
1468
1469 void KItemListView::setController(KItemListController* controller)
1470 {
1471 if (m_controller != controller) {
1472 KItemListController* previous = m_controller;
1473 if (previous) {
1474 KItemListSelectionManager* selectionManager = previous->selectionManager();
1475 disconnect(selectionManager, SIGNAL(currentChanged(int,int)), this, SLOT(slotCurrentChanged(int,int)));
1476 disconnect(selectionManager, SIGNAL(selectionChanged(QSet<int>,QSet<int>)), this, SLOT(slotSelectionChanged(QSet<int>,QSet<int>)));
1477 }
1478
1479 m_controller = controller;
1480
1481 if (controller) {
1482 KItemListSelectionManager* selectionManager = controller->selectionManager();
1483 connect(selectionManager, SIGNAL(currentChanged(int,int)), this, SLOT(slotCurrentChanged(int,int)));
1484 connect(selectionManager, SIGNAL(selectionChanged(QSet<int>,QSet<int>)), this, SLOT(slotSelectionChanged(QSet<int>,QSet<int>)));
1485 }
1486
1487 onControllerChanged(controller, previous);
1488 }
1489 }
1490
1491 void KItemListView::setModel(KItemModelBase* model)
1492 {
1493 if (m_model == model) {
1494 return;
1495 }
1496
1497 KItemModelBase* previous = m_model;
1498
1499 if (m_model) {
1500 disconnect(m_model, SIGNAL(itemsChanged(KItemRangeList,QSet<QByteArray>)),
1501 this, SLOT(slotItemsChanged(KItemRangeList,QSet<QByteArray>)));
1502 disconnect(m_model, SIGNAL(itemsInserted(KItemRangeList)),
1503 this, SLOT(slotItemsInserted(KItemRangeList)));
1504 disconnect(m_model, SIGNAL(itemsRemoved(KItemRangeList)),
1505 this, SLOT(slotItemsRemoved(KItemRangeList)));
1506 disconnect(m_model, SIGNAL(itemsMoved(KItemRange,QList<int>)),
1507 this, SLOT(slotItemsMoved(KItemRange,QList<int>)));
1508 disconnect(m_model, SIGNAL(groupedSortingChanged(bool)),
1509 this, SLOT(slotGroupedSortingChanged(bool)));
1510 disconnect(m_model, SIGNAL(sortOrderChanged(Qt::SortOrder,Qt::SortOrder)),
1511 this, SLOT(slotSortOrderChanged(Qt::SortOrder,Qt::SortOrder)));
1512 disconnect(m_model, SIGNAL(sortRoleChanged(QByteArray,QByteArray)),
1513 this, SLOT(slotSortRoleChanged(QByteArray,QByteArray)));
1514 }
1515
1516 m_sizeHintResolver->clearCache();
1517
1518 m_model = model;
1519 m_layouter->setModel(model);
1520 m_grouped = model->groupedSorting();
1521
1522 if (m_model) {
1523 connect(m_model, SIGNAL(itemsChanged(KItemRangeList,QSet<QByteArray>)),
1524 this, SLOT(slotItemsChanged(KItemRangeList,QSet<QByteArray>)));
1525 connect(m_model, SIGNAL(itemsInserted(KItemRangeList)),
1526 this, SLOT(slotItemsInserted(KItemRangeList)));
1527 connect(m_model, SIGNAL(itemsRemoved(KItemRangeList)),
1528 this, SLOT(slotItemsRemoved(KItemRangeList)));
1529 connect(m_model, SIGNAL(itemsMoved(KItemRange,QList<int>)),
1530 this, SLOT(slotItemsMoved(KItemRange,QList<int>)));
1531 connect(m_model, SIGNAL(groupedSortingChanged(bool)),
1532 this, SLOT(slotGroupedSortingChanged(bool)));
1533 connect(m_model, SIGNAL(sortOrderChanged(Qt::SortOrder,Qt::SortOrder)),
1534 this, SLOT(slotSortOrderChanged(Qt::SortOrder,Qt::SortOrder)));
1535 connect(m_model, SIGNAL(sortRoleChanged(QByteArray,QByteArray)),
1536 this, SLOT(slotSortRoleChanged(QByteArray,QByteArray)));
1537
1538 const int itemCount = m_model->count();
1539 if (itemCount > 0) {
1540 m_sizeHintResolver->itemsInserted(0, itemCount);
1541 slotItemsInserted(KItemRangeList() << KItemRange(0, itemCount));
1542 }
1543 }
1544
1545 onModelChanged(model, previous);
1546 }
1547
1548 KItemListRubberBand* KItemListView::rubberBand() const
1549 {
1550 return m_rubberBand;
1551 }
1552
1553 void KItemListView::doLayout(LayoutAnimationHint hint, int changedIndex, int changedCount)
1554 {
1555 if (m_layoutTimer->isActive()) {
1556 m_layoutTimer->stop();
1557 }
1558
1559 if (m_activeTransactions > 0) {
1560 if (hint == NoAnimation) {
1561 // As soon as at least one property change should be done without animation,
1562 // the whole transaction will be marked as not animated.
1563 m_endTransactionAnimationHint = NoAnimation;
1564 }
1565 return;
1566 }
1567
1568 if (!m_model || m_model->count() < 0) {
1569 return;
1570 }
1571
1572 int firstVisibleIndex = m_layouter->firstVisibleIndex();
1573 if (firstVisibleIndex < 0) {
1574 emitOffsetChanges();
1575 return;
1576 }
1577
1578 // Do a sanity check of the scroll-offset property: When properties of the itemlist-view have been changed
1579 // it might be possible that the maximum offset got changed too. Assure that the full visible range
1580 // is still shown if the maximum offset got decreased.
1581 const qreal visibleOffsetRange = (scrollOrientation() == Qt::Horizontal) ? size().width() : size().height();
1582 const qreal maxOffsetToShowFullRange = maximumScrollOffset() - visibleOffsetRange;
1583 if (scrollOffset() > maxOffsetToShowFullRange) {
1584 m_layouter->setScrollOffset(qMax(qreal(0), maxOffsetToShowFullRange));
1585 firstVisibleIndex = m_layouter->firstVisibleIndex();
1586 }
1587
1588 const int lastVisibleIndex = m_layouter->lastVisibleIndex();
1589
1590 int firstSibblingIndex = -1;
1591 int lastSibblingIndex = -1;
1592 const bool supportsExpanding = supportsItemExpanding();
1593
1594 QList<int> reusableItems = recycleInvisibleItems(firstVisibleIndex, lastVisibleIndex, hint);
1595
1596 // Assure that for each visible item a KItemListWidget is available. KItemListWidget
1597 // instances from invisible items are reused. If no reusable items are
1598 // found then new KItemListWidget instances get created.
1599 const bool animate = (hint == Animation);
1600 for (int i = firstVisibleIndex; i <= lastVisibleIndex; ++i) {
1601 bool applyNewPos = true;
1602 bool wasHidden = false;
1603
1604 const QRectF itemBounds = m_layouter->itemRect(i);
1605 const QPointF newPos = itemBounds.topLeft();
1606 KItemListWidget* widget = m_visibleItems.value(i);
1607 if (!widget) {
1608 wasHidden = true;
1609 if (!reusableItems.isEmpty()) {
1610 // Reuse a KItemListWidget instance from an invisible item
1611 const int oldIndex = reusableItems.takeLast();
1612 widget = m_visibleItems.value(oldIndex);
1613 setWidgetIndex(widget, i);
1614 updateWidgetProperties(widget, i);
1615 initializeItemListWidget(widget);
1616 } else {
1617 // No reusable KItemListWidget instance is available, create a new one
1618 widget = createWidget(i);
1619 }
1620 widget->resize(itemBounds.size());
1621
1622 if (animate && changedCount < 0) {
1623 // Items have been deleted, move the created item to the
1624 // imaginary old position. They will get animated to the new position
1625 // later.
1626 const QRectF itemRect = m_layouter->itemRect(i - changedCount);
1627 if (itemRect.isEmpty()) {
1628 const QPointF invisibleOldPos = (scrollOrientation() == Qt::Vertical)
1629 ? QPointF(0, size().height()) : QPointF(size().width(), 0);
1630 widget->setPos(invisibleOldPos);
1631 } else {
1632 widget->setPos(itemRect.topLeft());
1633 }
1634 applyNewPos = false;
1635 }
1636
1637 if (supportsExpanding && changedCount == 0) {
1638 if (firstSibblingIndex < 0) {
1639 firstSibblingIndex = i;
1640 }
1641 lastSibblingIndex = i;
1642 }
1643 }
1644
1645 if (animate) {
1646 if (m_animation->isStarted(widget, KItemListViewAnimation::MovingAnimation)) {
1647 m_animation->start(widget, KItemListViewAnimation::MovingAnimation, newPos);
1648 applyNewPos = false;
1649 }
1650
1651 const bool itemsRemoved = (changedCount < 0);
1652 const bool itemsInserted = (changedCount > 0);
1653 if (itemsRemoved && (i >= changedIndex + changedCount + 1)) {
1654 // The item is located after the removed items. Animate the moving of the position.
1655 applyNewPos = !moveWidget(widget, newPos);
1656 } else if (itemsInserted && i >= changedIndex) {
1657 // The item is located after the first inserted item
1658 if (i <= changedIndex + changedCount - 1) {
1659 // The item is an inserted item. Animate the appearing of the item.
1660 // For performance reasons no animation is done when changedCount is equal
1661 // to all available items.
1662 if (changedCount < m_model->count()) {
1663 m_animation->start(widget, KItemListViewAnimation::CreateAnimation);
1664 }
1665 } else if (!m_animation->isStarted(widget, KItemListViewAnimation::CreateAnimation)) {
1666 // The item was already there before, so animate the moving of the position.
1667 // No moving animation is done if the item is animated by a create animation: This
1668 // prevents a "move animation mess" when inserting several ranges in parallel.
1669 applyNewPos = !moveWidget(widget, newPos);
1670 }
1671 } else if (!itemsRemoved && !itemsInserted && !wasHidden) {
1672 // The size of the view might have been changed. Animate the moving of the position.
1673 applyNewPos = !moveWidget(widget, newPos);
1674 }
1675 } else {
1676 m_animation->stop(widget);
1677 }
1678
1679 if (applyNewPos) {
1680 widget->setPos(newPos);
1681 }
1682
1683 Q_ASSERT(widget->index() == i);
1684 widget->setVisible(true);
1685
1686 if (widget->size() != itemBounds.size()) {
1687 // Resize the widget for the item to the changed size.
1688 if (animate) {
1689 // If a dynamic item size is used then no animation is done in the direction
1690 // of the dynamic size.
1691 if (m_itemSize.width() <= 0) {
1692 // The width is dynamic, apply the new width without animation.
1693 widget->resize(itemBounds.width(), widget->size().height());
1694 } else if (m_itemSize.height() <= 0) {
1695 // The height is dynamic, apply the new height without animation.
1696 widget->resize(widget->size().width(), itemBounds.height());
1697 }
1698 m_animation->start(widget, KItemListViewAnimation::ResizeAnimation, itemBounds.size());
1699 } else {
1700 widget->resize(itemBounds.size());
1701 }
1702 }
1703
1704 // Updating the cell-information must be done as last step: The decision whether the
1705 // moving-animation should be started at all is based on the previous cell-information.
1706 const Cell cell(m_layouter->itemColumn(i), m_layouter->itemRow(i));
1707 m_visibleCells.insert(i, cell);
1708 }
1709
1710 // Delete invisible KItemListWidget instances that have not been reused
1711 foreach (int index, reusableItems) {
1712 recycleWidget(m_visibleItems.value(index));
1713 }
1714
1715 if (supportsExpanding && firstSibblingIndex >= 0) {
1716 Q_ASSERT(lastSibblingIndex >= 0);
1717 updateSiblingsInformation(firstSibblingIndex, lastSibblingIndex);
1718 }
1719
1720 if (m_grouped) {
1721 // Update the layout of all visible group headers
1722 QHashIterator<KItemListWidget*, KItemListGroupHeader*> it(m_visibleGroups);
1723 while (it.hasNext()) {
1724 it.next();
1725 updateGroupHeaderLayout(it.key());
1726 }
1727 }
1728
1729 emitOffsetChanges();
1730 }
1731
1732 QList<int> KItemListView::recycleInvisibleItems(int firstVisibleIndex,
1733 int lastVisibleIndex,
1734 LayoutAnimationHint hint)
1735 {
1736 // Determine all items that are completely invisible and might be
1737 // reused for items that just got (at least partly) visible. If the
1738 // animation hint is set to 'Animation' items that do e.g. an animated
1739 // moving of their position are not marked as invisible: This assures
1740 // that a scrolling inside the view can be done without breaking an animation.
1741
1742 QList<int> items;
1743
1744 QHashIterator<int, KItemListWidget*> it(m_visibleItems);
1745 while (it.hasNext()) {
1746 it.next();
1747
1748 KItemListWidget* widget = it.value();
1749 const int index = widget->index();
1750 const bool invisible = (index < firstVisibleIndex) || (index > lastVisibleIndex);
1751
1752 if (invisible) {
1753 if (m_animation->isStarted(widget)) {
1754 if (hint == NoAnimation) {
1755 // Stopping the animation will call KItemListView::slotAnimationFinished()
1756 // and the widget will be recycled if necessary there.
1757 m_animation->stop(widget);
1758 }
1759 } else {
1760 widget->setVisible(false);
1761 items.append(index);
1762
1763 if (m_grouped) {
1764 recycleGroupHeaderForWidget(widget);
1765 }
1766 }
1767 }
1768 }
1769
1770 return items;
1771 }
1772
1773 bool KItemListView::moveWidget(KItemListWidget* widget,const QPointF& newPos)
1774 {
1775 if (widget->pos() == newPos) {
1776 return false;
1777 }
1778
1779 bool startMovingAnim = false;
1780
1781 if (m_itemSize.isEmpty()) {
1782 // The items are not aligned in a grid but either as columns or rows.
1783 startMovingAnim = true;
1784 } else {
1785 // When having a grid the moving-animation should only be started, if it is done within
1786 // one row in the vertical scroll-orientation or one column in the horizontal scroll-orientation.
1787 // Otherwise instead of a moving-animation a create-animation on the new position will be used
1788 // instead. This is done to prevent overlapping (and confusing) moving-animations.
1789 const int index = widget->index();
1790 const Cell cell = m_visibleCells.value(index);
1791 if (cell.column >= 0 && cell.row >= 0) {
1792 if (scrollOrientation() == Qt::Vertical) {
1793 startMovingAnim = (cell.row == m_layouter->itemRow(index));
1794 } else {
1795 startMovingAnim = (cell.column == m_layouter->itemColumn(index));
1796 }
1797 }
1798 }
1799
1800 if (startMovingAnim) {
1801 m_animation->start(widget, KItemListViewAnimation::MovingAnimation, newPos);
1802 return true;
1803 }
1804
1805 m_animation->stop(widget);
1806 m_animation->start(widget, KItemListViewAnimation::CreateAnimation);
1807 return false;
1808 }
1809
1810 void KItemListView::emitOffsetChanges()
1811 {
1812 const qreal newScrollOffset = m_layouter->scrollOffset();
1813 if (m_oldScrollOffset != newScrollOffset) {
1814 emit scrollOffsetChanged(newScrollOffset, m_oldScrollOffset);
1815 m_oldScrollOffset = newScrollOffset;
1816 }
1817
1818 const qreal newMaximumScrollOffset = m_layouter->maximumScrollOffset();
1819 if (m_oldMaximumScrollOffset != newMaximumScrollOffset) {
1820 emit maximumScrollOffsetChanged(newMaximumScrollOffset, m_oldMaximumScrollOffset);
1821 m_oldMaximumScrollOffset = newMaximumScrollOffset;
1822 }
1823
1824 const qreal newItemOffset = m_layouter->itemOffset();
1825 if (m_oldItemOffset != newItemOffset) {
1826 emit itemOffsetChanged(newItemOffset, m_oldItemOffset);
1827 m_oldItemOffset = newItemOffset;
1828 }
1829
1830 const qreal newMaximumItemOffset = m_layouter->maximumItemOffset();
1831 if (m_oldMaximumItemOffset != newMaximumItemOffset) {
1832 emit maximumItemOffsetChanged(newMaximumItemOffset, m_oldMaximumItemOffset);
1833 m_oldMaximumItemOffset = newMaximumItemOffset;
1834 }
1835 }
1836
1837 KItemListWidget* KItemListView::createWidget(int index)
1838 {
1839 KItemListWidget* widget = widgetCreator()->create(this);
1840 widget->setFlag(QGraphicsItem::ItemStacksBehindParent);
1841
1842 m_visibleItems.insert(index, widget);
1843 m_visibleCells.insert(index, Cell());
1844 updateWidgetProperties(widget, index);
1845 initializeItemListWidget(widget);
1846 return widget;
1847 }
1848
1849 void KItemListView::recycleWidget(KItemListWidget* widget)
1850 {
1851 if (m_grouped) {
1852 recycleGroupHeaderForWidget(widget);
1853 }
1854
1855 const int index = widget->index();
1856 m_visibleItems.remove(index);
1857 m_visibleCells.remove(index);
1858
1859 widgetCreator()->recycle(widget);
1860 }
1861
1862 void KItemListView::setWidgetIndex(KItemListWidget* widget, int index)
1863 {
1864 const int oldIndex = widget->index();
1865 m_visibleItems.remove(oldIndex);
1866 m_visibleCells.remove(oldIndex);
1867
1868 m_visibleItems.insert(index, widget);
1869 m_visibleCells.insert(index, Cell());
1870
1871 widget->setIndex(index);
1872 }
1873
1874 void KItemListView::moveWidgetToIndex(KItemListWidget* widget, int index)
1875 {
1876 const int oldIndex = widget->index();
1877 const Cell oldCell = m_visibleCells.value(oldIndex);
1878
1879 setWidgetIndex(widget, index);
1880
1881 const Cell newCell(m_layouter->itemColumn(index), m_layouter->itemRow(index));
1882 const bool vertical = (scrollOrientation() == Qt::Vertical);
1883 const bool updateCell = (vertical && oldCell.row == newCell.row) ||
1884 (!vertical && oldCell.column == newCell.column);
1885 if (updateCell) {
1886 m_visibleCells.insert(index, newCell);
1887 }
1888 }
1889
1890 void KItemListView::setLayouterSize(const QSizeF& size, SizeType sizeType)
1891 {
1892 switch (sizeType) {
1893 case LayouterSize: m_layouter->setSize(size); break;
1894 case ItemSize: m_layouter->setItemSize(size); break;
1895 default: break;
1896 }
1897 }
1898
1899 void KItemListView::updateWidgetProperties(KItemListWidget* widget, int index)
1900 {
1901 widget->setVisibleRoles(m_visibleRoles);
1902 updateWidgetColumnWidths(widget);
1903 widget->setStyleOption(m_styleOption);
1904
1905 const KItemListSelectionManager* selectionManager = m_controller->selectionManager();
1906 widget->setCurrent(index == selectionManager->currentItem());
1907 widget->setSelected(selectionManager->isSelected(index));
1908 widget->setHovered(false);
1909 widget->setEnabledSelectionToggle(enabledSelectionToggles());
1910 widget->setIndex(index);
1911 widget->setData(m_model->data(index));
1912 widget->setSiblingsInformation(QBitArray());
1913 updateAlternateBackgroundForWidget(widget);
1914
1915 if (m_grouped) {
1916 updateGroupHeaderForWidget(widget);
1917 }
1918 }
1919
1920 void KItemListView::updateGroupHeaderForWidget(KItemListWidget* widget)
1921 {
1922 Q_ASSERT(m_grouped);
1923
1924 const int index = widget->index();
1925 if (!m_layouter->isFirstGroupItem(index)) {
1926 // The widget does not represent the first item of a group
1927 // and hence requires no header
1928 recycleGroupHeaderForWidget(widget);
1929 return;
1930 }
1931
1932 const QList<QPair<int, QVariant> > groups = model()->groups();
1933 if (groups.isEmpty() || !groupHeaderCreator()) {
1934 return;
1935 }
1936
1937 KItemListGroupHeader* groupHeader = m_visibleGroups.value(widget);
1938 if (!groupHeader) {
1939 groupHeader = groupHeaderCreator()->create(this);
1940 groupHeader->setParentItem(widget);
1941 m_visibleGroups.insert(widget, groupHeader);
1942 connect(widget, SIGNAL(geometryChanged()), this, SLOT(slotGeometryOfGroupHeaderParentChanged()));
1943 }
1944 Q_ASSERT(groupHeader->parentItem() == widget);
1945
1946 const int groupIndex = groupIndexForItem(index);
1947 Q_ASSERT(groupIndex >= 0);
1948 groupHeader->setData(groups.at(groupIndex).second);
1949 groupHeader->setRole(model()->sortRole());
1950 groupHeader->setStyleOption(m_styleOption);
1951 groupHeader->setScrollOrientation(scrollOrientation());
1952 groupHeader->setItemIndex(index);
1953
1954 groupHeader->show();
1955 }
1956
1957 void KItemListView::updateGroupHeaderLayout(KItemListWidget* widget)
1958 {
1959 KItemListGroupHeader* groupHeader = m_visibleGroups.value(widget);
1960 Q_ASSERT(groupHeader);
1961
1962 const int index = widget->index();
1963 const QRectF groupHeaderRect = m_layouter->groupHeaderRect(index);
1964 const QRectF itemRect = m_layouter->itemRect(index);
1965
1966 // The group-header is a child of the itemlist widget. Translate the
1967 // group header position to the relative position.
1968 if (scrollOrientation() == Qt::Vertical) {
1969 // In the vertical scroll orientation the group header should always span
1970 // the whole width no matter which temporary position the parent widget
1971 // has. In this case the x-position and width will be adjusted manually.
1972 const qreal x = -widget->x() - itemOffset();
1973 const qreal width = maximumItemOffset();
1974 groupHeader->setPos(x, -groupHeaderRect.height());
1975 groupHeader->resize(width, groupHeaderRect.size().height());
1976 } else {
1977 groupHeader->setPos(groupHeaderRect.x() - itemRect.x(), -widget->y());
1978 groupHeader->resize(groupHeaderRect.size());
1979 }
1980 }
1981
1982 void KItemListView::recycleGroupHeaderForWidget(KItemListWidget* widget)
1983 {
1984 KItemListGroupHeader* header = m_visibleGroups.value(widget);
1985 if (header) {
1986 header->setParentItem(0);
1987 groupHeaderCreator()->recycle(header);
1988 m_visibleGroups.remove(widget);
1989 disconnect(widget, SIGNAL(geometryChanged()), this, SLOT(slotGeometryOfGroupHeaderParentChanged()));
1990 }
1991 }
1992
1993 void KItemListView::updateVisibleGroupHeaders()
1994 {
1995 Q_ASSERT(m_grouped);
1996 m_layouter->markAsDirty();
1997
1998 QHashIterator<int, KItemListWidget*> it(m_visibleItems);
1999 while (it.hasNext()) {
2000 it.next();
2001 updateGroupHeaderForWidget(it.value());
2002 }
2003 }
2004
2005 int KItemListView::groupIndexForItem(int index) const
2006 {
2007 Q_ASSERT(m_grouped);
2008
2009 const QList<QPair<int, QVariant> > groups = model()->groups();
2010 if (groups.isEmpty()) {
2011 return -1;
2012 }
2013
2014 int min = 0;
2015 int max = groups.count() - 1;
2016 int mid = 0;
2017 do {
2018 mid = (min + max) / 2;
2019 if (index > groups[mid].first) {
2020 min = mid + 1;
2021 } else {
2022 max = mid - 1;
2023 }
2024 } while (groups[mid].first != index && min <= max);
2025
2026 if (min > max) {
2027 while (groups[mid].first > index && mid > 0) {
2028 --mid;
2029 }
2030 }
2031
2032 return mid;
2033 }
2034
2035 void KItemListView::updateAlternateBackgrounds()
2036 {
2037 QHashIterator<int, KItemListWidget*> it(m_visibleItems);
2038 while (it.hasNext()) {
2039 it.next();
2040 updateAlternateBackgroundForWidget(it.value());
2041 }
2042 }
2043
2044 void KItemListView::updateAlternateBackgroundForWidget(KItemListWidget* widget)
2045 {
2046 bool enabled = useAlternateBackgrounds();
2047 if (enabled) {
2048 const int index = widget->index();
2049 enabled = (index & 0x1) > 0;
2050 if (m_grouped) {
2051 const int groupIndex = groupIndexForItem(index);
2052 if (groupIndex >= 0) {
2053 const QList<QPair<int, QVariant> > groups = model()->groups();
2054 const int indexOfFirstGroupItem = groups[groupIndex].first;
2055 const int relativeIndex = index - indexOfFirstGroupItem;
2056 enabled = (relativeIndex & 0x1) > 0;
2057 }
2058 }
2059 }
2060 widget->setAlternateBackground(enabled);
2061 }
2062
2063 bool KItemListView::useAlternateBackgrounds() const
2064 {
2065 return m_itemSize.isEmpty() && m_visibleRoles.count() > 1;
2066 }
2067
2068 QHash<QByteArray, qreal> KItemListView::preferredColumnWidths(const KItemRangeList& itemRanges) const
2069 {
2070 QElapsedTimer timer;
2071 timer.start();
2072
2073 QHash<QByteArray, qreal> widths;
2074
2075 // Calculate the minimum width for each column that is required
2076 // to show the headline unclipped.
2077 const QFontMetricsF fontMetrics(m_headerWidget->font());
2078 const int gripMargin = m_headerWidget->style()->pixelMetric(QStyle::PM_HeaderGripMargin);
2079 const int headerMargin = m_headerWidget->style()->pixelMetric(QStyle::PM_HeaderMargin);
2080 foreach (const QByteArray& visibleRole, visibleRoles()) {
2081 const QString headerText = m_model->roleDescription(visibleRole);
2082 const qreal headerWidth = fontMetrics.width(headerText) + gripMargin + headerMargin * 2;
2083 widths.insert(visibleRole, headerWidth);
2084 }
2085
2086 // Calculate the preferred column withs for each item and ignore values
2087 // smaller than the width for showing the headline unclipped.
2088 const KItemListWidgetCreatorBase* creator = widgetCreator();
2089 int calculatedItemCount = 0;
2090 bool maxTimeExceeded = false;
2091 foreach (const KItemRange& itemRange, itemRanges) {
2092 const int startIndex = itemRange.index;
2093 const int endIndex = startIndex + itemRange.count - 1;
2094
2095 for (int i = startIndex; i <= endIndex; ++i) {
2096 foreach (const QByteArray& visibleRole, visibleRoles()) {
2097 qreal maxWidth = widths.value(visibleRole, 0);
2098 const qreal width = creator->preferredRoleColumnWidth(visibleRole, i, this);
2099 maxWidth = qMax(width, maxWidth);
2100 widths.insert(visibleRole, maxWidth);
2101 }
2102
2103 if (calculatedItemCount > 100 && timer.elapsed() > 200) {
2104 // When having several thousands of items calculating the sizes can get
2105 // very expensive. We accept a possibly too small role-size in favour
2106 // of having no blocking user interface.
2107 maxTimeExceeded = true;
2108 break;
2109 }
2110 ++calculatedItemCount;
2111 }
2112 if (maxTimeExceeded) {
2113 break;
2114 }
2115 }
2116
2117 return widths;
2118 }
2119
2120 void KItemListView::applyColumnWidthsFromHeader()
2121 {
2122 // Apply the new size to the layouter
2123 const qreal requiredWidth = columnWidthsSum();
2124 const QSizeF dynamicItemSize(qMax(size().width(), requiredWidth),
2125 m_itemSize.height());
2126 m_layouter->setItemSize(dynamicItemSize);
2127
2128 // Update the role sizes for all visible widgets
2129 QHashIterator<int, KItemListWidget*> it(m_visibleItems);
2130 while (it.hasNext()) {
2131 it.next();
2132 updateWidgetColumnWidths(it.value());
2133 }
2134 }
2135
2136 void KItemListView::updateWidgetColumnWidths(KItemListWidget* widget)
2137 {
2138 foreach (const QByteArray& role, m_visibleRoles) {
2139 widget->setColumnWidth(role, m_headerWidget->columnWidth(role));
2140 }
2141 }
2142
2143 void KItemListView::updatePreferredColumnWidths(const KItemRangeList& itemRanges)
2144 {
2145 Q_ASSERT(m_itemSize.isEmpty());
2146 const int itemCount = m_model->count();
2147 int rangesItemCount = 0;
2148 foreach (const KItemRange& range, itemRanges) {
2149 rangesItemCount += range.count;
2150 }
2151
2152 if (itemCount == rangesItemCount) {
2153 const QHash<QByteArray, qreal> preferredWidths = preferredColumnWidths(itemRanges);
2154 foreach (const QByteArray& role, m_visibleRoles) {
2155 m_headerWidget->setPreferredColumnWidth(role, preferredWidths.value(role));
2156 }
2157 } else {
2158 // Only a sub range of the roles need to be determined.
2159 // The chances are good that the widths of the sub ranges
2160 // already fit into the available widths and hence no
2161 // expensive update might be required.
2162 bool changed = false;
2163
2164 const QHash<QByteArray, qreal> updatedWidths = preferredColumnWidths(itemRanges);
2165 QHashIterator<QByteArray, qreal> it(updatedWidths);
2166 while (it.hasNext()) {
2167 it.next();
2168 const QByteArray& role = it.key();
2169 const qreal updatedWidth = it.value();
2170 const qreal currentWidth = m_headerWidget->preferredColumnWidth(role);
2171 if (updatedWidth > currentWidth) {
2172 m_headerWidget->setPreferredColumnWidth(role, updatedWidth);
2173 changed = true;
2174 }
2175 }
2176
2177 if (!changed) {
2178 // All the updated sizes are smaller than the current sizes and no change
2179 // of the stretched roles-widths is required
2180 return;
2181 }
2182 }
2183
2184 if (m_headerWidget->automaticColumnResizing()) {
2185 applyAutomaticColumnWidths();
2186 }
2187 }
2188
2189 void KItemListView::updatePreferredColumnWidths()
2190 {
2191 if (m_model) {
2192 updatePreferredColumnWidths(KItemRangeList() << KItemRange(0, m_model->count()));
2193 }
2194 }
2195
2196 void KItemListView::applyAutomaticColumnWidths()
2197 {
2198 Q_ASSERT(m_itemSize.isEmpty());
2199 Q_ASSERT(m_headerWidget->automaticColumnResizing());
2200 if (m_visibleRoles.isEmpty()) {
2201 return;
2202 }
2203
2204 // Calculate the maximum size of an item by considering the
2205 // visible role sizes and apply them to the layouter. If the
2206 // size does not use the available view-size the size of the
2207 // first role will get stretched.
2208
2209 foreach (const QByteArray& role, m_visibleRoles) {
2210 const qreal preferredWidth = m_headerWidget->preferredColumnWidth(role);
2211 m_headerWidget->setColumnWidth(role, preferredWidth);
2212 }
2213
2214 const QByteArray firstRole = m_visibleRoles.first();
2215 qreal firstColumnWidth = m_headerWidget->columnWidth(firstRole);
2216 QSizeF dynamicItemSize = m_itemSize;
2217
2218 qreal requiredWidth = columnWidthsSum();
2219 const qreal availableWidth = size().width();
2220 if (requiredWidth < availableWidth) {
2221 // Stretch the first column to use the whole remaining width
2222 firstColumnWidth += availableWidth - requiredWidth;
2223 m_headerWidget->setColumnWidth(firstRole, firstColumnWidth);
2224 } else if (requiredWidth > availableWidth && m_visibleRoles.count() > 1) {
2225 // Shrink the first column to be able to show as much other
2226 // columns as possible
2227 qreal shrinkedFirstColumnWidth = firstColumnWidth - requiredWidth + availableWidth;
2228
2229 // TODO: A proper calculation of the minimum width depends on the implementation
2230 // of KItemListWidget. Probably a kind of minimum size-hint should be introduced
2231 // later.
2232 const qreal minWidth = qMin(firstColumnWidth, qreal(m_styleOption.iconSize * 2 + 200));
2233 if (shrinkedFirstColumnWidth < minWidth) {
2234 shrinkedFirstColumnWidth = minWidth;
2235 }
2236
2237 m_headerWidget->setColumnWidth(firstRole, shrinkedFirstColumnWidth);
2238 requiredWidth -= firstColumnWidth - shrinkedFirstColumnWidth;
2239 }
2240
2241 dynamicItemSize.rwidth() = qMax(requiredWidth, availableWidth);
2242
2243 m_layouter->setItemSize(dynamicItemSize);
2244
2245 // Update the role sizes for all visible widgets
2246 QHashIterator<int, KItemListWidget*> it(m_visibleItems);
2247 while (it.hasNext()) {
2248 it.next();
2249 updateWidgetColumnWidths(it.value());
2250 }
2251 }
2252
2253 qreal KItemListView::columnWidthsSum() const
2254 {
2255 qreal widthsSum = 0;
2256 foreach (const QByteArray& role, m_visibleRoles) {
2257 widthsSum += m_headerWidget->columnWidth(role);
2258 }
2259 return widthsSum;
2260 }
2261
2262 QRectF KItemListView::headerBoundaries() const
2263 {
2264 return m_headerWidget->isVisible() ? m_headerWidget->geometry() : QRectF();
2265 }
2266
2267 bool KItemListView::changesItemGridLayout(const QSizeF& newGridSize,
2268 const QSizeF& newItemSize,
2269 const QSizeF& newItemMargin) const
2270 {
2271 if (newItemSize.isEmpty() || newGridSize.isEmpty()) {
2272 return false;
2273 }
2274
2275 if (m_layouter->scrollOrientation() == Qt::Vertical) {
2276 const qreal itemWidth = m_layouter->itemSize().width();
2277 if (itemWidth > 0) {
2278 const int newColumnCount = itemsPerSize(newGridSize.width(),
2279 newItemSize.width(),
2280 newItemMargin.width());
2281 if (m_model->count() > newColumnCount) {
2282 const int oldColumnCount = itemsPerSize(m_layouter->size().width(),
2283 itemWidth,
2284 m_layouter->itemMargin().width());
2285 return oldColumnCount != newColumnCount;
2286 }
2287 }
2288 } else {
2289 const qreal itemHeight = m_layouter->itemSize().height();
2290 if (itemHeight > 0) {
2291 const int newRowCount = itemsPerSize(newGridSize.height(),
2292 newItemSize.height(),
2293 newItemMargin.height());
2294 if (m_model->count() > newRowCount) {
2295 const int oldRowCount = itemsPerSize(m_layouter->size().height(),
2296 itemHeight,
2297 m_layouter->itemMargin().height());
2298 return oldRowCount != newRowCount;
2299 }
2300 }
2301 }
2302
2303 return false;
2304 }
2305
2306 bool KItemListView::animateChangedItemCount(int changedItemCount) const
2307 {
2308 if (m_itemSize.isEmpty()) {
2309 // We have only columns or only rows, but no grid: An animation is usually
2310 // welcome when inserting or removing items.
2311 return !supportsItemExpanding();
2312 }
2313
2314 if (m_layouter->size().isEmpty() || m_layouter->itemSize().isEmpty()) {
2315 return false;
2316 }
2317
2318 const int maximum = (scrollOrientation() == Qt::Vertical)
2319 ? m_layouter->size().width() / m_layouter->itemSize().width()
2320 : m_layouter->size().height() / m_layouter->itemSize().height();
2321 // Only animate if up to 2/3 of a row or column are inserted or removed
2322 return changedItemCount <= maximum * 2 / 3;
2323 }
2324
2325
2326 bool KItemListView::scrollBarRequired(const QSizeF& size) const
2327 {
2328 const QSizeF oldSize = m_layouter->size();
2329
2330 m_layouter->setSize(size);
2331 const qreal maxOffset = m_layouter->maximumScrollOffset();
2332 m_layouter->setSize(oldSize);
2333
2334 return m_layouter->scrollOrientation() == Qt::Vertical ? maxOffset > size.height()
2335 : maxOffset > size.width();
2336 }
2337
2338 int KItemListView::showDropIndicator(const QPointF& pos)
2339 {
2340 QHashIterator<int, KItemListWidget*> it(m_visibleItems);
2341 while (it.hasNext()) {
2342 it.next();
2343 const KItemListWidget* widget = it.value();
2344
2345 const QPointF mappedPos = widget->mapFromItem(this, pos);
2346 const QRectF rect = itemRect(widget->index());
2347 if (mappedPos.y() >= 0 && mappedPos.y() <= rect.height()) {
2348 if (m_model->supportsDropping(widget->index())) {
2349 const int gap = qMax(4, m_styleOption.padding);
2350 if (mappedPos.y() >= gap && mappedPos.y() <= rect.height() - gap) {
2351 return -1;
2352 }
2353 }
2354
2355 const bool isAboveItem = (mappedPos.y () < rect.height() / 2);
2356 const qreal y = isAboveItem ? rect.top() : rect.bottom();
2357
2358 const QRectF draggingInsertIndicator(rect.left(), y, rect.width(), 1);
2359 if (m_dropIndicator != draggingInsertIndicator) {
2360 m_dropIndicator = draggingInsertIndicator;
2361 update();
2362 }
2363
2364 int index = widget->index();
2365 if (!isAboveItem) {
2366 ++index;
2367 }
2368 return index;
2369 }
2370 }
2371
2372 const QRectF firstItemRect = itemRect(firstVisibleIndex());
2373 return (pos.y() <= firstItemRect.top()) ? 0 : -1;
2374 }
2375
2376 void KItemListView::hideDropIndicator()
2377 {
2378 if (!m_dropIndicator.isNull()) {
2379 m_dropIndicator = QRectF();
2380 update();
2381 }
2382 }
2383
2384 void KItemListView::updateGroupHeaderHeight()
2385 {
2386 qreal groupHeaderHeight = m_styleOption.fontMetrics.height();
2387 qreal groupHeaderMargin = 0;
2388
2389 if (scrollOrientation() == Qt::Horizontal) {
2390 // The vertical margin above and below the header should be
2391 // equal to the horizontal margin, not the vertical margin
2392 // from m_styleOption.
2393 groupHeaderHeight += 2 * m_styleOption.horizontalMargin;
2394 groupHeaderMargin = m_styleOption.horizontalMargin;
2395 } else if (m_itemSize.isEmpty()){
2396 groupHeaderHeight += 4 * m_styleOption.padding;
2397 groupHeaderMargin = m_styleOption.iconSize / 2;
2398 } else {
2399 groupHeaderHeight += 2 * m_styleOption.padding + m_styleOption.verticalMargin;
2400 groupHeaderMargin = m_styleOption.iconSize / 4;
2401 }
2402 m_layouter->setGroupHeaderHeight(groupHeaderHeight);
2403 m_layouter->setGroupHeaderMargin(groupHeaderMargin);
2404
2405 updateVisibleGroupHeaders();
2406 }
2407
2408 void KItemListView::updateSiblingsInformation(int firstIndex, int lastIndex)
2409 {
2410 if (!supportsItemExpanding() || !m_model) {
2411 return;
2412 }
2413
2414 if (firstIndex < 0 || lastIndex < 0) {
2415 firstIndex = m_layouter->firstVisibleIndex();
2416 lastIndex = m_layouter->lastVisibleIndex();
2417 } else {
2418 const bool isRangeVisible = (firstIndex <= m_layouter->lastVisibleIndex() &&
2419 lastIndex >= m_layouter->firstVisibleIndex());
2420 if (!isRangeVisible) {
2421 return;
2422 }
2423 }
2424
2425 int previousParents = 0;
2426 QBitArray previousSiblings;
2427
2428 // The rootIndex describes the first index where the siblings get
2429 // calculated from. For the calculation the upper most parent item
2430 // is required. For performance reasons it is checked first whether
2431 // the visible items before or after the current range already
2432 // contain a siblings information which can be used as base.
2433 int rootIndex = firstIndex;
2434
2435 KItemListWidget* widget = m_visibleItems.value(firstIndex - 1);
2436 if (!widget) {
2437 // There is no visible widget before the range, check whether there
2438 // is one after the range:
2439 widget = m_visibleItems.value(lastIndex + 1);
2440 if (widget) {
2441 // The sibling information of the widget may only be used if
2442 // all items of the range have the same number of parents.
2443 const int parents = m_model->expandedParentsCount(lastIndex + 1);
2444 for (int i = lastIndex; i >= firstIndex; --i) {
2445 if (m_model->expandedParentsCount(i) != parents) {
2446 widget = 0;
2447 break;
2448 }
2449 }
2450 }
2451 }
2452
2453 if (widget) {
2454 // Performance optimization: Use the sibling information of the visible
2455 // widget beside the given range.
2456 previousSiblings = widget->siblingsInformation();
2457 if (previousSiblings.isEmpty()) {
2458 return;
2459 }
2460 previousParents = previousSiblings.count() - 1;
2461 previousSiblings.truncate(previousParents);
2462 } else {
2463 // Potentially slow path: Go back to the upper most parent of firstIndex
2464 // to be able to calculate the initial value for the siblings.
2465 while (rootIndex > 0 && m_model->expandedParentsCount(rootIndex) > 0) {
2466 --rootIndex;
2467 }
2468 }
2469
2470 Q_ASSERT(previousParents >= 0);
2471 for (int i = rootIndex; i <= lastIndex; ++i) {
2472 // Update the parent-siblings in case if the current item represents
2473 // a child or an upper parent.
2474 const int currentParents = m_model->expandedParentsCount(i);
2475 Q_ASSERT(currentParents >= 0);
2476 if (previousParents < currentParents) {
2477 previousParents = currentParents;
2478 previousSiblings.resize(currentParents);
2479 previousSiblings.setBit(currentParents - 1, hasSiblingSuccessor(i - 1));
2480 } else if (previousParents > currentParents) {
2481 previousParents = currentParents;
2482 previousSiblings.truncate(currentParents);
2483 }
2484
2485 if (i >= firstIndex) {
2486 // The index represents a visible item. Apply the parent-siblings
2487 // and update the sibling of the current item.
2488 KItemListWidget* widget = m_visibleItems.value(i);
2489 if (!widget) {
2490 continue;
2491 }
2492
2493 QBitArray siblings = previousSiblings;
2494 siblings.resize(siblings.count() + 1);
2495 siblings.setBit(siblings.count() - 1, hasSiblingSuccessor(i));
2496
2497 widget->setSiblingsInformation(siblings);
2498 }
2499 }
2500 }
2501
2502 bool KItemListView::hasSiblingSuccessor(int index) const
2503 {
2504 bool hasSuccessor = false;
2505 const int parentsCount = m_model->expandedParentsCount(index);
2506 int successorIndex = index + 1;
2507
2508 // Search the next sibling
2509 const int itemCount = m_model->count();
2510 while (successorIndex < itemCount) {
2511 const int currentParentsCount = m_model->expandedParentsCount(successorIndex);
2512 if (currentParentsCount == parentsCount) {
2513 hasSuccessor = true;
2514 break;
2515 } else if (currentParentsCount < parentsCount) {
2516 break;
2517 }
2518 ++successorIndex;
2519 }
2520
2521 if (m_grouped && hasSuccessor) {
2522 // If the sibling is part of another group, don't mark it as
2523 // successor as the group header is between the sibling connections.
2524 for (int i = index + 1; i <= successorIndex; ++i) {
2525 if (m_layouter->isFirstGroupItem(i)) {
2526 hasSuccessor = false;
2527 break;
2528 }
2529 }
2530 }
2531
2532 return hasSuccessor;
2533 }
2534
2535 int KItemListView::calculateAutoScrollingIncrement(int pos, int range, int oldInc)
2536 {
2537 int inc = 0;
2538
2539 const int minSpeed = 4;
2540 const int maxSpeed = 128;
2541 const int speedLimiter = 96;
2542 const int autoScrollBorder = 64;
2543
2544 // Limit the increment that is allowed to be added in comparison to 'oldInc'.
2545 // This assures that the autoscrolling speed grows gradually.
2546 const int incLimiter = 1;
2547
2548 if (pos < autoScrollBorder) {
2549 inc = -minSpeed + qAbs(pos - autoScrollBorder) * (pos - autoScrollBorder) / speedLimiter;
2550 inc = qMax(inc, -maxSpeed);
2551 inc = qMax(inc, oldInc - incLimiter);
2552 } else if (pos > range - autoScrollBorder) {
2553 inc = minSpeed + qAbs(pos - range + autoScrollBorder) * (pos - range + autoScrollBorder) / speedLimiter;
2554 inc = qMin(inc, maxSpeed);
2555 inc = qMin(inc, oldInc + incLimiter);
2556 }
2557
2558 return inc;
2559 }
2560
2561 int KItemListView::itemsPerSize(qreal size, qreal itemSize, qreal itemMargin)
2562 {
2563 const qreal availableSize = size - itemMargin;
2564 const int count = availableSize / (itemSize + itemMargin);
2565 return count;
2566 }
2567
2568
2569
2570 KItemListCreatorBase::~KItemListCreatorBase()
2571 {
2572 qDeleteAll(m_recycleableWidgets);
2573 qDeleteAll(m_createdWidgets);
2574 }
2575
2576 void KItemListCreatorBase::addCreatedWidget(QGraphicsWidget* widget)
2577 {
2578 m_createdWidgets.insert(widget);
2579 }
2580
2581 void KItemListCreatorBase::pushRecycleableWidget(QGraphicsWidget* widget)
2582 {
2583 Q_ASSERT(m_createdWidgets.contains(widget));
2584 m_createdWidgets.remove(widget);
2585
2586 if (m_recycleableWidgets.count() < 100) {
2587 m_recycleableWidgets.append(widget);
2588 widget->setVisible(false);
2589 } else {
2590 delete widget;
2591 }
2592 }
2593
2594 QGraphicsWidget* KItemListCreatorBase::popRecycleableWidget()
2595 {
2596 if (m_recycleableWidgets.isEmpty()) {
2597 return 0;
2598 }
2599
2600 QGraphicsWidget* widget = m_recycleableWidgets.takeLast();
2601 m_createdWidgets.insert(widget);
2602 return widget;
2603 }
2604
2605 KItemListWidgetCreatorBase::~KItemListWidgetCreatorBase()
2606 {
2607 }
2608
2609 void KItemListWidgetCreatorBase::recycle(KItemListWidget* widget)
2610 {
2611 widget->setParentItem(0);
2612 widget->setOpacity(1.0);
2613 pushRecycleableWidget(widget);
2614 }
2615
2616 KItemListGroupHeaderCreatorBase::~KItemListGroupHeaderCreatorBase()
2617 {
2618 }
2619
2620 void KItemListGroupHeaderCreatorBase::recycle(KItemListGroupHeader* header)
2621 {
2622 header->setOpacity(1.0);
2623 pushRecycleableWidget(header);
2624 }
2625
2626 #include "kitemlistview.moc"