Skip to content

Commit

Permalink
Merge pull request #185 from skunkforce/AK/Refactoring
Browse files Browse the repository at this point in the history
Refactoring Part 1
  • Loading branch information
AKMaily authored Oct 23, 2024
2 parents f51d309 + 3f36e0d commit a14b275
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 137 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/analyze_data.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma once

#include <fstream>
#include <regex>
#include <imgui.h>
Expand Down
23 changes: 16 additions & 7 deletions src/handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ static std::vector<AxisInfo> getDeviceInfos() {
return axisInfos;
}

void addPlots(const char *name, fs::path &AnalyzedFilePath, bool &LOADANALYSISDATA,
void addPlots(const char *name, mainWindow &mWindow,
std::function<void(double, std::string, ImAxis_, double, double)>
axesSetup) {
static std::set<std::string> firstRun;
Expand All @@ -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();
}
Expand Down Expand Up @@ -339,17 +339,17 @@ 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");
update_config_from_github();
}
}

void set_json(nlohmann::json &config) {
if (fs::exists(load_json<std::string>(config, ("languagepath"))))
void set_json(mainWindow &mWindow) {
if (fs::exists(load_json<std::string>(mWindow.config, ("languagepath"))))
fmt::print("Found language: {}\n\r", appLanguage[Key::German]);
else {
fmt::print("Did not find {}.\n Download from Github\n\r",
Expand Down Expand Up @@ -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<std::string>(mWindow.config, "languagepath") +
load_json<std::string>(mWindow.config, "language") + ".json");
}

//TODO : Set this also up for saved OmniScope files

void AddPlotFromFile(fs::path &filePath) {
Expand Down
8 changes: 5 additions & 3 deletions src/handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <implot.h>
Expand All @@ -19,17 +20,18 @@ inline std::map<Omniscope::Id, std::vector<std::pair<double, double>>>
captureData;

void addPlots(
const char *,fs::path &,bool &,
const char *, mainWindow &,
std::function<void(double, std::string, ImAxis_, double, double)>);
void parseDeviceMetaData(Omniscope::MetaData,
std::shared_ptr<OmniscopeDevice> &);
void initDevices();
void devicesList(bool const &flagPaused);
void load_files(decltype(captureData) &, std::map<Omniscope::Id, std::string> &,
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 &);

Expand Down
102 changes: 30 additions & 72 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>(config, "languagepath") +
load_json<std::string>(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<Omniscope::Id, std::string> 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<Omniscope::Id, std::string> 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<std::string> 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});
Expand All @@ -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);
Expand All @@ -132,7 +90,7 @@ int main() {
ImGui::Dummy({windowSize.x * .36f, 0.f});
ImGui::SameLine();
ImGui::Text(appLanguage[Key::Devices_found]);

Check warning on line 92 in src/main.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-24.04-C++ CI

format not a string literal and no format arguments [-Wformat-security]
devicesList(flagPaused);
devicesList(mWindow.flagPaused);
if (!loadedFiles.empty()) { // if devices were successfully loaded from file
static std::map<Omniscope::Id, BoolWrapper> loadedFilesChkBxs;
for (auto it = loadedFiles.begin(); it != loadedFiles.end();) {
Expand Down Expand Up @@ -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();
Expand Down
14 changes: 14 additions & 0 deletions src/mainWindow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "mainWindow.hpp"

mainWindow::mainWindow() : configpath("config/config.json"){

AnalyzedFilePath = "";
}

mainWindow::~mainWindow(){

}

void mainWindow::setAnalyzePath(const std::filesystem::path &AnalyzedFilePath){

Check warning on line 12 in src/mainWindow.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-24.04-C++ CI

declaration of ‘AnalyzedFilePath’ shadows a member of ‘mainWindow’ [-Wshadow]
this->AnalyzedFilePath = AnalyzedFilePath;
}
23 changes: 23 additions & 0 deletions src/mainWindow.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <nlohmann/json.hpp>
#include <nlohmann/json_fwd.hpp>
#include <mutex>

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 &);
};
Loading

0 comments on commit a14b275

Please sign in to comment.