]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Increase the width of the first column automatically
authorPeter Penz <peter.penz19@gmail.com>
Fri, 23 Sep 2011 21:37:42 +0000 (23:37 +0200)
committerPeter Penz <peter.penz19@gmail.com>
Fri, 23 Sep 2011 21:38:51 +0000 (23:38 +0200)
Additionally it is assured that on role-changes the size-hints
get updated if necessary.

src/kitemviews/kfileitemlistview.cpp
src/kitemviews/kfileitemlistview.h
src/kitemviews/kitemlistview.cpp
src/kitemviews/kitemlistview.h

index 6ff83b79331998406d616e1446571a152d3f1426..48e7306fccd903999d4565e041f0cc4c4792c5c5 100644 (file)
@@ -194,6 +194,24 @@ QHash<QByteArray, QSizeF> 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<QByteArray, QSizeF> 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<QByteArray>& 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)
index 0f0c5e85a52a2f13a3de5bd517b48f93f1a2f4cf..77bfa21ed3068cad69ff41dfdcbe1a4527a2aa41 100644 (file)
@@ -68,6 +68,7 @@ public:
 
 protected:
     virtual void initializeItemListWidget(KItemListWidget* item);
+    virtual bool itemSizeHintUpdateRequired(const QSet<QByteArray>& 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);
index ef429e544e82ce689523c32e01f7f3115cf923db..f15d1ed9d5ffb390c8b160316ec349c2cdd691ab 100644 (file)
@@ -204,6 +204,7 @@ void KItemListView::setVisibleRoles(const QList<QByteArray>& 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<QByteArray>& 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<QByteArray>& 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()) {
index b088ea4873ca0f2271a5e053c24c37a103c774b7..75a298a1e79c0fd33f3490b592ac770f05e5ef64 100644 (file)
@@ -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<QByteArray>& changedRoles) const;
+
     virtual void onControllerChanged(KItemListController* current, KItemListController* previous);
     virtual void onModelChanged(KItemModelBase* current, KItemModelBase* previous);
 
@@ -216,6 +232,16 @@ protected:
 
     QList<KItemListWidget*> 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.