File indexing completed on 2024-04-28 05:46:32

0001 /*
0002     SPDX-FileCopyrightText: 2010 Volker Lanz <vl@fidra.de>
0003     SPDX-FileCopyrightText: 2014 Andrius Štikonas <andrius@stikonas.eu>
0004 
0005     SPDX-License-Identifier: GPL-3.0-or-later
0006 */
0007 
0008 #include "gui/formattedspinbox.h"
0009 
0010 #include <QLocale>
0011 
0012 // private method from Qt sources, qabstractspinbox.h:
0013 
0014 QString FormattedSpinBox::stripped(const QString &t, int *pos) const
0015 {
0016     QString text = t;
0017     if (specialValueText().size() == 0 || text != specialValueText()) {
0018         int from = 0;
0019         int size = text.size();
0020         bool changed = false;
0021         if (prefix().size() && text.startsWith(prefix())) {
0022             from += prefix().size();
0023             size -= from;
0024             changed = true;
0025         }
0026         if (suffix().size() && text.endsWith(suffix())) {
0027             size -= suffix().size();
0028             changed = true;
0029         }
0030         if (changed)
0031             text = text.mid(from, size);
0032     }
0033 
0034     const int s = text.size();
0035     text = text.trimmed();
0036     if (pos)
0037         (*pos) -= (s - text.size());
0038     return text;
0039 }
0040 
0041 QString FormattedSpinBox::textFromValue(double value) const
0042 {
0043     return QLocale().toString(value, 'f', decimals());
0044 }
0045 
0046 double FormattedSpinBox::valueFromText(const QString& text) const
0047 {
0048     return QLocale().toDouble(stripped(text));
0049 }