some experiments around therems, a request to update asset

This commit is contained in:
Blue 2024-04-07 17:07:52 -03:00
parent 374551d2bb
commit aa815a5bd7
Signed by: blue
GPG key ID: 9B203B252A63EE38
14 changed files with 229 additions and 59 deletions

View file

@ -6,7 +6,7 @@
#include "utils/helpers.h"
const QHash<int, QByteArray> Models::Assets::roles({
{Title, "title"}, {Icon, "icon"}, {Balance, "balance"}, {Archived, "archived"}, {Color, "color"}, {Currency, "currency"}, {Id, "assetId"}
{Title, "title"}, {Icon, "icon"}, {Balance, "balance"}, {Archived, "archived"}, {Color, "color"}, {Currency, "currency"}, {CurrencyID, "currencyID"}, {ID, "assetID"}
});
Models::Assets::Assets (Currencies& currencies, QObject* parent):
@ -23,8 +23,8 @@ void Models::Assets::clear () {
}
void Models::Assets::add (const Asset& asset) {
QModelIndex index = getIndex(asset.id);
if (index.isValid())
int index = getIndexByID(asset.id);
if (index != -1)
throw std::runtime_error("An attempt to insert a duplicating Asset to an asset model");
beginInsertRows(QModelIndex(), records.size(), records.size());
@ -37,7 +37,7 @@ void Models::Assets::add (const std::deque<Asset>& assets) {
return;
for (const Asset& asset : assets)
if (getIndex(asset.id).isValid())
if (getIndexByID(asset.id) != -1)
throw std::runtime_error("An attempt to insert a duplicating Asset to an asset model (bulk)");
beginInsertRows(QModelIndex(), records.size(), records.size() + assets.size() - 1);
@ -47,17 +47,24 @@ void Models::Assets::add (const std::deque<Asset>& assets) {
endInsertRows();
}
void Models::Assets::remove (unsigned int id) {
QModelIndex index = getIndex(id);
if (!index.isValid())
void Models::Assets::remove (Asset::ID id) {
int index = getIndexByID(id);
if (index == -1)
throw std::runtime_error("An attempt to delete non existing Asset from asset model");
int row = index.row();
beginRemoveRows(QModelIndex(), row, row);
records.erase(records.begin() + row);
beginRemoveRows(QModelIndex(), index, index);
records.erase(records.begin() + index);
endRemoveRows();
}
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];
}
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
@ -104,7 +111,9 @@ QVariant Models::Assets::data (const QModelIndex& index, int role) const {
return records[row].color;
case Currency:
return currencies.getCode(records[row].currency);
case Id:
case CurrencyID:
return records[row].currency;
case ID:
return records[row].id;
}
}
@ -139,11 +148,27 @@ void Models::Assets::receivedAssets (const std::deque<Asset>& assets) {
endResetModel();
}
QModelIndex Models::Assets::getIndex (unsigned int id) const {
int Models::Assets::getIndexByID (Asset::ID id) const {
for (std::size_t i = 0; i < records.size(); ++i) {
if (records[i].id == id)
return createIndex(i, 0, &records[i]);
return i;
}
return QModelIndex();
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));
}

View file

@ -14,7 +14,9 @@
namespace Models {
struct Asset {
unsigned int id;
using ID = uint32_t;
ID id;
QString title;
QString icon;
QColor color;
@ -37,13 +39,16 @@ public:
Archived,
Color,
Currency,
Id
CurrencyID,
ID
};
void clear ();
void add (const Asset& asset);
void add (const std::deque<Asset>& assets);
void remove (unsigned int id);
void remove (Asset::ID id);
Asset get(Asset::ID id) const;
//Basic functionality:
int rowCount (const QModelIndex& parent = QModelIndex()) const override;
@ -57,15 +62,16 @@ public:
static bool deserialize (const QVariantList& from, std::deque<Asset>& out);
static const QHash<int, QByteArray> roles;
Q_INVOKABLE int getIndexByID (Asset::ID id) const;
Q_INVOKABLE QVariantMap getAssetByIndex(int index) const;
Q_INVOKABLE QVariantMap getAssetByID(Asset::ID id) const;
signals:
void requestAssets ();
public slots:
void receivedAssets (const std::deque<Asset>& assets);
private:
QModelIndex getIndex (unsigned int id) const;
private:
enum class State {
initial,