]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/kfileitemlistview.cpp
Details view: Allow to turn off expandable folders like in Dolphin 1.7
[dolphin.git] / src / kitemviews / kfileitemlistview.cpp
index 86db28d3d1b26d2d0864d888e8e5bbf4b90bdd65..796a45099cd4818e6f3b50b6c5e9638dac02d62f 100644 (file)
@@ -97,7 +97,14 @@ bool KFileItemListView::previewsShown() const
 void KFileItemListView::setItemLayout(Layout layout)
 {
     if (m_itemLayout != layout) {
+        const bool updateRoles = (m_itemLayout == DetailsLayout || layout == DetailsLayout);
         m_itemLayout = layout;
+        if (updateRoles) {
+            // The details-layout requires some invisible roles that
+            // must be added to the model if the new layout is "details".
+            // If the old layout was "details" the roles will get removed.
+            applyRolesToModel();
+        }
         updateLayoutOfVisibleItems();
     }
 }
@@ -129,7 +136,7 @@ QSizeF KFileItemListView::itemSizeHint(int index) const
     case IconsLayout: {
         const QString text = KStringHandler::preProcessWrap(values["name"].toString());
 
-        const qreal maxWidth = itemSize().width() - 2 * option.margin;
+        const qreal maxWidth = itemSize().width() - 2 * option.padding;
         int textLinesCount = 0;
         QTextLine line;
 
@@ -152,7 +159,7 @@ QSizeF KFileItemListView::itemSizeHint(int index) const
 
         const qreal height = textLinesCount * option.fontMetrics.height() +
                              option.iconSize +
-                             option.margin * 4;
+                             option.padding * 3;
         return QSizeF(itemSize().width(), height);
     }
 
@@ -167,14 +174,14 @@ QSizeF KFileItemListView::itemSizeHint(int index) const
             maximumRequiredWidth = qMax(maximumRequiredWidth, requiredWidth);
         }
 
-        const qreal width = option.margin * 4 + option.iconSize + maximumRequiredWidth;
-        const qreal height = option.margin * 2 + qMax(option.iconSize, (1 + additionalRolesCount) * option.fontMetrics.height());
+        const qreal width = option.padding * 4 + option.iconSize + maximumRequiredWidth;
+        const qreal height = option.padding * 2 + qMax(option.iconSize, (1 + additionalRolesCount) * option.fontMetrics.height());
         return QSizeF(width, height);
     }
 
     case DetailsLayout: {
         // The width will be determined dynamically by KFileItemListView::visibleRoleSizes()
-        const qreal height = option.margin * 2 + qMax(option.iconSize, option.fontMetrics.height());
+        const qreal height = option.padding * 2 + qMax(option.iconSize, option.fontMetrics.height());
         return QSizeF(-1, height);
     }
 
@@ -234,11 +241,6 @@ QHash<QByteArray, QSizeF> KFileItemListView::visibleRolesSizes(const KItemRangeL
     return sizes;
 }
 
-bool KFileItemListView::supportsItemExpanding() const
-{
-    return m_itemLayout == DetailsLayout;
-}
-
 QPixmap KFileItemListView::createDragPixmap(const QSet<int>& indexes) const
 {
     if (!model()) {
@@ -321,8 +323,7 @@ void KFileItemListView::initializeItemListWidget(KItemListWidget* item)
     default:            Q_ASSERT(false); break;
     }
 
-    fileItemListWidget->setAlternatingBackgroundColors(m_itemLayout == DetailsLayout &&
-                                                       visibleRoles().count() > 1);
+    fileItemListWidget->setSupportsItemExpanding(supportsItemExpanding());
 }
 
 bool KFileItemListView::itemSizeHintUpdateRequired(const QSet<QByteArray>& changedRoles) const
@@ -347,8 +348,7 @@ void KFileItemListView::onModelChanged(KItemModelBase* current, KItemModelBase*
 
     delete m_modelRolesUpdater;
     m_modelRolesUpdater = new KFileItemModelRolesUpdater(static_cast<KFileItemModel*>(current), this);
-    const int size = styleOption().iconSize;
-    m_modelRolesUpdater->setIconSize(QSize(size, size));
+    m_modelRolesUpdater->setIconSize(availableIconSize());
 
     applyRolesToModel();
 }
