Items can be saved to JSON file "items.json" (in standard location).

This commit is contained in:
2025-12-23 10:14:13 +01:00
parent 2c3d49db30
commit 0e1a0d4959
9 changed files with 119 additions and 0 deletions

View File

@ -28,6 +28,7 @@ add_library(${TARGET_APP} STATIC
model/commands/insertrowscommand.h model/commands/insertrowscommand.cpp model/commands/insertrowscommand.h model/commands/insertrowscommand.cpp
model/commands/removerowscommand.h model/commands/removerowscommand.cpp model/commands/removerowscommand.h model/commands/removerowscommand.cpp
model/commands/edititemcommand.h model/commands/edititemcommand.cpp model/commands/edititemcommand.h model/commands/edititemcommand.cpp
data/filehandler.h data/filehandler.cpp
) )
include_directories(${CMAKE_CURRENT_BINARY_DIR}) include_directories(${CMAKE_CURRENT_BINARY_DIR})

31
data/filehandler.cpp Normal file
View File

@ -0,0 +1,31 @@
#include "filehandler.h"
#include <QDebug>
#include <QDir>
#include <QJsonDocument>
#include <QStandardPaths>
bool FileHandler::saveToFile(const QJsonDocument& doc, const QString& fileName) {
qDebug() << "saving file...";
QString path = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).at(0);
qDebug() << path;
QDir dir;
if (!dir.exists(path)) {
dir.mkpath(path);
}
// qDebug() << path + fileName;
const QString filePath = path + '/' + fileName;
QFile file(filePath);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
qWarning() << "can't open file";
return false;
}
QTextStream out(&file);
out.setEncoding(QStringConverter::Utf8);
out << doc.toJson(QJsonDocument::Indented);
return true;
}
FileHandler::FileHandler() {}

15
data/filehandler.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef FILEHANDLER_H
#define FILEHANDLER_H
class QJsonDocument;
class QString;
class FileHandler {
public:
static bool saveToFile(const QJsonDocument& doc, const QString& fileName);
private:
explicit FileHandler();
};
#endif // FILEHANDLER_H

View File

