Skip to content

Commit d512666

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 d512666

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
@@ -24,15 +24,31 @@
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);
38+
39+
if (m_nestedFilterEnabled) {
40+
nextedFilterProxyModel.reset(new AddressBookSortFilterProxyModel(type, this, false));
41+
nextedFilterProxyModel->setSourceModel(this);
42+
}
43+
}
44+
45+
AddressBookSortFilterProxyModel* nestedProxyModel() const noexcept{
46+
if (!m_nestedFilterEnabled) return const_cast<AddressBookSortFilterProxyModel*>(this);
47+
return nextedFilterProxyModel.get();
48+
}
49+
50+
bool isNestedFilterEnabled() const {
51+
return m_nestedFilterEnabled;
3652
}
3753

3854
protected:
@@ -62,6 +78,9 @@ class AddressBookSortFilterProxyModel final : public QSortFilterProxyModel
6278

6379
return filterBy;
6480
}
81+
82+
private:
83+
std::unique_ptr<AddressBookSortFilterProxyModel> nextedFilterProxyModel{nullptr};
6584
};
6685

6786
AddressBookPage::AddressBookPage(const PlatformStyle *platformStyle, Mode _mode, Tabs _tab, QWidget *parent) :
@@ -140,12 +159,12 @@ void AddressBookPage::setModel(AddressTableModel *_model)
140159
return;
141160

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

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

148-
ui->tableView->setModel(proxyModel);
167+
ui->tableView->setModel(proxyModel->nestedProxyModel());
149168
ui->tableView->sortByColumn(0, Qt::AscendingOrder);
150169

151170
// Set column widths
@@ -295,7 +314,7 @@ void AddressBookPage::on_exportButton_clicked()
295314
CSVModelWriter writer(filename);
296315

297316
// name, column, role
298-
writer.setModel(proxyModel);
317+
writer.setModel(proxyModel.get());
299318
writer.addColumn("Label", AddressTableModel::Label, Qt::EditRole);
300319
writer.addColumn("Address Type", AddressTableModel::Type, Qt::EditRole);
301320
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)