Skip to content

Commit 3facd07

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 c72c424 commit 3facd07

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

src/qt/addressbookpage.cpp

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,37 @@
2424
class AddressBookSortFilterProxyModel final : public QSortFilterProxyModel
2525
{
2626
const QString m_type;
27+
const bool m_nestedFilterEnabled;
2728

2829
public:
29-
AddressBookSortFilterProxyModel(const QString& type, QObject* parent)
30+
AddressBookSortFilterProxyModel(const QString& type, QObject* parent, bool enableNestedFilter)
3031
: QSortFilterProxyModel(parent)
3132
, m_type(type)
33+
, m_nestedFilterEnabled(enableNestedFilter)
3234
{
3335
setDynamicSortFilter(true);
3436
setFilterCaseSensitivity(Qt::CaseInsensitive);
3537
setSortCaseSensitivity(Qt::CaseInsensitive);
3638
}
3739

40+
AddressBookSortFilterProxyModel* nestedProxyModel() const noexcept {
41+
if (!m_nestedFilterEnabled) return const_cast<AddressBookSortFilterProxyModel*>(this);
42+
43+
// Lazily create the nested proxy model
44+
if (!nextedFilterProxyModel) {
45+
auto nextedFilterProxyModel = std::make_unique<AddressBookSortFilterProxyModel>(
46+
m_type,
47+
const_cast<AddressBookSortFilterProxyModel*>(this),
48+
false);
49+
nextedFilterProxyModel->setSourceModel(const_cast<AddressBookSortFilterProxyModel*>(this));
50+
}
51+
return nextedFilterProxyModel.get();
52+
}
53+
54+
bool isNestedFilterEnabled() const {
55+
return m_nestedFilterEnabled;
56+
}
57+
3858
protected:
3959
bool filterAcceptsRow(int row, const QModelIndex& parent) const override
4060
{
@@ -62,6 +82,9 @@ class AddressBookSortFilterProxyModel final : public QSortFilterProxyModel
6282

6383
return filterBy;
6484
}
85+
86+
private:
87+
std::unique_ptr<AddressBookSortFilterProxyModel> nextedFilterProxyModel{nullptr};
6588
};
6689

6790
AddressBookPage::AddressBookPage(const PlatformStyle *platformStyle, Mode _mode, Tabs _tab, QWidget *parent) :
@@ -140,12 +163,12 @@ void AddressBookPage::setModel(AddressTableModel *_model)
140163
return;
141164

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

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

148-
ui->tableView->setModel(proxyModel);
171+
ui->tableView->setModel(proxyModel->nestedProxyModel());
149172
ui->tableView->sortByColumn(0, Qt::AscendingOrder);
150173

151174
// Set column widths
@@ -295,7 +318,7 @@ void AddressBookPage::on_exportButton_clicked()
295318
CSVModelWriter writer(filename);
296319

297320
// name, column, role
298-
writer.setModel(proxyModel);
321+
writer.setModel(proxyModel.get());
299322
writer.addColumn("Label", AddressTableModel::Label, Qt::EditRole);
300323
writer.addColumn("Address Type", AddressTableModel::Type, Qt::EditRole);
301324
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)