File: SampleEditorController.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 (477 lines) | stat: -rw-r--r-- 17,797 bytes parent folder | download | duplicates (2)
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
//  ************************************************************************************************
//
//  BornAgain: simulate and fit reflection and scattering
//
//! @file      GUI/View/Sample/SampleEditorController.cpp
//! @brief     Implements class SampleEditorController.
//!
//! @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/Sample/SampleEditorController.h"
#include "Base/Util/Assert.h"
#include "Base/Util/Vec.h"
#include "GUI/Model/Material/MaterialsSet.h"
#include "GUI/Model/Project/ProjectDocument.h"
#include "GUI/Model/Sample/CompoundItem.h"
#include "GUI/Model/Sample/CoreAndShellItem.h"
#include "GUI/Model/Sample/InterferenceItems.h"
#include "GUI/Model/Sample/LayerItem.h"
#include "GUI/Model/Sample/LayerStackItem.h"
#include "GUI/Model/Sample/MesocrystalItem.h"
#include "GUI/Model/Sample/ParticleItem.h"
#include "GUI/Model/Sample/ParticleLayoutItem.h"
#include "GUI/Model/Sample/SampleItem.h"
#include "GUI/Model/Util/Backup.h"
#include "GUI/View/Sample/CompoundForm.h"
#include "GUI/View/Sample/CoreAndShellForm.h"
#include "GUI/View/Sample/InterferenceForm.h"
#include "GUI/View/Sample/LatticeTypeSelectionForm.h"
#include "GUI/View/Sample/LayerForm.h"
#include "GUI/View/Sample/LayerStackForm.h"
#include "GUI/View/Sample/MaterialInplaceForm.h"
#include "GUI/View/Sample/MesocrystalForm.h"
#include "GUI/View/Sample/ParticleLayoutForm.h"
#include "GUI/View/Sample/SampleForm.h"
#include <QLabel>

SampleEditorController::SampleEditorController(SampleItem* multi)
    : m_sample_item(multi)
    , m_sample_form(nullptr)
{
}

void SampleEditorController::setSampleForm(SampleForm* view)
{
    m_sample_form = view;
}

void SampleEditorController::addLayerItem(LayerStackForm& parentStackForm,
                                          const ItemWithLayers* before)
{
    LayerStackItem& parentStack = parentStackForm.stackItem();
    const int at_index = parentStack.indexOfComponent(before) + 1;
    QColor color = findColor(at_index); // before adding layer
    LayerItem* new_layer = m_sample_item->createLayerItemAt(parentStack, at_index);
    new_layer->setMaterial(materialModel()->defaultMaterialItem());
    new_layer->setColor(color);
    onComponentAdded(new_layer);
}

void SampleEditorController::addLayerStackItem(LayerStackForm& parentStackForm,
                                               const ItemWithLayers* before)
{
    LayerStackItem& parentStack = parentStackForm.stackItem();
    const int at_index = parentStack.indexOfComponent(before) + 1;
    LayerStackItem* new_stack = m_sample_item->createLayerStackItemAt(parentStack, at_index);
    onComponentAdded(new_stack);
}

void SampleEditorController::duplicateItemWithLayers(const ItemWithLayers* component)
{
    LayerStackItem* parentStack = m_sample_item->parentOfComponent(component);

    // case of the outer stack
    if (!parentStack)
        return;

    LayerStackForm* parentStackForm = m_sample_form->formOfStackItem(parentStack);
    ASSERT(parentStackForm);

    const int at_index = parentStack->indexOfComponent(component) + 1;

    if (const auto* layer = dynamic_cast<const LayerItem*>(component)) {
        QColor color = findColor(at_index); // before adding layer
        LayerItem* new_layer = m_sample_item->createLayerItemAt(*parentStack, at_index);
        new_layer->setMaterial(materialModel()->defaultMaterialItem());
        GUI::Util::copyContents(layer, new_layer);
        new_layer->setColor(color);
        onComponentAdded(new_layer);

    } else if (const auto* stack = dynamic_cast<const LayerStackItem*>(component)) {
        LayerStackItem* new_stack = m_sample_item->createLayerStackItemAt(*parentStack, at_index);
        GUI::Util::copyContents(stack, new_stack);
        onComponentAdded(new_stack);

    } else
        ASSERT_NEVER;
}

void SampleEditorController::onComponentAdded(ItemWithLayers* component)
{
    LayerStackItem* parentStack = m_sample_item->parentOfComponent(component);
    ASSERT(parentStack);

    LayerStackForm* parentStackForm = m_sample_form->formOfStackItem(parentStack);
    ASSERT(parentStackForm);

    parentStackForm->addComponentForm(component);
    m_sample_form->updateRowVisibilities();
    emit gDoc->sampleChanged();
}

