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