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) {
|
||||
// NEXT gather item values from model to send to server
|
||||
m_serverCommunicator->postItems();
|
||||
void GenericCore::onSendItemTriggered(const QByteArray& jsonData) {
|
||||
m_serverCommunicator->postItems(jsonData);
|
||||
}
|
||||
|
||||
void GenericCore::onItemsFetched(const QByteArray jsonDoc) {
|
||||
void GenericCore::onItemsFetched(const QByteArray jsonData) {
|
||||
emit displayStatusMessage("New items fetched.");
|
||||
// TODO ? check compability of JSON structure beforehand?
|
||||
// TODO check if item already exists?
|
||||
m_mainModel->appendItems(jsonDoc);
|
||||
m_mainModel->appendItems(jsonData);
|
||||
}
|
||||
|
||||
void GenericCore::onItemsFetchFailure(const QString errorString) {
|
||||
|
||||
@ -36,8 +36,8 @@ class GenericCore : public QObject {
|
||||
bool isSyncServerSetup() const;
|
||||
|
||||
public slots:
|
||||
void onSendItemTriggered(const int row);
|
||||
void onItemsFetched(const QByteArray jsonDoc);
|
||||
void onSendItemTriggered(const QByteArray& jsonData);
|
||||
void onItemsFetched(const QByteArray jsonData);
|
||||
void onItemsFetchFailure(const QString errorString);
|
||||
void onPostRequestSuccessful(const QString message);
|
||||
void onPostRequestFailure(const QString errorString);
|
||||
@ -45,7 +45,7 @@ class GenericCore : public QObject {
|
||||
signals:
|
||||
void displayStatusMessage(QString message);
|
||||
void fetchItemsFromServer();
|
||||
void sendItemToServer(int row);
|
||||
void sendItemToServer(const QByteArray& jsonData);
|
||||
|
||||
private:
|
||||
QUndoStack* m_modelUndoStack;
|
||||
|
||||
@ -23,6 +23,11 @@ QItemSelection GeneralSortFilterModel::findItems(const QString& text) const {
|
||||
return result;
|
||||
}
|
||||
|
||||
QByteArray GeneralSortFilterModel::jsonDataForServer(const QModelIndex& proxyIndex) {
|
||||
const QModelIndex sourceIndex = mapToSource(proxyIndex);
|
||||
return m_tableModel->jsonDataForServer(sourceIndex);
|
||||
}
|
||||
|
||||
void GeneralSortFilterModel::appendItems(const QByteArray& jsonDoc) {
|
||||
m_tableModel->appendItems(jsonDoc);
|
||||
}
|
||||
|
||||
@ -18,6 +18,8 @@ class GeneralSortFilterModel : public QSortFilterProxyModel {
|
||||
*/
|
||||
QItemSelection findItems(const QString& text) const;
|
||||
|
||||
QByteArray jsonDataForServer(const QModelIndex& proxyIndex);
|
||||
|
||||
public slots:
|
||||
void appendItems(const QByteArray& jsonDoc);
|
||||
bool removeRows(int firstRow, int nRows, const QModelIndex& parentIndex = QModelIndex()) override;
|
||||
|
||||
@ -15,9 +15,11 @@ enum UserRoles {
|
||||
InfoRole,
|
||||
AmountRole,
|
||||
FactorRole,
|
||||
/// helper roles
|
||||
/// Non user facing
|
||||
IdRole,
|
||||
/// read only roles
|
||||
ToStringRole,
|
||||
IdRole
|
||||
ToJsonRole
|
||||
};
|
||||
|
||||
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);
|
||||
case ToStringRole:
|
||||
return m_items.at(row)->toString();
|
||||
case ToJsonRole:
|
||||
return m_items.at(row)->toJsonObject();
|
||||
}
|
||||
|
||||
return QVariant();
|
||||
@ -169,6 +171,15 @@ QList<QStringList> TableModel::getItemsAsStringLists() const {
|
||||
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) {
|
||||
if (parentIndex != QModelIndex()) {
|
||||
qWarning() << "Removing of child rows is not supported yet!";
|
||||
|
||||
@ -38,6 +38,8 @@ class TableModel : public QAbstractTableModel {
|
||||
QJsonDocument getAllItemsAsJsonDoc() const;
|
||||
QList<QStringList> getItemsAsStringLists() const;
|
||||
|
||||
QByteArray jsonDataForServer(const QModelIndex& currentIndex) const;
|
||||
|
||||
public slots:
|
||||
// bool insertRows(int position, int rows, const QModelIndex& parentIndex = QModelIndex())
|
||||
// override;
|
||||
|
||||
@ -60,20 +60,7 @@ void ServerCommunicator::fetchItems() {
|
||||
});
|
||||
}
|
||||
|
||||
void ServerCommunicator::postItems() {
|
||||
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();
|
||||
|
||||
void ServerCommunicator::postItems(const QByteArray& jsonData) {
|
||||
QNetworkReply* reply = m_restManager->post(m_serviceApi->createRequest(ROUTE_ITEMS), jsonData);
|
||||
|
||||
QObject::connect(reply, &QNetworkReply::finished, [=]() {
|
||||
|
||||
@ -18,7 +18,8 @@ class ServerCommunicator : public QObject {
|
||||
|
||||
public slots:
|
||||
void fetchItems();
|
||||
void postItems(); /// NEXT add item(s) as argument;
|
||||
void postItems(const QByteArray& jsonData);
|
||||
// NEXT void deleteItems(QList<QString> idList)
|
||||
|
||||
signals:
|
||||
void urlChanged();
|
||||
|
||||
Reference in New Issue
Block a user