Skip to content

Commit

Permalink
Merge bitcoin#12080: Add support to search the address book
Browse files Browse the repository at this point in the history
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 dashpay#623.

Tree-SHA512: 316e646015c858fc70db6be72dc7922d5bb10a3399e7fa327c992e184cc37a124f11cffefab2dbe0d16bda790c7c0437db364686e66c40b4054b8250b4be15d0
  • Loading branch information
laanwj authored and PastaPastaPasta committed Jun 10, 2020
1 parent 6fbecce commit 8717a90
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 19 deletions.
57 changes: 40 additions & 17 deletions src/qt/addressbookpage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,41 @@
#include <QMessageBox>
#include <QSortFilterProxyModel>

class AddressBookSortFilterProxyModel final : public QSortFilterProxyModel
{
const QString m_type;

public:
AddressBookSortFilterProxyModel(const QString& type, QObject* parent)
: QSortFilterProxyModel(parent)
, m_type(type)
{
setDynamicSortFilter(true);
setFilterCaseSensitivity(Qt::CaseInsensitive);
setSortCaseSensitivity(Qt::CaseInsensitive);
}

protected:
bool filterAcceptsRow(int row, const QModelIndex& parent) const
{
auto model = sourceModel();
auto label = model->index(row, AddressTableModel::Label, parent);

if (model->data(label, AddressTableModel::TypeRole).toString() != m_type) {
return false;
}

auto address = model->index(row, AddressTableModel::Address, parent);

if (filterRegExp().indexIn(model->data(address).toString()) < 0 &&
filterRegExp().indexIn(model->data(label).toString()) < 0) {
return false;
}

return true;
}
};

AddressBookPage::AddressBookPage(const PlatformStyle *platformStyle, Mode _mode, Tabs _tab, QWidget *parent) :
QDialog(parent),
ui(new Ui::AddressBookPage),
Expand Down Expand Up @@ -119,24 +154,12 @@ void AddressBookPage::setModel(AddressTableModel *_model)
if(!_model)
return;

proxyModel = new QSortFilterProxyModel(this);
auto type = tab == ReceivingTab ? AddressTableModel::Receive : AddressTableModel::Send;
proxyModel = new AddressBookSortFilterProxyModel(type, this);
proxyModel->setSourceModel(_model);
proxyModel->setDynamicSortFilter(true);
proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
switch(tab)
{
case ReceivingTab:
// Receive filter
proxyModel->setFilterRole(AddressTableModel::TypeRole);
proxyModel->setFilterFixedString(AddressTableModel::Receive);
break;
case SendingTab:
// Send filter
proxyModel->setFilterRole(AddressTableModel::TypeRole);
proxyModel->setFilterFixedString(AddressTableModel::Send);
break;
}

connect(ui->searchLineEdit, SIGNAL(textChanged(QString)), proxyModel, SLOT(setFilterWildcard(QString)));

ui->tableView->setModel(proxyModel);
ui->tableView->sortByColumn(0, Qt::AscendingOrder);

Expand Down
4 changes: 2 additions & 2 deletions src/qt/addressbookpage.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <QDialog>

class AddressBookSortFilterProxyModel;
class AddressTableModel;
class PlatformStyle;

Expand All @@ -18,7 +19,6 @@ QT_BEGIN_NAMESPACE
class QItemSelection;
class QMenu;
class QModelIndex;
class QSortFilterProxyModel;
QT_END_NAMESPACE

/** Widget that shows a list of sending or receiving addresses.
Expand Down Expand Up @@ -53,7 +53,7 @@ public Q_SLOTS:
Mode mode;
Tabs tab;
QString returnValue;
QSortFilterProxyModel *proxyModel;
AddressBookSortFilterProxyModel *proxyModel;
QMenu *contextMenu;
QAction *deleteAction; // to be able to explicitly disable it
QString newAddressToSelect;
Expand Down
7 changes: 7 additions & 0 deletions src/qt/forms/addressbookpage.ui
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="searchLineEdit">
<property name="placeholderText">
<string>Enter address or label to search</string>
</property>
</widget>
</item>
<item>
<widget class="QTableView" name="tableView">
<property name="contextMenuPolicy">
Expand Down

0 comments on commit 8717a90

Please sign in to comment.