bitcoinunits.h raw
1 // Copyright (c) 2011-present The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5 #ifndef BITCOIN_QT_BITCOINUNITS_H
6 #define BITCOIN_QT_BITCOINUNITS_H
7
8 #include <consensus/amount.h>
9
10 #include <QAbstractListModel>
11 #include <QDataStream>
12 #include <QString>
13
14 // U+2009 THIN SPACE = UTF-8 E2 80 89
15 #define REAL_THIN_SP_CP 0x2009
16 #define REAL_THIN_SP_UTF8 "\xE2\x80\x89"
17
18 // QMessageBox seems to have a bug whereby it doesn't display thin/hair spaces
19 // correctly. Workaround is to display a space in a small font. If you
20 // change this, please test that it doesn't cause the parent span to start
21 // wrapping.
22 #define HTML_HACK_SP "<span style='white-space: nowrap; font-size: 6pt'> </span>"
23
24 // Define THIN_SP_* variables to be our preferred type of thin space
25 #define THIN_SP_CP REAL_THIN_SP_CP
26 #define THIN_SP_UTF8 REAL_THIN_SP_UTF8
27 #define THIN_SP_HTML HTML_HACK_SP
28
29 /** Bitcoin unit definitions. Encapsulates parsing and formatting
30 and serves as list model for drop-down selection boxes.
31 */
32 class BitcoinUnits: public QAbstractListModel
33 {
34 Q_OBJECT
35
36 public:
37 explicit BitcoinUnits(QObject *parent);
38
39 /** Bitcoin units.
40 @note Source: https://en.bitcoin.it/wiki/Units . Please add only sensible ones
41 */
42 enum class Unit {
43 BTC,
44 mBTC,
45 uBTC,
46 SAT
47 };
48 Q_ENUM(Unit)
49
50 enum class SeparatorStyle
51 {
52 NEVER,
53 STANDARD,
54 ALWAYS
55 };
56
57 //! @name Static API
58 //! Unit conversion and formatting
59 ///@{
60
61 //! Get list of units, for drop-down box
62 static QList<Unit> availableUnits();
63 //! Long name
64 static QString longName(Unit unit);
65 //! Short name
66 static QString shortName(Unit unit);
67 //! Longer description
68 static QString description(Unit unit);
69 //! Number of Satoshis (1e-8) per unit
70 static qint64 factor(Unit unit);
71 //! Number of decimals left
72 static int decimals(Unit unit);
73 //! Format as string
74 static QString format(Unit unit, const CAmount& amount, bool plussign = false, SeparatorStyle separators = SeparatorStyle::STANDARD, bool justify = false);
75 //! Format as string (with unit)
76 static QString formatWithUnit(Unit unit, const CAmount& amount, bool plussign = false, SeparatorStyle separators = SeparatorStyle::STANDARD);
77 //! Format as HTML string (with unit)
78 static QString formatHtmlWithUnit(Unit unit, const CAmount& amount, bool plussign = false, SeparatorStyle separators = SeparatorStyle::STANDARD);
79 //! Format as string (with unit) of fixed length to preserve privacy, if it is set.
80 static QString formatWithPrivacy(Unit unit, const CAmount& amount, SeparatorStyle separators, bool privacy);
81 //! Parse string to coin amount
82 static bool parse(Unit unit, const QString& value, CAmount* val_out);
83 //! Gets title for amount column including current display unit if optionsModel reference available */
84 static QString getAmountColumnTitle(Unit unit);
85 ///@}
86
87 //! @name AbstractListModel implementation
88 //! List model for unit drop-down selection box.
89 ///@{
90 enum RoleIndex {
91 /** Unit identifier */
92 UnitRole = Qt::UserRole
93 };
94 int rowCount(const QModelIndex &parent) const override;
95 QVariant data(const QModelIndex &index, int role) const override;
96 ///@}
97
98 static QString removeSpaces(QString text)
99 {
100 text.remove(' ');
101 text.remove(QChar(THIN_SP_CP));
102 return text;
103 }
104
105 //! Return maximum number of base units (Satoshis)
106 static CAmount maxMoney();
107
108 private:
109 QList<Unit> unitlist;
110 };
111 typedef BitcoinUnits::Unit BitcoinUnit;
112
113 QDataStream& operator<<(QDataStream& out, const BitcoinUnit& unit);
114 QDataStream& operator>>(QDataStream& in, BitcoinUnit& unit);
115
116 #endif // BITCOIN_QT_BITCOINUNITS_H
117