diff --git a/src/charts/axis/cartesianchartaxis.cpp b/src/charts/axis/cartesianchartaxis.cpp index 30263754e..d853c3c96 100644 --- a/src/charts/axis/cartesianchartaxis.cpp +++ b/src/charts/axis/cartesianchartaxis.cpp @@ -76,6 +76,7 @@ void CartesianChartAxis::createItems(int count) if (labelsEditable()) dateTimeLabel->setEditable(true); dateTimeLabel->setFormat(static_cast(axis())->format()); + dateTimeLabel->setCalendar(static_cast(axis())->calendar()); #endif } else { label = new QGraphicsTextItem(this); @@ -285,6 +286,17 @@ void CartesianChartAxis::setDateTimeLabelsFormat(const QString &format) #endif } +void CartesianChartAxis::setDateTimeLabelsCalendar(const QCalendar &calendar) { + if (max() <= min() || layout().size() < 1 || + axis()->type() != QAbstractAxis::AxisTypeDateTime) { + return; + } +#if QT_CONFIG(charts_datetime_axis) + for (int i = 0; i < layout().size(); i++) + static_cast(labelItems().at(i))->setCalendar(calendar); +#endif +} + void CartesianChartAxis::handleArrowPenChanged(const QPen &pen) { foreach (QGraphicsItem *item, arrowItems()) diff --git a/src/charts/axis/cartesianchartaxis_p.h b/src/charts/axis/cartesianchartaxis_p.h index e713e4272..6c48c62b7 100644 --- a/src/charts/axis/cartesianchartaxis_p.h +++ b/src/charts/axis/cartesianchartaxis_p.h @@ -37,6 +37,8 @@ class Q_CHARTS_PRIVATE_EXPORT CartesianChartAxis : public ChartAxisElement QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const override; void setDateTimeLabelsFormat(const QString &format); + + void setDateTimeLabelsCalendar(const QCalendar &calendar); protected: void setGeometry(const QRectF &size) override { Q_UNUSED(size);} diff --git a/src/charts/axis/datetimeaxis/datetimeaxislabel.cpp b/src/charts/axis/datetimeaxis/datetimeaxislabel.cpp index 5f732233f..2e4e471d9 100644 --- a/src/charts/axis/datetimeaxis/datetimeaxislabel.cpp +++ b/src/charts/axis/datetimeaxis/datetimeaxislabel.cpp @@ -15,7 +15,7 @@ DateTimeAxisLabel::DateTimeAxisLabel(QGraphicsItem *parent) : void DateTimeAxisLabel::finishEditing() { QDateTime oldDateTime = m_dateTime; - QDateTime newDateTime = QDateTime::fromString(document()->toPlainText(), m_format); + QDateTime newDateTime = QDateTime::fromString(document()->toPlainText(), m_format, m_calendar); if (newDateTime.isValid() && newDateTime != m_dateTime) { m_dateTime = newDateTime; emit dateTimeChanged(oldDateTime, newDateTime); @@ -49,10 +49,14 @@ void DateTimeAxisLabel::setFormat(const QString &format) m_format.replace(QChar::fromLatin1('\n'), QChar::fromLatin1(' ')); } +void DateTimeAxisLabel::setCalendar(const QCalendar &calendar) { + m_calendar = calendar; +} + void DateTimeAxisLabel::setInitialEditValue() { m_dateTimeBeforeEdit = m_dateTime; - setHtml(m_dateTime.toString(m_format)); + setHtml(m_dateTime.toString(m_format, m_calendar)); } void DateTimeAxisLabel::keyPressEvent(QKeyEvent *event) diff --git a/src/charts/axis/datetimeaxis/datetimeaxislabel_p.h b/src/charts/axis/datetimeaxis/datetimeaxislabel_p.h index ce3973fd8..5ebeb0bb4 100644 --- a/src/charts/axis/datetimeaxis/datetimeaxislabel_p.h +++ b/src/charts/axis/datetimeaxis/datetimeaxislabel_p.h @@ -15,6 +15,7 @@ #include +#include #include QT_BEGIN_NAMESPACE @@ -30,11 +31,13 @@ class Q_CHARTS_PRIVATE_EXPORT DateTimeAxisLabel : public EditableAxisLabel QDateTime value() const; void setValue(const QDateTime &value); void setFormat(const QString &format); + void setCalendar(const QCalendar &calendar); private: QDateTime m_dateTime; QDateTime m_dateTimeBeforeEdit; QString m_format; + Calendar m_calendar; void setInitialEditValue() override; void finishEditing() override; diff --git a/src/charts/axis/datetimeaxis/qdatetimeaxis.cpp b/src/charts/axis/datetimeaxis/qdatetimeaxis.cpp index c0465d2f9..d0a2a2042 100644 --- a/src/charts/axis/datetimeaxis/qdatetimeaxis.cpp +++ b/src/charts/axis/datetimeaxis/qdatetimeaxis.cpp @@ -245,6 +245,19 @@ QString QDateTimeAxis::format() const return d->m_format; } +void QDateTimeAxis::setCalendar(QCalendar calendar) { + Q_D(QDateTimeAxis); + if (d->axisItem()) + static_cast(d->axisItem()) + ->setDateTimeLabelsCalendar(calendar); + // emit formatChanged(format); TODO +} + +QCalendar QDateTimeAxis::calendar() const { + Q_D(const QDateTimeAxis); + return d->m_calendar; +} + /*! Sets the number of tick marks on the axis to \a count. */ @@ -284,6 +297,7 @@ QDateTimeAxisPrivate::QDateTimeAxisPrivate(QDateTimeAxis *q) m_tickCount(5) { m_format = QStringLiteral("dd-MM-yyyy\nh:mm"); + m_calendar = QCalendar(QCalendar::System::Gregorian); } QDateTimeAxisPrivate::~QDateTimeAxisPrivate() diff --git a/src/charts/axis/datetimeaxis/qdatetimeaxis.h b/src/charts/axis/datetimeaxis/qdatetimeaxis.h index 91f730826..1f9bda787 100644 --- a/src/charts/axis/datetimeaxis/qdatetimeaxis.h +++ b/src/charts/axis/datetimeaxis/qdatetimeaxis.h @@ -8,6 +8,7 @@ QT_BEGIN_NAMESPACE class QDateTime; +class QCalendar; QT_END_NAMESPACE QT_BEGIN_NAMESPACE @@ -42,6 +43,9 @@ class Q_CHARTS_EXPORT QDateTimeAxis : public QAbstractAxis void setFormat(QString format); QString format() const; + void setCalendar(QCalendar calendar); + QCalendar calendar() const; + //ticks handling void setTickCount(int count); int tickCount() const; diff --git a/src/charts/axis/datetimeaxis/qdatetimeaxis_p.h b/src/charts/axis/datetimeaxis/qdatetimeaxis_p.h index 9be3c7723..169ee1705 100644 --- a/src/charts/axis/datetimeaxis/qdatetimeaxis_p.h +++ b/src/charts/axis/datetimeaxis/qdatetimeaxis_p.h @@ -49,6 +49,7 @@ class Q_CHARTS_PRIVATE_EXPORT QDateTimeAxisPrivate : public QAbstractAxisPrivate qreal m_max; int m_tickCount; QString m_format; + QCalendar m_calendar; Q_DECLARE_PUBLIC(QDateTimeAxis) };