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>
|
|
|
|
|
|
|
|
namespace Models {
|
|
|
|
struct Asset {
|
|
|
|
unsigned int id;
|
|
|
|
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:
|
|
|
|
explicit Assets (QObject* parent = nullptr);
|
|
|
|
|
|
|
|
enum Roles {
|
|
|
|
Title = Qt::UserRole + 1,
|
|
|
|
Icon,
|
|
|
|
Balance,
|
2024-01-20 21:17:21 +00:00
|
|
|
Archived,
|
|
|
|
Color
|
2024-01-16 21:12:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void clear();
|
|
|
|
void addAsset(const Asset& asset);
|
|
|
|
void addAssets(const std::deque<Asset>& assets);
|
|
|
|
void deleteAsset(unsigned int id);
|
|
|
|
|
|
|
|
//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);
|
|
|
|
|
|
|
|
signals:
|
|
|
|
void requestAssets();
|
|
|
|
|
|
|
|
public slots:
|
|
|
|
void receivedAssets(const std::deque<Asset>& assets);
|
|
|
|
|
|
|
|
private:
|
|
|
|
QModelIndex getIndex(unsigned int id) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum class State {
|
|
|
|
initial,
|
|
|
|
requesting,
|
|
|
|
syncronized
|
|
|
|
};
|
|
|
|
|
|
|
|
State state;
|
|
|
|
std::deque<Asset> records;
|
|
|
|
};
|
|
|
|
}
|