|
| 1 | +#include "ArrowMassView.hpp" |
| 2 | +#include "solver/BowModel.hpp" |
| 3 | +#include "pre/widgets/DoubleSpinBox.hpp" |
| 4 | +#include "pre/models/units/UnitSystem.hpp" |
| 5 | +#include <QAbstractItemModel> |
| 6 | +#include <QVBoxLayout> |
| 7 | +#include <QStackedLayout> |
| 8 | +#include <QLabel> |
| 9 | +#include <QComboBox> |
| 10 | +#include <cmath> |
| 11 | + |
| 12 | +ArrowMassView::ArrowMassView(QAbstractItemModel* model, QPersistentModelIndex index) { |
| 13 | + /* |
| 14 | + setToolTip(tooltip); |
| 15 | +
|
| 16 | + // Set value from model and keep model up to date on changes |
| 17 | + setValue(model->data(index, Qt::DisplayRole).toDouble()); |
| 18 | + QObject::connect(this, &QDoubleSpinBox::valueChanged, this, [=](double value){ model->setData(index, value); }); |
| 19 | + */ |
| 20 | + |
| 21 | + auto selectionBox = new QComboBox(); |
| 22 | + selectionBox->setToolTip("Select how the arrow mass is to be defined"); |
| 23 | + selectionBox->addItems({"Mass", "Mass per Force", "Mass per Energy"}); |
| 24 | + |
| 25 | + auto massEdit = new DoubleSpinBox(Quantities::mass, DoubleRange::nonNegative(1e-3)); |
| 26 | + massEdit->setToolTip("Arrow mass (absolute value)"); |
| 27 | + massEdit->setValue(1e-3); |
| 28 | + |
| 29 | + auto massPerForceEdit = new DoubleSpinBox(Quantities::mass_per_force, DoubleRange::nonNegative(1e-6)); |
| 30 | + massPerForceEdit->setToolTip("Arrow mass relative to the final draw force of the bow"); |
| 31 | + massPerForceEdit->setValue(1e-6); |
| 32 | + |
| 33 | + auto massPerEnergyEdit = new DoubleSpinBox(Quantities::mass_per_energy, DoubleRange::nonNegative(1e-4)); |
| 34 | + massPerEnergyEdit->setToolTip("Arrow mass relative to the input energy of the bow"); |
| 35 | + massPerEnergyEdit->setValue(1e-4); |
| 36 | + |
| 37 | + auto stack = new QStackedLayout(); |
| 38 | + stack->addWidget(massEdit); |
| 39 | + stack->addWidget(massPerForceEdit); |
| 40 | + stack->addWidget(massPerEnergyEdit); |
| 41 | + |
| 42 | + auto vbox = new QVBoxLayout(); |
| 43 | + vbox->setContentsMargins(0, 0, 0, 0); |
| 44 | + vbox->addWidget(selectionBox); |
| 45 | + vbox->addLayout(stack); |
| 46 | + setLayout(vbox); |
| 47 | + |
| 48 | + // Update view according to the data in the model |
| 49 | + auto updateView = [=]() { |
| 50 | + // Set selection according to the type of mass definition |
| 51 | + ArrowMass mass = model->data(index, Qt::DisplayRole).value<ArrowMass>(); |
| 52 | + selectionBox->setCurrentIndex(mass.index()); |
| 53 | + |
| 54 | + // Assign value to the correct editor according to the type of mass definition |
| 55 | + if(auto value = std::get_if<Mass>(&mass)) { |
| 56 | + massEdit->setValue(value->value); |
| 57 | + } |
| 58 | + else if(auto value = std::get_if<MassPerForce>(&mass)) { |
| 59 | + massPerForceEdit->setValue(value->value); |
| 60 | + } |
| 61 | + else if(auto value = std::get_if<MassPerEnergy>(&mass)) { |
| 62 | + massPerEnergyEdit->setValue(value->value); |
| 63 | + } |
| 64 | + else { |
| 65 | + throw std::invalid_argument("Unknown variant"); |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + // Update model according to data in the view |
| 70 | + auto updateModel = [=]() { |
| 71 | + switch(selectionBox->currentIndex()) { |
| 72 | + case 0: |
| 73 | + model->setData(index, QVariant::fromValue<ArrowMass>(Mass{ .value = massEdit->value() })); |
| 74 | + break; |
| 75 | + |
| 76 | + case 1: |
| 77 | + model->setData(index, QVariant::fromValue<ArrowMass>(MassPerForce{ .value = massPerForceEdit->value() })); |
| 78 | + break; |
| 79 | + |
| 80 | + case 2: |
| 81 | + model->setData(index, QVariant::fromValue<ArrowMass>(MassPerEnergy{ .value = massPerEnergyEdit->value() })); |
| 82 | + break; |
| 83 | + |
| 84 | + default: |
| 85 | + throw std::invalid_argument("Unknown variant"); |
| 86 | + } |
| 87 | + }; |
| 88 | + |
| 89 | + // Activate the correct editor according to the selected mass definition |
| 90 | + QObject::connect(selectionBox, &QComboBox::currentIndexChanged, stack, &QStackedLayout::setCurrentIndex); |
| 91 | + |
| 92 | + // Initialize view from model once |
| 93 | + updateView(); |
| 94 | + |
| 95 | + // Keep model up to date on changes |
| 96 | + QObject::connect(selectionBox, &QComboBox::currentIndexChanged, this, updateModel); |
| 97 | + QObject::connect(massEdit, &DoubleSpinBox::valueChanged, this, updateModel); |
| 98 | + QObject::connect(massPerForceEdit, &DoubleSpinBox::valueChanged, this, updateModel); |
| 99 | + QObject::connect(massPerEnergyEdit, &DoubleSpinBox::valueChanged, this, updateModel); |
| 100 | +} |
0 commit comments