magpie/models/assets.h

89 lines
2.0 KiB
C++

//SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
//SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <deque>
#include <QString>
#include <QColor>
#include <QAbstractListModel>
#include <qqmlregistration.h>
#include "currencies.h"
namespace Models {
struct Asset {
using ID = uint32_t;
ID id;
QString title;
QString icon;
QColor color;
double balance;
bool archived;
unsigned int currency;
};
class Assets : public QAbstractListModel {
Q_OBJECT
QML_ELEMENT
public:
explicit Assets (Currencies& currencies, QObject* parent = nullptr);
enum Roles {
Title = Qt::UserRole + 1,
Icon,
Balance,
Archived,
Color,
Currency,
CurrencyID,
ID
};
void clear ();
void add (const Asset& asset);
void add (const std::deque<Asset>& assets);
void remove (Asset::ID id);
void change (const Asset& asset);
void change (const std::deque<Asset>& assets);
Asset get(Asset::ID id) const;
//Basic functionality:
int rowCount (const QModelIndex& parent = QModelIndex()) const override;
QHash<int, QByteArray> roleNames () const override;
//Fetch data dynamically:
bool canFetchMore (const QModelIndex& parent) const override;
void fetchMore (const QModelIndex& parent) override;
QVariant data (const QModelIndex& index, int role = Qt::DisplayRole) const override;
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:
enum class State {
initial,
requesting,
syncronized
};
State state;
std::deque<Asset> records;
Currencies& currencies;
};
}