Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add setCalendar function in qdatetimeaxis class #7

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/charts/axis/cartesianchartaxis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ void CartesianChartAxis::createItems(int count)
if (labelsEditable())
dateTimeLabel->setEditable(true);
dateTimeLabel->setFormat(static_cast<QDateTimeAxis*>(axis())->format());
dateTimeLabel->setCalendar(static_cast<QDateTimeAxis *>(axis())->calendar());
#endif
} else {
label = new QGraphicsTextItem(this);
Expand Down Expand Up @@ -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<DateTimeAxisLabel *>(labelItems().at(i))->setCalendar(calendar);
#endif
}

void CartesianChartAxis::handleArrowPenChanged(const QPen &pen)
{
foreach (QGraphicsItem *item, arrowItems())
Expand Down
2 changes: 2 additions & 0 deletions src/charts/axis/cartesianchartaxis_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);}
Expand Down
8 changes: 6 additions & 2 deletions src/charts/axis/datetimeaxis/datetimeaxislabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions src/charts/axis/datetimeaxis/datetimeaxislabel_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include <private/editableaxislabel_p.h>

#include <QtCore/qcalendar.h>
#include <QtCore/qdatetime.h>

QT_BEGIN_NAMESPACE
Expand All @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions src/charts/axis/datetimeaxis/qdatetimeaxis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<CartesianChartAxis *>(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.
*/
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions src/charts/axis/datetimeaxis/qdatetimeaxis.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

QT_BEGIN_NAMESPACE
class QDateTime;
class QCalendar;
QT_END_NAMESPACE

QT_BEGIN_NAMESPACE
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/charts/axis/datetimeaxis/qdatetimeaxis_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
};

Expand Down