added QAbstractSpinBox::setGroupSeparatorShown function.

[ChangeLog][QtWidgets][QAbstractSpinBox] QTBUG-5142 - This will
allow a group (thousand) separator to be shown in QSpinBox
and QDoubleSpinBox widgets.

Task-number: QTBUG-5142

Change-Id: I2e23f5f83c93bb092a2dbd784e06d17d40d42909
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
This commit is contained in:
Oto Magaldadze 2013-09-17 14:02:49 +04:00 committed by The Qt Project
parent fc5d07cee3
commit e913972d52
8 changed files with 163 additions and 8 deletions

View File

@ -94,6 +94,16 @@ void Window::createSpinBoxes()
priceSpinBox->setValue(99);
//! [4] //! [5]
groupSeparatorSpinBox = new QSpinBox;
groupSeparatorSpinBox->setRange(-99999999, 99999999);
groupSeparatorSpinBox->setValue(1000);
groupSeparatorSpinBox->setGroupSeparatorShown(true);
QCheckBox *groupSeparatorChkBox = new QCheckBox;
groupSeparatorChkBox->setText(tr("Show group separator"));
groupSeparatorChkBox->setChecked(true);
connect(groupSeparatorChkBox, &QCheckBox::toggled, groupSeparatorSpinBox,
&QSpinBox::setGroupSeparatorShown);
QLabel *hexLabel = new QLabel(tr("Enter a value between "
"%1 and %2:").arg('-' + QString::number(31, 16)).arg(QString::number(31, 16)));
QSpinBox *hexSpinBox = new QSpinBox;
@ -111,6 +121,8 @@ void Window::createSpinBoxes()
spinBoxLayout->addWidget(priceSpinBox);
spinBoxLayout->addWidget(hexLabel);
spinBoxLayout->addWidget(hexSpinBox);
spinBoxLayout->addWidget(groupSeparatorChkBox);
spinBoxLayout->addWidget(groupSeparatorSpinBox);
spinBoxesGroup->setLayout(spinBoxLayout);
}
//! [5]
@ -237,6 +249,17 @@ void Window::createDoubleSpinBoxes()
//! [17]
this, SLOT(changePrecision(int)));
groupSeparatorSpinBox_d = new QDoubleSpinBox;
groupSeparatorSpinBox_d->setRange(-99999999, 99999999);
groupSeparatorSpinBox_d->setDecimals(2);
groupSeparatorSpinBox_d->setValue(1000.00);
groupSeparatorSpinBox_d->setGroupSeparatorShown(true);
QCheckBox *groupSeparatorChkBox = new QCheckBox;
groupSeparatorChkBox->setText(tr("Show group separator"));
groupSeparatorChkBox->setChecked(true);
connect(groupSeparatorChkBox, &QCheckBox::toggled, groupSeparatorSpinBox_d,
&QDoubleSpinBox::setGroupSeparatorShown);
//! [18]
QVBoxLayout *spinBoxLayout = new QVBoxLayout;
spinBoxLayout->addWidget(precisionLabel);
@ -247,6 +270,8 @@ void Window::createDoubleSpinBoxes()
spinBoxLayout->addWidget(scaleSpinBox);
spinBoxLayout->addWidget(priceLabel);
spinBoxLayout->addWidget(priceSpinBox);
spinBoxLayout->addWidget(groupSeparatorChkBox);
spinBoxLayout->addWidget(groupSeparatorSpinBox_d);
doubleSpinBoxesGroup->setLayout(spinBoxLayout);
}
//! [18]

View File

@ -45,6 +45,7 @@
QT_BEGIN_NAMESPACE
class QDateTimeEdit;
class QSpinBox;
class QDoubleSpinBox;
class QGroupBox;
class QLabel;
@ -75,6 +76,8 @@ private:
QGroupBox *editsGroup;
QGroupBox *doubleSpinBoxesGroup;
QLabel *meetingLabel;
QSpinBox *groupSeparatorSpinBox;
QDoubleSpinBox *groupSeparatorSpinBox_d;
};
//! [0]

View File

