satdump/src-core/dsp/flowgraph/dsp_flowgraph_handler.cpp

154 lines
5 KiB
C++
Raw Normal View History

2025-03-09 10:16:40 +01:00
#include "dsp_flowgraph_handler.h"
2026-01-25 10:24:19 +01:00
#include "common/widgets/menuitem_tooltip.h"
2026-01-14 17:16:33 +01:00
#include "core/backend.h"
2026-01-16 18:16:43 +01:00
#include "dsp/flowgraph/dsp_flowgraph_register.h"
2026-01-14 17:16:33 +01:00
#include "imgui/imgui.h"
2026-01-24 00:04:14 +01:00
#include "imgui/imgui_flags.h"
2025-03-09 10:16:40 +01:00
#include "imgui/imnodes/imnodes.h"
#include "logger.h"
#include "nlohmann/json_utils.h"
2025-06-06 08:12:02 +02:00
#include <complex.h>
2026-01-14 17:16:33 +01:00
#include <fstream>
2025-04-15 19:06:12 +01:00
2025-03-09 10:16:40 +01:00
namespace satdump
{
namespace handlers
2025-03-09 10:16:40 +01:00
{
DSPFlowGraphHandler::DSPFlowGraphHandler(std::string file)
2025-03-09 10:16:40 +01:00
{
// TODOREWORK
if (ImNodes::GetCurrentContext() == nullptr)
ImNodes::CreateContext();
2025-05-04 17:27:35 +02:00
handler_tree_icon = u8"\uf92f";
2025-03-09 10:16:40 +01:00
2026-01-16 18:16:43 +01:00
ndsp::registerNodesInFlowgraph(flowgraph);
if (file != "")
flowgraph.setJSON(loadCborFile(file));
current_file = file;
2025-03-09 10:16:40 +01:00
}
DSPFlowGraphHandler::~DSPFlowGraphHandler()
{
if (flow_thread.joinable())
flow_thread.join();
}
void DSPFlowGraphHandler::drawMenu()
{
bool running = flowgraph.is_running;
if (ImGui::CollapsingHeader("Variables"))
{
if (running)
style::beginDisabled();
for (auto &v : flowgraph.variables)
{
ImGui::Text("%s", v.first.c_str());
ImGui::SameLine();
ImGui::InputDouble(std::string("##" + v.first).c_str(), &v.second);
}
ImGui::InputText("##addvarname", &to_add_var_name);
ImGui::SameLine();
if (ImGui::Button("Add##addnewvariable") && to_add_var_name.size())
{
flowgraph.variables.emplace(to_add_var_name, 0);
to_add_var_name.clear();
}
2025-03-09 10:16:40 +01:00
if (running)
style::endDisabled();
}
}
2026-01-14 17:16:33 +01:00
void DSPFlowGraphHandler::drawMenuBar()
{
2026-01-23 17:56:57 +01:00
bool running = flowgraph.is_running;
if (running)
{
2026-01-25 10:24:19 +01:00
if (widgets::MenuItemTooltip("\ufc62", "Stop flowgraph"))
2026-01-23 17:56:57 +01:00
flowgraph.stop();
}
else
{
2026-01-25 10:24:19 +01:00
if (widgets::MenuItemTooltip("\uf909", "Start flowgraph"))
2026-01-23 17:56:57 +01:00
{
if (flow_thread.joinable())
flow_thread.join();
auto fun = [&]() { flowgraph.run(); };
flow_thread = std::thread(fun);
}
}
2026-01-23 17:31:19 +01:00
bool is_save_as = false;
2026-01-25 10:24:19 +01:00
if (widgets::MenuItemTooltip("\ueb4b", "Save file", NULL, false, current_file != "") || //
(is_save_as = widgets::MenuItemTooltip("\ueb4a", "Save file as")) || //
2026-01-23 17:31:19 +01:00
(ImGui::IsKeyDown(ImGuiKey_ReservedForModCtrl) && ImGui::IsKeyPressed(ImGuiKey_S)))
2026-01-14 17:16:33 +01:00
{
2026-01-23 17:31:19 +01:00
auto fun = [this, is_save_as]()
2026-01-14 17:16:33 +01:00
{
2026-01-23 17:31:19 +01:00
std::string save_at = is_save_as ? backend::saveFileDialog({{"SatDump DSP Flowgraph", "satdump_dsp_flowgraph"}}, "", "flowgraph.satdump_dsp_flowgraph") : current_file;
2026-01-14 17:16:33 +01:00
logger->info("Saving flowgraph : " + save_at);
2026-01-14 17:16:33 +01:00
if (save_at == "")
return;
saveCborFile(save_at, flowgraph.getJSON());
2026-01-23 17:31:19 +01:00
current_file = save_at;
2026-01-14 17:16:33 +01:00
};
tq.push(fun);
}
2026-01-25 10:24:19 +01:00
if (widgets::MenuItemTooltip("\uea7f", "Load a file"))
2026-01-14 17:16:33 +01:00
{
auto fun = [this]()
{
std::string load_at = backend::selectFileDialog({{"SatDump DSP Flowgraph", "satdump_dsp_flowgraph"}}, "");
2026-01-14 17:16:33 +01:00
2026-01-20 13:08:46 +01:00
logger->info("Loading flowgraph : " + load_at);
2026-01-14 17:16:33 +01:00
if (load_at == "")
return;
flowgraph.setJSON(loadCborFile(load_at));
2026-01-14 17:16:33 +01:00
};
tq.push(fun);
}
2026-01-23 17:31:19 +01:00
#if 0
2026-01-14 17:16:33 +01:00
if (ImGui::MenuItem("To Clipboard"))
{
std::string json = flowgraph.getJSON().dump(4);
ImGui::SetClipboardText(json.c_str());
}
if (ImGui::MenuItem("From Clipboard"))
{
nlohmann::json v = nlohmann::json::parse(ImGui::GetClipboardText());
flowgraph.setJSON(v);
}
2026-01-23 17:31:19 +01:00
#endif
2026-01-14 17:16:33 +01:00
}
2026-01-24 00:04:14 +01:00
void DSPFlowGraphHandler::drawContents(ImVec2 win_size)
{
ctx.config().color = ImGui::GetColorU32(ImGuiCol_WindowBg);
ctx.begin();
float f = 15;
ImGui::SetNextWindowPos({-f / ctx.scale(), -f / ctx.scale()});
ImGui::SetNextWindowSize({(float)(win_size.x / ctx.scale()) + 2 * (f / ctx.scale()), (float)(win_size.y / ctx.scale()) + 2 * (f / ctx.scale())});
ImGui::Begin("SatDump UI", nullptr, NOWINDOW_FLAGS | ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoScrollWithMouse);
flowgraph.render();
ImGui::End();
ctx.end();
}
} // namespace handlers
} // namespace satdump