2024-01-16 21:12:41 +00:00
|
|
|
//SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
|
|
|
|
//SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
#include "assets.h"
|
|
|
|
|
2024-01-21 19:22:56 +00:00
|
|
|
#include "utils/helpers.h"
|
2024-01-16 21:12:41 +00:00
|
|
|
|
2024-01-24 21:00:15 +00:00
|
|
|
const QHash<int, QByteArray> Models::Assets::roles({
|
2024-04-07 20:07:52 +00:00
|
|
|
{Title, "title"}, {Icon, "icon"}, {Balance, "balance"}, {Archived, "archived"}, {Color, "color"}, {Currency, "currency"}, {CurrencyID, "currencyID"}, {ID, "assetID"}
|
2024-01-24 21:00:15 +00:00
|
|
|
});
|
|
|
|
|
2024-03-29 22:00:17 +00:00
|
|
|
Models::Assets::Assets (Currencies& currencies, QObject* parent):
|
2024-01-16 21:12:41 +00:00
|
|
|
QAbstractListModel(parent),
|
|
|
|
records(),
|
2024-03-29 22:00:17 +00:00
|
|
|
state(State::initial),
|
|
|
|
currencies(currencies)
|
2024-01-16 21:12:41 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
void Models::Assets::clear () {
|
|
|
|
beginResetModel();
|
|
|
|
records.clear();
|
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
2024-01-24 21:00:15 +00:00
|
|
|
void Models::Assets::add (const Asset& asset) {
|
2024-04-07 20:07:52 +00:00
|
|
|
int index = getIndexByID(asset.id);
|
|
|
|
if (index != -1)
|
2024-01-16 21:12:41 +00:00
|
|
|
throw std::runtime_error("An attempt to insert a duplicating Asset to an asset model");
|
|
|
|
|
2024-01-20 21:17:21 +00:00
|
|
|
beginInsertRows(QModelIndex(), records.size(), records.size());
|
2024-01-16 21:12:41 +00:00
|
|
|
records.push_back(asset);
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
2024-01-24 21:00:15 +00:00
|
|
|
void Models::Assets::add (const std::deque<Asset>& assets) {
|
2024-01-16 21:12:41 +00:00
|
|
|
if (assets.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (const Asset& asset : assets)
|
2024-04-07 20:07:52 +00:00
|
|
|
if (getIndexByID(asset.id) != -1)
|
2024-01-16 21:12:41 +00:00
|
|
|
throw std::runtime_error("An attempt to insert a duplicating Asset to an asset model (bulk)");
|
|
|
|
|
2024-01-20 21:17:21 +00:00
|
|
|
beginInsertRows(QModelIndex(), records.size(), records.size() + assets.size() - 1);
|
2024-01-16 21:12:41 +00:00
|
|
|
for (const Asset& asset : assets)
|
|
|
|
records.push_back(asset);
|
|
|
|
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
2024-04-07 20:07:52 +00:00
|
|
|
void Models::Assets::remove (Asset::ID id) {
|
|
|
|
int index = getIndexByID(id);
|
|
|
|
if (index == -1)
|
2024-01-21 19:22:56 +00:00
|
|
|
throw std::runtime_error("An attempt to delete non existing Asset from asset model");
|
2024-01-16 21:12:41 +00:00
|
|
|
|
2024-04-07 20:07:52 +00:00
|
|
|
beginRemoveRows(QModelIndex(), index, index);
|
|
|
|
records.erase(records.begin() + index);
|
2024-01-16 21:12:41 +00:00
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
|
2024-04-07 20:07:52 +00:00
|
|
|
Models::Asset Models::Assets::get (Asset::ID id) const {
|
|
|
|
int index = getIndexByID(id);
|
|
|
|
if (index == -1)
|
|
|
|
throw std::runtime_error("An attempt to access non existing Asset from asset model");
|
|
|
|
|
|
|
|
return records[index];
|
|
|
|
}
|
|
|
|
|
2024-01-16 21:12:41 +00:00
|
|
|
int Models::Assets::rowCount (const QModelIndex& parent) const {
|
|
|
|
//For list models only the root node (an invalid parent) should return the
|
|
|
|
//list's size. For all other (valid) parents, rowCount() should return 0 so
|
|
|
|
//that it does not become a tree model.
|
|
|
|
if (parent.isValid())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return records.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray> Models::Assets::roleNames () const {
|
2024-01-24 21:00:15 +00:00
|
|
|
return roles;
|
2024-01-16 21:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Models::Assets::canFetchMore (const QModelIndex& parent) const {
|
|
|
|
return state == State::initial;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Assets::fetchMore (const QModelIndex& parent) {
|
|
|
|
if (state != State::initial)
|
|
|
|
return;
|
|
|
|
|
|
|
|
state = State::requesting;
|
2024-01-18 22:14:33 +00:00
|
|
|
emit requestAssets();
|
2024-01-16 21:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant Models::Assets::data (const QModelIndex& index, int role) const {
|
|
|
|
if (!index.isValid())
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
int row = index.row();
|
|
|
|
if (row >= 0 && row < records.size()) {
|
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
case Title:
|
|
|
|
return records[row].title;
|
|
|
|
case Icon:
|
|
|
|
return records[row].icon;
|
|
|
|
case Balance:
|
|
|
|
return records[row].balance;
|
|
|
|
case Archived:
|
|
|
|
return records[row].archived;
|
2024-01-20 21:17:21 +00:00
|
|
|
case Color:
|
|
|
|
return records[row].color;
|
2024-03-29 22:00:17 +00:00
|
|
|
case Currency:
|
|
|
|
return currencies.getCode(records[row].currency);
|
2024-04-07 20:07:52 +00:00
|
|
|
case CurrencyID:
|
|
|
|
return records[row].currency;
|
|
|
|
case ID:
|
2024-01-21 19:22:56 +00:00
|
|
|
return records[row].id;
|
2024-01-16 21:12:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Models::Assets::deserialize (const QVariantList& from, std::deque<Asset>& out) {
|
|
|
|
for (const QVariant& item : from) {
|
|
|
|
if (!item.canConvert<QVariantMap>())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const QVariantMap& ser = qast<QVariantMap>(item);
|
|
|
|
Asset& asset = out.emplace_back();
|
|
|
|
asset.title = ser.value("title").toString();
|
|
|
|
asset.icon = ser.value("icon").toString();
|
|
|
|
asset.archived = ser.value("archived").toBool();
|
2024-01-18 22:14:33 +00:00
|
|
|
asset.id = ser.value("id").toUInt();
|
2024-03-29 22:00:17 +00:00
|
|
|
asset.currency = ser.value("currency").toUInt();
|
2024-01-20 21:17:21 +00:00
|
|
|
|
|
|
|
uint32_t color = ser.value("color").toUInt();
|
2024-01-21 19:22:56 +00:00
|
|
|
asset.color = QColor::fromRgba(color);
|
2024-01-16 21:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Models::Assets::receivedAssets (const std::deque<Asset>& assets) {
|
|
|
|
beginResetModel();
|
|
|
|
records = assets;
|
|
|
|
state = State::syncronized;
|
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
2024-04-07 20:07:52 +00:00
|
|
|
int Models::Assets::getIndexByID (Asset::ID id) const {
|
2024-01-16 21:12:41 +00:00
|
|
|
for (std::size_t i = 0; i < records.size(); ++i) {
|
|
|
|
if (records[i].id == id)
|
2024-04-07 20:07:52 +00:00
|
|
|
return i;
|
2024-01-16 21:12:41 +00:00
|
|
|
}
|
2024-01-20 21:17:21 +00:00
|
|
|
|
2024-04-07 20:07:52 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariantMap Models::Assets::getAssetByIndex (int index) const {
|
|
|
|
QVariantMap result;
|
|
|
|
if (index < 0 || index >= records.size())
|
|
|
|
return result;
|
|
|
|
|
|
|
|
QModelIndex idx = createIndex(index, 0, &records[index]);
|
|
|
|
for (int role = Roles::Title; role != Roles::ID; ++role)
|
|
|
|
result[roles[role]] = data(idx, role);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariantMap Models::Assets::getAssetByID (Asset::ID id) const {
|
|
|
|
return getAssetByIndex(getIndexByID(id));
|
2024-01-16 21:12:41 +00:00
|
|
|
}
|