initial port for Cevo game in C++ (libs)
This commit is contained in:
commit
8436242032
375 files changed
+46282
No files matched your search
@@ -0,0 +1,177 @@
|
||||
#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
|
||||
Reference in new issue
Block a user