Newer
Older
// ************************************************************************************************
//
// BornAgain: simulate and fit reflection and scattering
//
//! @file GUI/View/SampleDesigner/FormLayouter.cpp
//! @brief Implements class FormLayouter
//!
//! @homepage http://www.bornagainproject.org
//! @license GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2021
//! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
// ************************************************************************************************
#include "GUI/View/SampleDesigner/FormLayouter.h"
#include "GUI/Model/Types/UIntDescriptor.h"
#include "GUI/Model/Types/VectorDescriptor.h"
#include "GUI/View/Edit/DoubleSpinBox.h"
#include "GUI/View/SampleDesigner/LayerEditorUtils.h"
#include "GUI/View/SampleDesigner/SampleEditorController.h"
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
#include <QFormLayout>
#include <QGroupBox>
#include <QLabel>
#include <QPushButton>
namespace {
QWidget* createSpinBox(QWidget* parentWidget, const UIntDescriptor& d, SampleEditorController* ec)
{
QSpinBox* spinBox = new QSpinBox(parentWidget);
spinBox->setFocusPolicy(Qt::StrongFocus);
spinBox->setKeyboardTracking(false);
spinBox->setToolTip(d.tooltip);
spinBox->setMaximum(std::numeric_limits<int>::max());
spinBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
if (d.limits.hasLowerLimit())
spinBox->setMinimum(static_cast<int>(d.limits.lowerLimit()));
if (d.limits.hasUpperLimit())
spinBox->setMaximum(static_cast<int>(d.limits.upperLimit()));
spinBox->setValue(d.get());
QObject::connect(spinBox, QOverload<int>::of(&QSpinBox::valueChanged),
[=](int newValue) { ec->setInt(newValue, d); });
return spinBox;
}
} // namespace
FormLayouter::FormLayouter(QWidget* parent, SampleEditorController* ec) : m_ec(ec)
{
if (parent->layout() != nullptr) {
m_formLayout = dynamic_cast<QFormLayout*>(parent->layout());
if (m_formLayout == nullptr) {
auto* collapser =
GroupBoxCollapser::findInstalledCollapser(dynamic_cast<QGroupBox*>(parent));
if (collapser)
m_formLayout = dynamic_cast<QFormLayout*>(collapser->contentArea()->layout());
}
ASSERT(m_formLayout);
} else {
m_formLayout = new QFormLayout(parent);
m_formLayout->setFormAlignment(Qt::AlignLeft | Qt::AlignBottom);
m_formLayout->setFieldGrowthPolicy(QFormLayout::FieldsStayAtSizeHint);
}
}
QFormLayout* FormLayouter::layout()
{
return m_formLayout;
}
void FormLayouter::setContentsMargins(int left, int top, int right, int bottom)
{
m_formLayout->setContentsMargins(left, top, right, bottom);
}
int FormLayouter::addRow(const QString& label, QWidget* w)
{
insertRow(m_formLayout->rowCount(), label, w);
return m_formLayout->rowCount() - 1;
}
void FormLayouter::insertRow(int row, QString label, QWidget* w)
{
if (!label.endsWith(":"))
label += ":";
m_formLayout->insertRow(row, LayerEditorUtils::createBoldLabel(label), w);
}
int FormLayouter::addGroupOfValues(const QString& labelText, const DoubleDescriptors& values)
{
QWidget* w = new QWidget(m_formLayout->parentWidget());
w->setObjectName("PropertyBaseWidget");
w->setAttribute(Qt::WA_StyledBackground, true);
w->setStyleSheet("#PropertyBaseWidget {background-color: transparent}");
auto gridLayout = new QGridLayout(w);
gridLayout->setContentsMargins(0, 0, 0, 0);
gridLayout->setSpacing(6);
LayerEditorUtils::addMultiPropertyToGrid(gridLayout, 0, values, m_ec, true);
return addRow(labelText, w);
}
int FormLayouter::addVector(const VectorDescriptor& d, bool vertically /*= true*/)
{
QWidget* w = new QWidget(m_formLayout->parentWidget());
w->setObjectName("PropertyBaseWidget");
w->setAttribute(Qt::WA_StyledBackground, true);
w->setStyleSheet("#PropertyBaseWidget {background-color: transparent}");
auto gridLayout = new QGridLayout(w);
gridLayout->setContentsMargins(0, 0, 0, 0);
gridLayout->setSpacing(6);
LayerEditorUtils::addMultiPropertyToGrid(gridLayout, 0, {d.x, d.y, d.z}, m_ec, vertically,
true);
return addRow(d.label, w);
}
void FormLayouter::setRowVisible(int row, bool visible)
{
m_formLayout->itemAt(row, QFormLayout::LabelRole)->widget()->setVisible(visible);
m_formLayout->itemAt(row, QFormLayout::FieldRole)->widget()->setVisible(visible);
}
void FormLayouter::removeRow(int row)
{
m_formLayout->removeRow(row);
}
void FormLayouter::addStructureEditingRow(QPushButton* button)
{
QWidget* w = new QWidget(m_formLayout->parentWidget());
auto l = new QHBoxLayout(w);
l->setContentsMargins(0, 0, 0, 0);
l->setAlignment(Qt::AlignLeft);
l->setSizeConstraint(QLayout::SetMinimumSize);
l->addWidget(button);
l->addStretch();
addRow(w);
}
int FormLayouter::addValue(const UIntDescriptor& d)
{
auto* editor = createSpinBox(m_formLayout->parentWidget(), d, m_ec);
return addRow(d.label, editor);
}
int FormLayouter::addValue(const DoubleDescriptor& d)
{
insertValue(m_formLayout->rowCount(), d);
return m_formLayout->rowCount() - 1;
}
void FormLayouter::insertValue(int row, const DoubleDescriptor& d)
{
auto labelText = d.label;
if (!labelText.endsWith(":"))
labelText += ":";
auto* label = LayerEditorUtils::createBoldLabel(labelText);
label->setAlignment(Qt::AlignLeft | Qt::AlignBottom);
label->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::MinimumExpanding);
auto* editor = new DoubleSpinBox(m_formLayout->parentWidget(), d);
auto* ec = m_ec; // to allow copy-capture in the following lambda
QObject::connect(editor, &DoubleSpinBox::baseValueChanged,
[ec, d](double newValue) { ec->setDouble(newValue, d); });
label->setBuddy(editor);
LayerEditorUtils::updateLabelUnit(label, editor);
m_formLayout->insertRow(row, label, editor);
}
int FormLayouter::addRow(QWidget* w)
{
m_formLayout->addRow(w);
return m_formLayout->rowCount() - 1;
}
void FormLayouter::insertRow(int row, QWidget* w)
{
m_formLayout->insertRow(row, w);
}