Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support FileDialog::Mode::OPEN_DIR for selecting a directory #4272

Merged
merged 1 commit into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 28 additions & 16 deletions cpp/open3d/visualization/gui/FileDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,21 +159,26 @@ struct FileDialog::Impl {
auto d = utility::filesystem::GetFileNameWithoutDirectory(dir);
entries_.emplace_back(d, DirEntry::Type::DIR);
}
std::unordered_set<std::string> filter;
auto it = filter_idx_2_filter.find(filter_->GetSelectedIndex());
if (it != filter_idx_2_filter.end()) {
filter = it->second;
}
for (auto &file : raw_files) {
auto f = utility::filesystem::GetFileNameWithoutDirectory(file);
auto ext = utility::filesystem::GetFileExtensionInLowerCase(f);
if (!ext.empty()) {
ext = std::string(".") + ext;

// append file filters only for file modes
if (mode_ != Mode::OPEN_DIR) {
std::unordered_set<std::string> filter;
auto it = filter_idx_2_filter.find(filter_->GetSelectedIndex());
if (it != filter_idx_2_filter.end()) {
filter = it->second;
}
if (filter.empty() || filter.find(ext) != filter.end()) {
entries_.emplace_back(f, DirEntry::Type::FILE);
for (auto &file : raw_files) {
auto f = utility::filesystem::GetFileNameWithoutDirectory(file);
auto ext = utility::filesystem::GetFileExtensionInLowerCase(f);
if (!ext.empty()) {
ext = std::string(".") + ext;
}
if (filter.empty() || filter.find(ext) != filter.end()) {
entries_.emplace_back(f, DirEntry::Type::FILE);
}
}
}

std::sort(entries_.begin(), entries_.end());

// Include an entry for ".." for convenience on Linux.
Expand Down Expand Up @@ -223,7 +228,8 @@ struct FileDialog::Impl {
}

void UpdateOk() {
ok_->SetEnabled(std::string(filename_->GetText()) != "");
ok_->SetEnabled(mode_ == Mode::OPEN_DIR ||
std::string(filename_->GetText()) != "");
}
};

Expand Down Expand Up @@ -253,7 +259,7 @@ FileDialog::FileDialog(Mode mode, const char *title, const Theme &theme)
layout->AddChild(impl_->filelist_);

impl_->cancel_ = std::make_shared<Button>("Cancel");
if (mode == Mode::OPEN) {
if (mode == Mode::OPEN || mode == Mode::OPEN_DIR) {
impl_->ok_ = std::make_shared<Button>("Open");
} else if (mode == Mode::SAVE) {
impl_->ok_ = std::make_shared<Button>("Save");
Expand Down Expand Up @@ -405,8 +411,14 @@ void FileDialog::OnDone() {
}
}
}
utility::LogInfo("[o3d] name: {}.", name);
this->impl_->on_done_((dir + "/" + name).c_str());
std::string path;
if (!name.empty()) {
utility::LogInfo("[o3d] name: {}.", name);
path = dir + "/" + name;
} else {
path = dir;
}
this->impl_->on_done_(path.c_str());
} else {
utility::LogError("FileDialog: need to call SetOnDone()");
}
Expand Down
2 changes: 1 addition & 1 deletion cpp/open3d/visualization/gui/FileDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class FileDialog : public Dialog {
using Super = Dialog;

public:
enum class Mode { OPEN, SAVE };
enum class Mode { OPEN, SAVE, OPEN_DIR };

FileDialog(Mode type, const char *title, const Theme &theme);
virtual ~FileDialog();
Expand Down
1 change: 1 addition & 0 deletions cpp/pybind/visualization/gui/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1663,6 +1663,7 @@ void pybind_gui_classes(py::module &m) {
py::none(), py::none(), "");
filedlg_mode.value("OPEN", FileDialog::Mode::OPEN)
.value("SAVE", FileDialog::Mode::SAVE)
.value("OPEN_DIR", FileDialog::Mode::OPEN_DIR)
.export_values();
filedlg.def(py::init<FileDialog::Mode, const char *, const Theme &>(),
"Creates either an open or save file dialog. The first "
Expand Down