@ -395,6 +395,30 @@ bool QAbstractSpinBox::isAccelerated() const
return d->accelerate;
}
/*!
\property QAbstractSpinBox::showGroupSeparator
\since 5.3
This property holds whether a thousands separator is enabled. By default this
property is false.
*/
bool QAbstractSpinBox::isGroupSeparatorShown() const
{
Q_D(const QAbstractSpinBox);
return d->showGroupSeparator;
}
void QAbstractSpinBox::setGroupSeparatorShown(bool shown)
{
Q_D(QAbstractSpinBox);
if (d->showGroupSeparator == shown)
return;
d->showGroupSeparator = shown;
d->setValue(d->value, EmitIfChanged);
updateGeometry();
}
/*!
\enum QAbstractSpinBox::CorrectionMode
@ -1316,7 +1340,8 @@ QAbstractSpinBoxPrivate::QAbstractSpinBoxPrivate()
cachedState(QValidator::Invalid), pendingEmit(false), readOnly(false), wrapping(false),
ignoreCursorPositionChanged(false), frame(true), accelerate(false), keyboardTracking(true),
cleared(false), ignoreUpdateEdit(false), correctionMode(QAbstractSpinBox::CorrectToPreviousValue),
acceleration(0), hoverControl(QStyle::SC_None), buttonSymbols(QAbstractSpinBox::UpDownArrows), validator(0)
acceleration(0), hoverControl(QStyle::SC_None), buttonSymbols(QAbstractSpinBox::UpDownArrows), validator(0),
showGroupSeparator(0)
{
}

View File

@ -72,6 +72,7 @@ class Q_WIDGETS_EXPORT QAbstractSpinBox : public QWidget
Q_PROPERTY(CorrectionMode correctionMode READ correctionMode WRITE setCorrectionMode)
Q_PROPERTY(bool acceptableInput READ hasAcceptableInput)
Q_PROPERTY(bool keyboardTracking READ keyboardTracking WRITE setKeyboardTracking)
Q_PROPERTY(bool showGroupSeparator READ isGroupSeparatorShown WRITE setGroupSeparatorShown)
public:
explicit QAbstractSpinBox(QWidget *parent = 0);
~QAbstractSpinBox();
@ -114,6 +115,9 @@ public:
void setAccelerated(bool on);
bool isAccelerated() const;
void setGroupSeparatorShown(bool shown);
bool isGroupSeparatorShown() const;
QSize sizeHint() const;
QSize minimumSizeHint() const;
void interpretText();

View File

@ -150,6 +150,7 @@ public:
QRect hoverRect;
QAbstractSpinBox::ButtonSymbols buttonSymbols;
QSpinBoxValidator *validator;
uint showGroupSeparator : 1;
};
class QSpinBoxValidator : public QValidator

View File

@ -463,8 +463,8 @@ void QSpinBox::setDisplayIntegerBase(int base)
display the given \a value. The default implementation returns a
string containing \a value printed in the standard way using
QWidget::locale().toString(), but with the thousand separator
removed. Reimplementations may return anything. (See the example
in the detailed description.)
removed unless setGroupSeparatorShown() is set. Reimplementations may
return anything. (See the example in the detailed description.)
Note: QSpinBox does not call this function for specialValueText()
and that neither prefix() nor suffix() should be included in the
@ -487,7 +487,7 @@ QString QSpinBox::textFromValue(int value) const
str.prepend('-');
} else {
str = locale().toString(value);
if (qAbs(value) >= 1000 || value == INT_MIN) {
if (!d->showGroupSeparator && (qAbs(value) >= 1000 || value == INT_MIN)) {
str.remove(locale().groupSeparator());
}
}
@ -538,7 +538,8 @@ QValidator::State QSpinBox::validate(QString &text, int &pos) const
*/
void QSpinBox::fixup(QString &input) const
{
input.remove(locale().groupSeparator());
if (!isGroupSeparatorShown())
input.remove(locale().groupSeparator());
}
@ -891,7 +892,8 @@ void QDoubleSpinBox::setDecimals(int decimals)
display the given \a value. The default implementation returns a string
containing \a value printed using QWidget::locale().toString(\a value,
QLatin1Char('f'), decimals()) and will remove the thousand
separator. Reimplementations may return anything.
separator unless setGroupSeparatorShown() is set. Reimplementations may
return anything.
Note: QDoubleSpinBox does not call this function for
specialValueText() and that neither prefix() nor suffix() should
@ -908,9 +910,9 @@ QString QDoubleSpinBox::textFromValue(double value) const
{
Q_D(const QDoubleSpinBox);
QString str = locale().toString(value, 'f', d->decimals);
if (qAbs(value) >= 1000.0) {
if (!d->showGroupSeparator && qAbs(value) >= 1000.0)
str.remove(locale().groupSeparator());
}
return str;
}

View File

