Skip to content

Commit e4a3d5e

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 b431913 commit e4a3d5e

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

src/qt/addressbookpage.cpp

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ void AddressBookPage::setModel(AddressTableModel *_model)
184184

185185
selectionChanged();
186186
this->updateWindowsTitleWithWalletName();
187+
188+
this->setupAddressTypeCombo();
187189
}
188190

189191
void AddressBookPage::on_copyAddress_clicked()
@@ -212,7 +214,7 @@ void AddressBookPage::onEditAction()
212214
EditAddressDialog::EditSendingAddress :
213215
EditAddressDialog::EditReceivingAddress, this);
214216
dlg->setModel(model);
215-
QModelIndex origIndex = proxyModel->mapToSource(indexes.at(0));
217+
QModelIndex origIndex = proxyModel->nestedProxyModel()->mapToSource(indexes.at(0));
216218
dlg->loadRow(origIndex.row());
217219
GUIUtil::ShowModalDialogAsynchronously(dlg);
218220
}
@@ -316,7 +318,7 @@ void AddressBookPage::on_exportButton_clicked()
316318
CSVModelWriter writer(filename);
317319

318320
// name, column, role
319-
writer.setModel(proxyModel.get());
321+
writer.setModel(proxyModel->nestedProxyModel());
320322
writer.addColumn("Label", AddressTableModel::Label, Qt::EditRole);
321323
writer.addColumn("Address Type", AddressTableModel::Type, Qt::EditRole);
322324
writer.addColumn("Address", AddressTableModel::Address, Qt::EditRole);
@@ -340,7 +342,7 @@ void AddressBookPage::contextualMenu(const QPoint &point)
340342

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

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)