summaryrefslogtreecommitdiffstats
path: root/src/charts/axis/chartaxiselement.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/charts/axis/chartaxiselement.cpp')
-rw-r--r--src/charts/axis/chartaxiselement.cpp82
1 files changed, 82 insertions, 0 deletions
diff --git a/src/charts/axis/chartaxiselement.cpp b/src/charts/axis/chartaxiselement.cpp
index fc2b3e27..62da84f4 100644
--- a/src/charts/axis/chartaxiselement.cpp
+++ b/src/charts/axis/chartaxiselement.cpp
@@ -157,6 +157,60 @@ void ChartAxisElement::handleLabelsPositionChanged()
presenter()->layout()->invalidate();
}
+void ChartAxisElement::valueLabelEdited(qreal oldValue, qreal newValue)
+{
+ qreal range = max() - min();
+ qreal center = ((max() - min()) / 2.0) + 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::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();
@@ -502,6 +556,34 @@ 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
+ || axis()->type() == QAbstractAxis::AxisTypeDateTime) {
+ labelGroup()->setHandlesChildEvents(!labelsEditable);
+ const QList<QGraphicsItem *> childItems = labelGroup()->childItems();
+ 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;
+ }
+}
+
void ChartAxisElement::axisSelected()
{
emit clicked();