On sending (posting) item to the server the generated UUID is added to the local item.

This commit is contained in:
2026-02-02 16:13:44 +01:00
parent bedf8084d3
commit 63fe96fb2e
16 changed files with 145 additions and 33 deletions

View File

@ -26,6 +26,11 @@ EditItemCommand::EditItemCommand(TableModel* model,
.arg(roleName)
.arg(index.data(DEFAULT_ROLE).toString())
.arg(value.toString());
} else if (role == IdRole) {
commandText = QString("Setting '%1' of item '%2' to '%3'")
.arg(roleName)
.arg(index.data(DEFAULT_ROLE).toString())
.arg(value.toString());
} else {
qWarning() << "Role didn't match! Using a generic command text...";
commandText = QString("Edit item '%1'").arg(index.data(DEFAULT_ROLE).toString());

View File

@ -3,7 +3,7 @@
#include <QUndoCommand>
typedef QHash<int, QVariant> ModelItemValues;
typedef QMap<int, QVariant> ModelItemValues;
class TableModel;

View File

@ -5,7 +5,7 @@
class TableModel;
typedef QHash<int, QVariant> ModelItemValues;
typedef QMap<int, QVariant> ModelItemValues;
class RemoveRowsCommand : public QUndoCommand {
public:

View File

@ -35,10 +35,11 @@ static QList<UserRoles> INT_ROLES = {AmountRole};
static QList<UserRoles> DOUBLE_ROLES = {FactorRole};
/// JSON keys
static const QString ITEM_KEY_STRING = "items";
static const QString ITEMS_KEY_STRING = "items";
static const QString ITEM_KEY_STRING = "item";
/// file naming
static const QString ITEM_FILE_NAME = ITEM_KEY_STRING + ".json";
static const QString ITEMS_FILE_NAME = ITEMS_KEY_STRING + ".json";
/// functions
static int GET_ROLE_FOR_COLUMN(const int column) {

View File

@ -3,7 +3,7 @@
#include <QVariant>
typedef QHash<int, QVariant> ModelItemValues;
typedef QMap<int, QVariant> ModelItemValues;
class ModelItem {
public:
@ -18,7 +18,7 @@ class ModelItem {
QJsonObject toJsonObject() const;
private:
QHash<int, QVariant> m_values;
ModelItemValues m_values;
};
#endif // MODELITEM_H

View File

@ -29,7 +29,7 @@ QByteArray TableModel::generateExampleItems() {
array.append(itemObject);
}
rootObject.insert(ITEM_KEY_STRING, array);
rootObject.insert(ITEMS_KEY_STRING, array);
doc.setObject(rootObject);
return doc.toJson();
@ -83,6 +83,7 @@ QVariant TableModel::data(const QModelIndex& index, int role) const {
case InfoRole:
case AmountRole:
case FactorRole:
case IdRole:
return m_items.at(row)->data(role);
case ToStringRole:
return m_items.at(row)->toString();
@ -152,7 +153,7 @@ QJsonDocument TableModel::getAllItemsAsJsonDoc() const {
QJsonObject itemObject = item->toJsonObject();
array.append(itemObject);
}
rootObject.insert(ITEM_KEY_STRING, array);
rootObject.insert(ITEMS_KEY_STRING, array);
doc.setObject(rootObject);
return doc;
@ -175,11 +176,37 @@ QList<QStringList> TableModel::getItemsAsStringLists() const {
QByteArray TableModel::jsonDataForServer(const QModelIndex& currentIndex) const {
const QJsonObject itemObject = data(currentIndex, ToJsonRole).toJsonObject();
QJsonObject rootObject;
rootObject.insert("item", itemObject);
rootObject.insert(ITEM_KEY_STRING, itemObject);
const QJsonDocument jsonDoc(rootObject);
return jsonDoc.toJson(QJsonDocument::Compact);
}
QString TableModel::updateItemsFromJson(const QByteArray& jsonData) {
/// convert JSON data into a list of item values
const QList<ModelItemValues> valueList = JsonParser::toItemValuesList(jsonData, ITEM_KEY_STRING);
/// for each item values:
// NEXT iterate over all value items in the list (or disable updating multiple items for now)
// for (ModelItemValues itemValues : valueList) {
// }
// NEXT encapsulate into updateItem(const ModelItemValues& itemValues)
QModelIndex foundIndex = searchItemIndex(valueList.first());
qDebug() << "Search done!";
if (foundIndex == QModelIndex()) {
const QString errorMessage = "No matching item found!";
qWarning() << errorMessage;
return errorMessage;
} else {
qInfo() << "Item found!";
/// update existing item
QMap<int, QVariant> roles = valueList.first();
setItemData(foundIndex, roles);
/// return status what happened in this function (i. e. "Created x items, updated y items.")
// return data(foundIndex, ToStringRole).toString();
return QString("Item found at row %1.").arg(foundIndex.row());
}
}
bool TableModel::removeRows(int firstRow, int nRows, const QModelIndex& parentIndex) {
if (parentIndex != QModelIndex()) {
qWarning() << "Removing of child rows is not supported yet!";
@ -203,7 +230,7 @@ void TableModel::appendItems(const QByteArray& jsonDoc) { insertItems(-1, jsonDo
void TableModel::insertItems(int startPosition,
const QByteArray& jsonDoc,
const QModelIndex& parentIndex) {
const QList<ModelItemValues> valueList = JsonParser::toItemValuesList(jsonDoc, ITEM_KEY_STRING);
const QList<ModelItemValues> valueList = JsonParser::toItemValuesList(jsonDoc, ITEMS_KEY_STRING);
if (valueList.empty()) {
/// don't try inserting if no values to insert
@ -302,3 +329,60 @@ bool TableModel::isEmptyValueEqualToZero(const int role) const {
return false;
}
}
QModelIndex TableModel::searchItemIndex(const ModelItemValues givenItemValues) const {
// iterate over indexes to search item : see searchItem(...);
// for (const shared_ptr<ModelItem>& item : m_items) {
for (int row = 0; row < rowCount(); ++row) {
qDebug() << "Processing item at row" << row << "...";
QModelIndex itemIndex = index(row, 0);
if (isItemEqualToItemValues(itemIndex, givenItemValues)) {
qInfo() << "Found item at row" << row << "! Returning its index...";
return itemIndex;
}
}
qDebug() << "No matching item found. Returning empty pointer...";
return {};
}
bool TableModel::isItemEqualToItemValues(const QModelIndex& itemIndex,
const ModelItemValues givenItemValues) const {
/// do both have a UUID?
QVariant idOfItem = data(itemIndex, IdRole);
QVariant given = givenItemValues.value(IdRole);
if (idOfItem.isValid() && given.isValid()) {
/// are the UUIDs the same?
if (idOfItem.toString() == given.toString()) {
qInfo() << "UUIDs are the same.";
return true;
} else {
qDebug() << "UUIDs are NOT the same.";
return false;
}
} else {
/// are all other values the same? (for now only USER_FACING_ROLES are checked)
QListIterator<UserRoles> i(USER_FACING_ROLES);
while (i.hasNext()) {
const UserRoles role = i.next();
const QString roleName = ROLE_NAMES.value(role);
const QVariant valueOfItem = data(itemIndex, role);
const QVariant givenValue = givenItemValues.value(role);
if (STRING_ROLES.contains(role)) {
if (valueOfItem.toString() != givenValue.toString()) {
return false;
}
} else if (INT_ROLES.contains(role)) {
if (valueOfItem.toInt() != givenValue.toInt()) {
return false;
}
} else if (DOUBLE_ROLES.contains(role)) {
if (valueOfItem.toDouble() != givenValue.toDouble()) {
return false;
}
} else {
qCritical() << QString("Can't find data type for role %1!!!").arg(role);
}
}
return true;
}
}

View File

@ -8,7 +8,7 @@ class ModelItem;
using namespace std;
typedef QHash<int, QVariant> ModelItemValues;
typedef QMap<int, QVariant> ModelItemValues;
class TableModel : public QAbstractTableModel {
Q_OBJECT
@ -40,6 +40,8 @@ class TableModel : public QAbstractTableModel {
QByteArray jsonDataForServer(const QModelIndex& currentIndex) const;
QString updateItemsFromJson(const QByteArray& jsonData);
public slots:
// bool insertRows(int position, int rows, const QModelIndex& parentIndex = QModelIndex())
// override;
@ -68,6 +70,9 @@ class TableModel : public QAbstractTableModel {
QMap<int, QVariant> onlyChangedValues(const QModelIndex& index,
const QMap<int, QVariant>& roleValueMap) const;
bool isEmptyValueEqualToZero(const int role) const;
QModelIndex searchItemIndex(const ModelItemValues givenItemValues) const;
bool isItemEqualToItemValues(const QModelIndex& itemIndex,
const ModelItemValues givenItemValues) const;
};
#endif // TABLEMODEL_H