@ -145,6 +145,9 @@ private slots:
void taskQTBUG_6670_selectAllWithPrefix();
void taskQTBUG_6496_fiddlingWithPrecision();
void setGroupSeparatorShown_data();
void setGroupSeparatorShown();
public slots:
void valueChangedHelper(const QString &);
void valueChangedHelper(double);
@ -156,6 +159,9 @@ private:
typedef QList<double> DoubleList;
Q_DECLARE_METATYPE(QLocale::Language)
Q_DECLARE_METATYPE(QLocale::Country)
tst_QDoubleSpinBox::tst_QDoubleSpinBox()
{
@ -1099,5 +1105,45 @@ void tst_QDoubleSpinBox::taskQTBUG_6496_fiddlingWithPrecision()
QCOMPARE(dsb.maximum(), 0.991);
}
void tst_QDoubleSpinBox::setGroupSeparatorShown_data()
{
QTest::addColumn<QLocale::Language>("lang");
QTest::addColumn<QLocale::Country>("country");
QTest::newRow("data0") << QLocale::English << QLocale::UnitedStates;
QTest::newRow("data1") << QLocale::Swedish << QLocale::Sweden;
QTest::newRow("data2") << QLocale::German << QLocale::Germany;
QTest::newRow("data3") << QLocale::Georgian << QLocale::Georgia;
QTest::newRow("data3") << QLocale::Macedonian << QLocale::Macedonia;
}
void tst_QDoubleSpinBox::setGroupSeparatorShown()
{
QFETCH(QLocale::Language, lang);
QFETCH(QLocale::Country, country);
QLocale loc(lang, country);
QLocale::setDefault(loc);
DoubleSpinBox spinBox;
spinBox.setMaximum(99999999);
spinBox.setValue(1300000.00);
spinBox.setGroupSeparatorShown(true);
QCOMPARE(spinBox.lineEdit()->text(), spinBox.locale().toString(1300000.00, 'f', 2));
QCOMPARE(spinBox.isGroupSeparatorShown(), true);
QCOMPARE(spinBox.textFromValue(23421),spinBox.locale().toString(23421.00, 'f', 2));
spinBox.setGroupSeparatorShown(false);
QCOMPARE(spinBox.lineEdit()->text(), spinBox.locale().toString(1300000.00, 'f', 2).remove(
spinBox.locale().groupSeparator()));
QCOMPARE(spinBox.isGroupSeparatorShown(), false);
spinBox.setMaximum(72000);
spinBox.lineEdit()->setText(spinBox.locale().toString(32000.64, 'f', 2));
QCOMPARE(spinBox.value()+1000, 33000.64);
spinBox.lineEdit()->setText(spinBox.locale().toString(32000.44, 'f', 2));
QCOMPARE(spinBox.value()+1000, 33000.44);
}
QTEST_MAIN(tst_QDoubleSpinBox)
#include "tst_qdoublespinbox.moc"

View File

@ -142,6 +142,10 @@ private slots:
void taskQTBUG_5008_textFromValueAndValidate();
void lineEditReturnPressed();
void setGroupSeparatorShown_data();
void setGroupSeparatorShown();
public slots:
void valueChangedHelper(const QString &);
void valueChangedHelper(int);
@ -152,6 +156,9 @@ private:
typedef QList<int> IntList;
Q_DECLARE_METATYPE(QLocale::Language)
Q_DECLARE_METATYPE(QLocale::Country)
// Testing get/set functions
void tst_QSpinBox::getSetCheck()
{
@ -1111,5 +1118,47 @@ void tst_QSpinBox::lineEditReturnPressed()
QCOMPARE(spyCurrentChanged.count(), 1);
}
void tst_QSpinBox::setGroupSeparatorShown_data()
{
QTest::addColumn<QLocale::Language>("lang");
QTest::addColumn<QLocale::Country>("country");
QTest::newRow("data0") << QLocale::English << QLocale::UnitedStates;
QTest::newRow("data1") << QLocale::Swedish << QLocale::Sweden;
QTest::newRow("data2") << QLocale::German << QLocale::Germany;
QTest::newRow("data3") << QLocale::Georgian << QLocale::Georgia;
QTest::newRow("data3") << QLocale::Macedonian << QLocale::Macedonia;
}
void tst_QSpinBox::setGroupSeparatorShown()
{
QFETCH(QLocale::Language, lang);
QFETCH(QLocale::Country, country);
QLocale loc(lang, country);
QLocale::setDefault(loc);
SpinBox spinBox;
spinBox.setMaximum(99999);
spinBox.setValue(13000);
spinBox.setGroupSeparatorShown(true);
QCOMPARE(spinBox.lineEdit()->text(), spinBox.locale().toString(13000));
QCOMPARE(spinBox.isGroupSeparatorShown(), true);
QCOMPARE(spinBox.textFromValue(23421),spinBox.locale().toString(23421));
spinBox.setGroupSeparatorShown(false);
QCOMPARE(spinBox.lineEdit()->text(), QStringLiteral("13000"));
QCOMPARE(spinBox.isGroupSeparatorShown(), false);
spinBox.setMaximum(72000);
spinBox.lineEdit()->setText(spinBox.locale().toString(32000));
QCOMPARE(spinBox.value()+1000, 33000);
spinBox.lineEdit()->setText(QStringLiteral("32000"));
QCOMPARE(spinBox.value()+1000, 33000);
spinBox.lineEdit()->setText(QStringLiteral("32,000"));
QCOMPARE(spinBox.value()+1000, 33000);
}
QTEST_MAIN(tst_QSpinBox)
#include "tst_qspinbox.moc"