Bitcoin ABC  0.24.7
P2P Digital Currency
bitcoinunits.h
Go to the documentation of this file.
1 // Copyright (c) 2011-2016 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 change
19 // this, please test that it doesn't cause the parent span to start wrapping.
20 #define HTML_HACK_SP \
21  "<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 
32 class BitcoinUnits : public QAbstractListModel {
33  Q_OBJECT
34 
35 public:
36  explicit BitcoinUnits(QObject *parent);
37 
42  enum Unit { base, sub };
43 
44  enum class SeparatorStyle { NEVER, STANDARD, ALWAYS };
45 
49 
51  static QList<Unit> availableUnits();
53  static bool valid(int unit);
55  static QString longName(int unit);
57  static QString shortName(int unit);
59  static QString description(int unit);
61  static Amount factor(int unit);
63  static int decimals(int unit);
65  static QString format(int unit, const Amount amount, bool plussign = false,
67  bool justify = false);
69  static QString
70  formatWithUnit(int unit, const Amount amount, bool plussign = false,
73  static QString
74  formatHtmlWithUnit(int unit, const Amount amount, bool plussign = false,
78  static QString formatWithPrivacy(int unit, const Amount &amount,
79  SeparatorStyle separators, bool privacy);
81  static bool parse(int unit, const QString &value, Amount *val_out);
84  static QString getAmountColumnTitle(int 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  text.remove(' ');
100  text.remove(QChar(THIN_SP_CP));
101  return text;
102  }
103 
105  static Amount maxMoney();
106 
107 private:
108  QList<BitcoinUnits::Unit> unitlist;
109 };
110 
111 #endif // BITCOIN_QT_BITCOINUNITS_H
BitcoinUnits::RoleIndex
RoleIndex
Definition: bitcoinunits.h:90
BitcoinUnits::formatHtmlWithUnit
static QString formatHtmlWithUnit(int unit, const Amount amount, bool plussign=false, SeparatorStyle separators=SeparatorStyle::STANDARD)
Format as HTML string (with unit)
Definition: bitcoinunits.cpp:167
BitcoinUnits::sub
@ sub
Definition: bitcoinunits.h:42
BitcoinUnits::SeparatorStyle::NEVER
@ NEVER
BitcoinUnits::maxMoney
static Amount maxMoney()
Return maximum number of base units (Satoshis)
Definition: bitcoinunits.cpp:257
BitcoinUnits::decimals
static int decimals(int unit)
Number of decimals left.
Definition: bitcoinunits.cpp:97
BitcoinUnits::parse
static bool parse(int unit, const QString &value, Amount *val_out)
Parse string to coin amount.
Definition: bitcoinunits.cpp:189
BitcoinUnits::format
static QString format(int unit, const Amount amount, bool plussign=false, SeparatorStyle separators=SeparatorStyle::STANDARD, bool justify=false)
Format as string.
Definition: bitcoinunits.cpp:108
BitcoinUnits::valid
static bool valid(int unit)
Is unit ID valid?
Definition: bitcoinunits.cpp:56
BitcoinUnits::description
static QString description(int unit)
Longer description.
Definition: bitcoinunits.cpp:79
BitcoinUnits::removeSpaces
static QString removeSpaces(QString text)
Definition: bitcoinunits.h:98
BitcoinUnits::availableUnits
static QList< Unit > availableUnits()
Get list of units, for drop-down box.
Definition: bitcoinunits.cpp:49
THIN_SP_CP
#define THIN_SP_CP
Definition: bitcoinunits.h:24
BitcoinUnits::unitlist
QList< BitcoinUnits::Unit > unitlist
Definition: bitcoinunits.h:108
BitcoinUnits
Bitcoin unit definitions.
Definition: bitcoinunits.h:32
BitcoinUnits::formatWithUnit
static QString formatWithUnit(int unit, const Amount amount, bool plussign=false, SeparatorStyle separators=SeparatorStyle::STANDARD)
Format as string (with unit)
Definition: bitcoinunits.cpp:161
BitcoinUnits::SeparatorStyle
SeparatorStyle
Definition: bitcoinunits.h:44
BitcoinUnits::longName
static QString longName(int unit)
Long name.
Definition: bitcoinunits.cpp:66
BitcoinUnits::factor
static Amount factor(int unit)
Number of Satoshis (1e-8) per unit.
Definition: bitcoinunits.cpp:85
Amount
Definition: amount.h:19
BitcoinUnits::rowCount
int rowCount(const QModelIndex &parent) const override
Definition: bitcoinunits.cpp:235
BitcoinUnits::data
QVariant data(const QModelIndex &index, int role) const override
Definition: bitcoinunits.cpp:240
BitcoinUnits::SeparatorStyle::ALWAYS
@ ALWAYS
BitcoinUnits::formatWithPrivacy
static QString formatWithPrivacy(int unit, const Amount &amount, SeparatorStyle separators, bool privacy)
Format as string (with unit) of fixed length to preserve privacy, if it is set.
Definition: bitcoinunits.cpp:175
BitcoinUnits::getAmountColumnTitle
static QString getAmountColumnTitle(int unit)
Gets title for amount column including current display unit if optionsModel reference available *‍/.
Definition: bitcoinunits.cpp:227
BitcoinUnits::Unit
Unit
Currency units Please add only sensible ones.
Definition: bitcoinunits.h:42
BitcoinUnits::shortName
static QString shortName(int unit)
Short name.
Definition: bitcoinunits.cpp:72
BitcoinUnits::base
@ base
Definition: bitcoinunits.h:42
BitcoinUnits::BitcoinUnits
BitcoinUnits(QObject *parent)
Definition: bitcoinunits.cpp:46
amount.h
BitcoinUnits::UnitRole
@ UnitRole
Unit identifier.
Definition: bitcoinunits.h:92
BitcoinUnits::SeparatorStyle::STANDARD
@ STANDARD