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