Example #1
0
/*!
    \property QProgressBar::text
    \brief the descriptive text shown with the progress bar

    The text returned is the same as the text displayed in the center
    (or in some styles, to the left) of the progress bar.

    The progress shown in the text may be smaller than the minimum value,
    indicating that the progress bar is in the "reset" state before any
    progress is set.

    In the default implementation, the text either contains a percentage
    value that indicates the progress so far, or it is blank because the
    progress bar is in the reset state.
*/
QString QProgressBar::text() const
{
    Q_D(const QProgressBar);
    if ((d->maximum == 0 && d->minimum == 0) || d->value < d->minimum
            || (d->value == INT_MIN && d->minimum == INT_MIN))
        return QString();

    qint64 totalSteps = qint64(d->maximum) - d->minimum;

    QString result = d->format;
    QLocale locale = d->locale; // Omit group separators for compatibility with previous versions that were non-localized.
    locale.setNumberOptions(locale.numberOptions() | QLocale::OmitGroupSeparator);
    result.replace(QLatin1String("%m"), locale.toString(totalSteps));
    result.replace(QLatin1String("%v"), locale.toString(d->value));

    // If max and min are equal and we get this far, it means that the
    // progress bar has one step and that we are on that step. Return
    // 100% here in order to avoid division by zero further down.
    if (totalSteps == 0) {
        result.replace(QLatin1String("%p"), locale.toString(int(100)));
        return result;
    }

    int progress = (qreal(d->value) - d->minimum) * 100.0 / totalSteps;
    result.replace(QLatin1String("%p"), locale.toString(progress));
    return result;
}
Example #2
0
QValidator::State QDoubleValidatorPrivate::validateWithLocale(QString &input, QLocaleData::NumberMode numMode, const QLocale &locale) const
{
    Q_Q(const QDoubleValidator);
    QByteArray buff;
    if (!locale.d->m_data->validateChars(input, numMode, &buff, q->dec,
                                         locale.numberOptions() & QLocale::RejectGroupSeparator)) {
        return QValidator::Invalid;
    }

    if (buff.isEmpty())
        return QValidator::Intermediate;

    if (q->b >= 0 && buff.startsWith('-'))
        return QValidator::Invalid;

    if (q->t < 0 && buff.startsWith('+'))
        return QValidator::Invalid;

    bool ok, overflow;
    double i = QLocaleData::bytearrayToDouble(buff.constData(), &ok, &overflow);
    if (overflow)
        return QValidator::Invalid;
    if (!ok)
        return QValidator::Intermediate;

    if (i >= q->b && i <= q->t)
        return QValidator::Acceptable;

    if (notation == QDoubleValidator::StandardNotation) {
        double max = qMax(qAbs(q->b), qAbs(q->t));
        if (max < LLONG_MAX) {
            qlonglong n = pow10(numDigits(qlonglong(max))) - 1;
            if (qAbs(i) > n)
                return QValidator::Invalid;
        }
    }

    return QValidator::Intermediate;
}