File: MaterialEditorDialog.cpp

package info (click to toggle)
bornagain 23.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,936 kB
  • sloc: cpp: 423,131; python: 40,997; javascript: 11,167; awk: 630; sh: 318; ruby: 173; xml: 130; makefile: 51; ansic: 24
file content (396 lines) | stat: -rw-r--r-- 13,621 bytes parent folder | download
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/View/Material/MaterialEditorDialog.cpp
//! @brief     Implements class MaterialEditorDialog.
//!
//! @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/Material/MaterialEditorDialog.h"
#include "GUI/Model/Material/MaterialItem.h"
#include "GUI/Model/Material/MaterialsSet.h"
#include "GUI/Model/Sample/ItemWithMaterial.h"
#include "GUI/Model/Sample/SampleItem.h"
#include "GUI/View/Base/Fontsize.h"
#include "GUI/View/Material/MaterialsQModel.h"
#include "GUI/View/Numeric/DSpinBox.h"
#include "GUI/View/Setup/ActionFactory.h"
#include "GUI/View/Widget/StyledToolbar.h"
#include "GUI/View/Widget/WidgetSettings.h"
#include <QAction>
#include <QCheckBox>
#include <QColorDialog>
#include <QDialog>
#include <QDialogButtonBox>
#include <QFormLayout>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QHeaderView>
#include <QLabel>
#include <QLineEdit>
#include <QMessageBox>
#include <QPushButton>
#include <QSettings>
#include <QTreeView>
#include <QVBoxLayout>

//! Dialog to select a material and also to edit the list of existing materials.
//! The dialog operates on a copy of the current materials. The original material store is only
//! updated if changes have been made and the dialog has been closed with OK.
//! Use this dialog only with the two static methods to edit the list, or to choose a material
class MaterialEditorDialog : public QDialog {
public:
    MaterialEditorDialog(SampleItem* sample, const QString& identifier);
    ~MaterialEditorDialog() override;

    MaterialItem* currentMaterialItem();

private:
    void addRefractiveMaterial();
    void addSldMaterial();
    void removeCurrentMaterial();

    void setCurrentMaterial(const MaterialItem* m);

    void selectColor();

    void onSelectColor();
    void onSelectMaterial();

    void onChangeCurrent();
    QModelIndex currentIndex() const;

    //! Returns the list of material identifiers of the materials currently used in the sample.
    //! E.g. the material selected in a particle.
    QStringList identifiersOfUsedMaterials() const;

    QAction* m_remove_material_action;

    MaterialsQModel* m_model;
    SampleItem* m_sample;

    QTreeView* m_tree_view;
    QPushButton* m_select_color_button;
    QWidget* m_editor;
    QGroupBox* m_sld_group;
    QGroupBox* m_refr_group;
    DSpinBox* m_delta_spinbox;
    DSpinBox* m_beta_spinbox;
    DSpinBox* m_re_sld_spinbox;
    DSpinBox* m_im_sld_spinbox;
    QGroupBox* m_magn_group;
    QCheckBox* m_magn_on_off;
    DSpinBox* m_Bx_spinbox;
    DSpinBox* m_By_spinbox;
    DSpinBox* m_Bz_spinbox;
    QLineEdit* m_name_edit;
    QLineEdit* m_color_info;
};


