#include "kitemlistview.h"
#include "kitemlistcontroller.h"
-#include "kitemlistheader_p.h"
+#include "kitemlistheader.h"
+#include "kitemlistheaderwidget_p.h"
#include "kitemlistrubberband_p.h"
#include "kitemlistselectionmanager.h"
#include "kitemlistsizehintresolver_p.h"
m_controller(0),
m_model(0),
m_visibleRoles(),
- m_visibleRolesSizes(),
- m_stretchedVisibleRolesSizes(),
m_widgetCreator(0),
m_groupHeaderCreator(0),
m_styleOption(),
m_autoScrollIncrement(0),
m_autoScrollTimer(0),
m_header(0),
- m_useHeaderWidths(false)
+ m_headerWidget(0)
{
setAcceptHoverEvents(true);
m_rubberBand = new KItemListRubberBand(this);
connect(m_rubberBand, SIGNAL(activationChanged(bool)), this, SLOT(slotRubberBandActivationChanged(bool)));
+
+ m_headerWidget = new KItemListHeaderWidget(this);
+ m_headerWidget->setVisible(false);
+
+ m_header = new KItemListHeader(this);
}
KItemListView::~KItemListView()
itemSize,
m_layouter->itemMargin());
- const bool updateAlternateBackgrounds = (m_visibleRoles.count() > 1) &&
- (( m_itemSize.isEmpty() && !itemSize.isEmpty()) ||
- (!m_itemSize.isEmpty() && itemSize.isEmpty()));
+ const bool alternateBackgroundsChanged = (m_visibleRoles.count() > 1) &&
+ (( m_itemSize.isEmpty() && !itemSize.isEmpty()) ||
+ (!m_itemSize.isEmpty() && itemSize.isEmpty()));
m_itemSize = itemSize;
- if (updateAlternateBackgrounds) {
+ if (alternateBackgroundsChanged) {
// 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());
- }
+ updateAlternateBackgrounds();
}
if (itemSize.isEmpty()) {
- updateVisibleRolesSizes();
+ if (m_headerWidget->automaticColumnResizing()) {
+ updatePreferredColumnWidths();
+ } else {
+ // Only apply the changed height and respect the header widths
+ // set by the user
+ const qreal currentWidth = m_layouter->itemSize().width();
+ const QSizeF newSize(currentWidth, itemSize.height());
+ m_layouter->setItemSize(newSize);
+ }
} else {
m_layouter->setItemSize(itemSize);
}
}
m_layouter->setItemOffset(offset);
- if (m_header) {
- m_header->setPos(-offset, 0);
+ if (m_headerWidget->isVisible()) {
+ m_headerWidget->setOffset(offset);
}
// Don't check whether the m_layoutTimer is active: Changing the
{
const QList<QByteArray> previousRoles = m_visibleRoles;
m_visibleRoles = roles;
+ onVisibleRolesChanged(roles, previousRoles);
+
+ m_sizeHintResolver->clearCache();
+ m_layouter->markAsDirty();
+
+ if (m_itemSize.isEmpty()) {
+ m_headerWidget->setColumns(roles);
+ updatePreferredColumnWidths();
+ if (!m_headerWidget->automaticColumnResizing()) {
+ // The column-width of new roles are still 0. Apply the preferred
+ // column-width as default with.
+ foreach (const QByteArray& role, m_visibleRoles) {
+ if (m_headerWidget->columnWidth(role) == 0) {
+ const qreal width = m_headerWidget->preferredColumnWidth(role);
+ m_headerWidget->setColumnWidth(role, width);
+ }
+ }
+
+ applyColumnWidthsFromHeader();
+ }
+ }
- const bool updateAlternateBackgrounds = m_itemSize.isEmpty() &&
- ((roles.count() > 1 && previousRoles.count() <= 1) ||
- (roles.count() <= 1 && previousRoles.count() > 1));
+ const bool alternateBackgroundsChanged = 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) {
+ if (alternateBackgroundsChanged) {
updateAlternateBackgroundForWidget(widget);
}
}
- m_sizeHintResolver->clearCache();
- m_layouter->markAsDirty();
-
- if (m_header) {
- m_header->setVisibleRoles(roles);
- m_header->setVisibleRolesWidths(headerRolesWidths());
- m_useHeaderWidths = false;
- }
-
- updateVisibleRolesSizes();
doLayout(NoAnimation);
-
- onVisibleRolesChanged(roles, previousRoles);
}
QList<QByteArray> KItemListView::visibleRoles() const
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);
const QSizeF newSize = rect.size();
if (m_itemSize.isEmpty()) {
- // The item size is dynamic:
- // Changing the geometry does not require to do an expensive
- // 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) {
- dynamicItemSize.setWidth(newSize.width());
- }
- 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) {
- dynamicItemSize.setHeight(newSize.height());
- }
- // TODO: KItemListHeader is not prepared for vertical alignment
- }
-
+ m_headerWidget->resize(rect.width(), m_headerWidget->size().height());
+ if (m_headerWidget->automaticColumnResizing()) {
+ applyAutomaticColumnWidths();
+ } else {
+ const qreal requiredWidth = columnWidthsSum();
+ const QSizeF dynamicItemSize(qMax(newSize.width(), requiredWidth),
+ m_itemSize.height());
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);
- doLayout(Animation);
+ doLayout(NoAnimation);
} 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
QSizeF KItemListView::itemSizeHint(int index) const
{
- Q_UNUSED(index);
- return itemSize();
+ return m_widgetCreator->itemSizeHint(index, this);
}
-QHash<QByteArray, QSizeF> KItemListView::visibleRolesSizes(const KItemRangeList& itemRanges) const
+void KItemListView::setSupportsItemExpanding(bool supportsExpanding)
{
- Q_UNUSED(itemRanges);
- return QHash<QByteArray, QSizeF>();
+ if (m_supportsItemExpanding != supportsExpanding) {
+ m_supportsItemExpanding = supportsExpanding;
+ updateSiblingsInformation();
+ onSupportsItemExpandingChanged(supportsExpanding);
+ }
}
bool KItemListView::supportsItemExpanding() const
void KItemListView::scrollToItem(int index)
{
QRectF viewGeometry = geometry();
- if (m_header) {
- const qreal headerHeight = m_header->size().height();
+ if (m_headerWidget->isVisible()) {
+ const qreal headerHeight = m_headerWidget->size().height();
viewGeometry.adjust(0, headerHeight, 0, 0);
}
const QRectF currentRect = itemRect(index);
return m_activeTransactions > 0;
}
-void KItemListView::setHeaderShown(bool show)
+void KItemListView::setHeaderVisible(bool visible)
{
+ if (visible && !m_headerWidget->isVisible()) {
+ QStyleOptionHeader option;
+ const QSize headerSize = style()->sizeFromContents(QStyle::CT_HeaderSection,
+ &option, QSize());
- if (show && !m_header) {
- m_header = new KItemListHeader(this);
- m_header->setPos(0, 0);
- m_header->setModel(m_model);
- m_header->setVisibleRoles(m_visibleRoles);
- m_header->setVisibleRolesWidths(headerRolesWidths());
- m_header->setZValue(1);
+ m_headerWidget->setPos(0, 0);
+ m_headerWidget->resize(size().width(), headerSize.height());
+ m_headerWidget->setModel(m_model);
+ m_headerWidget->setColumns(m_visibleRoles);
+ m_headerWidget->setZValue(1);
- connect(m_header, SIGNAL(visibleRoleWidthChanged(QByteArray,qreal,qreal)),
- this, SLOT(slotVisibleRoleWidthChanged(QByteArray,qreal,qreal)));
- connect(m_header, SIGNAL(sortOrderChanged(Qt::SortOrder,Qt::SortOrder)),
+ connect(m_headerWidget, SIGNAL(columnWidthChanged(QByteArray,qreal,qreal)),
+ this, SLOT(slotHeaderColumnWidthChanged(QByteArray,qreal,qreal)));
+ connect(m_headerWidget, SIGNAL(columnMoved(QByteArray,int,int)),
+ this, SLOT(slotHeaderColumnMoved(QByteArray,int,int)));
+ connect(m_headerWidget, SIGNAL(sortOrderChanged(Qt::SortOrder,Qt::SortOrder)),
this, SIGNAL(sortOrderChanged(Qt::SortOrder,Qt::SortOrder)));
- connect(m_header, SIGNAL(sortRoleChanged(QByteArray,QByteArray)),
+ connect(m_headerWidget, SIGNAL(sortRoleChanged(QByteArray,QByteArray)),
this, SIGNAL(sortRoleChanged(QByteArray,QByteArray)));
- m_useHeaderWidths = false;
+ m_layouter->setHeaderHeight(headerSize.height());
+ m_headerWidget->setVisible(true);
+ } else if (!visible && m_headerWidget->isVisible()) {
+ disconnect(m_headerWidget, SIGNAL(columnWidthChanged(QByteArray,qreal,qreal)),
+ this, SLOT(slotHeaderColumnWidthChanged(QByteArray,qreal,qreal)));
+ disconnect(m_headerWidget, SIGNAL(columnMoved(QByteArray,int,int)),
+ this, SLOT(slotHeaderColumnMoved(QByteArray,int,int)));
+ disconnect(m_headerWidget, SIGNAL(sortOrderChanged(Qt::SortOrder,Qt::SortOrder)),
+ this, SIGNAL(sortOrderChanged(Qt::SortOrder,Qt::SortOrder)));
+ disconnect(m_headerWidget, SIGNAL(sortRoleChanged(QByteArray,QByteArray)),
+ this, SIGNAL(sortRoleChanged(QByteArray,QByteArray)));
- m_layouter->setHeaderHeight(m_header->size().height());
- } else if (!show && m_header) {
- delete m_header;
- m_header = 0;
- m_useHeaderWidths = false;
m_layouter->setHeaderHeight(0);
+ m_headerWidget->setVisible(false);
}
}
-bool KItemListView::isHeaderShown() const
+bool KItemListView::isHeaderVisible() const
+{
+ return m_headerWidget->isVisible();
+}
+
+KItemListHeader* KItemListView::header() const
{
- return m_header != 0;
+ return m_header;
}
QPixmap KItemListView::createDragPixmap(const QSet<int>& indexes) const
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);
+ if (m_itemSize.isEmpty()) {
+ updatePreferredColumnWidths(itemRanges);
+ }
const bool hasMultipleRanges = (itemRanges.count() > 1);
if (hasMultipleRanges) {
endTransaction();
updateSiblingsInformation();
}
+
+ if (useAlternateBackgrounds()) {
+ updateAlternateBackgrounds();
+ }
}
void KItemListView::slotItemsRemoved(const KItemRangeList& itemRanges)
{
- updateVisibleRolesSizes();
+ if (m_itemSize.isEmpty()) {
+ // Don't pass the item-range: The preferred column-widths of
+ // all items must be adjusted when removing items.
+ updatePreferredColumnWidths();
+ }
const bool hasMultipleRanges = (itemRanges.count() > 1);
if (hasMultipleRanges) {
m_layouter->markAsDirty();
+ int removedItemsCount = 0;
+ for (int i = 0; i < itemRanges.count(); ++i) {
+ removedItemsCount += itemRanges[i].count;
+ }
+
for (int i = itemRanges.count() - 1; i >= 0; --i) {
- const KItemRange& range = itemRanges.at(i);
+ const KItemRange& range = itemRanges[i];
const int index = range.index;
const int count = range.count;
if (index < 0 || count <= 0) {
const int firstRemovedIndex = index;
const int lastRemovedIndex = index + count - 1;
- const int lastIndex = m_model->count() + count - 1;
+ const int lastIndex = m_model->count() - 1 + removedItemsCount;
+ removedItemsCount -= count;
// Remove all KItemListWidget instances that got deleted
for (int i = firstRemovedIndex; i <= lastRemovedIndex; ++i) {
endTransaction();
updateSiblingsInformation();
}
+
+ if (useAlternateBackgrounds()) {
+ updateAlternateBackgrounds();
+ }
}
void KItemListView::slotItemsMoved(const KItemRange& itemRange, const QList<int>& movedToIndexes)
const QSet<QByteArray>& roles)
{
const bool updateSizeHints = itemSizeHintUpdateRequired(roles);
- if (updateSizeHints) {
- updateVisibleRolesSizes(itemRanges);
+ if (updateSizeHints && m_itemSize.isEmpty()) {
+ updatePreferredColumnWidths(itemRanges);
}
foreach (const KItemRange& itemRange, itemRanges) {
// 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());
- }
+ updateAlternateBackgrounds();
}
-
+ updateSiblingsInformation();
doLayout(NoAnimation);
}
update();
}
-void KItemListView::slotVisibleRoleWidthChanged(const QByteArray& role,
- qreal currentWidth,
- qreal previousWidth)
+void KItemListView::slotHeaderColumnWidthChanged(const QByteArray& role,
+ qreal currentWidth,
+ qreal previousWidth)
{
+ Q_UNUSED(role);
+ Q_UNUSED(currentWidth);
Q_UNUSED(previousWidth);
- m_useHeaderWidths = true;
+ m_headerWidget->setAutomaticColumnResizing(false);
+ applyColumnWidthsFromHeader();
+ doLayout(NoAnimation);
+}
- if (m_visibleRolesSizes.contains(role)) {
- QSizeF roleSize = m_visibleRolesSizes.value(role);
- roleSize.setWidth(currentWidth);
- m_visibleRolesSizes.insert(role, roleSize);
- m_stretchedVisibleRolesSizes.insert(role, roleSize);
+void KItemListView::slotHeaderColumnMoved(const QByteArray& role,
+ int currentIndex,
+ int previousIndex)
+{
+ Q_ASSERT(m_visibleRoles[previousIndex] == role);
- // Apply the new size to the layouter
- QSizeF dynamicItemSize = m_itemSize;
- if (dynamicItemSize.width() < 0) {
- const qreal requiredWidth = visibleRolesSizesWidthSum();
- dynamicItemSize.setWidth(qMax(size().width(), requiredWidth));
- }
- if (dynamicItemSize.height() < 0) {
- const qreal requiredHeight = visibleRolesSizesHeightSum();
- dynamicItemSize.setHeight(qMax(size().height(), requiredHeight));
- }
+ const QList<QByteArray> previous = m_visibleRoles;
- m_layouter->setItemSize(dynamicItemSize);
+ QList<QByteArray> current = m_visibleRoles;
+ current.removeAt(previousIndex);
+ current.insert(currentIndex, role);
- // Update the role sizes for all visible widgets
- QHashIterator<int, KItemListWidget*> it(m_visibleItems);
- while (it.hasNext()) {
- it.next();
- it.value()->setVisibleRolesSizes(m_stretchedVisibleRolesSizes);
- }
- doLayout(NoAnimation);
- }
+ setVisibleRoles(current);
+
+ emit visibleRolesChanged(current, previous);
}
void KItemListView::triggerAutoScrolling()
}
m_model = model;
- m_layouter->setModel(model);
+ m_layouter->setModel(model);
m_grouped = model->groupedSorting();
if (m_model) {
} 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
void KItemListView::updateWidgetProperties(KItemListWidget* widget, int index)
{
widget->setVisibleRoles(m_visibleRoles);
- widget->setVisibleRolesSizes(m_stretchedVisibleRolesSizes);
+ updateWidgetColumnWidths(widget);
widget->setStyleOption(m_styleOption);
const KItemListSelectionManager* selectionManager = m_controller->selectionManager();
return mid;
}
+void KItemListView::updateAlternateBackgrounds()
+{
+ QHashIterator<int, KItemListWidget*> it(m_visibleItems);
+ while (it.hasNext()) {
+ it.next();
+ updateAlternateBackgroundForWidget(it.value());
+ }
+}
+
void KItemListView::updateAlternateBackgroundForWidget(KItemListWidget* widget)
{
bool enabled = useAlternateBackgrounds();
const QList<QPair<int, QVariant> > groups = model()->groups();
const int indexOfFirstGroupItem = groups[groupIndex].first;
const int relativeIndex = index - indexOfFirstGroupItem;
- enabled = (relativeIndex & 0x1) == 0;
+ enabled = (relativeIndex & 0x1) > 0;
}
}
}
return m_itemSize.isEmpty() && m_visibleRoles.count() > 1;
}
-QHash<QByteArray, qreal> KItemListView::headerRolesWidths() const
+QHash<QByteArray, qreal> KItemListView::preferredColumnWidths(const KItemRangeList& itemRanges) const
+{
+ QElapsedTimer timer;
+ timer.start();
+
+ QHash<QByteArray, qreal> widths;
+
+ // Calculate the minimum width for each column that is required
+ // to show the headline unclipped.
+ const QFontMetricsF fontMetrics(m_headerWidget->font());
+ const int gripMargin = m_headerWidget->style()->pixelMetric(QStyle::PM_HeaderGripMargin);
+ const int headerMargin = m_headerWidget->style()->pixelMetric(QStyle::PM_HeaderMargin);
+ foreach (const QByteArray& visibleRole, visibleRoles()) {
+ const QString headerText = m_model->roleDescription(visibleRole);
+ const qreal headerWidth = fontMetrics.width(headerText) + gripMargin + headerMargin * 2;
+ widths.insert(visibleRole, headerWidth);
+ }
+
+ // Calculate the preferred column withs for each item and ignore values
+ // smaller than the width for showing the headline unclipped.
+ int calculatedItemCount = 0;
+ bool maxTimeExceeded = false;
+ foreach (const KItemRange& itemRange, itemRanges) {
+ const int startIndex = itemRange.index;
+ const int endIndex = startIndex + itemRange.count - 1;
+
+ for (int i = startIndex; i <= endIndex; ++i) {
+ foreach (const QByteArray& visibleRole, visibleRoles()) {
+ qreal maxWidth = widths.value(visibleRole, 0);
+ const qreal width = m_widgetCreator->preferredRoleColumnWidth(visibleRole, i, this);
+ maxWidth = qMax(width, maxWidth);
+ widths.insert(visibleRole, maxWidth);
+ }
+
+ if (calculatedItemCount > 100 && timer.elapsed() > 200) {
+ // When having several thousands of items calculating the sizes can get
+ // very expensive. We accept a possibly too small role-size in favour
+ // of having no blocking user interface.
+ maxTimeExceeded = true;
+ break;
+ }
+ ++calculatedItemCount;
+ }
+ if (maxTimeExceeded) {
+ break;
+ }
+ }
+
+ return widths;
+}
+
+void KItemListView::applyColumnWidthsFromHeader()
{
- QHash<QByteArray, qreal> rolesWidths;
+ // Apply the new size to the layouter
+ const qreal requiredWidth = columnWidthsSum();
+ const QSizeF dynamicItemSize(qMax(size().width(), requiredWidth),
+ m_itemSize.height());
+ m_layouter->setItemSize(dynamicItemSize);
- QHashIterator<QByteArray, QSizeF> it(m_stretchedVisibleRolesSizes);
+ // Update the role sizes for all visible widgets
+ QHashIterator<int, KItemListWidget*> it(m_visibleItems);
while (it.hasNext()) {
it.next();
- rolesWidths.insert(it.key(), it.value().width());
+ updateWidgetColumnWidths(it.value());
}
-
- return rolesWidths;
}
-void KItemListView::updateVisibleRolesSizes(const KItemRangeList& itemRanges)
+void KItemListView::updateWidgetColumnWidths(KItemListWidget* widget)
{
- if (!m_itemSize.isEmpty() || m_useHeaderWidths) {
- return;
+ foreach (const QByteArray& role, m_visibleRoles) {
+ widget->setColumnWidth(role, m_headerWidget->columnWidth(role));
}
+}
+void KItemListView::updatePreferredColumnWidths(const KItemRangeList& itemRanges)
+{
+ Q_ASSERT(m_itemSize.isEmpty());
const int itemCount = m_model->count();
int rangesItemCount = 0;
foreach (const KItemRange& range, itemRanges) {
}
if (itemCount == rangesItemCount) {
- m_visibleRolesSizes = visibleRolesSizes(itemRanges);
- if (m_header) {
- // Assure the the sizes are not smaller than the minimum defined by the header
- // TODO: Currently only implemented for a top-aligned header
- const qreal minHeaderRoleWidth = m_header->minimumRoleWidth();
- QMutableHashIterator<QByteArray, QSizeF> it (m_visibleRolesSizes);
- while (it.hasNext()) {
- it.next();
- const QSizeF& size = it.value();
- if (size.width() < minHeaderRoleWidth) {
- const QSizeF newSize(minHeaderRoleWidth, size.height());
- m_visibleRolesSizes.insert(it.key(), newSize);
- }
- }
+ const QHash<QByteArray, qreal> preferredWidths = preferredColumnWidths(itemRanges);
+ foreach (const QByteArray& role, m_visibleRoles) {
+ m_headerWidget->setPreferredColumnWidth(role, preferredWidths.value(role));
}
} else {
// Only a sub range of the roles need to be determined.
- // The chances are good that the sizes of the sub ranges
- // already fit into the available sizes and hence no
+ // The chances are good that the widths of the sub ranges
+ // already fit into the available widths and hence no
// expensive update might be required.
- bool updateRequired = false;
+ bool changed = false;
- const QHash<QByteArray, QSizeF> updatedSizes = visibleRolesSizes(itemRanges);
- QHashIterator<QByteArray, QSizeF> it(updatedSizes);
+ const QHash<QByteArray, qreal> updatedWidths = preferredColumnWidths(itemRanges);
+ QHashIterator<QByteArray, qreal> it(updatedWidths);
while (it.hasNext()) {
it.next();
const QByteArray& role = it.key();
- const QSizeF& updatedSize = it.value();
- const QSizeF currentSize = m_visibleRolesSizes.value(role);
- if (updatedSize.width() > currentSize.width() || updatedSize.height() > currentSize.height()) {
- m_visibleRolesSizes.insert(role, updatedSize);
- updateRequired = true;
+ const qreal updatedWidth = it.value();
+ const qreal currentWidth = m_headerWidget->preferredColumnWidth(role);
+ if (updatedWidth > currentWidth) {
+ m_headerWidget->setPreferredColumnWidth(role, updatedWidth);
+ changed = true;
}
}
- if (!updateRequired) {
+ if (!changed) {
// All the updated sizes are smaller than the current sizes and no change
// of the stretched roles-widths is required
return;
}
}
- updateStretchedVisibleRolesSizes();
+ if (m_headerWidget->automaticColumnResizing()) {
+ applyAutomaticColumnWidths();
+ }
}
-void KItemListView::updateVisibleRolesSizes()
+void KItemListView::updatePreferredColumnWidths()
{
- if (!m_model) {
- return;
- }
-
- const int itemCount = m_model->count();
- if (itemCount > 0) {
- updateVisibleRolesSizes(KItemRangeList() << KItemRange(0, itemCount));
+ if (m_model) {
+ updatePreferredColumnWidths(KItemRangeList() << KItemRange(0, m_model->count()));
}
}
-void KItemListView::updateStretchedVisibleRolesSizes()
+void KItemListView::applyAutomaticColumnWidths()
{
- if (!m_itemSize.isEmpty() || m_useHeaderWidths || m_visibleRoles.isEmpty()) {
- return;
- }
+ Q_ASSERT(m_itemSize.isEmpty());
+ Q_ASSERT(m_headerWidget->automaticColumnResizing());
// Calculate the maximum size of an item by considering the
// visible role sizes and apply them to the layouter. If the
- // size does not use the available view-size it the size of the
+ // size does not use the available view-size the size of the
// first role will get stretched.
- m_stretchedVisibleRolesSizes = m_visibleRolesSizes;
- const QByteArray role = m_visibleRoles.first();
- QSizeF firstRoleSize = m_stretchedVisibleRolesSizes.value(role);
+ foreach (const QByteArray& role, m_visibleRoles) {
+ const qreal preferredWidth = m_headerWidget->preferredColumnWidth(role);
+ m_headerWidget->setColumnWidth(role, preferredWidth);
+ }
+
+ const QByteArray firstRole = m_visibleRoles.first();
+ qreal firstColumnWidth = m_headerWidget->columnWidth(firstRole);
QSizeF dynamicItemSize = m_itemSize;
- if (dynamicItemSize.width() <= 0) {
- const qreal requiredWidth = visibleRolesSizesWidthSum();
- const qreal availableWidth = size().width();
- if (requiredWidth < availableWidth) {
- // Stretch the first role to use the whole width for the item
- firstRoleSize.rwidth() += availableWidth - requiredWidth;
- m_stretchedVisibleRolesSizes.insert(role, firstRoleSize);
+ qreal requiredWidth = columnWidthsSum();
+ const qreal availableWidth = size().width();
+ if (requiredWidth < availableWidth) {
+ // Stretch the first column to use the whole remaining width
+ firstColumnWidth += availableWidth - requiredWidth;
+ m_headerWidget->setColumnWidth(firstRole, firstColumnWidth);
+ } else if (requiredWidth > availableWidth) {
+ // Shrink the first column to be able to show as much other
+ // columns as possible
+ qreal shrinkedFirstColumnWidth = firstColumnWidth - requiredWidth + availableWidth;
+
+ // TODO: A proper calculation of the minimum width depends on the implementation
+ // of KItemListWidget. Probably a kind of minimum size-hint should be introduced
+ // later.
+ const qreal minWidth = qMin(firstColumnWidth, qreal(m_styleOption.iconSize * 2 + 200));
+ if (shrinkedFirstColumnWidth < minWidth) {
+ shrinkedFirstColumnWidth = minWidth;
}
- dynamicItemSize.setWidth(qMax(requiredWidth, availableWidth));
- }
- if (dynamicItemSize.height() <= 0) {
- const qreal requiredHeight = visibleRolesSizesHeightSum();
- const qreal availableHeight = size().height();
- if (requiredHeight < availableHeight) {
- // Stretch the first role to use the whole height for the item
- firstRoleSize.rheight() += availableHeight - requiredHeight;
- m_stretchedVisibleRolesSizes.insert(role, firstRoleSize);
- }
- dynamicItemSize.setHeight(qMax(requiredHeight, availableHeight));
+ m_headerWidget->setColumnWidth(firstRole, shrinkedFirstColumnWidth);
+ requiredWidth -= firstColumnWidth - shrinkedFirstColumnWidth;
}
- m_layouter->setItemSize(dynamicItemSize);
+ dynamicItemSize.rwidth() = qMax(requiredWidth, availableWidth);
- if (m_header) {
- m_header->setVisibleRolesWidths(headerRolesWidths());
- m_header->resize(dynamicItemSize.width(), m_header->size().height());
- }
+ m_layouter->setItemSize(dynamicItemSize);
// Update the role sizes for all visible widgets
QHashIterator<int, KItemListWidget*> it(m_visibleItems);
while (it.hasNext()) {
it.next();
- it.value()->setVisibleRolesSizes(m_stretchedVisibleRolesSizes);
- }
-}
-
-qreal KItemListView::visibleRolesSizesWidthSum() const
-{
- qreal widthSum = 0;
- QHashIterator<QByteArray, QSizeF> it(m_visibleRolesSizes);
- while (it.hasNext()) {
- it.next();
- widthSum += it.value().width();
+ updateWidgetColumnWidths(it.value());
}
- return widthSum;
}
-qreal KItemListView::visibleRolesSizesHeightSum() const
+qreal KItemListView::columnWidthsSum() const
{
- qreal heightSum = 0;
- QHashIterator<QByteArray, QSizeF> it(m_visibleRolesSizes);
- while (it.hasNext()) {
- it.next();
- heightSum += it.value().height();
+ qreal widthsSum = 0;
+ foreach (const QByteArray& role, m_visibleRoles) {
+ widthsSum += m_headerWidget->columnWidth(role);
}
- return heightSum;
+ return widthsSum;
}
QRectF KItemListView::headerBoundaries() const
{
- return m_header ? m_header->geometry() : QRectF();
+ return m_headerWidget->isVisible() ? m_headerWidget->geometry() : QRectF();
}
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);