Skip to content

Commit 56cc022

Browse files
committed
Merge #12080: Add support to search the address book
c316fdf [qt] Add support to search the address book (João Barbosa) Pull request description: This PR adds support to search the address book for both receiving and sending addresses. A specialisation of the `QSortFilterProxyModel` is added to implement the custom filtering. <img width="757" alt="screen shot 2018-01-03 at 16 05 57" src="https://user-images.githubusercontent.com/3534524/34528196-0347d61e-f0a0-11e7-9bd3-535e9e34ceb8.png"> <img width="759" alt="screen shot 2018-01-03 at 16 00 58" src="https://user-images.githubusercontent.com/3534524/34528202-07c99f24-f0a0-11e7-8e34-cff6a1ba2364.png"> Closes #623. Tree-SHA512: 316e646015c858fc70db6be72dc7922d5bb10a3399e7fa327c992e184cc37a124f11cffefab2dbe0d16bda790c7c0437db364686e66c40b4054b8250b4be15d0
2 parents 05042d3 + c316fdf commit 56cc022

File tree

3 files changed

+49
-19
lines changed

3 files changed

+49
-19
lines changed

src/qt/addressbookpage.cpp

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,41 @@
2121
#include <QMessageBox>
2222
#include <QSortFilterProxyModel>
2323

24+
class AddressBookSortFilterProxyModel final : public QSortFilterProxyModel
25+
{
26+
const QString m_type;
27+
28+
public:
29+
AddressBookSortFilterProxyModel(const QString& type, QObject* parent)
30+
: QSortFilterProxyModel(parent)
31+
, m_type(type)
32+
{
33+
setDynamicSortFilter(true);
34+
setFilterCaseSensitivity(Qt::CaseInsensitive);
35+
setSortCaseSensitivity(Qt::CaseInsensitive);
36+
}
37+
38+
protected:
39+
bool filterAcceptsRow(int row, const QModelIndex& parent) const
40+
{
41+
auto model = sourceModel();
42+
auto label = model->index(row, AddressTableModel::Label, parent);
43+
44+
if (model->data(label, AddressTableModel::TypeRole).toString() != m_type) {
45+
return false;
46+
}
47+
48+
auto address = model->index(row, AddressTableModel::Address, parent);
49+
50+
if (filterRegExp().indexIn(model->data(address).toString()) < 0 &&
51+
filterRegExp().indexIn(model->data(label).toString()) < 0) {
52+
return false;
53+
}
54+
55+
return true;
56+
}
57+
};
58+
2459
AddressBookPage::AddressBookPage(const PlatformStyle *platformStyle, Mode _mode, Tabs _tab, QWidget *parent) :
2560
QDialog(parent),
2661
ui(new Ui::AddressBookPage),
@@ -113,24 +148,12 @@ void AddressBookPage::setModel(AddressTableModel *_model)
113148
if(!_model)
114149
return;
115150

116-
proxyModel = new QSortFilterProxyModel(this);
151+
auto type = tab == ReceivingTab ? AddressTableModel::Receive : AddressTableModel::Send;
152+
proxyModel = new AddressBookSortFilterProxyModel(type, this);
117153
proxyModel->setSourceModel(_model);
118-
proxyModel->setDynamicSortFilter(true);
119-
proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
120-
proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
121-
switch(tab)
122-
{
123-
case ReceivingTab:
124-
// Receive filter
125-
proxyModel->setFilterRole(AddressTableModel::TypeRole);
126-
proxyModel->setFilterFixedString(AddressTableModel::Receive);
127-
break;
128-
case SendingTab:
129-
// Send filter
130-
proxyModel->setFilterRole(AddressTableModel::TypeRole);
131-
proxyModel->setFilterFixedString(AddressTableModel::Send);
132-
break;
133-
}
154+
155+
connect(ui->searchLineEdit, SIGNAL(textChanged(QString)), proxyModel, SLOT(setFilterWildcard(QString)));
156+
134157
ui->tableView->setModel(proxyModel);
135158
ui->tableView->sortByColumn(0, Qt::AscendingOrder);
136159

src/qt/addressbookpage.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <QDialog>
99

10+
class AddressBookSortFilterProxyModel;
1011
class AddressTableModel;
1112
class PlatformStyle;
1213

@@ -18,7 +19,6 @@ QT_BEGIN_NAMESPACE
1819
class QItemSelection;
1920
class QMenu;
2021
class QModelIndex;
21-
class QSortFilterProxyModel;
2222
QT_END_NAMESPACE
2323

2424
/** Widget that shows a list of sending or receiving addresses.
@@ -53,7 +53,7 @@ public Q_SLOTS:
5353
Mode mode;
5454
Tabs tab;
5555
QString returnValue;
56-
QSortFilterProxyModel *proxyModel;
56+
AddressBookSortFilterProxyModel *proxyModel;
5757
QMenu *contextMenu;
5858
QAction *deleteAction; // to be able to explicitly disable it
5959
QString newAddressToSelect;

src/qt/forms/addressbookpage.ui

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121
</property>
2222
</widget>
2323
</item>
24+
<item>
25+
<widget class="QLineEdit" name="searchLineEdit">
26+
<property name="placeholderText">
27+
<string>Enter address or label to search</string>
28+
</property>
29+
</widget>
30+
</item>
2431
<item>
2532
<widget class="QTableView" name="tableView">
2633
<property name="contextMenuPolicy">

0 commit comments

Comments
 (0)