Skip to content

Commit

Permalink
#5108: Add ability to register icons for file types
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Nov 19, 2020
1 parent 4de5833 commit 11cb2b1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
19 changes: 14 additions & 5 deletions include/ifiletypes.h
Expand Up @@ -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
{
Expand All @@ -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<FileTypePattern> FileTypePatterns;
Expand Down Expand Up @@ -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
Expand All @@ -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";

}

Expand Down
19 changes: 19 additions & 0 deletions radiantcore/filetypes/FileTypeRegistry.cpp
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions radiantcore/filetypes/FileTypeRegistry.h
Expand Up @@ -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;
Expand Down

0 comments on commit 11cb2b1

Please sign in to comment.