Split TableModel::updateItemsFromJson(...) into two functions and added support for multiple items in JSON data.

This commit is contained in:
2026-02-02 16:48:42 +01:00
parent ba482e6e17
commit d4ff1ffb61
3 changed files with 16 additions and 14 deletions

View File

@ -182,27 +182,30 @@ QByteArray TableModel::jsonDataForServer(const QModelIndex& currentIndex) const
}
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());
int nChangedItems = 0;
for (const ModelItemValues& itemValues : valueList) {
bool updateHappened = updateItem(itemValues);
if (updateHappened) {
nChangedItems++;
}
}
return QString("Found and updated %1 item(s).").arg(nChangedItems);
}
bool TableModel::updateItem(const ModelItemValues& itemValues) {
QModelIndex foundIndex = searchItemIndex(itemValues);
qDebug() << "Search done!";
if (foundIndex == QModelIndex()) {
const QString errorMessage = "No matching item found!";
qWarning() << errorMessage;
return errorMessage;
return false;
} 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 QString("Item found at row %1.").arg(foundIndex.row());
setItemData(foundIndex, itemValues);
return true;
}
}