Skip to content

Commit 5d597c0

Browse files
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.
1 parent 7be6b79 commit 5d597c0

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/qt/addressbookpage.cpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,31 @@
2828
class AddressBookSortFilterProxyModel final : public QSortFilterProxyModel
2929
{
3030
const QString m_type;
31+
const bool m_nestedFilterEnabled;
3132

3233
public:
33-
AddressBookSortFilterProxyModel(const QString& type, QObject* parent)
34+
AddressBookSortFilterProxyModel(const QString& type, QObject* parent, bool enableNestedFilter)
3435
: QSortFilterProxyModel(parent)
3536
, m_type(type)
37+
, m_nestedFilterEnabled(enableNestedFilter)
3638
{
3739
setDynamicSortFilter(true);
3840
setFilterCaseSensitivity(Qt::CaseInsensitive);
3941
setSortCaseSensitivity(Qt::CaseInsensitive);
42+
43+
if (m_nestedFilterEnabled) {
44+
nextedFilterProxyModel.reset(new AddressBookSortFilterProxyModel(type, this, false));
45+
nextedFilterProxyModel->setSourceModel(this);
46+
}
47+
}
48+
49+
AddressBookSortFilterProxyModel* nestedProxyModel() const noexcept{
50+
if (!m_nestedFilterEnabled) return const_cast<AddressBookSortFilterProxyModel*>(this);
51+
return nextedFilterProxyModel.get();
52+
}
53+
54+
bool isNestedFilterEnabled() const {
55+
return m_nestedFilterEnabled;
4056
}
4157

4258
protected:
@@ -66,6 +82,9 @@ class AddressBookSortFilterProxyModel final : public QSortFilterProxyModel
6682

6783
return filterBy;
6884
}
85+
86+
private:
87+
std::unique_ptr<AddressBookSortFilterProxyModel> nextedFilterProxyModel{nullptr};
6988
};
7089

7190
AddressBookPage::AddressBookPage(const PlatformStyle *platformStyle, Mode _mode, Tabs _tab, QWidget *parent) :
@@ -144,12 +163,12 @@ void AddressBookPage::setModel(AddressTableModel *_model)
144163
return;
145164

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

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

152-
ui->tableView->setModel(proxyModel);
171+
ui->tableView->setModel(proxyModel->nestedProxyModel());
153172
ui->tableView->sortByColumn(0, Qt::AscendingOrder);
154173

155174
// Set column widths
@@ -299,7 +318,7 @@ void AddressBookPage::on_exportButton_clicked()
299318
CSVModelWriter writer(filename);
300319

301320
// name, column, role
302-
writer.setModel(proxyModel);
321+
writer.setModel(proxyModel.get());
303322
writer.addColumn("Label", AddressTableModel::Label, Qt::EditRole);
304323
writer.addColumn("Address Type", AddressTableModel::Type, Qt::EditRole);
305324
writer.addColumn("Address", AddressTableModel::Address, Qt::EditRole);

src/qt/addressbookpage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public Q_SLOTS:
5353
Mode mode;
5454
Tabs tab;
5555
QString returnValue;
56-
AddressBookSortFilterProxyModel *proxyModel;
56+
std::unique_ptr<AddressBookSortFilterProxyModel> proxyModel{nullptr};
5757
QMenu *contextMenu;
5858
QString newAddressToSelect;
5959
void updateWindowsTitleWithWalletName();

0 commit comments

Comments
 (0)