feather/src/model/SubaddressAccountModel.cpp

166 lines
4.8 KiB
C++
Raw Normal View History

// SPDX-License-Identifier: BSD-3-Clause
2023-01-02 19:30:11 +00:00
// SPDX-FileCopyrightText: 2020-2023 The Monero Project
#include "SubaddressAccountModel.h"
#include "SubaddressAccount.h"
2021-05-25 22:08:32 +00:00
#include <QFont>
2023-03-28 20:42:12 +00:00
#include "utils/Utils.h"
2023-06-13 10:39:23 +00:00
#include "libwalletqt/WalletManager.h"
#include "rows/AccountRow.h"
2023-06-13 10:39:23 +00:00
SubaddressAccountModel::SubaddressAccountModel(QObject *parent, SubaddressAccount *subaddressAccount)
2021-05-22 13:42:26 +00:00
: QAbstractTableModel(parent)
, m_subaddressAccount(subaddressAccount)
{
connect(m_subaddressAccount, &SubaddressAccount::refreshStarted, this, &SubaddressAccountModel::startReset);
connect(m_subaddressAccount, &SubaddressAccount::refreshFinished, this, &SubaddressAccountModel::endReset);
}
void SubaddressAccountModel::startReset(){
beginResetModel();
}
2021-05-22 13:42:26 +00:00
void SubaddressAccountModel::endReset(){
endResetModel();
}
2021-05-22 13:42:26 +00:00
int SubaddressAccountModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid()) {
return 0;
} else {
return m_subaddressAccount->count();
}
}
int SubaddressAccountModel::columnCount(const QModelIndex &parent) const
{
2021-05-22 13:42:26 +00:00
if (parent.isValid()) {
return 0;
}
return Column::COUNT;
}
QVariant SubaddressAccountModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() < 0 || static_cast<quint64>(index.row()) >= m_subaddressAccount->count())
return {};
QVariant result;
bool found = m_subaddressAccount->getRow(index.row(), [this, &index, &result, &role](const AccountRow &row) {
2021-05-22 13:42:26 +00:00
if (role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::UserRole) {
result = parseSubaddressAccountRow(row, index, role);
}
2021-05-25 22:08:32 +00:00
else if (role == Qt::FontRole) {
if (index.column() == Column::Balance || index.column() == Column::UnlockedBalance) {
2023-03-28 20:42:12 +00:00
result = Utils::getMonospaceFont();
2021-05-25 22:08:32 +00:00
}
}
2021-05-27 01:15:56 +00:00
else if (role == Qt::TextAlignmentRole) {
if (index.column() == Column::Balance || index.column() == Column::UnlockedBalance) {
result = Qt::AlignRight;
}
}
});
2021-05-22 13:42:26 +00:00
if (!found) {
qCritical("%s: internal error: invalid index %d", __FUNCTION__, index.row());
}
return result;
}
QVariant SubaddressAccountModel::parseSubaddressAccountRow(const AccountRow &row,
2021-05-22 13:42:26 +00:00
const QModelIndex &index, int role) const
{
2021-05-22 13:42:26 +00:00
switch (index.column()) {
case Number:
2023-01-17 20:41:02 +00:00
if (role == Qt::UserRole) {
return index.row();
}
2021-05-22 13:42:26 +00:00
return QString("#%1").arg(QString::number(index.row()));
case Address:
return row.getAddress();
2021-05-22 13:42:26 +00:00
case Label:
return row.getLabel();
2021-05-22 13:42:26 +00:00
case Balance:
2023-06-13 10:39:23 +00:00
if (role == Qt::UserRole) {
return WalletManager::amountFromString(row.getBalance());
2023-06-13 10:39:23 +00:00
}
return row.getBalance();
2021-05-22 13:42:26 +00:00
case UnlockedBalance:
2023-06-13 10:39:23 +00:00
if (role == Qt::UserRole) {
return WalletManager::amountFromString(row.getUnlockedBalance());
2023-06-13 10:39:23 +00:00
}
return row.getUnlockedBalance();
2021-05-22 13:42:26 +00:00
default:
return QVariant();
}
}
QVariant SubaddressAccountModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole) {
return QVariant();
}
if (orientation == Qt::Horizontal)
{
2021-05-22 13:42:26 +00:00
switch (section) {
case Address:
return QString("Address");
case Label:
return QString("Label");
case Balance:
return QString("Balance");
case UnlockedBalance:
return QString("Spendable balance");
default:
return QVariant();
}
}
2021-05-22 13:42:26 +00:00
return QVariant();
}
2021-05-22 13:42:26 +00:00
bool SubaddressAccountModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (index.isValid() && role == Qt::EditRole) {
const int row = index.row();
switch (index.column()) {
case Label:
m_subaddressAccount->setLabel(row, value.toString());
break;
default:
return false;
}
emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole});
return true;
}
return false;
}
Qt::ItemFlags SubaddressAccountModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::ItemIsEnabled;
if (index.column() == Label)
return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
return QAbstractTableModel::flags(index);
}
AccountRow* SubaddressAccountModel::entryFromIndex(const QModelIndex &index) const {
2021-05-22 13:42:26 +00:00
return m_subaddressAccount->row(index.row());
2021-05-25 22:08:32 +00:00
}
SubaddressAccountProxyModel::SubaddressAccountProxyModel(QObject *parent)
: QSortFilterProxyModel(parent)
{
2023-01-17 20:41:02 +00:00
setSortRole(Qt::UserRole);
2021-05-22 13:42:26 +00:00
}