QColor SampleEditorController::findColor(size_t atIndex)
{
    std::vector<LayerItem*> uniqueLayerItems = m_sample_item->uniqueLayerItems();

    QColor result;
    auto unusedColors = GUI::Util::Layer::predefinedLayerColors();
    for (auto* l : uniqueLayerItems)
        unusedColors.removeAll(l->color());
    if (!unusedColors.isEmpty())
        result = unusedColors.first();
    else {
        // search for a color which has been used the less, and which is not the same as in the
        // layers above and below
        QMap<QString, int> usage;
        for (auto* l : uniqueLayerItems)
            usage[l->color().name()] += 1;

        auto sortedByUsage = GUI::Util::Layer::predefinedLayerColors();
        std::stable_sort(
            sortedByUsage.begin(), sortedByUsage.end(),
            [&](const QColor& a, const QColor& b) { return usage[a.name()] < usage[b.name()]; });

        const QColor above = (atIndex > 0) ? uniqueLayerItems[atIndex - 1]->color() : QColor();
        const QColor below =
            (atIndex < uniqueLayerItems.size()) ? uniqueLayerItems[atIndex]->color() : QColor();

        for (const auto& col : sortedByUsage)
            if (col != above && col != below) {
                result = col;
                break;
            }
    }
    return result;
}

void SampleEditorController::removeItemWithLayers(ItemWithLayers* component)
{
    LayerStackItem* parentStack = m_sample_item->parentOfComponent(component);

    // case of the outer stack
    if (!parentStack)
        return;

    LayerStackForm* parentStackForm = m_sample_form->formOfStackItem(parentStack);
    ASSERT(parentStackForm);

    emit aboutToRemoveItem(component);
    parentStackForm->removeComponentForm(component);
    m_sample_item->removeComponent(component);
    m_sample_form->updateRowVisibilities();
    emit gDoc->sampleChanged();
}

void SampleEditorController::onLayoutAdded(LayerForm* layerForm, ParticleLayoutItem* layout)
{
    layerForm->onLayoutAdded(layout);

    for (auto* layoutForms : layerForm->findChildren<ParticleLayoutForm*>())
        layoutForms->updateTitle(layerForm->layerItem());

    emit gDoc->sampleChanged();
}

void SampleEditorController::addLayoutItem(LayerForm* layerForm)
{
    auto* newLayout = layerForm->layerItem()->addLayoutItem();
    onLayoutAdded(layerForm, newLayout);
}

void SampleEditorController::duplicateLayoutItem(LayerForm* layerForm, ParticleLayoutItem* layout)
{
    auto* newLayout = layerForm->layerItem()->addLayoutItem();
    GUI::Util::copyContents(layout, newLayout);
    onLayoutAdded(layerForm, newLayout);
}

void SampleEditorController::removeLayoutItem(LayerForm* layerForm, ParticleLayoutItem* layout)
{
    emit aboutToRemoveItem(layout);
    layerForm->onAboutToRemoveLayout(layout);
    layerForm->layerItem()->removeLayoutItem(layout);

    for (auto* layoutForm : layerForm->findChildren<ParticleLayoutForm*>())
        layoutForm->updateTitle(layerForm->layerItem());

    emit gDoc->sampleChanged();
}

void SampleEditorController::onParticleLayoutAdded(ParticleLayoutItem* layout,
                                                   ItemWithParticles* newItem)
{
    emit gDoc->sampleChanged();

    //  search for particle layout widget for notification
    ASSERT(m_sample_form);
    for (auto* w : m_sample_form->findChildren<ParticleLayoutForm*>())
        if (w->layoutItem() == layout)
            w->onParticleAdded(newItem);
}

void SampleEditorController::addParticleLayoutItem(ParticleLayoutItem* layout,
                                                   FormfactorCatalog::Type formFactorType)
{
    auto* newParticle = createAndInitItem(formFactorType);
    layout->addItemWithParticleSelection(newParticle);
    onParticleLayoutAdded(layout, newParticle);
}

void SampleEditorController::addParticleLayoutItem(ParticleLayoutItem* layout,
                                                   ParticleCatalog::Type type)
{
    auto* newItem = createAndInitItem(type);
    layout->addItemWithParticleSelection(newItem);
    onParticleLayoutAdded(layout, newItem);
}

