Skip to content

Commit

Permalink
#5108: Add MapSelector::Result structure to contain more info about t…
Browse files Browse the repository at this point in the history
…he selected map file
  • Loading branch information
codereader committed Nov 20, 2020
1 parent 5715411 commit 57e3a4e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
11 changes: 11 additions & 0 deletions libs/wxutil/fsview/FileSystemView.cpp
Expand Up @@ -168,6 +168,17 @@ bool FileSystemView::GetIsFolderSelected()
return row[Columns().isFolder].getBool();
}

std::string FileSystemView::GetArchivePathOfSelection()
{
wxDataViewItem item = GetSelection();

if (!item.IsOk()) return "";

wxutil::TreeModel::Row row(item, *GetModel());

return row[Columns().archivePath];
}

void FileSystemView::SelectItem(const wxDataViewItem& item)
{
if (!item.IsOk()) return;
Expand Down
3 changes: 3 additions & 0 deletions libs/wxutil/fsview/FileSystemView.h
Expand Up @@ -85,6 +85,9 @@ class FileSystemView :
std::string GetSelectedPath();
bool GetIsFolderSelected();

// Returns the path to the archive (directory/PK4) of the selected path
std::string GetArchivePathOfSelection();

// Expands the given path, which is relative to the base path (e.g. "maps/")
void ExpandPath(const std::string& relativePath);

Expand Down
30 changes: 24 additions & 6 deletions radiant/ui/mapselector/MapSelector.cpp
Expand Up @@ -34,6 +34,7 @@ MapSelector::MapSelector() :
vbox->Add(_treeView, 1, wxEXPAND);

_buttons = CreateStdDialogButtonSizer(wxOK | wxCANCEL);
_buttons->GetAffirmativeButton()->SetLabel(_("Open"));
wxButton* reloadButton = new wxButton(this, wxID_ANY, _("Refresh"));
reloadButton->Bind(wxEVT_BUTTON, &MapSelector::onRescanPath, this);

Expand All @@ -58,14 +59,15 @@ int MapSelector::ShowModal()
return DialogBase::ShowModal();
}

std::string MapSelector::ChooseMapFile()
MapSelector::Result MapSelector::ChooseMapFile()
{
auto* dialog = new MapSelector();
std::string returnValue = "";
Result returnValue;

if (dialog->ShowModal() == wxID_OK)
{
returnValue = dialog->getSelectedPath();
returnValue.selectedPath = dialog->getSelectedPath();
returnValue.archivePath = dialog->getArchivePath();
}

// Use the instance to select a model.
Expand All @@ -74,11 +76,22 @@ std::string MapSelector::ChooseMapFile()

void MapSelector::OpenMapFromProject(const cmd::ArgumentList& args)
{
auto mapPath = ChooseMapFile();
auto result = ChooseMapFile();

if (!mapPath.empty())
if (result.selectedPath.empty())
{
GlobalCommandSystem().executeCommand("OpenMap", mapPath);
return;
}

auto extension = string::to_lower_copy(os::getExtension(result.archivePath));

if (!os::isDirectory(result.archivePath) && GlobalFileSystem().getArchiveExtensions().count(extension) > 0)
{
GlobalCommandSystem().executeCommand("OpenMapFromArchive", result.archivePath, result.selectedPath);
}
else
{
GlobalCommandSystem().executeCommand("OpenMap", result.selectedPath);
}
}

Expand Down Expand Up @@ -231,4 +244,9 @@ std::string MapSelector::getSelectedPath()
return _treeView->GetSelectedPath();
}

std::string MapSelector::getArchivePath()
{
return _treeView->GetArchivePathOfSelection();
}

}
10 changes: 9 additions & 1 deletion radiant/ui/mapselector/MapSelector.h
Expand Up @@ -50,6 +50,8 @@ class MapSelector :

// Return the selected map path
std::string getSelectedPath();
// Return the archive path of the selection
std::string getArchivePath();

void onRescanPath(wxCommandEvent& ev);
void onItemActivated(wxDataViewEvent& ev);
Expand All @@ -61,10 +63,16 @@ class MapSelector :
public:
int ShowModal() override;

struct Result
{
std::string archivePath; // path to containing archive
std::string selectedPath; // relative path within the archive
};

/**
* Display the Selector return the path of the file selected by the user.
*/
static std::string ChooseMapFile();
static Result ChooseMapFile();

static void OpenMapFromProject(const cmd::ArgumentList& args);
};
Expand Down

0 comments on commit 57e3a4e

Please sign in to comment.