Bitcoin Core  24.99.0
P2P Digital Currency
bitcoinunits.h
Go to the documentation of this file.
1 // Copyright (c) 2011-2021 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 
32 class BitcoinUnits: public QAbstractListModel
33 {
34  Q_OBJECT
35 
36 public:
37  explicit BitcoinUnits(QObject *parent);
38 
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 
60 
62  static QList<Unit> availableUnits();
64  static QString longName(Unit unit);
66  static QString shortName(Unit unit);
68  static QString description(Unit unit);
70  static qint64 factor(Unit unit);
72  static int decimals(Unit unit);
74  static QString format(Unit unit, const CAmount& amount, bool plussign = false, SeparatorStyle separators = SeparatorStyle::STANDARD, bool justify = false);
76  static QString formatWithUnit(Unit unit, const CAmount& amount, bool plussign = false, SeparatorStyle separators = SeparatorStyle::STANDARD);
78  static QString formatHtmlWithUnit(Unit unit, const CAmount& amount, bool plussign = false, SeparatorStyle separators = SeparatorStyle::STANDARD);
80  static QString formatWithPrivacy(Unit unit, const CAmount& amount, SeparatorStyle separators, bool privacy);
82  static bool parse(Unit unit, const QString& value, CAmount* val_out);
84  static QString getAmountColumnTitle(Unit unit);
86 
90  enum RoleIndex {
92  UnitRole = Qt::UserRole
93  };
94  int rowCount(const QModelIndex &parent) const override;
95  QVariant data(const QModelIndex &index, int role) const override;
97 
98  static QString removeSpaces(QString text)
99  {
100  text.remove(' ');
101  text.remove(QChar(THIN_SP_CP));
102  return text;
103  }
104 
106  static CAmount maxMoney();
107 
108 private:
109  QList<Unit> unitlist;
110 };
112 
113 QDataStream& operator<<(QDataStream& out, const BitcoinUnit& unit);
114 QDataStream& operator>>(QDataStream& in, BitcoinUnit& unit);
115 
116 #endif // BITCOIN_QT_BITCOINUNITS_H
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
QDataStream & operator<<(QDataStream &out, const BitcoinUnit &unit)
BitcoinUnits::Unit BitcoinUnit
Definition: bitcoinunits.h:111
#define THIN_SP_CP
Definition: bitcoinunits.h:25
QDataStream & operator>>(QDataStream &in, BitcoinUnit &unit)
Bitcoin unit definitions.
Definition: bitcoinunits.h:33
@ UnitRole
Unit identifier.
Definition: bitcoinunits.h:92
int rowCount(const QModelIndex &parent) const override
static QString formatWithPrivacy(Unit unit, const CAmount &amount, SeparatorStyle separators, bool privacy)
Format as string (with unit) of fixed length to preserve privacy, if it is set.
static CAmount maxMoney()
Return maximum number of base units (Satoshis)
static QString format(Unit unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=SeparatorStyle::STANDARD, bool justify=false)
Format as string.
QVariant data(const QModelIndex &index, int role) const override
static QString formatHtmlWithUnit(Unit unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=SeparatorStyle::STANDARD)
Format as HTML string (with unit)
QList< Unit > unitlist
Definition: bitcoinunits.h:109
static QString removeSpaces(QString text)
Definition: bitcoinunits.h:98
static QString getAmountColumnTitle(Unit unit)
Gets title for amount column including current display unit if optionsModel reference available *‍/.
static QString shortName(Unit unit)
Short name.
static QList< Unit > availableUnits()
Get list of units, for drop-down box.
static QString longName(Unit unit)
Long name.
static QString formatWithUnit(Unit unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=SeparatorStyle::STANDARD)
Format as string (with unit)
static qint64 factor(Unit unit)
Number of Satoshis (1e-8) per unit.
static bool parse(Unit unit, const QString &value, CAmount *val_out)
Parse string to coin amount.
static QString description(Unit unit)
Longer description.
Unit
Bitcoin units.
Definition: bitcoinunits.h:42
BitcoinUnits(QObject *parent)
static int decimals(Unit unit)
Number of decimals left.