Skip to content

Commit

Permalink
Default to standard models/ folder if nothing is set as recent path.
Browse files Browse the repository at this point in the history
Use a FileChooser's save logic to get the overwrite prompt.
  • Loading branch information
codereader committed Aug 17, 2017
1 parent 535c54c commit b235708
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 17 deletions.
22 changes: 12 additions & 10 deletions libs/wxutil/PathEntry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,28 @@
namespace wxutil
{

PathEntry::PathEntry(wxWindow* parent, const std::string& fileType) :
PathEntry(parent, false, fileType, std::string())
PathEntry::PathEntry(wxWindow* parent, const std::string& fileType, bool open) :
PathEntry(parent, false, open, fileType, std::string())
{}

PathEntry::PathEntry(wxWindow* parent, bool foldersOnly) :
PathEntry(parent, foldersOnly, std::string(), std::string())
PathEntry(parent, foldersOnly, true, std::string(), std::string())
{}

PathEntry::PathEntry(wxWindow* parent, const char* fileType) :
PathEntry(parent, std::string(fileType), std::string())
PathEntry::PathEntry(wxWindow* parent, const char* fileType, bool open) :
PathEntry(parent, std::string(fileType), open, std::string())
{}

PathEntry::PathEntry(wxWindow* parent, const std::string& fileType, const std::string& defaultExt) :
PathEntry(parent, false, fileType, defaultExt)
PathEntry::PathEntry(wxWindow* parent, const std::string& fileType, bool open, const std::string& defaultExt) :
PathEntry(parent, false, open, fileType, defaultExt)
{}

PathEntry::PathEntry(wxWindow* parent, bool foldersOnly, const std::string& fileType, const std::string& defaultExt) :
PathEntry::PathEntry(wxWindow* parent, bool foldersOnly, bool open,
const std::string& fileType, const std::string& defaultExt) :
wxPanel(parent, wxID_ANY),
_fileType(fileType),
_defaultExt(defaultExt)
_defaultExt(defaultExt),
_open(open)
{
SetSizer(new wxBoxSizer(wxHORIZONTAL));

Expand Down Expand Up @@ -92,7 +94,7 @@ void PathEntry::onBrowseFiles(wxCommandEvent& ev)
{
wxWindow* topLevel = wxGetTopLevelParent(this);

FileChooser fileChooser(topLevel, _("Choose File"), true, _fileType, _defaultExt);
FileChooser fileChooser(topLevel, _("Choose File"), _open, _fileType, _defaultExt);

fileChooser.setCurrentPath(getValue());

Expand Down
17 changes: 12 additions & 5 deletions libs/wxutil/PathEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ class PathEntry :
// in the FileChooser dialog
std::string _defaultExt;

// Whether we're opening the FileChooser in open or save mode
bool _open;

private:
// Private shared constructor
PathEntry(wxWindow* parent, bool foldersOnly, const std::string& fileType, const std::string& defaultExt);
PathEntry(wxWindow* parent, bool foldersOnly, bool open,
const std::string& fileType, const std::string& defaultExt);

public:
/**
Expand All @@ -47,21 +51,24 @@ class PathEntry :
* matching the given filetype. The filetype is used to populate
* the filter dropdown with the corresponding options registered
* in the FileTypeRegistry.
* The open flag indicates whether the FileChooser should be in
* open or save mode.
*/
explicit PathEntry(wxWindow* parent, const std::string& fileType);
explicit PathEntry(wxWindow* parent, const std::string& fileType, bool open);

// Same constructor as above but accepting const char* to avoid
// the char* being converted to bool even though it's marked explicit
explicit PathEntry(wxWindow* parent, const char* fileType);
explicit PathEntry(wxWindow* parent, const char* fileType, bool open);

/**
* Construct a new Path Entry which browses for files
* matching the given filetype. The filetype is used to populate
* the filter dropdown with the corresponding options registered
* in the FileTypeRegistry. The dropdown matching the default extension
* is preselected by default.
* is preselected by default. The open flag indicates whether the
* FileChooser should be in open or save mode.
*/
explicit PathEntry(wxWindow* parent, const std::string& fileType, const std::string& defaultExt);
explicit PathEntry(wxWindow* parent, const std::string& fileType, bool open, const std::string& defaultExt);

// Set the selected path, this does not fire the EV_PATH_ENTRY_CHANGED event
void setValue(const std::string& val);
Expand Down
28 changes: 26 additions & 2 deletions radiant/ui/modelexport/ExportAsModelDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
#include <wx/sizer.h>
#include <boost/algorithm/string/case_conv.hpp>

#include "os/path.h"
#include "os/file.h"
#include "os/dir.h"
#include "registry/registry.h"
#include "wxutil/dialog/MessageBox.h"
#include "wxutil/ChoiceHelper.h"
Expand Down Expand Up @@ -76,6 +79,26 @@ void ExportAsModelDialog::populateWindow()
std::string recentFormat = registry::getValue<std::string>(RKEY_MODEL_EXPORT_OUTPUT_FORMAT);
std::string recentPath = registry::getValue<std::string>(RKEY_MODEL_EXPORT_OUTPUT_PATH);

// Default to the models path of the current mod or game
if (recentPath.empty())
{
recentPath = GlobalGameManager().getModPath();

if (recentPath.empty())
{
recentPath = GlobalGameManager().getUserEnginePath();
}

recentPath = os::standardPathWithSlash(recentPath) + "models/";

if (!os::fileOrDirExists(recentPath))
{
rMessage() << "Creating default model output folder: " << recentPath << std::endl;

os::makeDirectory(recentPath);
}
}

if (!recentFormat.empty())
{
wxutil::ChoiceHelper::SelectItemByStoredString(formatChoice, recentFormat);
Expand All @@ -84,10 +107,11 @@ void ExportAsModelDialog::populateWindow()
// Replace the filepicker control with our own PathEntry
wxWindow* existing = findNamedObject<wxWindow>(this, "ExportDialogFilePicker");

wxutil::PathEntry* pathEntry = new wxutil::PathEntry(existing->GetParent(), filetype::TYPE_MODEL_EXPORT);
wxutil::PathEntry* pathEntry = new wxutil::PathEntry(existing->GetParent(),
filetype::TYPE_MODEL_EXPORT, false, recentFormat);

pathEntry->setValue(recentPath);
pathEntry->SetName("ExportDialogFilePicker");
pathEntry->setDefaultExtension(recentFormat);

existing->GetContainingSizer()->Replace(existing, pathEntry);
existing->Destroy();
Expand Down

0 comments on commit b235708

Please sign in to comment.