2015-02-07 00:32:25 +00:00
|
|
|
/*************************************************************************
|
|
|
|
* Copyright (C) 2010 by Volker Lanz <vl@fidra.de> *
|
|
|
|
* *
|
|
|
|
* 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/>.*
|
|
|
|
*************************************************************************/
|
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
|
|
|
}
|