1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include <private/editableaxislabel_p.h>
5
6#include <QtGui/qtextcursor.h>
7#include <QtGui/qtextdocument.h>
8
9QT_BEGIN_NAMESPACE
10
11EditableAxisLabel::EditableAxisLabel(QGraphicsItem *parent) :
12 QGraphicsTextItem(parent)
13{
14
15}
16
17void EditableAxisLabel::focusInEvent(QFocusEvent *event)
18{
19 m_htmlBeforeEdit = toHtml();
20 setTextWidth(-1);
21 setInitialEditValue();
22 m_editing = true;
23 QGraphicsTextItem::focusInEvent(event);
24}
25
26void EditableAxisLabel::focusOutEvent(QFocusEvent *event)
27{
28 // perform the modifications before calling finishEditing
29 // because finishEditing emits signals that can trigger
30 // range change which might invalidate the current label
31 QGraphicsTextItem::focusOutEvent(event);
32 setTextInteractionFlags(Qt::NoTextInteraction);
33 m_editing = false;
34
35 finishEditing();
36}
37
38bool EditableAxisLabel::sceneEvent(QEvent *event)
39{
40 if (m_editable && event->type() == QEvent::GraphicsSceneMouseDoubleClick) {
41 setTextInteractionFlags(Qt::TextEditorInteraction);
42
43 bool ret = QGraphicsTextItem::sceneEvent(event);
44 // QGraphicsTextItem::sceneevent needs to be processed before
45 // the focus and text selection
46 setFocus(Qt::MouseFocusReason);
47 QTextCursor cursor = textCursor();
48 cursor.select(selection: QTextCursor::Document);
49 setTextCursor(cursor);
50 return ret;
51 }
52 return QGraphicsTextItem::sceneEvent(event);
53}
54
55void EditableAxisLabel::setEditable(bool editable)
56{
57 m_editable = editable;
58}
59
60void EditableAxisLabel::reloadBeforeEditContent()
61{
62 resetBeforeEditValue();
63 setHtml(m_htmlBeforeEdit);
64}
65
66QRectF EditableAxisLabel::boundingRect() const
67{
68 QRectF ret = QGraphicsTextItem::boundingRect();
69
70 // add 2px margin to allow the cursor to
71 // show up properly when editing
72 if (m_editing)
73 ret.setWidth(ret.width() + 2);
74 return ret;
75}
76
77bool EditableAxisLabel::isEditEndingKeyPress(QKeyEvent *event)
78{
79 if (event->text().size() >= 1) {
80 // finish editing with enter or ESC
81 if (event->key() == Qt::Key_Enter ||
82 event->key() == Qt::Key_Return) {
83 clearFocus();
84 return true;
85 } else if (event->key() == Qt::Key_Escape) {
86 document()->setHtml(m_htmlBeforeEdit);
87 clearFocus();
88 return true;
89 }
90 }
91 return false;
92}
93
94QT_END_NAMESPACE
95
96#include "moc_editableaxislabel_p.cpp"
97

source code of qtcharts/src/charts/axis/editableaxislabel.cpp