From 96488416b52eaa221dd3258ab8b7c62247926a10 Mon Sep 17 00:00:00 2001
From: Matthias Puchner <github@mpuchner.de>
Date: Fri, 22 Oct 2021 08:37:22 +0200
Subject: [PATCH] improve unit switching (two auto-exclusive actions)

---
 .../LayerOrientedSampleEditor.cpp             | 34 ++++++++++++++-----
 .../LayerOrientedSampleEditor.h               |  4 +--
 2 files changed, 28 insertions(+), 10 deletions(-)

diff --git a/GUI/Views/SampleDesigner/LayerOrientedSampleEditor.cpp b/GUI/Views/SampleDesigner/LayerOrientedSampleEditor.cpp
index 0767aa78945..c14b68653be 100644
--- a/GUI/Views/SampleDesigner/LayerOrientedSampleEditor.cpp
+++ b/GUI/Views/SampleDesigner/LayerOrientedSampleEditor.cpp
@@ -20,6 +20,7 @@
 #include "GUI/Views/SampleDesigner/MultiLayerForm.h"
 #include "GUI/Views/SampleDesigner/SampleEditorController.h"
 
+#include "GUI/Application/Application.h"
 #include <QBoxLayout>
 #include <QScrollArea>
 
@@ -29,7 +30,7 @@ LayerOrientedSampleEditor::LayerOrientedSampleEditor(QWidget* parent)
     , m_undoAction(nullptr)
     , m_redoAction(nullptr)
     , m_showInlineEditButtonsAction(nullptr)
-    , m_asAngstrom(nullptr)
+    , m_asAngstromAction(nullptr)
     , m_currentMultiLayerItem(nullptr)
 {
     setAttribute(Qt::WA_StyledBackground, true);
@@ -55,11 +56,27 @@ LayerOrientedSampleEditor::LayerOrientedSampleEditor(QWidget* parent)
     m_showInlineEditButtonsAction->setChecked(false);
     m_toolbar->addAction(m_showInlineEditButtonsAction);
 
-    m_asAngstrom = new QAction("A", this);
-    connect(m_asAngstrom, &QAction::toggled, this, &LayerOrientedSampleEditor::onAsAngstromToggled);
-    m_asAngstrom->setCheckable(true);
-    m_asAngstrom->setChecked(false);
-    m_toolbar->addAction(m_asAngstrom);
+    m_asAngstromAction = new QAction("A", this); // #baLayerEditor use icon, not text
+    m_asAngstromAction->setToolTip("Use \303\205ngstrom as unit (where applicable)");
+    connect(m_asAngstromAction, &QAction::toggled, this,
+            &LayerOrientedSampleEditor::onUnitActionToggled);
+    m_asAngstromAction->setCheckable(true);
+
+    auto* asNanometerAction = new QAction("nm", this); // #baLayerEditor use icon, not text
+    asNanometerAction->setToolTip("Use nanometer as unit (where applicable)");
+    connect(asNanometerAction, &QAction::toggled, this,
+            &LayerOrientedSampleEditor::onUnitActionToggled);
+    asNanometerAction->setCheckable(true);
+
+    auto* ag = new QActionGroup(this);
+    ag->addAction(asNanometerAction);
+    ag->addAction(m_asAngstromAction);
+    if (baApp->settings().defaultUnitIsAngstrom())
+        m_asAngstromAction->setChecked(true);
+    else
+        asNanometerAction->setChecked(true);
+
+    m_toolbar->addActions(ag->actions());
 }
 
 LayerOrientedSampleEditor::~LayerOrientedSampleEditor()
@@ -108,6 +125,7 @@ void LayerOrientedSampleEditor::setCurrentSample(MultiLayerItem* multiLayerItem)
     m_currentMultiLayerWidget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
     m_scrollArea->setWidget(m_currentMultiLayerWidget);
     m_currentMultiLayerWidget->showInlineEditButtons(m_showInlineEditButtonsAction->isChecked());
+    m_currentMultiLayerWidget->useAngstrom(m_asAngstromAction->isChecked());
 
     updateActionEnabling();
 }
@@ -123,10 +141,10 @@ void LayerOrientedSampleEditor::onShowInlineEditButtonsToggled(bool checked)
         m_currentMultiLayerWidget->showInlineEditButtons(checked);
 }
 
-void LayerOrientedSampleEditor::onAsAngstromToggled()
+void LayerOrientedSampleEditor::onUnitActionToggled()
 {
     if (m_currentMultiLayerWidget)
-        m_currentMultiLayerWidget->useAngstrom(m_asAngstrom->isChecked());
+        m_currentMultiLayerWidget->useAngstrom(m_asAngstromAction->isChecked());
 }
 
 void LayerOrientedSampleEditor::createLayerColors() // #baLayerEditor move to better place
diff --git a/GUI/Views/SampleDesigner/LayerOrientedSampleEditor.h b/GUI/Views/SampleDesigner/LayerOrientedSampleEditor.h
index 96c2d368edc..431faafe8af 100644
--- a/GUI/Views/SampleDesigner/LayerOrientedSampleEditor.h
+++ b/GUI/Views/SampleDesigner/LayerOrientedSampleEditor.h
@@ -36,7 +36,7 @@ public:
 private:
     void updateActionEnabling();
     void onShowInlineEditButtonsToggled(bool checked);
-    void onAsAngstromToggled();
+    void onUnitActionToggled();
 
     void createLayerColors();
     QWidget* createEmptyWidget();
@@ -48,7 +48,7 @@ private:
     QAction* m_undoAction; // dedicated to the current sample
     QAction* m_redoAction;
     QAction* m_showInlineEditButtonsAction;
-    QAction* m_asAngstrom; // #baLayerEditor better two autoexclusive actions?
+    QAction* m_asAngstromAction;
     MultiLayerItem* m_currentMultiLayerItem = nullptr;
     QMap<MultiLayerItem*, SampleEditorController*> m_editControllers;
 };
-- 
GitLab