1 | /* -*- C++ -*- |
2 | This file is part of the KDE libraries |
3 | SPDX-FileCopyrightText: 1997 Tim D. Gilman <tdgilman@best.org> |
4 | SPDX-FileCopyrightText: 1998-2001 Mirko Boehm <mirko@kde.org> |
5 | SPDX-FileCopyrightText: 2007 John Layt <john@layt.net> |
6 | |
7 | SPDX-License-Identifier: LGPL-2.0-or-later |
8 | */ |
9 | |
10 | #ifndef KDATEPICKER_H |
11 | #define KDATEPICKER_H |
12 | |
13 | #include <kwidgetsaddons_export.h> |
14 | |
15 | #include <QDate> |
16 | #include <QFrame> |
17 | #include <memory> |
18 | |
19 | class QLineEdit; |
20 | class KDateTable; |
21 | |
22 | /*! |
23 | * \class KDatePicker |
24 | * \inmodule KWidgetsAddons |
25 | * |
26 | * \brief A date selection widget. |
27 | * |
28 | * Provides a widget for calendar date input. |
29 | * |
30 | * Different from the previous versions, it now emits two types of signals, |
31 | * either dateSelected() or dateEntered() (see documentation for both signals). |
32 | * |
33 | * A line edit has been added in the newer versions to allow the user |
34 | * to select a date directly by entering numbers like 19990101 |
35 | * or 990101. |
36 | * |
37 | * \image kdatepicker.png "KDatePicker Widget" |
38 | */ |
39 | class KWIDGETSADDONS_EXPORT KDatePicker : public QFrame |
40 | { |
41 | Q_OBJECT |
42 | /*! |
43 | * \property KDatePicker::date |
44 | */ |
45 | Q_PROPERTY(QDate date READ date WRITE setDate NOTIFY dateChanged USER true) |
46 | |
47 | /*! |
48 | * \property KDatePicker::closeButton |
49 | */ |
50 | Q_PROPERTY(bool closeButton READ hasCloseButton WRITE setCloseButton) |
51 | |
52 | /*! |
53 | * \property KDatePicker::fontSize |
54 | */ |
55 | Q_PROPERTY(int fontSize READ fontSize WRITE setFontSize) |
56 | |
57 | public: |
58 | /*! |
59 | * The constructor. The current date will be displayed initially. |
60 | */ |
61 | explicit KDatePicker(QWidget *parent = nullptr); |
62 | |
63 | /*! |
64 | * The constructor. The given date will be displayed initially. |
65 | */ |
66 | explicit KDatePicker(const QDate &dt, QWidget *parent = nullptr); |
67 | |
68 | ~KDatePicker() override; |
69 | |
70 | /*! |
71 | * The size hint for date pickers. The size hint recommends the |
72 | * minimum size of the widget so that all elements may be placed |
73 | * without clipping. This sometimes looks ugly, so when using the |
74 | * size hint, try adding 28 to each of the reported numbers of |
75 | * pixels. |
76 | */ |
77 | QSize sizeHint() const override; |
78 | |
79 | /*! |
80 | * Sets the date. |
81 | * |
82 | * Returns \c false and does not change anything if the date given is invalid. |
83 | */ |
84 | bool setDate(const QDate &date); |
85 | |
86 | /*! |
87 | * Returns the selected date. |
88 | */ |
89 | const QDate &date() const; |
90 | |
91 | /*! |
92 | * Sets the font size of the widgets elements. |
93 | */ |
94 | void setFontSize(int); |
95 | |
96 | /*! |
97 | * Returns the font size of the widget elements. |
98 | */ |
99 | int fontSize() const; |
100 | |
101 | /*! |
102 | * By calling this method with \a enable = true, KDatePicker will show |
103 | * a little close-button in the upper button-row. Clicking the |
104 | * close-button will cause the KDatePicker's topLevelWidget()'s close() |
105 | * method being called. This is mostly useful for toplevel datepickers |
106 | * without a window manager decoration. |
107 | * \sa hasCloseButton |
108 | */ |
109 | void setCloseButton(bool enable); |
110 | |
111 | /*! |
112 | * Returns true if a KDatePicker shows a close-button. |
113 | * \sa setCloseButton |
114 | */ |
115 | bool hasCloseButton() const; |
116 | |
117 | /*! |
118 | * Sets the range of dates that can be accepted. |
119 | * |
120 | * Invalid dates can be used to define open-ended ranges. |
121 | * If both values are valid, the minimum date must be less than |
122 | * or equal to the maximum date, otherwise the date range will |
123 | * not be set. |
124 | * |
125 | * \a minDate the minimum date |
126 | * |
127 | * \a maxDate the maximum date |
128 | * |
129 | * \since 6.12 |
130 | */ |
131 | void setDateRange(const QDate &minDate, const QDate &maxDate = QDate()); |
132 | |
133 | protected: |
134 | // to catch move keyEvents when QLineEdit has keyFocus |
135 | bool eventFilter(QObject *o, QEvent *e) override; |
136 | void resizeEvent(QResizeEvent *) override; |
137 | void changeEvent(QEvent *event) override; |
138 | |
139 | protected Q_SLOTS: |
140 | void dateChangedSlot(const QDate &date); |
141 | void tableClickedSlot(); |
142 | void monthForwardClicked(); |
143 | void monthBackwardClicked(); |
144 | void yearForwardClicked(); |
145 | void yearBackwardClicked(); |
146 | void selectMonthClicked(); |
147 | void selectYearClicked(); |
148 | void uncheckYearSelector(); |
149 | void lineEnterPressed(); |
150 | void todayButtonClicked(); |
151 | void weekSelected(int); |
152 | |
153 | Q_SIGNALS: |
154 | /*! |
155 | * This signal is emitted each time the selected date is changed. |
156 | * Usually, this does not mean that the date has been entered, |
157 | * since the date also changes, for example, when another month is |
158 | * selected. |
159 | * \sa dateSelected |
160 | */ |
161 | void dateChanged(const QDate &date); |
162 | |
163 | /*! |
164 | * This signal is emitted each time a day has been selected by |
165 | * clicking on the table (hitting a day in the current month). It |
166 | * has the same meaning as dateSelected() in older versions of |
167 | * KDatePicker. |
168 | */ |
169 | void dateSelected(const QDate &date); |
170 | |
171 | /*! |
172 | * This signal is emitted when enter is pressed and a VALID date |
173 | * has been entered before into the line edit. Connect to both |
174 | * dateEntered() and dateSelected() to receive all events where the |
175 | * user really enters a date. |
176 | */ |
177 | void dateEntered(const QDate &date); |
178 | |
179 | /*! |
180 | * This signal is emitted when the day has been selected by |
181 | * clicking on it in the table. |
182 | */ |
183 | void tableClicked(); |
184 | |
185 | private: |
186 | KWIDGETSADDONS_NO_EXPORT KDateTable *dateTable() const; |
187 | KWIDGETSADDONS_NO_EXPORT void initWidget(const QDate &date); |
188 | |
189 | private: |
190 | friend class KDatePickerPrivate; |
191 | std::unique_ptr<class KDatePickerPrivate> const d; |
192 | }; |
193 | |
194 | #endif // KDATEPICKER_H |
195 | |