Items are loaded from default JSON file in standard location at startup. If no items are found in file, example items are generated.
This commit is contained in:
@ -28,4 +28,26 @@ bool FileHandler::saveToFile(const QJsonDocument& doc, const QString& fileName)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QByteArray FileHandler::loadJSONDataFromFile(const QString fileName) {
|
||||||
|
QByteArray jsonData;
|
||||||
|
QFile file;
|
||||||
|
QString path = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).at(0);
|
||||||
|
file.setFileName(path + "/" + fileName);
|
||||||
|
if (file.exists()) {
|
||||||
|
qDebug() << "File found, reading content...";
|
||||||
|
const bool successfulOpened = file.open(QIODevice::ReadOnly | QIODevice::Text);
|
||||||
|
if (successfulOpened) {
|
||||||
|
// TODO learn and decide on the differences between "readAll" and using
|
||||||
|
// streams
|
||||||
|
jsonData = file.readAll();
|
||||||
|
file.close();
|
||||||
|
} else {
|
||||||
|
qWarning() << "File could not be opened!";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
qInfo() << "File not found. Returning empty result...";
|
||||||
|
}
|
||||||
|
return jsonData;
|
||||||
|
}
|
||||||
|
|
||||||
FileHandler::FileHandler() {}
|
FileHandler::FileHandler() {}
|
||||||
|
|||||||
@ -3,10 +3,12 @@
|
|||||||
|
|
||||||
class QJsonDocument;
|
class QJsonDocument;
|
||||||
class QString;
|
class QString;
|
||||||
|
class QByteArray;
|
||||||
|
|
||||||
class FileHandler {
|
class FileHandler {
|
||||||
public:
|
public:
|
||||||
static bool saveToFile(const QJsonDocument& doc, const QString& fileName);
|
static bool saveToFile(const QJsonDocument& doc, const QString& fileName);
|
||||||
|
static QByteArray loadJSONDataFromFile(const QString fileName);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit FileHandler();
|
explicit FileHandler();
|
||||||
|
|||||||
@ -51,7 +51,7 @@ QJsonArray JsonParser::extractItemArray(const QByteArray& jsonData, const QStrin
|
|||||||
|
|
||||||
} else {
|
} else {
|
||||||
QJsonObject rootObject = doc.object();
|
QJsonObject rootObject = doc.object();
|
||||||
itemArray = rootObject.value(QString("items")).toArray();
|
itemArray = rootObject.value(objectName).toArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
return itemArray;
|
return itemArray;
|
||||||
|
|||||||
@ -90,6 +90,10 @@ 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; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Save items to default file (in standard location).
|
||||||
|
* @brief GenericCore::saveItems Saves item fo file.
|
||||||
|
*/
|
||||||
void GenericCore::saveItems() {
|
void GenericCore::saveItems() {
|
||||||
qDebug() << "saving items...";
|
qDebug() << "saving items...";
|
||||||
|
|
||||||
@ -108,6 +112,29 @@ void GenericCore::saveItems() {
|
|||||||
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
|
||||||
|
initModelData();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializing model with data. Tries to read items from default file. Generating example items as
|
||||||
|
* fallback.
|
||||||
|
* @brief GenericCore::initModelData
|
||||||
|
*/
|
||||||
|
void GenericCore::initModelData() {
|
||||||
|
qInfo() << "Trying to read model data from file...";
|
||||||
|
const QByteArray jsonDoc = FileHandler::loadJSONDataFromFile("items.json");
|
||||||
|
// qDebug() << "jsonDoc:" << jsonDoc;
|
||||||
|
// TODO decide on lack of file(s) (config, data) if example items should be generated
|
||||||
|
// (see welcome wizard)
|
||||||
|
if (jsonDoc.isEmpty()) {
|
||||||
|
qDebug() << "No item content in file. Generating example items...";
|
||||||
|
const QByteArray exampleItems = m_mainModel->generateExampleItems();
|
||||||
|
m_mainModel->insertItems(0, exampleItems);
|
||||||
|
} else {
|
||||||
|
qDebug() << "Item in file found.";
|
||||||
|
m_mainModel->insertItems(0, jsonDoc);
|
||||||
|
}
|
||||||
|
m_modelUndoStack->clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
QString GenericCore::getMaintenanceToolFilePath() const {
|
QString GenericCore::getMaintenanceToolFilePath() const {
|
||||||
|
|||||||
@ -35,6 +35,7 @@ class GenericCore : public QObject {
|
|||||||
std::shared_ptr<TableModel> m_mainModel;
|
std::shared_ptr<TableModel> m_mainModel;
|
||||||
|
|
||||||
void setupModels();
|
void setupModels();
|
||||||
|
void initModelData();
|
||||||
|
|
||||||
QString getMaintenanceToolFilePath() const;
|
QString getMaintenanceToolFilePath() const;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -17,21 +17,35 @@ QHash<int, QByteArray> TableModel::ROLE_NAMES = {{NameRole, "Name"},
|
|||||||
{FactorRole, "Factor"}};
|
{FactorRole, "Factor"}};
|
||||||
QList<QString> TableModel::intColumns = {"Amount", "Factor"};
|
QList<QString> TableModel::intColumns = {"Amount", "Factor"};
|
||||||
|
|
||||||
|
QByteArray TableModel::generateExampleItems() {
|
||||||
|
QJsonDocument doc = QJsonDocument();
|
||||||
|
QJsonObject rootObject;
|
||||||
|
QJsonArray array;
|
||||||
|
|
||||||
|
for (int row = 0; row < 5; ++row) {
|
||||||
|
QJsonObject itemObject;
|
||||||
|
// itemObject.insert("uuid", m_uuid.toString());
|
||||||
|
// itemObject.insert("entryDateUTC", m_entryDateUTC.toString(Qt::ISODate));
|
||||||
|
itemObject.insert(TableModel::ROLE_NAMES.value(TableModel::NameRole),
|
||||||
|
QString("Item %1").arg(row));
|
||||||
|
itemObject.insert(TableModel::ROLE_NAMES.value(TableModel::DescriptionRole),
|
||||||
|
QString("This is item %1").arg(row));
|
||||||
|
itemObject.insert(TableModel::ROLE_NAMES.value(TableModel::InfoRole),
|
||||||
|
QString("Info of item %1").arg(row));
|
||||||
|
itemObject.insert(TableModel::ROLE_NAMES.value(TableModel::AmountRole), row);
|
||||||
|
itemObject.insert(TableModel::ROLE_NAMES.value(TableModel::FactorRole), row * 1.1);
|
||||||
|
|
||||||
|
array.append(itemObject);
|
||||||
|
}
|
||||||
|
rootObject.insert("items", array);
|
||||||
|
|
||||||
|
doc.setObject(rootObject);
|
||||||
|
return doc.toJson();
|
||||||
|
}
|
||||||
|
|
||||||
TableModel::TableModel(QUndoStack* undoStack, QObject* parent)
|
TableModel::TableModel(QUndoStack* undoStack, QObject* parent)
|
||||||
: QAbstractTableModel{parent}
|
: QAbstractTableModel{parent}
|
||||||
, m_undoStack(undoStack) {
|
, m_undoStack(undoStack) {}
|
||||||
for (int row = 0; row < 5; ++row) {
|
|
||||||
QHash<int, QVariant> values;
|
|
||||||
values[NameRole] = QString("Item %1").arg(row);
|
|
||||||
values[DescriptionRole] = QString("This is item %1").arg(row);
|
|
||||||
values[InfoRole] = QString("Info of item %1").arg(row);
|
|
||||||
values[AmountRole] = row;
|
|
||||||
values[FactorRole] = row * 1.1;
|
|
||||||
|
|
||||||
shared_ptr<ModelItem> item = make_unique<ModelItem>(values);
|
|
||||||
m_items.append(std::move(item));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Qt::ItemFlags TableModel::flags(const QModelIndex& index) const {
|
Qt::ItemFlags TableModel::flags(const QModelIndex& index) const {
|
||||||
return Qt::ItemIsEditable | QAbstractTableModel::flags(index);
|
return Qt::ItemIsEditable | QAbstractTableModel::flags(index);
|
||||||
@ -115,6 +129,21 @@ bool TableModel::setItemData(const QModelIndex& index, const QMap<int, QVariant>
|
|||||||
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;
|
||||||
|
}
|
||||||
|
|
||||||
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!";
|
||||||
@ -147,7 +176,7 @@ void TableModel::insertItems(int startPosition,
|
|||||||
startPosition = m_items.size();
|
startPosition = m_items.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<QHash<int, QVariant>> valueList = JsonParser::toItemValuesList(jsonDoc);
|
QList<QHash<int, QVariant>> valueList = JsonParser::toItemValuesList(jsonDoc, "items");
|
||||||
|
|
||||||
InsertRowsCommand* insertCommand = new InsertRowsCommand(this, startPosition, valueList);
|
InsertRowsCommand* insertCommand = new InsertRowsCommand(this, startPosition, valueList);
|
||||||
m_undoStack->push(insertCommand);
|
m_undoStack->push(insertCommand);
|
||||||
@ -245,18 +274,3 @@ 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;
|
|
||||||
}
|
|
||||||
|
|||||||
@ -19,6 +19,7 @@ class TableModel : public QAbstractTableModel {
|
|||||||
enum UserRoles { NameRole = Qt::UserRole + 1, DescriptionRole, InfoRole, AmountRole, FactorRole };
|
enum UserRoles { NameRole = Qt::UserRole + 1, DescriptionRole, InfoRole, AmountRole, FactorRole };
|
||||||
static QHash<int, QByteArray> ROLE_NAMES;
|
static QHash<int, QByteArray> ROLE_NAMES;
|
||||||
static QList<QString> intColumns;
|
static QList<QString> intColumns;
|
||||||
|
static QByteArray generateExampleItems();
|
||||||
|
|
||||||
explicit TableModel(QUndoStack* undoStack, QObject* parent = nullptr);
|
explicit TableModel(QUndoStack* undoStack, QObject* parent = nullptr);
|
||||||
|
|
||||||
@ -41,7 +42,9 @@ class TableModel : public QAbstractTableModel {
|
|||||||
// override;
|
// override;
|
||||||
bool removeRows(int firstRow, int nRows, const QModelIndex& parentIndex = QModelIndex()) override;
|
bool removeRows(int firstRow, int nRows, const QModelIndex& parentIndex = QModelIndex()) override;
|
||||||
void appendItems(const QByteArray& jsonDoc);
|
void appendItems(const QByteArray& jsonDoc);
|
||||||
void insertItems(int startPosition, const QByteArray& jsonDoc, const QModelIndex& parentIndex);
|
void insertItems(int startPosition,
|
||||||
|
const QByteArray& jsonDoc,
|
||||||
|
const QModelIndex& parentIndex = QModelIndex());
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// *** members ***
|
/// *** members ***
|
||||||
|
|||||||
Reference in New Issue
Block a user