2024-01-16 21:12:41 +00:00
|
|
|
//SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
|
|
|
|
//SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <deque>
|
|
|
|
|
|
|
|
#include <QString>
|
2024-01-20 21:17:21 +00:00
|
|
|
#include <QColor>
|
2024-01-16 21:12:41 +00:00
|
|
|
#include <QAbstractListModel>
|
|
|
|
#include <qqmlregistration.h>
|
|
|
|
|
2024-03-29 22:00:17 +00:00
|
|
|
#include "currencies.h"
|
|
|
|
|
2024-01-16 21:12:41 +00:00
|
|
|
namespace Models {
|
|
|
|
struct Asset {
|
2024-04-07 20:07:52 +00:00
|
|
|
using ID = uint32_t;
|
|
|
|
|
|
|
|
ID id;
|
2024-01-16 21:12:41 +00:00
|
|
|
QString title;
|
|
|
|
QString icon;
|
2024-01-20 21:17:21 +00:00
|
|
|
QColor color;
|
2024-01-16 21:12:41 +00:00
|
|
|
double balance;
|
|
|
|
bool archived;
|
|
|
|
unsigned int currency;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Assets : public QAbstractListModel {
|
|
|
|
Q_OBJECT
|
|
|
|
QML_ELEMENT
|
|
|
|
|
|
|
|
public:
|
2024-03-29 22:00:17 +00:00
|
|
|
explicit Assets (Currencies& currencies, QObject* parent = nullptr);
|
2024-01-16 21:12:41 +00:00
|
|
|
|
|
|
|
enum Roles {
|
|
|
|
Title = Qt::UserRole + 1,
|
|
|
|
Icon,
|
|
|
|
Balance,
|
2024-01-20 21:17:21 +00:00
|
|
|
Archived,
|
2024-01-21 19:22:56 +00:00
|
|
|
Color,
|
2024-03-29 22:00:17 +00:00
|
|
|
Currency,
|
2024-04-07 20:07:52 +00:00
|
|
|
CurrencyID,
|
|
|
|
ID
|
2024-01-16 21:12:41 +00:00
|
|
|
};
|
|
|
|
|
2024-01-24 21:00:15 +00:00
|
|
|
void clear ();
|
|
|
|
void add (const Asset& asset);
|
|
|
|
void add (const std::deque<Asset>& assets);
|
2024-04-07 20:07:52 +00:00
|
|
|
void remove (Asset::ID id);
|
|
|
|
|
|
|
|
Asset get(Asset::ID id) const;
|
2024-01-16 21:12:41 +00:00
|
|
|
|
|
|
|
//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;
|
|
|
|
|
2024-01-24 21:00:15 +00:00
|
|
|
static bool deserialize (const QVariantList& from, std::deque<Asset>& out);
|
|
|
|
static const QHash<int, QByteArray> roles;
|
2024-01-16 21:12:41 +00:00
|
|
|
|
2024-04-07 20:07:52 +00:00
|
|
|
Q_INVOKABLE int getIndexByID (Asset::ID id) const;
|
|
|
|
Q_INVOKABLE QVariantMap getAssetByIndex(int index) const;
|
|
|
|
Q_INVOKABLE QVariantMap getAssetByID(Asset::ID id) const;
|
|
|
|
|
2024-01-16 21:12:41 +00:00
|
|
|
signals:
|
2024-01-24 21:00:15 +00:00
|
|
|
void requestAssets ();
|
2024-01-16 21:12:41 +00:00
|
|
|
|
|
|
|
public slots:
|
2024-01-24 21:00:15 +00:00
|
|
|
void receivedAssets (const std::deque<Asset>& assets);
|
2024-01-16 21:12:41 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
enum class State {
|
|
|
|
initial,
|
|
|
|
requesting,
|
|
|
|
syncronized
|
|
|
|
};
|
|
|
|
|
|
|
|
State state;
|
|
|
|
std::deque<Asset> records;
|
2024-03-29 22:00:17 +00:00
|
|
|
Currencies& currencies;
|
2024-01-16 21:12:41 +00:00
|
|
|
};
|
|
|
|
}
|