File: bitview.cpp

package info (click to toggle)
satdump 1.2.2%2Bgb79af48-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 81,648 kB
  • sloc: cpp: 276,768; ansic: 164,598; lisp: 1,219; sh: 283; xml: 106; makefile: 7
file content (204 lines) | stat: -rw-r--r-- 7,184 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "bitview.h"
#include "imgui/imgui_image.h"
#include "imgui/imgui_stdlib.h"
#include "imgui/implot/implot.h"
#include <fstream>
#include "core/style.h"

#include <fcntl.h>
#include "common/utils.h"
#include <filesystem>

#include "logger.h"

//////
#include "tools/deframer.h"
#include "tools/diff_decode.h"
#include "tools/soft2hard.h"
#include "tools/ccsds_vcid_splitter.h"

namespace satdump
{
    BitViewApplication::BitViewApplication()
        : Application("bitview")
    {
        all_tools.push_back(std::make_shared<DeframerTool>());
        all_tools.push_back(std::make_shared<DifferentialTool>());
        all_tools.push_back(std::make_shared<Soft2HardTool>());
        all_tools.push_back(std::make_shared<CCSDSVcidSplitterTool>());
    }

    BitViewApplication::~BitViewApplication()
    {
    }

    void BitViewApplication::drawUI()
    {
        ImVec2 bitviewer_size = ImGui::GetContentRegionAvail();

        if (ImGui::BeginTable("##bitviewer_table", 2, ImGuiTableFlags_NoBordersInBodyUntilResize | ImGuiTableFlags_Resizable | ImGuiTableFlags_SizingStretchProp))
        {
            ImGui::TableSetupColumn("##bitpanel_v", ImGuiTableColumnFlags_None, bitviewer_size.x * panel_ratio);
            ImGui::TableSetupColumn("##bitview", ImGuiTableColumnFlags_None, bitviewer_size.x * (1.0f - panel_ratio));
            ImGui::TableNextColumn();

            float left_width = ImGui::GetColumnWidth(0);
            float right_width = bitviewer_size.x - left_width;
            if (left_width != last_width && last_width != -1)
                panel_ratio = left_width / bitviewer_size.x;
            last_width = left_width;

            ImGui::BeginChild("BitViewerChildPanel", {left_width, float(bitviewer_size.y - 10)});
            drawPanel();
            ImGui::EndChild();

            ImGui::TableNextColumn();
            ImGui::BeginGroup();
            drawContents();
            ImGui::EndGroup();
            ImGui::EndTable();
        }
    }

    void renderBitContainers(std::vector<std::shared_ptr<satdump::BitContainer>> &all_bit_containers, std::shared_ptr<satdump::BitContainer> &current_container)
    {
        for (int i = 0; i < all_bit_containers.size(); i++)
        {
            auto &cont = all_bit_containers[i];

            if (!cont)
                continue;

            ImGui::TreeNodeEx(cont->getName().c_str(), ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen | (cont == current_container ? ImGuiTreeNodeFlags_Selected : 0));
            if (ImGui::IsItemClicked())
            {
                current_container = cont;
            }
            ImGui::TreePush(std::string("##BitViewerTree" + cont->getID()).c_str());

            bool del = false;
            { // Closing button
                ImGui::SameLine();
                ImGui::Text("  ");
                ImGui::SameLine();

                ImGui::PushStyleColor(ImGuiCol_Text, style::theme.red.Value);
                ImGui::PushStyleColor(ImGuiCol_Button, ImVec4());
                ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 0);
                if (ImGui::SmallButton(std::string(u8"\uf00d##dataset" + cont->getName()).c_str()))
                {
                    logger->info("Closing bit container " + cont->getName());
                    del = true;
                }
                ImGui::PopStyleVar();
                ImGui::PopStyleColor(2);
            }

            renderBitContainers(cont->all_bit_containers, current_container);
            ImGui::TreePop();

            if (del)
                cont.reset();
        }
    }

    void BitViewApplication::drawPanel()
    {
        if (is_busy)
            style::beginDisabled();
        if (ImGui::CollapsingHeader("Files##bitview", ImGuiTreeNodeFlags_DefaultOpen))
        {
            ImGui::Text("Load File :");
            if (select_bitfile_dialog.draw() && select_bitfile_dialog.isValid())
            {
                try
                {
                    all_bit_containers.push_back(std::make_shared<BitContainer>(std::filesystem::path(select_bitfile_dialog.getPath()).stem().string(), select_bitfile_dialog.getPath()));
                }
                catch (std::exception &e)
                {
                    logger->error("Could not load file: %s", e.what());
                }
            }

            ImGui::Separator();

            renderBitContainers(all_bit_containers, current_bit_container);
        }
        if (ImGui::CollapsingHeader("Control"))
        {
            if (current_bit_container)
            {
                double vv = current_bit_container->d_bitperiod;
                if (ImGui::InputDouble("Period", &vv))
                {
                    current_bit_container->d_bitperiod = vv;
                    current_bit_container->init_bitperiod();
                    current_bit_container->forceUpdateAll();
                }

                if (ImGui::RadioButton("Bits", current_bit_container->d_display_mode == 0))
                {
                    current_bit_container->d_display_mode = 0;
                    current_bit_container->forceUpdateAll();
                }
                if (ImGui::RadioButton("Bytes", current_bit_container->d_display_mode == 1))
                {
                    current_bit_container->d_display_mode = 1;
                    current_bit_container->forceUpdateAll();
                }
            }
        }
        if (is_busy)
            style::endDisabled();
        if (ImGui::CollapsingHeader("Tools"))
        {
            if (current_bit_container)
            {
                for (auto &tool : all_tools)
                {
                    ImGui::Separator();
                    ImGui::Text("%s", tool->getName().c_str());
                    ImGui::Separator();

                    tool->renderMenu(current_bit_container, is_busy);

                    if (tool->needToProcess())
                    {
                        tool->setProcessed();
                        auto func = [this, tool](int)
                        {
                            tool->process(current_bit_container, process_progress);
                            is_busy = false;
                        };
                        is_busy = true;
                        process_threadp.push(func);
                    }
                }

                ImGui::Spacing();

                ImGui::ProgressBar(process_progress);
            }
        }
    }

    void BitViewApplication::drawContents()
    {
        ImVec2 window_size = ImGui::GetContentRegionAvail();

        if (current_bit_container)
            current_bit_container->doUpdateTextures();

        ImPlot::BeginPlot("MainPlot", window_size, ImPlotFlags_Equal | ImPlotFlags_NoLegend);
        ImPlot::SetupAxes(nullptr, nullptr, 0, ImPlotAxisFlags_Invert);

        ImPlotRect c = ImPlot::GetPlotLimits();
        if (current_bit_container)
            current_bit_container->doDrawPlotTextures(c);

        // ImPlot::GetPlotDrawList()->AddRectFilled(ImPlot::PlotToPixels({0, 0}), ImPlot::PlotToPixels({1, 1}), ImColor(255, 0, 0, 255 * 0.5));

        ImPlot::EndPlot();
    }
}