From edda24eb851c2647f7dde01885008ef60fcadd9a Mon Sep 17 00:00:00 2001 From: Victor Blanchard Date: Wed, 28 May 2025 12:33:52 +0200 Subject: dolphinview: Add a dynamic view option Added a 'dynamic view' option, which allows to switch from a 'compact' or 'details' view to an 'icons' view if most of the files in the directory are images or videos. It reverts to the previous view mode when we switch to a directory which doesn't meet that criteria. The view mode is only changed once so users don't have to undo that for specific folders when they don't want icon view. A setting is added in the "Display style" section of the general view setting page. BUG: 491139 --- src/views/dolphinview.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++ src/views/dolphinview.h | 5 +++++ src/views/viewproperties.cpp | 13 +++++++++++++ src/views/viewproperties.h | 3 +++ 4 files changed, 67 insertions(+) (limited to 'src/views') diff --git a/src/views/dolphinview.cpp b/src/views/dolphinview.cpp index bff6e7586..50c014c6a 100644 --- a/src/views/dolphinview.cpp +++ b/src/views/dolphinview.cpp @@ -1945,6 +1945,7 @@ void DolphinView::slotDirectoryLoadingCompleted() Q_EMIT directoryLoadingCompleted(); + applyDynamicView(); updatePlaceholderLabel(); updateWritableState(); } @@ -2213,6 +2214,51 @@ void DolphinView::applyModeToView() } } +void DolphinView::applyDynamicView() +{ + ViewProperties props(viewPropertiesUrl()); + /* return early if: + * - dynamic view is not enabled + * - the current view mode is already Icon View + * - dynamic view has previously changed the view mode + */ + if (!GeneralSettings::dynamicView() || m_mode == IconsView || props.dynamicViewPassed()) { + return; + } + + uint imageAndVideoCount = 0; + uint checkedItems = 0; + const uint totalItems = itemsCount(); + const KFileItemList itemList = items(); + bool applyDynamicView = false; + + for (const auto &file : itemList) { + ++checkedItems; + const QString type = file.mimetype().slice(0, 5); + + if (type == "image" || type == "video") { + ++imageAndVideoCount; + // if 2/3 or more of the items are images/videos, dynamic view should be applied + applyDynamicView = imageAndVideoCount >= (totalItems * 2 / 3); + if (applyDynamicView) { + break; + } + } else if (checkedItems - imageAndVideoCount > totalItems / 3) { + // if more than a third of the checked files are not media files, return + return; + } + } + + if (!applyDynamicView) { + return; + } + + props.setAutoSaveEnabled(!GeneralSettings::globalViewProps()); + props.setDynamicViewPassed(true); + props.setViewMode(IconsView); + applyViewProperties(props); +} + void DolphinView::pasteToUrl(const QUrl &url) { KIO::PasteJob *job = KIO::paste(QApplication::clipboard()->mimeData(), url); diff --git a/src/views/dolphinview.h b/src/views/dolphinview.h index de4bc1af2..f491b6dd5 100644 --- a/src/views/dolphinview.h +++ b/src/views/dolphinview.h @@ -867,6 +867,11 @@ private: */ void applyModeToView(); + /** + * Changes the current view based on the content of the directory. + */ + void applyDynamicView(); + enum Selection { HasSelection, NoSelection }; /** * Helper method for DolphinView::requestStatusBarText(). diff --git a/src/views/viewproperties.cpp b/src/views/viewproperties.cpp index 5dbdd938e..8e09009e5 100644 --- a/src/views/viewproperties.cpp +++ b/src/views/viewproperties.cpp @@ -369,6 +369,19 @@ bool ViewProperties::sortHiddenLast() const return m_node->sortHiddenLast(); } +void ViewProperties::setDynamicViewPassed(bool dynamicViewPassed) +{ + if (m_node->dynamicViewPassed() != dynamicViewPassed) { + m_node->setDynamicViewPassed(dynamicViewPassed); + update(); + } +} + +bool ViewProperties::dynamicViewPassed() const +{ + return m_node->dynamicViewPassed(); +} + void ViewProperties::setVisibleRoles(const QList &roles) { if (roles == visibleRoles()) { diff --git a/src/views/viewproperties.h b/src/views/viewproperties.h index 44c703482..bee1e7330 100644 --- a/src/views/viewproperties.h +++ b/src/views/viewproperties.h @@ -65,6 +65,9 @@ public: void setSortHiddenLast(bool hiddenLast); bool sortHiddenLast() const; + void setDynamicViewPassed(bool dynamicViewPassed); + bool dynamicViewPassed() const; + /** * Sets the additional information for the current set view-mode. * Note that the additional-info property is the only property where -- cgit v1.3