summaryrefslogtreecommitdiffstats
path: root/src/widgets/widgets/qdatetimeedit.cpp
diff options
context:
space:
mode:
authorSoroush Rabiei <soroush@ametisco.ir>2019-08-08 15:19:18 +0200
committerEdward Welbourne <edward.welbourne@qt.io>2019-08-22 12:10:06 +0200
commit2dee00621632ab8acd0b3d59bdba264afe2f32c1 (patch)
tree45d59d948997e46f5e9916f388306cbaed87807a /src/widgets/widgets/qdatetimeedit.cpp
parent702664571252e4f5705e39a8258fc81a3fbf37d4 (diff)
Adapt QCalendarWidget and QDateTimeEdit to support choice of calendar
[ChangeLog][QtWidgets][QCalendarWidget] Allow choice of calendar, with Gregorian remaining the default. [ChangeLog][QtWidgets][QDateTimeEdit] Allow choice of calendar, with Gregorian remaining the default. Change-Id: Ia11dd895032393543aee534b177324e643dcfb20 Done-with: Edward Welbourne <edward.welbourne@qt.io> Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/widgets/widgets/qdatetimeedit.cpp')
-rw-r--r--src/widgets/widgets/qdatetimeedit.cpp50
1 files changed, 31 insertions, 19 deletions
diff --git a/src/widgets/widgets/qdatetimeedit.cpp b/src/widgets/widgets/qdatetimeedit.cpp
index 17dc94bba1..31607d77e7 100644
--- a/src/widgets/widgets/qdatetimeedit.cpp
+++ b/src/widgets/widgets/qdatetimeedit.cpp
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2016 The Qt Company Ltd.
+** Copyright (C) 2019 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtWidgets module of the Qt Toolkit.
@@ -312,6 +312,22 @@ void QDateTimeEdit::setTime(const QTime &time)
}
+QCalendar QDateTimeEdit::calendar() const
+{
+ Q_D(const QDateTimeEdit);
+ return d->calendar;
+}
+
+void QDateTimeEdit::setCalendar(QCalendar calendar)
+{
+ Q_D(QDateTimeEdit);
+ // Set invalid date time to prevent runtime crashes on calendar change
+ QDateTime previousValue = d->value.toDateTime();
+ setDateTime(QDateTime());
+ d->setCalendar(calendar);
+ setDateTime(previousValue);
+}
+
/*!
\property QDateTimeEdit::minimumDateTime
\since 4.4
@@ -1344,7 +1360,7 @@ void QDateTimeEdit::stepBy(int steps)
QString QDateTimeEdit::textFromDateTime(const QDateTime &dateTime) const
{
Q_D(const QDateTimeEdit);
- return locale().toString(dateTime, d->displayFormat);
+ return locale().toString(dateTime, d->displayFormat, d->calendar);
}
@@ -1636,7 +1652,7 @@ QDateEdit::~QDateEdit()
QDateTimeEditPrivate::QDateTimeEditPrivate()
- : QDateTimeParser(QVariant::DateTime, QDateTimeParser::DateTimeEdit)
+ : QDateTimeParser(QVariant::DateTime, QDateTimeParser::DateTimeEdit, QCalendar())
{
hasHadFocus = false;
formatExplicitlySet = false;
@@ -1661,10 +1677,6 @@ QDateTimeEditPrivate::QDateTimeEditPrivate()
#endif
}
-QDateTimeEditPrivate::~QDateTimeEditPrivate()
-{
-}
-
void QDateTimeEditPrivate::updateTimeSpec()
{
minimum = minimum.toDateTime().toTimeSpec(spec);
@@ -2008,8 +2020,7 @@ QDateTime QDateTimeEditPrivate::stepBy(int sectionIndex, int steps, bool test) c
val = (wrapping ? min + val - max - 1 : max);
}
-
- const int oldDay = v.date().day();
+ const int oldDay = v.date().day(calendar);
setDigit(v, sectionIndex, val);
// if this sets year or month it will make
@@ -2028,10 +2039,10 @@ QDateTime QDateTimeEditPrivate::stepBy(int sectionIndex, int steps, bool test) c
if (steps > 0) {
setDigit(v, sectionIndex, min);
if (!(sn.type & DaySectionMask) && sections & DateSectionMask) {
- const int daysInMonth = v.date().daysInMonth();
- if (v.date().day() < oldDay && v.date().day() < daysInMonth) {
+ const int daysInMonth = v.date().daysInMonth(calendar);
+ if (v.date().day(calendar) < oldDay && v.date().day(calendar) < daysInMonth) {
const int adds = qMin(oldDay, daysInMonth);
- v = v.addDays(adds - v.date().day());
+ v = v.addDays(adds - v.date().day(calendar));
}
}
@@ -2043,10 +2054,10 @@ QDateTime QDateTimeEditPrivate::stepBy(int sectionIndex, int steps, bool test) c
} else {
setDigit(v, sectionIndex, max);
if (!(sn.type & DaySectionMask) && sections & DateSectionMask) {
- const int daysInMonth = v.date().daysInMonth();
- if (v.date().day() < oldDay && v.date().day() < daysInMonth) {
+ const int daysInMonth = v.date().daysInMonth(calendar);
+ if (v.date().day(calendar) < oldDay && v.date().day(calendar) < daysInMonth) {
const int adds = qMin(oldDay, daysInMonth);
- v = v.addDays(adds - v.date().day());
+ v = v.addDays(adds - v.date().day(calendar));
}
}
@@ -2060,7 +2071,7 @@ QDateTime QDateTimeEditPrivate::stepBy(int sectionIndex, int steps, bool test) c
setDigit(v, sectionIndex, (steps > 0 ? localmax : localmin));
}
}
- if (!test && oldDay != v.date().day() && !(sn.type & DaySectionMask)) {
+ if (!test && oldDay != v.date().day(calendar) && !(sn.type & DaySectionMask)) {
// this should not happen when called from stepEnabled
cachedDay = qMax<int>(oldDay, cachedDay);
}
@@ -2513,7 +2524,7 @@ void QDateTimeEditPrivate::initCalendarPopup(QCalendarWidget *cw)
{
Q_Q(QDateTimeEdit);
if (!monthCalendar) {
- monthCalendar = new QCalendarPopup(q, cw);
+ monthCalendar = new QCalendarPopup(q, cw, calendar);
monthCalendar->setObjectName(QLatin1String("qt_datetimedit_calendar"));
QObject::connect(monthCalendar, SIGNAL(newDateSelected(QDate)), q, SLOT(setDate(QDate)));
QObject::connect(monthCalendar, SIGNAL(hidingCalendar(QDate)), q, SLOT(setDate(QDate)));
@@ -2574,8 +2585,8 @@ void QDateTimeEditPrivate::syncCalendarWidget()
}
}
-QCalendarPopup::QCalendarPopup(QWidget * parent, QCalendarWidget *cw)
- : QWidget(parent, Qt::Popup)
+QCalendarPopup::QCalendarPopup(QWidget *parent, QCalendarWidget *cw, QCalendar ca)
+ : QWidget(parent, Qt::Popup), calendarSystem(ca)
{
setAttribute(Qt::WA_WindowPropagation);
@@ -2591,6 +2602,7 @@ QCalendarWidget *QCalendarPopup::verifyCalendarInstance()
{
if (calendar.isNull()) {
QCalendarWidget *cw = new QCalendarWidget(this);
+ cw->setCalendar(calendarSystem);
cw->setVerticalHeaderFormat(QCalendarWidget::NoVerticalHeader);
#ifdef QT_KEYPAD_NAVIGATION
if (QApplicationPrivate::keypadNavigationEnabled())