From db2ad2602b53a6712c9e398af4642dc8f6bfa0f6 Mon Sep 17 00:00:00 2001
From: "Joachim Wuttke (h)" <j.wuttke@fz-juelich.de>
Date: Sat, 28 Oct 2023 13:07:55 +0200
Subject: [PATCH] CollapsibleGroupBox works for OffspecDetectorEditor

---
 GUI/View/Instrument/OffspecDetectorEditor.cpp | 17 ++---------------
 GUI/View/Instrument/OffspecDetectorEditor.h   |  4 ++--
 GUI/View/Widget/GroupBoxes.cpp                |  7 +++----
 3 files changed, 7 insertions(+), 21 deletions(-)

diff --git a/GUI/View/Instrument/OffspecDetectorEditor.cpp b/GUI/View/Instrument/OffspecDetectorEditor.cpp
index 18dfa6648a4..d6625057d90 100644
--- a/GUI/View/Instrument/OffspecDetectorEditor.cpp
+++ b/GUI/View/Instrument/OffspecDetectorEditor.cpp
@@ -17,25 +17,12 @@
 #include "GUI/Model/Detector/OffspecDetectorItem.h"
 #include "GUI/Model/Device/InstrumentItems.h"
 #include "GUI/View/Device/AxisPropertyForm.h"
-#include "GUI/View/Widget/GroupBoxes.h"
 #include <QFormLayout>
 
-
 OffspecDetectorEditor::OffspecDetectorEditor(QWidget* parent, OffspecInstrumentItem* instrument)
-    : QGroupBox("Detector parameters", parent)
+    : CollapsibleGroupBox("Detector parameters", parent, instrument->expandDetector)
     , m_instrument(instrument)
 {
-    ASSERT(instrument);
-    m_formLayout = new QFormLayout(this);
-
-    auto* collapser = GroupBoxCollapser::installIntoGroupBox(this);
-    collapser->setExpanded(instrument->expandDetector);
-    connect(collapser, &GroupBoxCollapser::toggled, this,
-            [instrument](bool b) { instrument->expandDetector = b; });
-
-    while (m_formLayout->rowCount() > 1)
-        m_formLayout->removeRow(1);
-
     OffspecDetectorItem* detector_item = m_instrument->detectorItem();
 
     auto* grid = new QGridLayout;
@@ -57,5 +44,5 @@ OffspecDetectorEditor::OffspecDetectorEditor(QWidget* parent, OffspecInstrumentI
 
     connect(alphaForm, &AxisPropertyForm::dataChanged, this, &OffspecDetectorEditor::dataChanged);
 
-    m_formLayout->addRow(grid);
+    body()->setLayout(grid);
 }
diff --git a/GUI/View/Instrument/OffspecDetectorEditor.h b/GUI/View/Instrument/OffspecDetectorEditor.h
index 46eed19d5bc..dec2f40b569 100644
--- a/GUI/View/Instrument/OffspecDetectorEditor.h
+++ b/GUI/View/Instrument/OffspecDetectorEditor.h
@@ -15,12 +15,12 @@
 #ifndef BORNAGAIN_GUI_VIEW_INSTRUMENT_OFFSPECDETECTOREDITOR_H
 #define BORNAGAIN_GUI_VIEW_INSTRUMENT_OFFSPECDETECTOREDITOR_H
 
-#include <QGroupBox>
+#include "GUI/View/Widget/GroupBoxes.h"
 
 class OffspecInstrumentItem;
 class QFormLayout;
 
-class OffspecDetectorEditor : public QGroupBox {
+class OffspecDetectorEditor : public CollapsibleGroupBox {
     Q_OBJECT
 public:
     OffspecDetectorEditor(QWidget* parent, OffspecInstrumentItem* item);
diff --git a/GUI/View/Widget/GroupBoxes.cpp b/GUI/View/Widget/GroupBoxes.cpp
index 0d75bbef542..f22d4c04e18 100644
--- a/GUI/View/Widget/GroupBoxes.cpp
+++ b/GUI/View/Widget/GroupBoxes.cpp
@@ -85,11 +85,10 @@ CollapsibleGroupBox::CollapsibleGroupBox(const QString& title, QWidget* parent,
 
     connect(
         m_toggleButton, &QAbstractButton::clicked,
-        [this, &expanded](bool checked) {
-            this->m_toggleButton->setArrowType(checked ? Qt::ArrowType::DownArrow
+        [tb=m_toggleButton, bo=m_body, &expanded](bool checked) {
+            tb->setArrowType(checked ? Qt::ArrowType::DownArrow
                                                  : Qt::ArrowType::RightArrow);
-
-            this->m_body->setVisible(checked);
+            bo->setVisible(checked);
 	    expanded = checked;
         });
 }
-- 
GitLab