MaterialEditorDialog::MaterialEditorDialog(SampleItem* sample, const QString& identifier)
    : m_sample(sample)
{
    setObjectName("MaterialEditorDialog");

    m_model = new MaterialsQModel(m_sample->materialModel());

    setGeometry(0, 0, 1023, 469);
    setWindowTitle("Material Editor");

    auto* layout = new QVBoxLayout;
    setLayout(layout);

    auto* hlayout = new QHBoxLayout;
    layout->addLayout(hlayout);

    //... Left side: list of materials

    m_tree_view = new QTreeView;
    hlayout->addWidget(m_tree_view);
    m_tree_view->setCurrentIndex(m_model->indexFromMaterial(identifier));
    m_tree_view->setContextMenuPolicy(Qt::ActionsContextMenu);
    m_tree_view->setSelectionMode(QAbstractItemView::SingleSelection);
    m_tree_view->setSelectionBehavior(QAbstractItemView::SelectRows);
    m_tree_view->setRootIsDecorated(false);

    //... Right side: editor for one material

    m_editor = new QWidget;
    hlayout->addWidget(m_editor);
    auto* ed_vlayout = new QVBoxLayout;
    m_editor->setLayout(ed_vlayout);
    ed_vlayout->setSpacing(6);
    ed_vlayout->setContentsMargins(0, 0, 0, 0);

    // Name and color

    auto* nc_form = new QFormLayout;
    ed_vlayout->addLayout(nc_form);
    nc_form->setWidget(0, QFormLayout::LabelRole, new QLabel("Name:"));
    m_name_edit = new QLineEdit;
    nc_form->setWidget(0, QFormLayout::FieldRole, m_name_edit);

    nc_form->setWidget(1, QFormLayout::LabelRole, new QLabel("Color:"));
    auto* colorLine = new QHBoxLayout;
    nc_form->setLayout(1, QFormLayout::FieldRole, colorLine);

    m_select_color_button = new QPushButton;
    colorLine->addWidget(m_select_color_button);
    QSizePolicy sizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
    sizePolicy.setHorizontalStretch(0);
    sizePolicy.setVerticalStretch(0);
    sizePolicy.setHeightForWidth(m_select_color_button->sizePolicy().hasHeightForWidth());
    m_select_color_button->setSizePolicy(sizePolicy);
    m_select_color_button->setToolTip("Choose color");

    m_color_info = new QLineEdit;
    colorLine->addWidget(m_color_info);
    m_color_info->setEnabled(false);

    // Refractive data

    m_refr_group = new QGroupBox("Refractive index");
    ed_vlayout->addWidget(m_refr_group);

    auto* refr_form = new QFormLayout;
    m_refr_group->setLayout(refr_form);
    m_delta_spinbox = new DSpinBox(nullptr);
    m_beta_spinbox = new DSpinBox(nullptr);
    refr_form->addRow("Delta:", m_delta_spinbox);
    refr_form->addRow("Beta:", m_beta_spinbox);

    m_sld_group = new QGroupBox("Scattering length density (1/Ų)");
    ed_vlayout->addWidget(m_sld_group);

    auto* sld_form = new QFormLayout;
    m_sld_group->setLayout(sld_form);
    m_re_sld_spinbox = new DSpinBox(nullptr);
    m_im_sld_spinbox = new DSpinBox(nullptr);
    sld_form->addRow("Real:", m_re_sld_spinbox);
    sld_form->addRow("Imaginary:", m_im_sld_spinbox);

    // Magnetization

    m_magn_on_off = new QCheckBox("Enable magnetization");
    ed_vlayout->addWidget(m_magn_on_off);

    m_magn_group = new QGroupBox("Magnetization (A/m)");
    ed_vlayout->addWidget(m_magn_group);
    auto* magn_form = new QFormLayout(m_magn_group);

    m_Bx_spinbox = new DSpinBox(nullptr);
    m_By_spinbox = new DSpinBox(nullptr);
    m_Bz_spinbox = new DSpinBox(nullptr);

    magn_form->addRow("X:", m_Bx_spinbox);
    magn_form->addRow("Y:", m_By_spinbox);
    magn_form->addRow("Z:", m_Bz_spinbox);

    //... Buttons at bottom

    auto* verticalSpacer = new QSpacerItem(20, 15, QSizePolicy::Minimum, QSizePolicy::Expanding);
    ed_vlayout->addItem(verticalSpacer);

    auto* m_button_box = new QDialogButtonBox;
    layout->addWidget(m_button_box);
    m_button_box->setOrientation(Qt::Horizontal);
    m_button_box->setStandardButtons(QDialogButtonBox::Ok);

    //... Configure

    auto* addRefractiveMaterialAction = new QAction("Add material (refractive index)");
    addRefractiveMaterialAction->setIcon(QIcon(":/images/shape-square-plus.svg"));
    addRefractiveMaterialAction->setToolTip("Add new material");
    connect(addRefractiveMaterialAction, &QAction::triggered, this,
            &MaterialEditorDialog::addRefractiveMaterial);

    auto* addSldMaterialAction = new QAction("Add material (SLD)");
    addSldMaterialAction->setIcon(QIcon(":/images/shape-square-plus.svg"));
    addSldMaterialAction->setToolTip("Add new material");
    connect(addSldMaterialAction, &QAction::triggered, this, &MaterialEditorDialog::addSldMaterial);

    m_remove_material_action = ActionFactory::createRemoveAction("material");
    connect(m_remove_material_action, &QAction::triggered, this,
            &MaterialEditorDialog::removeCurrentMaterial);

    m_tree_view->addAction(addRefractiveMaterialAction);
    m_tree_view->addAction(addSldMaterialAction);
    auto* separator = new QAction;
    separator->setSeparator(true);
    m_tree_view->addAction(separator);
    m_tree_view->addAction(m_remove_material_action);
    m_tree_view->setModel(m_model);
    m_tree_view->header()->setSectionResizeMode(QHeaderView::ResizeToContents);

    auto* toolbar = new StyledToolbar;
    toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
    toolbar->addAction(addRefractiveMaterialAction);
    toolbar->addAction(addSldMaterialAction);
    toolbar->addAction(m_remove_material_action);
    layout->insertWidget(0, toolbar);

    GUI::WidgetSettings::load(this);

    connect(m_tree_view->selectionModel(), &QItemSelectionModel::currentChanged, this,
            &MaterialEditorDialog::onChangeCurrent);

    connect(m_select_color_button, &QPushButton::clicked, this, &MaterialEditorDialog::selectColor);

    connect(m_name_edit, &QLineEdit::textEdited,
            [&](const QString& t) { m_model->setMaterialItemName(currentIndex(), t); });

    connect(m_button_box, &QDialogButtonBox::accepted, this, &QDialog::accept);
    connect(m_button_box, &QDialogButtonBox::rejected, this, &QDialog::reject);

    if (m_model->rowCount() > 0)
        m_tree_view->setCurrentIndex(m_model->first());
    else {
        m_sld_group->hide();
        m_editor->setEnabled(false);
    }

    onSelectMaterial();
}

