satdump/src-core/handler/handler.cpp

153 lines
5.5 KiB
C++
Raw Normal View History

2024-12-31 17:01:12 +01:00
#include "handler.h"
2025-05-13 09:15:20 +02:00
#include "core/style.h"
2024-12-31 17:01:12 +01:00
#include "imgui/imgui.h"
2025-04-19 12:01:12 +02:00
#include "logger.h"
2024-12-31 17:01:12 +01:00
//// TODOREWORK?
2025-01-01 05:57:58 +01:00
#include "product/image_product_handler.h"
#include <utility>
2024-12-31 17:01:12 +01:00
namespace satdump
{
namespace viewer
{
2025-01-02 17:04:32 +01:00
inline ImGuiTreeNodeFlags nodeFlags(std::shared_ptr<Handler> &h, bool sec)
{
ImGuiTreeNodeFlags flags = ImGuiTreeNodeFlags_None;
2025-02-07 22:57:27 +01:00
// if (!h->hasSubhandlers())
flags |= ImGuiTreeNodeFlags_Leaf; // TODOREWORK?
2025-01-02 17:04:32 +01:00
if (sec)
flags |= ImGuiTreeNodeFlags_Selected;
return flags;
}
bool Handler::isParent(const std::shared_ptr<Handler> &dragged, const std::shared_ptr<Handler> &potential_child)
{
for (auto &s : dragged->subhandlers)
{
if (s == potential_child)
return true;
2025-03-14 14:51:14 +01:00
if (isParent(s, potential_child))
return true;
}
return false;
}
2024-12-31 17:01:12 +01:00
void Handler::drawTreeMenu(std::shared_ptr<Handler> &h)
{
// TODOREWORK CLEANUP
struct DragDropWip
{
std::shared_ptr<Handler> drag;
2025-05-05 21:03:57 +02:00
Handler *hdel;
2024-12-31 17:01:12 +01:00
};
tree_local.start();
for (int i = 0; i < subhandlers.size(); i++)
{
auto &handler = subhandlers[i];
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
2025-05-13 09:15:20 +02:00
bool tree_extended = ImGui::TreeNodeEx(handler->getTreeID().c_str(), nodeFlags(handler, h == handler));
2025-05-12 06:35:58 +02:00
tree_local.node(handler->handler_tree_icon);
2025-05-13 09:15:20 +02:00
if (tree_extended)
2024-12-31 17:01:12 +01:00
{
if (ImGui::IsItemClicked())
h = handler;
2025-04-06 10:27:54 +02:00
if (ImGui::IsItemHovered())
ImGui::SetTooltip("%s", handler->getName().c_str());
2024-12-31 17:01:12 +01:00
// TODOREWORK CLEANUP
2025-05-05 21:03:57 +02:00
if (handler_can_subhandlers_be_dragged && handler->handler_can_be_dragged && ImGui::BeginDragDropSource(ImGuiDragDropFlags_SourceAllowNullID))
2024-12-31 17:01:12 +01:00
{
2025-05-05 21:03:57 +02:00
DragDropWip d({handler, this});
2025-01-01 18:43:35 +01:00
ImGui::SetDragDropPayload("VIEWER_HANDLER", &d, sizeof(DragDropWip));
2024-12-31 17:01:12 +01:00
ImGui::Text("%s", handler->getName().c_str());
ImGui::EndDragDropSource();
}
if (handler->handler_can_be_dragged_to && ImGui::BeginDragDropTarget())
2024-12-31 17:01:12 +01:00
{
if (const ImGuiPayload *payload = ImGui::AcceptDragDropPayload("VIEWER_HANDLER"))
{
IM_ASSERT(payload->DataSize == sizeof(DragDropWip));
const DragDropWip *payload_n = (const DragDropWip *)payload->Data;
2025-05-05 21:03:57 +02:00
logger->info("Handler " + handler->getName() + " got drag of " + payload_n->drag->getName());
if (!isParent(payload_n->drag, handler))
{
handler->addSubHandler(payload_n->drag);
2025-05-05 21:03:57 +02:00
payload_n->hdel->delSubHandler(payload_n->drag);
}
else
{
logger->error("Can't drag parent onto child handler!");
}
2024-12-31 17:01:12 +01:00
}
ImGui::EndDragDropTarget();
}
// TODOREWORK CLEANUP
2025-05-13 09:15:20 +02:00
if (handler_can_be_reorg)
2025-05-12 06:35:58 +02:00
{
2025-05-18 20:37:09 +02:00
float xpos = ImGui::GetWindowWidth();
2025-05-13 09:15:20 +02:00
if (i > 0)
{
2025-05-13 09:15:20 +02:00
ImGui::SameLine();
2025-05-18 20:37:09 +02:00
ImGui::SetCursorPosX(xpos - 35 * ui_scale);
ImGui::Text(u8"\ueaf4");
2025-05-13 09:15:20 +02:00
if (ImGui::IsItemClicked(ImGuiMouseButton_Left))
std::swap(subhandlers[i], subhandlers[i - 1]);
}
2025-05-13 09:15:20 +02:00
if (i != (int)subhandlers.size() - 1)
{
2025-05-13 09:15:20 +02:00
ImGui::SameLine();
2025-05-18 20:37:09 +02:00
ImGui::SetCursorPosX(xpos - 55 * ui_scale);
ImGui::Text(u8"\ueaf3");
2025-05-13 09:15:20 +02:00
if (ImGui::IsItemClicked(ImGuiMouseButton_Left))
std::swap(subhandlers[i], subhandlers[i + 1]);
}
2025-05-12 06:35:58 +02:00
}
2025-05-13 09:15:20 +02:00
handler->drawTreeMenu(h);
ImGui::TreePop();
2025-05-12 06:35:58 +02:00
}
2024-12-31 17:01:12 +01:00
}
tree_local.end();
// Try handle deletion
2025-02-04 19:49:31 +01:00
delSubHandlersNow();
2024-12-31 17:01:12 +01:00
}
bool Handler::hasSubhandlers()
{
subhandlers_mtx.lock();
bool has = subhandlers.size();
subhandlers_mtx.unlock();
return has;
}
void Handler::addSubHandler(std::shared_ptr<Handler> handler)
{
subhandlers_mtx.lock();
subhandlers.push_back(handler);
subhandlers_mtx.unlock();
}
2025-02-04 19:49:31 +01:00
void Handler::delSubHandler(std::shared_ptr<Handler> handler, bool now)
2024-12-31 17:01:12 +01:00
{
subhandlers_mtx.lock();
subhandlers_marked_for_del.push_back(handler);
subhandlers_mtx.unlock();
2025-02-04 19:49:31 +01:00
if (now)
delSubHandlersNow();
2024-12-31 17:01:12 +01:00
}
2025-04-14 19:03:42 +01:00
nlohmann::json Handler::getConfig() { return {}; }
} // namespace viewer
} // namespace satdump