qt: Use QVariant instead of int for BitcoinUnit in QSettings

This change improves type safety.
This commit is contained in:
Hennadii Stepanov 2021-01-21 22:27:28 +02:00 committed by William Casarin
parent 5d254a234d
commit 75832fdc37
5 changed files with 63 additions and 11 deletions

View file

@ -95,6 +95,8 @@ static void RegisterMetaTypes()
qRegisterMetaType<std::function<void()>>("std::function<void()>");
qRegisterMetaType<QMessageBox::Icon>("QMessageBox::Icon");
qRegisterMetaType<interfaces::BlockAndHeaderTipInfo>("interfaces::BlockAndHeaderTipInfo");
qRegisterMetaTypeStreamOperators<BitcoinUnit>("BitcoinUnit");
}
static QString GetLangTerritory()

View file

@ -250,3 +250,40 @@ CAmount BitcoinUnits::maxMoney()
{
return MAX_MONEY;
}
namespace {
qint8 ToQint8(BitcoinUnit unit)
{
switch (unit) {
case BitcoinUnits::BTC: return 0;
case BitcoinUnits::mBTC: return 1;
case BitcoinUnits::uBTC: return 2;
case BitcoinUnits::SAT: return 3;
} // no default case, so the compiler can warn about missing cases
assert(false);
}
BitcoinUnit FromQint8(qint8 num)
{
switch (num) {
case 0: return BitcoinUnits::BTC;
case 1: return BitcoinUnits::mBTC;
case 2: return BitcoinUnits::uBTC;
case 3: return BitcoinUnits::SAT;
}
assert(false);
}
} // namespace
QDataStream& operator<<(QDataStream& out, const BitcoinUnit& unit)
{
return out << ToQint8(unit);
}
QDataStream& operator>>(QDataStream& in, BitcoinUnit& unit)
{
qint8 input;
in >> input;
unit = FromQint8(input);
return in;
}

View file

@ -8,6 +8,7 @@
#include <consensus/amount.h>
#include <QAbstractListModel>
#include <QDataStream>
#include <QString>
// U+2009 THIN SPACE = UTF-8 E2 80 89
@ -45,6 +46,7 @@ public:
uBTC,
SAT
};
Q_ENUM(Unit)
enum class SeparatorStyle
{
@ -111,4 +113,7 @@ private:
};
typedef BitcoinUnits::Unit BitcoinUnit;
QDataStream& operator<<(QDataStream& out, const BitcoinUnit& unit);
QDataStream& operator>>(QDataStream& in, BitcoinUnit& unit);
#endif // BITCOIN_QT_BITCOINUNITS_H

View file

@ -24,6 +24,7 @@
#include <QLatin1Char>
#include <QSettings>
#include <QStringList>
#include <QVariant>
const char *DEFAULT_GUI_PROXY_HOST = "127.0.0.1";
@ -71,9 +72,16 @@ void OptionsModel::Init(bool resetSettings)
fMinimizeOnClose = settings.value("fMinimizeOnClose").toBool();
// Display
if (!settings.contains("nDisplayUnit"))
settings.setValue("nDisplayUnit", BitcoinUnits::BTC);
nDisplayUnit = settings.value("nDisplayUnit").toInt();
if (!settings.contains("DisplayBitcoinUnit")) {
settings.setValue("DisplayBitcoinUnit", QVariant::fromValue(BitcoinUnit::BTC));
}
QVariant unit = settings.value("DisplayBitcoinUnit");
if (unit.canConvert<BitcoinUnit>()) {
m_display_bitcoin_unit = unit.value<BitcoinUnit>();
} else {
m_display_bitcoin_unit = BitcoinUnit::BTC;
settings.setValue("DisplayBitcoinUnit", QVariant::fromValue(m_display_bitcoin_unit));
}
if (!settings.contains("strThirdPartyTxUrls"))
settings.setValue("strThirdPartyTxUrls", "");
@ -356,7 +364,7 @@ QVariant OptionsModel::data(const QModelIndex & index, int role) const
return m_sub_fee_from_amount;
#endif
case DisplayUnit:
return nDisplayUnit;
return QVariant::fromValue(m_display_bitcoin_unit);
case ThirdPartyTxUrls:
return strThirdPartyTxUrls;
case Language:
@ -567,12 +575,11 @@ bool OptionsModel::setData(const QModelIndex & index, const QVariant & value, in
/** Updates current unit in memory, settings and emits displayUnitChanged(newUnit) signal */
void OptionsModel::setDisplayUnit(const QVariant &value)
{
if (!value.isNull())
{
if (!value.isNull()) {
QSettings settings;
nDisplayUnit = value.toInt();
settings.setValue("nDisplayUnit", nDisplayUnit);
Q_EMIT displayUnitChanged(nDisplayUnit);
m_display_bitcoin_unit = value.value<BitcoinUnit>();
settings.setValue("DisplayBitcoinUnit", QVariant::fromValue(m_display_bitcoin_unit));
Q_EMIT displayUnitChanged(static_cast<int>(m_display_bitcoin_unit));
}
}

View file

@ -6,6 +6,7 @@
#define BITCOIN_QT_OPTIONSMODEL_H
#include <cstdint>
#include <qt/bitcoinunits.h>
#include <qt/guiconstants.h>
#include <QAbstractListModel>
@ -86,7 +87,7 @@ public:
bool getShowTrayIcon() const { return m_show_tray_icon; }
bool getMinimizeToTray() const { return fMinimizeToTray; }
bool getMinimizeOnClose() const { return fMinimizeOnClose; }
int getDisplayUnit() const { return nDisplayUnit; }
int getDisplayUnit() const { return static_cast<int>(m_display_bitcoin_unit); }
QString getThirdPartyTxUrls() const { return strThirdPartyTxUrls; }
bool getUseEmbeddedMonospacedFont() const { return m_use_embedded_monospaced_font; }
bool getCoinControlFeatures() const { return fCoinControlFeatures; }
@ -112,7 +113,7 @@ private:
bool fMinimizeToTray;
bool fMinimizeOnClose;
QString language;
int nDisplayUnit;
BitcoinUnit m_display_bitcoin_unit;
QString strThirdPartyTxUrls;
bool m_use_embedded_monospaced_font;
bool fCoinControlFeatures;