diff --git a/formats/jsonparser.cpp b/formats/jsonparser.cpp index aa24722..a29f8e7 100644 --- a/formats/jsonparser.cpp +++ b/formats/jsonparser.cpp @@ -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 keyValuePair = getKeyValuePair(itemJsonObject, idRole); + values.insert(keyValuePair.first, keyValuePair.second); + } + QListIterator i(USER_FACING_ROLES); while (i.hasNext()) { const UserRoles role = i.next(); diff --git a/genericcore.cpp b/genericcore.cpp index 4394f3b..e757136 100644 --- a/genericcore.cpp +++ b/genericcore.cpp @@ -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); } diff --git a/model/metadata.h b/model/metadata.h index 91da345..b5c4bda 100644 --- a/model/metadata.h +++ b/model/metadata.h @@ -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 USER_FACING_ROLES = {NameRole, DescriptionRole, InfoRole, AmountRole, FactorRole}; -static QHash ROLE_NAMES = {{NameRole, "Name"}, - {DescriptionRole, "Description"}, - {InfoRole, "Info"}, - {AmountRole, "Amount"}, - {FactorRole, "Factor"}}; -static QList STRING_ROLES = {NameRole, DescriptionRole, InfoRole}; -static QList INT_ROLES = {AmountRole}; -static QList DOUBLE_ROLES = {FactorRole}; +static QHash ROLE_NAMES = { + {NameRole, "Name"}, {DescriptionRole, "Description"}, {InfoRole, "Info"}, + {AmountRole, "Amount"}, {FactorRole, "Factor"}, {ToStringRole, "ToString"}, + {IdRole, "id"}}; +static QList STRING_ROLES = {NameRole, DescriptionRole, InfoRole, IdRole}; +static QList INT_ROLES = {AmountRole}; +static QList DOUBLE_ROLES = {FactorRole}; /// JSON keys static const QString ITEM_KEY_STRING = "items"; diff --git a/model/modelitem.cpp b/model/modelitem.cpp index a096705..88ac31b 100644 --- a/model/modelitem.cpp +++ b/model/modelitem.cpp @@ -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 i(USER_FACING_ROLES); while (i.hasNext()) { diff --git a/model/tablemodel.cpp b/model/tablemodel.cpp index e22a151..504ceec 100644 --- a/model/tablemodel.cpp +++ b/model/tablemodel.cpp @@ -49,16 +49,16 @@ QHash 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& 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 roles = changedValues.keys(); roles.insert(0, Qt::DisplayRole); emit dataChanged(firstIndex, lastIndex, roles.toVector());