1 | // Copyright (C) 2018 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | #include "datetimeaxislabel_p.h" |
4 | |
5 | #include <QtCore/qdatetime.h> |
6 | |
7 | QT_BEGIN_NAMESPACE |
8 | |
9 | DateTimeAxisLabel::DateTimeAxisLabel(QGraphicsItem *parent) : |
10 | EditableAxisLabel(parent) |
11 | { |
12 | |
13 | } |
14 | |
15 | void DateTimeAxisLabel::finishEditing() |
16 | { |
17 | QDateTime oldDateTime = m_dateTime; |
18 | QDateTime newDateTime = QDateTime::fromString(string: document()->toPlainText(), format: m_format); |
19 | if (newDateTime.isValid() && newDateTime != m_dateTime) { |
20 | m_dateTime = newDateTime; |
21 | emit dateTimeChanged(oldDateTime, newDateTime); |
22 | } else { |
23 | document()->setHtml(m_htmlBeforeEdit); |
24 | } |
25 | } |
26 | |
27 | QDateTime DateTimeAxisLabel::value() const |
28 | { |
29 | return m_dateTime; |
30 | } |
31 | |
32 | void DateTimeAxisLabel::setValue(const QDateTime &value) |
33 | { |
34 | setTextInteractionFlags(Qt::NoTextInteraction); |
35 | clearFocus(); |
36 | m_dateTime = value; |
37 | } |
38 | |
39 | void DateTimeAxisLabel::resetBeforeEditValue() |
40 | { |
41 | m_dateTime = m_dateTimeBeforeEdit; |
42 | } |
43 | |
44 | void DateTimeAxisLabel::setFormat(const QString &format) |
45 | { |
46 | m_format = format; |
47 | // Labels should be edited as a single line regardless to their |
48 | // format because enter triggers applying the current text. |
49 | m_format.replace(before: QChar::fromLatin1(c: '\n'), after: QChar::fromLatin1(c: ' ')); |
50 | } |
51 | |
52 | void DateTimeAxisLabel::setInitialEditValue() |
53 | { |
54 | m_dateTimeBeforeEdit = m_dateTime; |
55 | setHtml(m_dateTime.toString(format: m_format)); |
56 | } |
57 | |
58 | void DateTimeAxisLabel::keyPressEvent(QKeyEvent *event) |
59 | { |
60 | if (isEditEndingKeyPress(event)) { |
61 | // prevent further event processing with a return |
62 | // because the focusOutEvent could have triggered |
63 | // a range change which might have invalidated the current label |
64 | return; |
65 | } |
66 | |
67 | QGraphicsTextItem::keyPressEvent(event); |
68 | } |
69 | |
70 | QT_END_NAMESPACE |
71 | |
72 | #include "moc_datetimeaxislabel_p.cpp" |
73 |