@@ -18,94 +18,75 @@ BitcoinUnits::BitcoinUnits(QObject *parent):
1818{
1919}
2020
21- QList<BitcoinUnits::Unit > BitcoinUnits::availableUnits ()
21+ QList<BitcoinUnit > BitcoinUnits::availableUnits ()
2222{
23- QList<BitcoinUnits::Unit > unitlist;
24- unitlist.append (BTC);
25- unitlist.append (mBTC );
26- unitlist.append (uBTC);
27- unitlist.append (SAT);
23+ QList<BitcoinUnit > unitlist;
24+ unitlist.append (Unit:: BTC);
25+ unitlist.append (Unit:: mBTC );
26+ unitlist.append (Unit:: uBTC);
27+ unitlist.append (Unit:: SAT);
2828 return unitlist;
2929}
3030
31- bool BitcoinUnits::valid ( int unit)
31+ QString BitcoinUnits::longName (Unit unit)
3232{
33- switch (unit)
34- {
35- case BTC:
36- case mBTC :
37- case uBTC:
38- case SAT:
39- return true ;
40- default :
41- return false ;
42- }
43- }
44-
45- QString BitcoinUnits::longName (int unit)
46- {
47- switch (unit)
48- {
49- case BTC: return QString (" BTC" );
50- case mBTC : return QString (" mBTC" );
51- case uBTC: return QString::fromUtf8 (" µBTC (bits)" );
52- case SAT: return QString (" Satoshi (sat)" );
53- default : return QString (" ???" );
54- }
33+ switch (unit) {
34+ case Unit::BTC: return QString (" BTC" );
35+ case Unit::mBTC : return QString (" mBTC" );
36+ case Unit::uBTC: return QString::fromUtf8 (" µBTC (bits)" );
37+ case Unit::SAT: return QString (" Satoshi (sat)" );
38+ } // no default case, so the compiler can warn about missing cases
39+ assert (false );
5540}
5641
57- QString BitcoinUnits::shortName (int unit)
42+ QString BitcoinUnits::shortName (Unit unit)
5843{
59- switch (unit)
60- {
61- case uBTC: return QString::fromUtf8 (" bits" );
62- case SAT: return QString (" sat" );
63- default : return longName (unit);
64- }
44+ switch (unit) {
45+ case Unit::BTC: return longName (unit);
46+ case Unit::mBTC : return longName (unit);
47+ case Unit::uBTC: return QString (" bits" );
48+ case Unit::SAT: return QString (" sat" );
49+ } // no default case, so the compiler can warn about missing cases
50+ assert (false );
6551}
6652
67- QString BitcoinUnits::description (int unit)
53+ QString BitcoinUnits::description (Unit unit)
6854{
69- switch (unit)
70- {
71- case BTC: return QString (" Bitcoins" );
72- case mBTC : return QString (" Milli-Bitcoins (1 / 1" THIN_SP_UTF8 " 000)" );
73- case uBTC: return QString (" Micro-Bitcoins (bits) (1 / 1" THIN_SP_UTF8 " 000" THIN_SP_UTF8 " 000)" );
74- case SAT: return QString (" Satoshi (sat) (1 / 100" THIN_SP_UTF8 " 000" THIN_SP_UTF8 " 000)" );
75- default : return QString (" ???" );
76- }
55+ switch (unit) {
56+ case Unit::BTC: return QString (" Bitcoins" );
57+ case Unit::mBTC : return QString (" Milli-Bitcoins (1 / 1" THIN_SP_UTF8 " 000)" );
58+ case Unit::uBTC: return QString (" Micro-Bitcoins (bits) (1 / 1" THIN_SP_UTF8 " 000" THIN_SP_UTF8 " 000)" );
59+ case Unit::SAT: return QString (" Satoshi (sat) (1 / 100" THIN_SP_UTF8 " 000" THIN_SP_UTF8 " 000)" );
60+ } // no default case, so the compiler can warn about missing cases
61+ assert (false );
7762}
7863
79- qint64 BitcoinUnits::factor (int unit)
64+ qint64 BitcoinUnits::factor (Unit unit)
8065{
81- switch (unit)
82- {
83- case BTC: return 100000000 ;
84- case mBTC : return 100000 ;
85- case uBTC: return 100 ;
86- case SAT: return 1 ;
87- default : return 100000000 ;
88- }
66+ switch (unit) {
67+ case Unit::BTC: return 100'000'000 ;
68+ case Unit::mBTC : return 100'000 ;
69+ case Unit::uBTC: return 100 ;
70+ case Unit::SAT: return 1 ;
71+ } // no default case, so the compiler can warn about missing cases
72+ assert (false );
8973}
9074
91- int BitcoinUnits::decimals (int unit)
75+ int BitcoinUnits::decimals (Unit unit)
9276{
93- switch (unit)
94- {
95- case BTC: return 8 ;
96- case mBTC : return 5 ;
97- case uBTC: return 2 ;
98- case SAT: return 0 ;
99- default : return 0 ;
100- }
77+ switch (unit) {
78+ case Unit::BTC: return 8 ;
79+ case Unit::mBTC : return 5 ;
80+ case Unit::uBTC: return 2 ;
81+ case Unit::SAT: return 0 ;
82+ } // no default case, so the compiler can warn about missing cases
83+ assert (false );
10184}
10285
103- QString BitcoinUnits::format (int unit, const CAmount& nIn, bool fPlus , SeparatorStyle separators, bool justify)
86+ QString BitcoinUnits::format (Unit unit, const CAmount& nIn, bool fPlus , SeparatorStyle separators, bool justify)
10487{
10588 // Note: not using straight sprintf here because we do NOT want
10689 // localized number formatting.
107- if (!valid (unit))
108- return QString (); // Refuse to format invalid unit
10990 qint64 n = (qint64)nIn;
11091 qint64 coin = factor (unit);
11192 int num_decimals = decimals (unit);
@@ -147,19 +128,19 @@ QString BitcoinUnits::format(int unit, const CAmount& nIn, bool fPlus, Separator
147128// Please take care to use formatHtmlWithUnit instead, when
148129// appropriate.
149130
150- QString BitcoinUnits::formatWithUnit (int unit, const CAmount& amount, bool plussign, SeparatorStyle separators)
131+ QString BitcoinUnits::formatWithUnit (Unit unit, const CAmount& amount, bool plussign, SeparatorStyle separators)
151132{
152133 return format (unit, amount, plussign, separators) + QString (" " ) + shortName (unit);
153134}
154135
155- QString BitcoinUnits::formatHtmlWithUnit (int unit, const CAmount& amount, bool plussign, SeparatorStyle separators)
136+ QString BitcoinUnits::formatHtmlWithUnit (Unit unit, const CAmount& amount, bool plussign, SeparatorStyle separators)
156137{
157138 QString str (formatWithUnit (unit, amount, plussign, separators));
158139 str.replace (QChar (THIN_SP_CP), QString (THIN_SP_HTML));
159140 return QString (" <span style='white-space: nowrap;'>%1</span>" ).arg (str);
160141}
161142
162- QString BitcoinUnits::formatWithPrivacy (int unit, const CAmount& amount, SeparatorStyle separators, bool privacy)
143+ QString BitcoinUnits::formatWithPrivacy (Unit unit, const CAmount& amount, SeparatorStyle separators, bool privacy)
163144{
164145 assert (amount >= 0 );
165146 QString value;
@@ -171,10 +152,11 @@ QString BitcoinUnits::formatWithPrivacy(int unit, const CAmount& amount, Separat
171152 return value + QString (" " ) + shortName (unit);
172153}
173154
174- bool BitcoinUnits::parse (int unit, const QString & value, CAmount * val_out)
155+ bool BitcoinUnits::parse (Unit unit, const QString& value, CAmount* val_out)
175156{
176- if (! valid (unit) || value.isEmpty ())
157+ if ( value.isEmpty ()) {
177158 return false ; // Refuse to parse invalid unit or empty string
159+ }
178160 int num_decimals = decimals (unit);
179161
180162 // Ignore spaces and thin spaces when parsing
@@ -210,14 +192,9 @@ bool BitcoinUnits::parse(int unit, const QString &value, CAmount *val_out)
210192 return ok;
211193}
212194
213- QString BitcoinUnits::getAmountColumnTitle (int unit)
195+ QString BitcoinUnits::getAmountColumnTitle (Unit unit)
214196{
215- QString amountTitle = QObject::tr (" Amount" );
216- if (BitcoinUnits::valid (unit))
217- {
218- amountTitle += " (" +BitcoinUnits::shortName (unit) + " )" ;
219- }
220- return amountTitle;
197+ return QObject::tr (" Amount" ) + " (" + shortName (unit) + " )" ;
221198}
222199
223200int BitcoinUnits::rowCount (const QModelIndex &parent) const
@@ -240,7 +217,7 @@ QVariant BitcoinUnits::data(const QModelIndex &index, int role) const
240217 case Qt::ToolTipRole:
241218 return QVariant (description (unit));
242219 case UnitRole:
243- return QVariant ( static_cast < int >( unit) );
220+ return QVariant::fromValue ( unit);
244221 }
245222 }
246223 return QVariant ();
@@ -250,3 +227,40 @@ CAmount BitcoinUnits::maxMoney()
250227{
251228 return MAX_MONEY;
252229}
230+
231+ namespace {
232+ qint8 ToQint8 (BitcoinUnit unit)
233+ {
234+ switch (unit) {
235+ case BitcoinUnit::BTC: return 0 ;
236+ case BitcoinUnit::mBTC : return 1 ;
237+ case BitcoinUnit::uBTC: return 2 ;
238+ case BitcoinUnit::SAT: return 3 ;
239+ } // no default case, so the compiler can warn about missing cases
240+ assert (false );
241+ }
242+
243+ BitcoinUnit FromQint8 (qint8 num)
244+ {
245+ switch (num) {
246+ case 0 : return BitcoinUnit::BTC;
247+ case 1 : return BitcoinUnit::mBTC ;
248+ case 2 : return BitcoinUnit::uBTC;
249+ case 3 : return BitcoinUnit::SAT;
250+ }
251+ assert (false );
252+ }
253+ } // namespace
254+
255+ QDataStream& operator <<(QDataStream& out, const BitcoinUnit& unit)
256+ {
257+ return out << ToQint8 (unit);
258+ }
259+
260+ QDataStream& operator >>(QDataStream& in, BitcoinUnit& unit)
261+ {
262+ qint8 input;
263+ in >> input;
264+ unit = FromQint8 (input);
265+ return in;
266+ }
0 commit comments