[qt] Add support to search the address book

This commit is contained in:
João Barbosa 2018-01-03 16:00:18 +00:00
parent c991b304de
commit c316fdffec
3 changed files with 49 additions and 19 deletions

View file

@ -21,6 +21,41 @@
#include <QMessageBox> #include <QMessageBox>
#include <QSortFilterProxyModel> #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) : AddressBookPage::AddressBookPage(const PlatformStyle *platformStyle, Mode _mode, Tabs _tab, QWidget *parent) :
QDialog(parent), QDialog(parent),
ui(new Ui::AddressBookPage), ui(new Ui::AddressBookPage),
@ -113,24 +148,12 @@ void AddressBookPage::setModel(AddressTableModel *_model)
if(!_model) if(!_model)
return; return;
proxyModel = new QSortFilterProxyModel(this); auto type = tab == ReceivingTab ? AddressTableModel::Receive : AddressTableModel::Send;
proxyModel = new AddressBookSortFilterProxyModel(type, this);
proxyModel->setSourceModel(_model); proxyModel->setSourceModel(_model);
proxyModel->setDynamicSortFilter(true);
proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive); connect(ui->searchLineEdit, SIGNAL(textChanged(QString)), proxyModel, SLOT(setFilterWildcard(QString)));
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;
}
ui->tableView->setModel(proxyModel); ui->tableView->setModel(proxyModel);
ui->tableView->sortByColumn(0, Qt::AscendingOrder); ui->tableView->sortByColumn(0, Qt::AscendingOrder);

View file

@ -7,6 +7,7 @@
#include <QDialog> #include <QDialog>
class AddressBookSortFilterProxyModel;
class AddressTableModel; class AddressTableModel;
class PlatformStyle; class PlatformStyle;
@ -18,7 +19,6 @@ QT_BEGIN_NAMESPACE
class QItemSelection; class QItemSelection;
class QMenu; class QMenu;
class QModelIndex; class QModelIndex;
class QSortFilterProxyModel;
QT_END_NAMESPACE QT_END_NAMESPACE
/** Widget that shows a list of sending or receiving addresses. /** Widget that shows a list of sending or receiving addresses.
@ -53,7 +53,7 @@ private:
Mode mode; Mode mode;
Tabs tab; Tabs tab;
QString returnValue; QString returnValue;
QSortFilterProxyModel *proxyModel; AddressBookSortFilterProxyModel *proxyModel;
QMenu *contextMenu; QMenu *contextMenu;
QAction *deleteAction; // to be able to explicitly disable it QAction *deleteAction; // to be able to explicitly disable it
QString newAddressToSelect; QString newAddressToSelect;

View file

@ -21,6 +21,13 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QLineEdit" name="searchLineEdit">
<property name="placeholderText">
<string>Enter address or label to search</string>
</property>
</widget>
</item>
<item> <item>
<widget class="QTableView" name="tableView"> <widget class="QTableView" name="tableView">
<property name="contextMenuPolicy"> <property name="contextMenuPolicy">