From ba579114b3bba6fe07cf24ba22192b4b2de7e3ee Mon Sep 17 00:00:00 2001 From: Murray Stevenson <50844517+murraystevenson@users.noreply.github.com> Date: Thu, 22 Aug 2024 16:21:04 -0700 Subject: [PATCH] PathColumn : Allow sorting of FileIconPathColumn Sorting by file extension is an approximation of sorting by file type, but is likely enough for our purposes. `std::filesystem::path::extension()` returns the last part of the filename from the rightmost `.`, which means "foo.tar.gz" will sort with "foo.gz", but this is preferable to a more greedy approach that would treat "foo.0001.exr" as having an extension of "0001.exr". --- Changes.md | 1 + src/GafferUI/PathColumn.cpp | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Changes.md b/Changes.md index 63dfbc55e09..8e31449e047 100644 --- a/Changes.md +++ b/Changes.md @@ -7,6 +7,7 @@ Improvements - UI Editor : - Added the ability to edit the scale of node icons. - Improved layout of Box node plug creator visibility toggles. +- File Browser : The "Type" column can now be sorted. This sorts directories separately from files, which are sorted by their extension. API --- diff --git a/src/GafferUI/PathColumn.cpp b/src/GafferUI/PathColumn.cpp index 8af129a815b..2d2e9519ab0 100644 --- a/src/GafferUI/PathColumn.cpp +++ b/src/GafferUI/PathColumn.cpp @@ -241,6 +241,8 @@ FileIconPathColumn::FileIconPathColumn( SizeMode sizeMode ) PathColumn::CellData FileIconPathColumn::cellData( const Gaffer::Path &path, const IECore::Canceller *canceller ) const { + CellData result; + std::string s = path.string(); if( const FileSystemPath *fileSystemPath = runTimeCast( &path ) ) { @@ -256,7 +258,13 @@ PathColumn::CellData FileIconPathColumn::cellData( const Gaffer::Path &path, con } } - return CellData( /* value = */ nullptr, /* icon = */ new StringData( "fileIcon:" + s ) ); + result.icon = new StringData( "fileIcon:" + s ); + const auto p = std::filesystem::path( s ); + // Use a sortValue of `extension:stem` to allow sorting by extension + // while maintaining a reasonable ordering within each extension. + result.sortValue = new StringData( ( std::filesystem::is_directory( p ) ? " " : p.extension().string() ) + ":" + p.stem().string() ); + + return result; } PathColumn::CellData FileIconPathColumn::headerData( const IECore::Canceller *canceller ) const