QProgressBar: Use localized numbers and percent sign.
Task-number: QTBUG-28751 Change-Id: I56aca3e0ee9c579297110c69d2d832c7a57f1ae7 Reviewed-by: J-P Nurmi <jpnurmi@digia.com> Reviewed-by: Karim Pinter <karim.pinter@digia.com>
This commit is contained in:
parent
8efad82d6c
commit
4606ea5395
@ -41,6 +41,7 @@
|
|||||||
|
|
||||||
#include "qprogressbar.h"
|
#include "qprogressbar.h"
|
||||||
#ifndef QT_NO_PROGRESSBAR
|
#ifndef QT_NO_PROGRESSBAR
|
||||||
|
#include <qlocale.h>
|
||||||
#include <qevent.h>
|
#include <qevent.h>
|
||||||
#include <qpainter.h>
|
#include <qpainter.h>
|
||||||
#include <qstylepainter.h>
|
#include <qstylepainter.h>
|
||||||
@ -61,6 +62,7 @@ public:
|
|||||||
QProgressBarPrivate();
|
QProgressBarPrivate();
|
||||||
|
|
||||||
void init();
|
void init();
|
||||||
|
void initDefaultFormat();
|
||||||
inline void resetLayoutItemMargins();
|
inline void resetLayoutItemMargins();
|
||||||
|
|
||||||
int minimum;
|
int minimum;
|
||||||
@ -68,6 +70,7 @@ public:
|
|||||||
int value;
|
int value;
|
||||||
Qt::Alignment alignment;
|
Qt::Alignment alignment;
|
||||||
uint textVisible : 1;
|
uint textVisible : 1;
|
||||||
|
uint defaultFormat: 1;
|
||||||
int lastPaintedValue;
|
int lastPaintedValue;
|
||||||
Qt::Orientation orientation;
|
Qt::Orientation orientation;
|
||||||
bool invertedAppearance;
|
bool invertedAppearance;
|
||||||
@ -79,9 +82,16 @@ public:
|
|||||||
|
|
||||||
QProgressBarPrivate::QProgressBarPrivate()
|
QProgressBarPrivate::QProgressBarPrivate()
|
||||||
: minimum(0), maximum(100), value(-1), alignment(Qt::AlignLeft), textVisible(true),
|
: minimum(0), maximum(100), value(-1), alignment(Qt::AlignLeft), textVisible(true),
|
||||||
lastPaintedValue(-1), orientation(Qt::Horizontal), invertedAppearance(false),
|
defaultFormat(true), lastPaintedValue(-1), orientation(Qt::Horizontal), invertedAppearance(false),
|
||||||
textDirection(QProgressBar::TopToBottom), format(QLatin1String("%p%"))
|
textDirection(QProgressBar::TopToBottom)
|
||||||
{
|
{
|
||||||
|
initDefaultFormat();
|
||||||
|
}
|
||||||
|
|
||||||
|
void QProgressBarPrivate::initDefaultFormat()
|
||||||
|
{
|
||||||
|
if (defaultFormat)
|
||||||
|
format = QLatin1String("%p") + locale.percent();
|
||||||
}
|
}
|
||||||
|
|
||||||
void QProgressBarPrivate::init()
|
void QProgressBarPrivate::init()
|
||||||
@ -466,19 +476,21 @@ QString QProgressBar::text() const
|
|||||||
qint64 totalSteps = qint64(d->maximum) - d->minimum;
|
qint64 totalSteps = qint64(d->maximum) - d->minimum;
|
||||||
|
|
||||||
QString result = d->format;
|
QString result = d->format;
|
||||||
result.replace(QLatin1String("%m"), QString::number(totalSteps));
|
QLocale locale = d->locale; // Omit group separators for compatibility with previous versions that were non-localized.
|
||||||
result.replace(QLatin1String("%v"), QString::number(d->value));
|
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
|
// 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
|
// progress bar has one step and that we are on that step. Return
|
||||||
// 100% here in order to avoid division by zero further down.
|
// 100% here in order to avoid division by zero further down.
|
||||||
if (totalSteps == 0) {
|
if (totalSteps == 0) {
|
||||||
result.replace(QLatin1String("%p"), QString::number(100));
|
result.replace(QLatin1String("%p"), locale.toString(int(100)));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
int progress = (qreal(d->value) - d->minimum) * 100.0 / totalSteps;
|
int progress = (qreal(d->value) - d->minimum) * 100.0 / totalSteps;
|
||||||
result.replace(QLatin1String("%p"), QString::number(progress));
|
result.replace(QLatin1String("%p"), locale.toString(progress));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -568,12 +580,19 @@ QProgressBar::Direction QProgressBar::textDirection() const
|
|||||||
bool QProgressBar::event(QEvent *e)
|
bool QProgressBar::event(QEvent *e)
|
||||||
{
|
{
|
||||||
Q_D(QProgressBar);
|
Q_D(QProgressBar);
|
||||||
if (e->type() == QEvent::StyleChange
|
switch (e->type()) {
|
||||||
|
case QEvent::StyleChange:
|
||||||
#ifdef Q_OS_MAC
|
#ifdef Q_OS_MAC
|
||||||
|| e->type() == QEvent::MacSizeChange
|
case QEvent::MacSizeChange:
|
||||||
#endif
|
#endif
|
||||||
)
|
|
||||||
d->resetLayoutItemMargins();
|
d->resetLayoutItemMargins();
|
||||||
|
break;
|
||||||
|
case QEvent::LocaleChange:
|
||||||
|
d->initDefaultFormat();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
return QWidget::event(e);
|
return QWidget::event(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -596,6 +615,15 @@ void QProgressBar::setFormat(const QString &format)
|
|||||||
if (d->format == format)
|
if (d->format == format)
|
||||||
return;
|
return;
|
||||||
d->format = format;
|
d->format = format;
|
||||||
|
d->defaultFormat = false;
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
|
||||||
|
void QProgressBar::resetFormat()
|
||||||
|
{
|
||||||
|
Q_D(QProgressBar);
|
||||||
|
d->defaultFormat = true;
|
||||||
|
d->initDefaultFormat();
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,7 +65,7 @@ class Q_WIDGETS_EXPORT QProgressBar : public QWidget
|
|||||||
Q_PROPERTY(Qt::Orientation orientation READ orientation WRITE setOrientation)
|
Q_PROPERTY(Qt::Orientation orientation READ orientation WRITE setOrientation)
|
||||||
Q_PROPERTY(bool invertedAppearance READ invertedAppearance WRITE setInvertedAppearance)
|
Q_PROPERTY(bool invertedAppearance READ invertedAppearance WRITE setInvertedAppearance)
|
||||||
Q_PROPERTY(Direction textDirection READ textDirection WRITE setTextDirection)
|
Q_PROPERTY(Direction textDirection READ textDirection WRITE setTextDirection)
|
||||||
Q_PROPERTY(QString format READ format WRITE setFormat)
|
Q_PROPERTY(QString format READ format WRITE setFormat RESET resetFormat)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
enum Direction { TopToBottom, BottomToTop };
|
enum Direction { TopToBottom, BottomToTop };
|
||||||
@ -96,6 +96,7 @@ public:
|
|||||||
QProgressBar::Direction textDirection() const;
|
QProgressBar::Direction textDirection() const;
|
||||||
|
|
||||||
void setFormat(const QString &format);
|
void setFormat(const QString &format);
|
||||||
|
void resetFormat();
|
||||||
QString format() const;
|
QString format() const;
|
||||||
|
|
||||||
public Q_SLOTS:
|
public Q_SLOTS:
|
||||||
|
@ -42,6 +42,7 @@
|
|||||||
|
|
||||||
#include <QtTest/QtTest>
|
#include <QtTest/QtTest>
|
||||||
#include "qprogressbar.h"
|
#include "qprogressbar.h"
|
||||||
|
#include <qlocale.h>
|
||||||
#include <qapplication.h>
|
#include <qapplication.h>
|
||||||
#include <qstyleoption.h>
|
#include <qstyleoption.h>
|
||||||
#include <qdebug.h>
|
#include <qdebug.h>
|
||||||
@ -64,6 +65,7 @@ private slots:
|
|||||||
void sizeHint();
|
void sizeHint();
|
||||||
void formatedText_data();
|
void formatedText_data();
|
||||||
void formatedText();
|
void formatedText();
|
||||||
|
void localizedFormattedText();
|
||||||
|
|
||||||
void task245201_testChangeStyleAndDelete_data();
|
void task245201_testChangeStyleAndDelete_data();
|
||||||
void task245201_testChangeStyleAndDelete();
|
void task245201_testChangeStyleAndDelete();
|
||||||
@ -301,6 +303,40 @@ void tst_QProgressBar::formatedText()
|
|||||||
QCOMPARE(bar.text(), text);
|
QCOMPARE(bar.text(), text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void tst_QProgressBar::localizedFormattedText() // QTBUG-28751
|
||||||
|
{
|
||||||
|
QProgressBar bar;
|
||||||
|
const int value = 42;
|
||||||
|
bar.setValue(value);
|
||||||
|
const QString defaultExpectedNumber = QString::number(value);
|
||||||
|
const QString defaultExpectedValue = defaultExpectedNumber + QLatin1Char('%');
|
||||||
|
QCOMPARE(bar.text(), defaultExpectedValue);
|
||||||
|
|
||||||
|
// Temporarily switch to Egyptian, which has a different percent sign and number formatting
|
||||||
|
QLocale egypt(QLocale::Arabic, QLocale::Egypt);
|
||||||
|
bar.setLocale(egypt);
|
||||||
|
const QString egyptianExpectedNumber = egypt.toString(value);
|
||||||
|
const QString egyptianExpectedValue = egyptianExpectedNumber + egypt.percent();
|
||||||
|
if (egyptianExpectedValue == defaultExpectedValue)
|
||||||
|
QSKIP("Egyptian locale does not work on this system.");
|
||||||
|
QCOMPARE(bar.text(), egyptianExpectedValue);
|
||||||
|
|
||||||
|
bar.setLocale(QLocale());
|
||||||
|
QCOMPARE(bar.text(), defaultExpectedValue);
|
||||||
|
|
||||||
|
// Set a custom format containing only the number
|
||||||
|
bar.setFormat(QStringLiteral("%p"));
|
||||||
|
QCOMPARE(bar.text(), defaultExpectedNumber);
|
||||||
|
bar.setLocale(egypt);
|
||||||
|
QCOMPARE(bar.text(), egyptianExpectedNumber);
|
||||||
|
|
||||||
|
// Clear the format
|
||||||
|
bar.resetFormat();
|
||||||
|
QCOMPARE(bar.text(), egyptianExpectedValue);
|
||||||
|
bar.setLocale(QLocale());
|
||||||
|
QCOMPARE(bar.text(), defaultExpectedValue);
|
||||||
|
}
|
||||||
|
|
||||||
void tst_QProgressBar::task245201_testChangeStyleAndDelete_data()
|
void tst_QProgressBar::task245201_testChangeStyleAndDelete_data()
|
||||||
{
|
{
|
||||||
QTest::addColumn<QString>("style1_str");
|
QTest::addColumn<QString>("style1_str");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user