Files
c-evo-evo/common/src/Item.cpp
T

206 lines
6.8 KiB
C++

// 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/Item.h"
#include <QCoreApplication>
#include <stdexcept>
namespace Cevo {
QString dataTypeStr(DataType dataType)
{
switch (dataType) {
case DataType::None:
return QStringLiteral("None");
case DataType::String:
return QStringLiteral("String");
case DataType::Boolean:
return QStringLiteral("Boolean");
case DataType::Integer:
return QStringLiteral("Integer");
case DataType::Float:
return QStringLiteral("Float");
case DataType::Color:
return QStringLiteral("Color");
case DataType::Time:
return QStringLiteral("Time");
case DataType::Date:
return QStringLiteral("Date");
case DataType::DateTime:
return QStringLiteral("DateTime");
case DataType::Enumeration:
return QStringLiteral("Enumeration");
case DataType::Reference:
return QStringLiteral("Reference");
}
return {};
}
ItemField &ItemFields::addField(int index, const QString &sysName, const QString &name, DataType dataType)
{
ItemField field;
field.index = index;
field.sysName = sysName;
field.name = name;
field.dataType = dataType;
append(field);
return last();
}
const ItemField *ItemFields::searchByIndex(int index) const
{
for (const ItemField &field : *this)
if (field.index == index)
return &field;
return nullptr;
}
ItemFields Item::getFields() const
{
ItemFields fields;
fields.addField(1, QStringLiteral("Name"), QCoreApplication::translate("Item", "Name"), DataType::String);
return fields;
}
const ItemField *Item::getField(int index) const
{
return getFields().searchByIndex(index);
}
QVariant Item::getValue(int index) const
{
throw std::out_of_range(QStringLiteral("Unsupported value index %1").arg(index).toStdString());
}
void Item::setValue(int index, const QVariant &value)
{
Q_UNUSED(value);
throw std::out_of_range(QStringLiteral("Unsupported value index %1").arg(index).toStdString());
}
QString Item::getValueAsText(int index) const
{
const ItemField *field = getField(index);
if (!field)
throw std::invalid_argument("Unknown field index");
switch (field->dataType) {
case DataType::Integer:
return QString::number(getValueInt(index));
case DataType::String:
return getValueString(index);
case DataType::Color:
return {};
case DataType::Enumeration:
return field->enumStates.value(getValueEnum(index));
case DataType::Reference: {
const Item *item = getValueReference(index);
return item ? item->name : QString();
}
case DataType::Boolean:
return getValueBool(index) ? QCoreApplication::translate("Item", "Yes")
: QCoreApplication::translate("Item", "No");
default:
throw std::invalid_argument(("Unsupported field value data type " + dataTypeStr(field->dataType)).toStdString());
}
}
void Item::assign(const Item &source)
{
id = source.id;
const ItemFields fields = getFields();
for (const ItemField &field : fields) {
switch (field.dataType) {
case DataType::String:
setValueString(field.index, source.getValueString(field.index));
break;
case DataType::Color:
setValueColor(field.index, source.getValueColor(field.index));
break;
case DataType::Integer:
setValueInt(field.index, source.getValueInt(field.index));
break;
case DataType::Boolean:
setValueBool(field.index, source.getValueBool(field.index));
break;
case DataType::Enumeration:
setValueEnum(field.index, source.getValueEnum(field.index));
break;
case DataType::Reference:
setValueReference(field.index, source.getValueReference(field.index));
break;
default:
throw std::invalid_argument(
("Unsupported field value data type " + dataTypeStr(field.dataType)).toStdString());
}
}
}
bool Item::compare(const Item &other) const
{
if (id != other.id)
return false;
const ItemFields fields = getFields();
for (const ItemField &field : fields) {
bool equal = true;
switch (field.dataType) {
case DataType::String:
equal = getValueString(field.index) == other.getValueString(field.index);
break;
case DataType::Color:
equal = getValueColor(field.index) == other.getValueColor(field.index);
break;
case DataType::Integer:
equal = getValueInt(field.index) == other.getValueInt(field.index);
break;
case DataType::Boolean:
equal = getValueBool(field.index) == other.getValueBool(field.index);
break;
case DataType::Enumeration:
equal = getValueEnum(field.index) == other.getValueEnum(field.index);
break;
case DataType::Reference:
equal = getValueReference(field.index) == other.getValueReference(field.index);
break;
default:
throw std::invalid_argument(
("Unsupported field value data type " + dataTypeStr(field.dataType)).toStdString());
}
if (!equal)
return false;
}
return true;
}
QString Item::className() const
{
return QCoreApplication::translate("Item", "Item");
}
} // namespace Cevo