Make QCalendarWidget use its locale's firstDayOfWeek.
authorMitch Curtis <mitch.curtis@nokia.com>
Thu, 21 Jun 2012 15:44:04 +0000 (17:44 +0200)
committerQt by Nokia <qt-info@nokia.com>
Thu, 28 Jun 2012 15:15:17 +0000 (17:15 +0200)
QCalendarWidget currently uses Qt::Sunday as the default first day
of the week. It has been suggested that a better user experience
would see the calendar's locale be used instead.

Task-number: QTBUG-19811
Change-Id: I4441bf9ffd52213ef622a4a7f498530b7cc9e110
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Reviewed-by: Denis Dzyubenko <denis.dzyubenko@nokia.com>
examples/widgets/calendarwidget/window.cpp
src/widgets/widgets/qcalendarwidget.cpp
tests/auto/widgets/widgets/qcalendarwidget/tst_qcalendarwidget.cpp

index 6796ef7..8ba044b 100644 (file)
@@ -67,7 +67,10 @@ Window::Window()
 
 void Window::localeChanged(int index)
 {
-    calendar->setLocale(localeCombo->itemData(index).toLocale());
+    const QLocale newLocale(localeCombo->itemData(index).toLocale());
+    calendar->setLocale(newLocale);
+    int newLocaleFirstDayIndex = firstDayCombo->findData(newLocale.firstDayOfWeek());
+    firstDayCombo->setCurrentIndex(newLocaleFirstDayIndex);
 }
 
 //! [1]
index 5ac7348..7830076 100644 (file)
@@ -941,7 +941,7 @@ QCalendarModel::QCalendarModel(QObject *parent)
     m_maximumDate = QDate(7999, 12, 31);
     m_shownYear = m_date.year();
     m_shownMonth = m_date.month();
-    m_firstDay = Qt::Sunday;
+    m_firstDay = QLocale().firstDayOfWeek();
     m_horizontalHeaderFormat = QCalendarWidget::ShortDayNames;
     m_weekNumbersShown = true;
     m_firstColumn = 1;
@@ -1987,7 +1987,7 @@ void QCalendarWidgetPrivate::_q_editingFinished()
     A newly created calendar widget uses abbreviated day names, and
     both Saturdays and Sundays are marked in red. The calendar grid is
     not visible. The week numbers are displayed, and the first column
-    day is Sunday.
+    day is the first day of the week for the calendar's locale.
 
     The notation of the days can be altered to a single letter
     abbreviations ("M" for "Monday") by setting the
@@ -2696,7 +2696,8 @@ void QCalendarWidget::setSelectionMode(SelectionMode mode)
     \property QCalendarWidget::firstDayOfWeek
     \brief a value identifying the day displayed in the first column.
 
-    By default, the day displayed in the first column is Sunday
+    By default, the day displayed in the first column
+    is the first day of the week for the calendar's locale.
 */
 
 void QCalendarWidget::setFirstDayOfWeek(Qt::DayOfWeek dayOfWeek)
@@ -2997,6 +2998,7 @@ bool QCalendarWidget::event(QEvent *event)
         case QEvent::LayoutDirectionChange:
             d->updateButtonIcons();
         case QEvent::LocaleChange:
+            d->m_model->setFirstColumnDay(locale().firstDayOfWeek());
             d->cachedSizeHint = QSize();
             d->updateMonthMenuNames();
             d->updateNavigationBar();
index 4857900..1b48278 100644 (file)
@@ -73,6 +73,8 @@ private slots:
     void setWeekdayFormat();
     void showPrevNext_data();
     void showPrevNext();
+
+    void firstDayOfWeek();
 };
 
 // Testing get/set functions
@@ -361,5 +363,41 @@ void tst_QCalendarWidget::showPrevNext()
     }
 }
 
+void tst_QCalendarWidget::firstDayOfWeek()
+{
+    // Ensure the default locale is chosen.
+    QCalendarWidget calendar;
+    QLocale locale;
+    QCOMPARE(calendar.firstDayOfWeek(), locale.firstDayOfWeek());
+
+    QLocale germanLocale(QLocale::German);
+    QLocale::setDefault(germanLocale);
+    QCalendarWidget germanLocaleCalendar;
+    QCOMPARE(germanLocaleCalendar.firstDayOfWeek(), germanLocale.firstDayOfWeek());
+
+    // Ensure calling setLocale works as well.
+    QLocale frenchLocale(QLocale::French);
+    calendar.setLocale(frenchLocale);
+    QCOMPARE(calendar.firstDayOfWeek(), frenchLocale.firstDayOfWeek());
+
+    // Ensure that widget-specific locale takes precedence over default.
+    QLocale::setDefault(QLocale::English);
+    QCOMPARE(calendar.firstDayOfWeek(), frenchLocale.firstDayOfWeek());
+
+    // Ensure that setting the locale of parent widget has an effect.
+    QWidget* parent = new QWidget;
+    calendar.setParent(parent);
+    QLocale hausaLocale(QLocale::Hausa);
+    parent->setLocale(hausaLocale);
+    QCOMPARE(calendar.firstDayOfWeek(), hausaLocale.firstDayOfWeek());
+
+    // Ensure that widget-specific locale takes precedence over parent.
+    calendar.setLocale(germanLocale);
+    // Sanity check...
+    QCOMPARE(calendar.locale(), germanLocale);
+    QCOMPARE(parent->locale(), hausaLocale);
+    QCOMPARE(calendar.firstDayOfWeek(), germanLocale.firstDayOfWeek());
+}
+
 QTEST_MAIN(tst_QCalendarWidget)
 #include "tst_qcalendarwidget.moc"