KoUnitDoubleSpinBox.cpp 6.68 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* This file is part of the KDE project
   Copyright (C) 2002, Rob Buis(buis@kde.org)
   Copyright (C) 2004, Nicolas GOUTTE <goutte@kde.org>
   Copyright (C) 2007, Thomas Zander <zander@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "KoUnitDoubleSpinBox.h"
23 24 25

#include <KoUnit.h>

26 27
#include <WidgetsDebug.h>

28
#include <klocalizedstring.h>
29
#include <qnumeric.h>
30

31
// #define DEBUG_VALIDATOR
Sebastian Sauer's avatar
Sebastian Sauer committed
32
// #define DEBUG_VALUEFROMTEXT
33

34 35
class Q_DECL_HIDDEN KoUnitDoubleSpinBox::Private
{
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
public:
    Private(double low, double up, double step)
        : lowerInPoints(low),
        upperInPoints(up),
        stepInPoints(step),
        unit(KoUnit(KoUnit::Point))
    {
    }

    double lowerInPoints; ///< lowest value in points
    double upperInPoints; ///< highest value in points
    double stepInPoints;  ///< step in points
    KoUnit unit;
};

KoUnitDoubleSpinBox::KoUnitDoubleSpinBox( QWidget *parent)
    : QDoubleSpinBox( parent ),
    d( new Private(-9999, 9999, 1))
{
    QDoubleSpinBox::setDecimals( 2 );
    //setAcceptLocalizedNumbers( true );
    setUnit( KoUnit(KoUnit::Point) );
    setAlignment( Qt::AlignRight );

60
    connect(this, SIGNAL(valueChanged(double)), SLOT(privateValueChanged()));
61 62 63 64 65 66 67 68 69 70
}

KoUnitDoubleSpinBox::~KoUnitDoubleSpinBox()
{
    delete d;
}

QValidator::State KoUnitDoubleSpinBox::validate(QString &input, int &pos) const
{
#ifdef DEBUG_VALIDATOR
71
    debugWidgets <<"KoUnitDoubleSpinBox::validate :" << input <<" at" << pos;
72 73 74 75 76 77 78 79 80 81 82
#else
    Q_UNUSED(pos);
#endif

    QRegExp regexp ("([ a-zA-Z]+)$"); // Letters or spaces at end
    const int res = input.indexOf( regexp );

    if ( res == -1 )
    {
        // Nothing like an unit? The user is probably editing the unit
#ifdef DEBUG_VALIDATOR
83
        debugWidgets <<"Intermediate (no unit)";
84 85 86 87 88 89 90 91 92
#endif
        return QValidator::Intermediate;
    }

    // ### TODO: are all the QString::trimmed really necessary?
    const QString number ( input.left( res ).trimmed() );
    const QString unitName ( regexp.cap( 1 ).trimmed().toLower() );

#ifdef DEBUG_VALIDATOR
93
    debugWidgets <<"Split:" << number <<":" << unitName <<":";
94 95 96 97
#endif

    const double value = valueFromText( number );
    double newVal = 0.0;
98
    if (!qIsNaN(value)) {
99
        bool ok;
100
        const KoUnit unit = KoUnit::fromSymbol(unitName, &ok);
101 102 103 104 105 106
        if ( ok )
            newVal = unit.fromUserValue( value );
        else
        {
            // Probably the user is trying to edit the unit
#ifdef DEBUG_VALIDATOR
107
            debugWidgets <<"Intermediate (unknown unit)";
108 109 110 111 112 113
#endif
            return QValidator::Intermediate;
        }
    }
    else
    {
114
        warnWidgets << "Not a number: " << number;
115 116
        return QValidator::Invalid;
    }
Agata Cacko's avatar
Agata Cacko committed
117
    newVal = d->unit.toUserValuePrecise(newVal);
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    //input = textFromValue( newVal ); // don't overwrite for now; the effect is not exactly what I expect...

    return QValidator::Acceptable;
}