@@ -379,19 +379,6 @@ void KFileItemListView::onVisibleRolesChanged(const QList<QByteArray>& current,
     Q_UNUSED(current);
     Q_UNUSED(previous);
     applyRolesToModel();
-
-    if (m_itemLayout == DetailsLayout) {
-        // Only enable the alternating background colors if more than one role
-        // is visible
-        const int previousCount = previous.count();
-        const int currentCount = current.count();
-        if ((previousCount <= 1 && currentCount > 1) || (previousCount > 1 && currentCount <= 1)) {
-            const bool enabled = (currentCount > 1);
-            foreach (KItemListWidget* widget, visibleItemListWidgets()) {
-                widget->setAlternatingBackgroundColors(enabled);
-            }
-        }
-    }
 }
 
 void KFileItemListView::onStyleOptionChanged(const KItemListStyleOption& current, const KItemListStyleOption& previous)
@@ -401,6 +388,13 @@ void KFileItemListView::onStyleOptionChanged(const KItemListStyleOption& current
     triggerIconSizeUpdate();
 }
 
+void KFileItemListView::onSupportsItemExpandingChanged(bool supportsExpanding)
+{
+    Q_UNUSED(supportsExpanding);
+    applyRolesToModel();
+    updateLayoutOfVisibleItems();
+}
+
 void KFileItemListView::onTransactionBegin()
 {
     m_modelRolesUpdater->setPaused(true);
@@ -464,8 +458,7 @@ void KFileItemListView::updateVisibleIndexRange()
         // of the icon-size before unpausing m_modelRolesUpdater. This prevents
         // an unnecessary expensive recreation of all previews afterwards.
         m_updateIconSizeTimer->stop();
-        const KItemListStyleOption& option = styleOption();
-        m_modelRolesUpdater->setIconSize(QSize(option.iconSize, option.iconSize));
+        m_modelRolesUpdater->setIconSize(availableIconSize());
     }
 
     m_modelRolesUpdater->setPaused(isTransactionActive());
@@ -487,8 +480,7 @@ void KFileItemListView::updateIconSize()
         return;
     }
 
-    const KItemListStyleOption& option = styleOption();
-    m_modelRolesUpdater->setIconSize(QSize(option.iconSize, option.iconSize));
+    m_modelRolesUpdater->setIconSize(availableIconSize());
 
     if (m_updateVisibleIndexRangeTimer->isActive()) {
         // If the visibility-index-range update is pending do an immediate update
@@ -509,22 +501,22 @@ QSizeF KFileItemListView::visibleRoleSizeHint(int index, const QByteArray& role)
     const KItemListStyleOption& option = styleOption();
 
     qreal width = m_minimumRolesWidths.value(role, 0);
-    const qreal height = option.margin * 2 + option.fontMetrics.height();
+    const qreal height = option.padding * 2 + option.fontMetrics.height();
 
     const QHash<QByteArray, QVariant> values = model()->data(index);
     const QString text = KFileItemListWidget::roleText(role, values);
     if (!text.isEmpty()) {
-        const qreal columnMargin = option.margin * 3;
-        width = qMax(width, qreal(2 * columnMargin + option.fontMetrics.width(text)));
+        const qreal columnPadding = option.padding * 3;
+        width = qMax(width, qreal(2 * columnPadding + option.fontMetrics.width(text)));
     }
 
     if (role == "name") {
         // Increase the width by the expansion-toggle and the current expansion level
-        const int expansionLevel = values.value("expansionLevel", 0).toInt();
-        width += option.margin + expansionLevel * itemSize().height() + KIconLoader::SizeSmall;
+        const int expandedParentsCount = values.value("expandedParentsCount", 0).toInt();
+        width += option.padding + expandedParentsCount * itemSize().height() + KIconLoader::SizeSmall;
 
         // Increase the width by the required space for the icon
-        width += option.margin * 2 + option.iconSize;
+        width += option.padding * 2 + option.iconSize;
     }
 
     return QSizeF(width, height);
@@ -584,10 +576,10 @@ void KFileItemListView::applyRolesToModel()
     roles.insert("iconName");
     roles.insert("name");
     roles.insert("isDir");
-    if (m_itemLayout == DetailsLayout) {
+    if (supportsItemExpanding()) {
         roles.insert("isExpanded");
         roles.insert("isExpandable");
-        roles.insert("expansionLevel");
+        roles.insert("expandedParentsCount");
     }
 
     // Assure that the role that is used for sorting will be determined
@@ -597,4 +589,16 @@ void KFileItemListView::applyRolesToModel()
     m_modelRolesUpdater->setRoles(roles);
 }
 
+QSize KFileItemListView::availableIconSize() const
+{
+    const KItemListStyleOption& option = styleOption();
+    const int iconSize = option.iconSize;
+    if (m_itemLayout == IconsLayout) {
+        const int maxIconWidth = itemSize().width() - 2 * option.padding;
+        return QSize(maxIconWidth, iconSize);
+    }
+
+    return QSize(iconSize, iconSize);
+}
+
 #include "kfileitemlistview.moc"