Skip to content

Commit

Permalink
Automatically add extension to non-native file dialog if none is spec…
Browse files Browse the repository at this point in the history
…ified (#2818)
  • Loading branch information
prewettg authored Dec 24, 2020
1 parent 68e0ab3 commit f08db30
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
27 changes: 23 additions & 4 deletions cpp/open3d/visualization/gui/FileDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
// IN THE SOFTWARE.
// ----------------------------------------------------------------------------

#if defined(__APPLE__)
// Include FileDialog here to get value of GUI_USE_NATIVE_FILE_DIALOG
#include "open3d/visualization/gui/FileDialog.h"

#if defined(__APPLE__) && GUI_USE_NATIVE_FILE_DIALOG
// see FileDialogNative.cpp
#else

#include "open3d/visualization/gui/FileDialog.h"

#include <string>
#include <unordered_map>
#include <unordered_set>
Expand Down Expand Up @@ -387,7 +388,25 @@ void FileDialog::OnDone() {
if (this->impl_->on_done_) {
auto dir = this->impl_->CalcCurrentDirectory();
utility::filesystem::ChangeWorkingDirectory(dir);
auto name = this->impl_->filename_->GetText();
std::string name = this->impl_->filename_->GetText();
// If the user didn't specify an extension, automatically add one
// (unless we don't have the any-files (*.*) filter selected).
if (name.find(".") == std::string::npos && !name.empty()) {
int idx = this->impl_->filter_->GetSelectedIndex();
if (idx >= 0) {
auto &exts = impl_->filter_idx_2_filter[idx];
// Prefer PNG if available (otherwise in a list of common
// image files, e.g., ".jpg .png", we might pick the lossy one.
if (exts.find(".png") != exts.end()) {
name += ".png";
} else {
if (!exts.empty()) {
name += *exts.begin();
}
}
}
}
std::cout << "[o3d] name: '" << name << "'" << std::endl;
this->impl_->on_done_((dir + "/" + name).c_str());
} else {
utility::LogError("FileDialog: need to call SetOnDone()");
Expand Down
2 changes: 2 additions & 0 deletions cpp/open3d/visualization/gui/FileDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

#include "open3d/visualization/gui/Dialog.h"

#define GUI_USE_NATIVE_FILE_DIALOG 1

namespace open3d {
namespace visualization {
namespace gui {
Expand Down
8 changes: 5 additions & 3 deletions cpp/open3d/visualization/gui/FileDialogNative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
// IN THE SOFTWARE.
// ----------------------------------------------------------------------------

#if defined(__APPLE__)
// Include FileDialog here to get value of GUI_USE_NATIVE_FILE_DIALOG
#include "open3d/visualization/gui/FileDialog.h"

#if defined(__APPLE__) && GUI_USE_NATIVE_FILE_DIALOG

#include <string>
#include <vector>

#include "open3d/visualization/gui/FileDialog.h"
#include "open3d/visualization/gui/Native.h"

namespace open3d {
Expand Down Expand Up @@ -83,4 +85,4 @@ void FileDialog::OnDone() {}
} // namespace visualization
} // namespace open3d

#endif // __APPLE__
#endif // __APPLE__ && GUI_USE_NATIVE_FILE_DIALOG

0 comments on commit f08db30

Please sign in to comment.