MaterialEditorDialog::~MaterialEditorDialog()
{
    GUI::WidgetSettings::save(this);
    if (currentMaterialItem())
        emit currentMaterialItem() -> dataChanged();
}

void MaterialEditorDialog::addRefractiveMaterial()
{
    setCurrentMaterial(m_model->addRefractiveMaterialItem("unnamed", 0.0, 0.0));
}

void MaterialEditorDialog::addSldMaterial()
{
    setCurrentMaterial(m_model->addSLDMaterialItem("unnamed", 0.0, 0.0));
}

void MaterialEditorDialog::removeCurrentMaterial()
{
    MaterialItem* material = currentMaterialItem();
    if (!material)
        return;

    if (identifiersOfUsedMaterials().contains(material->identifier())) {
        QMessageBox::warning(this, "Remove material",
                             "This material cannot be removed - it is used in the current sample.");
        return;
    }

    m_model->removeMaterial(currentIndex());
}

void MaterialEditorDialog::selectColor()
{
    if (auto newColor = QColorDialog::getColor(currentMaterialItem()->color()); newColor.isValid())
        m_model->setColor(currentIndex(), newColor);
    onSelectColor();
}

void MaterialEditorDialog::onSelectColor()
{
    auto* materialItem = currentMaterialItem();
    m_name_edit->setText(materialItem->matItemName());
    m_color_info->setText(QString("[%1, %2, %3] (%4)")
                              .arg(materialItem->color().red())
                              .arg(materialItem->color().green())
                              .arg(materialItem->color().blue())
                              .arg(materialItem->color().alpha()));
    QPixmap pixmap(m_select_color_button->iconSize());
    pixmap.fill(materialItem->color());
    m_select_color_button->setIcon(pixmap);
}

void MaterialEditorDialog::onSelectMaterial()
{
    MaterialItem* materialItem = currentMaterialItem();
    if (!materialItem)
        return;

    disconnect(m_magn_on_off, &QCheckBox::stateChanged, nullptr, nullptr);
    m_magn_on_off->setChecked(materialItem->isMagnetizatioEnabled());
    m_magn_group->setEnabled(materialItem->isMagnetizatioEnabled());
    connect(m_magn_on_off, &QCheckBox::stateChanged, [this, materialItem](bool enable) {
        m_magn_group->setEnabled(enable);
        materialItem->setMagnetizationEnabled(enable);
    });

    m_refr_group->setVisible(materialItem->hasRefractiveIndex());
    m_sld_group->setVisible(!materialItem->hasRefractiveIndex());

    m_editor->setEnabled(materialItem != nullptr);
    if (materialItem == nullptr) {
        m_refr_group->show();
        m_sld_group->hide();
        for (auto* lineEdit : m_editor->findChildren<QLineEdit*>())
            lineEdit->clear();
        for (auto* spinBox : m_editor->findChildren<DSpinBox*>())
            spinBox->replaceProperty(nullptr);
        return;
    }

    m_delta_spinbox->replaceProperty(&materialItem->delta());
    m_beta_spinbox->replaceProperty(&materialItem->beta());
    m_re_sld_spinbox->replaceProperty(&materialItem->sldRe());
    m_im_sld_spinbox->replaceProperty(&materialItem->sldIm());

    m_Bx_spinbox->replaceProperty(&materialItem->magnetization().x());
    m_By_spinbox->replaceProperty(&materialItem->magnetization().y());
    m_Bz_spinbox->replaceProperty(&materialItem->magnetization().z());
}

void MaterialEditorDialog::onChangeCurrent()
{
    m_remove_material_action->setEnabled(currentIndex().isValid());

    onSelectMaterial();
    onSelectColor();
}

MaterialItem* MaterialEditorDialog::currentMaterialItem()
{
    return currentIndex().isValid() ? m_model->materialItemFromIndex(currentIndex()) : nullptr;
}

void MaterialEditorDialog::setCurrentMaterial(const MaterialItem* m)
{
    m_tree_view->setCurrentIndex(m_model->indexFromMaterial(m));
    onSelectMaterial();
}

QModelIndex MaterialEditorDialog::currentIndex() const
{
    return m_tree_view->currentIndex();
}

QStringList MaterialEditorDialog::identifiersOfUsedMaterials() const
{
    QStringList result;
    for (auto* p : m_sample->itemsWithMaterial())
        result << p->materialIdentifier();
    return result;
}


//! Static caller

QString GUI::chooseMaterial(SampleItem* sample, const QString& identifierOfPreviousMaterial)
{
    MaterialEditorDialog dialog(sample, identifierOfPreviousMaterial);
    if (dialog.exec() == QDialog::Accepted)
        if (MaterialItem* material = dialog.currentMaterialItem())
            return material->identifier();

    return {};
}