Skip to content

Commit c5acd3a

Browse files
gui: Refactor AddressType from ReceiveCoinsDialog
Extract and refactor ui->addressType combo content filling into GUIUtil, having the translated descriptions in one place and allowing other Qt widget containers to easily create similar combo boxes with customizable tooltips per item and default selected item, both specified by the caller.
1 parent 8f7b9eb commit c5acd3a

File tree

3 files changed

+52
-12
lines changed

3 files changed

+52
-12
lines changed

src/qt/guiutil.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <QAbstractItemView>
3838
#include <QApplication>
3939
#include <QClipboard>
40+
#include <QComboBox>
4041
#include <QDateTime>
4142
#include <QDesktopServices>
4243
#include <QDialog>
@@ -87,6 +88,14 @@ using namespace std::chrono_literals;
8788

8889
namespace GUIUtil {
8990

91+
const std::map<OutputType, OutputTypeInfo> outputTypeDescriptionsMap() {
92+
return {
93+
{OutputType::LEGACY, {QObject::tr("Base58 (Legacy)"), false}},
94+
{OutputType::P2SH_SEGWIT, {QObject::tr("Base58 (P2SH-SegWit)"), false}},
95+
{OutputType::BECH32, {QObject::tr("Bech32 (SegWit)"), false}},
96+
{OutputType::BECH32M, {QObject::tr("Bech32m (Taproot)"), true}}};
97+
}
98+
9099
QString dateTimeStr(const QDateTime &date)
91100
{
92101
return QLocale::system().toString(date.date(), QLocale::ShortFormat) + QString(" ") + date.toString("hh:mm");
@@ -1002,4 +1011,31 @@ void ShowModalDialogAsynchronously(QDialog* dialog)
10021011
dialog->show();
10031012
}
10041013

1014+
void AddItemsToAddressTypeCombo(QComboBox* addressType, bool taprootEnabled, const std::map<OutputType, QString>& outputTypeTooltipsMap, std::optional<OutputType> defaultType)
1015+
{
1016+
auto add_address_type = [&](OutputType type, QString description) {
1017+
const auto index{addressType->count()};
1018+
// getting the tooltip (provided by the caller) that will be displayed on the item in the combo
1019+
QString tooltip{};
1020+
auto it{outputTypeTooltipsMap.find(type)};
1021+
// if can't find it no tooltip will be displayed
1022+
if (it != outputTypeTooltipsMap.end()) tooltip = it->second;
1023+
addressType->addItem(description, static_cast<int>(type));
1024+
addressType->setItemData(index, tooltip, Qt::ToolTipRole);
1025+
// setting default selected value for the combo if it has been passed
1026+
if (defaultType.has_value() && defaultType.value() == type) addressType->setCurrentIndex(index);
1027+
};
1028+
1029+
// Adding all output types defined in the map to the combo
1030+
for (const auto& pair : outputTypeDescriptionsMap()) {
1031+
OutputType type{pair.first};
1032+
OutputTypeInfo typeInfo{pair.second};
1033+
// if it requires taproot check it with arg taprootEnabled
1034+
// eg call from receivecoindialog.cpp
1035+
if ((!typeInfo.requiresTaprootEnabled) || (typeInfo.requiresTaprootEnabled == taprootEnabled)) {
1036+
add_address_type(type, typeInfo.description);
1037+
}
1038+
}
1039+
}
1040+
10051041
} // namespace GUIUtil

src/qt/guiutil.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <consensus/amount.h>
99
#include <net.h>
1010
#include <netaddress.h>
11+
#include <outputtype.h>
1112
#include <util/check.h>
1213
#include <util/fs.h>
1314

@@ -40,6 +41,7 @@ QT_BEGIN_NAMESPACE
4041
class QAbstractButton;
4142
class QAbstractItemView;
4243
class QAction;
44+
class QComboBox;
4345
class QDateTime;
4446
class QDialog;
4547
class QFont;
@@ -436,6 +438,12 @@ namespace GUIUtil
436438
return false;
437439
}
438440

441+
struct OutputTypeInfo {
442+
const QString description;
443+
const bool requiresTaprootEnabled;
444+
};
445+
const std::map<OutputType, OutputTypeInfo> outputTypeDescriptionsMap();
446+
void AddItemsToAddressTypeCombo(QComboBox* addressType, bool taprootEnabled, const std::map<OutputType, QString>& outputTypeTooltipsMap, std::optional<OutputType> defaultType = std::nullopt);
439447
} // namespace GUIUtil
440448

441449
#endif // BITCOIN_QT_GUIUTIL_H

src/qt/receivecoinsdialog.cpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,15 @@ void ReceiveCoinsDialog::setModel(WalletModel *_model)
8686
&QItemSelectionModel::selectionChanged, this,
8787
&ReceiveCoinsDialog::recentRequestsView_selectionChanged);
8888

89+
/** Tooltip for each address type that will be displayed on the combo*/
90+
std::map<OutputType, QString> addressTypeTooltipMap{
91+
{OutputType::LEGACY, QObject::tr("Not recommended due to higher fees and less protection against typos.")},
92+
{OutputType::P2SH_SEGWIT, QObject::tr("Generates an address compatible with older wallets.")},
93+
{OutputType::BECH32, QObject::tr("Generates a native segwit address (BIP-173). Some old wallets don't support it.")},
94+
{OutputType::BECH32M, QObject::tr("Bech32m (BIP-350) is an upgrade to Bech32, wallet support is still limited.")}};
95+
8996
// Populate address type dropdown and select default
90-
auto add_address_type = [&](OutputType type, const QString& text, const QString& tooltip) {
91-
const auto index = ui->addressType->count();
92-
ui->addressType->addItem(text, (int) type);
93-
ui->addressType->setItemData(index, tooltip, Qt::ToolTipRole);
94-
if (model->wallet().getDefaultAddressType() == type) ui->addressType->setCurrentIndex(index);
95-
};
96-
add_address_type(OutputType::LEGACY, tr("Base58 (Legacy)"), tr("Not recommended due to higher fees and less protection against typos."));
97-
add_address_type(OutputType::P2SH_SEGWIT, tr("Base58 (P2SH-SegWit)"), tr("Generates an address compatible with older wallets."));
98-
add_address_type(OutputType::BECH32, tr("Bech32 (SegWit)"), tr("Generates a native segwit address (BIP-173). Some old wallets don't support it."));
99-
if (model->wallet().taprootEnabled()) {
100-
add_address_type(OutputType::BECH32M, tr("Bech32m (Taproot)"), tr("Bech32m (BIP-350) is an upgrade to Bech32, wallet support is still limited."));
101-
}
97+
GUIUtil::AddItemsToAddressTypeCombo(ui->addressType, model->wallet().taprootEnabled(), addressTypeTooltipMap, model->wallet().getDefaultAddressType());
10298

10399
// Set the button to be enabled or disabled based on whether the wallet can give out new addresses.
104100
ui->receiveButton->setEnabled(model->wallet().canGetAddresses());

0 commit comments

Comments
 (0)