Skip to content

Commit

Permalink
#5108: Extend VFSTreePopulator interface to include the full path inf…
Browse files Browse the repository at this point in the history
…o when immediately populating the items we're adding.
  • Loading branch information
codereader committed Nov 19, 2020
1 parent f183d3b commit 933f954
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 35 deletions.
7 changes: 3 additions & 4 deletions libs/wxutil/EntityClassChooser.cpp
Expand Up @@ -81,14 +81,13 @@ class EntityClassTreePopulator:
// of the DISPLAY_FOLDER_KEY.
addPath(
eclass->getModName() + folderPath + "/" + eclass->getName(),
[this](wxutil::TreeModel::Row& row, const std::string& path,
bool isFolder)
[this](wxutil::TreeModel::Row& row, const std::string& path,
const std::string& leafName, bool isFolder)
{
// Get the display name by stripping off everything before the
// last slash
row[_columns.name] = wxVariant(
wxDataViewIconText(path.substr(path.rfind("/") + 1),
!isFolder ? _entityIcon : _folderIcon)
wxDataViewIconText(leafName, !isFolder ? _entityIcon : _folderIcon)
);
row[_columns.isFolder] = isFolder;
row.SendItemAdded();
Expand Down
4 changes: 2 additions & 2 deletions libs/wxutil/VFSTreePopulator.cpp
Expand Up @@ -20,7 +20,7 @@ VFSTreePopulator::~VFSTreePopulator()
void VFSTreePopulator::addPath(const std::string& path)
{
// Pass an empty ColumnPopulationCallback
addRecursive(path, [](TreeModel::Row&, const std::string&, bool) {});
addRecursive(path, [](TreeModel::Row&, const std::string&, const std::string&, bool) {});

// Add the explicit path to the set, we need it later
// when being visited by the Visitor implementation
Expand Down Expand Up @@ -66,7 +66,7 @@ const wxDataViewItem& VFSTreePopulator::addRecursive(const std::string& path,

// Call the population callback. If recursionLevel > 0 we had at least one
// path split operation, so this must be a folder
func(row, slashPos != std::string::npos ? path.substr(slashPos+1) : path, recursionLevel > 0);
func(row, path, slashPos != std::string::npos ? path.substr(slashPos+1) : path, recursionLevel > 0);

// Add a copy of the wxDataViewItem to our hashmap and return it
std::pair<NamedIterMap::iterator, bool> result = _iters.insert(
Expand Down
5 changes: 5 additions & 0 deletions libs/wxutil/VFSTreePopulator.h
Expand Up @@ -70,6 +70,7 @@ class VFSTreePopulator

// Column population function. The inserted Row will be passed as argument.
typedef std::function<void(TreeModel::Row& row,
const std::string& path,
const std::string& leafName,
bool isFolder)> ColumnPopulationCallback;

Expand All @@ -78,6 +79,10 @@ class VFSTreePopulator
* argument to allow for immediate population of the TreeStore's column values.
* As soon as the item has been created in the correct spot of the tree
* the callback will be invoked passing the TreeModel::Row as argument.
*
* Note that the callback can be invoked multiple times for one single addPath()
* call, since this method will automatically insert missing parent folder items
* when it encounters them.
*
* Client code needs to decide between this or the regular addPath() method
* and cannot mix between these two, as this variant here does not keep track
Expand Down
43 changes: 20 additions & 23 deletions libs/wxutil/fsview/Populator.cpp
Expand Up @@ -60,7 +60,26 @@ void Populator::visitFile(const vfs::FileInfo& fileInfo)
}

// Let the VFSTreePopulator do the insertion
_treePopulator.addPath(fileInfo.name);
_treePopulator.addPath(fileInfo.name, [&](TreeModel::Row& row,
const std::string& path, const std::string& leafName,
bool isFolder)
{
// The population callback will be called multiple times for deeper files,
// but only one of them will be have isFolder == false, which is our actual file

// Get the display path, everything after rightmost slash
row[_columns.filename] = wxVariant(wxDataViewIconText(leafName,
isFolder ? _folderIcon : GetIconForFile(leafName)));
row[_columns.vfspath] = _basePath + path;
row[_columns.isFolder] = isFolder;

if (!isFolder)
{
// Get the file size if possible
auto file = GlobalFileSystem().openFile(_basePath + path);
row[_columns.size] = os::getFormattedFileSize(file ? file->size() : -1);
}
});
}

void Populator::SearchForFilesMatchingExtension(const std::string& extension)
Expand Down Expand Up @@ -97,11 +116,6 @@ wxThread::ExitCode Populator::Entry()
if (TestDestroy()) return static_cast<wxThread::ExitCode>(0);
}

// Visit the tree populator in order to fill in the column data
_treePopulator.forEachNode(*this);

if (TestDestroy()) return static_cast<wxThread::ExitCode>(0);

// Sort the model before returning it
_treeStore->SortModelFoldersFirst(_columns.filename, _columns.isFolder);

Expand Down Expand Up @@ -155,23 +169,6 @@ const wxIcon& Populator::GetIconForFile(const std::string& path)
return foundIcon->second;
}

void Populator::visit(wxutil::TreeModel& /* store */, wxutil::TreeModel::Row& row,
const std::string& path, bool isExplicit)
{
if (TestDestroy()) return;

auto filename = path.substr(path.rfind("/") + 1);
// Get the display path, everything after rightmost slash
row[_columns.filename] = wxVariant(wxDataViewIconText(filename,
isExplicit ? GetIconForFile(filename) : _folderIcon));
row[_columns.vfspath] = _basePath + path;
row[_columns.isFolder] = !isExplicit;

// Get the file size if possible
auto file = GlobalFileSystem().openFile(_basePath + path);
row[_columns.size] = os::getFormattedFileSize(file ? file->size() : -1);
}

}

}
4 changes: 0 additions & 4 deletions libs/wxutil/fsview/Populator.h
Expand Up @@ -28,7 +28,6 @@ struct TreeColumns :
};

class Populator :
public wxutil::VFSTreePopulator::Visitor,
public wxThread
{
private:
Expand Down Expand Up @@ -64,9 +63,6 @@ class Populator :

void Populate();

void visit(wxutil::TreeModel& store, wxutil::TreeModel::Row& row,
const std::string& path, bool isExplicit);

void SetDefaultFileIcon(const std::string& fileIcon);

protected:
Expand Down
2 changes: 1 addition & 1 deletion radiant/ui/einspector/SkinChooser.cpp
Expand Up @@ -182,7 +182,7 @@ void SkinChooser::populateSkins()
for (const auto& skin : skins)
{
pop.addPath(skin, [&] (wxutil::TreeModel::Row& row,
const std::string& leafName, bool isFolder)
const std::string& path, const std::string& leafName, bool isFolder)
{
// Get the display path, everything after rightmost slash
std::string displayPath = leafName.substr(leafName.rfind("/") + 1);
Expand Down
3 changes: 2 additions & 1 deletion radiant/uimanager/SoundChooser.cpp
Expand Up @@ -69,7 +69,8 @@ class SoundShaderPopulator :
shader.getModName() + "/" + shaderNameForwardSlashes;

// Sort the shader into the tree and set the values
addPath(fullPath, [&](wxutil::TreeModel::Row& row, const std::string& leafName, bool isFolder)
addPath(fullPath, [&](wxutil::TreeModel::Row& row, const std::string& path,
const std::string& leafName, bool isFolder)
{
row[_columns.displayName] = wxVariant(
wxDataViewIconText(leafName, isFolder ? _folderIcon : _shaderIcon));
Expand Down

0 comments on commit 933f954

Please sign in to comment.