// This file is part of c-evo-evo. // // This software is governed by the CeCILL license under French law and // abiding by the rules of distribution of free software. You can use, // modify and/or redistribute the software under the terms of the CeCILL // license as circulated by CEA, CNRS and INRIA at the following URL // "http://www.cecill.info". // // As a counterpart to the access to the source code and rights to copy, // modify and redistribute granted by the license, users are provided only // with a limited warranty and the software's author, the holder of the // economic rights, and the successive licensors have only limited // liability. // // In this respect, the user's attention is drawn to the risks associated // with loading, using, modifying and/or developing or reproducing the // software by the user in light of its specific status of free software, // that may mean that it is complicated to manipulate, and that also // therefore means that it is reserved for developers and experienced // professionals having in-depth computer knowledge. Users are therefore // encouraged to load and test the software's suitability as regards their // requirements in conditions enabling the security of their systems and/or // data to be ensured and, more generally, to use and operate it in the // same conditions as regards security. // // The fact that you are presently reading this means that you have had // knowledge of the CeCILL license and that you accept its terms. #include "Cevo/FormItem.h" #include "Cevo/ColorButton.h" #include "ui_FormItem.h" #include #include #include #include #include #include #include #include namespace Cevo { FormItem::FormItem(QWidget *parent) : FormBase(parent), ui(new Ui::FormItem) { ui->setupUi(this); connect(ui->buttonOk, &QPushButton::clicked, this, &FormItem::onOkClicked); } FormItem::~FormItem() { delete ui; } void FormItem::setItem(Item *value) { if (m_item == value) return; m_item = value; if (m_item) { initControls(); loadData(m_item); setWindowTitle(m_item->className()); } else { qDeleteAll(m_dataControls); qDeleteAll(m_dataLabels); m_dataControls.clear(); m_dataLabels.clear(); } } void FormItem::initControls() { auto *layout = qobject_cast(ui->scrollBoxContents->layout()); qDeleteAll(m_dataControls); qDeleteAll(m_dataLabels); m_dataControls.clear(); m_dataLabels.clear(); const ItemFields fields = m_item->getFields(); for (const ItemField &field : fields) { auto *label = new QLabel(field.name + ':', ui->scrollBoxContents); QWidget *control = nullptr; switch (field.dataType) { case DataType::Integer: { auto *spin = new QSpinBox(ui->scrollBoxContents); spin->setMaximum(std::numeric_limits::max()); control = spin; break; } case DataType::String: control = new QLineEdit(ui->scrollBoxContents); break; case DataType::Color: control = new ColorButton(ui->scrollBoxContents); break; case DataType::Enumeration: { auto *combo = new QComboBox(ui->scrollBoxContents); connect(combo, &QComboBox::currentIndexChanged, this, &FormItem::onControlChanged); control = combo; break; } case DataType::Reference: control = new QComboBox(ui->scrollBoxContents); break; case DataType::Boolean: control = new QCheckBox(ui->scrollBoxContents); break; default: throw std::invalid_argument(("Unsupported field value data type " + dataTypeStr(field.dataType)).toStdString()); } layout->addRow(label, control); m_dataLabels.append(label); m_dataControls.append(control); } } void FormItem::loadData(Item *item) { const ItemFields fields = item->getFields(); for (int i = 0; i < fields.count(); ++i) { const ItemField &field = fields.at(i); QWidget *control = m_dataControls.at(i); switch (field.dataType) { case DataType::Integer: qobject_cast(control)->setValue(item->getValueInt(field.index)); break; case DataType::String: qobject_cast(control)->setText(item->getValueString(field.index)); break; case DataType::Color: qobject_cast(control)->setColor(item->getValueColor(field.index)); break; case DataType::Boolean: qobject_cast(control)->setChecked(item->getValueBool(field.index)); break; case DataType::Enumeration: { auto *combo = qobject_cast(control); const QSignalBlocker blocker(combo); combo->clear(); combo->addItems(field.enumStates); combo->setCurrentIndex(item->getValueEnum(field.index)); break; } case DataType::Reference: { auto *combo = qobject_cast(control); const QSignalBlocker blocker(combo); combo->clear(); BaseItemList *referenceList = item->getReferenceList(field.index); if (referenceList) { for (int j = 0; j < referenceList->count(); ++j) { Item *refItem = referenceList->at(j); combo->addItem(refItem->name, QVariant::fromValue(static_cast(refItem))); } } Item *referenceItem = item->getValueReference(field.index); combo->setCurrentIndex(combo->findData(QVariant::fromValue(referenceItem))); break; } default: throw std::invalid_argument(("Unsupported field value data type " + dataTypeStr(field.dataType)).toStdString()); } } updateInterface(); } void FormItem::saveData(Item *item) { const ItemFields fields = item->getFields(); for (int i = 0; i < fields.count(); ++i) { const ItemField &field = fields.at(i); QWidget *control = m_dataControls.at(i); switch (field.dataType) { case DataType::Integer: item->setValueInt(field.index, qobject_cast(control)->value()); break; case DataType::String: item->setValueString(field.index, qobject_cast(control)->text()); break; case DataType::Color: item->setValueColor(field.index, qobject_cast(control)->color()); break; case DataType::Boolean: item->setValueBool(field.index, qobject_cast(control)->isChecked()); break; case DataType::Enumeration: item->setValueEnum(field.index, qobject_cast(control)->currentIndex()); break; case DataType::Reference: { auto *combo = qobject_cast(control); const int index = combo->currentIndex(); item->setValueReference(field.index, index >= 0 ? combo->currentData().value() : nullptr); break; } default: throw std::invalid_argument(("Unsupported field value data type " + dataTypeStr(field.dataType)).toStdString()); } } } void FormItem::updateInterface() { const ItemFields fields = m_item->getFields(); for (int i = 0; i < fields.count(); ++i) { const ItemField &field = fields.at(i); bool visible = field.visibleIfIndex <= 0; if (field.visibleIfIndex > 0) { const ItemField &controlField = fields.at(field.visibleIfIndex); if (controlField.dataType == DataType::Enumeration) visible = qobject_cast(m_dataControls.at(field.visibleIfIndex))->currentIndex() > 0; } m_dataControls.at(i)->setVisible(visible); m_dataLabels.at(i)->setVisible(visible); } } void FormItem::onControlChanged() { updateInterface(); } void FormItem::onOkClicked() { saveData(m_item); accept(); } } // namespace Cevo