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 | |
30 | #include <private/editableaxislabel_p.h> |
31 | |
32 | #include <QtGui/qtextcursor.h> |
33 | #include <QtGui/qtextdocument.h> |
34 | |
35 | QT_CHARTS_BEGIN_NAMESPACE |
36 | |
37 | EditableAxisLabel::EditableAxisLabel(QGraphicsItem *parent) : |
38 | QGraphicsTextItem(parent) |
39 | { |
40 | |
41 | } |
42 | |
43 | void EditableAxisLabel::focusInEvent(QFocusEvent *event) |
44 | { |
45 | m_htmlBeforeEdit = toHtml(); |
46 | setTextWidth(-1); |
47 | setInitialEditValue(); |
48 | m_editing = true; |
49 | QGraphicsTextItem::focusInEvent(event); |
50 | } |
51 | |
52 | void EditableAxisLabel::focusOutEvent(QFocusEvent *event) |
53 | { |
54 | // perform the modifications before calling finishEditing |
55 | // because finishEditing emits signals that can trigger |
56 | // range change which might invalidate the current label |
57 | QGraphicsTextItem::focusOutEvent(event); |
58 | setTextInteractionFlags(Qt::NoTextInteraction); |
59 | m_editing = false; |
60 | |
61 | finishEditing(); |
62 | } |
63 | |
64 | bool EditableAxisLabel::sceneEvent(QEvent *event) |
65 | { |
66 | if (m_editable && event->type() == QEvent::GraphicsSceneMouseDoubleClick) { |
67 | setTextInteractionFlags(Qt::TextEditorInteraction); |
68 | |
69 | bool ret = QGraphicsTextItem::sceneEvent(event); |
70 | // QGraphicsTextItem::sceneevent needs to be processed before |
71 | // the focus and text selection |
72 | setFocus(Qt::MouseFocusReason); |
73 | QTextCursor cursor = textCursor(); |
74 | cursor.select(selection: QTextCursor::Document); |
75 | setTextCursor(cursor); |
76 | return ret; |
77 | } |
78 | return QGraphicsTextItem::sceneEvent(event); |
79 | } |
80 | |
81 | void EditableAxisLabel::setEditable(bool editable) |
82 | { |
83 | m_editable = editable; |
84 | } |
85 | |
86 | void EditableAxisLabel::reloadBeforeEditContent() |
87 | { |
88 | resetBeforeEditValue(); |
89 | setHtml(m_htmlBeforeEdit); |
90 | } |
91 | |
92 | QRectF EditableAxisLabel::boundingRect() const |
93 | { |
94 | QRectF ret = QGraphicsTextItem::boundingRect(); |
95 | |
96 | // add 2px margin to allow the cursor to |
97 | // show up properly when editing |
98 | if (m_editing) |
99 | ret.setWidth(ret.width() + 2); |
100 | return ret; |
101 | } |
102 | |
103 | bool EditableAxisLabel::isEditEndingKeyPress(QKeyEvent *event) |
104 | { |
105 | if (event->text().length() >= 1) { |
106 | // finish editing with enter or ESC |
107 | if (event->key() == Qt::Key_Enter || |
108 | event->key() == Qt::Key_Return) { |
109 | clearFocus(); |
110 | return true; |
111 | } else if (event->key() == Qt::Key_Escape) { |
112 | document()->setHtml(m_htmlBeforeEdit); |
113 | clearFocus(); |
114 | return true; |
115 | } |
116 | } |
117 | return false; |
118 | } |
119 | |
120 | QT_CHARTS_END_NAMESPACE |
121 | |
122 | #include "moc_editableaxislabel_p.cpp" |
123 |