void SampleEditorController::duplicateItemWithParticles(ItemWithParticles* item)
{
    auto type = ParticleCatalog::type(item);
    auto* newItem = createAndInitItem(type);
    GUI::Util::copyContents(item, newItem);

    if (ParticleLayoutItem* parent_layout = parentLayoutItem(item)) {
        parent_layout->addItemWithParticleSelection(newItem);
        onParticleLayoutAdded(parent_layout, newItem);
    } else if (CompoundItem* parent_compound = parentCompoundItem(item)) {
        parent_compound->addItemWithParticleSelection(newItem);
        onParticleCompoundAdded(parent_compound, newItem);
    } else
        ASSERT_NEVER;
}

void SampleEditorController::onParticleCompoundAdded(CompoundItem* composition,
                                                     ItemWithParticles* newItem)
{
    emit gDoc->sampleChanged();

    //  search for composition widget for notification
    ASSERT(m_sample_form);
    for (auto* c : m_sample_form->findChildren<CompoundForm*>())
        if (c->compositionItem() == composition)
            c->onParticleAdded(newItem);
}

void SampleEditorController::addCompoundItem(CompoundItem* compositionItem,
                                             ParticleCatalog::Type type)
{
    auto* newItem = createAndInitItem(type);
    compositionItem->addItemWithParticleSelection(newItem);
    onParticleCompoundAdded(compositionItem, newItem);
}

void SampleEditorController::addCompoundItem(CompoundItem* compositionItem,
                                             FormfactorCatalog::Type formFactorType)
{
    auto* newParticle = createAndInitItem(formFactorType);
    compositionItem->addItemWithParticleSelection(newParticle);
    onParticleCompoundAdded(compositionItem, newParticle);
}

ItemWithParticles*
SampleEditorController::createAndInitItem(FormfactorCatalog::Type formFactorType) const
{
    auto* newParticle = new ParticleItem(materialModel());
    newParticle->setFormfactor(FormfactorCatalog::create(formFactorType));
    newParticle->setMaterial(materialModel()->defaultParticleMaterialItem());
    return newParticle;
}

ItemWithParticles* SampleEditorController::createAndInitItem(ParticleCatalog::Type type) const
{
    auto* newItem = ParticleCatalog::create(type, materialModel());

    if (auto* p = dynamic_cast<ItemWithMaterial*>(newItem))
        p->setMaterial(materialModel()->defaultMaterialItem());

    if (auto* cs = dynamic_cast<CoreAndShellItem*>(newItem)) {
        cs->createCoreItem(materialModel());
        cs->createShellItem(materialModel());
        cs->coreItem()->setFormfactor(new CylinderItem());
        cs->shellItem()->setFormfactor(new CylinderItem());
    }

    if (auto* meso = dynamic_cast<MesocrystalItem*>(newItem); meso && meso->basisItem())
        if (auto* p = dynamic_cast<ItemWithMaterial*>(meso->basisItem()))
            p->setMaterial(materialModel()->defaultMaterialItem());

    return newItem;
}

void SampleEditorController::setCoreFormfactor(CoreAndShellForm* widget,
                                               FormfactorCatalog::Type type)
{
    auto* particleCoreShell = widget->coreShellItem();

    if (particleCoreShell->coreItem() == nullptr)
        particleCoreShell->createCoreItem(materialModel());

    particleCoreShell->coreItem()->setFormfactor(FormfactorCatalog::create(type));
    widget->createCoreWidgets();
    emit gDoc->sampleChanged();
}

void SampleEditorController::setShellFormfactor(CoreAndShellForm* widget,
                                                FormfactorCatalog::Type type)
{
    auto* particleCoreShell = widget->coreShellItem();

    if (particleCoreShell->shellItem() == nullptr)
        particleCoreShell->createShellItem(materialModel());

    particleCoreShell->shellItem()->setFormfactor(FormfactorCatalog::create(type));
    widget->createShellWidgets();
    emit gDoc->sampleChanged();
}

ParticleLayoutItem* SampleEditorController::parentLayoutItem(ItemWithParticles* item)
{
    for (auto* layoutForm : m_sample_form->findChildren<ParticleLayoutForm*>())
        if (Vec::containsPtr(item, layoutForm->layoutItem()->itemsWithParticles()))
            return layoutForm->layoutItem();
    return nullptr;
}

CompoundItem* SampleEditorController::parentCompoundItem(ItemWithParticles* item)
{
    for (auto* compoundForm : m_sample_form->findChildren<CompoundForm*>())
        if (Vec::containsPtr(item, compoundForm->compositionItem()->itemsWithParticles()))
            return compoundForm->compositionItem();
    return nullptr;
}

