Skip to content

Commit 6eb2511

Browse files
gui: Add a combo widget to filter by address type
Introduce a label and a combobox UI widgets for address type filtering on the address book page. These 2 widgets are been displayed only on the Receiving tab. To ensure that the combo box selection updates the filter model correctly, an intermediary signal (addressTypeChanged) and a slot (handleAddressTypeChanged) were necessary due to the incompatibility between QComboBox::currentIndexChanged and QSortFilterProxyModel::setFilterFixedString. Update filter model to use nested filtering so when selected item changes on address type combo it will update the filter pattern on top of what the parent filter has already, which is lead by the searchLineEdit widget and all references of the current proxyModel (eg mapToSource, mapFromSource) to the nested and final filter model. Use GUIUtil::AddItemsToAddressTypeCombo to populate the combo with the default output types descriptions passing a defined local map for the output types tooltips that will be displayed for each item, similarly to the address types combobox in receivecoinsdialog.
1 parent 5d597c0 commit 6eb2511

File tree

3 files changed

+86
-4
lines changed

3 files changed

+86
-4
lines changed

src/qt/addressbookpage.cpp

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ void AddressBookPage::setModel(AddressTableModel *_model)
163163
return;
164164

165165
auto type = tab == ReceivingTab ? AddressTableModel::Receive : AddressTableModel::Send;
166-
proxyModel.reset(new AddressBookSortFilterProxyModel(type, this, false));
166+
proxyModel.reset(new AddressBookSortFilterProxyModel(type, this, true));
167167
proxyModel->setSourceModel(_model);
168168

169169
connect(ui->searchLineEdit, &QLineEdit::textChanged, proxyModel.get(), &QSortFilterProxyModel::setFilterWildcard);
@@ -186,6 +186,8 @@ void AddressBookPage::setModel(AddressTableModel *_model)
186186

187187
selectionChanged();
188188
this->updateWindowsTitleWithWalletName();
189+
190+
this->setupAddressTypeCombo();
189191
}
190192

191193
void AddressBookPage::on_copyAddress_clicked()
@@ -214,7 +216,7 @@ void AddressBookPage::onEditAction()
214216
EditAddressDialog::EditSendingAddress :
215217
EditAddressDialog::EditReceivingAddress, this);
216218
dlg->setModel(model);
217-
QModelIndex origIndex = proxyModel->mapToSource(indexes.at(0));
219+
QModelIndex origIndex = proxyModel->nestedProxyModel()->mapToSource(indexes.at(0));
218220
dlg->loadRow(origIndex.row());
219221
GUIUtil::ShowModalDialogAsynchronously(dlg);
220222
}
@@ -318,7 +320,7 @@ void AddressBookPage::on_exportButton_clicked()
318320
CSVModelWriter writer(filename);
319321

320322
// name, column, role
321-
writer.setModel(proxyModel.get());
323+
writer.setModel(proxyModel->nestedProxyModel());
322324
writer.addColumn("Label", AddressTableModel::Label, Qt::EditRole);
323325
writer.addColumn("Address Type", AddressTableModel::Type, Qt::EditRole);
324326
writer.addColumn("Address", AddressTableModel::Address, Qt::EditRole);
@@ -342,7 +344,7 @@ void AddressBookPage::contextualMenu(const QPoint &point)
342344

