Bitcoin Core  0.20.99
P2P Digital Currency
bitcoinunits.h
Go to the documentation of this file.
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 
31 class BitcoinUnits: public QAbstractListModel
32 {
33  Q_OBJECT
34 
35 public:
36  explicit BitcoinUnits(QObject *parent);
37 
41  enum Unit
42  {
43  BTC,
47  };
48 
50  {
54  };
55 
59 
61  static QList<Unit> availableUnits();
63  static bool valid(int unit);
65  static QString longName(int unit);
67  static QString shortName(int unit);
69  static QString description(int unit);
71  static qint64 factor(int unit);
73  static int decimals(int unit);
75  static QString format(int unit, const CAmount& amount, bool plussign=false, SeparatorStyle separators=separatorStandard);
77  static QString formatWithUnit(int unit, const CAmount& amount, bool plussign=false, SeparatorStyle separators=separatorStandard);
79  static QString formatHtmlWithUnit(int unit, const CAmount& amount, bool plussign=false, SeparatorStyle separators=separatorStandard);
81  static bool parse(int unit, const QString &value, CAmount *val_out);
83  static QString getAmountColumnTitle(int unit);
85 
89  enum RoleIndex {
91  UnitRole = Qt::UserRole
92  };
93  int rowCount(const QModelIndex &parent) const override;
94  QVariant data(const QModelIndex &index, int role) const override;
96 
97  static QString removeSpaces(QString text)
98  {
99  text.remove(' ');
100  text.remove(QChar(THIN_SP_CP));
101  return text;
102  }
103 
105  static CAmount maxMoney();
106 
107 private:
108  QList<BitcoinUnits::Unit> unitlist;
109 };
111 
112 #endif // BITCOIN_QT_BITCOINUNITS_H
QList< BitcoinUnits::Unit > unitlist
Definition: bitcoinunits.h:108
Unit
Bitcoin units.
Definition: bitcoinunits.h:41
Bitcoin unit definitions.
Definition: bitcoinunits.h:31
BitcoinUnits::Unit BitcoinUnit
Definition: bitcoinunits.h:110
static QString formatHtmlWithUnit(int unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=separatorStandard)
Format as HTML string (with unit)
static bool valid(int unit)
Is unit ID valid?
static QString longName(int unit)
Long name.
static bool parse(int unit, const QString &value, CAmount *val_out)
Parse string to coin amount.
static QString getAmountColumnTitle(int unit)
Gets title for amount column including current display unit if optionsModel reference available */...
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
BitcoinUnits(QObject *parent)
Definition: bitcoinunits.cpp:9
static QString description(int unit)
Longer description.
static QString shortName(int unit)
Short name.
static qint64 factor(int unit)
Number of Satoshis (1e-8) per unit.
Unit identifier.
Definition: bitcoinunits.h:91
static QList< Unit > availableUnits()
Get list of units, for drop-down box.
int rowCount(const QModelIndex &parent) const override
static QString removeSpaces(QString text)
Definition: bitcoinunits.h:97
QVariant data(const QModelIndex &index, int role) const override
static QString formatWithUnit(int unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=separatorStandard)
Format as string (with unit)
#define THIN_SP_CP
Definition: bitcoinunits.h:24
static QString format(int unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=separatorStandard)
Format as string.
static CAmount maxMoney()
Return maximum number of base units (Satoshis)
static int decimals(int unit)
Number of decimals left.