optionsmodel.h raw

   1  // Copyright (c) 2011-present 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_OPTIONSMODEL_H
   6  #define BITCOIN_QT_OPTIONSMODEL_H
   7  
   8  #include <cstdint>
   9  #include <qt/bitcoinunits.h>
  10  #include <qt/guiconstants.h>
  11  #include <util/byte_units.h>
  12  
  13  #include <QAbstractListModel>
  14  #include <QFont>
  15  
  16  #include <cassert>
  17  #include <variant>
  18  
  19  struct bilingual_str;
  20  namespace interfaces {
  21  class Node;
  22  }
  23  
  24  extern const char *DEFAULT_GUI_PROXY_HOST;
  25  static constexpr uint16_t DEFAULT_GUI_PROXY_PORT = 9050;
  26  
  27  /**
  28   * Convert configured prune target MiB to displayed GB. Round up to avoid underestimating max disk usage.
  29   */
  30  static inline int PruneMiBtoGB(int64_t mib) { return (mib * 1_MiB + GB_BYTES - 1) / GB_BYTES; }
  31  
  32  /**
  33   * Convert displayed prune target GB to configured MiB. Round down so roundtrip GB -> MiB -> GB conversion is stable.
  34   */
  35  static inline int64_t PruneGBtoMiB(int gb) { return gb * GB_BYTES / 1_MiB; }
  36  
  37  /** Interface from Qt to configuration data structure for Bitcoin client.
  38     To Qt, the options are presented as a list with the different options
  39     laid out vertically.
  40     This can be changed to a tree once the settings become sufficiently
  41     complex.
  42   */
  43  class OptionsModel : public QAbstractListModel
  44  {
  45      Q_OBJECT
  46  
  47  public:
  48      explicit OptionsModel(interfaces::Node& node, QObject *parent = nullptr);
  49  
  50      enum OptionID {
  51          StartAtStartup,         // bool
  52          ShowTrayIcon,           // bool
  53          MinimizeToTray,         // bool
  54          MapPortNatpmp,          // bool
  55          MinimizeOnClose,        // bool
  56          ProxyUse,               // bool
  57          ProxyIP,                // QString
  58          ProxyPort,              // int
  59          ProxyUseTor,            // bool
  60          ProxyIPTor,             // QString
  61          ProxyPortTor,           // int
  62          DisplayUnit,            // BitcoinUnit
  63          ThirdPartyTxUrls,       // QString
  64          Language,               // QString
  65          FontForMoney,           // FontChoice
  66          CoinControlFeatures,    // bool
  67          SubFeeFromAmount,       // bool
  68          ThreadsScriptVerif,     // int
  69          Prune,                  // bool
  70          PruneSize,              // int
  71          DatabaseCache,          // int
  72          ExternalSignerPath,     // QString
  73          SpendZeroConfChange,    // bool
  74          Listen,                 // bool
  75          Server,                 // bool
  76          EnablePSBTControls,     // bool
  77          MaskValues,             // bool
  78          OptionIDRowCount,
  79      };
  80  
  81      enum class FontChoiceAbstract {
  82          EmbeddedFont,
  83          BestSystemFont,
  84      };
  85      typedef std::variant<FontChoiceAbstract, QFont> FontChoice;
  86      static inline const FontChoice UseBestSystemFont{FontChoiceAbstract::BestSystemFont};
  87      static QFont getFontForChoice(const FontChoice& fc);
  88  
  89      bool Init(bilingual_str& error);
  90      void Reset();
  91  
  92      int rowCount(const QModelIndex & parent = QModelIndex()) const override;
  93      QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const override;
  94      bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole) override;
  95      QVariant getOption(OptionID option, const std::string& suffix="") const;
  96      bool setOption(OptionID option, const QVariant& value, const std::string& suffix="");
  97      /** Updates current unit in memory, settings and emits displayUnitChanged(new_unit) signal */
  98      void setDisplayUnit(const QVariant& new_unit);
  99  
 100      /* Explicit getters */
 101      bool getShowTrayIcon() const { return m_show_tray_icon; }
 102      bool getMinimizeToTray() const { return fMinimizeToTray; }
 103      bool getMinimizeOnClose() const { return fMinimizeOnClose; }
 104      BitcoinUnit getDisplayUnit() const { return m_display_bitcoin_unit; }
 105      QString getThirdPartyTxUrls() const { return strThirdPartyTxUrls; }
 106      QFont getFontForMoney() const;
 107      bool getCoinControlFeatures() const { return fCoinControlFeatures; }
 108      bool getSubFeeFromAmount() const { return m_sub_fee_from_amount; }
 109      bool getEnablePSBTControls() const { return m_enable_psbt_controls; }
 110      const QString& getOverriddenByCommandLine() { return strOverriddenByCommandLine; }
 111  
 112      /** Whether -signer was set or not */
 113      bool hasSigner();
 114  
 115      /* Explicit setters */
 116      void SetPruneTargetGB(int prune_target_gb);
 117  
 118      /* Restart flag helper */
 119      void setRestartRequired(bool fRequired);
 120      bool isRestartRequired() const;
 121  
 122      interfaces::Node& node() const { return m_node; }
 123  
 124  private:
 125      interfaces::Node& m_node;
 126      /* Qt-only settings */
 127      bool m_show_tray_icon;
 128      bool fMinimizeToTray;
 129      bool fMinimizeOnClose;
 130      QString language;
 131      BitcoinUnit m_display_bitcoin_unit;
 132      QString strThirdPartyTxUrls;
 133      FontChoice m_font_money{FontChoiceAbstract::EmbeddedFont};
 134      bool fCoinControlFeatures;
 135      bool m_sub_fee_from_amount;
 136      bool m_enable_psbt_controls;
 137      bool m_mask_values;
 138  
 139      /* settings that were overridden by command-line */
 140      QString strOverriddenByCommandLine;
 141  
 142      static QString FontChoiceToString(const OptionsModel::FontChoice&);
 143      static FontChoice FontChoiceFromString(const QString&);
 144  
 145      // Add option to list of GUI options overridden through command line/config file
 146      void addOverriddenOption(const std::string &option);
 147  
 148      // Check settings version and upgrade default values if required
 149      void checkAndMigrate();
 150  
 151  Q_SIGNALS:
 152      void displayUnitChanged(BitcoinUnit unit);
 153      void coinControlFeaturesChanged(bool);
 154      void showTrayIconChanged(bool);
 155      void fontForMoneyChanged(const QFont&);
 156  };
 157  
 158  Q_DECLARE_METATYPE(OptionsModel::FontChoice)
 159  
 160  #endif // BITCOIN_QT_OPTIONSMODEL_H
 161