const bool animate = !changesItemGridLayout(m_layouter->size(),
itemSize,
m_layouter->itemMargin());
-
+
+ const bool updateAlternateBackgrounds = (m_visibleRoles.count() > 1) &&
+ (( m_itemSize.isEmpty() && !itemSize.isEmpty()) ||
+ (!m_itemSize.isEmpty() && itemSize.isEmpty()));
+
m_itemSize = itemSize;
+ if (updateAlternateBackgrounds) {
+ // For an empty item size alternate backgrounds are drawn if more than
+ // one role is shown. Assure that the backgrounds for visible items are
+ // updated when changing the size in this context.
+ QHashIterator<int, KItemListWidget*> it(m_visibleItems);
+ while (it.hasNext()) {
+ it.next();
+ updateAlternateBackgroundForWidget(it.value());
+ }
+ }
+
if (itemSize.isEmpty()) {
updateVisibleRolesSizes();
} else {
const QList<QByteArray> previousRoles = m_visibleRoles;
m_visibleRoles = roles;
+ const bool updateAlternateBackgrounds = m_itemSize.isEmpty() &&
+ ((roles.count() > 1 && previousRoles.count() <= 1) ||
+ (roles.count() <= 1 && previousRoles.count() > 1));
+
QHashIterator<int, KItemListWidget*> it(m_visibleItems);
while (it.hasNext()) {
it.next();
KItemListWidget* widget = it.value();
widget->setVisibleRoles(roles);
widget->setVisibleRolesSizes(m_stretchedVisibleRolesSizes);
+ if (updateAlternateBackgrounds) {
+ updateAlternateBackgroundForWidget(widget);
+ }
}
m_sizeHintResolver->clearCache();
delete m_autoScrollTimer;
m_autoScrollTimer = 0;
}
-
}
bool KItemListView::autoScroll() const
const KItemListStyleOption previousOption = m_styleOption;
m_styleOption = option;
- bool animate = true;
+ bool animate = true;
const QSizeF margin(option.horizontalMargin, option.verticalMargin);
if (margin != m_layouter->itemMargin()) {
// Skip animations when the number of rows or columns
it.value()->setStyleOption(option);
}
- m_sizeHintResolver->clearCache();
+ m_sizeHintResolver->clearCache();
doLayout(animate ? Animation : NoAnimation);
onStyleOptionChanged(option, previousOption);
// update of the visible-roles sizes, only the stretched sizes
// need to be adjusted to the new size.
updateStretchedVisibleRolesSizes();
-
+
if (m_useHeaderWidths) {
QSizeF dynamicItemSize = m_layouter->itemSize();
-
+
if (m_itemSize.width() < 0) {
const qreal requiredWidth = visibleRolesSizesWidthSum();
if (newSize.width() > requiredWidth) {
const qreal headerWidth = qMax(newSize.width(), requiredWidth);
m_header->resize(headerWidth, m_header->size().height());
}
-
+
if (m_itemSize.height() < 0) {
const qreal requiredHeight = visibleRolesSizesHeightSum();
if (newSize.height() > requiredHeight) {
}
// TODO: KItemListHeader is not prepared for vertical alignment
}
-
+
m_layouter->setItemSize(dynamicItemSize);
}
-
+
// Triggering a synchronous layout is fine from a performance point of view,
// as with dynamic item sizes no moving animation must be done.
m_layouter->setSize(newSize);
} else {
const bool animate = !changesItemGridLayout(newSize,
m_layouter->itemSize(),
- m_layouter->itemMargin());
+ m_layouter->itemMargin());
m_layouter->setSize(newSize);
-
+
if (animate) {
// Trigger an asynchronous relayout with m_layoutTimer to prevent
// performance bottlenecks. If the timer is exceeded, an animated layout
return QHash<QByteArray, QSizeF>();
}
+void KItemListView::setSupportsItemExpanding(bool supportsExpanding)
+{
+ if (m_supportsItemExpanding != supportsExpanding) {
+ m_supportsItemExpanding = supportsExpanding;
+ updateSiblingsInformation();
+ onSupportsItemExpandingChanged(supportsExpanding);
+ }
+}
+
bool KItemListView::supportsItemExpanding() const
{
return m_supportsItemExpanding;
connect(m_header, SIGNAL(visibleRoleWidthChanged(QByteArray,qreal,qreal)),
this, SLOT(slotVisibleRoleWidthChanged(QByteArray,qreal,qreal)));
+ connect(m_header, SIGNAL(visibleRoleMoved(QByteArray,int,int)),
+ this, SLOT(slotVisibleRoleMoved(QByteArray,int,int)));
connect(m_header, SIGNAL(sortOrderChanged(Qt::SortOrder,Qt::SortOrder)),
this, SIGNAL(sortOrderChanged(Qt::SortOrder,Qt::SortOrder)));
connect(m_header, SIGNAL(sortRoleChanged(QByteArray,QByteArray)),
Q_UNUSED(previous);
}
+void KItemListView::onSupportsItemExpandingChanged(bool supportsExpanding)
+{
+ Q_UNUSED(supportsExpanding);
+}
+
void KItemListView::onTransactionBegin()
{
}
return m_visibleItems.values();
}
-void KItemListView::setSupportsItemExpanding(bool supportsExpanding)
-{
- if (m_supportsItemExpanding != supportsExpanding) {
- m_supportsItemExpanding = supportsExpanding;
- updateSiblingsInformation();
- }
-}
-
void KItemListView::slotItemsInserted(const KItemRangeList& itemRanges)
{
updateVisibleRolesSizes(itemRanges);
beginTransaction();
}
- // Important: Don't read any m_layouter-property inside the for-loop in case if
- // multiple ranges are given! m_layouter accesses m_sizeHintResolver which is
- // updated in each loop-cycle and has only a consistent state after the loop.
m_layouter->markAsDirty();
int previouslyInsertedCount = 0;
for (int i = itemsToMove.count() - 1; i >= 0; --i) {
KItemListWidget* widget = m_visibleItems.value(itemsToMove[i]);
Q_ASSERT(widget);
+ const int newIndex = widget->index() + count;
if (hasMultipleRanges) {
- setWidgetIndex(widget, widget->index() + count);
+ setWidgetIndex(widget, newIndex);
} else {
// Try to animate the moving of the item
- moveWidgetToIndex(widget, widget->index() + count);
+ moveWidgetToIndex(widget, newIndex);
}
}
}
if (hasMultipleRanges) {
+#ifndef QT_NO_DEBUG
+ // Important: Don't read any m_layouter-property inside the for-loop in case if
+ // multiple ranges are given! m_layouter accesses m_sizeHintResolver which is
+ // updated in each loop-cycle and has only a consistent state after the loop.
+ Q_ASSERT(m_layouter->isDirty());
+#endif
m_endTransactionAnimationHint = NoAnimation;
endTransaction();
updateSiblingsInformation();
beginTransaction();
}
- // Important: Don't read any m_layouter-property inside the for-loop in case if
- // multiple ranges are given! m_layouter accesses m_sizeHintResolver which is
- // updated in each loop-cycle and has only a consistent state after the loop.
m_layouter->markAsDirty();
for (int i = itemRanges.count() - 1; i >= 0; --i) {
}
if (hasMultipleRanges) {
+#ifndef QT_NO_DEBUG
+ // Important: Don't read any m_layouter-property inside the for-loop in case if
+ // multiple ranges are given! m_layouter accesses m_sizeHintResolver which is
+ // updated in each loop-cycle and has only a consistent state after the loop.
+ Q_ASSERT(m_layouter->isDirty());
+#endif
m_endTransactionAnimationHint = NoAnimation;
endTransaction();
updateSiblingsInformation();
KItemListWidget* widget = m_visibleItems.value(index);
if (widget) {
updateWidgetProperties(widget, index);
- if (m_grouped) {
- updateGroupHeaderForWidget(widget);
- }
initializeItemListWidget(widget);
}
}
Q_ASSERT(m_visibleGroups.isEmpty());
}
+ if (useAlternateBackgrounds()) {
+ // Changing the group mode requires to update the alternate backgrounds
+ // as with the enabled group mode the altering is done on base of the first
+ // group item.
+ QHashIterator<int, KItemListWidget*> it(m_visibleItems);
+ while (it.hasNext()) {
+ it.next();
+ updateAlternateBackgroundForWidget(it.value());
+ }
+ }
+
+ updateSiblingsInformation();
doLayout(NoAnimation);
}
KItemListWidget* previousWidget = m_visibleItems.value(previous, 0);
if (previousWidget) {
- Q_ASSERT(previousWidget->isCurrent());
previousWidget->setCurrent(false);
}
KItemListWidget* currentWidget = m_visibleItems.value(current, 0);
if (currentWidget) {
- Q_ASSERT(!currentWidget->isCurrent());
currentWidget->setCurrent(true);
}
}
}
}
+void KItemListView::slotVisibleRoleMoved(const QByteArray& role,
+ int currentIndex,
+ int previousIndex)
+{
+ Q_ASSERT(m_visibleRoles[previousIndex] == role);
+
+ const QList<QByteArray> previous = m_visibleRoles;
+
+ QList<QByteArray> current = m_visibleRoles;
+ current.removeAt(previousIndex);
+ current.insert(currentIndex, role);
+
+ setVisibleRoles(current);
+
+ emit visibleRolesChanged(current, previous);
+}
+
void KItemListView::triggerAutoScrolling()
{
if (!m_autoScrollTimer) {
}
m_model = model;
- m_layouter->setModel(model);
+ m_layouter->setModel(model);
m_grouped = model->groupedSorting();
if (m_model) {
const int oldIndex = reusableItems.takeLast();
widget = m_visibleItems.value(oldIndex);
setWidgetIndex(widget, i);
-
- if (m_grouped) {
- updateGroupHeaderForWidget(widget);
- }
+ updateWidgetProperties(widget, i);
+ initializeItemListWidget(widget);
} else {
// No reusable KItemListWidget instance is available, create a new one
widget = createWidget(i);
} else {
widget->setVisible(false);
items.append(index);
-
+
if (m_grouped) {
recycleGroupHeaderForWidget(widget);
}
if (widget->pos() == newPos) {
return false;
}
-
+
bool startMovingAnim = false;
// When having a grid the moving-animation should only be started, if it is done within
KItemListWidget* widget = m_widgetCreator->create(this);
widget->setFlag(QGraphicsItem::ItemStacksBehindParent);
- updateWidgetProperties(widget, index);
m_visibleItems.insert(index, widget);
m_visibleCells.insert(index, Cell());
-
- if (m_grouped) {
- updateGroupHeaderForWidget(widget);
- }
-
+ updateWidgetProperties(widget, index);
initializeItemListWidget(widget);
return widget;
}
void KItemListView::setWidgetIndex(KItemListWidget* widget, int index)
{
const int oldIndex = widget->index();
-
m_visibleItems.remove(oldIndex);
m_visibleCells.remove(oldIndex);
- updateWidgetProperties(widget, index);
-
m_visibleItems.insert(index, widget);
m_visibleCells.insert(index, Cell());
- initializeItemListWidget(widget);
+ widget->setIndex(index);
}
void KItemListView::moveWidgetToIndex(KItemListWidget* widget, int index)
widget->setCurrent(index == selectionManager->currentItem());
widget->setSelected(selectionManager->isSelected(index));
widget->setHovered(false);
- widget->setAlternatingBackgroundColors(false);
widget->setEnabledSelectionToggle(enabledSelectionToggles());
widget->setIndex(index);
widget->setData(m_model->data(index));
widget->setSiblingsInformation(QBitArray());
+ updateAlternateBackgroundForWidget(widget);
+
+ if (m_grouped) {
+ updateGroupHeaderForWidget(widget);
+ }
}
void KItemListView::updateGroupHeaderForWidget(KItemListWidget* widget)
}
Q_ASSERT(groupHeader->parentItem() == widget);
- // Determine the shown data for the header by doing a binary
- // search in the groups-list
- int min = 0;
- int max = groups.count() - 1;
- int mid = 0;
- do {
- mid = (min + max) / 2;
- if (index > groups.at(mid).first) {
- min = mid + 1;
- } else {
- max = mid - 1;
- }
- } while (groups.at(mid).first != index && min <= max);
-
- groupHeader->setData(groups.at(mid).second);
+ const int groupIndex = groupIndexForItem(index);
+ Q_ASSERT(groupIndex >= 0);
+ groupHeader->setData(groups.at(groupIndex).second);
groupHeader->setRole(model()->sortRole());
groupHeader->setStyleOption(m_styleOption);
groupHeader->setScrollOrientation(scrollOrientation());
}
}
+int KItemListView::groupIndexForItem(int index) const
+{
+ Q_ASSERT(m_grouped);
+
+ const QList<QPair<int, QVariant> > groups = model()->groups();
+ if (groups.isEmpty()) {
+ return -1;
+ }
+
+ int min = 0;
+ int max = groups.count() - 1;
+ int mid = 0;
+ do {
+ mid = (min + max) / 2;
+ if (index > groups[mid].first) {
+ min = mid + 1;
+ } else {
+ max = mid - 1;
+ }
+ } while (groups[mid].first != index && min <= max);
+
+ if (min > max) {
+ while (groups[mid].first > index && mid > 0) {
+ --mid;
+ }
+ }
+
+ return mid;
+}
+
+void KItemListView::updateAlternateBackgroundForWidget(KItemListWidget* widget)
+{
+ bool enabled = useAlternateBackgrounds();
+ if (enabled) {
+ const int index = widget->index();
+ enabled = (index & 0x1) > 0;
+ if (m_grouped) {
+ const int groupIndex = groupIndexForItem(index);
+ if (groupIndex >= 0) {
+ const QList<QPair<int, QVariant> > groups = model()->groups();
+ const int indexOfFirstGroupItem = groups[groupIndex].first;
+ const int relativeIndex = index - indexOfFirstGroupItem;
+ enabled = (relativeIndex & 0x1) > 0;
+ }
+ }
+ }
+ widget->setAlternateBackground(enabled);
+}
+
+bool KItemListView::useAlternateBackgrounds() const
+{
+ return m_itemSize.isEmpty() && m_visibleRoles.count() > 1;
+}
+
QHash<QByteArray, qreal> KItemListView::headerRolesWidths() const
{
QHash<QByteArray, qreal> rolesWidths;
bool KItemListView::changesItemGridLayout(const QSizeF& newGridSize,
const QSizeF& newItemSize,
const QSizeF& newItemMargin) const
-{
+{
if (newItemSize.isEmpty() || newGridSize.isEmpty()) {
return false;
}
-
+
if (m_layouter->scrollOrientation() == Qt::Vertical) {
const qreal itemWidth = m_layouter->itemSize().width();
if (itemWidth > 0) {
m_layouter->itemMargin().width());
return oldColumnCount != newColumnCount;
}
- }
+ }
} else {
const qreal itemHeight = m_layouter->itemSize().height();
if (itemHeight > 0) {
m_layouter->itemMargin().height());
return oldRowCount != newRowCount;
}
- }
+ }
}
-
+
return false;
}
if (m_layouter->size().isEmpty() || m_layouter->itemSize().isEmpty()) {
return false;
}
-
+
const int maximum = (scrollOrientation() == Qt::Vertical)
? m_layouter->size().width() / m_layouter->itemSize().width()
: m_layouter->size().height() / m_layouter->itemSize().height();
{
qreal groupHeaderHeight = m_styleOption.fontMetrics.height();
qreal groupHeaderMargin = 0;
-
+
if (scrollOrientation() == Qt::Horizontal) {
// The vertical margin above and below the header should be
// equal to the horizontal margin, not the vertical margin
groupHeaderHeight += 2 * m_styleOption.horizontalMargin;
groupHeaderMargin = m_styleOption.horizontalMargin;
} else if (m_itemSize.isEmpty()){
- groupHeaderHeight += 2 * m_styleOption.padding;
+ groupHeaderHeight += 4 * m_styleOption.padding;
groupHeaderMargin = m_styleOption.iconSize / 2;
} else {
- groupHeaderHeight += 2 * m_styleOption.padding;
+ groupHeaderHeight += 2 * m_styleOption.padding + m_styleOption.verticalMargin;
groupHeaderMargin = m_styleOption.iconSize / 4;
}
m_layouter->setGroupHeaderHeight(groupHeaderHeight);
void KItemListView::updateSiblingsInformation(int firstIndex, int lastIndex)
{
- if (!supportsItemExpanding()) {
+ if (!supportsItemExpanding() || !m_model) {
return;
}