Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

bitcoinamountfield.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2015 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 
6 
7 #include <qt/bitcoinunits.h>
8 #include <qt/guiutil.h>
9 
10 #include <QApplication>
11 #include <QAbstractSpinBox>
12 #include <QHBoxLayout>
13 #include <QKeyEvent>
14 #include <QLineEdit>
15 
21 static CAmount parse(const QString &text, int nUnit, bool *valid_out=0)
22 {
23  CAmount val = 0;
24  bool valid = BitcoinUnits::parse(nUnit, text, &val);
25  if(valid)
26  {
27  if(val < 0 || val > BitcoinUnits::maxMoney())
28  valid = false;
29  }
30  if(valid_out)
31  *valid_out = valid;
32  return valid ? val : 0;
33 }
34 
37 class AmountValidator : public QValidator
38 {
39  Q_OBJECT
41 public:
42  explicit AmountValidator(QObject *parent) :
43  QValidator(parent),
44  currentUnit(BitcoinUnits::DASH) {}
45 
46  State validate(QString &input, int &pos) const
47  {
48  if(input.isEmpty())
49  return QValidator::Intermediate;
50  bool valid = false;
51  parse(input, currentUnit, &valid);
52  /* Make sure we return Intermediate so that fixup() is called on defocus */
53  return valid ? QValidator::Intermediate : QValidator::Invalid;
54  }
55 
56  void updateUnit(int nUnit)
57  {
58  currentUnit = nUnit;
59  }
60 };
61 
65 class AmountLineEdit: public QLineEdit
66 {
67  Q_OBJECT
69 public:
70  explicit AmountLineEdit(QWidget *parent):
71  QLineEdit(parent),
73  {
74  setAlignment(Qt::AlignLeft);
75  amountValidator = new AmountValidator(this);
76  setValidator(amountValidator);
77  connect(this, SIGNAL(textEdited(QString)), this, SIGNAL(valueChanged()));
78  }
79 
80  void fixup(const QString &input)
81  {
82  bool valid = false;
83  CAmount val = parse(input, currentUnit, &valid);
84  if(valid)
85  {
87  }
88  }
89 
90  CAmount value(bool *valid_out=0) const
91  {
92  return parse(text(), currentUnit, valid_out);
93  }
94 
95  void setValue(const CAmount& value)
96  {
98  Q_EMIT valueChanged();
99  }
100 
101  void setDisplayUnit(int unit)
102  {
103  bool valid = false;
104  CAmount val = value(&valid);
105 
106  currentUnit = unit;
108 
109  if(valid)
110  setValue(val);
111  else
112  clear();
113  }
114 
115  QSize minimumSizeHint() const
116  {
117  ensurePolished();
118  const QFontMetrics fm(fontMetrics());
119  int h = 0;
121  w += 2; // cursor blinking space
122  w += GUIUtil::dashThemeActive() ? 24 : 0; // counteract padding from css
123  return QSize(w, h);
124  }
125 
126 private:
128 
129 protected:
130  bool event(QEvent *event)
131  {
132  if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease)
133  {
134  QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
135  if (keyEvent->key() == Qt::Key_Comma)
136  {
137  // Translate a comma into a period
138  QKeyEvent periodKeyEvent(event->type(), Qt::Key_Period, keyEvent->modifiers(), ".", keyEvent->isAutoRepeat(), keyEvent->count());
139  return QLineEdit::event(&periodKeyEvent);
140  }
141  if(keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return)
142  {
143  clearFocus();
144  }
145  }
146  if (event->type() == QEvent::FocusOut)
147  {
148  fixup(text());
149  }
150  return QLineEdit::event(event);
151  }
152 
153 Q_SIGNALS:
154  void valueChanged();
155 };
156 
157 #include <qt/bitcoinamountfield.moc>
158 
160  QWidget(parent),
161  amount(0)
162 {
163  amount = new AmountLineEdit(this);
164  amount->setLocale(QLocale::c());
165  amount->installEventFilter(this);
166  amount->setMaximumWidth(300);
167 
168  units = new BitcoinUnits(this);
169 
170  QHBoxLayout *layout = new QHBoxLayout(this);
171  layout->setSpacing(0);
172  layout->setMargin(0);
173  layout->addWidget(amount);
174 
175  setLayout(layout);
176 
177  setFocusPolicy(Qt::TabFocus);
178  setFocusProxy(amount);
179 
180  // If one if the widgets changes, the combined content changes as well
181  connect(amount, SIGNAL(valueChanged()), this, SIGNAL(valueChanged()));
182 }
183 
185 {
186  amount->clear();
187 }
188 
190 {
191  amount->setEnabled(fEnabled);
192 }
193 
195 {
196  bool valid = false;
197  value(&valid);
198  setValid(valid);
199  return valid;
200 }
201 
203 {
204  if (valid)
205  amount->setStyleSheet("");
206  else
208 }
209 
210 bool BitcoinAmountField::eventFilter(QObject *object, QEvent *event)
211 {
212  if (event->type() == QEvent::FocusIn)
213  {
214  // Clear invalid flag on focus
215  setValid(true);
216  }
217  return QWidget::eventFilter(object, event);
218 }
219 
220 QWidget *BitcoinAmountField::setupTabChain(QWidget *prev)
221 {
222  QWidget::setTabOrder(prev, amount);
223  return amount;
224 }
225 
226 CAmount BitcoinAmountField::value(bool *valid_out) const
227 {
228  return amount->value(valid_out);
229 }
230 
232 {
234 }
235 
237 {
238  amount->setReadOnly(fReadOnly);
239 }
240 
242 {
243  // Use description tooltip for current unit for the combobox
244  amount->setToolTip(units->data(idx, Qt::ToolTipRole).toString());
245 
246  // Determine new unit ID
247  int newUnit = units->data(idx, BitcoinUnits::UnitRole).toInt();
248 
249  amount->setPlaceholderText(tr("Amount in %1").arg(units->data(idx,Qt::DisplayRole).toString()));
250 
251  amount->setDisplayUnit(newUnit);
252 }
253 
255 {
256  unitChanged(newUnit);
257 }
bool validate()
Perform input validation, mark field as invalid if entered value is not valid.
Dash unit definitions.
Definition: bitcoinunits.h:48
void setDisplayUnit(int unit)
void setReadOnly(bool fReadOnly)
Make read-only.
bool dashThemeActive()
Check if a dash specific theme is activated (light/dark).
Definition: guiutil.cpp:1775
Amount widget validator, checks for valid CAmount value.
QWidget * setupTabChain(QWidget *prev)
Qt messes up the tab chain by default in some cases (issue https://bugreports.qt-project.org/browse/QTBUG-10907), in these cases we have to set it up manually.
CAmount value(bool *valid_out=0) const
bool eventFilter(QObject *object, QEvent *event)
Intercept focus-in event and &#39;,&#39; key presses.
AmountValidator * amountValidator
static bool parse(int unit, const QString &value, CAmount *val_out)
Parse string to coin amount.
QSize minimumSizeHint() const
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
BitcoinAmountField(QWidget *parent=0)
bool event(QEvent *event)
void setEnabled(bool fEnabled)
Enable/Disable.
static CAmount parse(const QString &text, int nUnit, bool *valid_out=0)
Parse a string into a number of base monetary units and return validity.
AmountLineEdit * amount
QLineEdit that uses fixed-point numbers internally and uses our own formatting/parsing functions...
Unit identifier.
Definition: bitcoinunits.h:110
void fixup(const QString &input)
AmountValidator(QObject *parent)
BitcoinUnits * units
void clear()
Make field empty and ready for new input.
QVariant data(const QModelIndex &index, int role) const
void valueChanged()
QString getThemedStyleQString(ThemedStyle style)
Helper to get css style strings which are injected into rich text through qt.
Definition: guiutil.cpp:210
State validate(QString &input, int &pos) const
void setValid(bool valid)
Mark current value as invalid in UI.
void updateUnit(int nUnit)
void setDisplayUnit(int unit)
Change unit used to display amount.
void setValue(const CAmount &value)
void setValue(const CAmount &value)
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)
AmountLineEdit(QWidget *parent)
Released under the MIT license