1/****************************************************************************
2**
3** Copyright (C) 2018 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Charts module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 or (at your option) any later version
20** approved by the KDE Free Qt Foundation. The licenses are as published by
21** the Free Software Foundation and appearing in the file LICENSE.GPL3
22** included in the packaging of this file. Please review the following
23** information to ensure the GNU General Public License requirements will
24** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25**
26** $QT_END_LICENSE$
27**
28****************************************************************************/
29#include <private/valueaxislabel_p.h>
30
31#include <QtCore/qlocale.h>
32
33QT_CHARTS_BEGIN_NAMESPACE
34
35ValueAxisLabel::ValueAxisLabel(QGraphicsItem *parent) :
36 EditableAxisLabel(parent)
37{
38
39}
40
41void ValueAxisLabel::finishEditing()
42{
43 bool ok = false;
44 QLocale locale;
45 qreal oldValue = m_value;
46 qreal newValue = locale.toDouble(s: document()->toPlainText(), ok: &ok);
47 if (ok && newValue != m_value) {
48 m_value = newValue;
49 emit valueChanged(oldValue, newValue);
50 } else {
51 document()->setHtml(m_htmlBeforeEdit);
52 }
53}
54
55void ValueAxisLabel::resetBeforeEditValue()
56{
57 m_value = m_valueBeforeEdit;
58}
59
60qreal ValueAxisLabel::value() const
61{
62 return m_value;
63}
64
65void ValueAxisLabel::setValue(const qreal &value)
66{
67 setTextInteractionFlags(Qt::NoTextInteraction);
68 clearFocus();
69 m_value = value;
70}
71
72void ValueAxisLabel::setInitialEditValue()
73{
74 m_valueBeforeEdit = m_value;
75 setHtml(QString::number(m_value));
76}
77
78void ValueAxisLabel::keyPressEvent(QKeyEvent *event)
79{
80 if (isEditEndingKeyPress(event)) {
81 // prevent further event processing with a return
82 // because the focusOutEvent could have triggered
83 // a range change which might have invalidated the current label
84 return;
85 }
86
87 if (event->text().length() >= 1) {
88 QLocale locale;
89 if (!event->text().at(i: 0).isDigit()
90 && event->text().at(i: 0) != locale.decimalPoint()
91 && event->text().at(i: 0) != locale.negativeSign()
92 && event->text().at(i: 0) != locale.exponential()
93 && event->key() != Qt::Key_Backspace
94 && event->key() != Qt::Key_Delete) {
95 event->ignore();
96 return;
97 }
98 }
99 QGraphicsTextItem::keyPressEvent(event);
100}
101
102QT_CHARTS_END_NAMESPACE
103
104#include "moc_valueaxislabel_p.cpp"
105

source code of qtcharts/src/charts/axis/valueaxis/valueaxislabel.cpp