void SampleEditorController::removeParticle(ItemWithParticles* itemToRemove)
{
    ASSERT(m_sample_form);

    for (auto* layoutForm : m_sample_form->findChildren<ParticleLayoutForm*>())
        if (Vec::containsPtr(itemToRemove, layoutForm->layoutItem()->itemsWithParticles())) {
            layoutForm->onAboutToRemoveParticle(itemToRemove);

            emit aboutToRemoveItem(itemToRemove);
            layoutForm->layoutItem()->removeItemWithParticle(itemToRemove);
            emit gDoc->sampleChanged();
            return;
        }

    for (auto* c : m_sample_form->findChildren<CompoundForm*>())
        if (Vec::containsPtr(itemToRemove, c->compositionItem()->itemsWithParticles())) {
            c->onAboutToRemoveParticle(itemToRemove);

            emit aboutToRemoveItem(itemToRemove);
            c->compositionItem()->removeItemWithParticle(itemToRemove);
            emit gDoc->sampleChanged();
            return;
        }
}

MaterialsSet* SampleEditorController::materialModel() const
{
    return &m_sample_item->materialModel();
}

void SampleEditorController::selectMaterial(ItemWithMaterial* item,
                                            const QString& newMaterialIdentifier)
{
    item->setMaterial(newMaterialIdentifier);

    //  update Layer title
    ASSERT(m_sample_form);
    for (auto* c : m_sample_form->findChildren<LayerForm*>())
        if (c->layerItem() == item)
            c->updateTitle();

    // #baLayerEditor notify all material users (update link info)
    emit gDoc->sampleChanged();
}

void SampleEditorController::setMaterialValue(ItemWithMaterial* item)
{
    // -- notify all other users of this material (update values in the UI)
    ASSERT(m_sample_form);
    for (auto* c : m_sample_form->findChildren<MaterialInplaceForm*>())
        if (c->itemWithMaterial() != item
            && c->itemWithMaterial()->materialIdentifier() == item->materialIdentifier())
            c->updateValues();
    emit gDoc->sampleChanged();
}


//! notify the containing particle layout UI about changed value
void SampleEditorController::setDensityRelatedValue(InterferenceItem* interferenceItem)
{
    ASSERT(m_sample_form);
    for (auto* c : m_sample_form->findChildren<ParticleLayoutForm*>())
        if (c->layoutItem()->interferenceSelection().certainItem() == interferenceItem) {
            c->updateDensityValue();
            break;
        }
}

void SampleEditorController::onStoppedToMoveComponent(QWidget* widgetToMove,
                                                      QWidget* moveAboveThisWidget)
{
    auto* formToMove = dynamic_cast<LayerContainerForm*>(widgetToMove);
    ASSERT(formToMove);
    auto* itemToMove = formToMove->item();

    LayerStackItem* parentStack = m_sample_item->parentOfComponent(itemToMove);
    ASSERT(parentStack);

    LayerStackForm* parentStackForm = m_sample_form->formOfStackItem(parentStack);
    ASSERT(parentStackForm);

    const LayerContainerForm* moveAboveThisLayerForm =
        parentStackForm->findNextComponentForm(moveAboveThisWidget);

    auto* moveAboveThisItem = moveAboveThisLayerForm ? moveAboveThisLayerForm->item() : nullptr;

    m_sample_item->moveComponent(itemToMove, moveAboveThisItem);
    parentStackForm->onComponentMoved(itemToMove);
    m_sample_form->updateRowVisibilities();

    emit gDoc->sampleChanged();
}

void SampleEditorController::setMesocrystalBasis(MesocrystalForm* widget,
                                                 ParticleCatalog::Type type)
{
    auto* meso = widget->mesocrystalItem();
    meso->setBasisItem(createAndInitItem(type));
    widget->createBasisWidgets();
    emit gDoc->sampleChanged();
}

void SampleEditorController::setMesocrystalBasis(MesocrystalForm* widget,
                                                 FormfactorCatalog::Type type)
{
    auto* meso = widget->mesocrystalItem();
    meso->setBasisItem(createAndInitItem(type));
    widget->createBasisWidgets();
    emit gDoc->sampleChanged();
}

void SampleEditorController::selectInterference(InterferenceForm* widget, int newIndex)
{
    widget->layoutItem()->interferenceSelection().setCertainIndex(newIndex);
    widget->onInterferenceTypeChanged();

    // Disable/enable total density property in the particle layout, depending on type of
    // interference function.
    QWidget* parent = widget->parentWidget();
    while (parent != nullptr && dynamic_cast<ParticleLayoutForm*>(parent) == nullptr)
        parent = parent->parentWidget();

    if (auto* particleLayoutForm = dynamic_cast<ParticleLayoutForm*>(parent)) {
        particleLayoutForm->updateDensityEnabling();
        particleLayoutForm->updateDensityValue();
    }

    emit gDoc->sampleChanged();
}