]> 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 a7fa27f0d75315e2bb5ff5801aa0d08200c1464d..796a45099cd4818e6f3b50b6c5e9638dac02d62f 100644 (file)
@@ -34,7 +34,7 @@
 #include <QTextLine>
 #include <QTimer>
 
-#define KFILEITEMLISTVIEW_DEBUG
+// #define KFILEITEMLISTVIEW_DEBUG
 
 namespace {
     const int ShortInterval = 50;
@@ -65,6 +65,8 @@ KFileItemListView::KFileItemListView(QGraphicsWidget* parent) :
     m_updateIconSizeTimer->setInterval(ShortInterval);
     connect(m_updateIconSizeTimer, SIGNAL(timeout()), this, SLOT(updateIconSize()));
 
+    setVisibleRoles(QList<QByteArray>() << "name");
+
     updateMinimumRolesWidths();
 }
 
@@ -95,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();
     }
 }
@@ -105,6 +114,18 @@ KFileItemListView::Layout KFileItemListView::itemLayout() const
     return m_itemLayout;
 }
 
+void KFileItemListView::setEnabledPlugins(const QStringList& list)
+{
+    if (m_modelRolesUpdater) {
+        m_modelRolesUpdater->setEnabledPlugins(list);
+    }
+}
+
+QStringList KFileItemListView::enabledPlugins() const
+{
+    return m_modelRolesUpdater ? m_modelRolesUpdater->enabledPlugins() : QStringList();
+}
+
 QSizeF KFileItemListView::itemSizeHint(int index) const
 {
     const QHash<QByteArray, QVariant> values = model()->data(index);
@@ -115,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;
 
@@ -138,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);
     }
 
@@ -153,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);
     }
 
@@ -302,7 +323,7 @@ void KFileItemListView::initializeItemListWidget(KItemListWidget* item)
     default:            Q_ASSERT(false); break;
     }
 
-    fileItemListWidget->setAlternatingBackgroundColors(m_itemLayout == DetailsLayout);
+    fileItemListWidget->setSupportsItemExpanding(supportsItemExpanding());
 }
 
 bool KFileItemListView::itemSizeHintUpdateRequired(const QSet<QByteArray>& changedRoles) const
@@ -325,13 +346,11 @@ void KFileItemListView::onModelChanged(KItemModelBase* current, KItemModelBase*
     Q_UNUSED(previous);
     Q_ASSERT(qobject_cast<KFileItemModel*>(current));
 
-    if (m_modelRolesUpdater) {
-        delete m_modelRolesUpdater;
-    }
-
+    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();
 }
 
 void KFileItemListView::onScrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous)
@@ -369,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);
@@ -410,6 +436,9 @@ void KFileItemListView::slotSortRoleChanged(const QByteArray& current, const QBy
 
 void KFileItemListView::triggerVisibleIndexRangeUpdate()
 {
+    if (!model()) {
+        return;
+    }
     m_modelRolesUpdater->setPaused(true);
     m_updateVisibleIndexRangeTimer->start();
 }
@@ -429,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());
@@ -439,6 +467,9 @@ void KFileItemListView::updateVisibleIndexRange()
 
 void KFileItemListView::triggerIconSizeUpdate()
 {
+    if (!model()) {
+        return;
+    }
     m_modelRolesUpdater->setPaused(true);
     m_updateIconSizeTimer->start();
 }
@@ -449,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
@@ -471,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);
@@ -494,6 +524,10 @@ QSizeF KFileItemListView::visibleRoleSizeHint(int index, const QByteArray& role)
 
 void KFileItemListView::updateLayoutOfVisibleItems()
 {
+    if (!model()) {
+        return;
+    }
+
     foreach (KItemListWidget* widget, visibleItemListWidgets()) {
         initializeItemListWidget(widget);
     }
@@ -528,6 +562,10 @@ void KFileItemListView::updateMinimumRolesWidths()
 
 void KFileItemListView::applyRolesToModel()
 {
+    if (!model()) {
+        return;
+    }
+
     Q_ASSERT(qobject_cast<KFileItemModel*>(model()));
     KFileItemModel* fileItemModel = static_cast<KFileItemModel*>(model());
 
@@ -538,9 +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("expansionLevel");
+        roles.insert("isExpandable");
+        roles.insert("expandedParentsCount");
     }
 
     // Assure that the role that is used for sorting will be determined
@@ -550,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"