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:
@ -17,21 +17,35 @@ QHash<int, QByteArray> TableModel::ROLE_NAMES = {{NameRole, "Name"},
|
||||
{FactorRole, "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)
|
||||
: QAbstractTableModel{parent}
|
||||
, 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));
|
||||
}
|
||||
}
|
||||
, m_undoStack(undoStack) {}
|
||||
|
||||
Qt::ItemFlags TableModel::flags(const QModelIndex& index) const {
|
||||
return Qt::ItemIsEditable | QAbstractTableModel::flags(index);
|
||||
@ -115,6 +129,21 @@ bool TableModel::setItemData(const QModelIndex& index, const QMap<int, QVariant>
|
||||
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) {
|
||||
if (parentIndex != QModelIndex()) {
|
||||
qWarning() << "Removing of child rows is not supported yet!";
|
||||
@ -147,7 +176,7 @@ void TableModel::insertItems(int startPosition,
|
||||
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);
|
||||
m_undoStack->push(insertCommand);
|
||||
@ -245,18 +274,3 @@ bool TableModel::isEmptyValueEqualToZero(const int role) const {
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user