Skip to content

Commit

Permalink
GUI param FilePathStorage_t: carry along file param project directory
Browse files Browse the repository at this point in the history
simplify file storage/flags/extensions/project dir data flow in gui parameter
and file browser widget by passing FilePathStorage_t everywhere, which contains the flags, extensions and project dir
  • Loading branch information
geringsj committed Apr 25, 2023
1 parent a5adaa1 commit d200867
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 67 deletions.
29 changes: 10 additions & 19 deletions frontend/services/gui/src/GUIManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1134,8 +1134,8 @@ void megamol::gui::GUIManager::draw_popups() {
float widget_width = ImGui::CalcItemWidth() - (ImGui::GetFrameHeightWithSpacing() + style.ItemSpacing.x);
ImGui::PushItemWidth(widget_width);

this->file_browser.Button_Select(this->gui_state.font_input_string_buffer, {"ttf"},
megamol::core::param::FilePathParam::Flag_File_RestrictExtension);
this->file_browser.Button_Select(this->gui_state.font_input_string_buffer,
FilePathStorage_t{megamol::core::param::FilePathParam::Flag_File_RestrictExtension, {"ttf"}});
ImGui::SameLine();
ImGui::InputText("Font Filename (.ttf)", &this->gui_state.font_input_string_buffer, ImGuiInputTextFlags_None);
ImGui::PopItemWidth();
Expand Down Expand Up @@ -1255,9 +1255,9 @@ void megamol::gui::GUIManager::draw_popups() {
// Default for saving gui state and parameter values
bool save_all_param_values = true;
bool save_gui_state = false;
if (this->file_browser.PopUp_Save("Save Running Project", filename, this->gui_state.open_popup_save, {"lua"},
megamol::core::param::FilePathParam::Flag_File_ToBeCreatedWithRestrExts, save_gui_state,
save_all_param_values)) {
if (this->file_browser.PopUp_Save("Save Running Project", filename, this->gui_state.open_popup_save,
FilePathStorage_t{megamol::core::param::FilePathParam::Flag_File_ToBeCreatedWithRestrExts, {"lua"}},
save_gui_state, save_all_param_values)) {
std::string state_str;
if (save_gui_state) {
state_str = this->project_to_lua_string(true);
Expand All @@ -1273,8 +1273,8 @@ void megamol::gui::GUIManager::draw_popups() {
// Load project pop-up
std::string filename;
this->gui_state.open_popup_load |= this->gui_hotkeys[HOTKEY_GUI_LOAD_PROJECT].is_pressed;
if (this->file_browser.PopUp_Load("Load Project", filename, this->gui_state.open_popup_load, {"lua", "png"},
megamol::core::param::FilePathParam::Flag_File_RestrictExtension)) {
if (this->file_browser.PopUp_Load("Load Project", filename, this->gui_state.open_popup_load,
FilePathStorage_t{megamol::core::param::FilePathParam::Flag_File_RestrictExtension, {"lua", "png"}})) {
// Redirect project loading request to Lua_Wrapper_service and load new project to megamol graph
/// GUI graph and GUI state are updated at next synchronization
this->gui_state.request_load_projet_file = filename;
Expand All @@ -1284,8 +1284,9 @@ void megamol::gui::GUIManager::draw_popups() {
// File name for screenshot pop-up
auto dummy = false;
if (this->file_browser.PopUp_Save("File Name for Screenshot", this->gui_state.screenshot_filepath,
this->gui_state.open_popup_screenshot, {"png"},
megamol::core::param::FilePathParam::Flag_File_ToBeCreatedWithRestrExts, dummy, dummy)) {
this->gui_state.open_popup_screenshot,
FilePathStorage_t{megamol::core::param::FilePathParam::Flag_File_ToBeCreatedWithRestrExts, {"png"}}, dummy,
dummy)) {
this->gui_state.screenshot_filepath_id = 0;
}

Expand All @@ -1300,7 +1301,6 @@ void megamol::gui::GUIManager::load_preset_window_docking(ImGuiID global_docking

/// DOCKING
#ifdef IMGUI_HAS_DOCK

// Create preset using DockBuilder
/// https://github.com/ocornut/imgui/issues/2109#issuecomment-426204357
// -------------------------------
Expand Down Expand Up @@ -1379,7 +1379,6 @@ std::string megamol::gui::GUIManager::save_imgui_settings_to_string() const {


std::string megamol::gui::GUIManager::project_to_lua_string(bool as_lua) {

std::string state_str;
if (this->state_to_string(state_str)) {
std::string return_state_str;
Expand All @@ -1406,7 +1405,6 @@ std::string megamol::gui::GUIManager::project_to_lua_string(bool as_lua) {


bool megamol::gui::GUIManager::state_from_string(const std::string& state) {

try {
nlohmann::json state_json = nlohmann::json::parse(state);
if (!state_json.is_object()) {
Expand Down Expand Up @@ -1456,7 +1454,6 @@ bool megamol::gui::GUIManager::state_from_string(const std::string& state) {


bool megamol::gui::GUIManager::state_to_string(std::string& out_state) {

ImGuiIO& io = ImGui::GetIO();

try {
Expand Down Expand Up @@ -1492,7 +1489,6 @@ bool megamol::gui::GUIManager::state_to_string(std::string& out_state) {


bool megamol::gui::GUIManager::create_unique_screenshot_filename(std::string& inout_filepath) {

// Check for existing file
bool created_filepath = false;
if (!inout_filepath.empty()) {
Expand Down Expand Up @@ -1531,26 +1527,22 @@ bool megamol::gui::GUIManager::create_unique_screenshot_filename(std::string& in

void GUIManager::RegisterWindow(
const std::string& window_name, std::function<void(AbstractWindow::BasicConfig&)> const& callback) {

this->win_collection.AddWindow(window_name, callback);
}


void GUIManager::RegisterPopUp(
const std::string& name, std::weak_ptr<bool> open, const std::function<void()>& callback) {

this->popup_collection[name] = PopUpData{open, const_cast<std::function<void()>&>(callback)};
}


void GUIManager::RegisterNotification(const std::string& name, std::weak_ptr<bool> open, const std::string& message) {

this->notification_collection[name] = NotificationData{open, false, message};
}


std::string GUIManager::extract_fontname(const std::string& imgui_fontname) const {

auto return_fontname = std::string(imgui_fontname);
auto sep_index = return_fontname.find(',');
return_fontname = return_fontname.substr(0, sep_index);
Expand All @@ -1560,7 +1552,6 @@ std::string GUIManager::extract_fontname(const std::string& imgui_fontname) cons

void GUIManager::RegisterHotkeys(
megamol::core::view::CommandRegistry& cmdregistry, megamol::core::MegaMolGraph& megamolgraph) {

if (auto win_hkeditor_ptr = this->win_collection.GetWindow<HotkeyEditor>()) {
win_hkeditor_ptr->RegisterHotkeys(&cmdregistry, &megamolgraph, &this->win_collection, &this->gui_hotkeys);
}
Expand Down
6 changes: 3 additions & 3 deletions frontend/services/gui/src/graph/GraphCollection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1849,9 +1849,9 @@ bool megamol::gui::GraphCollection::save_graph_dialog(ImGuiID graph_uid, bool& o
// Default for saving gui state and parameter values
bool save_all_param_values = true;
bool save_gui_state = false;
if (this->gui_file_browser.PopUp_Save("Save Configurator Project", project_filename, open_dialog, {"lua"},
megamol::core::param::FilePathParam::Flag_File_ToBeCreatedWithRestrExts, save_gui_state,
save_all_param_values)) {
if (this->gui_file_browser.PopUp_Save("Save Configurator Project", project_filename, open_dialog,
FilePathStorage_t{megamol::core::param::FilePathParam::Flag_File_ToBeCreatedWithRestrExts, {"lua"}},
save_gui_state, save_all_param_values)) {

std::string gui_state;
if (save_gui_state) {
Expand Down
5 changes: 1 addition & 4 deletions frontend/services/gui/src/graph/Parameter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1453,10 +1453,7 @@ bool megamol::gui::Parameter::widget_filepath(megamol::gui::Parameter::WidgetSco
ImGuiStyle& style = ImGui::GetStyle();

auto last_val = val;
auto file_flags = store.first;
auto file_extensions = store.second;
bool button_edit = this->gui_file_browser.Button_Select(
std::get<std::string>(this->gui_widget_value), file_extensions, file_flags);
bool button_edit = this->gui_file_browser.Button_Select(std::get<std::string>(this->gui_widget_value), store);
ImGui::SameLine();

auto item_width = ImGui::CalcItemWidth();
Expand Down
8 changes: 6 additions & 2 deletions frontend/services/gui/src/graph/Parameter.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ typedef std::shared_ptr<Module> ModulePtr_t;
// Types
typedef std::vector<Parameter> ParamVector_t;
typedef std::map<int, std::string> EnumStorage_t;
typedef std::pair<megamol::core::param::FilePathParam::Flags_t, megamol::core::param::FilePathParam::Extensions_t>
FilePathStorage_t;

struct FilePathStorage_t {
megamol::core::param::FilePathParam::Flags_t flags = 0;
megamol::core::param::FilePathParam::Extensions_t extensions = {};
std::filesystem::path project_directory = "";
};


/** ************************************************************************
Expand Down
43 changes: 25 additions & 18 deletions frontend/services/gui/src/widgets/FileBrowserWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "mmcore/utility/String.h"
#include "widgets/ButtonWidgets.h"

#include "../gui/src/graph/Parameter.h"


using namespace megamol;
using namespace megamol::gui;
Expand All @@ -39,8 +41,7 @@ megamol::gui::FileBrowserWidget::FileBrowserWidget()
}


bool megamol::gui::FileBrowserWidget::Button_Select(
std::string& inout_filename, const FilePathParam::Extensions_t& extensions, FilePathParam::Flags_t flags) {
bool megamol::gui::FileBrowserWidget::Button_Select(std::string& inout_filename, const FilePathStorage_t& store) {

assert(ImGui::GetCurrentContext() != nullptr);
ImGuiStyle& style = ImGui::GetStyle();
Expand Down Expand Up @@ -79,16 +80,19 @@ bool megamol::gui::FileBrowserWidget::Button_Select(
ImGui::EndChild();
ImGui::PopStyleColor();

return this->PopUp_Select("Select File", inout_filename, open_popup_select_file, extensions, flags);
return this->PopUp_Select("Select File", inout_filename, open_popup_select_file, store);
}


bool megamol::gui::FileBrowserWidget::popup(FileBrowserWidget::DialogMode mode, const std::string& label,
std::string& inout_filename, bool& inout_open_popup, const FilePathParam::Extensions_t& extensions,
FilePathParam::Flags_t flags, bool& inout_save_gui_state, bool& inout_save_all_param_values) {
std::string& inout_filename, bool& inout_open_popup, const FilePathStorage_t& store, bool& inout_save_gui_state,
bool& inout_save_all_param_values) {

bool retval = false;

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

try {
// Generate UID independent of label name
if (this->label_uid_map.find(label) == this->label_uid_map.end()) {
Expand All @@ -108,9 +112,9 @@ bool megamol::gui::FileBrowserWidget::popup(FileBrowserWidget::DialogMode mode,
popup_label += "###fbw" + this->label_uid_map[label];

if (inout_open_popup) {
this->validate_split_path(inout_filename, flags, this->current_directory_str, this->current_file_str);
this->validate_directory(flags, this->current_directory_str);
this->validate_file(mode, extensions, flags, this->current_directory_str, this->current_file_str);
this->validate_split_path(inout_filename, store, this->current_directory_str, this->current_file_str);
this->validate_directory(store, this->current_directory_str);
this->validate_file(mode, store, this->current_directory_str, this->current_file_str);
this->path_changed = true;

ImGui::OpenPopup(popup_label.c_str());
Expand Down Expand Up @@ -145,7 +149,7 @@ bool megamol::gui::FileBrowserWidget::popup(FileBrowserWidget::DialogMode mode,
ImGui::InputText("Directory", &this->current_directory_str, ImGuiInputTextFlags_AutoSelectAll);
this->tooltip.ToolTip(this->current_directory_str);
if (last_file_path_str != this->current_directory_str) {
this->validate_directory(flags, this->current_directory_str);
this->validate_directory(store, this->current_directory_str);
this->path_changed = true;
}
// Error message when path is no valid directory
Expand Down Expand Up @@ -242,9 +246,8 @@ bool megamol::gui::FileBrowserWidget::popup(FileBrowserWidget::DialogMode mode,
last_file_path_str = this->current_directory_str;
auto new_path = path_pair.first.generic_u8string();
this->validate_split_path(
new_path, flags, this->current_directory_str, this->current_file_str);
this->validate_file(
mode, extensions, flags, this->current_directory_str, this->current_file_str);
new_path, store, this->current_directory_str, this->current_file_str);
this->validate_file(mode, store, this->current_directory_str, this->current_file_str);
if (last_file_path_str != this->current_directory_str) {
this->path_changed = true;
}
Expand Down Expand Up @@ -296,7 +299,7 @@ bool megamol::gui::FileBrowserWidget::popup(FileBrowserWidget::DialogMode mode,
ImGui::PopItemFlag();
}
if (last_file_name_str != this->current_file_str) {
this->validate_file(mode, extensions, flags, this->current_directory_str, this->current_file_str);
this->validate_file(mode, store, this->current_directory_str, this->current_file_str);
}

// Optional save GUI state option ------------
Expand Down Expand Up @@ -383,7 +386,9 @@ bool megamol::gui::FileBrowserWidget::popup(FileBrowserWidget::DialogMode mode,


bool megamol::gui::FileBrowserWidget::validate_split_path(
const std::string& in_path, FilePathParam::Flags_t flags, std::string& out_dir, std::string& out_file) const {
const std::string& in_path, const FilePathStorage_t& store, std::string& out_dir, std::string& out_file) const {

const auto& flags = store.flags;

try {
out_dir.clear();
Expand Down Expand Up @@ -437,7 +442,7 @@ bool megamol::gui::FileBrowserWidget::validate_split_path(
}


void megamol::gui::FileBrowserWidget::validate_directory(FilePathParam::Flags_t flags, const std::string& path_str) {
void megamol::gui::FileBrowserWidget::validate_directory(const FilePathStorage_t& store, const std::string& path_str) {

try {
auto tmp_path = std::filesystem::u8path(path_str);
Expand All @@ -451,9 +456,11 @@ void megamol::gui::FileBrowserWidget::validate_directory(FilePathParam::Flags_t
}


void megamol::gui::FileBrowserWidget::validate_file(FileBrowserWidget::DialogMode mode,
const FilePathParam::Extensions_t& extensions, FilePathParam::Flags_t flags, const std::string& directory_str,
const std::string& file_str) {
void megamol::gui::FileBrowserWidget::validate_file(FileBrowserWidget::DialogMode mode, const FilePathStorage_t& store,
const std::string& directory_str, const std::string& file_str) {

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

try {
this->file_errors.clear();
Expand Down
40 changes: 21 additions & 19 deletions frontend/services/gui/src/widgets/FileBrowserWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@

#include "HoverToolTip.h"
#include "StringSearchWidget.h"
#include "mmcore/param/FilePathParam.h"


using namespace megamol::core::param;


namespace megamol::gui {

// forward declare the FilePathStorage_t type here
// we cant include gui/src/grpah/Parameter.h because
// it includes this header file itself, leading to endless recursive problems
// instead we include the Parameter.h in the FileBrowserWidget.cpp file
// since we actuall yuse the FilePathStorage_t implementation there
struct FilePathStorage_t;

/** ************************************************************************
* File browser widget
Expand All @@ -40,20 +45,19 @@ class FileBrowserWidget {
* @return True on success, false otherwise.
*/
bool PopUp_Save(const std::string& label, std::string& inout_filename, bool& inout_open_popup,
const FilePathParam::Extensions_t& extensions, FilePathParam::Flags_t flags, bool& inout_save_gui_state,
bool& inout_save_all_param_values) {
return this->popup(DIALOGMODE_SAVE, label, inout_filename, inout_open_popup, extensions, flags,
inout_save_gui_state, inout_save_all_param_values);
const FilePathStorage_t& store, bool& inout_save_gui_state, bool& inout_save_all_param_values) {
return this->popup(DIALOGMODE_SAVE, label, inout_filename, inout_open_popup, store, inout_save_gui_state,
inout_save_all_param_values);
}
bool PopUp_Load(const std::string& label, std::string& inout_filename, bool& inout_open_popup,
const FilePathParam::Extensions_t& extensions, FilePathParam::Flags_t flags) {
bool PopUp_Load(
const std::string& label, std::string& inout_filename, bool& inout_open_popup, const FilePathStorage_t& store) {
bool dummy = false;
return this->popup(DIALOGMODE_LOAD, label, inout_filename, inout_open_popup, extensions, flags, dummy, dummy);
return this->popup(DIALOGMODE_LOAD, label, inout_filename, inout_open_popup, store, dummy, dummy);
}
bool PopUp_Select(const std::string& label, std::string& inout_filename, bool& inout_open_popup,
const FilePathParam::Extensions_t& extensions, FilePathParam::Flags_t flags) {
bool PopUp_Select(
const std::string& label, std::string& inout_filename, bool& inout_open_popup, const FilePathStorage_t& store) {
bool dummy = false;
return this->popup(DIALOGMODE_SELECT, label, inout_filename, inout_open_popup, extensions, flags, dummy, dummy);
return this->popup(DIALOGMODE_SELECT, label, inout_filename, inout_open_popup, store, dummy, dummy);
}

/**
Expand All @@ -67,8 +71,7 @@ class FileBrowserWidget {
*
* @return True on success, false otherwise.
*/
bool Button_Select(
std::string& inout_filename, const FilePathParam::Extensions_t& extensions, FilePathParam::Flags_t flags);
bool Button_Select(std::string& inout_filename, const FilePathStorage_t& store);

private:
typedef std::pair<std::filesystem::path, bool> ChildData_t;
Expand Down Expand Up @@ -100,14 +103,13 @@ class FileBrowserWidget {
// FUNCTIONS --------------------------------------------------------------

bool popup(DialogMode mode, const std::string& label, std::string& inout_filename, bool& inout_open_popup,
const FilePathParam::Extensions_t& extensions, FilePathParam::Flags_t flags, bool& inout_save_gui_state,
bool& inout_save_all_param_values);
const FilePathStorage_t& store, bool& inout_save_gui_state, bool& inout_save_all_param_values);

bool validate_split_path(
const std::string& in_path, FilePathParam::Flags_t flags, std::string& out_dir, std::string& out_file) const;
void validate_directory(FilePathParam::Flags_t flags, const std::string& directory_str);
void validate_file(DialogMode mode, const FilePathParam::Extensions_t& extensions, FilePathParam::Flags_t flags,
const std::string& directory_str, const std::string& file_str);
const std::string& in_path, const FilePathStorage_t& store, std::string& out_dir, std::string& out_file) const;
void validate_directory(const FilePathStorage_t& store, const std::string& directory_str);
void validate_file(
DialogMode mode, const FilePathStorage_t& store, const std::string& directory_str, const std::string& file_str);

std::string get_parent_path(const std::string& dir) const;
std::string get_absolute_path(const std::string& dir) const;
Expand Down
4 changes: 2 additions & 2 deletions frontend/services/gui/src/windows/Configurator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ void megamol::gui::Configurator::PopUps() {
if (auto graph_ptr = this->graph_collection.GetGraph(this->graph_state.graph_selected_uid)) {
project_filename = graph_ptr->GetFilename();
}
if (this->file_browser.PopUp_Load("Load Project", project_filename, this->open_popup_load, {"lua"},
megamol::core::param::FilePathParam::Flag_File_RestrictExtension)) {
if (this->file_browser.PopUp_Load("Load Project", project_filename, this->open_popup_load,
FilePathStorage_t{megamol::core::param::FilePathParam::Flag_File_RestrictExtension, {"lua"}})) {

popup_failed = !this->graph_collection.LoadOrAddProjectFromFile(this->add_project_graph_uid, project_filename);
this->add_project_graph_uid = GUI_INVALID_ID;
Expand Down

0 comments on commit d200867

Please sign in to comment.