2020-09-16 00:35:12 +01:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: 2010 Volker Lanz <vl@fidra.de>
|
|
|
|
SPDX-FileCopyrightText: 2014 Andrius Štikonas <andrius@stikonas.eu>
|
|
|
|
|
|
|
|
SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
*/
|
2010-04-27 12:41:35 +01:00
|
|
|
|
|
|
|
#include "gui/formattedspinbox.h"
|
|
|
|
|
2014-05-13 23:53:37 +01:00
|
|
|
#include <QLocale>
|
2010-05-23 15:05:57 +01:00
|
|
|
|
|
|
|
// private method from Qt sources, qabstractspinbox.h:
|
|
|
|
|
|
|
|
QString FormattedSpinBox::stripped(const QString &t, int *pos) const
|
|
|
|
{
|
2015-07-13 15:17:41 +01:00
|
|
|
QString text = t;
|
|
|
|
if (specialValueText().size() == 0 || text != specialValueText()) {
|
|
|
|
int from = 0;
|
|
|
|
int size = text.size();
|
|
|
|
bool changed = false;
|
|
|
|
if (prefix().size() && text.startsWith(prefix())) {
|
|
|
|
from += prefix().size();
|
|
|
|
size -= from;
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
if (suffix().size() && text.endsWith(suffix())) {
|
|
|
|
size -= suffix().size();
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
if (changed)
|
|
|
|
text = text.mid(from, size);
|
|
|
|
}
|
2010-05-23 15:05:57 +01:00
|
|
|
|
2015-07-13 15:17:41 +01:00
|
|
|
const int s = text.size();
|
|
|
|
text = text.trimmed();
|
|
|
|
if (pos)
|
|
|
|
(*pos) -= (s - text.size());
|
|
|
|
return text;
|
2010-05-23 15:05:57 +01:00
|
|
|
}
|
2010-04-27 12:41:35 +01:00
|
|
|
|
|
|
|
QString FormattedSpinBox::textFromValue(double value) const
|
|
|
|
{
|
2015-07-13 15:17:41 +01:00
|
|
|
return QLocale().toString(value, 'f', decimals());
|
2010-04-27 12:41:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
double FormattedSpinBox::valueFromText(const QString& text) const
|
|
|
|
{
|
2015-07-13 15:17:41 +01:00
|
|
|
return QLocale().toDouble(stripped(text));
|
2010-04-27 12:41:35 +01:00
|
|
|
}
|