diff options
author | Edward Welbourne <edward.welbourne@qt.io> | 2019-10-25 16:43:47 +0200 |
---|---|---|
committer | Edward Welbourne <edward.welbourne@qt.io> | 2019-10-25 18:06:06 +0200 |
commit | 265b25a5c36a938ca13c016a09caf9fb9384e408 (patch) | |
tree | fb8919be82a5c30101c3a7dcf8280d0ab4e7e3ee /src/widgets/widgets | |
parent | 968cc365d24e3995d8456ed209fc1287a4935975 (diff) |
Specify year in month name lookups for QCalendarWidget
The compiler didn't complain at a QLocale::FormatType values being
passed for the int year parameters of the month-name functions, which
all have a default for their final QLocale::FormatType parameters. So
we didn't notice that the year parameter was missing until the bug was
reported.
Removed some code duplication by giving QCalendarModel a monthName()
method. Reworked QCalendarMonthValidator::text() to avoid repeated
calendar calculations (and use fewer braces).
This commit amends commit 2dee00621632ab8acd0b3d59bdba264afe2f32c1
Fixes: QTBUG-79495
Change-Id: Iad48c3b648a0139ab43511e6fb4e6a8f63a0495f
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/widgets/widgets')
-rw-r--r-- | src/widgets/widgets/qcalendarwidget.cpp | 35 |
1 files changed, 21 insertions, 14 deletions
diff --git a/src/widgets/widgets/qcalendarwidget.cpp b/src/widgets/widgets/qcalendarwidget.cpp index cc0c51b237..8593001f8b 100644 --- a/src/widgets/widgets/qcalendarwidget.cpp +++ b/src/widgets/widgets/qcalendarwidget.cpp @@ -309,15 +309,17 @@ QString QCalendarMonthValidator::text() const QString QCalendarMonthValidator::text(QDate date, QCalendar cal, int repeat) const { - if (repeat <= 1) { - return QString::number(date.month(cal)); - } else if (repeat == 2) { - return formatNumber(date.month(cal), 2); - } else if (repeat == 3) { - return cal.standaloneMonthName(m_locale, date.month(cal), QLocale::ShortFormat); - } else /*if (repeat >= 4)*/ { - return cal.standaloneMonthName(m_locale, date.month(cal), QLocale::LongFormat); - } + const auto parts = cal.partsFromDate(date); + // Numeric forms: + if (repeat <= 1) + return QString::number(parts.month); + if (repeat == 2) + return formatNumber(parts.month, 2); + // Text forms: + if (repeat == 3) + return cal.standaloneMonthName(m_locale, parts.month, parts.year, QLocale::ShortFormat); + /* repeat >= 4 */ + return cal.standaloneMonthName(m_locale, parts.month, parts.year, QLocale::LongFormat); } ////////////////////////////////// @@ -923,6 +925,11 @@ public: QDate referenceDate() const; int columnForFirstOfMonth(QDate date) const; + QString monthName(const QLocale &locale, int month) + { + return m_calendar.standaloneMonthName(locale, month, m_shownYear, QLocale::LongFormat); + } + int m_firstColumn; int m_firstRow; QCalendar m_calendar; @@ -1786,8 +1793,8 @@ void QCalendarWidgetPrivate::createNavigationBar(QWidget *widget) monthButton->setAutoRaise(true); monthButton->setPopupMode(QToolButton::InstantPopup); monthMenu = new QMenu(monthButton); - for (int i = 1; i <= 12; i++) { - QString monthName(m_model->m_calendar.standaloneMonthName(q->locale(), i, QLocale::LongFormat)); + for (int i = 1, e = m_model->m_calendar.maximumMonthsInYear(); i <= e; i++) { + QString monthName(m_model->monthName(q->locale(), i)); QAction *act = monthMenu->addAction(monthName); act->setData(i); monthToAction[i] = act; @@ -1876,7 +1883,7 @@ void QCalendarWidgetPrivate::updateMonthMenuNames() Q_Q(QCalendarWidget); for (int i = 1; i <= 12; i++) { - QString monthName(m_model->m_calendar.standaloneMonthName(q->locale(), i, QLocale::LongFormat)); + QString monthName(m_model->monthName(q->locale(), i)); monthToAction[i]->setText(monthName); } } @@ -1979,7 +1986,7 @@ void QCalendarWidgetPrivate::updateNavigationBar() { Q_Q(QCalendarWidget); - QString monthName = m_model->m_calendar.standaloneMonthName(q->locale(), m_model->m_shownMonth, QLocale::LongFormat); + QString monthName = m_model->monthName(q->locale(), m_model->m_shownMonth); monthButton->setText(monthName); yearEdit->setValue(m_model->m_shownYear); @@ -2277,7 +2284,7 @@ QSize QCalendarWidget::minimumSizeHint() const QFontMetrics fm = d->monthButton->fontMetrics(); int monthW = 0; for (int i = 1; i < 12; i++) { - QString monthName = d->m_model->m_calendar.standaloneMonthName(locale(), i, QLocale::LongFormat); + QString monthName = d->m_model->monthName(locale(), i); monthW = qMax(monthW, fm.boundingRect(monthName).width()); } const int buttonDecoMargin = d->monthButton->sizeHint().width() - fm.boundingRect(d->monthButton->text()).width(); |