<commit_before>#include "DateTimeAxis.hpp"

#include <QPainter>
#include <QDateTime>

namespace cutehmi {
namespace charts {

constexpr DateTimeAxis::interval_t DateTimeAxis::INITIAL_TICK_INTERVAL;
constexpr DateTimeAxis::interval_t DateTimeAxis::INITIAL_MINOR_TICK_INTERVAL;

DateTimeAxis::DateTimeAxis(QQuickItem * parent):
	LabeledAxis(parent),
	m_tickInterval(INITIAL_TICK_INTERVAL),
	m_minorTickInterval(INITIAL_MINOR_TICK_INTERVAL)
{
	switch (orientation()) {
		case Qt::Horizontal:
			connect(this, & DateTimeAxis::widthChanged, this, & DateTimeAxis::recalcRatio);
			break;
		case Qt::Vertical:
			connect(this, & DateTimeAxis::heightChanged, this, & DateTimeAxis::recalcRatio);
			break;
		default:
			qWarning("Unsupported orientation: %d.", orientation());
	}
	connect(this, & DateTimeAxis::fromChanged, this, & DateTimeAxis::recalcRatio);
	connect(this, & DateTimeAxis::toChanged, this, & DateTimeAxis::recalcRatio);

	connect(this, & ValueAxis::plotAreaChanged, this, & DateTimeAxis::connectPlotArea);

	connect(this, & DateTimeAxis::orientationChanged, this, & DateTimeAxis::connectOrientation);

	// Connect signals that should trigger paint.
	connect(this, & DateTimeAxis::colorChanged, this, & QQuickItem::update);
	connect(this, & DateTimeAxis::textColorChanged, this, & QQuickItem::update);
	connect(this, & DateTimeAxis::fontChanged, this, & QQuickItem::update);
	connect(this, & DateTimeAxis::thicknessChanged, this, & QQuickItem::update);
	connect(this, & DateTimeAxis::fromChanged, this, & QQuickItem::update);
	connect(this, & DateTimeAxis::toChanged, this, & QQuickItem::update);
	connect(this, & DateTimeAxis::tickIntervalChanged, this, & QQuickItem::update);
	connect(this, & DateTimeAxis::minorTickIntervalChanged, this, & QQuickItem::update);
	connect(this, & DateTimeAxis::axisPositionChanged, this, & QQuickItem::update);
	connect(this, & ValueAxis::plotAreaChanged, this, & QQuickItem::update);
}

DateTimeAxis::interval_t DateTimeAxis::tickInterval() const
{
	return m_tickInterval;
}

void DateTimeAxis::setTickInterval(interval_t interval)
{
	if (m_tickInterval != interval) {
		m_tickInterval = interval;
		emit tickIntervalChanged();
	}
}

DateTimeAxis::interval_t DateTimeAxis::minorTickInterval() const
{
	return m_minorTickInterval;
}

void DateTimeAxis::setMinorTickInterval(interval_t interval)
{
	if (m_minorTickInterval != interval) {
		m_minorTickInterval = interval;
		emit minorTickIntervalChanged();
	}
}

qreal DateTimeAxis::mapToPlotArea(qreal value) const
{
	switch (orientation()) {
		case Qt::Horizontal:
			return (value - from()) * m_screenScaleRatio;
		case Qt::Vertical:
			return (to() - value) * m_screenScaleRatio;
		default:
			return qQNaN();
	}
}

void DateTimeAxis::walkTicks(std::function<void(qreal tick)> callback) const
{
	QDateTime curTickDT = roundTick(from(), m_tickInterval);
	QDateTime endDate = QDateTime::fromMSecsSinceEpoch(to());

	while (curTickDT < endDate) {
		callback(curTickDT.toMSecsSinceEpoch());
		advanceTickDateTime(curTickDT, m_tickInterval);
	}
}

void DateTimeAxis::walkMinorTicks(std::function<void(qreal tick)> callback) const
{
	QDateTime curTickDT = roundTick(from(), m_minorTickInterval);
	QDateTime endDate = QDateTime::fromMSecsSinceEpoch(to());

	while (curTickDT < endDate) {
		callback(curTickDT.toMSecsSinceEpoch());
		advanceTickDateTime(curTickDT, m_minorTickInterval);
	}
}

void DateTimeAxis::walkTickLabels(std::function<void(qreal labelPos)> callback) const
{
	QDateTime curTickDT = roundTick(from(), m_tickInterval);
	// Put labels in between ticks.
	if ((m_tickInterval == DAYS) ||
		(m_tickInterval == WEEKS) ||
		(m_tickInterval == MONTHS) ||
		(m_tickInterval == YEARS)) {
		QDateTime nextTickDT = curTickDT;
		advanceTickDateTime(nextTickDT, m_tickInterval);
		curTickDT = curTickDT.addMSecs((nextTickDT.toMSecsSinceEpoch() - curTickDT.toMSecsSinceEpoch()) / 2.0);
	}
	QDateTime endDate = QDateTime::fromMSecsSinceEpoch(to());

	while (curTickDT < endDate) {
		callback(curTickDT.toMSecsSinceEpoch());
		advanceTickDateTime(curTickDT, m_tickInterval);
	}
}

QString DateTimeAxis::tickToString(qreal tick) const
{
	QDateTime tickDT = QDateTime::fromMSecsSinceEpoch(tick);
	// Restore original tick position of the labels in between ticks.
	if ((m_tickInterval == DAYS) ||
		(m_tickInterval == WEEKS) ||
		(m_tickInterval == MONTHS) ||
		(m_tickInterval == YEARS)) {
		QDateTime nextTickDT = tickDT;
		advanceTickDateTime(nextTickDT, m_tickInterval);
		tickDT = tickDT.addMSecs(-((nextTickDT.toMSecsSinceEpoch() - tickDT.toMSecsSinceEpoch()) / 2.0));
	}

	switch (m_tickInterval) {
		case MILLISECONDS:
			return tickDT.toString("zzz");
		case SECONDS:
			return tickDT.toString("ss");
		case MINUTES:
			return tickDT.toString("mm");
		case HOURS:
			return tickDT.toString("hh:mm");
		case DAYS:
			return tickDT.toString("ddd");
		case WEEKS:
			return QString::number(tickDT.date().weekNumber());
		case MONTHS:
			return tickDT.toString("MMM");
		case YEARS:
			return tickDT.toString("yyyy");
	}
	return QDateTime::fromMSecsSinceEpoch(tick).toString();<commit_msg>Remove unnecessary restore to original tick position.<commit_after>#include "DateTimeAxis.hpp"

#include <QPainter>
#include <QDateTime>

namespace cutehmi {
namespace charts {

constexpr DateTimeAxis::interval_t DateTimeAxis::INITIAL_TICK_INTERVAL;
constexpr DateTimeAxis::interval_t DateTimeAxis::INITIAL_MINOR_TICK_INTERVAL;

DateTimeAxis::DateTimeAxis(QQuickItem * parent):
	LabeledAxis(parent),
	m_tickInterval(INITIAL_TICK_INTERVAL),
	m_minorTickInterval(INITIAL_MINOR_TICK_INTERVAL)
{
	switch (orientation()) {
		case Qt::Horizontal:
			connect(this, & DateTimeAxis::widthChanged, this, & DateTimeAxis::recalcRatio);
			break;
		case Qt::Vertical:
			connect(this, & DateTimeAxis::heightChanged, this, & DateTimeAxis::recalcRatio);
			break;
		default:
			qWarning("Unsupported orientation: %d.", orientation());
	}
	connect(this, & DateTimeAxis::fromChanged, this, & DateTimeAxis::recalcRatio);
	connect(this, & DateTimeAxis::toChanged, this, & DateTimeAxis::recalcRatio);

	connect(this, & ValueAxis::plotAreaChanged, this, & DateTimeAxis::connectPlotArea);

	connect(this, & DateTimeAxis::orientationChanged, this, & DateTimeAxis::connectOrientation);

	// Connect signals that should trigger paint.
	connect(this, & DateTimeAxis::colorChanged, this, & QQuickItem::update);
	connect(this, & DateTimeAxis::textColorChanged, this, & QQuickItem::update);
	connect(this, & DateTimeAxis::fontChanged, this, & QQuickItem::update);
	connect(this, & DateTimeAxis::thicknessChanged, this, & QQuickItem::update);
	connect(this, & DateTimeAxis::fromChanged, this, & QQuickItem::update);
	connect(this, & DateTimeAxis::toChanged, this, & QQuickItem::update);
	connect(this, & DateTimeAxis::tickIntervalChanged, this, & QQuickItem::update);
	connect(this, & DateTimeAxis::minorTickIntervalChanged, this, & QQuickItem::update);
	connect(this, & DateTimeAxis::axisPositionChanged, this, & QQuickItem::update);
	connect(this, & ValueAxis::plotAreaChanged, this, & QQuickItem::update);
}

DateTimeAxis::interval_t DateTimeAxis::tickInterval() const
{
	return m_tickInterval;
}

void DateTimeAxis::setTickInterval(interval_t interval)
{
	if (m_tickInterval != interval) {
		m_tickInterval = interval;
		emit tickIntervalChanged();
	}
}

DateTimeAxis::interval_t DateTimeAxis::minorTickInterval() const
{
	return m_minorTickInterval;
}

void DateTimeAxis::setMinorTickInterval(interval_t interval)
{
	if (m_minorTickInterval != interval) {
		m_minorTickInterval = interval;
		emit minorTickIntervalChanged();
	}
}

qreal DateTimeAxis::mapToPlotArea(qreal value) const
{
	switch (orientation()) {
		case Qt::Horizontal:
			return (value - from()) * m_screenScaleRatio;
		case Qt::Vertical:
			return (to() - value) * m_screenScaleRatio;
		default:
			return qQNaN();
	}
}

void DateTimeAxis::walkTicks(std::function<void(qreal tick)> callback) const
{
	QDateTime curTickDT = roundTick(from(), m_tickInterval);
	QDateTime endDate = QDateTime::fromMSecsSinceEpoch(to());

	while (curTickDT < endDate) {
		callback(curTickDT.toMSecsSinceEpoch());
		advanceTickDateTime(curTickDT, m_tickInterval);
	}
}

void DateTimeAxis::walkMinorTicks(std::function<void(qreal tick)> callback) const
{
	QDateTime curTickDT = roundTick(from(), m_minorTickInterval);
	QDateTime endDate = QDateTime::fromMSecsSinceEpoch(to());

	while (curTickDT < endDate) {
		callback(curTickDT.toMSecsSinceEpoch());
		advanceTickDateTime(curTickDT, m_minorTickInterval);
	}
}

void DateTimeAxis::walkTickLabels(std::function<void(qreal labelPos)> callback) const
{
	QDateTime curTickDT = roundTick(from(), m_tickInterval);
	// Put labels in between ticks.
	if ((m_tickInterval == DAYS) ||
		(m_tickInterval == WEEKS) ||
		(m_tickInterval == MONTHS) ||
		(m_tickInterval == YEARS)) {
		QDateTime nextTickDT = curTickDT;
		advanceTickDateTime(nextTickDT, m_tickInterval);
		curTickDT = curTickDT.addMSecs((nextTickDT.toMSecsSinceEpoch() - curTickDT.toMSecsSinceEpoch()) / 2.0);
	}
	QDateTime endDate = QDateTime::fromMSecsSinceEpoch(to());

	while (curTickDT < endDate) {
		callback(curTickDT.toMSecsSinceEpoch());
		advanceTickDateTime(curTickDT, m_tickInterval);
	}
}

QString DateTimeAxis::tickToString(qreal tick) const
{
	QDateTime tickDT = QDateTime::fromMSecsSinceEpoch(tick);

	switch (m_tickInterval) {
		case MILLISECONDS:
			return tickDT.toString("zzz");
		case SECONDS:
			return tickDT.toString("ss");
		case MINUTES:
			return tickDT.toString("mm");
		case HOURS:
			return tickDT.toString("hh:mm");
		case DAYS:
			return tickDT.toString("ddd");
		case WEEKS:
			return QString::number(tickDT.date().weekNumber());
		case MONTHS:
			return tickDT.toString("MMM");
		case YEARS:
			return tickDT.toString("yyyy");
	}
	return QDateTime::fromMSecsSinceEpoch(tick).toString();
{'fraction_non_alphanumeric': 0.1302046218059656, 'fraction_numerical': 0.0006108113610913163, 'mean_word_length': 6.941794664510914, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2265161', 'n_tokens_mistral': 3372, 'n_tokens_neox': 3177, 'n_words': 583}