1
0
Fork 0
mirror of https://github.com/gwm17/Specter.git synced 2025-02-07 05:38:51 -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,21 +174,29 @@ 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)
{
case FileDialog::Type::OpenFile:
{
SpectrumSerializer serializer(fd_result.first);
serializer.DeserializeData(); serializer.DeserializeData();
UpdateHistogramList(); UpdateHistogramList();
UpdateCutList(); UpdateCutList();
break;
} }
else if (!save_file_result.empty()) case FileDialog::Type::SaveFile:
{ {
NAV_INFO("Found a Save File! {0}", save_file_result); NAV_INFO("Found a Save File! {0}", fd_result.first);
SpectrumSerializer serializer(save_file_result); SpectrumSerializer serializer(fd_result.first);
serializer.SerializeData(m_histoList, m_cutList); 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,19 +39,49 @@ namespace Navigator {
//Each type of action has its own render function //Each type of action has its own render function
std::pair<std::string, FileDialog::Type> FileDialog::RenderFileDialog(const std::string& ext)
{
if (m_openDialogFlag)
{
m_selectedItem = "";
m_openDialogFlag = false;
m_currentPath = std::filesystem::current_path();
ImGui::OpenPopup("File Dialog");
}
std::string result = "";
if (ImGui::BeginPopupModal("File Dialog"))
{
switch (m_type)
{
case Type::OpenFile:
{
result = ImGuiRenderOpenFile(ext);
break;
}
case Type::SaveFile:
{
result = ImGuiRenderSaveFile(ext);
break;
}
case Type::OpenDir:
{
result = ImGuiRenderOpenDir();
break;
}
case Type::None: break;
}
ImGui::EndPopup();
}
return std::make_pair(result, m_type);
}
std::string FileDialog::ImGuiRenderOpenFile(const std::string& ext) std::string FileDialog::ImGuiRenderOpenFile(const std::string& ext)
{ {
if (m_openFileFlag)
{
m_openFileFlag = false;
m_selectedItem = "";
m_currentPath = std::filesystem::current_path();
ImGui::OpenPopup(m_openFileName.c_str());
}
std::string result = ""; std::string result = "";
std::string text = ""; std::string text = "";
if (ImGui::BeginPopupModal(m_openFileName.c_str()))
{
ImGui::Text("%s", ("Current Directory: " + m_currentPath.lexically_normal().string()).c_str()); ImGui::Text("%s", ("Current Directory: " + m_currentPath.lexically_normal().string()).c_str());
ImGui::SameLine(); ImGui::SameLine();
ImGui::Text("%s", ("Extension Filter: "+ext).c_str()); ImGui::Text("%s", ("Extension Filter: "+ext).c_str());
@ -66,81 +95,7 @@ namespace Navigator {
ImGui::SameLine(); ImGui::SameLine();
if (ImGui::Button("Cancel")) if (ImGui::Button("Cancel"))
ImGui::CloseCurrentPopup(); 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();
}
ImGui::EndPopup();
}
return result;
}
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 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"))
{
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))) if (ImGui::BeginTable("File System", 2, table_flags, ImVec2(-1, -1)))
{ {
ImGui::TableSetupColumn("Name"); ImGui::TableSetupColumn("Name");
@ -183,24 +138,78 @@ namespace Navigator {
} }
ImGui::EndTable(); ImGui::EndTable();
} }
ImGui::EndPopup(); return result;
}
std::string FileDialog::ImGuiRenderSaveFile(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::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::Text("%s", ("Current Directory: "+m_currentPath.lexically_normal().string()).c_str());
ImGui::InputText("Selected", &m_selectedItem); ImGui::InputText("Selected", &m_selectedItem);
if (ImGui::Button("Ok")) if (ImGui::Button("Ok"))
@ -211,6 +220,7 @@ namespace Navigator {
ImGui::SameLine(); ImGui::SameLine();
if (ImGui::Button("Cancel")) if (ImGui::Button("Cancel"))
ImGui::CloseCurrentPopup(); ImGui::CloseCurrentPopup();
if (ImGui::BeginTable("File System", 2, table_flags, ImVec2(-1, -1))) if (ImGui::BeginTable("File System", 2, table_flags, ImVec2(-1, -1)))
{ {
ImGui::TableSetupColumn("Name"); ImGui::TableSetupColumn("Name");
@ -251,8 +261,6 @@ namespace Navigator {
} }
ImGui::EndTable(); ImGui::EndTable();
} }
ImGui::EndPopup();
}
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);