1
0
Fork 0
mirror of https://github.com/gwm17/Specter.git synced 2025-02-06 21:38:49 -05:00

Fixed janky FileDialog, now has properly implemented state control.

This commit is contained in:
Gordon McCann 2022-04-02 21:45:12 -04:00
parent e27d29e919
commit bed9b0fc1e
4 changed files with 220 additions and 208 deletions

View File

@ -117,11 +117,11 @@ namespace Navigator {
{ {
if(ImGui::MenuItem(ICON_FA_FOLDER_OPEN "\tOpen")) if(ImGui::MenuItem(ICON_FA_FOLDER_OPEN "\tOpen"))
{ {
m_fileDialog.SetOpenFileDialog(true); m_fileDialog.OpenDialog(FileDialog::Type::OpenFile);
} }
if(ImGui::MenuItem(ICON_FA_SAVE "\tSave")) if(ImGui::MenuItem(ICON_FA_SAVE "\tSave"))
{ {
m_fileDialog.SetSaveFileDialog(true); m_fileDialog.OpenDialog(FileDialog::Type::SaveFile);
} }
if (ImGui::MenuItem(ICON_FA_TIMES_CIRCLE "\tExit")) if (ImGui::MenuItem(ICON_FA_TIMES_CIRCLE "\tExit"))
{ {
@ -174,22 +174,30 @@ namespace Navigator {
} }
//Render all of our sub-windows, dialogs, panels, etc //Render all of our sub-windows, dialogs, panels, etc
std::string open_file_result = m_fileDialog.ImGuiRenderOpenFile(".nav"); auto fd_result = m_fileDialog.RenderFileDialog(".nav");
std::string save_file_result = m_fileDialog.ImGuiRenderSaveFile(".nav"); if (!fd_result.first.empty())
if (!open_file_result.empty())
{ {
SpectrumSerializer serializer(open_file_result); switch (fd_result.second)
serializer.DeserializeData(); {
UpdateHistogramList(); case FileDialog::Type::OpenFile:
UpdateCutList(); {
} SpectrumSerializer serializer(fd_result.first);
else if (!save_file_result.empty()) serializer.DeserializeData();
{ UpdateHistogramList();
NAV_INFO("Found a Save File! {0}", save_file_result); UpdateCutList();
SpectrumSerializer serializer(save_file_result); break;
serializer.SerializeData(m_histoList, m_cutList); }
case FileDialog::Type::SaveFile:
{
NAV_INFO("Found a Save File! {0}", fd_result.first);
SpectrumSerializer serializer(fd_result.first);
serializer.SerializeData(m_histoList, m_cutList);
break;
}
}
} }
if(m_spectrumDialog.ImGuiRenderSpectrumDialog(m_histoList, m_cutList, m_paramList)) if(m_spectrumDialog.ImGuiRenderSpectrumDialog(m_histoList, m_cutList, m_paramList))
UpdateHistogramList(); UpdateHistogramList();
@ -355,11 +363,11 @@ namespace Navigator {
ImGui::SameLine(); ImGui::SameLine();
if(ImGui::Button("Open")) if(ImGui::Button("Open"))
{ {
m_fileDialog.SetSaveFileDialog(true); m_fileDialog.OpenDialog(FileDialog::Type::SaveFile);
} }
std::string result = m_fileDialog.ImGuiRenderSaveFile(".csv"); auto result = m_fileDialog.RenderFileDialog(".csv");
if(!result.empty()) if(!result.first.empty() && result.second == FileDialog::Type::SaveFile)
filename = result; filename = result.first;
if(ImGui::Button("Ok")) if(ImGui::Button("Ok"))
{ {
ExportHistogram(selectedGram, filename); ExportHistogram(selectedGram, filename);

View File

@ -29,8 +29,7 @@ namespace Navigator {
} }
FileDialog::FileDialog() : FileDialog::FileDialog() :
m_currentPath(std::filesystem::current_path()), m_openFileName(ICON_FA_FILE " Open File"), m_saveFileName(ICON_FA_SAVE " Save File"), m_openDirName(ICON_FA_FOLDER " Open Directory"), m_currentPath(std::filesystem::current_path()), m_type(Type::None), m_selectedItem(""), m_openDialogFlag(false)
m_selectedItem(""), m_openFileFlag(false), m_saveFileFlag(false), m_openDirFlag(false)
{ {
table_flags = ImGuiTableFlags_BordersH | ImGuiTableFlags_SizingStretchProp | ImGuiTableFlags_BordersOuterV | ImGuiTableFlags_RowBg; table_flags = ImGuiTableFlags_BordersH | ImGuiTableFlags_SizingStretchProp | ImGuiTableFlags_BordersOuterV | ImGuiTableFlags_RowBg;
select_flags = ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_DontClosePopups; select_flags = ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_DontClosePopups;
@ -40,218 +39,227 @@ namespace Navigator {
//Each type of action has its own render function //Each type of action has its own render function
std::string FileDialog::ImGuiRenderOpenFile(const std::string& ext) std::pair<std::string, FileDialog::Type> FileDialog::RenderFileDialog(const std::string& ext)
{ {
if (m_openFileFlag) if (m_openDialogFlag)
{ {
m_openFileFlag = false;
m_selectedItem = ""; m_selectedItem = "";
m_openDialogFlag = false;
m_currentPath = std::filesystem::current_path(); m_currentPath = std::filesystem::current_path();
ImGui::OpenPopup(m_openFileName.c_str()); ImGui::OpenPopup("File Dialog");
} }
std::string result = ""; std::string result = "";
std::string text = ""; if (ImGui::BeginPopupModal("File Dialog"))
if (ImGui::BeginPopupModal(m_openFileName.c_str()))
{ {
ImGui::Text("%s", ("Current Directory: " + m_currentPath.lexically_normal().string()).c_str()); switch (m_type)
ImGui::SameLine();
ImGui::Text("%s", ("Extension Filter: "+ext).c_str());
ImGui::InputText("Selected", &m_selectedItem);
if (ImGui::Button("Ok"))
{ {
std::filesystem::path filepath = m_currentPath / m_selectedItem; case Type::OpenFile:
result = filepath.string();
ImGui::CloseCurrentPopup();
}
ImGui::SameLine();
if (ImGui::Button("Cancel"))
ImGui::CloseCurrentPopup();
if (ImGui::BeginTable("File System", 2, table_flags, ImVec2(-1,-1)))
{
ImGui::TableSetupColumn("Name");
ImGui::TableSetupColumn("Size");
ImGui::TableHeadersRow();
ImGui::TableNextRow();
ImGui::TableNextColumn();
if (ImGui::Selectable(ICON_FA_FOLDER " ..", false, select_flags))
{ {
m_selectedItem.clear(); result = ImGuiRenderOpenFile(ext);
m_currentPath.append(".."); break;
} }
ImGui::TableNextColumn(); case Type::SaveFile:
ImGui::Text("N/A");
for (auto& entry : std::filesystem::directory_iterator(m_currentPath))
{ {
if (entry.is_directory()) result = ImGuiRenderSaveFile(ext);
{ break;
ImGui::TableNextRow();
text = ICON_FA_FOLDER " " + std::filesystem::relative(entry.path(), m_currentPath).string();
ImGui::TableNextColumn();
if (ImGui::Selectable(text.c_str(), false, select_flags))
{
m_selectedItem.clear();
m_currentPath /= entry.path();
}
ImGui::TableNextColumn();
ImGui::Text("N/A");
}
else if(entry.path().filename().extension() == ext)
{
ImGui::TableNextRow();
text = ICON_FA_FILE " " + entry.path().filename().string();
ImGui::TableNextColumn();
if (ImGui::Selectable(text.c_str(), false, select_flags))
m_selectedItem = entry.path().filename().string();
ImGui::TableNextColumn();
ImGui::Text("%s", ConvertFileSystemSizeToString(entry.file_size()).c_str());
}
} }
ImGui::EndTable(); case Type::OpenDir:
{
result = ImGuiRenderOpenDir();
break;
}
case Type::None: break;
} }
ImGui::EndPopup(); ImGui::EndPopup();
} }
return std::make_pair(result, m_type);
}
std::string FileDialog::ImGuiRenderOpenFile(const std::string& ext)
{
std::string result = "";
std::string text = "";
ImGui::Text("%s", ("Current Directory: " + m_currentPath.lexically_normal().string()).c_str());
ImGui::SameLine();
ImGui::Text("%s", ("Extension Filter: "+ext).c_str());
ImGui::InputText("Selected", &m_selectedItem);
if (ImGui::Button("Ok"))
{
std::filesystem::path filepath = m_currentPath / m_selectedItem;
result = filepath.string();
ImGui::CloseCurrentPopup();
}
ImGui::SameLine();
if (ImGui::Button("Cancel"))
ImGui::CloseCurrentPopup();
if (ImGui::BeginTable("File System", 2, table_flags, ImVec2(-1, -1)))
{
ImGui::TableSetupColumn("Name");
ImGui::TableSetupColumn("Size");
ImGui::TableHeadersRow();
ImGui::TableNextRow();
ImGui::TableNextColumn();
if (ImGui::Selectable(ICON_FA_FOLDER " ..", false, select_flags))
{
m_selectedItem.clear();
m_currentPath.append("..");
}
ImGui::TableNextColumn();
ImGui::Text("N/A");
for (auto& entry : std::filesystem::directory_iterator(m_currentPath))
{
if (entry.is_directory())
{
ImGui::TableNextRow();
text = ICON_FA_FOLDER " " + std::filesystem::relative(entry.path(), m_currentPath).string();
ImGui::TableNextColumn();
if (ImGui::Selectable(text.c_str(), false, select_flags))
{
m_selectedItem.clear();
m_currentPath /= entry.path();
}
ImGui::TableNextColumn();
ImGui::Text("N/A");
}
else if (entry.path().filename().extension() == ext)
{
ImGui::TableNextRow();
text = ICON_FA_FILE " " + entry.path().filename().string();
ImGui::TableNextColumn();
if (ImGui::Selectable(text.c_str(), false, select_flags))
m_selectedItem = entry.path().filename().string();
ImGui::TableNextColumn();
ImGui::Text("%s", ConvertFileSystemSizeToString(entry.file_size()).c_str());
}
}
ImGui::EndTable();
}
return result; return result;
} }
std::string FileDialog::ImGuiRenderSaveFile(const std::string& ext) std::string FileDialog::ImGuiRenderSaveFile(const std::string& ext)
{ {
if (m_saveFileFlag)
{
m_saveFileFlag = false;
m_selectedItem = "";
m_currentPath = std::filesystem::current_path();
ImGui::OpenPopup(m_saveFileName.c_str());
}
std::string result = ""; std::string result = "";
std::string text = ""; std::string text = "";
if (ImGui::BeginPopupModal(m_saveFileName.c_str()))
ImGui::Text("%s", ("Current Directory: "+m_currentPath.lexically_normal().string()).c_str());
ImGui::SameLine();
ImGui::Text("%s", ("Extension Filter: "+ext).c_str());
ImGui::InputText("Selected", &m_selectedItem);
if (ImGui::Button("Ok"))
{ {
ImGui::Text("%s", ("Current Directory: "+m_currentPath.lexically_normal().string()).c_str()); std::filesystem::path filepath = m_currentPath / m_selectedItem;
ImGui::SameLine(); result = filepath.string();
ImGui::Text("%s", ("Extension Filter: "+ext).c_str()); ImGui::CloseCurrentPopup();
ImGui::InputText("Selected", &m_selectedItem); }
if (ImGui::Button("Ok")) ImGui::SameLine();
if (ImGui::Button("Cancel"))
ImGui::CloseCurrentPopup();
if (ImGui::BeginTable("File System", 2, table_flags, ImVec2(-1, -1)))
{
ImGui::TableSetupColumn("Name");
ImGui::TableSetupColumn("Size");
ImGui::TableHeadersRow();
ImGui::TableNextRow();
ImGui::TableNextColumn();
if (ImGui::Selectable(ICON_FA_FOLDER " ..", false, select_flags))
{ {
std::filesystem::path filepath = m_currentPath / m_selectedItem; m_selectedItem.clear();
result = filepath.string(); m_currentPath.append("..");
ImGui::CloseCurrentPopup();
} }
ImGui::SameLine(); ImGui::TableNextColumn();
if (ImGui::Button("Cancel")) ImGui::Text("N/A");
ImGui::CloseCurrentPopup(); for (auto& entry : std::filesystem::directory_iterator(m_currentPath))
if (ImGui::BeginTable("File System", 2, table_flags, ImVec2(-1, -1)))
{ {
ImGui::TableSetupColumn("Name"); if (entry.is_directory())
ImGui::TableSetupColumn("Size");
ImGui::TableHeadersRow();
ImGui::TableNextRow();
ImGui::TableNextColumn();
if (ImGui::Selectable(ICON_FA_FOLDER " ..", false, select_flags))
{ {
m_selectedItem.clear(); ImGui::TableNextRow();
m_currentPath.append(".."); text = ICON_FA_FOLDER " " + std::filesystem::relative(entry.path(), m_currentPath).string();
ImGui::TableNextColumn();
if (ImGui::Selectable(text.c_str(), false, select_flags))
{
m_selectedItem.clear();
m_currentPath /= entry.path();
}
ImGui::TableNextColumn();
ImGui::Text("N/A");
} }
ImGui::TableNextColumn(); else if (entry.path().filename().extension() == ext)
ImGui::Text("N/A");
for (auto& entry : std::filesystem::directory_iterator(m_currentPath))
{ {
if (entry.is_directory()) ImGui::TableNextRow();
{ text = ICON_FA_FILE " " + entry.path().filename().string();
ImGui::TableNextRow(); ImGui::TableNextColumn();
text = ICON_FA_FOLDER " " + std::filesystem::relative(entry.path(), m_currentPath).string(); if (ImGui::Selectable(text.c_str(), false, select_flags))
ImGui::TableNextColumn(); m_selectedItem = entry.path().filename().string();
if (ImGui::Selectable(text.c_str(), false, select_flags)) ImGui::TableNextColumn();
{ ImGui::Text("%s", ConvertFileSystemSizeToString(entry.file_size()).c_str());
m_selectedItem.clear();
m_currentPath /= entry.path();
}
ImGui::TableNextColumn();
ImGui::Text("N/A");
}
else if (entry.path().filename().extension() == ext)
{
ImGui::TableNextRow();
text = ICON_FA_FILE " " + entry.path().filename().string();
ImGui::TableNextColumn();
if (ImGui::Selectable(text.c_str(), false, select_flags))
m_selectedItem = entry.path().filename().string();
ImGui::TableNextColumn();
ImGui::Text("%s", ConvertFileSystemSizeToString(entry.file_size()).c_str());
}
} }
ImGui::EndTable();
} }
ImGui::EndPopup(); ImGui::EndTable();
} }
return result; return result;
} }
std::string FileDialog::ImGuiRenderOpenDir() std::string FileDialog::ImGuiRenderOpenDir()
{ {
if (m_openDirFlag)
{
m_openDirFlag = false;
m_currentPath = std::filesystem::current_path();
m_selectedItem = m_currentPath.string();
ImGui::OpenPopup(m_openDirName.c_str());
}
std::string result = ""; std::string result = "";
std::string text = ""; std::string text = "";
if (ImGui::BeginPopupModal(m_openDirName.c_str()))
ImGui::Text("%s", ("Current Directory: "+m_currentPath.lexically_normal().string()).c_str());
ImGui::InputText("Selected", &m_selectedItem);
if (ImGui::Button("Ok"))
{ {
ImGui::Text("%s", ("Current Directory: "+m_currentPath.lexically_normal().string()).c_str()); result = m_selectedItem;
ImGui::InputText("Selected", &m_selectedItem); ImGui::CloseCurrentPopup();
if (ImGui::Button("Ok")) }
ImGui::SameLine();
if (ImGui::Button("Cancel"))
ImGui::CloseCurrentPopup();
if (ImGui::BeginTable("File System", 2, table_flags, ImVec2(-1, -1)))
{
ImGui::TableSetupColumn("Name");
ImGui::TableSetupColumn("Size");
ImGui::TableHeadersRow();
ImGui::TableNextRow();
ImGui::TableNextColumn();
if (ImGui::Selectable(ICON_FA_FOLDER " ..", false, select_flags))
{ {
result = m_selectedItem; m_currentPath.append("..");
ImGui::CloseCurrentPopup(); m_selectedItem = m_currentPath.string();
} }
ImGui::SameLine(); ImGui::TableNextColumn();
if (ImGui::Button("Cancel")) ImGui::Text("N/A");
ImGui::CloseCurrentPopup(); for (auto& entry : std::filesystem::directory_iterator(m_currentPath))
if (ImGui::BeginTable("File System", 2, table_flags, ImVec2(-1, -1)))
{ {
ImGui::TableSetupColumn("Name");
ImGui::TableSetupColumn("Size");
ImGui::TableHeadersRow();
ImGui::TableNextRow(); ImGui::TableNextRow();
ImGui::TableNextColumn(); if (entry.is_directory())
if (ImGui::Selectable(ICON_FA_FOLDER " ..", false, select_flags))
{ {
m_currentPath.append(".."); text = ICON_FA_FOLDER " " + std::filesystem::relative(entry.path(), m_currentPath).string();
m_selectedItem = m_currentPath.string(); ImGui::TableNextColumn();
if (ImGui::Selectable(text.c_str(), false, select_flags))
{
m_currentPath /= entry.path();
m_selectedItem = m_currentPath.string();
}
ImGui::TableNextColumn();
ImGui::Text("N/A");
} }
ImGui::TableNextColumn(); else
ImGui::Text("N/A");
for (auto& entry : std::filesystem::directory_iterator(m_currentPath))
{ {
ImGui::TableNextRow(); text = ICON_FA_FILE " " + entry.path().filename().string();
if (entry.is_directory()) ImGui::TableNextColumn();
{ ImGui::Text("%s", text.c_str());
text = ICON_FA_FOLDER " " + std::filesystem::relative(entry.path(), m_currentPath).string(); ImGui::TableNextColumn();
ImGui::TableNextColumn(); ImGui::Text("%s", ConvertFileSystemSizeToString(entry.file_size()).c_str());
if (ImGui::Selectable(text.c_str(), false, select_flags))
{
m_currentPath /= entry.path();
m_selectedItem = m_currentPath.string();
}
ImGui::TableNextColumn();
ImGui::Text("N/A");
}
else
{
text = ICON_FA_FILE " " + entry.path().filename().string();
ImGui::TableNextColumn();
ImGui::Text("%s", text.c_str());
ImGui::TableNextColumn();
ImGui::Text("%s", ConvertFileSystemSizeToString(entry.file_size()).c_str());
}
} }
ImGui::EndTable();
} }
ImGui::EndPopup(); ImGui::EndTable();
} }
return result; return result;
} }

View File

@ -19,41 +19,37 @@
namespace Navigator { namespace Navigator {
class NAV_API FileDialog class NAV_API FileDialog
{ {
public: public:
enum class Type
{
OpenFile,
SaveFile,
OpenDir,
None
};
FileDialog(); FileDialog();
~FileDialog(); ~FileDialog();
inline void OpenDialog(Type type) { m_type = type; m_openDialogFlag = true; }
std::pair<std::string, Type> RenderFileDialog(const std::string& ext = "");
private:
std::string ImGuiRenderOpenFile(const std::string& ext); std::string ImGuiRenderOpenFile(const std::string& ext);
std::string ImGuiRenderSaveFile(const std::string& ext); std::string ImGuiRenderSaveFile(const std::string& ext);
std::string ImGuiRenderOpenDir(); std::string ImGuiRenderOpenDir();
inline void ResetCurrentDirectory() { m_currentPath = std::filesystem::current_path(); }
inline void SetOpenFileDialog(bool value) { m_openFileFlag = value; }
inline void SetSaveFileDialog(bool value) { m_saveFileFlag = value; }
inline void SetOpenDirDialog(bool value) { m_openDirFlag = value; }
inline bool IsOpenFileOpen() { return m_openFileFlag; }
inline bool IsOpenDirOpen() { return m_openDirFlag; }
inline bool IsSaveFileOpen() { return m_saveFileFlag; }
inline const std::string& GetOpenFileWindowName() { return m_openFileName; }
inline const std::string& GetSaveFileWindowName() { return m_saveFileName; }
inline const std::string& GetOpenDirWindowName() { return m_openDirName; }
private:
std::filesystem::path m_currentPath; std::filesystem::path m_currentPath;
std::string m_openFileName; Type m_type;
std::string m_saveFileName;
std::string m_openDirName;
std::string m_selectedItem; std::string m_selectedItem;
bool m_openFileFlag; bool m_openDialogFlag;
bool m_saveFileFlag;
bool m_openDirFlag;
ImGuiTableFlags table_flags; ImGuiTableFlags table_flags;
ImGuiSelectableFlags select_flags; ImGuiSelectableFlags select_flags;

View File

@ -66,11 +66,11 @@ namespace Navigator {
ImGui::InputText("Run Directory", &m_chosenLocation); ImGui::InputText("Run Directory", &m_chosenLocation);
if (ImGui::Button("Choose Location")) if (ImGui::Button("Choose Location"))
{ {
m_fileDialog.SetOpenDirDialog(true); m_fileDialog.OpenDialog(FileDialog::Type::OpenDir);
} }
auto temp = m_fileDialog.ImGuiRenderOpenDir(); auto temp = m_fileDialog.RenderFileDialog();
if (temp != "") if (!temp.first.empty() && temp.second == FileDialog::Type::OpenDir)
m_chosenLocation = temp; m_chosenLocation = temp.first;
} }
ImGui::InputInt("Coinc. Window (ps)", &m_chosenWindow); ImGui::InputInt("Coinc. Window (ps)", &m_chosenWindow);