Files
pay/BitcoinValue.cpp
T

208 lines
5.4 KiB
C++
Raw Permalink Normal View History

2020-06-12 20:53:01 +02:00
/*
* This file is part of the Flowee project
* Copyright (C) 2020-2021 Tom Zander <tom@flowee.org>
2020-06-12 20:53:01 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "BitcoinValue.h"
#include "FloweePay.h"
2020-06-22 12:14:13 +02:00
#include <QClipboard>
#include <QGuiApplication>
2020-10-15 20:04:10 +02:00
BitcoinValue::BitcoinValue(QObject *parent)
: QObject(parent),
m_value(0)
2020-06-12 20:53:01 +02:00
{
connect (FloweePay::instance(), &FloweePay::unitChanged, this, [this]() {
2020-10-15 20:04:10 +02:00
setStringValue(m_typedNumber);
});
2020-06-12 20:53:01 +02:00
}
qint64 BitcoinValue::value() const
{
return m_value;
}
void BitcoinValue::setValue(qint64 value)
{
if (m_value == value)
return;
m_value = value;
emit valueChanged();
}
2021-11-03 18:29:31 +01:00
void BitcoinValue::moveLeft()
{
if (m_cursorPos > 0)
setCursorPos(m_cursorPos - 1);
}
void BitcoinValue::moveRight()
{
if (m_typedNumber.size() > m_cursorPos)
setCursorPos(m_cursorPos + 1);
}
void BitcoinValue::insertNumber(QChar number)
2020-06-12 20:53:01 +02:00
{
int pos = m_typedNumber.indexOf('.');
const int unitConfigDecimals = m_maxFractionalDigits == -1 ? FloweePay::instance()->unitAllowedDecimals() : m_maxFractionalDigits;
2021-11-03 22:00:58 +01:00
if (pos > -1 && m_cursorPos > pos && m_typedNumber.size() - pos - unitConfigDecimals > 0)
2020-06-12 20:53:01 +02:00
return;
2021-11-03 18:29:31 +01:00
int cursorPos = m_cursorPos;
m_typedNumber.insert(cursorPos, number);
while (((pos < 0 && m_typedNumber.size() > 1) || pos > 1) && m_typedNumber.startsWith('0')) {
--cursorPos;
2020-06-12 20:53:01 +02:00
m_typedNumber = m_typedNumber.mid(1);
2021-11-03 18:29:31 +01:00
}
2020-06-12 20:53:01 +02:00
setStringValue(m_typedNumber);
2021-11-03 18:29:31 +01:00
setCursorPos(cursorPos + 1);
2020-06-12 20:53:01 +02:00
emit enteredStringChanged();
}
void BitcoinValue::addSeparator()
{
if (m_typedNumber.indexOf('.') == -1) {
2021-11-03 18:29:31 +01:00
m_typedNumber.insert(m_cursorPos, '.');
int movedPlaces = 1;
if (m_typedNumber.size() == 1) {
++movedPlaces;
2020-06-12 20:53:01 +02:00
m_typedNumber = "0.";
2021-11-03 18:29:31 +01:00
}
setCursorPos(m_cursorPos + movedPlaces);
2020-06-12 20:53:01 +02:00
setStringValue(m_typedNumber);
emit enteredStringChanged();
}
}
2020-06-22 12:14:13 +02:00
void BitcoinValue::paste()
{
QClipboard *clipboard = QGuiApplication::clipboard();
assert(clipboard);
2021-11-03 18:29:31 +01:00
setEnteredString(clipboard->text().trimmed());
}
void BitcoinValue::backspacePressed()
{
int cursorPos = m_cursorPos;
if (m_typedNumber.size() <= 1) {
m_typedNumber.clear();
cursorPos = 0;
} else {
m_typedNumber.remove(--cursorPos, 1);
}
setStringValue(m_typedNumber);
emit enteredStringChanged();
setCursorPos(cursorPos);
}
2021-11-09 20:41:37 +01:00
void BitcoinValue::reset()
{
m_value = 0;
m_typedNumber.clear();
m_cursorPos = 0;
emit enteredStringChanged();
emit cursorPosChanged();
emit valueChanged();
}
2021-11-03 18:29:31 +01:00
void BitcoinValue::setEnteredString(const QString &value)
{
2020-06-22 12:14:13 +02:00
bool started = false;
2021-11-03 18:29:31 +01:00
setCursorPos(0);
m_typedNumber.clear();
for (int i = 0; i < value.size(); ++i) {
auto k = value.at(i);
2020-06-22 12:14:13 +02:00
if (k.isDigit()) {
started = true;
2021-11-03 18:29:31 +01:00
insertNumber(k);
2020-06-22 12:14:13 +02:00
}
2021-11-03 18:29:31 +01:00
else if ((started || (value.size() > i + 1 && value.at(i+1).isDigit()))
2020-06-22 12:14:13 +02:00
&& (k.unicode() == ',' || k.unicode() == '.')) {
addSeparator();
}
else if (started)
return;
}
if (!m_cursorPos)
setValue(0);
2020-06-22 12:14:13 +02:00
}
2020-06-12 20:53:01 +02:00
void BitcoinValue::setStringValue(const QString &value)
{
int separator = value.indexOf('.');
QString before, after;
if (separator == -1) {
before = value;
after = "000000000";
} else {
before = value.left(separator);
after = value.mid(separator + 1);
}
qint64 newVal = before.toLong();
const int unitConfigDecimals = m_maxFractionalDigits == -1 ? FloweePay::instance()->unitAllowedDecimals() : m_maxFractionalDigits;
2020-06-12 20:53:01 +02:00
for (int i = 0; i < unitConfigDecimals; ++i) {
newVal *= 10;
}
while (after.size() < unitConfigDecimals)
after += '0';
newVal += after.toInt();
setValue(newVal);
}
QString BitcoinValue::enteredString() const
{
const char decimalPoint = QLocale::system().decimalPoint().unicode();
if (decimalPoint != '.') { // make the user-visible one always use the locale-aware decimalpoint.
QString answer(m_typedNumber);
answer.replace('.', decimalPoint);
return answer;
}
return m_typedNumber;
}
2021-11-03 18:29:31 +01:00
int BitcoinValue::maxFractionalDigits() const
2020-06-12 20:53:01 +02:00
{
2021-11-03 18:29:31 +01:00
return m_maxFractionalDigits;
}
void BitcoinValue::setMaxFractionalDigits(int newMaxFractionalDigits)
{
if (m_maxFractionalDigits == newMaxFractionalDigits)
2020-06-12 20:53:01 +02:00
return;
2021-11-03 18:29:31 +01:00
m_maxFractionalDigits = newMaxFractionalDigits;
emit maxFractionalDigitsChanged();
}
void BitcoinValue::resetMaxFractionalDigits()
{
setMaxFractionalDigits(-1);
}
int BitcoinValue::cursorPos() const
{
return m_cursorPos;
}
void BitcoinValue::setCursorPos(int cp)
{
if (m_cursorPos == cp)
return;
m_cursorPos = cp;
emit cursorPosChanged();
2020-06-12 20:53:01 +02:00
}