From 3def7f4dd09f5d93f90d20eaedd17d8bd8c4a6fd Mon Sep 17 00:00:00 2001 From: Zeno Albisser Date: Tue, 22 Feb 2011 15:41:59 +0100 Subject: Implemented QLocale::weekendStart and QLocale::weekendEnd Reviewed-by: Denis Dzyubenko Task-number: QTBUG-17088 --- src/corelib/tools/qlocale.cpp | 38 ++++++++++++++++++++- src/corelib/tools/qlocale.h | 4 +++ src/corelib/tools/qlocale_p.h | 2 ++ tests/auto/qlocale/tst_qlocale.cpp | 11 ++++++ tests/manual/qlocale/calendar.cpp | 45 ++++++++++++++++-------- tests/manual/qlocale/calendar.h | 2 ++ util/local_database/cldr2qlocalexml.py | 62 ++++++++++++++++++++++++++++++++++ util/local_database/qlocalexml2cpp.py | 8 +++-- 8 files changed, 154 insertions(+), 18 deletions(-) diff --git a/src/corelib/tools/qlocale.cpp b/src/corelib/tools/qlocale.cpp index 61f7b7c..1f8e73c 100644 --- a/src/corelib/tools/qlocale.cpp +++ b/src/corelib/tools/qlocale.cpp @@ -1795,7 +1795,9 @@ QVariant QSystemLocale::query(QueryType /* type */, QVariant /* in */) const \value MeasurementSystem a QLocale::MeasurementSystem enum specifying the measurement system \value AMText a string that represents the system AM designator associated with a 12-hour clock. \value PMText a string that represents the system PM designator associated with a 12-hour clock. - \value FirstDayOfWeek a Qt::DayOfWeek enum specifiying the first day of the week + \value FirstDayOfWeek a Qt::DayOfWeek enum specifying the first day of the week + \value WeekendStart a Qt::DayOfWeek enum specifying the first day of the weekend + \value WeekendEnd a Qt::DayOfWeek enum specifying the last day of the weekend \value CurrencySymbol a string that represents a currency in a format QLocale::CurrencyFormat. \value FormatCurrency a localized string representation of a number with a currency symbol. \value UILanguages a list of strings representing locale names that could be used for UI translation. @@ -3941,6 +3943,40 @@ Qt::DayOfWeek QLocale::firstDayOfWeek() const } /*! + \since 4.8 + + Returns the first day of the weekend according to the current locale. +*/ +Qt::DayOfWeek QLocale::weekendStart() const +{ +#ifndef QT_NO_SYSTEMLOCALE + if (d() == systemPrivate()) { + QVariant res = systemLocale()->query(QSystemLocale::WeekendStart, QVariant()); + if (!res.isNull()) + return static_cast(res.toUInt()); + } +#endif + return static_cast(d()->m_weekend_start); +} + +/*! + \since 4.8 + + Returns the last day of the weekend according to the current locale. +*/ +Qt::DayOfWeek QLocale::weekendEnd() const +{ +#ifndef QT_NO_SYSTEMLOCALE + if (d() == systemPrivate()) { + QVariant res = systemLocale()->query(QSystemLocale::WeekendEnd, QVariant()); + if (!res.isNull()) + return static_cast(res.toUInt()); + } +#endif + return static_cast(d()->m_weekend_end); +} + +/*! \since 4.4 Returns the measurement system for the locale. diff --git a/src/corelib/tools/qlocale.h b/src/corelib/tools/qlocale.h index e62e2d9..38f8fa0 100644 --- a/src/corelib/tools/qlocale.h +++ b/src/corelib/tools/qlocale.h @@ -96,6 +96,8 @@ public: AMText, // QString PMText, // QString FirstDayOfWeek, // Qt::DayOfWeek + WeekendStart, // Qt::DayOfWeek + WeekendEnd, // Qt::DayOfWeek CurrencySymbol, // QString in: format FormatCurrency, // QString in: qlonglong, qulonglong or double UILanguages, // QStringList @@ -677,6 +679,8 @@ public: QString standaloneDayName(int, FormatType format = LongFormat) const; Qt::DayOfWeek firstDayOfWeek() const; + Qt::DayOfWeek weekendStart() const; + Qt::DayOfWeek weekendEnd() const; QString amText() const; QString pmText() const; diff --git a/src/corelib/tools/qlocale_p.h b/src/corelib/tools/qlocale_p.h index 1d286ab..1218e27 100644 --- a/src/corelib/tools/qlocale_p.h +++ b/src/corelib/tools/qlocale_p.h @@ -193,6 +193,8 @@ public: quint16 m_currency_digits : 2; quint16 m_currency_rounding : 3; quint16 m_first_day_of_week : 3; + quint16 m_weekend_start : 3; + quint16 m_weekend_end : 3; }; inline char QLocalePrivate::digitToCLocale(const QChar &in) const diff --git a/tests/auto/qlocale/tst_qlocale.cpp b/tests/auto/qlocale/tst_qlocale.cpp index 878a31e..913dbba 100644 --- a/tests/auto/qlocale/tst_qlocale.cpp +++ b/tests/auto/qlocale/tst_qlocale.cpp @@ -143,6 +143,7 @@ private slots: void currency(); void quoteString(); void uiLanguages(); + void weekendDays(); private: QString m_decimal, m_thousand, m_sdate, m_ldate, m_time; @@ -1124,6 +1125,9 @@ void tst_QLocale::macDefaultLocale() QCOMPARE(locale.dayName(7), QString("Sunday")); QCOMPARE(locale.monthName(1), QString("January")); QCOMPARE(locale.monthName(12), QString("December")); + QCOMPARE(locale.firstDayOfWeek(), Qt::Sunday); + QCOMPARE(locale.weekendStart(), Qt::Saturday); + QCOMPARE(locale.weekendEnd(), Qt::Sunday); QCOMPARE(locale.quoteString("string"), QString::fromUtf8("\xe2\x80\x9c" "string" "\xe2\x80\x9d")); QCOMPARE(locale.quoteString("string", QLocale::AlternateQuotation), QString::fromUtf8("\xe2\x80\x98" "string" "\xe2\x80\x99")); @@ -2185,5 +2189,12 @@ void tst_QLocale::uiLanguages() QCOMPARE(ru_RU.uiLanguages().at(0), QLatin1String("ru_RU")); } +void tst_QLocale::weekendDays() +{ + const QLocale c(QLocale::C); + QCOMPARE(c.weekendStart(), Qt::Saturday); + QCOMPARE(c.weekendEnd(), Qt::Sunday); +} + QTEST_APPLESS_MAIN(tst_QLocale) #include "tst_qlocale.moc" diff --git a/tests/manual/qlocale/calendar.cpp b/tests/manual/qlocale/calendar.cpp index b59fae6..615e44e 100644 --- a/tests/manual/qlocale/calendar.cpp +++ b/tests/manual/qlocale/calendar.cpp @@ -67,6 +67,7 @@ void CalendarWidget::localeChanged(QLocale locale) { calendar->setLocale(locale); firstDayCombo->setCurrentIndex(locale.firstDayOfWeek()-1); + updateWeekendDays(); } void CalendarWidget::firstDayChanged(int index) @@ -110,27 +111,41 @@ void CalendarWidget::maximumDateChanged(const QDate &date) minimumDateEdit->setDate(calendar->minimumDate()); } -void CalendarWidget::weekdayFormatChanged() -{ - QTextCharFormat format; +bool CalendarWidget::isWeekendDay(Qt::DayOfWeek day) { + Qt::DayOfWeek start = calendar->locale().weekendStart(); + Qt::DayOfWeek end = calendar->locale().weekendEnd(); - format.setForeground(qvariant_cast( + if (start <= day && day <= end) + return true; + if (start > end && (day >= start || day <= end)) + return true; + return false; +} + +void CalendarWidget::updateWeekendDays() { + QTextCharFormat weekFormat, weekendFormat; + weekFormat.setForeground(qvariant_cast( weekdayColorCombo->itemData(weekdayColorCombo->currentIndex()))); - calendar->setWeekdayTextFormat(Qt::Monday, format); - calendar->setWeekdayTextFormat(Qt::Tuesday, format); - calendar->setWeekdayTextFormat(Qt::Wednesday, format); - calendar->setWeekdayTextFormat(Qt::Thursday, format); - calendar->setWeekdayTextFormat(Qt::Friday, format); + weekendFormat.setForeground(qvariant_cast( + weekendColorCombo->itemData(weekendColorCombo->currentIndex()))); + + calendar->setWeekdayTextFormat(Qt::Monday, isWeekendDay(Qt::Monday) ? weekendFormat : weekFormat); + calendar->setWeekdayTextFormat(Qt::Tuesday, isWeekendDay(Qt::Tuesday) ? weekendFormat : weekFormat); + calendar->setWeekdayTextFormat(Qt::Wednesday, isWeekendDay(Qt::Wednesday) ? weekendFormat : weekFormat); + calendar->setWeekdayTextFormat(Qt::Thursday, isWeekendDay(Qt::Thursday) ? weekendFormat : weekFormat); + calendar->setWeekdayTextFormat(Qt::Friday, isWeekendDay(Qt::Friday) ? weekendFormat : weekFormat); + calendar->setWeekdayTextFormat(Qt::Saturday, isWeekendDay(Qt::Saturday) ? weekendFormat : weekFormat); + calendar->setWeekdayTextFormat(Qt::Sunday, isWeekendDay(Qt::Sunday) ? weekendFormat : weekFormat); } -void CalendarWidget::weekendFormatChanged() +void CalendarWidget::weekdayFormatChanged() { - QTextCharFormat format; + updateWeekendDays(); +} - format.setForeground(qvariant_cast( - weekendColorCombo->itemData(weekendColorCombo->currentIndex()))); - calendar->setWeekdayTextFormat(Qt::Saturday, format); - calendar->setWeekdayTextFormat(Qt::Sunday, format); +void CalendarWidget::weekendFormatChanged() +{ + updateWeekendDays(); } void CalendarWidget::reformatHeaders() diff --git a/tests/manual/qlocale/calendar.h b/tests/manual/qlocale/calendar.h index 3ac39c3..b5bb3c9 100644 --- a/tests/manual/qlocale/calendar.h +++ b/tests/manual/qlocale/calendar.h @@ -59,12 +59,14 @@ private slots: void selectedDateChanged(); void minimumDateChanged(const QDate &date); void maximumDateChanged(const QDate &date); + void updateWeekendDays(); void weekdayFormatChanged(); void weekendFormatChanged(); void reformatHeaders(); void reformatCalendarPage(); private: + bool isWeekendDay(Qt::DayOfWeek); void createPreviewGroupBox(); void createGeneralOptionsGroupBox(); void createDatesGroupBox(); diff --git a/util/local_database/cldr2qlocalexml.py b/util/local_database/cldr2qlocalexml.py index 0bc1664..c70cf18 100755 --- a/util/local_database/cldr2qlocalexml.py +++ b/util/local_database/cldr2qlocalexml.py @@ -413,6 +413,22 @@ def integrateWeekData(filePath): satFirstDayIn = findEntryInFile(filePath, "weekData/firstDay[day=sat]", attribute="territories")[0].split(" ") sunFirstDayIn = findEntryInFile(filePath, "weekData/firstDay[day=sun]", attribute="territories")[0].split(" ") + monWeekendStart = findEntryInFile(filePath, "weekData/weekendStart[day=mon]", attribute="territories")[0].split(" ") + tueWeekendStart = findEntryInFile(filePath, "weekData/weekendStart[day=tue]", attribute="territories")[0].split(" ") + wedWeekendStart = findEntryInFile(filePath, "weekData/weekendStart[day=wed]", attribute="territories")[0].split(" ") + thuWeekendStart = findEntryInFile(filePath, "weekData/weekendStart[day=thu]", attribute="territories")[0].split(" ") + friWeekendStart = findEntryInFile(filePath, "weekData/weekendStart[day=fri]", attribute="territories")[0].split(" ") + satWeekendStart = findEntryInFile(filePath, "weekData/weekendStart[day=sat]", attribute="territories")[0].split(" ") + sunWeekendStart = findEntryInFile(filePath, "weekData/weekendStart[day=sun]", attribute="territories")[0].split(" ") + + monWeekendEnd = findEntryInFile(filePath, "weekData/weekendEnd[day=mon]", attribute="territories")[0].split(" ") + tueWeekendEnd = findEntryInFile(filePath, "weekData/weekendEnd[day=tue]", attribute="territories")[0].split(" ") + wedWeekendEnd = findEntryInFile(filePath, "weekData/weekendEnd[day=wed]", attribute="territories")[0].split(" ") + thuWeekendEnd = findEntryInFile(filePath, "weekData/weekendEnd[day=thu]", attribute="territories")[0].split(" ") + friWeekendEnd = findEntryInFile(filePath, "weekData/weekendEnd[day=fri]", attribute="territories")[0].split(" ") + satWeekendEnd = findEntryInFile(filePath, "weekData/weekendEnd[day=sat]", attribute="territories")[0].split(" ") + sunWeekendEnd = findEntryInFile(filePath, "weekData/weekendEnd[day=sun]", attribute="territories")[0].split(" ") + firstDayByCountryCode = {} for countryCode in monFirstDayIn: firstDayByCountryCode[countryCode] = "mon" @@ -429,6 +445,38 @@ def integrateWeekData(filePath): for countryCode in sunFirstDayIn: firstDayByCountryCode[countryCode] = "sun" + weekendStartByCountryCode = {} + for countryCode in monWeekendStart: + weekendStartByCountryCode[countryCode] = "mon" + for countryCode in tueWeekendStart: + weekendStartByCountryCode[countryCode] = "tue" + for countryCode in wedWeekendStart: + weekendStartByCountryCode[countryCode] = "wed" + for countryCode in thuWeekendStart: + weekendStartByCountryCode[countryCode] = "thu" + for countryCode in friWeekendStart: + weekendStartByCountryCode[countryCode] = "fri" + for countryCode in satWeekendStart: + weekendStartByCountryCode[countryCode] = "sat" + for countryCode in sunWeekendStart: + weekendStartByCountryCode[countryCode] = "sun" + + weekendEndByCountryCode = {} + for countryCode in monWeekendEnd: + weekendEndByCountryCode[countryCode] = "mon" + for countryCode in tueWeekendEnd: + weekendEndByCountryCode[countryCode] = "tue" + for countryCode in wedWeekendEnd: + weekendEndByCountryCode[countryCode] = "wed" + for countryCode in thuWeekendEnd: + weekendEndByCountryCode[countryCode] = "thu" + for countryCode in friWeekendEnd: + weekendEndByCountryCode[countryCode] = "fri" + for countryCode in satWeekendEnd: + weekendEndByCountryCode[countryCode] = "sat" + for countryCode in sunWeekendEnd: + weekendEndByCountryCode[countryCode] = "sun" + for (key,locale) in locale_database.iteritems(): countryCode = locale['country_code'] if countryCode in firstDayByCountryCode: @@ -436,6 +484,16 @@ def integrateWeekData(filePath): else: locale_database[key]['firstDayOfWeek'] = firstDayByCountryCode["001"] + if countryCode in weekendStartByCountryCode: + locale_database[key]['weekendStart'] = weekendStartByCountryCode[countryCode] + else: + locale_database[key]['weekendStart'] = weekendStartByCountryCode["001"] + + if countryCode in weekendEndByCountryCode: + locale_database[key]['weekendEnd'] = weekendEndByCountryCode[countryCode] + else: + locale_database[key]['weekendEnd'] = weekendEndByCountryCode["001"] + if len(sys.argv) != 2: usage() @@ -611,6 +669,8 @@ print \ AM\n\ PM\n\ mon\n\ + sat\n\ + sun\n\ EEEE, d MMMM yyyy\n\ d MMM yyyy\n\ HH:mm:ss z\n\ @@ -659,6 +719,8 @@ for key in locale_keys: print " " + l['am'].encode('utf-8') + "" print " " + l['pm'].encode('utf-8') + "" print " " + l['firstDayOfWeek'].encode('utf-8') + "" + print " " + l['weekendStart'].encode('utf-8') + "" + print " " + l['weekendEnd'].encode('utf-8') + "" print " " + l['longDateFormat'].encode('utf-8') + "" print " " + l['shortDateFormat'].encode('utf-8') + "" print " " + l['longTimeFormat'].encode('utf-8') + "" diff --git a/util/local_database/qlocalexml2cpp.py b/util/local_database/qlocalexml2cpp.py index 6b8fdb9..86adf90 100755 --- a/util/local_database/qlocalexml2cpp.py +++ b/util/local_database/qlocalexml2cpp.py @@ -218,6 +218,8 @@ class Locale: self.am = eltText(firstChildElt(elt, "am")) self.pm = eltText(firstChildElt(elt, "pm")) self.firstDayOfWeek = convertToQtDayOfWeek(eltText(firstChildElt(elt, "firstDayOfWeek"))) + self.weekendStart = convertToQtDayOfWeek(eltText(firstChildElt(elt, "weekendStart"))) + self.weekendEnd = convertToQtDayOfWeek(eltText(firstChildElt(elt, "weekendEnd"))) self.longDateFormat = convertFormat(eltText(firstChildElt(elt, "longDateFormat"))) self.shortDateFormat = convertFormat(eltText(firstChildElt(elt, "shortDateFormat"))) self.longTimeFormat = convertFormat(eltText(firstChildElt(elt, "longTimeFormat"))) @@ -451,7 +453,7 @@ def main(): for key in locale_keys: l = locale_map[key] - data_temp_file.write(" { %6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s, {%s}, %s,%s,%s,%s,%6d,%6d,%6d }, // %s/%s\n" \ + data_temp_file.write(" { %6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%6d,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s, {%s}, %s,%s,%s,%s,%6d,%6d,%6d,%6d,%6d }, // %s/%s\n" \ % (key[0], key[1], l.decimal, l.group, @@ -491,9 +493,11 @@ def main(): l.currencyDigits, l.currencyRounding, l.firstDayOfWeek, + l.weekendStart, + l.weekendEnd, l.language, l.country)) - data_temp_file.write(" { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, {0,0,0}, 0,0, 0,0, 0,0, 0,0, 0, 0, 0 } // trailing 0s\n") + data_temp_file.write(" { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, 0,0, {0,0,0}, 0,0, 0,0, 0,0, 0,0, 0, 0, 0, 0, 0 } // trailing 0s\n") data_temp_file.write("};\n") data_temp_file.write("\n") -- cgit v0.12