From: Peter Penz Date: Fri, 23 Sep 2011 21:37:42 +0000 (+0200) Subject: Increase the width of the first column automatically X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/commitdiff_plain/9ea4d3c70c3765c1ef417642a392465caa31997c Increase the width of the first column automatically Additionally it is assured that on role-changes the size-hints get updated if necessary. --- diff --git a/src/kitemviews/kfileitemlistview.cpp b/src/kitemviews/kfileitemlistview.cpp index 6ff83b793..48e7306fc 100644 --- a/src/kitemviews/kfileitemlistview.cpp +++ b/src/kitemviews/kfileitemlistview.cpp @@ -194,6 +194,24 @@ QHash KFileItemListView::visibleRoleSizes() const } } + // Stretch the width of the first role so that the full visible view-width + // is used to show all roles. + const qreal availableWidth = size().width(); + + qreal usedWidth = 0; + QHashIterator it(sizes); + while (it.hasNext()) { + it.next(); + usedWidth += it.value().width(); + } + + if (usedWidth < availableWidth) { + const QByteArray role = visibleRoles().first(); + QSizeF firstRoleSize = sizes.value(role); + firstRoleSize.rwidth() += availableWidth - usedWidth; + sizes.insert(role, firstRoleSize); + } + #ifdef KFILEITEMLISTVIEW_DEBUG kDebug() << "[TIME] Calculated dynamic item size for " << itemCount << "items:" << timer.elapsed(); #endif @@ -232,6 +250,21 @@ void KFileItemListView::initializeItemListWidget(KItemListWidget* item) } } +bool KFileItemListView::itemSizeHintUpdateRequired(const QSet& changedRoles) const +{ + // Even if the icons have a different size they are always aligned within + // the area defined by KItemStyleOption.iconSize and hence result in no + // change of the item-size. + const bool containsIconName = changedRoles.contains("iconName"); + const bool containsIconPixmap = changedRoles.contains("iconPixmap"); + const int count = changedRoles.count(); + + const bool iconChanged = (containsIconName && containsIconPixmap && count == 2) || + (containsIconName && count == 1) || + (containsIconPixmap && count == 1); + return !iconChanged; +} + void KFileItemListView::onModelChanged(KItemModelBase* current, KItemModelBase* previous) { Q_UNUSED(previous); @@ -320,6 +353,7 @@ void KFileItemListView::resizeEvent(QGraphicsSceneResizeEvent* event) { KItemListView::resizeEvent(event); triggerVisibleIndexRangeUpdate(); + markVisibleRolesSizesAsDirty(); } void KFileItemListView::slotItemsRemoved(const KItemRangeList& itemRanges) diff --git a/src/kitemviews/kfileitemlistview.h b/src/kitemviews/kfileitemlistview.h index 0f0c5e85a..77bfa21ed 100644 --- a/src/kitemviews/kfileitemlistview.h +++ b/src/kitemviews/kfileitemlistview.h @@ -68,6 +68,7 @@ public: protected: virtual void initializeItemListWidget(KItemListWidget* item); + virtual bool itemSizeHintUpdateRequired(const QSet& changedRoles) const; virtual void onModelChanged(KItemModelBase* current, KItemModelBase* previous); virtual void onScrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous); virtual void onItemSizeChanged(const QSizeF& current, const QSizeF& previous); diff --git a/src/kitemviews/kitemlistview.cpp b/src/kitemviews/kitemlistview.cpp index ef429e544..f15d1ed9d 100644 --- a/src/kitemviews/kitemlistview.cpp +++ b/src/kitemviews/kitemlistview.cpp @@ -204,6 +204,7 @@ void KItemListView::setVisibleRoles(const QList& roles) if (m_header) { m_header->setVisibleRoles(roles); m_header->setVisibleRolesWidths(headerRolesWidths()); + m_useHeaderWidths = false; } } @@ -463,6 +464,12 @@ void KItemListView::initializeItemListWidget(KItemListWidget* item) Q_UNUSED(item); } +bool KItemListView::itemSizeHintUpdateRequired(const QSet& changedRoles) const +{ + Q_UNUSED(changedRoles); + return true; +} + void KItemListView::onControllerChanged(KItemListController* current, KItemListController* previous) { Q_UNUSED(current); @@ -578,11 +585,19 @@ void KItemListView::resizeEvent(QGraphicsSceneResizeEvent* event) updateHeaderWidth(); } -void KItemListView::slotItemsInserted(const KItemRangeList& itemRanges) +bool KItemListView::markVisibleRolesSizesAsDirty() { - if (!m_useHeaderWidths) { - markVisibleRolesSizesAsDirty(); + const bool dirty = m_itemSize.isEmpty(); + if (dirty && !m_useHeaderWidths) { + m_visibleRolesSizes.clear(); + m_layouter->setItemSize(QSizeF()); } + return dirty; +} + +void KItemListView::slotItemsInserted(const KItemRangeList& itemRanges) +{ + markVisibleRolesSizesAsDirty(); const bool hasMultipleRanges = (itemRanges.count() > 1); if (hasMultipleRanges) { @@ -655,9 +670,7 @@ void KItemListView::slotItemsInserted(const KItemRangeList& itemRanges) void KItemListView::slotItemsRemoved(const KItemRangeList& itemRanges) { - if (!m_useHeaderWidths) { - markVisibleRolesSizesAsDirty(); - } + markVisibleRolesSizesAsDirty(); const bool hasMultipleRanges = (itemRanges.count() > 1); if (hasMultipleRanges) { @@ -739,12 +752,22 @@ void KItemListView::slotItemsRemoved(const KItemRangeList& itemRanges) void KItemListView::slotItemsChanged(const KItemRangeList& itemRanges, const QSet& roles) { + const bool updateSizeHints = itemSizeHintUpdateRequired(roles); + if (updateSizeHints) { + markVisibleRolesSizesAsDirty(); + } + foreach (const KItemRange& itemRange, itemRanges) { const int index = itemRange.index; const int count = itemRange.count; - m_sizeHintResolver->itemsChanged(index, count, roles); + if (updateSizeHints) { + m_sizeHintResolver->itemsChanged(index, count, roles); + m_layouter->markAsDirty(); + updateLayout(); + } + // Apply the changed roles to the visible item-widgets const int lastIndex = index + count - 1; for (int i = index; i <= lastIndex; ++i) { KItemListWidget* widget = m_visibleItems.value(i); @@ -752,6 +775,7 @@ void KItemListView::slotItemsChanged(const KItemRangeList& itemRanges, widget->setData(m_model->data(i), roles); } } + } } @@ -884,7 +908,6 @@ void KItemListView::slotVisibleRoleWidthChanged(const QByteArray& role, qreal previousWidth) { Q_UNUSED(previousWidth); - Q_ASSERT(m_header); m_useHeaderWidths = true; @@ -1292,17 +1315,6 @@ void KItemListView::setLayouterSize(const QSizeF& size, SizeType sizeType) } } -bool KItemListView::markVisibleRolesSizesAsDirty() -{ - const bool dirty = m_itemSize.isEmpty(); - if (dirty) { - m_visibleRolesSizes.clear(); - m_layouter->setItemSize(QSizeF()); - m_useHeaderWidths = false; - } - return dirty; -} - void KItemListView::applyDynamicItemSize() { if (!m_itemSize.isEmpty()) { diff --git a/src/kitemviews/kitemlistview.h b/src/kitemviews/kitemlistview.h index b088ea487..75a298a1e 100644 --- a/src/kitemviews/kitemlistview.h +++ b/src/kitemviews/kitemlistview.h @@ -146,6 +146,12 @@ public: int firstVisibleIndex() const; int lastVisibleIndex() const; + /** + * @return Required size for the item with the index \p index. + * Per default KItemListView::itemSize() is returned. + * When reimplementing this method it is recommended to + * also reimplement KItemListView::itemSizeHintUpdateRequired(). + */ virtual QSizeF itemSizeHint(int index) const; /** @@ -194,6 +200,16 @@ signals: protected: virtual void initializeItemListWidget(KItemListWidget* item); + /** + * @return True if at least one of the changed roles \p changedRoles might result + * in the need to update the item-size hint (see KItemListView::itemSizeHint()). + * Per default true is returned which means on each role-change of existing items + * the item-size hints are recalculated. For performance reasons it is recommended + * to return false in case if a role-change will not result in a changed + * item-size hint. + */ + virtual bool itemSizeHintUpdateRequired(const QSet& changedRoles) const; + virtual void onControllerChanged(KItemListController* current, KItemListController* previous); virtual void onModelChanged(KItemModelBase* current, KItemModelBase* previous); @@ -216,6 +232,16 @@ protected: QList visibleItemListWidgets() const; + /** + * Marks the visible roles as dirty so that they will get updated when doing the next + * layout. The visible roles will only get marked as dirty if an empty item-size is + * given and if the roles have not already been customized by the user by adjusting + * the view-header. + * @return True if the visible roles have been marked as dirty. + */ + bool markVisibleRolesSizesAsDirty(); + + /** @reimp */ virtual void resizeEvent(QGraphicsSceneResizeEvent* event); protected slots: @@ -294,14 +320,6 @@ private: */ void setLayouterSize(const QSizeF& size, SizeType sizeType); - /** - * Marks the visible roles as dirty so that they will get updated when doing the next - * layout. The visible roles will only get marked as dirty if an empty item-size is - * given. - * @return True if the visible roles have been marked as dirty. - */ - bool markVisibleRolesSizesAsDirty(); - /** * Updates the m_visibleRoleSizes property and applies the dynamic * size to the layouter.