From ca58cb01982df55670b1979e19baf6b68aa9b187 Mon Sep 17 00:00:00 2001 From: pablomartin4btc Date: Sat, 9 Sep 2023 23:18:03 -0300 Subject: [PATCH] gui: Extend address book filter for nested filtering 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. --- src/qt/addressbookpage.cpp | 29 ++++++++++++++++++++++++----- src/qt/addressbookpage.h | 2 +- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/qt/addressbookpage.cpp b/src/qt/addressbookpage.cpp index da47befd530..07492ad12b1 100644 --- a/src/qt/addressbookpage.cpp +++ b/src/qt/addressbookpage.cpp @@ -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(this); + return nextedFilterProxyModel.get(); + } + + bool isNestedFilterEnabled() const { + return m_nestedFilterEnabled; } protected: @@ -66,6 +82,9 @@ class AddressBookSortFilterProxyModel final : public QSortFilterProxyModel return filterBy; } + +private: + std::unique_ptr nextedFilterProxyModel{nullptr}; }; AddressBookPage::AddressBookPage(const PlatformStyle *platformStyle, Mode _mode, Tabs _tab, QWidget *parent) : @@ -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 @@ -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); diff --git a/src/qt/addressbookpage.h b/src/qt/addressbookpage.h index 283209d00c7..fe7095118bd 100644 --- a/src/qt/addressbookpage.h +++ b/src/qt/addressbookpage.h @@ -53,7 +53,7 @@ public Q_SLOTS: Mode mode; Tabs tab; QString returnValue; - AddressBookSortFilterProxyModel *proxyModel; + std::unique_ptr proxyModel{nullptr}; QMenu *contextMenu; QString newAddressToSelect;