|
21 | 21 | #include <QMessageBox>
|
22 | 22 | #include <QSortFilterProxyModel>
|
23 | 23 |
|
| 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 | + |
24 | 59 | AddressBookPage::AddressBookPage(const PlatformStyle *platformStyle, Mode _mode, Tabs _tab, QWidget *parent) :
|
25 | 60 | QDialog(parent),
|
26 | 61 | ui(new Ui::AddressBookPage),
|
@@ -113,24 +148,12 @@ void AddressBookPage::setModel(AddressTableModel *_model)
|
113 | 148 | if(!_model)
|
114 | 149 | return;
|
115 | 150 |
|
116 |
| - proxyModel = new QSortFilterProxyModel(this); |
| 151 | + auto type = tab == ReceivingTab ? AddressTableModel::Receive : AddressTableModel::Send; |
| 152 | + proxyModel = new AddressBookSortFilterProxyModel(type, this); |
117 | 153 | 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 | + |
134 | 157 | ui->tableView->setModel(proxyModel);
|
135 | 158 | ui->tableView->sortByColumn(0, Qt::AscendingOrder);
|
136 | 159 |
|
|
0 commit comments