| 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 "datetimeaxislabel_p.h" |
| 30 | |
| 31 | #include <QtCore/qdatetime.h> |
| 32 | |
| 33 | QT_CHARTS_BEGIN_NAMESPACE |
| 34 | |
| 35 | DateTimeAxisLabel::DateTimeAxisLabel(QGraphicsItem *parent) : |
| 36 | EditableAxisLabel(parent) |
| 37 | { |
| 38 | |
| 39 | } |
| 40 | |
| 41 | void DateTimeAxisLabel::finishEditing() |
| 42 | { |
| 43 | QDateTime oldDateTime = m_dateTime; |
| 44 | QDateTime newDateTime = QDateTime::fromString(s: document()->toPlainText(), format: m_format); |
| 45 | if (newDateTime.isValid() && newDateTime != m_dateTime) { |
| 46 | m_dateTime = newDateTime; |
| 47 | emit dateTimeChanged(oldDateTime, newDateTime); |
| 48 | } else { |
| 49 | document()->setHtml(m_htmlBeforeEdit); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | QDateTime DateTimeAxisLabel::value() const |
| 54 | { |
| 55 | return m_dateTime; |
| 56 | } |
| 57 | |
| 58 | void DateTimeAxisLabel::setValue(const QDateTime &value) |
| 59 | { |
| 60 | setTextInteractionFlags(Qt::NoTextInteraction); |
| 61 | clearFocus(); |
| 62 | m_dateTime = value; |
| 63 | } |
| 64 | |
| 65 | void DateTimeAxisLabel::resetBeforeEditValue() |
| 66 | { |
| 67 | m_dateTime = m_dateTimeBeforeEdit; |
| 68 | } |
| 69 | |
| 70 | void DateTimeAxisLabel::setFormat(const QString &format) |
| 71 | { |
| 72 | m_format = format; |
| 73 | // Labels should be edited as a single line regardless to their |
| 74 | // format because enter triggers applying the current text. |
| 75 | m_format.replace(before: QChar::fromLatin1(c: '\n'), after: QChar::fromLatin1(c: ' ')); |
| 76 | } |
| 77 | |
| 78 | void DateTimeAxisLabel::setInitialEditValue() |
| 79 | { |
| 80 | m_dateTimeBeforeEdit = m_dateTime; |
| 81 | setHtml(m_dateTime.toString(format: m_format)); |
| 82 | } |
| 83 | |
| 84 | void DateTimeAxisLabel::keyPressEvent(QKeyEvent *event) |
| 85 | { |
| 86 | if (isEditEndingKeyPress(event)) { |
| 87 | // prevent further event processing with a return |
| 88 | // because the focusOutEvent could have triggered |
| 89 | // a range change which might have invalidated the current label |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | QGraphicsTextItem::keyPressEvent(event); |
| 94 | } |
| 95 | |
| 96 | QT_CHARTS_END_NAMESPACE |
| 97 | |
| 98 | #include "moc_datetimeaxislabel_p.cpp" |
| 99 |
