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:
parent
dca0a1119b
commit
224e1a620b
@ -29,7 +29,7 @@ Calendar::Calendar(QObject *parent)
|
||||
, m_weekList()
|
||||
, m_days(0)
|
||||
, m_weeks(0)
|
||||
, m_startDay(Qt::Sunday)
|
||||
, m_firstDayOfWeek(QLocale::system().firstDayOfWeek())
|
||||
, m_errorMessage()
|
||||
{
|
||||
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) {
|
||||
// set the errorString to some useful message and return.
|
||||
if (day > 7) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(m_startDay != day) {
|
||||
m_startDay = day;
|
||||
emit startDayChanged();
|
||||
if (m_firstDayOfWeek != day) {
|
||||
// QML has Sunday as 0, so we need to accomodate here
|
||||
// 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
|
||||
{
|
||||
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
|
||||
@ -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.
|
||||
daysBeforeCurrentMonth = firstDay.dayOfWeek();
|
||||
daysBeforeCurrentMonth = firstDay.dayOfWeek() - m_firstDayOfWeek;
|
||||
|
||||
int daysThusFar = daysBeforeCurrentMonth + m_startDate.daysInMonth();
|
||||
if(daysThusFar < totalDays) {
|
||||
|
@ -36,7 +36,7 @@ class Calendar : public QObject
|
||||
Q_PROPERTY(QList<int> weeksModel READ weeksModel CONSTANT)
|
||||
Q_PROPERTY(int days READ days WRITE setDays NOTIFY daysChanged)
|
||||
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(QString errorMessage READ errorMessage NOTIFY errorMessageChanged)
|
||||
Q_PROPERTY(QString monthName READ monthName NOTIFY monthNameChanged)
|
||||
@ -73,8 +73,8 @@ public:
|
||||
void setWeeks(int weeks);
|
||||
|
||||
// Start day
|
||||
int startDay();
|
||||
void setStartDay(int day);
|
||||
int firstDayOfWeek();
|
||||
void setFirstDayOfWeek(int day);
|
||||
|
||||
// Error message
|
||||
QString errorMessage() const;
|
||||
@ -101,7 +101,7 @@ Q_SIGNALS:
|
||||
void typesChanged();
|
||||
void daysChanged();
|
||||
void weeksChanged();
|
||||
void startDayChanged();
|
||||
void firstDayOfWeekChanged();
|
||||
void errorMessageChanged();
|
||||
void monthNameChanged();
|
||||
void yearChanged();
|
||||
@ -118,7 +118,7 @@ private:
|
||||
|
||||
int m_days;
|
||||
int m_weeks;
|
||||
int m_startDay;
|
||||
int m_firstDayOfWeek;
|
||||
QString m_errorMessage;
|
||||
};
|
||||
|
||||
|
@ -100,7 +100,7 @@ Item {
|
||||
width: cellWidth
|
||||
height: cellHeight
|
||||
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: theme.smallestFont
|
||||
opacity: 0.2
|
||||
|
@ -101,7 +101,7 @@ Item {
|
||||
|
||||
days: 7
|
||||
weeks: 6
|
||||
startDay: 1
|
||||
firstDayOfWeek: Qt.locale().firstDayOfWeek
|
||||
startDate: isTodayMonth();
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user