From 2c334c08ac632a6ccfd8b1fa62fe4a2073563796 Mon Sep 17 00:00:00 2001
From: Ryzerth <whatsthetgeek@gmail.com>
Date: Wed, 14 Apr 2021 01:45:21 +0200
Subject: [PATCH] Added position option for the bandplan

---
 core/src/core.cpp                  |  1 +
 core/src/gui/menus/bandplan.cpp    | 18 ++++++++++++++++-
 core/src/gui/widgets/waterfall.cpp | 31 ++++++++++++++++++++++--------
 core/src/gui/widgets/waterfall.h   | 10 ++++++++++
 4 files changed, 51 insertions(+), 9 deletions(-)

diff --git a/core/src/core.cpp b/core/src/core.cpp
index 5c5b8494..6a288b8e 100644
--- a/core/src/core.cpp
+++ b/core/src/core.cpp
@@ -104,6 +104,7 @@ int sdrpp_main(int argc, char *argv[]) {
     defConfig["bandColors"]["military"] = "#FFFF00FF";
     defConfig["bandPlan"] = "General";
     defConfig["bandPlanEnabled"] = true;
+    defConfig["bandPlanPos"] = 0;
     defConfig["centerTuning"] = false;
     defConfig["colorMap"] = "Classic";
     defConfig["fastFFT"] = false;
diff --git a/core/src/gui/menus/bandplan.cpp b/core/src/gui/menus/bandplan.cpp
index 7cb8664a..832ae90f 100644
--- a/core/src/gui/menus/bandplan.cpp
+++ b/core/src/gui/menus/bandplan.cpp
@@ -6,6 +6,9 @@
 namespace bandplanmenu {
     int bandplanId;
     bool bandPlanEnabled;
+    int bandPlanPos = 0;
+
+    const char* bandPlanPosTxt = "Bottom\0Top\0";
 
     void init() {
         // todo: check if the bandplan wasn't removed
@@ -26,18 +29,31 @@ namespace bandplanmenu {
 
         bandPlanEnabled = core::configManager.conf["bandPlanEnabled"];
         bandPlanEnabled ? gui::waterfall.showBandplan() : gui::waterfall.hideBandplan();
+        bandPlanPos = core::configManager.conf["bandPlanPos"];
+        gui::waterfall.setBandPlanPos(bandPlanPos);
     }
 
     void draw(void* ctx) {
         float menuColumnWidth = ImGui::GetContentRegionAvailWidth();
         ImGui::PushItemWidth(menuColumnWidth);
-        if (ImGui::Combo("##_4_", &bandplanId, bandplan::bandplanNameTxt.c_str())) {
+        if (ImGui::Combo("##_bandplan_name_", &bandplanId, bandplan::bandplanNameTxt.c_str())) {
             gui::waterfall.bandplan = &bandplan::bandplans[bandplan::bandplanNames[bandplanId]];
             core::configManager.aquire();
             core::configManager.conf["bandPlan"] = bandplan::bandplanNames[bandplanId];
             core::configManager.release(true);
         }
         ImGui::PopItemWidth();
+
+        ImGui::Text("Position");
+        ImGui::SameLine();
+        ImGui::SetNextItemWidth(menuColumnWidth - ImGui::GetCursorPosX());
+        if (ImGui::Combo("##_bandplan_pos_", &bandPlanPos, bandPlanPosTxt)) {
+            gui::waterfall.setBandPlanPos(bandPlanPos);
+            core::configManager.aquire();
+            core::configManager.conf["bandPlanPos"] = bandPlanPos;
+            core::configManager.release(true);
+        }
+
         if (ImGui::Checkbox("Enabled", &bandPlanEnabled)) {
             bandPlanEnabled ? gui::waterfall.showBandplan() : gui::waterfall.hideBandplan();
             core::configManager.aquire();
diff --git a/core/src/gui/widgets/waterfall.cpp b/core/src/gui/widgets/waterfall.cpp
index 86f26157..d64b1cd1 100644
--- a/core/src/gui/widgets/waterfall.cpp
+++ b/core/src/gui/widgets/waterfall.cpp
@@ -367,6 +367,18 @@ namespace ImGui {
         ImVec2 txtSz;
         bool startVis, endVis;
         uint32_t color, colorTrans;
+
+        float height = ImGui::CalcTextSize("0").y * 2.5f;
+        float bpBottom;
+
+        if (bandPlanPos == BANDPLAN_POS_BOTTOM) {
+            bpBottom = widgetPos.y + fftHeight + 10;
+        }
+        else {
+            bpBottom = widgetPos.y + height + 10;
+        }
+        
+
         for (int i = 0; i < count; i++) {
             start = bandplan->bands[i].start;
             end = bandplan->bands[i].end;
@@ -386,7 +398,6 @@ namespace ImGui {
             cPos = widgetPos.x + 50 + ((center - lowerFreq) * horizScale);
             width = bPos - aPos;
             txtSz = ImGui::CalcTextSize(bandplan->bands[i].name.c_str());
-            float height = txtSz.y * 2.5f;
             if (bandplan::colorTable.find(bandplan->bands[i].type.c_str()) != bandplan::colorTable.end()) {
                 color = bandplan::colorTable[bandplan->bands[i].type].colorValue;
                 colorTrans = bandplan::colorTable[bandplan->bands[i].type].transColorValue;
@@ -402,19 +413,19 @@ namespace ImGui {
                 bPos = widgetPos.x + 51;
             }
             if (width >= 1.0) {
-                window->DrawList->AddRectFilled(ImVec2(roundf(aPos), widgetPos.y + fftHeight + 10 - height), 
-                                        ImVec2(roundf(bPos), widgetPos.y + fftHeight + 10), colorTrans);
+                window->DrawList->AddRectFilled(ImVec2(roundf(aPos), bpBottom - height), 
+                                        ImVec2(roundf(bPos), bpBottom), colorTrans);
                 if (startVis) {
-                    window->DrawList->AddLine(ImVec2(roundf(aPos), widgetPos.y + fftHeight + 10 - height - 1), 
-                                        ImVec2(roundf(aPos), widgetPos.y + fftHeight + 9), color);
+                    window->DrawList->AddLine(ImVec2(roundf(aPos), bpBottom - height - 1), 
+                                        ImVec2(roundf(aPos), bpBottom - 1), color);
                 }
                 if (endVis) {
-                    window->DrawList->AddLine(ImVec2(roundf(bPos), widgetPos.y + fftHeight + 10 - height - 1), 
-                                        ImVec2(roundf(bPos), widgetPos.y + fftHeight + 9), color);
+                    window->DrawList->AddLine(ImVec2(roundf(bPos), bpBottom - height - 1), 
+                                        ImVec2(roundf(bPos), bpBottom - 1), color);
                 }
             }
             if (txtSz.x <= width) {
-                window->DrawList->AddText(ImVec2(cPos - (txtSz.x / 2.0), widgetPos.y + fftHeight + 10 - (height / 2.0f) - (txtSz.y / 2.0f)), 
+                window->DrawList->AddText(ImVec2(cPos - (txtSz.x / 2.0), bpBottom - (height / 2.0f) - (txtSz.y / 2.0f)), 
                                     IM_COL32(255, 255, 255, 255), bandplan->bands[i].name.c_str());
             }
         }
@@ -821,6 +832,10 @@ namespace ImGui {
         memset(rawFFTs, 0, rawFFTSize * waterfallHeight * sizeof(float));
     }
 
+    void WaterFall::setBandPlanPos(int pos) {
+        bandPlanPos = pos;
+    }
+
     void WaterfallVFO::setOffset(double offset) {
         generalOffset = offset;
         if (reference == REF_CENTER) {
diff --git a/core/src/gui/widgets/waterfall.h b/core/src/gui/widgets/waterfall.h
index eb79b06f..18e9238d 100644
--- a/core/src/gui/widgets/waterfall.h
+++ b/core/src/gui/widgets/waterfall.h
@@ -109,6 +109,8 @@ namespace ImGui {
 
         void setFullWaterfallUpdate(bool fullUpdate);
 
+        void setBandPlanPos(int pos);
+
         bool centerFreqMoved = false;
         bool vfoFreqChanged = false;
         bool bandplanEnabled = false;
@@ -125,6 +127,12 @@ namespace ImGui {
             _REF_COUNT
         };
 
+        enum {
+            BANDPLAN_POS_BOTTOM,
+            BANDPLAN_POS_TOP,
+            _BANDPLAN_POS_COUNT
+        };
+
 
     private:
         void drawWaterfall();
@@ -210,5 +218,7 @@ namespace ImGui {
 
         bool _fastFFT = true;
         bool _fullUpdate = true;
+
+        int bandPlanPos = BANDPLAN_POS_BOTTOM;
     };
 };
\ No newline at end of file