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