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
|
// ************************************************************************************************
//
// BornAgain: simulate and fit reflection and scattering
//
//! @file GUI/View/Sample/LayerEditorUtil.cpp
//! @brief Implements class GUI::Util::Layer.
//!
//! @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/LayerEditorUtil.h"
#include "Base/Util/Assert.h"
#include "GUI/Model/Descriptor/VectorProperty.h"
#include "GUI/Model/Project/ProjectDocument.h"
#include "GUI/Model/Sample/CompoundItem.h"
#include "GUI/Model/Sample/CoreAndShellItem.h"
#include "GUI/Model/Sample/MesocrystalItem.h"
#include "GUI/Model/Sample/ParticleItem.h"
#include "GUI/View/Numeric/DSpinBox.h"
#include "GUI/View/Sample/CompoundForm.h"
#include "GUI/View/Sample/CoreAndShellForm.h"
#include "GUI/View/Sample/MesocrystalForm.h"
#include "GUI/View/Sample/ParticleForm.h"
#include "GUI/View/Sample/SampleEditorController.h"
#include <QLabel>
#include <QMenu>
QVector<QWidget*> GUI::Util::Layer::addMultiPropertyToGrid(QGridLayout* m_grid_layout, int firstCol,
const DoubleProperties& valueProperties,
bool vertically, bool addSpacer,
std::function<void()> setNewValue)
{
QVector<QWidget*> result;
int col = firstCol;
for (auto* d : valueProperties) {
auto* editor = new DSpinBox(&*d);
result.push_back(editor);
QObject::connect(editor, &DSpinBox::valueChanged, [setNewValue] {
if (setNewValue)
setNewValue();
emit gDoc->sampleChanged();
});
QString labeltext = d->label();
if (!vertically && !labeltext.endsWith(":"))
labeltext += ":";
auto* label = new QLabel(labeltext, m_grid_layout->parentWidget());
result.push_back(label);
label->setBuddy(editor); // necessary for unit-updating
if (vertically) {
m_grid_layout->addWidget(label, 0, col);
m_grid_layout->addWidget(editor, 1, col);
col++;
} else {
m_grid_layout->addWidget(label, 1, col++);
m_grid_layout->addWidget(editor, 1, col++);
}
}
if (addSpacer)
m_grid_layout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding), 0, col);
return result;
}
QVector<QWidget*> GUI::Util::Layer::addMultiPropertyToGrid(QGridLayout* m_grid_layout, int firstCol,
const DoubleProperties& valueProperties,
bool addSpacer)
{
return addMultiPropertyToGrid(m_grid_layout, firstCol, valueProperties,
valueProperties.size() > 1, addSpacer);
}
QWidget* GUI::Util::Layer::createWidgetForItemWithParticles(QWidget* parentWidget,
ItemWithParticles* itemWithParticles,
bool allowAbundance,
SampleEditorController* ec,
bool allowRemove /*= true*/)
{
if (auto* composition = dynamic_cast<CompoundItem*>(itemWithParticles))
return new CompoundForm(parentWidget, composition, ec, allowRemove);
if (auto* coreShell = dynamic_cast<CoreAndShellItem*>(itemWithParticles))
return new CoreAndShellForm(parentWidget, coreShell, ec, allowRemove);
if (auto* meso = dynamic_cast<MesocrystalItem*>(itemWithParticles))
return new MesocrystalForm(parentWidget, meso, ec, allowRemove);
if (auto* particle = dynamic_cast<ParticleItem*>(itemWithParticles))
return new ParticleForm(parentWidget, particle, allowAbundance, ec, allowRemove);
ASSERT_NEVER;
return nullptr;
}
QPushButton* GUI::Util::Layer::createAddParticleButton(
QWidget* parentWidget, std::function<void(FormfactorCatalog::Type t)> slotAddFormfactor,
std::function<void(ParticleCatalog::Type t)> slotAddParticle)
{
auto* btn = new QPushButton("Add particle", parentWidget);
auto* menu = new QMenu(btn);
QMenu* menuForEntries = menu;
const auto group = [&](const QString& title) { menuForEntries = menu->addMenu(title); };
group("Hard particles");
for (const auto type : FormfactorCatalog::hardParticleTypes()) {
const auto ui = FormfactorCatalog::uiInfo(type);
QAction* a = menuForEntries->addAction(QIcon(ui.iconPath), ui.menuEntry);
a->setToolTip(ui.description);
QObject::connect(a, &QAction::triggered, [=] { slotAddFormfactor(type); });
}
group("Ripples");
for (const auto type : FormfactorCatalog::rippleTypes()) {
const auto ui = FormfactorCatalog::uiInfo(type);
QAction* a = menuForEntries->addAction(QIcon(ui.iconPath), ui.menuEntry);
a->setToolTip(ui.description);
QObject::connect(a, &QAction::triggered, [=] { slotAddFormfactor(type); });
}
group("Particle assemblies");
for (const auto type : {ParticleCatalog::Type::Composition, ParticleCatalog::Type::CoreShell,
ParticleCatalog::Type::Mesocrystal}) {
const auto ui = ParticleCatalog::uiInfo(type);
QAction* a = menuForEntries->addAction(QIcon(ui.iconPath), ui.menuEntry);
a->setToolTip(ui.description);
QObject::connect(a, &QAction::triggered, [=] { slotAddParticle(type); });
}
btn->setMenu(menu);
return btn;
}
QVector<QColor> GUI::Util::Layer::predefinedLayerColors()
{
static QVector<QColor> colors = {QColor(230, 255, 213), QColor(194, 252, 240),
QColor(239, 228, 176), QColor(200, 191, 231),
QColor(253, 205, 193), QColor(224, 193, 253)};
return colors;
}
|