You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

starwelsunits.h 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright (c) 2011-2016 The Starwels developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #ifndef STARWELS_QT_STARWELSUNITS_H
  5. #define STARWELS_QT_STARWELSUNITS_H
  6. #include "amount.h"
  7. #include <QAbstractListModel>
  8. #include <QString>
  9. // U+2009 THIN SPACE = UTF-8 E2 80 89
  10. #define REAL_THIN_SP_CP 0x2009
  11. #define REAL_THIN_SP_UTF8 "\xE2\x80\x89"
  12. #define REAL_THIN_SP_HTML "&thinsp;"
  13. // U+200A HAIR SPACE = UTF-8 E2 80 8A
  14. #define HAIR_SP_CP 0x200A
  15. #define HAIR_SP_UTF8 "\xE2\x80\x8A"
  16. #define HAIR_SP_HTML "&#8202;"
  17. // U+2006 SIX-PER-EM SPACE = UTF-8 E2 80 86
  18. #define SIXPEREM_SP_CP 0x2006
  19. #define SIXPEREM_SP_UTF8 "\xE2\x80\x86"
  20. #define SIXPEREM_SP_HTML "&#8198;"
  21. // U+2007 FIGURE SPACE = UTF-8 E2 80 87
  22. #define FIGURE_SP_CP 0x2007
  23. #define FIGURE_SP_UTF8 "\xE2\x80\x87"
  24. #define FIGURE_SP_HTML "&#8199;"
  25. // QMessageBox seems to have a bug whereby it doesn't display thin/hair spaces
  26. // correctly. Workaround is to display a space in a small font. If you
  27. // change this, please test that it doesn't cause the parent span to start
  28. // wrapping.
  29. #define HTML_HACK_SP "<span style='white-space: nowrap; font-size: 6pt'> </span>"
  30. // Define THIN_SP_* variables to be our preferred type of thin space
  31. #define THIN_SP_CP REAL_THIN_SP_CP
  32. #define THIN_SP_UTF8 REAL_THIN_SP_UTF8
  33. #define THIN_SP_HTML HTML_HACK_SP
  34. /** Starwels unit definitions. Encapsulates parsing and formatting
  35. and serves as list model for drop-down selection boxes.
  36. */
  37. class StarwelsUnits: public QAbstractListModel
  38. {
  39. Q_OBJECT
  40. public:
  41. explicit StarwelsUnits(QObject *parent);
  42. /** Starwels units.
  43. @note Source: https://en.bitcoin.it/wiki/Units . Please add only sensible ones
  44. */
  45. enum Unit
  46. {
  47. MAI,
  48. mMAI,
  49. uMAI
  50. };
  51. enum SeparatorStyle
  52. {
  53. separatorNever,
  54. separatorStandard,
  55. separatorAlways
  56. };
  57. //! @name Static API
  58. //! Unit conversion and formatting
  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. //! Short name
  65. static QString name(int unit);
  66. //! Longer description
  67. static QString description(int unit);
  68. //! Number of Satoshis (1e-8) per unit
  69. static qint64 factor(int unit);
  70. //! Number of decimals left
  71. static int decimals(int unit);
  72. //! Format as string
  73. static QString format(int unit, const CAmount& amount, bool plussign=false, SeparatorStyle separators=separatorStandard);
  74. //! Format as string (with unit)
  75. static QString formatWithUnit(int unit, const CAmount& amount, bool plussign=false, SeparatorStyle separators=separatorStandard);
  76. //! Format as HTML string (with unit)
  77. static QString formatHtmlWithUnit(int unit, const CAmount& amount, bool plussign=false, SeparatorStyle separators=separatorStandard);
  78. //! Parse string to coin amount
  79. static bool parse(int unit, const QString &value, CAmount *val_out);
  80. //! Gets title for amount column including current display unit if optionsModel reference available */
  81. static QString getAmountColumnTitle(int unit);
  82. ///@}
  83. //! @name AbstractListModel implementation
  84. //! List model for unit drop-down selection box.
  85. ///@{
  86. enum RoleIndex {
  87. /** Unit identifier */
  88. UnitRole = Qt::UserRole
  89. };
  90. int rowCount(const QModelIndex &parent) const;
  91. QVariant data(const QModelIndex &index, int role) const;
  92. ///@}
  93. static QString removeSpaces(QString text)
  94. {
  95. text.remove(' ');
  96. text.remove(QChar(THIN_SP_CP));
  97. #if (THIN_SP_CP != REAL_THIN_SP_CP)
  98. text.remove(QChar(REAL_THIN_SP_CP));
  99. #endif
  100. return text;
  101. }
  102. //! Return maximum number of base units (Satoshis)
  103. static CAmount maxMoney();
  104. private:
  105. QList<StarwelsUnits::Unit> unitlist;
  106. };
  107. typedef StarwelsUnits::Unit StarwelsUnit;
  108. #endif // STARWELS_QT_STARWELSUNITS_H