Skip to content

Commit 68f34f9

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 9dc4eed commit 68f34f9

File tree

3 files changed

+81
-12
lines changed

3 files changed

+81
-12
lines changed

src/qt/guiutil.cpp

Lines changed: 64 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+
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");
@@ -1008,6 +1017,33 @@ void ShowModalDialogAsynchronously(QDialog* dialog)
10081017
dialog->show();
10091018
}
10101019

1020+
void AddItemsToAddressTypeCombo(QComboBox* addressType, bool taprootEnabled, const std::map<OutputType, QString>& outputTypeTooltipsMap, std::optional<OutputType> defaultType)
1021+
{
1022+
auto add_address_type = [&](OutputType type, QString description) {
1023+
const auto index{addressType->count()};
1024+
// getting the tooltip (provided by the caller) that will be displayed on the item in the combo
1025+
QString tooltip{};
1026+
auto it{outputTypeTooltipsMap.find(type)};
1027+
// if can't find it no tooltip will be displayed
1028+
if (it != outputTypeTooltipsMap.end()) tooltip = it->second;
1029+
addressType->addItem(description, static_cast<int>(type));
1030+
addressType->setItemData(index, tooltip, Qt::ToolTipRole);
1031+
// setting default selected value for the combo if it has been passed
1032+
if (defaultType.has_value() && defaultType.value() == type) addressType->setCurrentIndex(index);
1033+
};
1034+
1035+
// Adding all output types defined in the map to the combo
1036+
for (const auto& pair : outputTypeDescriptionsMap()) {
1037+
OutputType type{pair.first};
1038+
OutputTypeInfo typeInfo{pair.second};
1039+
// if it requires taproot check it with arg taprootEnabled
1040+
// eg call from receivecoindialog.cpp
1041+
if ((!typeInfo.requiresTaprootEnabled) || (typeInfo.requiresTaprootEnabled == taprootEnabled)) {
1042+
add_address_type(type, typeInfo.description);
1043+
}
1044+
}
1045+
}
1046+
10111047
QString WalletDisplayName(const QString& name)
10121048
{
10131049
return name.isEmpty() ? "[" + QObject::tr("default wallet") + "]" : name;
@@ -1017,4 +1053,32 @@ QString WalletDisplayName(const std::string& name)
10171053
{
10181054
return WalletDisplayName(QString::fromStdString(name));
10191055
}
1056+
1057+
void AddItemsToAddressTypeCombo(QComboBox* addressType, bool taprootEnabled, const std::map<OutputType, QString>& outputTypeTooltipsMap, std::optional<OutputType> defaultType)
1058+
{
1059+
auto add_address_type = [&](OutputType type, QString description) {
1060+
const auto index{addressType->count()};
1061+
// getting the tooltip (provided by the caller) that will be displayed on the item in the combo
1062+
QString tooltip{};
1063+
auto it{outputTypeTooltipsMap.find(type)};
1064+
// if can't find it no tooltip will be displayed
1065+
if (it != outputTypeTooltipsMap.end()) tooltip = it->second;
1066+
addressType->addItem(description, static_cast<int>(type));
1067+
addressType->setItemData(index, tooltip, Qt::ToolTipRole);
1068+
// setting default selected value for the combo if it has been passed
1069+
if (defaultType.has_value() && defaultType.value() == type) addressType->setCurrentIndex(index);
1070+
};
1071+
1072+
// Adding all output types defined in the map to the combo
1073+
for (const auto& pair : outputTypeDescriptionsMap()) {
1074+
OutputType type{pair.first};
1075+
OutputTypeInfo typeInfo{pair.second};
1076+
// if it requires taproot check it with arg taprootEnabled
1077+
// eg call from receivecoindialog.cpp
1078+
if ((!typeInfo.requiresTaprootEnabled) || (typeInfo.requiresTaprootEnabled == taprootEnabled)) {
1079+
add_address_type(type, typeInfo.description);
1080+
}
1081+
}
1082+
}
1083+
10201084
} // namespace GUIUtil

src/qt/guiutil.h

Lines changed: 9 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;
@@ -439,6 +441,13 @@ namespace GUIUtil
439441
QString WalletDisplayName(const std::string& name);
440442
QString WalletDisplayName(const QString& name);
441443

444+
struct OutputTypeInfo {
445+
const QString description;
446+
const bool requiresTaprootEnabled;
447+
};
448+
std::map<OutputType, OutputTypeInfo> outputTypeDescriptionsMap();
449+
450+
void AddItemsToAddressTypeCombo(QComboBox* addressType, bool taprootEnabled, const std::map<OutputType, QString>& outputTypeTooltipsMap, std::optional<OutputType> defaultType = std::nullopt);
442451
} // namespace GUIUtil
443452

444453
#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)