0.4.1
Loading...
Searching...
No Matches
FileDialog.cpp
Go to the documentation of this file.
1// This file is part of INSTINCT, the INS Toolkit for Integrated
2// Navigation Concepts and Training by the Institute of Navigation of
3// the University of Stuttgart, Germany.
4//
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
9#include "FileDialog.hpp"
10
12
13#include "util/Logger.hpp"
14
15#include "imgui.h"
16#include "imgui_stdlib.h"
17#include "ImGuiFileDialog.h"
18
19#include <filesystem>
20
21bool NAV::gui::widgets::FileDialogSave(std::string& path, const char* vName,
22 const char* vFilters, const std::vector<std::string>& extensions,
23 const std::filesystem::path& startPath,
24 size_t id, [[maybe_unused]] const std::string& nameId,
25 const char* buttonText)
26{
27 bool changed = false;
28 // Filepath
29 if (ImGui::InputText("Filepath", &path))
30 {
31 LOG_DEBUG("{}: Filepath changed to {}", nameId, path);
32 changed = true;
33 }
34 if (ImGui::IsItemHovered())
35 {
36 ImGui::SetTooltip("%s", path.c_str());
37 }
38 ImGui::SameLine();
39 std::string saveFileDialogKey = fmt::format("Save File ({})", id);
40 if (ImGui::Button(buttonText))
41 {
42 ImGuiFileDialog::Instance()->OpenDialog(saveFileDialogKey, vName, vFilters, (startPath / ".").string(), 1, nullptr, ImGuiFileDialogFlags_ConfirmOverwrite);
43 for (const auto& ext : extensions)
44 {
45 ImGuiFileDialog::Instance()->SetFileStyle(IGFD_FileStyleByExtention, ext.c_str(), ImVec4(0.0F, 1.0F, 0.0F, 0.9F));
46 }
47 }
48
49 if (ImGuiFileDialog::Instance()->Display(saveFileDialogKey, ImGuiWindowFlags_NoCollapse, ImVec2(600, 500)))
50 {
51 if (ImGuiFileDialog::Instance()->IsOk())
52 {
53 path = ImGuiFileDialog::Instance()->GetFilePathName();
54 if (path.find(startPath.string()) != std::string::npos)
55 {
56 path = path.substr(startPath.string().size() + 1);
57 }
58 LOG_DEBUG("{}: Selected file: {}", nameId, path);
59 changed = true;
60 }
61
62 ImGuiFileDialog::Instance()->Close();
63 }
64
65 return changed;
66}
67
68bool NAV::gui::widgets::FileDialogLoad(std::string& path, const char* vName,
69 const char* vFilters, const std::vector<std::string>& extensions,
70 const std::filesystem::path& startPath,
71 size_t id, [[maybe_unused]] const std::string& nameId,
72 const char* buttonText)
73{
74 bool changed = false;
75 // Filepath
76 if (ImGui::InputText(fmt::format("Filepath##{}", id).c_str(), &path))
77 {
78 LOG_DEBUG("{}: Filepath changed to {}", nameId, path);
79 changed = true;
80 }
81 if (ImGui::IsItemHovered())
82 {
83 ImGui::SetTooltip("%s", path.c_str());
84 }
85 ImGui::SameLine();
86 std::string openFileDialogKey = fmt::format("Select File ({})", id);
87 if (ImGui::Button(fmt::format("{}##{}", buttonText, id).c_str()))
88 {
89 ImGuiFileDialog::Instance()->OpenDialog(openFileDialogKey, vName, vFilters, (startPath / ".").string());
90 for (const auto& ext : extensions)
91 {
92 ImGuiFileDialog::Instance()->SetFileStyle(ext.starts_with('(') ? IGFD_FileStyleByFullName : IGFD_FileStyleByExtention, ext.c_str(), ImVec4(0.0F, 1.0F, 0.0F, 0.9F));
93 }
94 }
95
96 if (ImGuiFileDialog::Instance()->Display(openFileDialogKey, ImGuiWindowFlags_NoCollapse, ImVec2(600, 500)))
97 {
98 if (ImGuiFileDialog::Instance()->IsOk())
99 {
100 if (!ImGuiFileDialog::Instance()->GetSelection().empty())
101 {
102 if (std::filesystem::exists(ImGuiFileDialog::Instance()->GetSelection().begin()->second))
103 {
104 path = ImGuiFileDialog::Instance()->GetSelection().begin()->second;
105 if (path.find(startPath.string()) != std::string::npos)
106 {
107 path = path.substr(startPath.string().size() + 1);
108 }
109 LOG_DEBUG("{}: Selected file: {}", nameId, path);
110 changed = true;
111 }
112 else
113 {
114 LOG_WARN("{}: Selected path does not exist: {}", nameId, ImGuiFileDialog::Instance()->GetSelection().begin()->second);
115 }
116 }
117 else
118 {
119 LOG_WARN("{}: FileDialog is okay, but nothing selected", nameId);
120 }
121 }
122
123 ImGuiFileDialog::Instance()->Close();
124 }
125
126 return changed;
127}
File Chooser.
Save/Load the Nodes.
Utility class for logging to console and file.
#define LOG_DEBUG
Debug information. Should not be called on functions which receive observations (spamming)
Definition Logger.hpp:67
#define LOG_WARN
Error occurred, but a fallback option exists and program continues to work normally.
Definition Logger.hpp:71
bool FileDialogSave(std::string &path, const char *vName, const char *vFilters, const std::vector< std::string > &extensions, const std::filesystem::path &startPath, size_t id, const std::string &nameId, const char *buttonText="Choose")
Shows an InputText and a button which opens a file dialog to select a path to save a file to.
bool FileDialogLoad(std::string &path, const char *vName, const char *vFilters, const std::vector< std::string > &extensions, const std::filesystem::path &startPath, size_t id, const std::string &nameId, const char *buttonText="Choose")
Shows an InputText and a button which opens a file dialog to select a path to load a file to.