From 3f36e0d832000c4b57e4bb886e74e49cdd952683 Mon Sep 17 00:00:00 2001 From: AKMaily Date: Wed, 23 Oct 2024 09:04:27 +0200 Subject: [PATCH] Refactoring Part 1 mainWindow object for variables that are used throughout the whole window Popup Menus in own function unused code deleted --- CMakeLists.txt | 1 + src/analyze_data.hpp | 2 + src/handler.cpp | 23 +++++--- src/handler.hpp | 8 ++- src/main.cpp | 102 ++++++++++---------------------- src/mainWindow.cpp | 14 +++++ src/mainWindow.hpp | 23 ++++++++ src/style.cpp | 135 +++++++++++++++++++++++++++---------------- src/style.hpp | 8 +-- 9 files changed, 179 insertions(+), 137 deletions(-) create mode 100644 src/mainWindow.cpp create mode 100644 src/mainWindow.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e135b479..2521f385 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,6 +31,7 @@ include(cmake_helpers/FindOrFetch.cmake) add_executable(OmniView src/main.cpp + src/mainWindow.cpp src/generateTrainingData.cpp src/info_popup.cpp src/imgui_stdlib.cpp diff --git a/src/analyze_data.hpp b/src/analyze_data.hpp index 9a617a2d..ca64cd2e 100644 --- a/src/analyze_data.hpp +++ b/src/analyze_data.hpp @@ -1,3 +1,5 @@ +#pragma once + #include #include #include diff --git a/src/handler.cpp b/src/handler.cpp index 6793bcf1..2da6095c 100644 --- a/src/handler.cpp +++ b/src/handler.cpp @@ -74,7 +74,7 @@ static std::vector getDeviceInfos() { return axisInfos; } -void addPlots(const char *name, fs::path &AnalyzedFilePath, bool &LOADANALYSISDATA, +void addPlots(const char *name, mainWindow &mWindow, std::function axesSetup) { static std::set firstRun; @@ -86,8 +86,8 @@ void addPlots(const char *name, fs::path &AnalyzedFilePath, bool &LOADANALYSISDA ImPlot::SetupAxis(ImAxis_X1, "[x]"); ImPlot::SetupAxis(ImAxis_Y1, "[y]"); - if(!AnalyzedFilePath.empty() && LOADANALYSISDATA){ - AddPlotFromFile(AnalyzedFilePath); + if(!mWindow.AnalyzedFilePath.empty() && mWindow.LOADANALYSISDATA){ + AddPlotFromFile(mWindow.AnalyzedFilePath); ImPlot::EndPlot(); ImPlot::PopStyleColor(); } @@ -339,8 +339,8 @@ void load_files(decltype(captureData) &loadedFiles, } } -void set_config(const std::string &configpath) { - if (fs::exists(configpath)) +void set_config(mainWindow &mWindow) { + if (fs::exists(mWindow.configpath)) fmt::print("found config.json\n\r"); else { fmt::print("Did not find config.json.\n Download from Github\n\r"); @@ -348,8 +348,8 @@ void set_config(const std::string &configpath) { } } -void set_json(nlohmann::json &config) { - if (fs::exists(load_json(config, ("languagepath")))) +void set_json(mainWindow &mWindow) { + if (fs::exists(load_json(mWindow.config, ("languagepath")))) fmt::print("Found language: {}\n\r", appLanguage[Key::German]); else { fmt::print("Did not find {}.\n Download from Github\n\r", @@ -380,6 +380,15 @@ void rstSettings(const decltype(captureData) &loadedFiles) { } } +void setupSW(mainWindow &mWindow){ + set_config(mWindow); + mWindow.config = load_json_file(mWindow.configpath); + set_json(mWindow); + mWindow.language = + load_json_file(load_json(mWindow.config, "languagepath") + + load_json(mWindow.config, "language") + ".json"); +} + //TODO : Set this also up for saved OmniScope files void AddPlotFromFile(fs::path &filePath) { diff --git a/src/handler.hpp b/src/handler.hpp index 751c0a97..4250b96d 100644 --- a/src/handler.hpp +++ b/src/handler.hpp @@ -2,6 +2,7 @@ #ifndef HANDLER_HEADER_HPP #define HANDLER_HEADER_HPP +#include "mainWindow.hpp" #include "../ai_omniscope-v2-communication_sw/src/OmniscopeSampler.hpp" #include "languages.hpp" #include @@ -19,7 +20,7 @@ inline std::map>> captureData; void addPlots( - const char *,fs::path &,bool &, + const char *, mainWindow &, std::function); void parseDeviceMetaData(Omniscope::MetaData, std::shared_ptr &); @@ -27,9 +28,10 @@ void initDevices(); void devicesList(bool const &flagPaused); void load_files(decltype(captureData) &, std::map &, bool &); -void set_config(const std::string &); -void set_json(nlohmann::json &); +void set_config(mainWindow &); +void set_json(mainWindow &); void set_inital_config(nlohmann::json &); +void setupSW(mainWindow &); void rstSettings(const decltype(captureData) &); void AddPlotFromFile(fs::path &); diff --git a/src/main.cpp b/src/main.cpp index 39a9714a..d8ed22ed 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,88 +5,50 @@ #include "analyze_data.hpp" int main() { - const std::string configpath = "config/config.json"; - set_config(configpath); - nlohmann::json config = load_json_file(configpath); - set_json(config); - nlohmann::json language = - load_json_file(load_json(config, "languagepath") + - load_json(config, "language") + ".json"); - // local variables - bool flagPaused{true}, development{false}, open_generate_training_data{false},open_analyze_menu{false}, - open_settings{false}; - std::once_flag configFlag; - auto loadedFiles = captureData; - std::map loadedFilenames; - // temporary solution - bool LOADANALYSISDATA{false}; - fs::path AnalyzedFilePath(""); + mainWindow mWindow; + + setupSW(mWindow); // Set up config and old language file + + // temporary solution + // TODO:: Change loadedFiles from datatype Scope to a concrete Filetype + auto loadedFiles = captureData; + std::map loadedFilenames; // main loop auto render = [&]() { - std::call_once(configFlag, set_inital_config, std::ref(config)); + std::call_once(mWindow.configFlag, set_inital_config, std::ref(mWindow.config)); SetupImGuiStyle(false, 0.99f); ImGui::SetNextWindowPos({0.f, 0.f}); auto windowSize{ImGui::GetIO().DisplaySize}; ImGui::SetNextWindowSize(windowSize); + + // #################################################### Begin MainWindow ImGui::Begin("OmniScopev2 Data Capture Tool", nullptr, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoTitleBar); + + // Dev Color menu + if (mWindow.development && ImGui::Button("Development")) + ImGui::OpenPopup("Development Colors"); - if (development && ImGui::Button("Development")) - ImGui::OpenPopup("Development Colors"); + // Popup-Window content + if (ImGui::BeginPopup("Development Colors")) { + PopupStyleEditor(); + ImGui::EndPopup(); + } - // Popup-Window content - if (ImGui::BeginPopup("Development Colors")) { - PopupStyleEditor(); - ImGui::EndPopup(); - } + set_side_menu(mWindow, loadedFiles, loadedFilenames); // style.cpp - ImGui::BeginChild("Left Side", {windowSize.x * .18f, 0.f}); - // ############################################# Side Menu - set_side_menu(config, open_settings, open_generate_training_data,open_analyze_menu, - loadedFiles, loadedFilenames, LOADANALYSISDATA); - // there're four "BeginChild"s, one as the left side - // and three on the right side - ImGui::EndChild(); // end child "Left Side" ImGui::SameLine(); ImGui::BeginChild("Right Side", {0.f, 0.f}); - if (sampler.has_value() && !flagPaused) - sampler->copyOut(captureData); - // ######################################### Toolbar - set_toolbar(config, language, flagPaused, loadedFiles, LOADANALYSISDATA); + if (sampler.has_value() && !mWindow.flagPaused) + sampler->copyOut(captureData); - // ############################ Settings Menu - static int title = 0; - static std::vector titles(2); // two languages - if (open_settings) { - const auto EngItr = englishLan.find(Key::Settings); - const auto GrmItr = germanLan.find(Key::Settings); - // check returned value from find() and set titles - if (EngItr != englishLan.end() && GrmItr != germanLan.end()) { - titles[0] = (std::string)EngItr->second + "###ID"; - titles[1] = (std::string)GrmItr->second + "###ID"; - ImGui::OpenPopup(titles[title].c_str()); - } else - fmt::println("Settings values not found."); - open_settings = false; - } - if (ImGui::BeginPopupModal(titles[title].c_str(), nullptr, - ImGuiWindowFlags_AlwaysAutoResize)) { - ImGui::SetItemDefaultFocus(); - popup_settings(config, configpath, title); - ImGui::EndPopup(); - } - // Generate training data popup - if (open_generate_training_data) - generateTrainingData(open_generate_training_data, captureData, - savedFileNames); + set_toolbar(mWindow, loadedFiles); // style.cpp - // Generate analyze data popup - if (open_analyze_menu) - AnalyzedFilePath = generate_analyze_menu(open_analyze_menu, LOADANALYSISDATA, captureData); + generatePopUpMenus(mWindow); // style.cpp // ############################ addPlots("Recording the data", ...) ImGui::Dummy({0.f, windowSize.y * .01f}); @@ -99,15 +61,11 @@ int main() { // check if egu and timescale for plot are same // error if third device is added addPlots( - appLanguage[Key::Recording_Data],AnalyzedFilePath,LOADANALYSISDATA, - [flagPaused](double x_max, std::string yLabel, ImAxis_ axis, + appLanguage[Key::Recording_Data], mWindow, + [&mWindow](double x_max, std::string yLabel, ImAxis_ axis, double yMin, double yMax) { ImPlot::SetupLegend(ImPlotLocation_NorthWest); - // auto auxFlagsMeasuring = - // ImPlotAxisFlags_AutoFit | ImPlotAxisFlags_NoGridLines; - // auto auxFlagsPaused = ImPlotAxisFlags_NoGridLines; - //ImPlot::SetupAxisTicks(ImAxis_Y1, -10, 200, 22, nullptr, true); - if (!flagPaused) { + if (!mWindow.flagPaused) { ImPlot::SetupAxis(axis, yLabel.c_str(), ImPlotAxisFlags_AutoFit); ImPlot::SetupAxis(ImAxis_X1, appLanguage[Key::Time_sec], ImPlotAxisFlags_AutoFit); @@ -132,7 +90,7 @@ int main() { ImGui::Dummy({windowSize.x * .36f, 0.f}); ImGui::SameLine(); ImGui::Text(appLanguage[Key::Devices_found]); - devicesList(flagPaused); + devicesList(mWindow.flagPaused); if (!loadedFiles.empty()) { // if devices were successfully loaded from file static std::map loadedFilesChkBxs; for (auto it = loadedFiles.begin(); it != loadedFiles.end();) { @@ -160,7 +118,7 @@ int main() { loadedFilenames.erase(it->first); loadedFilesChkBxs[it->first].b = false; it = loadedFiles.erase(it); - AnalyzedFilePath = ""; + mWindow.setAnalyzePath(""); } else it++; ImGui::PopID(); diff --git a/src/mainWindow.cpp b/src/mainWindow.cpp new file mode 100644 index 00000000..0d168d73 --- /dev/null +++ b/src/mainWindow.cpp @@ -0,0 +1,14 @@ +#include "mainWindow.hpp" + +mainWindow::mainWindow() : configpath("config/config.json"){ + + AnalyzedFilePath = ""; +} + +mainWindow::~mainWindow(){ + +} + +void mainWindow::setAnalyzePath(const std::filesystem::path &AnalyzedFilePath){ + this->AnalyzedFilePath = AnalyzedFilePath; +} \ No newline at end of file diff --git a/src/mainWindow.hpp b/src/mainWindow.hpp new file mode 100644 index 00000000..f40f4889 --- /dev/null +++ b/src/mainWindow.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include +#include +#include + +class mainWindow{ + public: + nlohmann::json config; + const std::string configpath; + nlohmann::json language; + bool flagPaused{true}, open_generate_training_data{false}, open_analyze_menu{false}, open_settings{false}, LOADANALYSISDATA{false}; + + bool development{false}; + std::once_flag configFlag; + + std::filesystem::path AnalyzedFilePath; + + mainWindow(); + ~mainWindow(); + + void setAnalyzePath(const std::filesystem::path &); +}; \ No newline at end of file diff --git a/src/style.cpp b/src/style.cpp index 6c5895ee..faab2945 100644 --- a/src/style.cpp +++ b/src/style.cpp @@ -13,7 +13,7 @@ #include "../imgui-filebrowser/imfilebrowser.h" #include "popups.hpp" #include "analyze_data.hpp" - +#include "settingspopup.hpp" void SetupImGuiStyle(bool bStyleDark_, float alpha_) { @@ -248,40 +248,43 @@ bool LoadTextureFromHeader(unsigned char const *png_data, int png_data_len, return true; } -void set_side_menu(const nlohmann::json &config, bool &open_settings, - bool &open_generate_training_data, bool &open_analyze_menu, +void set_side_menu(mainWindow &mWindow, decltype(captureData) &loadedFiles, - std::map &loadedFilenames, bool &LOADANALYSISDATA) { - - auto windowSize{ImGui::GetIO().DisplaySize}; - // Initializing all variables for images - static constexpr size_t size{6}; // number of pictures - size_t PngRenderedCnt{}; - static bool loaded_png[size]; - static int image_height[size]; - static int image_width[size]; - static GLuint image_texture[size]; - - // The order matters because of the counter for the images !!! - static const unsigned char *imagesNames[] = { - AutoInternLogo_png, RefreshIcon_png, Load_file_png, - DiagnosticIcon_png, SettingIcon_png, HelpIcon_png}; - static const unsigned int imagesLen[] = { - AutoInternLogo_png_len, RefreshIcon_png_len, Load_file_png_len, - DiagnosticIcon_png_len, SettingIcon_png_len, HelpIcon_png_len}; + std::map &loadedFilenames) { + + // Variables + auto windowSize{ImGui::GetIO().DisplaySize}; + // Initializing all variables for images + static constexpr size_t size{6}; // number of pictures + size_t PngRenderedCnt{}; + static bool loaded_png[size]; + static int image_height[size]; + static int image_width[size]; + static GLuint image_texture[size]; + + // The order matters because of the counter for the images !!! + static const unsigned char *imagesNames[] = { + AutoInternLogo_png, RefreshIcon_png, Load_file_png, + DiagnosticIcon_png, SettingIcon_png, HelpIcon_png}; + static const unsigned int imagesLen[] = { + AutoInternLogo_png_len, RefreshIcon_png_len, Load_file_png_len, + DiagnosticIcon_png_len, SettingIcon_png_len, HelpIcon_png_len}; // Load the images for the SideBarMenu - for (size_t i = 0; i < size; i++) - if (!loaded_png[i]) { - if (LoadTextureFromHeader(imagesNames[i], imagesLen[i], &image_texture[i], - &image_width[i], &image_height[i])) - loaded_png[i] = true; - else - fmt::println("Error Loading Png #{}.", i); - } + for (size_t i = 0; i < size; i++) + if (!loaded_png[i]) { + if (LoadTextureFromHeader(imagesNames[i], imagesLen[i], &image_texture[i], + &image_width[i], &image_height[i])) + loaded_png[i] = true; + else + fmt::println("Error Loading Png #{}.", i); + } - float scaleWidth = windowSize.x * 0.00035f; - float scaleHeight = windowSize.y * 0.00065f; + float scaleWidth = windowSize.x * 0.00035f; + float scaleHeight = windowSize.y * 0.00065f; // Begin the SideBarMenu + + ImGui::BeginChild("Side Menu", {windowSize.x * .18f, 0.f}); + if (loaded_png[PngRenderedCnt]) { // render AIGroupLogo ImGui::Image((void *)(intptr_t)image_texture[PngRenderedCnt], ImVec2(image_width[PngRenderedCnt] * scaleWidth, @@ -295,7 +298,7 @@ void set_side_menu(const nlohmann::json &config, bool &open_settings, ImGui::ImageButtonWithText( (void *)(intptr_t)image_texture[PngRenderedCnt], appLanguage[Key::Dvc_search])) { - LOADANALYSISDATA= false; + mWindow.LOADANALYSISDATA= false; devices.clear(); deviceManager.clearDevices(); initDevices(); @@ -331,7 +334,7 @@ void set_side_menu(const nlohmann::json &config, bool &open_settings, ImGui::SetNextItemOpen(false); if (showDiag && ImGui::TreeNode(appLanguage[Key::MathematicalAnalysis])) { if (ImGui::Button(appLanguage[Key::FFT_Analyze])){ - open_analyze_menu = true; + mWindow.open_analyze_menu = true; showDiag = false; } ImGui::TreePop(); @@ -353,7 +356,7 @@ void set_side_menu(const nlohmann::json &config, bool &open_settings, ImGui::ImageButtonWithText( (void *)(intptr_t)image_texture[PngRenderedCnt], appLanguage[Key::Attitude])) { - open_settings = true; + mWindow.open_settings = true; showSettings = false; } @@ -361,17 +364,18 @@ void set_side_menu(const nlohmann::json &config, bool &open_settings, ImGui::ImageButtonWithText( (void *)(intptr_t)image_texture[PngRenderedCnt], appLanguage[Key::Help])) { - system(("start " + load_json(config, "helplink")).c_str()); + system(("start " + load_json(mWindow.config, "helplink")).c_str()); showSettings = false; } ImGui::SetCursorPosY(windowSize.y * 0.9f); ImGui::Text(fmt::format("{}: {}", appLanguage[Key::Version], "1.0.2") .c_str()); + + ImGui::EndChild(); } -void set_toolbar(const nlohmann::json &config, const nlohmann::json &language, - bool &flagPaused, const decltype(captureData) &loadedFiles,bool &LOADANALYSISDATA) { +void set_toolbar(mainWindow &mWindow, const decltype(captureData) &loadedFiles) { // variable declaration static auto now = std::chrono::system_clock::now(); @@ -389,7 +393,7 @@ void set_toolbar(const nlohmann::json &config, const nlohmann::json &language, if (ImGui::BeginPopupModal(appLanguage[Key::Save_Recorded_Data], nullptr, ImGuiWindowFlags_AlwaysAutoResize)) { ImGui::SetItemDefaultFocus(); - saves_popup(config, language, now, now_time_t, now_tm, flagDataNotSaved, + saves_popup(mWindow.config, mWindow.language, now, now_time_t, now_tm, flagDataNotSaved, has_loaded_file ? liveDvcs : captureData); ImGui::EndPopup(); } @@ -435,19 +439,19 @@ void set_toolbar(const nlohmann::json &config, const nlohmann::json &language, // ImGui::SetCursorPosY(windowSize.y * 0.05f); - if (flagPaused) { + if (mWindow.flagPaused) { // ######################## Buttonstripe if (!devices.empty()) if (!sampler.has_value()) { PngRenderedCnt = 0; - set_button_style_to(config, "start"); // Start Button + set_button_style_to(mWindow.config, "start"); // Start Button if (ImGui::ImageButton( appLanguage[Key::Start], (void *)(intptr_t)image_texture[PngRenderedCnt], ImVec2(image_width[PngRenderedCnt] * iconsSacle, image_height[PngRenderedCnt] * iconsSacle))) { sampler.emplace(deviceManager, std::move(devices)); - flagPaused = false; + mWindow.flagPaused = false; flagDataNotSaved = true; } ImGui::PopStyleColor(3); @@ -456,31 +460,31 @@ void set_toolbar(const nlohmann::json &config, const nlohmann::json &language, } else { // ############################ Stop Button PngRenderedCnt = 1; - set_button_style_to(config, "stop"); + set_button_style_to(mWindow.config, "stop"); if (ImGui::ImageButton(appLanguage[Key::Stop], (void *)(intptr_t)image_texture[PngRenderedCnt], ImVec2(image_width[PngRenderedCnt] * iconsSacle, image_height[PngRenderedCnt] * iconsSacle))) { - flagPaused = true; + mWindow.flagPaused = true; for (auto &device : sampler->sampleDevices) { device.first->send(Omniscope::Stop{}); } } ImGui::PopStyleColor(3); } - if (flagPaused) { + if (mWindow.flagPaused) { // Start/reset the measurement when the measurement is paused, // followed by a query as to whether the old data should be saved if (sampler.has_value()) { ImGui::SameLine(); PngRenderedCnt = 0; - set_button_style_to(config, "start"); + set_button_style_to(mWindow.config, "start"); if (ImGui::ImageButton( appLanguage[Key::Continue], (void *)(intptr_t)image_texture[PngRenderedCnt], ImVec2(image_width[PngRenderedCnt] * iconsSacle, image_height[PngRenderedCnt] * iconsSacle))) { - flagPaused = false; + mWindow.flagPaused = false; flagDataNotSaved = true; for (auto &device : sampler->sampleDevices) { device.first->send(Omniscope::Start{}); @@ -490,7 +494,7 @@ void set_toolbar(const nlohmann::json &config, const nlohmann::json &language, ImGui::SameLine(); PngRenderedCnt = 3; - set_button_style_to(config, "stop"); + set_button_style_to(mWindow.config, "stop"); if (ImGui::ImageButton( appLanguage[Key::Reset], (void *)(intptr_t)image_texture[PngRenderedCnt], @@ -499,9 +503,9 @@ void set_toolbar(const nlohmann::json &config, const nlohmann::json &language, if (flagDataNotSaved) { ImGui::OpenPopup(appLanguage[Key::Reset_q]); } else { - LOADANALYSISDATA = false; + mWindow.LOADANALYSISDATA = false; rstSettings(loadedFiles); - flagPaused = true; + mWindow.flagPaused = true; } } ImGui::PopStyleColor(3); @@ -585,4 +589,35 @@ void PopupStyleEditor() { // For Development void SetHorizontalSepeareatorColours() { ImGuiStyle &style = ImGui::GetStyle(); style.Colors[ImGuiCol_Separator] = ImVec4(1.0f, 0.0f, 0.0f, 1.0f); -} \ No newline at end of file +} + +void generatePopUpMenus(mainWindow &mWindow){ + static int title = 0; + static std::vector titles(2); // two languages + if (mWindow.open_settings) { + const auto EngItr = englishLan.find(Key::Settings); + const auto GrmItr = germanLan.find(Key::Settings); + // check returned value from find() and set titles + if (EngItr != englishLan.end() && GrmItr != germanLan.end()) { + titles[0] = (std::string)EngItr->second + "###ID"; + titles[1] = (std::string)GrmItr->second + "###ID"; + ImGui::OpenPopup(titles[title].c_str()); + } else + fmt::println("Settings values not found."); + mWindow.open_settings = false; + } + if (ImGui::BeginPopupModal(titles[title].c_str(), nullptr, + ImGuiWindowFlags_AlwaysAutoResize)) { + ImGui::SetItemDefaultFocus(); + popup_settings(mWindow.config, mWindow.configpath, title); + ImGui::EndPopup(); + } + // Generate training data popup + if (mWindow.open_generate_training_data) + generateTrainingData(mWindow.open_generate_training_data, captureData, + savedFileNames); + + // Generate analyze data popup + if (mWindow.open_analyze_menu) + mWindow.setAnalyzePath(generate_analyze_menu(mWindow.open_analyze_menu, mWindow.LOADANALYSISDATA, captureData)); +}; \ No newline at end of file diff --git a/src/style.hpp b/src/style.hpp index 1c9ed56b..cf291f29 100644 --- a/src/style.hpp +++ b/src/style.hpp @@ -17,15 +17,13 @@ inline constexpr ImVec4 normColStyle{0.1f, 0.1f, 0.1f, 1.f}; void SetupImGuiStyle(bool, float); void set_button_style_to(const nlohmann::json &, const std::string &); bool LoadTextureFromHeader(unsigned char const *, int, GLuint *, int *, int *); -void set_side_menu(const nlohmann::json &, bool &, bool &, bool &, - decltype(captureData) &, - std::map &, bool &); -void set_toolbar(const nlohmann::json &, const nlohmann::json &, bool &, - const decltype(captureData) &, bool&); +void set_side_menu(mainWindow &, decltype(captureData) &, std::map &); +void set_toolbar(mainWindow &, const decltype(captureData) &); void PopupStyleEditor(); void PushPlotRegionColors(); void PopPlotRegionColors(); void SetDeviceMenuStyle(); +void generatePopUpMenus(mainWindow &); namespace ImGui { IMGUI_API bool ImageButtonWithText(ImTextureID texId, const char *label,