Make the calendar applet follow current locale with first day in week

Also rename startDay property to firstDayOfWeek property so it's more
clear what it does. Handling the first day of week is changed a bit too
as QML uses 0 for Sunday while QDate uses 7 for Sunday, so the
getter/setter is accomodated to that fact and converts from 0 to 7 on
setting and vice-versa in the getter.

REVIEW: 114099
This commit is contained in:
Martin Klapetek 2013-11-25 13:24:30 +01:00
parent dca0a1119b
commit 224e1a620b
4 changed files with 28 additions and 18 deletions

View File

@ -29,7 +29,7 @@ Calendar::Calendar(QObject *parent)
, m_weekList() , m_weekList()
, m_days(0) , m_days(0)
, m_weeks(0) , m_weeks(0)
, m_startDay(Qt::Sunday) , m_firstDayOfWeek(QLocale::system().firstDayOfWeek())
, m_errorMessage() , m_errorMessage()
{ {
m_daysModel = new DaysModel(this); m_daysModel = new DaysModel(this);
@ -99,21 +99,29 @@ void Calendar::setWeeks(int weeks)
} }
} }
int Calendar::startDay() int Calendar::firstDayOfWeek()
{ {
return m_startDay; // QML has Sunday as 0, so we need to accomodate here
return m_firstDayOfWeek == 7 ? 0 : m_firstDayOfWeek;
} }
void Calendar::setStartDay(int day) void Calendar::setFirstDayOfWeek(int day)
{ {
if(day > 7 || day < 1) { if (day > 7) {
// set the errorString to some useful message and return.
return; return;
} }
if(m_startDay != day) { if (m_firstDayOfWeek != day) {
m_startDay = day; // QML has Sunday as 0, so we need to accomodate here
emit startDayChanged(); // for QDate functions which have Sunday as 7
if (day == 0) {
m_firstDayOfWeek = 7;
} else {
m_firstDayOfWeek = day;
}
updateData();
emit firstDayOfWeekChanged();
} }
} }
@ -124,7 +132,9 @@ QString Calendar::errorMessage() const
QString Calendar::monthName() const QString Calendar::monthName() const
{ {
return QDate::longMonthName(m_startDate.month()); // Capitalize the first letter
QString tmp = QDate::longMonthName(m_startDate.month(), QDate::StandaloneFormat);
return tmp.left(1).toUpper() + tmp.mid(1);
} }
int Calendar::year() const int Calendar::year() const
@ -159,7 +169,7 @@ void Calendar::updateData()
// If the first day is the same as the starting day then we add a complete row before it. // If the first day is the same as the starting day then we add a complete row before it.
daysBeforeCurrentMonth = firstDay.dayOfWeek(); daysBeforeCurrentMonth = firstDay.dayOfWeek() - m_firstDayOfWeek;
int daysThusFar = daysBeforeCurrentMonth + m_startDate.daysInMonth(); int daysThusFar = daysBeforeCurrentMonth + m_startDate.daysInMonth();
if(daysThusFar < totalDays) { if(daysThusFar < totalDays) {

View File

@ -36,7 +36,7 @@ class Calendar : public QObject
Q_PROPERTY(QList<int> weeksModel READ weeksModel CONSTANT) Q_PROPERTY(QList<int> weeksModel READ weeksModel CONSTANT)
Q_PROPERTY(int days READ days WRITE setDays NOTIFY daysChanged) Q_PROPERTY(int days READ days WRITE setDays NOTIFY daysChanged)
Q_PROPERTY(int weeks READ weeks WRITE setWeeks NOTIFY weeksChanged) Q_PROPERTY(int weeks READ weeks WRITE setWeeks NOTIFY weeksChanged)
Q_PROPERTY(int startDay READ startDay WRITE setStartDay NOTIFY startDayChanged) Q_PROPERTY(int firstDayOfWeek READ firstDayOfWeek WRITE setFirstDayOfWeek NOTIFY firstDayOfWeekChanged)
Q_PROPERTY(int year READ year NOTIFY yearChanged) Q_PROPERTY(int year READ year NOTIFY yearChanged)
Q_PROPERTY(QString errorMessage READ errorMessage NOTIFY errorMessageChanged) Q_PROPERTY(QString errorMessage READ errorMessage NOTIFY errorMessageChanged)
Q_PROPERTY(QString monthName READ monthName NOTIFY monthNameChanged) Q_PROPERTY(QString monthName READ monthName NOTIFY monthNameChanged)
@ -73,8 +73,8 @@ public:
void setWeeks(int weeks); void setWeeks(int weeks);
// Start day // Start day
int startDay(); int firstDayOfWeek();
void setStartDay(int day); void setFirstDayOfWeek(int day);
// Error message // Error message
QString errorMessage() const; QString errorMessage() const;
@ -101,7 +101,7 @@ Q_SIGNALS:
void typesChanged(); void typesChanged();
void daysChanged(); void daysChanged();
void weeksChanged(); void weeksChanged();
void startDayChanged(); void firstDayOfWeekChanged();
void errorMessageChanged(); void errorMessageChanged();
void monthNameChanged(); void monthNameChanged();
void yearChanged(); void yearChanged();
@ -118,7 +118,7 @@ private:
int m_days; int m_days;
int m_weeks; int m_weeks;
int m_startDay; int m_firstDayOfWeek;
QString m_errorMessage; QString m_errorMessage;
}; };

View File

@ -100,7 +100,7 @@ Item {
width: cellWidth width: cellWidth
height: cellHeight height: cellHeight
Components.Label { Components.Label {
text: Qt.formatDate(new Date(showDate.getFullYear(), showDate.getMonth(), index - firstDay +1), "ddd"); text: Qt.locale().dayName(monthCalendar.firstDayOfWeek + index, Locale.ShortFormat)
font.pixelSize: Math.max(theme.smallestFont.pixelSize, root.cellHeight / 6) font.pixelSize: Math.max(theme.smallestFont.pixelSize, root.cellHeight / 6)
//font: theme.smallestFont //font: theme.smallestFont
opacity: 0.2 opacity: 0.2

View File

@ -101,7 +101,7 @@ Item {
days: 7 days: 7
weeks: 6 weeks: 6
startDay: 1 firstDayOfWeek: Qt.locale().firstDayOfWeek
startDate: isTodayMonth(); startDate: isTodayMonth();
} }