343345
void AddressBookPage::selectNewAddress(const QModelIndex &parent, int begin, int /*end*/)
344346
{
345-
QModelIndex idx = proxyModel->mapFromSource(model->index(begin, AddressTableModel::Address, parent));
347+
QModelIndex idx = proxyModel.get()->mapFromSource(model->index(begin, AddressTableModel::Address, parent));
346348
if(idx.isValid() && (idx.data(Qt::EditRole).toString() == newAddressToSelect))
347349
{
348350
// Select row of newly created address, once
@@ -364,3 +366,57 @@ void AddressBookPage::updateWindowsTitleWithWalletName()
364366
}
365367
}
366368
}
369+
370+
std::map<OutputType, QString> AddressBookPage::addressTypeTooltipMap() {
371+
return {{OutputType::LEGACY, QObject::tr("Not recommended due to higher fees and less protection against typos.")},
372+
{OutputType::P2SH_SEGWIT, QObject::tr("An address compatible with older wallets.")},
373+
{OutputType::BECH32, QObject::tr("Native segwit address (BIP-173). Some old wallets don't support it.")},
374+
{OutputType::BECH32M, QObject::tr("Bech32m (BIP-350) is an upgrade to Bech32, wallet support is still limited.")}};
375+
}
376+
377+
QString AddressBookPage::showAllTypes() const{
378+
return QObject::tr("All");
379+
}
380+
381+
QString AddressBookPage::showAllTypesToolTip() const{
382+
return QObject::tr("Select an address type to filter by.");
383+
}
384+
385+
void AddressBookPage::handleAddressTypeChanged(int index)
386+
{
387+
QString selectedValue = ui->addressType->currentText();
388+
// If show all types is selected then clear the selected value
389+
// that will be sent to the filter so it shows everything
390+
if (selectedValue == showAllTypes()) selectedValue.clear();
391+
// Emit a signal with the selected value
392+
Q_EMIT addressTypeChanged(selectedValue);
393+
// Forcing the resize as if it was selected an item with
394+
// shorter content and right after a longer one, the
395+
// columns are not resizing properly, this fixes it
396+
ui->tableView->resizeColumnsToContents();
397+
}
398+
399+
void AddressBookPage::initializeAddressTypeCombo()
400+
{
401+
const auto index = ui->addressType->count();
402+
ui->addressType->addItem(showAllTypes(), index);
403+
ui->addressType->setItemData(index, showAllTypesToolTip(), Qt::ToolTipRole);
404+
ui->addressType->setCurrentIndex(index);
405+
}
406+
407+
void AddressBookPage::setupAddressTypeCombo()
408+
{
409+
this->initializeAddressTypeCombo();
410+
ui->labelAddressType->setVisible(tab == ReceivingTab);
411+
ui->addressType->setVisible(tab == ReceivingTab);
412+
GUIUtil::AddItemsToAddressTypeCombo(ui->addressType, true, this->addressTypeTooltipMap());
413+
connect(ui->addressType, qOverload<int>(&QComboBox::currentIndexChanged), this, &AddressBookPage::handleAddressTypeChanged);
414+
connect(this, &AddressBookPage::addressTypeChanged, proxyModel->nestedProxyModel(), &QSortFilterProxyModel::setFilterFixedString);
415+
}
416+
417+
std::map<OutputType, QString> AddressBookPage::addressTypeTooltipMap() {
418+
return {{OutputType::LEGACY, QObject::tr("Not recommended due to higher fees and less protection against typos.")},
419+
{OutputType::P2SH_SEGWIT, QObject::tr("An address compatible with older wallets.")},
420+
{OutputType::BECH32, QObject::tr("Native segwit address (BIP-173). Some old wallets don't support it.")},
421+
{OutputType::BECH32M, QObject::tr("Bech32m (BIP-350) is an upgrade to Bech32, wallet support is still limited.")}};
422+
}

src/qt/addressbookpage.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#ifndef BITCOIN_QT_ADDRESSBOOKPAGE_H
66
#define BITCOIN_QT_ADDRESSBOOKPAGE_H
77

8+
#include <outputtype.h>
9+
810
#include <QDialog>
911

1012
class AddressBookSortFilterProxyModel;
@@ -57,6 +59,13 @@ public Q_SLOTS:
5759
QMenu *contextMenu;
5860
QString newAddressToSelect;
5961
void updateWindowsTitleWithWalletName();
62+
void initializeAddressTypeCombo();
63+
/** Default selected item of the address type combo to display all address types */
64+
QString showAllTypes() const;
65+
QString showAllTypesToolTip() const;
66+
/** Tooltip for each address type that will be displayed on the combo*/
67+
std::map<OutputType, QString> addressTypeTooltipMap();
68+
void setupAddressTypeCombo();
6069

6170
private Q_SLOTS:
6271
/** Delete currently selected address entry */
@@ -78,9 +87,16 @@ private Q_SLOTS:
7887
void contextualMenu(const QPoint &point);
7988
/** New entry/entries were added to address table */
8089
void selectNewAddress(const QModelIndex &parent, int begin, int /*end*/);
90+
/** Address type combo selection changed */
91+
void handleAddressTypeChanged(int index);
8192

8293
Q_SIGNALS:
8394
void sendCoins(QString addr);
95+
/** Emitted when the addressType combobox is changed (handled by handleAddressTypeChange).
96+
* This signal is used as a workaround to connect the combobox and the proxy model filter,
97+
* preventing the compiler error "Signal and slot arguments are not compatible."*/
98+
void addressTypeChanged(const QString &addressType);
99+
84100
};
85101

86102
#endif // BITCOIN_QT_ADDRESSBOOKPAGE_H

src/qt/forms/addressbookpage.ui

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@
109109
</property>
110110
</widget>
111111
</item>
112+
<item>
113+
<widget class="QLabel" name="labelAddressType">
114+
<property name="text">
115+
<string>Show address type:</string>
116+
</property>
117+
</widget>
118+
</item>
119+
<item>
120+
<widget class="QComboBox" name="addressType"/>
121+
</item>
112122
<item>
113123
<spacer name="horizontalSpacer">
114124
<property name="orientation">

0 commit comments

Comments
 (0)