68 lines
1.7 KiB
C++
68 lines
1.7 KiB
C++
#include "modelitem.h"
|
|
|
|
#include "metadata.h"
|
|
|
|
#include <QJsonObject>
|
|
#include <QJsonValue>
|
|
|
|
ModelItem::ModelItem(const QHash<int, QVariant> values)
|
|
: m_values(values) {}
|
|
|
|
QVariant ModelItem::data(int role) const { return m_values.value(role); }
|
|
|
|
bool ModelItem::setData(const QVariant& value, int role) {
|
|
bool valueChanged = false;
|
|
if (m_values.contains(role)) {
|
|
if (m_values.value(role) != value) {
|
|
valueChanged = true;
|
|
}
|
|
}
|
|
m_values[role] = value;
|
|
|
|
return valueChanged;
|
|
}
|
|
|
|
bool ModelItem::setItemData(const QMap<int, QVariant>& changedValues) {
|
|
bool valueChanged = false;
|
|
|
|
QMap<int, QVariant>::const_iterator citer = changedValues.constBegin();
|
|
|
|
while (citer != changedValues.constEnd()) {
|
|
const int role = citer.key();
|
|
const QVariant value = citer.value();
|
|
|
|
if (m_values.contains(role)) {
|
|
if (m_values.value(role) != value) {
|
|
valueChanged = true;
|
|
}
|
|
}
|
|
m_values[role] = value;
|
|
|
|
citer++;
|
|
}
|
|
|
|
return valueChanged;
|
|
}
|
|
|
|
QJsonObject ModelItem::toJsonObject() const {
|
|
QJsonObject itemObject;
|
|
// TODO add UUID and dates (entry, modification, end)
|
|
|
|
QListIterator<UserRoles> i(USER_FACING_ROLES);
|
|
while (i.hasNext()) {
|
|
const UserRoles role = i.next();
|
|
const QString roleName = ROLE_NAMES.value(role);
|
|
const QVariant value = data(role);
|
|
if (STRING_ROLES.contains(role)) {
|
|
itemObject.insert(roleName, value.toString());
|
|
} else if (INT_ROLES.contains(role)) {
|
|
itemObject.insert(roleName, value.toInt());
|
|
} else if (DOUBLE_ROLES.contains(role)) {
|
|
itemObject.insert(roleName, value.toDouble());
|
|
} else {
|
|
qCritical() << QString("Cant find data type of role %1!!!").arg(role);
|
|
}
|
|
}
|
|
return itemObject;
|
|
}
|