示例#1
0
double DoubleSpinBox::valueFromText( const QString & text ) const {

    DoubleSpinbox_qDebug() << Q_FUNC_INFO << "text = " << text;

    KLocale * locale = KGlobal::locale();

    // Fetch the characters that we don't want to discard
    const QString exclude = locale->decimalSymbol()
            + locale->thousandsSeparator()
            + locale->positiveSign()
            + locale->negativeSign();

    QString textToStrip( text );
    QString numberToRead = textToStrip.remove( QRegExp("[^"+exclude+"\\d]") );

    bool ok;
    double value = locale->readNumber( numberToRead, &ok );
    if (!ok) {
        DoubleSpinbox_qDebug() << Q_FUNC_INFO << "numberToRead = |" << numberToRead << "| NOT OK";
        value = 0;
    }
    DoubleSpinbox_qDebug() << Q_FUNC_INFO << "numberToRead = " << numberToRead << ", value = " << value;

    if ( value > maximum() ) {
        value = maximum();
    } else if ( value < minimum() ) {
        value = minimum();
    }

    if ( std::abs(value) < m_minAbsValue*0.9999 ) {
        value = 0.0;
    }

    double multiplier = 1.0;
    //updateSuffix( value );
    QString textForSuffix( text );

    if ( textForSuffix.length() != 0 ) {

        if ( textForSuffix.endsWith( m_unit, false ) ) {
            textForSuffix = textForSuffix.remove( textForSuffix.length() - m_unit.length(), m_unit.length() );
        }

        textForSuffix.stripWhiteSpace();

        QChar siExp = textForSuffix[ textForSuffix.length()-1 ];

        DoubleSpinbox_qDebug() << Q_FUNC_INFO << "SI exp = " << siExp;

        if ( siExp.isLetter() || siExp.isSymbol() ) {
            multiplier = CNItem::getMultiplier( QString(siExp) );
        } else {
            multiplier = 1;
        }
    }
    DoubleSpinbox_qDebug() << Q_FUNC_INFO << "multiplier = " << multiplier;

    //value /= Item::getMultiplier( value );
    value *= multiplier;

    DoubleSpinbox_qDebug() << Q_FUNC_INFO << "value = " << value;

    return value;
}