AccountSwitcher: make table sortable

This commit is contained in:
tobtoht 2021-05-26 00:08:32 +02:00
parent afd29c81fc
commit 4dcd34e4bf
No known key found for this signature in database
GPG key ID: 1CADD27F41F45C3C
5 changed files with 61 additions and 4 deletions

View file

@ -6,6 +6,7 @@
#include "libwalletqt/SubaddressAccount.h"
#include "utils/Icons.h"
#include "model/ModelUtils.h"
#include <QMenu>
AccountSwitcherDialog::AccountSwitcherDialog(QSharedPointer<AppContext> ctx, QWidget *parent)
@ -13,15 +14,22 @@ AccountSwitcherDialog::AccountSwitcherDialog(QSharedPointer<AppContext> ctx, QWi
, ui(new Ui::AccountSwitcherDialog)
, m_ctx(std::move(ctx))
, m_model(m_ctx->wallet->subaddressAccountModel())
, m_proxyModel(new SubaddressAccountProxyModel(this))
{
ui->setupUi(this);
ui->accounts->setContextMenuPolicy(Qt::CustomContextMenu);
m_ctx->wallet->subaddressAccount()->refresh();
ui->accounts->setModel(m_ctx->wallet->subaddressAccountModel());
m_proxyModel->setSourceModel(m_model);
ui->label_totalBalance->setFont(ModelUtils::getMonospaceFont());
ui->label_totalBalance->setText(WalletManager::displayAmount(m_ctx->wallet->balanceAll()));
ui->accounts->setModel(m_proxyModel);
ui->accounts->setContextMenuPolicy(Qt::CustomContextMenu);
ui->accounts->setSelectionMode(QAbstractItemView::SingleSelection);
ui->accounts->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->accounts->setSortingEnabled(true);
ui->accounts->sortByColumn(SubaddressAccountModel::Column::Number, Qt::AscendingOrder);
ui->accounts->hideColumn(SubaddressAccountModel::Column::Address);
ui->accounts->hideColumn(SubaddressAccountModel::Column::UnlockedBalance);
ui->accounts->setColumnWidth(SubaddressAccountModel::Column::Label, 200);

View file

@ -36,6 +36,7 @@ private:
Ui::AccountSwitcherDialog *ui;
QSharedPointer<AppContext> m_ctx;
SubaddressAccountModel *m_model;
SubaddressAccountProxyModel *m_proxyModel;
};
#endif //FEATHER_ACCOUNTSWITCHERDIALOG_H

View file

@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>521</width>
<height>275</height>
<height>347</height>
</rect>
</property>
<property name="windowTitle">
@ -27,6 +27,33 @@
</attribute>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Total balance:</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_totalBalance">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>TextLabel</string>
</property>
<property name="textInteractionFlags">
<set>Qt::LinksAccessibleByMouse|Qt::TextSelectableByMouse</set>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>

View file

@ -5,6 +5,8 @@
#include "SubaddressAccount.h"
#include <QDebug>
#include <QFont>
#include "ModelUtils.h"
SubaddressAccountModel::SubaddressAccountModel(QObject *parent, SubaddressAccount *subaddressAccount)
: QAbstractTableModel(parent)
@ -50,6 +52,11 @@ QVariant SubaddressAccountModel::data(const QModelIndex &index, int role) const
if (role == Qt::DisplayRole || role == Qt::EditRole || role == Qt::UserRole) {
result = parseSubaddressAccountRow(row, index, role);
}
else if (role == Qt::FontRole) {
if (index.column() == Column::Balance || index.column() == Column::UnlockedBalance) {
result = ModelUtils::getMonospaceFont();
}
}
});
if (!found) {
@ -135,4 +142,9 @@ Qt::ItemFlags SubaddressAccountModel::flags(const QModelIndex &index) const
Monero::SubaddressAccountRow* SubaddressAccountModel::entryFromIndex(const QModelIndex &index) const {
Q_ASSERT(index.isValid() && index.row() < m_subaddressAccount->count());
return m_subaddressAccount->row(index.row());
}
SubaddressAccountProxyModel::SubaddressAccountProxyModel(QObject *parent)
: QSortFilterProxyModel(parent)
{
}

View file

@ -6,6 +6,7 @@
#include "Subaddress.h"
#include <QAbstractTableModel>
#include <QSortFilterProxyModel>
class SubaddressAccount;
@ -45,4 +46,12 @@ private:
SubaddressAccount *m_subaddressAccount;
};
class SubaddressAccountProxyModel : public QSortFilterProxyModel
{
Q_OBJECT
public:
explicit SubaddressAccountProxyModel(QObject *parent = nullptr);
};
#endif // SUBADDRESSACCOUNTMODEL_H