# HG changeset patch # User Markus Mützel # Date 1708102869 -3600 # Fri Feb 16 18:01:09 2024 +0100 # Node ID 8af2de066a1de58988ca40228b3f315b17ae0c54 # Parent 7de59b26cf79af92668a3b573ccc54feb4006805 GUI: Cache file icons for file browser (bug #65312). diff -r 7de59b26cf79 -r 8af2de066a1d libgui/src/files-dock-widget.cc --- a/libgui/src/files-dock-widget.cc Fri Feb 16 03:09:40 2024 -0500 +++ b/libgui/src/files-dock-widget.cc Fri Feb 16 18:01:09 2024 +0100 @@ -27,6 +27,8 @@ # include "config.h" #endif +#include + #include #include #include @@ -39,6 +41,8 @@ #include #include #include +#include +#include #include #include #include @@ -177,6 +181,56 @@ } }; +class cache_file_icon_provider : public QFileIconProvider +{ +public: + cache_file_icon_provider () + { + m_null_icon = QIcon (); + m_file_icon = m_file_icon_provider.icon (QFileIconProvider::File); + m_folder_icon = m_file_icon_provider.icon (QFileIconProvider::Folder); + } + + QIcon icon (IconType ict) const + { + if (ict == QFileIconProvider::File) + return m_file_icon; + else if (ict == QFileIconProvider::Folder) + return m_folder_icon; + else + return m_null_icon; + } + + QIcon icon (const QFileInfo &fi) const + { + static bool no_platform_theme = QIcon::themeName ().isEmpty (); + + if (no_platform_theme) + return m_file_icon_provider.icon (fi); + + QMimeType mime_type = m_db.mimeTypeForFile (fi.absoluteFilePath ()); + QString icon_name = mime_type.iconName (); + auto mime_type_iter = m_icon_cache.find (icon_name); + if (mime_type_iter != m_icon_cache.end ()) + return mime_type_iter->second; + + QIcon icon = QIcon::fromTheme (icon_name); + m_icon_cache.insert ({icon_name, icon}); + return icon; + } + +private: + QIcon m_null_icon; + QIcon m_file_icon; + QIcon m_folder_icon; + + QFileIconProvider m_file_icon_provider; + QMimeDatabase m_db; + static std::map m_icon_cache; +}; + +std::map cache_file_icon_provider::m_icon_cache; + files_dock_widget::files_dock_widget (QWidget *p) : octave_dock_widget ("FilesDockWidget", p) { @@ -315,6 +369,9 @@ QModelIndex rootPathIndex = m_file_system_model->setRootPath (startup_dir.absolutePath ()); + m_file_icon_provider = new cache_file_icon_provider (); + m_file_system_model->setIconProvider (m_file_icon_provider); + // Attach the model to the QTreeView and set the root index m_file_tree_view = new FileTreeViewer (container); m_file_tree_view->setSelectionMode (QAbstractItemView::ExtendedSelection); diff -r 7de59b26cf79 -r 8af2de066a1d libgui/src/files-dock-widget.h --- a/libgui/src/files-dock-widget.h Fri Feb 16 03:09:40 2024 -0500 +++ b/libgui/src/files-dock-widget.h Fri Feb 16 18:01:09 2024 +0100 @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -210,6 +211,7 @@ //! The file system model. QFileSystemModel *m_file_system_model; + QFileIconProvider *m_file_icon_provider; //! The file system view. //!@{