diff options
author | Miklós Márton <martonmiklosqdev@gmail.com> | 2018-09-13 22:43:26 +0200 |
---|---|---|
committer | Miklós Márton <martonmiklosqdev@gmail.com> | 2018-10-30 10:11:05 +0000 |
commit | 796419e32863c1d322d5f9af1fedd032bdd738d7 (patch) | |
tree | 9a7638f1a107a9b55de0f2ad086e218de7dd12a3 /src/charts/axis/chartaxiselement.cpp | |
parent | 7f1e35a01880346e6e1a735f3e474ca4e9e257f5 (diff) |
Add label editing feature to the QValueAxis
Add a new property (labelsEditable) to the QAbstractAxis. 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 value. After
finishing the edit the axis range will adjusted according to the
entered value. At the moment only the QValueAxis is supported,
but I am planning to add support for QLogValueAxis, and QDateTimeAxis
after the basic implementation details got reviewed.
Change-Id: Id87ebfbfb37ed00f09cf7b651395cf3caead1dbb
Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io>
Diffstat (limited to 'src/charts/axis/chartaxiselement.cpp')
-rw-r--r-- | src/charts/axis/chartaxiselement.cpp | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/charts/axis/chartaxiselement.cpp b/src/charts/axis/chartaxiselement.cpp index fc2b3e27..bf33d330 100644 --- a/src/charts/axis/chartaxiselement.cpp +++ b/src/charts/axis/chartaxiselement.cpp @@ -157,6 +157,30 @@ void ChartAxisElement::handleLabelsPositionChanged() presenter()->layout()->invalidate(); } +void ChartAxisElement::labelEdited(qreal oldValue, qreal newValue) +{ + qreal range = max() - min(); + qreal center = ((max() - min()) / 2) + min(); + qreal newRange = 0.0; + auto label = static_cast<ValueAxisLabel *>(this->sender()); + if ((oldValue >= center && newValue >= min()) + || (oldValue < center && newValue >= max() && oldValue != min())) { + newRange = range * ((newValue - min()) / (oldValue - min())); + if (newRange > 0) { + m_axis->setRange(min(), 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()); + return; + } + } + label->reloadBeforeEditContent(); +} + void ChartAxisElement::handleLabelsVisibleChanged(bool visible) { QGraphicsLayoutItem::updateGeometry(); @@ -502,6 +526,23 @@ QStringList ChartAxisElement::createDateTimeLabels(qreal min, qreal max,int tick return labels; } + +bool ChartAxisElement::labelsEditable() const +{ + return m_labelsEditable; +} + +void ChartAxisElement::setLabelsEditable(bool labelsEditable) +{ + if (axis()->type() == QAbstractAxis::AxisTypeValue) { + labelGroup()->setHandlesChildEvents(!labelsEditable); + const QList<QGraphicsItem *> childItems = labelGroup()->childItems(); + for (auto item : childItems) + static_cast<ValueAxisLabel *>(item)->setEditable(labelsEditable); + m_labelsEditable = labelsEditable; + } +} + void ChartAxisElement::axisSelected() { emit clicked(); |