From 682630e62d588dace079a717e6cd4e43275a2db4 2014-09-17 10:49:00 From: Miikka Heikkinen Date: 2014-09-17 10:49:00 Subject: [PATCH] Additional fixes to label localization - The default bar/boxplot categories were not localized. - Might as well support "%i" format, as it is equivalent to "%d", and some of our own examples use it. - If precision is not specified in the label format, default to six instead of zero. Change-Id: I937b6a76128fc506d8db4b9974569e590d85ac5f Reviewed-by: Titta Heikkala --- diff --git a/src/axis/chartaxiselement.cpp b/src/axis/chartaxiselement.cpp index fb9be95..310555e 100644 --- a/src/axis/chartaxiselement.cpp +++ b/src/axis/chartaxiselement.cpp @@ -29,7 +29,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE static const char *labelFormatMatchString = "%[\\-\\+#\\s\\d\\.lhjztL]*([dicuoxfegXFEG])"; -static const char *labelFormatMatchLocalizedString = "^([^%]*)%\\.?(\\d)*([defgEG])(.*)$"; +static const char *labelFormatMatchLocalizedString = "^([^%]*)%\\.?(\\d)*([defgiEG])(.*)$"; static QRegExp *labelFormatMatcher = 0; static QRegExp *labelFormatMatcherLocalized = 0; class StaticLabelFormatMatcherDeleter @@ -295,13 +295,14 @@ QStringList ChartAxisElement::createValueLabels(qreal min, qreal max, int ticks, QString formatSpec; QString preStr; QString postStr; - int precision = 0; + int precision = 6; // Six is the default precision in Qt API if (presenter()->localizeNumbers()) { if (!labelFormatMatcherLocalized) labelFormatMatcherLocalized = new QRegExp(labelFormatMatchLocalizedString); if (labelFormatMatcherLocalized->indexIn(format, 0) != -1) { preStr = labelFormatMatcherLocalized->cap(1); - precision = labelFormatMatcherLocalized->cap(2).toInt(); + if (!labelFormatMatcherLocalized->cap(2).isEmpty()) + precision = labelFormatMatcherLocalized->cap(2).toInt(); formatSpec = labelFormatMatcherLocalized->cap(3); postStr = labelFormatMatcherLocalized->cap(4); } @@ -348,13 +349,14 @@ QStringList ChartAxisElement::createLogValueLabels(qreal min, qreal max, qreal b QString formatSpec; QString preStr; QString postStr; - int precision = 0; + int precision = 6; // Six is the default precision in Qt API if (presenter()->localizeNumbers()) { if (!labelFormatMatcherLocalized) labelFormatMatcherLocalized = new QRegExp(labelFormatMatchLocalizedString); if (labelFormatMatcherLocalized->indexIn(format, 0) != -1) { preStr = labelFormatMatcherLocalized->cap(1); - precision = labelFormatMatcherLocalized->cap(2).toInt(); + if (!labelFormatMatcherLocalized->cap(2).isEmpty()) + precision = labelFormatMatcherLocalized->cap(2).toInt(); formatSpec = labelFormatMatcherLocalized->cap(3); postStr = labelFormatMatcherLocalized->cap(4); } diff --git a/src/axis/valueaxis/qvalueaxis.cpp b/src/axis/valueaxis/qvalueaxis.cpp index d76bb0f..8656ebd 100644 --- a/src/axis/valueaxis/qvalueaxis.cpp +++ b/src/axis/valueaxis/qvalueaxis.cpp @@ -110,7 +110,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE See QString::sprintf() for additional details. If the QChart::localizeNumbers is \c{true}, the supported specifiers are limited to: d, e, E, f, - g, and G. Also, only the precision modifier is supported. The rest of the formatting comes from + g, G, and i. Also, only the precision modifier is supported. The rest of the formatting comes from the default QLocale of the application. */ /*! @@ -120,7 +120,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE See QString::sprintf() for additional details. If the ChartView::localizeNumbers is \c{true}, the supported specifiers are limited to: d, e, E, f, - g, and G. Also, only the precision modifier is supported. The rest of the formatting comes from + g, G, and i. Also, only the precision modifier is supported. The rest of the formatting comes from the default QLocale of the application. */ diff --git a/src/barchart/qabstractbarseries.cpp b/src/barchart/qabstractbarseries.cpp index 6b40197..564ee3a 100644 --- a/src/barchart/qabstractbarseries.cpp +++ b/src/barchart/qabstractbarseries.cpp @@ -950,7 +950,7 @@ void QAbstractBarSeriesPrivate::populateCategories(QBarCategoryAxis *axis) QStringList categories; if (axis->categories().isEmpty()) { for (int i(1); i < categoryCount() + 1; i++) - categories << QString::number(i); + categories << presenter()->numberToString(i); axis->append(categories); } } diff --git a/src/boxplotchart/qboxplotseries.cpp b/src/boxplotchart/qboxplotseries.cpp index c645d86..4f562d7 100644 --- a/src/boxplotchart/qboxplotseries.cpp +++ b/src/boxplotchart/qboxplotseries.cpp @@ -418,7 +418,7 @@ void QBoxPlotSeriesPrivate::populateCategories(QBarCategoryAxis *axis) for (int i(1); i < m_boxSets.count() + 1; i++) { QBoxSet *set = m_boxSets.at(i - 1); if (set->label().isEmpty()) - categories << QString::number(i); + categories << presenter()->numberToString(i); else categories << set->label(); } diff --git a/src/chartpresenter.cpp b/src/chartpresenter.cpp index 35b32bc..619f400 100644 --- a/src/chartpresenter.cpp +++ b/src/chartpresenter.cpp @@ -496,6 +496,14 @@ QString ChartPresenter::numberToString(double value, char f, int prec) return QString::number(value, f, prec); } +QString ChartPresenter::numberToString(int value) +{ + if (m_localizeNumbers) + return m_locale.toString(value); + else + return QString::number(value); +} + #include "moc_chartpresenter_p.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/chartpresenter_p.h b/src/chartpresenter_p.h index 8c9cffe..c72a12d 100644 --- a/src/chartpresenter_p.h +++ b/src/chartpresenter_p.h @@ -156,6 +156,7 @@ public: inline static qreal textMargin() { return qreal(0.5); } QString numberToString(double value, char f = 'g', int prec = 6); + QString numberToString(int value); private: void createBackgroundItem();