void KoUnitDoubleSpinBox::changeValue( double val )
{
    QDoubleSpinBox::setValue( d->unit.toUserValue( val ) );
    // TODO: emit valueChanged ONLY if the value was out-of-bounds
    // This will allow the 'user' dialog to set a dirty bool and ensure
    // a proper value is getting saved.
}

void KoUnitDoubleSpinBox::privateValueChanged() {
    emit valueChangedPt( value () );
}

135
void KoUnitDoubleSpinBox::setUnit( const KoUnit &unit )
136
{
137 138
    if (unit == d->unit) return;

139 140 141
    double oldvalue = d->unit.fromUserValue( QDoubleSpinBox::value() );
    QDoubleSpinBox::setMinimum( unit.toUserValue( d->lowerInPoints ) );
    QDoubleSpinBox::setMaximum( unit.toUserValue( d->upperInPoints ) );
142 143 144 145

    qreal step = unit.toUserValue( d->stepInPoints );

    if (unit.type() == KoUnit::Pixel) {
146
        // limit the pixel step by 1.0
147
        step = qMax(qreal(1.0), step);
148 149 150
    }

    QDoubleSpinBox::setSingleStep( step );
151
    d->unit = unit;
Agata Cacko's avatar
Agata Cacko committed
152
    QDoubleSpinBox::setValue(unit.toUserValuePrecise(oldvalue));
153
    setSuffix(unit.symbol().prepend(QLatin1Char(' ')));
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
}

double KoUnitDoubleSpinBox::value( ) const
{
    return d->unit.fromUserValue( QDoubleSpinBox::value() );
}

void KoUnitDoubleSpinBox::setMinimum( double min )
{
  d->lowerInPoints = min;
  QDoubleSpinBox::setMinimum( d->unit.toUserValue( min ) );
}

void KoUnitDoubleSpinBox::setMaximum( double max )
{
  d->upperInPoints = max;
  QDoubleSpinBox::setMaximum( d->unit.toUserValue( max ) );
}

void KoUnitDoubleSpinBox::setLineStep( double step )
{
  d->stepInPoints = KoUnit(KoUnit::Point).toUserValue(step);
  QDoubleSpinBox::setSingleStep( step );
}

void KoUnitDoubleSpinBox::setLineStepPt( double step )
{
  d->stepInPoints = step;
  QDoubleSpinBox::setSingleStep( d->unit.toUserValue( step ) );
}

void KoUnitDoubleSpinBox::setMinMaxStep( double min, double max, double step )
{
  setMinimum( min );
  setMaximum( max );
  setLineStepPt( step );
}

QString KoUnitDoubleSpinBox::textFromValue( double value ) const
{
194
    //debugWidgets <<"textFromValue:" << QString::number( value, 'f', 12 ) <<" =>" << num;
195 196
    //const QString num(QString("%1%2").arg(QLocale().toString(value, d->precision ), m_unit.symbol()));
    //const QString num ( QString( "%1").arg( QLocale().toString( value, d->precision )) );
197
    return QLocale().toString( value, 'f', decimals() );
198 199 200 201
}

double KoUnitDoubleSpinBox::valueFromText( const QString& str ) const
{
202 203 204
    QString str2( str );
    str2.remove(d->unit.symbol());
    return QLocale().toDouble(str2);
205 206 207 208 209 210 211 212 213 214 215
//    QString str2( str );
//    /* KLocale::readNumber wants the thousand separator exactly at 1000.
//       But when editing, it might be anywhere. So we need to remove it. */
//    const QString sep( KGlobal::locale()->thousandsSeparator() );
//    if ( !sep.isEmpty() )
//        str2.remove( sep );
//    str2.remove(d->unit.symbol());
//    bool ok;
//    const double dbl = KGlobal::locale()->readNumber( str2, &ok );
//#ifdef DEBUG_VALUEFROMTEXT
//    if ( ok )
216
//      debugWidgets <<"valueFromText:" << str <<": => :" << str2 <<": =>" << QString::number( dbl, 'f', 12 );
217
//    else
218
//        warnWidgets << "valueFromText error:" << str << ": => :" << str2 << ":";
219 220
//#endif
//    return dbl;
221
}