| 1 |
/* This file is part of the KDE project |
| 2 |
SPDX-FileCopyrightText: 2002 Rob Buis ([email protected]) |
| 3 |
SPDX-FileCopyrightText: 2004 Nicolas GOUTTE <[email protected]> |
| 4 |
SPDX-FileCopyrightText: 2007 Thomas Zander <[email protected]> |
| 5 |
|
| 6 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 7 |
*/ |
| 8 |
|
| 9 |
// clazy:excludeall=qstring-arg |
| 10 |
#include "KoUnitDoubleSpinBox.h" |
| 11 |
|
| 12 |
#include <KoUnit.h> |
| 13 |
|
| 14 |
#include <WidgetsDebug.h> |
| 15 |
|
| 16 |
#include <KLocalizedString> |
| 17 |
|
| 18 |
// #define DEBUG_VALIDATOR |
| 19 |
// #define DEBUG_VALUEFROMTEXT |
| 20 |
|
| 21 |
class Q_DECL_HIDDEN KoUnitDoubleSpinBox::Private |
| 22 |
{ |
| 23 |
public: |
| 24 |
Private(double low, double up, double step) |
| 25 |
: lowerInPoints(low), |
| 26 |
upperInPoints(up), |
| 27 |
stepInPoints(step), |
| 28 |
unit(KoUnit(KoUnit::Point)) |
| 29 |
{ |
| 30 |
} |
| 31 |
|
| 32 |
double lowerInPoints; ///< lowest value in points |
| 33 |
double upperInPoints; ///< highest value in points |
| 34 |
double stepInPoints; ///< step in points |
| 35 |
KoUnit unit; |
| 36 |
}; |
| 37 |
|
| 38 |
KoUnitDoubleSpinBox::KoUnitDoubleSpinBox(QWidget *parent) |
| 39 |
: QDoubleSpinBox(parent), |
| 40 |
d(new Private(-9999, 9999, 1)) |
| 41 |
{ |
| 42 |
QDoubleSpinBox::setDecimals(2); |
| 43 |
//setAcceptLocalizedNumbers(true); |
| 44 |
KoUnitDoubleSpinBox::setUnit(KoUnit(KoUnit::Point)); |
| 45 |
setAlignment(Qt::AlignRight); |
| 46 |
|
| 47 |
connect(this, SIGNAL(valueChanged(double)), SLOT(privateValueChanged())); |
| 48 |
} |
| 49 |
|
| 50 |
KoUnitDoubleSpinBox::~KoUnitDoubleSpinBox() |
| 51 |
{ |
| 52 |
delete d; |
| 53 |
} |
| 54 |
|
| 55 |
QValidator::State KoUnitDoubleSpinBox::validate(QString &input, int &pos) const |
| 56 |
{ |
| 57 |
#ifdef DEBUG_VALIDATOR |
| 58 |
debugWidgets <<"KoUnitDoubleSpinBox::validate :" << input <<" at" << pos; |
| 59 |
#else |
| 60 |
Q_UNUSED(pos); |
| 61 |
#endif |
| 62 |
|
| 63 |
QRegularExpression regexp(QStringLiteral("([ a-zA-Z]+)$")); // Letters or spaces at end |
| 64 |
auto match = regexp.match(input); |
| 65 |
const int res = match.hasMatch() ? match.capturedStart(1) : -1; |
| 66 |
|
| 67 |
if (res == -1) |
| 68 |
{ |
| 69 |
// Nothing like an unit? The user is probably editing the unit |
| 70 |
#ifdef DEBUG_VALIDATOR |
| 71 |
debugWidgets <<"Intermediate (no unit)"; |
| 72 |
#endif |
| 73 |
return QValidator::Intermediate; |
| 74 |
} |
| 75 |
|
| 76 |
// ### TODO: are all the QString::trimmed really necessary? |
| 77 |
const QString number (input.left(res).trimmed()); |
| 78 |
const QString unitName (match.captured(1).trimmed().toLower()); |
| 79 |
|
| 80 |
#ifdef DEBUG_VALIDATOR |
| 81 |
debugWidgets <<"Split:" << number <<":" << unitName <<":"; |
| 82 |
#endif |
| 83 |
|
| 84 |
const double value = valueFromText(number); |
| 85 |
if (!qIsNaN(value)) |
| 86 |
{ |
| 87 |
bool ok; |
| 88 |
KoUnit::fromSymbol(unitName, &ok); |
| 89 |
if (!ok) |
| 90 |
{ |
| 91 |
// Probably the user is trying to edit the unit |
| 92 |
#ifdef DEBUG_VALIDATOR |
| 93 |
debugWidgets <<"Intermediate (unknown unit)"; |
| 94 |
#endif |
| 95 |
return QValidator::Intermediate; |
| 96 |
} |
| 97 |
} |
| 98 |
else |
| 99 |
{ |
| 100 |
warnWidgets << "Not a number: " << number; |
| 101 |
return QValidator::Invalid; |
| 102 |
} |
| 103 |
//newVal = KoUnit::ptToUnit(newVal, d->unit); |
| 104 |
//input = textFromValue(newVal); // FIXME: don't overwrite for now; the effect is not exactly what I expect... |
| 105 |
|
| 106 |
return QValidator::Acceptable; |
| 107 |
} |
| 108 |
|
| 109 |
void KoUnitDoubleSpinBox::changeValue(double val) |
| 110 |
{ |
| 111 |
QDoubleSpinBox::setValue(d->unit.toUserValue(val)); |
| 112 |
// TODO: Q_EMIT valueChanged ONLY if the value was out-of-bounds |
| 113 |
// This will allow the 'user' dialog to set a dirty bool and ensure |
| 114 |
// a proper value is getting saved. |
| 115 |
} |
| 116 |
|
| 117 |
void KoUnitDoubleSpinBox::privateValueChanged() { |
| 118 |
Q_EMIT valueChangedPt(value ()); |
| 119 |
} |
| 120 |
|
| 121 |
void KoUnitDoubleSpinBox::setUnit(const KoUnit &unit) |
| 122 |
{ |
| 123 |
if (unit == d->unit) return; |
| 124 |
|
| 125 |
double oldvalue = d->unit.fromUserValue(QDoubleSpinBox::value()); |
| 126 |
QDoubleSpinBox::setMinimum(unit.toUserValue(d->lowerInPoints)); |
| 127 |
QDoubleSpinBox::setMaximum(unit.toUserValue(d->upperInPoints)); |
| 128 |
|
| 129 |
qreal step = unit.toUserValue(d->stepInPoints); |
| 130 |
|
| 131 |
if (unit.type() == KoUnit::Pixel) { |
| 132 |
// limit the pixel step by 1.0 |
| 133 |
step = qMax(qreal(1.0), step); |
| 134 |
} |
| 135 |
|
| 136 |
QDoubleSpinBox::setSingleStep(step); |
| 137 |
d->unit = unit; |
| 138 |
QDoubleSpinBox::setValue(KoUnit::ptToUnit(oldvalue, unit)); |
| 139 |
setSuffix(unit.symbol().prepend(QLatin1Char(' '))); |
| 140 |
} |
| 141 |
|
| 142 |
double KoUnitDoubleSpinBox::value() const |
| 143 |
{ |
| 144 |
return d->unit.fromUserValue(QDoubleSpinBox::value()); |
| 145 |
} |
| 146 |
|
| 147 |
void KoUnitDoubleSpinBox::setMinimum(double min) |
| 148 |
{ |
| 149 |
d->lowerInPoints = min; |
| 150 |
QDoubleSpinBox::setMinimum(d->unit.toUserValue(min)); |
| 151 |
} |
| 152 |
|
| 153 |
void KoUnitDoubleSpinBox::setMaximum(double max) |
| 154 |
{ |
| 155 |
d->upperInPoints = max; |
| 156 |
QDoubleSpinBox::setMaximum(d->unit.toUserValue(max)); |
| 157 |
} |
| 158 |
|
| 159 |
void KoUnitDoubleSpinBox::setLineStep(double step) |
| 160 |
{ |
| 161 |
d->stepInPoints = KoUnit(KoUnit::Point).toUserValue(step); |
| 162 |
QDoubleSpinBox::setSingleStep(step); |
| 163 |
} |
| 164 |
|
| 165 |
void KoUnitDoubleSpinBox::setLineStepPt(double step) |
| 166 |
{ |
| 167 |
d->stepInPoints = step; |
| 168 |
QDoubleSpinBox::setSingleStep(d->unit.toUserValue(step)); |
| 169 |
} |
| 170 |
|
| 171 |
void KoUnitDoubleSpinBox::setMinMaxStep(double min, double max, double step) |
| 172 |
{ |
| 173 |
setMinimum(min); |
| 174 |
setMaximum(max); |
| 175 |
setLineStepPt(step); |
| 176 |
} |
| 177 |
|
| 178 |
QString KoUnitDoubleSpinBox::textFromValue(double value) const |
| 179 |
{ |
| 180 |
//debugWidgets <<"textFromValue:" << QString::number(value, 'f', 12) <<" =>" << num; |
| 181 |
//const QString num(QString("%1%2").arg(QLocale().toString(value, d->precision), m_unit.symbol())); |
| 182 |
//const QString num (QString("%1").arg(QLocale().toString(value, d->precision))); |
| 183 |
return QLocale().toString(value, 'f', decimals()); |
| 184 |
} |
| 185 |
|
| 186 |
double KoUnitDoubleSpinBox::valueFromText(const QString& str) const |
| 187 |
{ |
| 188 |
QString str2(str); |
| 189 |
str2.remove(d->unit.symbol()); |
| 190 |
return QLocale().toDouble(str2); |
| 191 |
// QString str2(str); |
| 192 |
// /* KLocale::readNumber wants the thousand separator exactly at 1000. |
| 193 |
// But when editing, it might be anywhere. So we need to remove it. */ |
| 194 |
// const QString sep(KGlobal::locale()->thousandsSeparator()); |
| 195 |
// if (!sep.isEmpty()) |
| 196 |
// str2.remove(sep); |
| 197 |
// str2.remove(d->unit.symbol()); |
| 198 |
// bool ok; |
| 199 |
// const double dbl = KGlobal::locale()->readNumber(str2, &ok); |
| 200 |
//#ifdef DEBUG_VALUEFROMTEXT |
| 201 |
// if (ok) |
| 202 |
// debugWidgets <<"valueFromText:" << str <<": => :" << str2 <<": =>" << QString::number(dbl, 'f', 12); |
| 203 |
// else |
| 204 |
// warnWidgets << "valueFromText error:" << str << ": => :" << str2 << ":"; |
| 205 |
//#endif |
| 206 |
// return dbl; |
| 207 |
} |