Added IdRole for the server generated UUID of the items.

This commit is contained in:
2026-01-29 09:27:50 +01:00
parent bc96a805f8
commit 08c2e3a093
5 changed files with 38 additions and 14 deletions

View File

@ -75,6 +75,14 @@ QJsonArray JsonParser::extractItemArray(const QByteArray& jsonData, const QStrin
ModelItemValues JsonParser::jsonObjectToItemValues(const QJsonObject& itemJsonObject) {
ModelItemValues values;
const UserRoles idRole = IdRole;
const QString idRoleName = ROLE_NAMES.value(idRole);
// QVariant idValue = data(idRole);
if (itemJsonObject.contains(idRoleName)) {
std::pair<int, QVariant> keyValuePair = getKeyValuePair(itemJsonObject, idRole);
values.insert(keyValuePair.first, keyValuePair.second);
}
QListIterator<UserRoles> i(USER_FACING_ROLES);
while (i.hasNext()) {
const UserRoles role = i.next();

View File

@ -150,6 +150,7 @@ void GenericCore::onSendItemTriggered(const int row) {
void GenericCore::onItemsFetched(const QByteArray jsonDoc) {
emit displayStatusMessage("New items fetched.");
// TODO ? check compability of JSON structure beforehand?
// TODO check if item already exists?
m_mainModel->appendItems(jsonDoc);
}
@ -158,6 +159,7 @@ void GenericCore::onItemsFetchFailure(const QString errorString) {
}
void GenericCore::onPostRequestSuccessful(const QString message) {
// NEXT search local item an set server generated UUID
emit displayStatusMessage(message);
}

View File

@ -16,20 +16,21 @@ enum UserRoles {
AmountRole,
FactorRole,
/// helper roles
ToStringRole
ToStringRole,
IdRole
};
static UserRoles DEFAULT_ROLE = NameRole;
static UserRoles DEFAULT_ROLE = NameRole;
// TODO ?rename USER_FACING_ROLES -> MAIN_ROLES ?
static QList<UserRoles> USER_FACING_ROLES = {NameRole, DescriptionRole, InfoRole, AmountRole,
FactorRole};
static QHash<int, QByteArray> ROLE_NAMES = {{NameRole, "Name"},
{DescriptionRole, "Description"},
{InfoRole, "Info"},
{AmountRole, "Amount"},
{FactorRole, "Factor"}};
static QList<UserRoles> STRING_ROLES = {NameRole, DescriptionRole, InfoRole};
static QList<UserRoles> INT_ROLES = {AmountRole};
static QList<UserRoles> DOUBLE_ROLES = {FactorRole};
static QHash<int, QByteArray> ROLE_NAMES = {
{NameRole, "Name"}, {DescriptionRole, "Description"}, {InfoRole, "Info"},
{AmountRole, "Amount"}, {FactorRole, "Factor"}, {ToStringRole, "ToString"},
{IdRole, "id"}};
static QList<UserRoles> STRING_ROLES = {NameRole, DescriptionRole, InfoRole, IdRole};
static QList<UserRoles> INT_ROLES = {AmountRole};
static QList<UserRoles> DOUBLE_ROLES = {FactorRole};
/// JSON keys
static const QString ITEM_KEY_STRING = "items";

View File

@ -55,12 +55,25 @@ QString ModelItem::toString() const {
// result.append(value.toString());
result.append(QString("%1: %2\n").arg(roleName, data(role).toString()));
}
const UserRoles idRole = IdRole;
QVariant idValue = data(idRole);
if (!idValue.isNull()) {
const QString idRoleName = ROLE_NAMES.value(idRole);
result.append(QString("%1: %2\n").arg(idRoleName, idValue.toString()));
}
return result;
}
QJsonObject ModelItem::toJsonObject() const {
QJsonObject itemObject;
// TODO add UUID and dates (entry, modification, end)
const UserRoles idRole = IdRole;
QVariant idValue = data(idRole);
if (!idValue.isNull()) {
const QString idRoleName = ROLE_NAMES.value(idRole);
itemObject.insert(idRoleName, idValue.toString());
}
QListIterator<UserRoles> i(USER_FACING_ROLES);
while (i.hasNext()) {

View File

@ -49,16 +49,16 @@ QHash<int, QByteArray> TableModel::roleNames() const { return ROLE_NAMES; }
int TableModel::rowCount(const QModelIndex& parent) const {
if (parent.isValid()) {
return 0; // no children
return 0; /// no children
}
return m_items.size();
}
int TableModel::columnCount(const QModelIndex& parent) const {
if (parent.isValid()) {
return 0; // no children
return 0; /// no children
}
return ROLE_NAMES.size();
return USER_FACING_ROLES.size();
}
QVariant TableModel::data(const QModelIndex& index, int role) const {
@ -249,7 +249,7 @@ void TableModel::execEditItemData(const int row, const QMap<int, QVariant>& chan
/// is getting notified about (potential) data changes; dataChanged should be called only for
/// the affected columns
const QModelIndex firstIndex = this->index(row, 0);
const QModelIndex lastIndex = this->index(row, ROLE_NAMES.size() - 1);
const QModelIndex lastIndex = this->index(row, USER_FACING_ROLES.size() - 1);
QList<int> roles = changedValues.keys();
roles.insert(0, Qt::DisplayRole);
emit dataChanged(firstIndex, lastIndex, roles.toVector());