Skip to content

Commit b431913

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 b431913

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/qt/addressbookpage.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,24 @@ class AddressBookSortFilterProxyModel final : public QSortFilterProxyModel
3535
setSortCaseSensitivity(Qt::CaseInsensitive);
3636
}
3737

38+
AddressBookSortFilterProxyModel(const QString& type, QObject* parent, AddressBookSortFilterProxyModel* nestedProxy)
39+
: AddressBookSortFilterProxyModel(type, parent)
40+
{
41+
if (nestedProxy) {
42+
nextedFilterProxyModel.reset(nestedProxy);
43+
nextedFilterProxyModel->setSourceModel(this);
44+
}
45+
}
46+
47+
AddressBookSortFilterProxyModel* nestedProxyModel() const noexcept{
48+
if (!nextedFilterProxyModel) return const_cast<AddressBookSortFilterProxyModel*>(this);
49+
return nextedFilterProxyModel.get();
50+
}
51+
52+
bool isNestedFilterEnabled() const {
53+
return nextedFilterProxyModel!=nullptr;
54+
}
55+
3856
protected:
3957
bool filterAcceptsRow(int row, const QModelIndex& parent) const override
4058
{
@@ -62,6 +80,9 @@ class AddressBookSortFilterProxyModel final : public QSortFilterProxyModel
6280

6381
return filterBy;
6482
}
83+
84+
private:
85+
std::unique_ptr<AddressBookSortFilterProxyModel> nextedFilterProxyModel{nullptr};
6586
};
6687

6788
AddressBookPage::AddressBookPage(const PlatformStyle *platformStyle, Mode _mode, Tabs _tab, QWidget *parent) :
@@ -140,12 +161,12 @@ void AddressBookPage::setModel(AddressTableModel *_model)
140161
return;
141162

142163
auto type = tab == ReceivingTab ? AddressTableModel::Receive : AddressTableModel::Send;
143-
proxyModel = new AddressBookSortFilterProxyModel(type, this);
164+
proxyModel.reset(new AddressBookSortFilterProxyModel(type, this, new AddressBookSortFilterProxyModel(type, this)));
144165
proxyModel->setSourceModel(_model);
145166

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

148-
ui->tableView->setModel(proxyModel);
169+
ui->tableView->setModel(proxyModel->nestedProxyModel());
149170
ui->tableView->sortByColumn(0, Qt::AscendingOrder);
150171

151172
// Set column widths
@@ -295,7 +316,7 @@ void AddressBookPage::on_exportButton_clicked()
295316
CSVModelWriter writer(filename);
296317

297318
// name, column, role
298-
writer.setModel(proxyModel);
319+
writer.setModel(proxyModel.get());
299320
writer.addColumn("Label", AddressTableModel::Label, Qt::EditRole);
300321
writer.addColumn("Address Type", AddressTableModel::Type, Qt::EditRole);
301322
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)