Added IdRole for the server generated UUID of the items.
This commit is contained in:
@ -75,6 +75,14 @@ QJsonArray JsonParser::extractItemArray(const QByteArray& jsonData, const QStrin
|
|||||||
ModelItemValues JsonParser::jsonObjectToItemValues(const QJsonObject& itemJsonObject) {
|
ModelItemValues JsonParser::jsonObjectToItemValues(const QJsonObject& itemJsonObject) {
|
||||||
ModelItemValues values;
|
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);
|
QListIterator<UserRoles> i(USER_FACING_ROLES);
|
||||||
while (i.hasNext()) {
|
while (i.hasNext()) {
|
||||||
const UserRoles role = i.next();
|
const UserRoles role = i.next();
|
||||||
|
|||||||
@ -150,6 +150,7 @@ void GenericCore::onSendItemTriggered(const int row) {
|
|||||||
void GenericCore::onItemsFetched(const QByteArray jsonDoc) {
|
void GenericCore::onItemsFetched(const QByteArray jsonDoc) {
|
||||||
emit displayStatusMessage("New items fetched.");
|
emit displayStatusMessage("New items fetched.");
|
||||||
// TODO ? check compability of JSON structure beforehand?
|
// TODO ? check compability of JSON structure beforehand?
|
||||||
|
// TODO check if item already exists?
|
||||||
m_mainModel->appendItems(jsonDoc);
|
m_mainModel->appendItems(jsonDoc);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -158,6 +159,7 @@ void GenericCore::onItemsFetchFailure(const QString errorString) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void GenericCore::onPostRequestSuccessful(const QString message) {
|
void GenericCore::onPostRequestSuccessful(const QString message) {
|
||||||
|
// NEXT search local item an set server generated UUID
|
||||||
emit displayStatusMessage(message);
|
emit displayStatusMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -16,18 +16,19 @@ enum UserRoles {
|
|||||||
AmountRole,
|
AmountRole,
|
||||||
FactorRole,
|
FactorRole,
|
||||||
/// helper roles
|
/// 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,
|
static QList<UserRoles> USER_FACING_ROLES = {NameRole, DescriptionRole, InfoRole, AmountRole,
|
||||||
FactorRole};
|
FactorRole};
|
||||||
static QHash<int, QByteArray> ROLE_NAMES = {{NameRole, "Name"},
|
static QHash<int, QByteArray> ROLE_NAMES = {
|
||||||
{DescriptionRole, "Description"},
|
{NameRole, "Name"}, {DescriptionRole, "Description"}, {InfoRole, "Info"},
|
||||||
{InfoRole, "Info"},
|
{AmountRole, "Amount"}, {FactorRole, "Factor"}, {ToStringRole, "ToString"},
|
||||||
{AmountRole, "Amount"},
|
{IdRole, "id"}};
|
||||||
{FactorRole, "Factor"}};
|
static QList<UserRoles> STRING_ROLES = {NameRole, DescriptionRole, InfoRole, IdRole};
|
||||||
static QList<UserRoles> STRING_ROLES = {NameRole, DescriptionRole, InfoRole};
|
|
||||||
static QList<UserRoles> INT_ROLES = {AmountRole};
|
static QList<UserRoles> INT_ROLES = {AmountRole};
|
||||||
static QList<UserRoles> DOUBLE_ROLES = {FactorRole};
|
static QList<UserRoles> DOUBLE_ROLES = {FactorRole};
|
||||||
|
|
||||||
|
|||||||
@ -55,12 +55,25 @@ QString ModelItem::toString() const {
|
|||||||
// result.append(value.toString());
|
// result.append(value.toString());
|
||||||
result.append(QString("%1: %2\n").arg(roleName, data(role).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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
QJsonObject ModelItem::toJsonObject() const {
|
QJsonObject ModelItem::toJsonObject() const {
|
||||||
QJsonObject itemObject;
|
QJsonObject itemObject;
|
||||||
// TODO add UUID and dates (entry, modification, end)
|
// 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);
|
QListIterator<UserRoles> i(USER_FACING_ROLES);
|
||||||
while (i.hasNext()) {
|
while (i.hasNext()) {
|
||||||
|
|||||||
@ -49,16 +49,16 @@ QHash<int, QByteArray> TableModel::roleNames() const { return ROLE_NAMES; }
|
|||||||
|
|
||||||
int TableModel::rowCount(const QModelIndex& parent) const {
|
int TableModel::rowCount(const QModelIndex& parent) const {
|
||||||
if (parent.isValid()) {
|
if (parent.isValid()) {
|
||||||
return 0; // no children
|
return 0; /// no children
|
||||||
}
|
}
|
||||||
return m_items.size();
|
return m_items.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
int TableModel::columnCount(const QModelIndex& parent) const {
|
int TableModel::columnCount(const QModelIndex& parent) const {
|
||||||
if (parent.isValid()) {
|
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 {
|
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
|
/// is getting notified about (potential) data changes; dataChanged should be called only for
|
||||||
/// the affected columns
|
/// the affected columns
|
||||||
const QModelIndex firstIndex = this->index(row, 0);
|
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();
|
QList<int> roles = changedValues.keys();
|
||||||
roles.insert(0, Qt::DisplayRole);
|
roles.insert(0, Qt::DisplayRole);
|
||||||
emit dataChanged(firstIndex, lastIndex, roles.toVector());
|
emit dataChanged(firstIndex, lastIndex, roles.toVector());
|
||||||
|
|||||||
Reference in New Issue
Block a user