Retrieving the JSON data of the current index to send to the server through the proxy model. Sending this data happens by triggering the core.
This commit is contained in:
@ -142,16 +142,15 @@ bool GenericCore::isSyncServerSetup() const {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GenericCore::onSendItemTriggered(const int row) {
|
void GenericCore::onSendItemTriggered(const QByteArray& jsonData) {
|
||||||
// NEXT gather item values from model to send to server
|
m_serverCommunicator->postItems(jsonData);
|
||||||
m_serverCommunicator->postItems();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void GenericCore::onItemsFetched(const QByteArray jsonDoc) {
|
void GenericCore::onItemsFetched(const QByteArray jsonData) {
|
||||||
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?
|
// TODO check if item already exists?
|
||||||
m_mainModel->appendItems(jsonDoc);
|
m_mainModel->appendItems(jsonData);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GenericCore::onItemsFetchFailure(const QString errorString) {
|
void GenericCore::onItemsFetchFailure(const QString errorString) {
|
||||||
|
|||||||
@ -36,8 +36,8 @@ class GenericCore : public QObject {
|
|||||||
bool isSyncServerSetup() const;
|
bool isSyncServerSetup() const;
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void onSendItemTriggered(const int row);
|
void onSendItemTriggered(const QByteArray& jsonData);
|
||||||
void onItemsFetched(const QByteArray jsonDoc);
|
void onItemsFetched(const QByteArray jsonData);
|
||||||
void onItemsFetchFailure(const QString errorString);
|
void onItemsFetchFailure(const QString errorString);
|
||||||
void onPostRequestSuccessful(const QString message);
|
void onPostRequestSuccessful(const QString message);
|
||||||
void onPostRequestFailure(const QString errorString);
|
void onPostRequestFailure(const QString errorString);
|
||||||
@ -45,7 +45,7 @@ class GenericCore : public QObject {
|
|||||||
signals:
|
signals:
|
||||||
void displayStatusMessage(QString message);
|
void displayStatusMessage(QString message);
|
||||||
void fetchItemsFromServer();
|
void fetchItemsFromServer();
|
||||||
void sendItemToServer(int row);
|
void sendItemToServer(const QByteArray& jsonData);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QUndoStack* m_modelUndoStack;
|
QUndoStack* m_modelUndoStack;
|
||||||
|
|||||||
@ -23,6 +23,11 @@ QItemSelection GeneralSortFilterModel::findItems(const QString& text) const {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QByteArray GeneralSortFilterModel::jsonDataForServer(const QModelIndex& proxyIndex) {
|
||||||
|
const QModelIndex sourceIndex = mapToSource(proxyIndex);
|
||||||
|
return m_tableModel->jsonDataForServer(sourceIndex);
|
||||||
|
}
|
||||||
|
|
||||||
void GeneralSortFilterModel::appendItems(const QByteArray& jsonDoc) {
|
void GeneralSortFilterModel::appendItems(const QByteArray& jsonDoc) {
|
||||||
m_tableModel->appendItems(jsonDoc);
|
m_tableModel->appendItems(jsonDoc);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,6 +18,8 @@ class GeneralSortFilterModel : public QSortFilterProxyModel {
|
|||||||
*/
|
*/
|
||||||
QItemSelection findItems(const QString& text) const;
|
QItemSelection findItems(const QString& text) const;
|
||||||
|
|
||||||
|
QByteArray jsonDataForServer(const QModelIndex& proxyIndex);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void appendItems(const QByteArray& jsonDoc);
|
void appendItems(const QByteArray& jsonDoc);
|
||||||
bool removeRows(int firstRow, int nRows, const QModelIndex& parentIndex = QModelIndex()) override;
|
bool removeRows(int firstRow, int nRows, const QModelIndex& parentIndex = QModelIndex()) override;
|
||||||
|
|||||||
@ -15,9 +15,11 @@ enum UserRoles {
|
|||||||
InfoRole,
|
InfoRole,
|
||||||
AmountRole,
|
AmountRole,
|
||||||
FactorRole,
|
FactorRole,
|
||||||
/// helper roles
|
/// Non user facing
|
||||||
|
IdRole,
|
||||||
|
/// read only roles
|
||||||
ToStringRole,
|
ToStringRole,
|
||||||
IdRole
|
ToJsonRole
|
||||||
};
|
};
|
||||||
|
|
||||||
static UserRoles DEFAULT_ROLE = NameRole;
|
static UserRoles DEFAULT_ROLE = NameRole;
|
||||||
|
|||||||
@ -86,6 +86,8 @@ QVariant TableModel::data(const QModelIndex& index, int role) const {
|
|||||||
return m_items.at(row)->data(role);
|
return m_items.at(row)->data(role);
|
||||||
case ToStringRole:
|
case ToStringRole:
|
||||||
return m_items.at(row)->toString();
|
return m_items.at(row)->toString();
|
||||||
|
case ToJsonRole:
|
||||||
|
return m_items.at(row)->toJsonObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
return QVariant();
|
return QVariant();
|
||||||
@ -169,6 +171,15 @@ QList<QStringList> TableModel::getItemsAsStringLists() const {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO use item selection as parameter to wrap multiple items into JSON data structure
|
||||||
|
QByteArray TableModel::jsonDataForServer(const QModelIndex& currentIndex) const {
|
||||||
|
const QJsonObject itemObject = data(currentIndex, ToJsonRole).toJsonObject();
|
||||||
|
QJsonObject rootObject;
|
||||||
|
rootObject.insert("item", itemObject);
|
||||||
|
const QJsonDocument jsonDoc(rootObject);
|
||||||
|
return jsonDoc.toJson(QJsonDocument::Compact);
|
||||||
|
}
|
||||||
|
|
||||||
bool TableModel::removeRows(int firstRow, int nRows, const QModelIndex& parentIndex) {
|
bool TableModel::removeRows(int firstRow, int nRows, const QModelIndex& parentIndex) {
|
||||||
if (parentIndex != QModelIndex()) {
|
if (parentIndex != QModelIndex()) {
|
||||||
qWarning() << "Removing of child rows is not supported yet!";
|
qWarning() << "Removing of child rows is not supported yet!";
|
||||||
|
|||||||
@ -38,6 +38,8 @@ class TableModel : public QAbstractTableModel {
|
|||||||
QJsonDocument getAllItemsAsJsonDoc() const;
|
QJsonDocument getAllItemsAsJsonDoc() const;
|
||||||
QList<QStringList> getItemsAsStringLists() const;
|
QList<QStringList> getItemsAsStringLists() const;
|
||||||
|
|
||||||
|
QByteArray jsonDataForServer(const QModelIndex& currentIndex) const;
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
// bool insertRows(int position, int rows, const QModelIndex& parentIndex = QModelIndex())
|
// bool insertRows(int position, int rows, const QModelIndex& parentIndex = QModelIndex())
|
||||||
// override;
|
// override;
|
||||||
|
|||||||
@ -60,20 +60,7 @@ void ServerCommunicator::fetchItems() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServerCommunicator::postItems() {
|
void ServerCommunicator::postItems(const QByteArray& jsonData) {
|
||||||
QJsonObject itemObject;
|
|
||||||
itemObject["name"] = "Post test 1";
|
|
||||||
itemObject["description"] = "Post description 1";
|
|
||||||
itemObject["info"] = "Post info 1";
|
|
||||||
itemObject["amount"] = 1;
|
|
||||||
itemObject["factor"] = 5.3;
|
|
||||||
|
|
||||||
QJsonObject rootObject;
|
|
||||||
rootObject.insert("item", itemObject);
|
|
||||||
|
|
||||||
QJsonDocument jsonDoc(rootObject);
|
|
||||||
QByteArray jsonData = jsonDoc.toJson();
|
|
||||||
|
|
||||||
QNetworkReply* reply = m_restManager->post(m_serviceApi->createRequest(ROUTE_ITEMS), jsonData);
|
QNetworkReply* reply = m_restManager->post(m_serviceApi->createRequest(ROUTE_ITEMS), jsonData);
|
||||||
|
|
||||||
QObject::connect(reply, &QNetworkReply::finished, [=]() {
|
QObject::connect(reply, &QNetworkReply::finished, [=]() {
|
||||||
|
|||||||
@ -18,7 +18,8 @@ class ServerCommunicator : public QObject {
|
|||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void fetchItems();
|
void fetchItems();
|
||||||
void postItems(); /// NEXT add item(s) as argument;
|
void postItems(const QByteArray& jsonData);
|
||||||
|
// NEXT void deleteItems(QList<QString> idList)
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void urlChanged();
|
void urlChanged();
|
||||||
|
|||||||
Reference in New Issue
Block a user