From 94a751513c2d8005ab978c2667cc2a1a077cf1f4 Mon Sep 17 00:00:00 2001 From: codereader Date: Tue, 17 Nov 2020 08:21:11 +0100 Subject: [PATCH] #5108: Add ability to register icons for file types --- include/ifiletypes.h | 19 ++++++++++++++----- radiantcore/filetypes/FileTypeRegistry.cpp | 19 +++++++++++++++++++ radiantcore/filetypes/FileTypeRegistry.h | 2 ++ 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/include/ifiletypes.h b/include/ifiletypes.h index af7b327572..e68f964e95 100644 --- a/include/ifiletypes.h +++ b/include/ifiletypes.h @@ -7,9 +7,6 @@ /** * Simple structure to store a file pattern (e.g. "*.map") * along with its name (e.g. "Map files") and extension. - * - * If a module has been registering itself for a certain - * filetype/extension combo, its name is in associatedModule. */ struct FileTypePattern { @@ -22,13 +19,18 @@ struct FileTypePattern // The mask pattern ("*.map") std::string pattern; + // Optional icon string, referring to an image in the bitmaps folder + std::string icon; + // Constructor with optional initialisation parameters FileTypePattern(const std::string& name_ = "", const std::string& extension_ = "", - const std::string& pattern_ = "") : + const std::string& pattern_ = "", + const std::string& icon_ = "") : name(name_), extension(extension_), - pattern(pattern_) + pattern(pattern_), + icon(icon_) {} }; typedef std::list FileTypePatterns; @@ -68,6 +70,12 @@ class IFileTypeRegistry : * @returns: a list of FileTypePatterns containing extension, display name, etc. */ virtual FileTypePatterns getPatternsForType(const std::string& fileType) = 0; + + /** + * Tries to find an icon file for the given extension. If not empty, + * the returned string refers to a filename in the bitmaps/ folder. + */ + virtual std::string getIconForExtension(const std::string& extension) = 0; }; namespace filetype @@ -79,6 +87,7 @@ const char* const TYPE_MAP_EXPORT = "mapexport"; const char* const TYPE_PREFAB = "prefab"; const char* const TYPE_REGION = "region"; const char* const TYPE_MODEL_EXPORT = "modelexport"; +const char* const TYPE_PAK = "pak"; } diff --git a/radiantcore/filetypes/FileTypeRegistry.cpp b/radiantcore/filetypes/FileTypeRegistry.cpp index f4f2620b9d..bff9f5cbbe 100644 --- a/radiantcore/filetypes/FileTypeRegistry.cpp +++ b/radiantcore/filetypes/FileTypeRegistry.cpp @@ -52,6 +52,25 @@ FileTypePatterns FileTypeRegistry::getPatternsForType(const std::string& fileTyp return i != _fileTypes.end() ? i->second : FileTypePatterns(); } +std::string FileTypeRegistry::getIconForExtension(const std::string& extension) +{ + auto extLower = string::to_lower_copy(extension); + + // We pick the first icon in any of the patterns matching the extension + for (const auto& patterns : _fileTypes) + { + for (const auto& pattern : patterns.second) + { + if (pattern.extension == extension && !pattern.icon.empty()) + { + return pattern.icon; + } + } + } + + return std::string(); +} + const std::string& FileTypeRegistry::getName() const { static std::string _name(MODULE_FILETYPES); diff --git a/radiantcore/filetypes/FileTypeRegistry.h b/radiantcore/filetypes/FileTypeRegistry.h index 289c346949..afbf479e40 100644 --- a/radiantcore/filetypes/FileTypeRegistry.h +++ b/radiantcore/filetypes/FileTypeRegistry.h @@ -28,6 +28,8 @@ class FileTypeRegistry : void registerPattern(const std::string& fileType, const FileTypePattern& pattern) override; FileTypePatterns getPatternsForType(const std::string& fileType) override; + + std::string getIconForExtension(const std::string& extension) override; // RegisterableModule implementation const std::string& getName() const override;