Skip to content

Commit

Permalink
GUI/Parameter: put back file param safety checks via FilePathParam pr…
Browse files Browse the repository at this point in the history
…oject directory validation support
  • Loading branch information
geringsj committed Oct 5, 2022
1 parent 992211d commit 371d2fd
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 51 deletions.
6 changes: 6 additions & 0 deletions core/include/mmcore/param/FilePathParam.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class FilePathParam : public AbstractParam {
* @param flags The flags for the parameter
* @param exts The required file extensions for the parameter
*/
FilePathParam(const std::filesystem::path& initVal, Flags_t flags, const Extensions_t& exts,
const std::filesystem::path& project_directory);
FilePathParam(const std::filesystem::path& initVal, Flags_t flags = Flag_File, const Extensions_t& exts = {});
FilePathParam(const std::string& initVal, Flags_t flags = Flag_File, const Extensions_t& exts = {});
FilePathParam(const char* initVal, Flags_t flags = Flag_File, const Extensions_t& exts = {});
Expand Down Expand Up @@ -129,6 +131,10 @@ class FilePathParam : public AbstractParam {
return this->extensions;
}

inline std::filesystem::path GetProjectDirectory() const {
return this->project_directory;
}

/**
* Function checks if path is valid for given flags
*
Expand Down
5 changes: 5 additions & 0 deletions core/src/param/FilePathParam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

using namespace megamol::core::param;

FilePathParam::FilePathParam(const std::filesystem::path& initVal, Flags_t flags, const Extensions_t& exts,
const std::filesystem::path& project_directory)
: FilePathParam(initVal, flags, exts) {
this->SetProjectDirectory(project_directory);
}

FilePathParam::FilePathParam(const std::filesystem::path& initVal, Flags_t flags, const Extensions_t& exts)
: AbstractParam()
Expand Down
101 changes: 51 additions & 50 deletions frontend/services/gui/src/graph/Parameter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,10 @@ std::string megamol::gui::Parameter::GetValueString() const {
} else if constexpr (std::is_same_v<T, std::string>) {
value_string = arg;
} else if constexpr (std::is_same_v<T, std::filesystem::path>) {
//if (this->core_param_ptr != nullptr) {
// value_string = this->core_param_ptr->ValueString();
//} else {
// auto file_storage = this->GetStorage<FilePathStorage_t>();
// auto parameter = megamol::core::param::FilePathParam(arg, file_storage.first, file_storage.second);
// value_string = parameter.ValueString();
//}
value_string = arg.u8string();
auto file_storage = this->GetStorage<FilePathStorage_t>();
auto parameter = megamol::core::param::FilePathParam(
arg, file_storage.flags, file_storage.extensions, file_storage.project_directory);
value_string = parameter.ValueString();
} else if constexpr (std::is_same_v<T, vislib::math::Ternary>) {
auto parameter = megamol::core::param::TernaryParam(arg);
value_string = parameter.ValueString();
Expand Down Expand Up @@ -250,10 +246,10 @@ bool megamol::gui::Parameter::SetValueString(const std::string& val_str, bool se
} break;
case (ParamType_t::FILEPATH): {
auto file_storage = this->GetStorage<FilePathStorage_t>();
megamol::core::param::FilePathParam parameter(
std::filesystem::u8path(val_str), file_storage.first, file_storage.second);
megamol::core::param::FilePathParam parameter(std::filesystem::path(val_str), file_storage.flags,
file_storage.extensions, file_storage.project_directory);
retval = parameter.ParseValue(val_str);
this->SetValue(parameter.Value(), set_default_val, set_dirty);
this->SetValue(parameter.ValueString(), set_default_val, set_dirty);
} break;
case (ParamType_t::FLEXENUM): {
megamol::core::param::FlexEnumParam parameter(val_str);
Expand Down Expand Up @@ -348,7 +344,7 @@ bool megamol::gui::Parameter::ReadNewCoreParameterToStockParameter(
} else if (auto* p_ptr = in_param_slot.Param<core::param::FilePathParam>()) {
out_param.type = ParamType_t::FILEPATH;
out_param.default_value = p_ptr->ValueString();
out_param.storage = FilePathStorage_t({p_ptr->GetFlags(), p_ptr->GetExtensions()});
out_param.storage = FilePathStorage_t{p_ptr->GetFlags(), p_ptr->GetExtensions(), p_ptr->GetProjectDirectory()};
} else if (auto* p_ptr = in_param_slot.Param<core::param::FlexEnumParam>()) {
out_param.type = ParamType_t::FLEXENUM;
out_param.default_value = p_ptr->ValueString();
Expand Down Expand Up @@ -495,8 +491,8 @@ bool megamol::gui::Parameter::ReadNewCoreParameterToNewParameter(megamol::core::
out_param->SetValue(p_ptr->Value(), set_default_val, set_dirty);
} else if (auto* p_ptr = in_param_slot.Param<core::param::FilePathParam>()) {
out_param = std::make_shared<Parameter>(megamol::gui::GenerateUniqueID(), ParamType_t::FILEPATH,
FilePathStorage_t({p_ptr->GetFlags(), p_ptr->GetExtensions()}), std::monostate(), std::monostate(),
std::monostate(), param_name, description);
FilePathStorage_t{p_ptr->GetFlags(), p_ptr->GetExtensions(), p_ptr->GetProjectDirectory()},
std::monostate(), std::monostate(), std::monostate(), param_name, description);
out_param->SetValue(p_ptr->Value(), set_default_val, set_dirty);
} else {
megamol::core::utility::log::Log::DefaultLog.WriteError(
Expand Down Expand Up @@ -577,7 +573,8 @@ bool megamol::gui::Parameter::ReadCoreParameterToParameter(
} else if (auto* p_ptr = in_param_ptr.DynamicCast<core::param::FilePathParam>()) {
if (out_param.type == ParamType_t::FILEPATH) {
out_param.SetValue(std::filesystem::path{p_ptr->ValueString()}, set_default_val, set_dirty);
auto file_storage = FilePathStorage_t({p_ptr->GetFlags(), p_ptr->GetExtensions()});
auto file_storage =
FilePathStorage_t{p_ptr->GetFlags(), p_ptr->GetExtensions(), p_ptr->GetProjectDirectory()};
out_param.SetStorage(file_storage);
} else {
type_error = true;
Expand Down Expand Up @@ -1485,41 +1482,45 @@ bool megamol::gui::Parameter::widget_filepath(megamol::gui::Parameter::WidgetSco
auto tmp_val_str = std::get<std::string>(this->gui_widget_value);
std::replace(tmp_val_str.begin(), tmp_val_str.end(), '\\', '/');
val = std::filesystem::path(tmp_val_str);
//try {
// if (last_val != val) {
// auto error_flags = FilePathParam::ValidatePath(val, file_extensions, file_flags);
// if (error_flags & FilePathParam::FilePathParam::Flag_File) {
// this->gui_popup_msg =
// "Omitting value '" + val.generic_u8string() + "'. Expected file but directory is given.";
// }
// if (error_flags & FilePathParam::Flag_Directory) {
// this->gui_popup_msg =
// "Omitting value '" + val.generic_u8string() + "'. Expected directory but file is given.";
// }
// if (error_flags & FilePathParam::Internal_NoExistenceCheck) {
// this->gui_popup_msg = "Omitting value '" + val.generic_u8string() + "'. File does not exist.";
// }
// if (error_flags & FilePathParam::Internal_RestrictExtension) {
// std::string log_exts;
// for (auto& ext : file_extensions) {
// log_exts += "'." + ext + "' ";
// }
// this->gui_popup_msg = "Omitting value '" + val.generic_u8string() +
// "'. File does not have required extension: " + log_exts;
// }
// if (error_flags != 0) {
// val = last_val;
// megamol::core::utility::log::Log::DefaultLog.WriteWarn(
// (std::string("[FilePathParam] ") + this->gui_popup_msg).c_str());
// if (!this->gui_popup_disabled) {
// ImGui::OpenPopup(popup_name.c_str());
// }
// }
// }
//} catch (std::filesystem::filesystem_error& e) {
// megamol::core::utility::log::Log::DefaultLog.WriteError(
// "Filesystem Error: %s [%s, %s, line %d]\n", e.what(), __FILE__, __FUNCTION__, __LINE__);
//}
try {
const auto& extensions = store.extensions;
const auto& flags = store.flags;
const auto& projdir = store.project_directory;

if (last_val != val) {
auto error_flags = FilePathParam::ValidatePath(val, extensions, flags, projdir);
if (error_flags & FilePathParam::FilePathParam::Flag_File) {
this->gui_popup_msg =
"Omitting value '" + val.generic_u8string() + "'. Expected file but directory is given.";
}
if (error_flags & FilePathParam::Flag_Directory) {
this->gui_popup_msg =
"Omitting value '" + val.generic_u8string() + "'. Expected directory but file is given.";
}
if (error_flags & FilePathParam::Internal_NoExistenceCheck) {
this->gui_popup_msg = "Omitting value '" + val.generic_u8string() + "'. File does not exist.";
}
if (error_flags & FilePathParam::Internal_RestrictExtension) {
std::string log_exts;
for (auto& ext : extensions) {
log_exts += "'." + ext + "' ";
}
this->gui_popup_msg = "Omitting value '" + val.generic_u8string() +
"'. File does not have required extension: " + log_exts;
}
if (error_flags != 0) {
val = last_val;
megamol::core::utility::log::Log::DefaultLog.WriteWarn(
(std::string("[FilePathParam] ") + this->gui_popup_msg).c_str());
if (!this->gui_popup_disabled) {
ImGui::OpenPopup(popup_name.c_str());
}
}
}
} catch (std::filesystem::filesystem_error& e) {
megamol::core::utility::log::Log::DefaultLog.WriteError(
"Filesystem Error: %s [%s, %s, line %d]\n", e.what(), __FILE__, __FUNCTION__, __LINE__);
}
this->filepath_scroll_xmax = true;
retval = true;
} else if (!ImGui::IsItemActive() && !ImGui::IsItemEdited()) {
Expand Down
3 changes: 2 additions & 1 deletion frontend/services/gui/src/widgets/FileBrowserWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ void megamol::gui::FileBrowserWidget::validate_file(FileBrowserWidget::DialogMod

auto const& flags = store.flags;
auto const& extensions = store.extensions;
auto const& project_dir = store.project_directory;

try {
this->file_errors.clear();
Expand All @@ -472,7 +473,7 @@ void megamol::gui::FileBrowserWidget::validate_file(FileBrowserWidget::DialogMod
tmp_filepath = dir;
}
}
auto error_flags = FilePathParam::ValidatePath(tmp_filepath, extensions, flags);
auto error_flags = FilePathParam::ValidatePath(tmp_filepath, extensions, flags, project_dir);
if (error_flags != 0) {
this->valid_file = false;
}
Expand Down

0 comments on commit 371d2fd

Please sign in to comment.