@ -3,6 +3,7 @@
#include <QCoreApplication> #include <QCoreApplication>
#include <QDateTime> #include <QDateTime>
#include <QDebug> #include <QDebug>
#include <QJsonDocument>
#include <QProcess> #include <QProcess>
#include <QSettings> #include <QSettings>
#include <QString> #include <QString>
@ -10,6 +11,7 @@
#include "../../ApplicationConfig.h" #include "../../ApplicationConfig.h"
#include "CoreConfig.h" #include "CoreConfig.h"
#include "constants.h" #include "constants.h"
#include "data/filehandler.h"
#include "model/tablemodel.h" #include "model/tablemodel.h"
#include <QtGui/QUndoStack> #include <QtGui/QUndoStack>
@ -88,6 +90,21 @@ QUndoStack* GenericCore::getModelUndoStack() const { return m_modelUndoStack; }
std::shared_ptr<TableModel> GenericCore::getModel() const { return m_mainModel; } std::shared_ptr<TableModel> GenericCore::getModel() const { return m_mainModel; }
void GenericCore::saveItems() {
qDebug() << "saving items...";
const QJsonDocument doc = m_mainModel->getAllItemsAsJsonDoc();
const bool successfulSave = FileHandler::saveToFile(doc, "items.json");
if (successfulSave) {
// QStringList completedTaskStrings = m_model->completedTasks();
// appendCompletedTasksToFile(completedTaskStrings, "completed.txt");
m_modelUndoStack->setClean();
emit displayStatusMessage(QString("Items saved."));
} else {
emit displayStatusMessage(QString("Error: Items couldn't be saved."));
}
}
void GenericCore::setupModels() { void GenericCore::setupModels() {
m_mainModel = make_shared<TableModel>(m_modelUndoStack, this); m_mainModel = make_shared<TableModel>(m_modelUndoStack, this);
// TODO add QAbstractItemModelTester // TODO add QAbstractItemModelTester

View File

@ -25,6 +25,8 @@ class GenericCore : public QObject {
QUndoStack* getModelUndoStack() const; QUndoStack* getModelUndoStack() const;
std::shared_ptr<TableModel> getModel() const; std::shared_ptr<TableModel> getModel() const;
void saveItems();
signals: signals:
void displayStatusMessage(QString message); void displayStatusMessage(QString message);

View File

@ -1,5 +1,10 @@
#include "modelitem.h" #include "modelitem.h"
#include "tablemodel.h"
#include <QJsonObject>
#include <QJsonValue>
ModelItem::ModelItem(const QHash<int, QVariant> values) ModelItem::ModelItem(const QHash<int, QVariant> values)
: m_values(values) {} : m_values(values) {}
@ -38,3 +43,27 @@ bool ModelItem::setItemData(const QMap<int, QVariant>& changedValues) {
return valueChanged; return valueChanged;
} }
QJsonObject ModelItem::toJsonObject() const {
QJsonObject itemObject;
// itemObject.insert("uuid", m_uuid.toString());
// itemObject.insert("entryDateUTC", m_entryDateUTC.toString(Qt::ISODate));
itemObject.insert(TableModel::ROLE_NAMES.value(TableModel::NameRole),
data(TableModel::NameRole).toString());
itemObject.insert(TableModel::ROLE_NAMES.value(TableModel::DescriptionRole),
data(TableModel::DescriptionRole).toString());
itemObject.insert(TableModel::ROLE_NAMES.value(TableModel::InfoRole),
data(TableModel::InfoRole).toString());
itemObject.insert(TableModel::ROLE_NAMES.value(TableModel::AmountRole),
data(TableModel::AmountRole).toInt());
itemObject.insert(TableModel::ROLE_NAMES.value(TableModel::FactorRole),
data(TableModel::FactorRole).toReal());
// if (m_modifiedDateUTC.isValid()) {
// itemObject.insert("modifiedDateUTC", m_modifiedDateUTC.toString(Qt::ISODate));
// }
// if (m_endDateUTC.isValid()) {
// itemObject.insert("endDateUTC", m_endDateUTC.toString(Qt::ISODate));
// }
return itemObject;
}

View File

@ -12,6 +12,9 @@ class ModelItem {
// TODO change return value to list of changed roles // TODO change return value to list of changed roles
bool setItemData(const QMap<int, QVariant>& changedValues); bool setItemData(const QMap<int, QVariant>& changedValues);
// QString toString() const;
QJsonObject toJsonObject() const;
private: private:
QHash<int, QVariant> m_values; QHash<int, QVariant> m_values;
}; };

View File

@ -6,6 +6,10 @@
#include "commands/removerowscommand.h" #include "commands/removerowscommand.h"
#include "modelitem.h" #include "modelitem.h"
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
QHash<int, QByteArray> TableModel::ROLE_NAMES = {{NameRole, "Name"}, QHash<int, QByteArray> TableModel::ROLE_NAMES = {{NameRole, "Name"},
{DescriptionRole, "Description"}, {DescriptionRole, "Description"},
{InfoRole, "Info"}, {InfoRole, "Info"},
@ -241,3 +245,18 @@ bool TableModel::isEmptyValueEqualToZero(const int role) const {
} }
return false; return false;
} }
QJsonDocument TableModel::getAllItemsAsJsonDoc() const {
QJsonDocument doc = QJsonDocument();
QJsonObject rootObject;
QJsonArray array;
foreach (shared_ptr<ModelItem> item, m_items) {
QJsonObject itemObject = item->toJsonObject();
array.append(itemObject);
}
rootObject.insert("items", array);
doc.setObject(rootObject);
return doc;
}

View File

@ -34,6 +34,8 @@ class TableModel : public QAbstractTableModel {
bool setData(const QModelIndex& index, const QVariant& value, int role) override; bool setData(const QModelIndex& index, const QVariant& value, int role) override;
bool setItemData(const QModelIndex& index, const QMap<int, QVariant>& roles) override; bool setItemData(const QModelIndex& index, const QMap<int, QVariant>& roles) override;
QJsonDocument getAllItemsAsJsonDoc() 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;