Remove unused variable int64_t nEnd
[bitcoinplatinum.git] / src / qt / bitcoinamountfield.cpp
blob73eb35a54e56b78e3c2a9953c6858e9110f78708
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.
5 #include "bitcoinamountfield.h"
7 #include "bitcoinunits.h"
8 #include "guiconstants.h"
9 #include "qvaluecombobox.h"
11 #include <QApplication>
12 #include <QAbstractSpinBox>
13 #include <QHBoxLayout>
14 #include <QKeyEvent>
15 #include <QLineEdit>
17 /** QSpinBox that uses fixed-point numbers internally and uses our own
18 * formatting/parsing functions.
20 class AmountSpinBox: public QAbstractSpinBox
22 Q_OBJECT
24 public:
25 explicit AmountSpinBox(QWidget *parent):
26 QAbstractSpinBox(parent),
27 currentUnit(BitcoinUnits::BTC),
28 singleStep(100000) // satoshis
30 setAlignment(Qt::AlignRight);
32 connect(lineEdit(), SIGNAL(textEdited(QString)), this, SIGNAL(valueChanged()));
35 QValidator::State validate(QString &text, int &pos) const
37 if(text.isEmpty())
38 return QValidator::Intermediate;
39 bool valid = false;
40 parse(text, &valid);
41 /* Make sure we return Intermediate so that fixup() is called on defocus */
42 return valid ? QValidator::Intermediate : QValidator::Invalid;
45 void fixup(QString &input) const
47 bool valid = false;
48 CAmount val = parse(input, &valid);
49 if(valid)
51 input = BitcoinUnits::format(currentUnit, val, false, BitcoinUnits::separatorAlways);
52 lineEdit()->setText(input);
56 CAmount value(bool *valid_out=0) const
58 return parse(text(), valid_out);
61 void setValue(const CAmount& value)
63 lineEdit()->setText(BitcoinUnits::format(currentUnit, value, false, BitcoinUnits::separatorAlways));
64 Q_EMIT valueChanged();
67 void stepBy(int steps)
69 bool valid = false;
70 CAmount val = value(&valid);
71 val = val + steps * singleStep;
72 val = qMin(qMax(val, CAmount(0)), BitcoinUnits::maxMoney());
73 setValue(val);
76 void setDisplayUnit(int unit)
78 bool valid = false;
79 CAmount val = value(&valid);
81 currentUnit = unit;
83 if(valid)
84 setValue(val);
85 else
86 clear();
89 void setSingleStep(const CAmount& step)
91 singleStep = step;
94 QSize minimumSizeHint() const
96 if(cachedMinimumSizeHint.isEmpty())
98 ensurePolished();
100 const QFontMetrics fm(fontMetrics());
101 int h = lineEdit()->minimumSizeHint().height();
102 int w = fm.width(BitcoinUnits::format(BitcoinUnits::BTC, BitcoinUnits::maxMoney(), false, BitcoinUnits::separatorAlways));
103 w += 2; // cursor blinking space
105 QStyleOptionSpinBox opt;
106 initStyleOption(&opt);
107 QSize hint(w, h);
108 QSize extra(35, 6);
109 opt.rect.setSize(hint + extra);
110 extra += hint - style()->subControlRect(QStyle::CC_SpinBox, &opt,
111 QStyle::SC_SpinBoxEditField, this).size();
112 // get closer to final result by repeating the calculation
113 opt.rect.setSize(hint + extra);
114 extra += hint - style()->subControlRect(QStyle::CC_SpinBox, &opt,
115 QStyle::SC_SpinBoxEditField, this).size();
116 hint += extra;
117 hint.setHeight(h);
119 opt.rect = rect();
121 cachedMinimumSizeHint = style()->sizeFromContents(QStyle::CT_SpinBox, &opt, hint, this)
122 .expandedTo(QApplication::globalStrut());
124 return cachedMinimumSizeHint;
127 private:
128 int currentUnit;
129 CAmount singleStep;
130 mutable QSize cachedMinimumSizeHint;
133 * Parse a string into a number of base monetary units and
134 * return validity.
135 * @note Must return 0 if !valid.
137 CAmount parse(const QString &text, bool *valid_out=0) const
139 CAmount val = 0;
140 bool valid = BitcoinUnits::parse(currentUnit, text, &val);
141 if(valid)
143 if(val < 0 || val > BitcoinUnits::maxMoney())
144 valid = false;
146 if(valid_out)
147 *valid_out = valid;
148 return valid ? val : 0;
151 protected:
152 bool event(QEvent *event)
154 if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease)
156 QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
157 if (keyEvent->key() == Qt::Key_Comma)
159 // Translate a comma into a period
160 QKeyEvent periodKeyEvent(event->type(), Qt::Key_Period, keyEvent->modifiers(), ".", keyEvent->isAutoRepeat(), keyEvent->count());
161 return QAbstractSpinBox::event(&periodKeyEvent);
164 return QAbstractSpinBox::event(event);
167 StepEnabled stepEnabled() const
169 if (isReadOnly()) // Disable steps when AmountSpinBox is read-only
170 return StepNone;
171 if (text().isEmpty()) // Allow step-up with empty field
172 return StepUpEnabled;
174 StepEnabled rv = 0;
175 bool valid = false;
176 CAmount val = value(&valid);
177 if(valid)
179 if(val > 0)
180 rv |= StepDownEnabled;
181 if(val < BitcoinUnits::maxMoney())
182 rv |= StepUpEnabled;
184 return rv;
187 Q_SIGNALS:
188 void valueChanged();
191 #include "bitcoinamountfield.moc"
193 BitcoinAmountField::BitcoinAmountField(QWidget *parent) :
194 QWidget(parent),
195 amount(0)
197 amount = new AmountSpinBox(this);
198 amount->setLocale(QLocale::c());
199 amount->installEventFilter(this);
200 amount->setMaximumWidth(170);
202 QHBoxLayout *layout = new QHBoxLayout(this);
203 layout->addWidget(amount);
204 unit = new QValueComboBox(this);
205 unit->setModel(new BitcoinUnits(this));
206 layout->addWidget(unit);
207 layout->addStretch(1);
208 layout->setContentsMargins(0,0,0,0);
210 setLayout(layout);
212 setFocusPolicy(Qt::TabFocus);
213 setFocusProxy(amount);
215 // If one if the widgets changes, the combined content changes as well
216 connect(amount, SIGNAL(valueChanged()), this, SIGNAL(valueChanged()));
217 connect(unit, SIGNAL(currentIndexChanged(int)), this, SLOT(unitChanged(int)));
219 // Set default based on configuration
220 unitChanged(unit->currentIndex());
223 void BitcoinAmountField::clear()
225 amount->clear();
226 unit->setCurrentIndex(0);
229 void BitcoinAmountField::setEnabled(bool fEnabled)
231 amount->setEnabled(fEnabled);
232 unit->setEnabled(fEnabled);
235 bool BitcoinAmountField::validate()
237 bool valid = false;
238 value(&valid);
239 setValid(valid);
240 return valid;
243 void BitcoinAmountField::setValid(bool valid)
245 if (valid)
246 amount->setStyleSheet("");
247 else
248 amount->setStyleSheet(STYLE_INVALID);
251 bool BitcoinAmountField::eventFilter(QObject *object, QEvent *event)
253 if (event->type() == QEvent::FocusIn)
255 // Clear invalid flag on focus
256 setValid(true);
258 return QWidget::eventFilter(object, event);
261 QWidget *BitcoinAmountField::setupTabChain(QWidget *prev)
263 QWidget::setTabOrder(prev, amount);
264 QWidget::setTabOrder(amount, unit);
265 return unit;
268 CAmount BitcoinAmountField::value(bool *valid_out) const
270 return amount->value(valid_out);
273 void BitcoinAmountField::setValue(const CAmount& value)
275 amount->setValue(value);
278 void BitcoinAmountField::setReadOnly(bool fReadOnly)
280 amount->setReadOnly(fReadOnly);
283 void BitcoinAmountField::unitChanged(int idx)
285 // Use description tooltip for current unit for the combobox
286 unit->setToolTip(unit->itemData(idx, Qt::ToolTipRole).toString());
288 // Determine new unit ID
289 int newUnit = unit->itemData(idx, BitcoinUnits::UnitRole).toInt();
291 amount->setDisplayUnit(newUnit);
294 void BitcoinAmountField::setDisplayUnit(int newUnit)
296 unit->setValue(newUnit);
299 void BitcoinAmountField::setSingleStep(const CAmount& step)
301 amount->setSingleStep(step);