1 /****************************************************************************
2 **
3 ** Copyright (C) 2021 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtWidgets module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
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 Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QDATETIMEEDIT_P_H
41 #define QDATETIMEEDIT_P_H
42 
43 //
44 //  W A R N I N G
45 //  -------------
46 //
47 // This file is not part of the Qt API.  It exists purely as an
48 // implementation detail.  This header file may change from version to
49 // version without notice, or even be removed.
50 //
51 // We mean it.
52 //
53 
54 #include <QtWidgets/private/qtwidgetsglobal_p.h>
55 #include <QtCore/qcalendar.h>
56 #include "QtWidgets/qcalendarwidget.h"
57 #include "QtWidgets/qspinbox.h"
58 #include "QtWidgets/qtoolbutton.h"
59 #include "QtWidgets/qmenu.h"
60 #include "QtWidgets/qdatetimeedit.h"
61 #include "private/qabstractspinbox_p.h"
62 #include "private/qdatetimeparser_p.h"
63 
64 #include "qdebug.h"
65 
66 QT_BEGIN_NAMESPACE
67 
68 class QCalendarPopup;
69 class Q_AUTOTEST_EXPORT QDateTimeEditPrivate : public QAbstractSpinBoxPrivate, public QDateTimeParser
70 {
71     Q_DECLARE_PUBLIC(QDateTimeEdit)
72 public:
73     QDateTimeEditPrivate();
74 
75     void init(const QVariant &var);
76     void readLocaleSettings();
77 
78     QDateTime validateAndInterpret(QString &input, int &, QValidator::State &state,
79                                    bool fixup = false) const;
80     void clearSection(int index);
81 
82     // Override QAbstractSpinBoxPrivate:
83     void emitSignals(EmitPolicy ep, const QVariant &old) override;
84     QString textFromValue(const QVariant &f) const override;
85     QVariant valueFromText(const QString &f) const override;
86     void _q_editorCursorPositionChanged(int oldpos, int newpos) override;
87     void interpret(EmitPolicy ep) override;
88     void clearCache() const override;
89     QStyle::SubControl newHoverControl(const QPoint &pos) override;
90     void updateEditFieldGeometry() override;
91     QVariant getZeroVariant() const override;
92     void setRange(const QVariant &min, const QVariant &max) override;
93     void updateEdit() override;
94 
95     // Override QDateTimeParser:
displayText()96     QString displayText() const override { return edit->text(); }
getMinimum()97     QDateTime getMinimum() const override
98     {
99         if (keyboardTracking)
100             return minimum.toDateTime();
101         if (spec != Qt::LocalTime)
102             return QDateTime(QDATETIMEEDIT_DATE_MIN.startOfDay(spec));
103         return QDateTimeParser::getMinimum();
104     }
getMaximum()105     QDateTime getMaximum() const override
106     {
107         if (keyboardTracking)
108             return maximum.toDateTime();
109         if (spec != Qt::LocalTime)
110             return QDateTime(QDATETIMEEDIT_DATE_MAX.endOfDay(spec));
111         return QDateTimeParser::getMaximum();
112     }
locale()113     QLocale locale() const override { return q_func()->locale(); }
114     QString getAmPmText(AmPm ap, Case cs) const override;
cursorPosition()115     int cursorPosition() const override { return edit ? edit->cursorPosition() : -1; }
116 
117     int absoluteIndex(QDateTimeEdit::Section s, int index) const;
118     int absoluteIndex(const SectionNode &s) const;
119     QDateTime stepBy(int index, int steps, bool test = false) const;
120     int sectionAt(int pos) const;
121     int closestSection(int index, bool forward) const;
122     int nextPrevSection(int index, bool forward) const;
123     void setSelected(int index, bool forward = false);
124 
125     void updateCache(const QVariant &val, const QString &str) const;
126 
127     void updateTimeSpec();
valueToText(const QVariant & var)128     QString valueToText(const QVariant &var) const { return textFromValue(var); }
129 
130     void _q_resetButton();
131     void updateArrow(QStyle::StateFlag state);
132     bool calendarPopupEnabled() const;
133     void syncCalendarWidget();
134 
135     bool isSeparatorKey(const QKeyEvent *k) const;
136 
137     static QDateTimeEdit::Sections convertSections(QDateTimeParser::Sections s);
138     static QDateTimeEdit::Section convertToPublic(QDateTimeParser::Section s);
139 
140     void initCalendarPopup(QCalendarWidget *cw = nullptr);
141     void positionCalendarPopup();
142 
143     QDateTimeEdit::Sections sections;
144     mutable bool cacheGuard;
145 
146     QString defaultDateFormat, defaultTimeFormat, defaultDateTimeFormat, unreversedFormat;
147     mutable QVariant conflictGuard;
148     bool hasHadFocus, formatExplicitlySet, calendarPopup;
149     QStyle::StateFlag arrowState;
150     QCalendarPopup *monthCalendar;
151 
152 #ifdef QT_KEYPAD_NAVIGATION
153     bool focusOnButton;
154 #endif
155 
156     Qt::TimeSpec spec = Qt::LocalTime;
157 };
158 
159 
160 class QCalendarPopup : public QWidget
161 {
162     Q_OBJECT
163 public:
164     explicit QCalendarPopup(QWidget *parent = nullptr, QCalendarWidget *cw = nullptr,
165                             QCalendar ca = QCalendar());
selectedDate()166     QDate selectedDate() { return verifyCalendarInstance()->selectedDate(); }
167     void setDate(QDate date);
168     void setDateRange(QDate min, QDate max);
setFirstDayOfWeek(Qt::DayOfWeek dow)169     void setFirstDayOfWeek(Qt::DayOfWeek dow) { verifyCalendarInstance()->setFirstDayOfWeek(dow); }
calendarWidget()170     QCalendarWidget *calendarWidget() const { return const_cast<QCalendarPopup*>(this)->verifyCalendarInstance(); }
171     void setCalendarWidget(QCalendarWidget *cw);
172 Q_SIGNALS:
173     void activated(QDate date);
174     void newDateSelected(QDate newDate);
175     void hidingCalendar(QDate oldDate);
176     void resetButton();
177 
178 private Q_SLOTS:
179     void dateSelected(QDate date);
180     void dateSelectionChanged();
181 
182 protected:
183     void hideEvent(QHideEvent *) override;
184     void mousePressEvent(QMouseEvent *e) override;
185     void mouseReleaseEvent(QMouseEvent *) override;
186     bool event(QEvent *e) override;
187 
188 private:
189     QCalendarWidget *verifyCalendarInstance();
190 
191     QPointer<QCalendarWidget> calendar;
192     QDate oldDate;
193     bool dateChanged;
194     QCalendar calendarSystem;
195 };
196 
197 QT_END_NAMESPACE
198 
199 #endif // QDATETIMEEDIT_P_H
200