Skip to content

Commit

Permalink
gui: Extend address book filter for nested filtering
Browse files Browse the repository at this point in the history
Extend AddressBookFilterProxyModel to allow using nested filters to be applied
on top of it. If future needs arise for similar filters outside the address book page,
the code could be easily refactored and moved in a new subclass of QSortFilterProxyModel,
perhaps with limits on nested levels.

For safety and performance reasons, the code of the filter proxy model class declaration
(in addressbookpage.h) and its instance creation were updated by aligning it with
TransactionFilterProxy in overviewpage.h as this addresses situations of unexpected crashes,
such as segfault on closing the app, double free or corruption, or stack smashing detection.
  • Loading branch information
pablomartin4btc committed Sep 11, 2023
1 parent 1fcff19 commit 78558de
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
29 changes: 24 additions & 5 deletions src/qt/addressbookpage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,31 @@
class AddressBookSortFilterProxyModel final : public QSortFilterProxyModel
{
const QString m_type;
const bool m_nestedFilterEnabled;

public:
AddressBookSortFilterProxyModel(const QString& type, QObject* parent)
AddressBookSortFilterProxyModel(const QString& type, QObject* parent, bool enableNestedFilter)
: QSortFilterProxyModel(parent)
, m_type(type)
, m_nestedFilterEnabled(enableNestedFilter)
{
setDynamicSortFilter(true);
setFilterCaseSensitivity(Qt::CaseInsensitive);
setSortCaseSensitivity(Qt::CaseInsensitive);

if (m_nestedFilterEnabled) {
nextedFilterProxyModel.reset(new AddressBookSortFilterProxyModel(type, this, false));
nextedFilterProxyModel->setSourceModel(this);
}
}

AddressBookSortFilterProxyModel* nestedProxyModel() const noexcept{
if (!m_nestedFilterEnabled) return const_cast<AddressBookSortFilterProxyModel*>(this);
return nextedFilterProxyModel.get();
}

bool isNestedFilterEnabled() const {
return m_nestedFilterEnabled;
}

protected:
Expand Down Expand Up @@ -66,6 +82,9 @@ class AddressBookSortFilterProxyModel final : public QSortFilterProxyModel

return filterBy;
}

private:
std::unique_ptr<AddressBookSortFilterProxyModel> nextedFilterProxyModel{nullptr};
};

AddressBookPage::AddressBookPage(const PlatformStyle *platformStyle, Mode _mode, Tabs _tab, QWidget *parent) :
Expand Down Expand Up @@ -154,12 +173,12 @@ void AddressBookPage::setModel(AddressTableModel *_model)
return;

auto type = tab == ReceivingTab ? AddressTableModel::Receive : AddressTableModel::Send;
proxyModel = new AddressBookSortFilterProxyModel(type, this);
proxyModel.reset(new AddressBookSortFilterProxyModel(type, this, false));
proxyModel->setSourceModel(_model);

connect(ui->searchLineEdit, &QLineEdit::textChanged, proxyModel, &QSortFilterProxyModel::setFilterWildcard);
connect(ui->searchLineEdit, &QLineEdit::textChanged, proxyModel.get(), &QSortFilterProxyModel::setFilterWildcard);

ui->tableView->setModel(proxyModel);
ui->tableView->setModel(proxyModel->nestedProxyModel());
ui->tableView->sortByColumn(0, Qt::AscendingOrder);

// Set column widths
Expand Down Expand Up @@ -308,7 +327,7 @@ void AddressBookPage::on_exportButton_clicked()
CSVModelWriter writer(filename);

// name, column, role
writer.setModel(proxyModel);
writer.setModel(proxyModel.get());
writer.addColumn("Label", AddressTableModel::Label, Qt::EditRole);
writer.addColumn("Address Type", AddressTableModel::Type, Qt::EditRole);
writer.addColumn("Address", AddressTableModel::Address, Qt::EditRole);
Expand Down
2 changes: 1 addition & 1 deletion src/qt/addressbookpage.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Q_SLOTS:
Mode mode;
Tabs tab;
QString returnValue;
AddressBookSortFilterProxyModel *proxyModel;
std::unique_ptr<AddressBookSortFilterProxyModel> proxyModel{nullptr};
QMenu *contextMenu;
QString newAddressToSelect;

Expand Down

0 comments on commit 78558de

Please sign in to comment.