summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiklós Márton <martonmiklosqdev@gmail.com>2018-10-28 18:04:41 +0100
committerMiikka Heikkinen <miikka.heikkinen@qt.io>2018-11-06 09:42:16 +0000
commit10890cdc849c8537b1951ff54eff801aa6ac502f (patch)
tree6af6c5e26833a2c9d29f327d75761dbec4e5be8d
parent796419e32863c1d322d5f9af1fedd032bdd738d7 (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>
-rw-r--r--src/charts/axis/axis.pri8
-rw-r--r--src/charts/axis/cartesianchartaxis.cpp50
-rw-r--r--src/charts/axis/cartesianchartaxis_p.h3
-rw-r--r--src/charts/axis/chartaxiselement.cpp55
-rw-r--r--src/charts/axis/chartaxiselement_p.h5
-rw-r--r--src/charts/axis/datetimeaxis/chartdatetimeaxisx.cpp1
-rw-r--r--src/charts/axis/datetimeaxis/chartdatetimeaxisy.cpp1
-rw-r--r--src/charts/axis/datetimeaxis/datetimeaxislabel.cpp98
-rw-r--r--src/charts/axis/datetimeaxis/datetimeaxislabel_p.h75
-rw-r--r--src/charts/axis/datetimeaxis/qdatetimeaxis.cpp4
-rw-r--r--src/charts/axis/editableaxislabel.cpp122
-rw-r--r--src/charts/axis/editableaxislabel_p.h78
-rw-r--r--src/charts/axis/qabstractaxis.cpp2
-rw-r--r--src/charts/axis/valueaxis/valueaxislabel.cpp94
-rw-r--r--src/charts/axis/valueaxis/valueaxislabel_p.h23
15 files changed, 506 insertions, 113 deletions
diff --git a/src/charts/axis/axis.pri b/src/charts/axis/axis.pri
index 4909e77c..c5efddc2 100644
--- a/src/charts/axis/axis.pri
+++ b/src/charts/axis/axis.pri
@@ -24,7 +24,8 @@ SOURCES += \
$$PWD/categoryaxis/qcategoryaxis.cpp \
$$PWD/logvalueaxis/chartlogvalueaxisx.cpp \
$$PWD/logvalueaxis/chartlogvalueaxisy.cpp \
- $$PWD/logvalueaxis/qlogvalueaxis.cpp
+ $$PWD/logvalueaxis/qlogvalueaxis.cpp \
+ $$PWD/editableaxislabel.cpp
PRIVATE_HEADERS += \
$$PWD/chartaxiselement_p.h \
@@ -45,7 +46,8 @@ PRIVATE_HEADERS += \
$$PWD/categoryaxis/qcategoryaxis_p.h \
$$PWD/logvalueaxis/chartlogvalueaxisx_p.h \
$$PWD/logvalueaxis/chartlogvalueaxisy_p.h \
- $$PWD/logvalueaxis/qlogvalueaxis_p.h
+ $$PWD/logvalueaxis/qlogvalueaxis_p.h \
+ $$PWD/editableaxislabel_p.h
PUBLIC_HEADERS += \
$$PWD/qabstractaxis.h \
@@ -85,6 +87,7 @@ SOURCES += \
$$PWD/datetimeaxis/chartdatetimeaxisx.cpp \
$$PWD/datetimeaxis/chartdatetimeaxisy.cpp \
$$PWD/datetimeaxis/qdatetimeaxis.cpp \
+ $$PWD/datetimeaxis/datetimeaxislabel.cpp \
$$PWD/datetimeaxis/polarchartdatetimeaxisangular.cpp \
$$PWD/datetimeaxis/polarchartdatetimeaxisradial.cpp
@@ -92,6 +95,7 @@ PRIVATE_HEADERS += \
$$PWD/datetimeaxis/chartdatetimeaxisx_p.h \
$$PWD/datetimeaxis/chartdatetimeaxisy_p.h \
$$PWD/datetimeaxis/qdatetimeaxis_p.h \
+ $$PWD/datetimeaxis/datetimeaxislabel_p.h \
$$PWD/datetimeaxis/polarchartdatetimeaxisangular_p.h \
$$PWD/datetimeaxis/polarchartdatetimeaxisradial_p.h
diff --git a/src/charts/axis/cartesianchartaxis.cpp b/src/charts/axis/cartesianchartaxis.cpp
index 34e618c2..935a9601 100644
--- a/src/charts/axis/cartesianchartaxis.cpp
+++ b/src/charts/axis/cartesianchartaxis.cpp
@@ -81,17 +81,25 @@ void CartesianChartAxis::createItems(int count)
for (int i = 0; i < count; ++i) {
QGraphicsLineItem *arrow = new QGraphicsLineItem(this);
QGraphicsLineItem *grid = new QGraphicsLineItem(this);
- QGraphicsTextItem *label = axis()->type() == QAbstractAxis::AxisTypeValue
- ? new ValueAxisLabel(this)
- : new QGraphicsTextItem(this);
- if (axis()->type() == QAbstractAxis::AxisTypeValue) {
- ValueAxisLabel *valueLabel = static_cast<ValueAxisLabel *>(label);
- connect(valueLabel, &ValueAxisLabel::valueChanged,
- this, &ChartAxisElement::labelEdited);
- // only QValueAxis labels editable for now
+ QGraphicsTextItem *label;
+ if (axis()->type() == QtCharts::QAbstractAxis::AxisTypeValue) {
+ label = new ValueAxisLabel(this);
+ connect(static_cast<ValueAxisLabel *>(label), &ValueAxisLabel::valueChanged,
+ this, &ChartAxisElement::valueLabelEdited);
if (labelsEditable())
- valueLabel->setEditable(true);
+ static_cast<ValueAxisLabel *>(label)->setEditable(true);
+ } else if (axis()->type() == QtCharts::QAbstractAxis::AxisTypeDateTime) {
+ DateTimeAxisLabel *dateTimeLabel = new DateTimeAxisLabel(this);
+ label = dateTimeLabel;
+ connect(dateTimeLabel, &DateTimeAxisLabel::dateTimeChanged,
+ this, &ChartAxisElement::dateTimeLabelEdited);
+ if (labelsEditable())
+ dateTimeLabel->setEditable(true);
+ dateTimeLabel->setFormat(static_cast<QDateTimeAxis*>(axis())->format());
+ } else {
+ label = new QGraphicsTextItem(this);
}
+
label->document()->setDocumentMargin(ChartPresenter::textMargin());
arrow->setPen(axis()->linePen());
grid->setPen(axis()->gridLinePen());
@@ -281,6 +289,18 @@ QSizeF CartesianChartAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint
return QSizeF();
}
+void CartesianChartAxis::setDateTimeLabelsFormat(const QString &format)
+{
+ if (max() <= min()
+ || layout().size() < 1
+ || axis()->type() != QAbstractAxis::AxisTypeDateTime) {
+ return;
+ }
+
+ for (int i = 0; i < layout().size(); i++)
+ static_cast<DateTimeAxisLabel *>(labelItems().at(i))->setFormat(format);
+}
+
void CartesianChartAxis::handleArrowPenChanged(const QPen &pen)
{
foreach (QGraphicsItem *item, arrowItems())
@@ -366,6 +386,18 @@ void CartesianChartAxis::updateLabelsValues(QValueAxis *axis)
}
}
+void CartesianChartAxis::updateLabelsDateTimes()
+{
+ if (max() <= min() || layout().size() < 1)
+ return;
+
+ for (int i = 0; i < layout().size(); i++) {
+ qreal value = min() + (i * (max() - min()) / (layout().size() - 1));
+ static_cast<DateTimeAxisLabel *>(labelItems().at(i))->setValue(
+ QDateTime::fromMSecsSinceEpoch(value));
+ }
+}
+
QT_CHARTS_END_NAMESPACE
#include "moc_cartesianchartaxis_p.cpp"
diff --git a/src/charts/axis/cartesianchartaxis_p.h b/src/charts/axis/cartesianchartaxis_p.h
index dc274522..b619ed8b 100644
--- a/src/charts/axis/cartesianchartaxis_p.h
+++ b/src/charts/axis/cartesianchartaxis_p.h
@@ -62,6 +62,8 @@ public:
virtual QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const;
+ void setDateTimeLabelsFormat(const QString &format);
+
protected:
void setGeometry(const QRectF &size) { Q_UNUSED(size);}
virtual void updateGeometry() = 0;
@@ -79,6 +81,7 @@ public Q_SLOTS:
protected:
void updateLabelsValues(QValueAxis *axis);
+ void updateLabelsDateTimes();
private:
void createItems(int count);
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;
}
}
diff --git a/src/charts/axis/chartaxiselement_p.h b/src/charts/axis/chartaxiselement_p.h
index 4bc1475e..d421dc8b 100644
--- a/src/charts/axis/chartaxiselement_p.h
+++ b/src/charts/axis/chartaxiselement_p.h
@@ -43,9 +43,11 @@
#include <QtCharts/private/qchartglobal_p.h>
#include <private/chartelement_p.h>
#include <private/axisanimation_p.h>
+#include <private/datetimeaxislabel_p.h>
#include <private/valueaxislabel_p.h>
#include <QtWidgets/QGraphicsItem>
#include <QtWidgets/QGraphicsLayoutItem>
+#include <QtCharts/qdatetimeaxis.h>
#include <QtCharts/QValueAxis>
#include <QtGui/QFont>
@@ -159,7 +161,8 @@ public Q_SLOTS:
void handleMinorArrowVisibleChanged(bool visible);
void handleMinorGridVisibleChanged(bool visible);
void handleLabelsPositionChanged();
- void labelEdited(qreal oldValue, qreal newValue);
+ void valueLabelEdited(qreal oldValue, qreal newValue);
+ void dateTimeLabelEdited(const QDateTime &oldTime, const QDateTime &newTime);
Q_SIGNALS:
void clicked();
diff --git a/src/charts/axis/datetimeaxis/chartdatetimeaxisx.cpp b/src/charts/axis/datetimeaxis/chartdatetimeaxisx.cpp
index 111b3526..7b304b6c 100644
--- a/src/charts/axis/datetimeaxis/chartdatetimeaxisx.cpp
+++ b/src/charts/axis/datetimeaxis/chartdatetimeaxisx.cpp
@@ -71,6 +71,7 @@ void ChartDateTimeAxisX::updateGeometry()
return;
setLabels(createDateTimeLabels(min(), max(), layout.size(), m_axis->format()));
HorizontalAxis::updateGeometry();
+ updateLabelsDateTimes();
}
void ChartDateTimeAxisX::handleTickCountChanged(int tick)
diff --git a/src/charts/axis/datetimeaxis/chartdatetimeaxisy.cpp b/src/charts/axis/datetimeaxis/chartdatetimeaxisy.cpp
index 489ba3cd..9f3a9eb3 100644
--- a/src/charts/axis/datetimeaxis/chartdatetimeaxisy.cpp
+++ b/src/charts/axis/datetimeaxis/chartdatetimeaxisy.cpp
@@ -72,6 +72,7 @@ void ChartDateTimeAxisY::updateGeometry()
return;
setLabels(createDateTimeLabels(min(), max(), layout.size(), m_axis->format()));
VerticalAxis::updateGeometry();
+ updateLabelsDateTimes();
}
void ChartDateTimeAxisY::handleTickCountChanged(int tick)
diff --git a/src/charts/axis/datetimeaxis/datetimeaxislabel.cpp b/src/charts/axis/datetimeaxis/datetimeaxislabel.cpp
new file mode 100644
index 00000000..90646ae5
--- /dev/null
+++ b/src/charts/axis/datetimeaxis/datetimeaxislabel.cpp
@@ -0,0 +1,98 @@
+/****************************************************************************
+**
+** Copyright (C) 2018 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the Qt Charts module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:GPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 or (at your option) any later version
+** approved by the KDE Free Qt Foundation. The licenses are as published by
+** the Free Software Foundation and appearing in the file LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+#include "datetimeaxislabel_p.h"
+
+#include <QtCore/qdatetime.h>
+
+QT_CHARTS_BEGIN_NAMESPACE
+
+DateTimeAxisLabel::DateTimeAxisLabel(QGraphicsItem *parent) :
+ EditableAxisLabel(parent)
+{
+
+}
+
+void DateTimeAxisLabel::finishEditing()
+{
+ QDateTime oldDateTime = m_dateTime;
+ QDateTime newDateTime = QDateTime::fromString(document()->toPlainText(), m_format);
+ if (newDateTime.isValid() && newDateTime != m_dateTime) {
+ m_dateTime = newDateTime;
+ emit dateTimeChanged(oldDateTime, newDateTime);
+ } else {
+ document()->setHtml(m_htmlBeforeEdit);
+ }
+}
+
+QDateTime DateTimeAxisLabel::value() const
+{
+ return m_dateTime;
+}
+
+void DateTimeAxisLabel::setValue(const QDateTime &value)
+{
+ setTextInteractionFlags(Qt::NoTextInteraction);
+ clearFocus();
+ m_dateTime = value;
+}
+
+void DateTimeAxisLabel::resetBeforeEditValue()
+{
+ m_dateTime = m_dateTimeBeforeEdit;
+}
+
+void DateTimeAxisLabel::setFormat(const QString &format)
+{
+ m_format = format;
+ // Labels should be edited as a single line regardless to their
+ // format because enter triggers applying the current text.
+ m_format.replace(QChar::fromLatin1('\n'), QChar::fromLatin1(' '));
+}
+
+void DateTimeAxisLabel::setInitialEditValue()
+{
+ m_dateTimeBeforeEdit = m_dateTime;
+ setHtml(m_dateTime.toString(m_format));
+}
+
+void DateTimeAxisLabel::keyPressEvent(QKeyEvent *event)
+{
+ if (isEditEndingKeyPress(event)) {
+ // prevent further event processing with a return
+ // because the focusOutEvent could have triggered
+ // a range change which might have invalidated the current label
+ return;
+ }
+
+ QGraphicsTextItem::keyPressEvent(event);
+}
+
+QT_CHARTS_END_NAMESPACE
+
+#include "moc_datetimeaxislabel_p.cpp"
diff --git a/src/charts/axis/datetimeaxis/datetimeaxislabel_p.h b/src/charts/axis/datetimeaxis/datetimeaxislabel_p.h
new file mode 100644
index 00000000..c06c6240
--- /dev/null
+++ b/src/charts/axis/datetimeaxis/datetimeaxislabel_p.h
@@ -0,0 +1,75 @@
+/****************************************************************************
+**
+** Copyright (C) 2018 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the Qt Charts module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:GPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 or (at your option) any later version
+** approved by the KDE Free Qt Foundation. The licenses are as published by
+** the Free Software Foundation and appearing in the file LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt Chart API. It exists purely as an
+// implementation detail. This header file may change from version to
+// version without notice, or even be removed.
+//
+// We mean it.
+
+#ifndef DATETIMEAXISLABEL_H
+#define DATETIMEAXISLABEL_H
+
+#include <private/editableaxislabel_p.h>
+
+#include <QtCore/qdatetime.h>
+
+QT_CHARTS_BEGIN_NAMESPACE
+
+class QT_CHARTS_PRIVATE_EXPORT DateTimeAxisLabel : public EditableAxisLabel
+{
+ Q_OBJECT
+public:
+ DateTimeAxisLabel(QGraphicsItem *parent = nullptr);
+
+ void keyPressEvent(QKeyEvent *event);
+
+ QDateTime value() const;
+ void setValue(const QDateTime &value);
+ void setFormat(const QString &format);
+
+private:
+ QDateTime m_dateTime;
+ QDateTime m_dateTimeBeforeEdit;
+ QString m_format;
+
+ void setInitialEditValue() override;
+ void finishEditing() override;
+ void resetBeforeEditValue() override;
+
+Q_SIGNALS:
+ void dateTimeChanged(const QDateTime &oldDateTime, const QDateTime &newDateTime);
+};
+
+QT_CHARTS_END_NAMESPACE
+
+#endif // VALUEAXISLABEL_H
diff --git a/src/charts/axis/datetimeaxis/qdatetimeaxis.cpp b/src/charts/axis/datetimeaxis/qdatetimeaxis.cpp
index a7ccd37b..dd32c299 100644
--- a/src/charts/axis/datetimeaxis/qdatetimeaxis.cpp
+++ b/src/charts/axis/datetimeaxis/qdatetimeaxis.cpp
@@ -259,6 +259,8 @@ void QDateTimeAxis::setFormat(QString format)
Q_D(QDateTimeAxis);
if (d->m_format != format) {
d->m_format = format;
+ if (d->axisItem())
+ static_cast<CartesianChartAxis*>(d->axisItem())->setDateTimeLabelsFormat(format);
emit formatChanged(format);
}
}
@@ -339,7 +341,6 @@ void QDateTimeAxisPrivate::setRange(qreal min,qreal max)
}
}
-
void QDateTimeAxisPrivate::setMin(const QVariant &min)
{
Q_Q(QDateTimeAxis);
@@ -371,6 +372,7 @@ void QDateTimeAxisPrivate::initializeGraphics(QGraphicsItem* parent)
axis = new ChartDateTimeAxisY(q,parent);
if (orientation() == Qt::Horizontal)
axis = new ChartDateTimeAxisX(q,parent);
+ axis->setLabelsEditable(q->labelsEditable());
}
if (m_chart->chartType() == QChart::ChartTypePolar) {
diff --git a/src/charts/axis/editableaxislabel.cpp b/src/charts/axis/editableaxislabel.cpp
new file mode 100644
index 00000000..aa4b0554
--- /dev/null
+++ b/src/charts/axis/editableaxislabel.cpp
@@ -0,0 +1,122 @@
+/****************************************************************************
+**
+** Copyright (C) 2018 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the Qt Charts module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:GPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 or (at your option) any later version
+** approved by the KDE Free Qt Foundation. The licenses are as published by
+** the Free Software Foundation and appearing in the file LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <private/editableaxislabel_p.h>
+
+#include <QtGui/qtextcursor.h>
+#include <QtGui/qtextdocument.h>
+
+QT_CHARTS_BEGIN_NAMESPACE
+
+EditableAxisLabel::EditableAxisLabel(QGraphicsItem *parent) :
+ QGraphicsTextItem(parent)
+{
+
+}
+
+void EditableAxisLabel::focusInEvent(QFocusEvent *event)
+{
+ m_htmlBeforeEdit = toHtml();
+ setTextWidth(-1);
+ setInitialEditValue();
+ m_editing = true;
+ QGraphicsTextItem::focusInEvent(event);
+}
+
+void EditableAxisLabel::focusOutEvent(QFocusEvent *event)
+{
+ // perform the modifications before calling finishEditing
+ // because finishEditing emits signals that can trigger
+ // range change which might invalidate the current label
+ QGraphicsTextItem::focusOutEvent(event);
+ setTextInteractionFlags(Qt::NoTextInteraction);
+ m_editing = false;
+
+ finishEditing();
+}
+
+bool EditableAxisLabel::sceneEvent(QEvent *event)
+{
+ if (m_editable && event->type() == QEvent::GraphicsSceneMouseDoubleClick) {
+ setTextInteractionFlags(Qt::TextEditorInteraction);
+
+ bool ret = QGraphicsTextItem::sceneEvent(event);
+ // QGraphicsTextItem::sceneevent needs to be processed before
+ // the focus and text selection
+ setFocus(Qt::MouseFocusReason);
+ QTextCursor cursor = textCursor();
+ cursor.select(QTextCursor::Document);
+ setTextCursor(cursor);
+ return ret;
+ }
+ return QGraphicsTextItem::sceneEvent(event);
+}
+
+void EditableAxisLabel::setEditable(bool editable)
+{
+ m_editable = editable;
+}
+
+void EditableAxisLabel::reloadBeforeEditContent()
+{
+ resetBeforeEditValue();
+ setHtml(m_htmlBeforeEdit);
+}
+
+QRectF EditableAxisLabel::boundingRect() const
+{
+ QRectF ret = QGraphicsTextItem::boundingRect();
+
+ // add 2px margin to allow the cursor to
+ // show up properly when editing
+ if (m_editing)
+ ret.setWidth(ret.width() + 2);
+ return ret;
+}
+
+bool EditableAxisLabel::isEditEndingKeyPress(QKeyEvent *event)
+{
+ if (event->text().length() >= 1) {
+ // finish editing with enter or ESC
+ if (event->key() == Qt::Key_Enter ||
+ event->key() == Qt::Key_Return) {
+ clearFocus();
+ return true;
+ } else if (event->key() == Qt::Key_Escape) {
+ document()->setHtml(m_htmlBeforeEdit);
+ clearFocus();
+ return true;
+ }
+ }
+ return false;
+}
+
+QT_CHARTS_END_NAMESPACE
+
+#include "moc_editableaxislabel_p.cpp"
diff --git a/src/charts/axis/editableaxislabel_p.h b/src/charts/axis/editableaxislabel_p.h
new file mode 100644
index 00000000..55c94e08
--- /dev/null
+++ b/src/charts/axis/editableaxislabel_p.h
@@ -0,0 +1,78 @@
+/****************************************************************************
+**
+** Copyright (C) 2018 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the Qt Charts module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:GPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 or (at your option) any later version
+** approved by the KDE Free Qt Foundation. The licenses are as published by
+** the Free Software Foundation and appearing in the file LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt Chart API. It exists purely as an
+// implementation detail. This header file may change from version to
+// version without notice, or even be removed.
+//
+// We mean it.
+
+#ifndef EDITABLEAXISLABEL_P_H
+#define EDITABLEAXISLABEL_P_H
+
+#include <QtCharts/private/qchartglobal_p.h>
+
+#include <QtWidgets/qgraphicsitem.h>
+#include <QtGui/qevent.h>
+#include <QtGui/qtextdocument.h>
+
+QT_CHARTS_BEGIN_NAMESPACE
+
+class QT_CHARTS_PRIVATE_EXPORT EditableAxisLabel : public QGraphicsTextItem
+{
+ Q_OBJECT
+public:
+ EditableAxisLabel(QGraphicsItem *parent = nullptr);
+
+ void focusInEvent(QFocusEvent *event);
+ void focusOutEvent(QFocusEvent *event);
+ bool sceneEvent(QEvent *event);
+ void setEditable(bool editable);
+ void reloadBeforeEditContent();
+
+ QRectF boundingRect() const;
+
+protected:
+ QString m_htmlBeforeEdit;
+ bool m_editing = false;
+ bool m_editable = false;
+
+ virtual void setInitialEditValue() = 0;
+ virtual void finishEditing() = 0;
+ virtual void resetBeforeEditValue() = 0;
+
+ bool isEditEndingKeyPress(QKeyEvent *event);
+};
+
+QT_CHARTS_END_NAMESPACE
+
+#endif // EDITABLEAXISLABEL_P_H
diff --git a/src/charts/axis/qabstractaxis.cpp b/src/charts/axis/qabstractaxis.cpp
index ed246536..f9d211ca 100644
--- a/src/charts/axis/qabstractaxis.cpp
+++ b/src/charts/axis/qabstractaxis.cpp
@@ -107,7 +107,7 @@ QT_CHARTS_BEGIN_NAMESPACE
\brief This property holds whether the labels of the axis are editable or not.
When the labels set to editable the user will be able to change the range of the
axis conveniently by editing any of the labels. This feature is only implemented
- for the QValueAxis.
+ for the QValueAxis and the QDateTimeAxis.
By default, the value is \c false.
*/
diff --git a/src/charts/axis/valueaxis/valueaxislabel.cpp b/src/charts/axis/valueaxis/valueaxislabel.cpp
index cd81a857..ecd1cb46 100644
--- a/src/charts/axis/valueaxis/valueaxislabel.cpp
+++ b/src/charts/axis/valueaxis/valueaxislabel.cpp
@@ -26,40 +26,20 @@
** $QT_END_LICENSE$
**
****************************************************************************/
-#include "valueaxislabel_p.h"
+#include <private/valueaxislabel_p.h>
#include <QtCore/qlocale.h>
-#include <QtGui/qevent.h>
-#include <QtGui/qtextcursor.h>
-#include <QtGui/qtextdocument.h>
QT_CHARTS_BEGIN_NAMESPACE
ValueAxisLabel::ValueAxisLabel(QGraphicsItem *parent) :
- QGraphicsTextItem(parent)
+ EditableAxisLabel(parent)
{
}
-void ValueAxisLabel::focusInEvent(QFocusEvent *event)
+void ValueAxisLabel::finishEditing()
{
- m_htmlBeforeEdit = toHtml();
- setTextWidth(-1);
- m_valueBeforeEdit = m_value;
- setHtml(QString::number(m_value));
- m_editing = true;
- QGraphicsTextItem::focusInEvent(event);
-}
-
-void ValueAxisLabel::focusOutEvent(QFocusEvent *event)
-{
- // perform the modifications before emitting valueChanged
- // because slots attached to valueChanged can trigger
- // a range change which might invalidate the current label
- QGraphicsTextItem::focusOutEvent(event);
- setTextInteractionFlags(Qt::NoTextInteraction);
- m_editing = false;
-
bool ok = false;
QLocale locale;
qreal oldValue = m_value;
@@ -72,6 +52,11 @@ void ValueAxisLabel::focusOutEvent(QFocusEvent *event)
}
}
+void ValueAxisLabel::resetBeforeEditValue()
+{
+ m_value = m_valueBeforeEdit;
+}
+
qreal ValueAxisLabel::value() const
{
return m_value;
@@ -84,48 +69,22 @@ void ValueAxisLabel::setValue(const qreal &value)
m_value = value;
}
-void ValueAxisLabel::reloadBeforeEditContent()
-{
- m_value = m_valueBeforeEdit;
- setHtml(m_htmlBeforeEdit);
-}
-
-QRectF ValueAxisLabel::boundingRect() const
-{
- QRectF ret = QGraphicsTextItem::boundingRect();
-
- // add 2px margin to allow the cursor to
- // show up properly when editing
- if (m_editing)
- ret.setWidth(ret.width() + 2);
- return ret;
-}
-
-void ValueAxisLabel::setEditable(bool editable)
+void ValueAxisLabel::setInitialEditValue()
{
- m_editable = editable;
+ m_valueBeforeEdit = m_value;
+ setHtml(QString::number(m_value));
}
void ValueAxisLabel::keyPressEvent(QKeyEvent *event)
{
- if (event->text().length() >= 1) {
- // finish editing with enter
- if (event->key() == Qt::Key_Enter ||
- event->key() == Qt::Key_Return) {
- clearFocus();
- // prevent further event processing with a return
- // because the focusOutEvent could have triggered
- // a range change which might have invalidated the current label
- return;
- } else if (event->key() == Qt::Key_Escape) {
- document()->setHtml(m_htmlBeforeEdit);
- clearFocus();
- // prevent further event processing with a return
- // because the focusOutEvent could have triggered
- // a range change which might have invalidated the current label
- return;
- }
+ if (isEditEndingKeyPress(event)) {
+ // prevent further event processing with a return
+ // because the focusOutEvent could have triggered
+ // a range change which might have invalidated the current label
+ return;
+ }
+ if (event->text().length() >= 1) {
QLocale locale;
if (!event->text().at(0).isDigit()
&& event->text().at(0) != locale.decimalPoint()
@@ -140,23 +99,6 @@ void ValueAxisLabel::keyPressEvent(QKeyEvent *event)
QGraphicsTextItem::keyPressEvent(event);
}
-bool ValueAxisLabel::sceneEvent(QEvent *event)
-{
- if (m_editable && event->type() == QEvent::GraphicsSceneMouseDoubleClick) {
- setTextInteractionFlags(Qt::TextEditorInteraction);
-
- bool ret = QGraphicsTextItem::sceneEvent(event);
- // QGraphicsTextItem::sceneevent needs to be processed before
- // the focus and text selection
- setFocus(Qt::MouseFocusReason);
- QTextCursor cursor = textCursor();
- cursor.select(QTextCursor::Document);
- setTextCursor(cursor);
- return ret;
- }
- return QGraphicsTextItem::sceneEvent(event);
-}
-
QT_CHARTS_END_NAMESPACE
#include "moc_valueaxislabel_p.cpp"
diff --git a/src/charts/axis/valueaxis/valueaxislabel_p.h b/src/charts/axis/valueaxis/valueaxislabel_p.h
index 14f3eb48..4fe3c1aa 100644
--- a/src/charts/axis/valueaxis/valueaxislabel_p.h
+++ b/src/charts/axis/valueaxis/valueaxislabel_p.h
@@ -39,39 +39,30 @@
#ifndef VALUEAXISLABEL_H
#define VALUEAXISLABEL_H
-#include <QtWidgets/qgraphicsitem.h>
-#include <QtCharts/private/qchartglobal_p.h>
+#include <private/editableaxislabel_p.h>
QT_CHARTS_BEGIN_NAMESPACE
-class QT_CHARTS_PRIVATE_EXPORT ValueAxisLabel : public QGraphicsTextItem
+class QT_CHARTS_PRIVATE_EXPORT ValueAxisLabel : public EditableAxisLabel
{
Q_OBJECT
public:
ValueAxisLabel(QGraphicsItem *parent = nullptr);
- void focusInEvent(QFocusEvent *event);
- void focusOutEvent(QFocusEvent *event);
void keyPressEvent(QKeyEvent *event);
- bool sceneEvent(QEvent *event);
-
qreal value() const;
void setValue(const qreal &value);
- void reloadBeforeEditContent();
-
- QRectF boundingRect() const;
-
- void setEditable(bool editable);
private:
qreal m_value = 0.0;
qreal m_valueBeforeEdit = 0.0;
- QString m_htmlBeforeEdit;
- bool m_editing = false;
- bool m_editable = false;
-signals:
+ void setInitialEditValue() override;
+ void finishEditing() override;
+ void resetBeforeEditValue() override;
+
+Q_SIGNALS:
void valueChanged(qreal oldValue, qreal newValue);
};