diff options
author | Miklós Márton <martonmiklosqdev@gmail.com> | 2018-10-28 18:04:41 +0100 |
---|---|---|
committer | Miikka Heikkinen <miikka.heikkinen@qt.io> | 2018-11-06 09:42:16 +0000 |
commit | 10890cdc849c8537b1951ff54eff801aa6ac502f (patch) | |
tree | 6af6c5e26833a2c9d29f327d75761dbec4e5be8d /src/charts/axis/chartaxiselement.cpp | |
parent | 796419e32863c1d322d5f9af1fedd032bdd738d7 (diff) |
Implement label editing feature in the QDateTimeAxis
Implement the labelsEditable property handling in the QDateTimeAxis.
If this property set to true the user will be able to manipulate
the axis labels by double clicking on it and entering a new date
in the same format as it is displayed. After finishing the edit
the axis range will be adjusted according to the entered date.
Change-Id: Ide45982875924c8bb4e937b47511dfa3c9237750
Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io>
Diffstat (limited to 'src/charts/axis/chartaxiselement.cpp')
-rw-r--r-- | src/charts/axis/chartaxiselement.cpp | 55 |
1 files changed, 48 insertions, 7 deletions
diff --git a/src/charts/axis/chartaxiselement.cpp b/src/charts/axis/chartaxiselement.cpp index bf33d330..62da84f4 100644 --- a/src/charts/axis/chartaxiselement.cpp +++ b/src/charts/axis/chartaxiselement.cpp @@ -157,13 +157,13 @@ void ChartAxisElement::handleLabelsPositionChanged() presenter()->layout()->invalidate(); } -void ChartAxisElement::labelEdited(qreal oldValue, qreal newValue) +void ChartAxisElement::valueLabelEdited(qreal oldValue, qreal newValue) { qreal range = max() - min(); - qreal center = ((max() - min()) / 2) + min(); + qreal center = ((max() - min()) / 2.0) + min(); qreal newRange = 0.0; auto label = static_cast<ValueAxisLabel *>(this->sender()); - if ((oldValue >= center && newValue >= min()) + if ((oldValue >= center && newValue >= min()) || (oldValue < center && newValue >= max() && oldValue != min())) { newRange = range * ((newValue - min()) / (oldValue - min())); if (newRange > 0) { @@ -171,7 +171,7 @@ void ChartAxisElement::labelEdited(qreal oldValue, qreal newValue) return; } } else if ((oldValue >= center && newValue <= min() && max() != oldValue) - || (oldValue < center && newValue < max())) { + || (oldValue < center && newValue < max())) { newRange = range * ((max() - newValue) / (max() - oldValue)); if (newRange > 0) { m_axis->setRange(max() - newRange, max()); @@ -181,6 +181,36 @@ void ChartAxisElement::labelEdited(qreal oldValue, qreal newValue) label->reloadBeforeEditContent(); } +void ChartAxisElement::dateTimeLabelEdited(const QDateTime &oldTime, const QDateTime &newTime) +{ + qreal range = max() - min(); + qreal center = ((max() - min()) / 2.0) + min(); + qreal newRange = 0.0; + qint64 oldValue = oldTime.toMSecsSinceEpoch(); + qint64 newValue = newTime.toMSecsSinceEpoch(); + if ((oldValue >= center && newValue >= min()) + || (oldValue < center && newValue >= max() && oldValue != min())) { + newRange = range * ((newValue - min()) / (oldValue - min())); + if (newRange > 0) { + m_axis->setRange( + QDateTime::fromMSecsSinceEpoch(min()), + QDateTime::fromMSecsSinceEpoch(min() + newRange)); + return; + } + } else if ((oldValue >= center && newValue <= min() && max() != oldValue) + || (oldValue < center && newValue < max())) { + newRange = range * ((max() - newValue) / (max() - oldValue)); + if (newRange > 0) { + m_axis->setRange(max() - newRange, max()); + m_axis->setRange( + QDateTime::fromMSecsSinceEpoch(max() - newRange), + QDateTime::fromMSecsSinceEpoch(max())); + return; + } + } + static_cast<DateTimeAxisLabel *>(this->sender())->reloadBeforeEditContent(); +} + void ChartAxisElement::handleLabelsVisibleChanged(bool visible) { QGraphicsLayoutItem::updateGeometry(); @@ -534,11 +564,22 @@ bool ChartAxisElement::labelsEditable() const void ChartAxisElement::setLabelsEditable(bool labelsEditable) { - if (axis()->type() == QAbstractAxis::AxisTypeValue) { + if (axis()->type() == QAbstractAxis::AxisTypeValue + || axis()->type() == QAbstractAxis::AxisTypeDateTime) { labelGroup()->setHandlesChildEvents(!labelsEditable); const QList<QGraphicsItem *> childItems = labelGroup()->childItems(); - for (auto item : childItems) - static_cast<ValueAxisLabel *>(item)->setEditable(labelsEditable); + for (auto item : childItems) { + switch (axis()->type()) { + case QtCharts::QAbstractAxis::AxisTypeValue: + static_cast<ValueAxisLabel *>(item)->setEditable(labelsEditable); + break; + case QtCharts::QAbstractAxis::AxisTypeDateTime: + static_cast<DateTimeAxisLabel *>(item)->setEditable(labelsEditable); + break; + default: + break; + } + } m_labelsEditable = labelsEditable; } } |