1 | // Copyright (C) 2018 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | #include <private/valueaxislabel_p.h> |
4 | |
5 | #include <QtCore/qlocale.h> |
6 | |
7 | QT_BEGIN_NAMESPACE |
8 | |
9 | ValueAxisLabel::ValueAxisLabel(QGraphicsItem *parent) : |
10 | EditableAxisLabel(parent) |
11 | { |
12 | |
13 | } |
14 | |
15 | void ValueAxisLabel::finishEditing() |
16 | { |
17 | bool ok = false; |
18 | QLocale locale; |
19 | qreal oldValue = m_value; |
20 | qreal newValue = locale.toDouble(s: document()->toPlainText(), ok: &ok); |
21 | if (ok && newValue != m_value) { |
22 | m_value = newValue; |
23 | emit valueChanged(oldValue, newValue); |
24 | } else { |
25 | document()->setHtml(m_htmlBeforeEdit); |
26 | } |
27 | } |
28 | |
29 | void ValueAxisLabel::resetBeforeEditValue() |
30 | { |
31 | m_value = m_valueBeforeEdit; |
32 | } |
33 | |
34 | qreal ValueAxisLabel::value() const |
35 | { |
36 | return m_value; |
37 | } |
38 | |
39 | void ValueAxisLabel::setValue(const qreal &value) |
40 | { |
41 | setTextInteractionFlags(Qt::NoTextInteraction); |
42 | clearFocus(); |
43 | m_value = value; |
44 | } |
45 | |
46 | void ValueAxisLabel::setInitialEditValue() |
47 | { |
48 | m_valueBeforeEdit = m_value; |
49 | setHtml(QString::number(m_value)); |
50 | } |
51 | |
52 | void ValueAxisLabel::keyPressEvent(QKeyEvent *event) |
53 | { |
54 | if (isEditEndingKeyPress(event)) { |
55 | // prevent further event processing with a return |
56 | // because the focusOutEvent could have triggered |
57 | // a range change which might have invalidated the current label |
58 | return; |
59 | } |
60 | |
61 | if (event->text().size() >= 1) { |
62 | QLocale locale; |
63 | if (!event->text().at(i: 0).isDigit() |
64 | && event->text().at(i: 0) != locale.decimalPoint() |
65 | && event->text().at(i: 0) != locale.negativeSign() |
66 | && event->text().at(i: 0) != locale.exponential() |
67 | && event->key() != Qt::Key_Backspace |
68 | && event->key() != Qt::Key_Delete) { |
69 | event->ignore(); |
70 | return; |
71 | } |
72 | } |
73 | QGraphicsTextItem::keyPressEvent(event); |
74 | } |
75 | |
76 | QT_END_NAMESPACE |
77 | |
78 | #include "moc_valueaxislabel_p.cpp" |
79 |