Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/widgets/widgets/qcalendarwidget.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||
2 | ** | - | ||||||||||||
3 | ** Copyright (C) 2016 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 | #include "qcalendarwidget.h" | - | ||||||||||||
41 | - | |||||||||||||
42 | #ifndef QT_NO_CALENDARWIDGET | - | ||||||||||||
43 | - | |||||||||||||
44 | #include <qabstractitemmodel.h> | - | ||||||||||||
45 | #include <qitemdelegate.h> | - | ||||||||||||
46 | #include <qdatetime.h> | - | ||||||||||||
47 | #include <qtableview.h> | - | ||||||||||||
48 | #include <qlayout.h> | - | ||||||||||||
49 | #include <qevent.h> | - | ||||||||||||
50 | #include <qtextformat.h> | - | ||||||||||||
51 | #include <qheaderview.h> | - | ||||||||||||
52 | #include <private/qwidget_p.h> | - | ||||||||||||
53 | #include <qpushbutton.h> | - | ||||||||||||
54 | #include <qtoolbutton.h> | - | ||||||||||||
55 | #include <qlabel.h> | - | ||||||||||||
56 | #include <qspinbox.h> | - | ||||||||||||
57 | #include <qmenu.h> | - | ||||||||||||
58 | #include <qapplication.h> | - | ||||||||||||
59 | #include <qbasictimer.h> | - | ||||||||||||
60 | #include <qstylepainter.h> | - | ||||||||||||
61 | - | |||||||||||||
62 | #include <vector> | - | ||||||||||||
63 | - | |||||||||||||
64 | QT_BEGIN_NAMESPACE | - | ||||||||||||
65 | - | |||||||||||||
66 | enum { | - | ||||||||||||
67 | RowCount = 6, | - | ||||||||||||
68 | ColumnCount = 7, | - | ||||||||||||
69 | HeaderColumn = 0, | - | ||||||||||||
70 | HeaderRow = 0, | - | ||||||||||||
71 | MinimumDayOffset = 1 | - | ||||||||||||
72 | }; | - | ||||||||||||
73 | - | |||||||||||||
74 | namespace { | - | ||||||||||||
75 | - | |||||||||||||
76 | static QString formatNumber(int number, int fieldWidth) | - | ||||||||||||
77 | { | - | ||||||||||||
78 | return QString::number(number).rightJustified(fieldWidth, QLatin1Char('0')); | - | ||||||||||||
79 | } | - | ||||||||||||
80 | - | |||||||||||||
81 | class QCalendarDateSectionValidator | - | ||||||||||||
82 | { | - | ||||||||||||
83 | public: | - | ||||||||||||
84 | - | |||||||||||||
85 | enum Section { | - | ||||||||||||
86 | NextSection, | - | ||||||||||||
87 | ThisSection, | - | ||||||||||||
88 | PrevSection | - | ||||||||||||
89 | }; | - | ||||||||||||
90 | - | |||||||||||||
91 | QCalendarDateSectionValidator() {} | - | ||||||||||||
92 | virtual ~QCalendarDateSectionValidator() {} | - | ||||||||||||
93 | virtual Section handleKey(int key) = 0; | - | ||||||||||||
94 | virtual QDate applyToDate(const QDate &date) const = 0; | - | ||||||||||||
95 | virtual void setDate(const QDate &date) = 0; | - | ||||||||||||
96 | virtual QString text() const = 0; | - | ||||||||||||
97 | virtual QString text(const QDate &date, int repeat) const = 0; | - | ||||||||||||
98 | - | |||||||||||||
99 | QLocale m_locale; | - | ||||||||||||
100 | - | |||||||||||||
101 | protected: | - | ||||||||||||
102 | static QString highlightString(const QString &str, int pos); | - | ||||||||||||
103 | private: | - | ||||||||||||
104 | }; | - | ||||||||||||
105 | - | |||||||||||||
106 | QString QCalendarDateSectionValidator::highlightString(const QString &str, int pos) | - | ||||||||||||
107 | { | - | ||||||||||||
108 | if (pos == 0) | - | ||||||||||||
109 | return QLatin1String("<b>") + str + QLatin1String("</b>"); | - | ||||||||||||
110 | int startPos = str.length() - pos; | - | ||||||||||||
111 | return str.midRef(0, startPos) + QLatin1String("<b>") + str.midRef(startPos, pos) + QLatin1String("</b>"); | - | ||||||||||||
112 | - | |||||||||||||
113 | } | - | ||||||||||||
114 | - | |||||||||||||
115 | class QCalendarDayValidator : public QCalendarDateSectionValidator | - | ||||||||||||
116 | { | - | ||||||||||||
117 | - | |||||||||||||
118 | public: | - | ||||||||||||
119 | QCalendarDayValidator(); | - | ||||||||||||
120 | virtual Section handleKey(int key) Q_DECL_OVERRIDE; | - | ||||||||||||
121 | virtual QDate applyToDate(const QDate &date) const Q_DECL_OVERRIDE; | - | ||||||||||||
122 | virtual void setDate(const QDate &date) Q_DECL_OVERRIDE; | - | ||||||||||||
123 | virtual QString text() const Q_DECL_OVERRIDE; | - | ||||||||||||
124 | virtual QString text(const QDate &date, int repeat) const Q_DECL_OVERRIDE; | - | ||||||||||||
125 | private: | - | ||||||||||||
126 | int m_pos; | - | ||||||||||||
127 | int m_day; | - | ||||||||||||
128 | int m_oldDay; | - | ||||||||||||
129 | }; | - | ||||||||||||
130 | - | |||||||||||||
131 | QCalendarDayValidator::QCalendarDayValidator() | - | ||||||||||||
132 | : QCalendarDateSectionValidator(), m_pos(0), m_day(1), m_oldDay(1) | - | ||||||||||||
133 | { | - | ||||||||||||
134 | } | - | ||||||||||||
135 | - | |||||||||||||
136 | QCalendarDateSectionValidator::Section QCalendarDayValidator::handleKey(int key) | - | ||||||||||||
137 | { | - | ||||||||||||
138 | if (key == Qt::Key_Right || key == Qt::Key_Left) { | - | ||||||||||||
139 | m_pos = 0; | - | ||||||||||||
140 | return QCalendarDateSectionValidator::ThisSection; | - | ||||||||||||
141 | } else if (key == Qt::Key_Up) { | - | ||||||||||||
142 | m_pos = 0; | - | ||||||||||||
143 | ++m_day; | - | ||||||||||||
144 | if (m_day > 31) | - | ||||||||||||
145 | m_day = 1; | - | ||||||||||||
146 | return QCalendarDateSectionValidator::ThisSection; | - | ||||||||||||
147 | } else if (key == Qt::Key_Down) { | - | ||||||||||||
148 | m_pos = 0; | - | ||||||||||||
149 | --m_day; | - | ||||||||||||
150 | if (m_day < 1) | - | ||||||||||||
151 | m_day = 31; | - | ||||||||||||
152 | return QCalendarDateSectionValidator::ThisSection; | - | ||||||||||||
153 | } else if (key == Qt::Key_Back || key == Qt::Key_Backspace) { | - | ||||||||||||
154 | --m_pos; | - | ||||||||||||
155 | if (m_pos < 0) | - | ||||||||||||
156 | m_pos = 1; | - | ||||||||||||
157 | - | |||||||||||||
158 | if (m_pos == 0) | - | ||||||||||||
159 | m_day = m_oldDay; | - | ||||||||||||
160 | else | - | ||||||||||||
161 | m_day = m_day / 10; | - | ||||||||||||
162 | //m_day = m_oldDay / 10 * 10 + m_day / 10; | - | ||||||||||||
163 | - | |||||||||||||
164 | if (m_pos == 0) | - | ||||||||||||
165 | return QCalendarDateSectionValidator::PrevSection; | - | ||||||||||||
166 | return QCalendarDateSectionValidator::ThisSection; | - | ||||||||||||
167 | } | - | ||||||||||||
168 | if (key < Qt::Key_0 || key > Qt::Key_9) | - | ||||||||||||
169 | return QCalendarDateSectionValidator::ThisSection; | - | ||||||||||||
170 | int pressedKey = key - Qt::Key_0; | - | ||||||||||||
171 | if (m_pos == 0) | - | ||||||||||||
172 | m_day = pressedKey; | - | ||||||||||||
173 | else | - | ||||||||||||
174 | m_day = m_day % 10 * 10 + pressedKey; | - | ||||||||||||
175 | if (m_day > 31) | - | ||||||||||||
176 | m_day = 31; | - | ||||||||||||
177 | ++m_pos; | - | ||||||||||||
178 | if (m_pos > 1) { | - | ||||||||||||
179 | m_pos = 0; | - | ||||||||||||
180 | return QCalendarDateSectionValidator::NextSection; | - | ||||||||||||
181 | } | - | ||||||||||||
182 | return QCalendarDateSectionValidator::ThisSection; | - | ||||||||||||
183 | } | - | ||||||||||||
184 | - | |||||||||||||
185 | QDate QCalendarDayValidator::applyToDate(const QDate &date) const | - | ||||||||||||
186 | { | - | ||||||||||||
187 | int day = m_day; | - | ||||||||||||
188 | if (day < 1) | - | ||||||||||||
189 | day = 1; | - | ||||||||||||
190 | else if (day > 31) | - | ||||||||||||
191 | day = 31; | - | ||||||||||||
192 | if (day > date.daysInMonth()) | - | ||||||||||||
193 | day = date.daysInMonth(); | - | ||||||||||||
194 | return QDate(date.year(), date.month(), day); | - | ||||||||||||
195 | } | - | ||||||||||||
196 | - | |||||||||||||
197 | void QCalendarDayValidator::setDate(const QDate &date) | - | ||||||||||||
198 | { | - | ||||||||||||
199 | m_day = m_oldDay = date.day(); | - | ||||||||||||
200 | m_pos = 0; | - | ||||||||||||
201 | } | - | ||||||||||||
202 | - | |||||||||||||
203 | QString QCalendarDayValidator::text() const | - | ||||||||||||
204 | { | - | ||||||||||||
205 | return highlightString(formatNumber(m_day, 2), m_pos); | - | ||||||||||||
206 | } | - | ||||||||||||
207 | - | |||||||||||||
208 | QString QCalendarDayValidator::text(const QDate &date, int repeat) const | - | ||||||||||||
209 | { | - | ||||||||||||
210 | if (repeat <= 1) { | - | ||||||||||||
211 | return QString::number(date.day()); | - | ||||||||||||
212 | } else if (repeat == 2) { | - | ||||||||||||
213 | return formatNumber(date.day(), 2); | - | ||||||||||||
214 | } else if (repeat == 3) { | - | ||||||||||||
215 | return m_locale.dayName(date.dayOfWeek(), QLocale::ShortFormat); | - | ||||||||||||
216 | } else /* repeat >= 4 */ { | - | ||||||||||||
217 | return m_locale.dayName(date.dayOfWeek(), QLocale::LongFormat); | - | ||||||||||||
218 | } | - | ||||||||||||
219 | } | - | ||||||||||||
220 | - | |||||||||||||
221 | ////////////////////////////////// | - | ||||||||||||
222 | - | |||||||||||||
223 | class QCalendarMonthValidator : public QCalendarDateSectionValidator | - | ||||||||||||
224 | { | - | ||||||||||||
225 | - | |||||||||||||
226 | public: | - | ||||||||||||
227 | QCalendarMonthValidator(); | - | ||||||||||||
228 | virtual Section handleKey(int key) Q_DECL_OVERRIDE; | - | ||||||||||||
229 | virtual QDate applyToDate(const QDate &date) const Q_DECL_OVERRIDE; | - | ||||||||||||
230 | virtual void setDate(const QDate &date) Q_DECL_OVERRIDE; | - | ||||||||||||
231 | virtual QString text() const Q_DECL_OVERRIDE; | - | ||||||||||||
232 | virtual QString text(const QDate &date, int repeat) const Q_DECL_OVERRIDE; | - | ||||||||||||
233 | private: | - | ||||||||||||
234 | int m_pos; | - | ||||||||||||
235 | int m_month; | - | ||||||||||||
236 | int m_oldMonth; | - | ||||||||||||
237 | }; | - | ||||||||||||
238 | - | |||||||||||||
239 | QCalendarMonthValidator::QCalendarMonthValidator() | - | ||||||||||||
240 | : QCalendarDateSectionValidator(), m_pos(0), m_month(1), m_oldMonth(1) | - | ||||||||||||
241 | { | - | ||||||||||||
242 | } | - | ||||||||||||
243 | - | |||||||||||||
244 | QCalendarDateSectionValidator::Section QCalendarMonthValidator::handleKey(int key) | - | ||||||||||||
245 | { | - | ||||||||||||
246 | if (key == Qt::Key_Right || key == Qt::Key_Left) { | - | ||||||||||||
247 | m_pos = 0; | - | ||||||||||||
248 | return QCalendarDateSectionValidator::ThisSection; | - | ||||||||||||
249 | } else if (key == Qt::Key_Up) { | - | ||||||||||||
250 | m_pos = 0; | - | ||||||||||||
251 | ++m_month; | - | ||||||||||||
252 | if (m_month > 12) | - | ||||||||||||
253 | m_month = 1; | - | ||||||||||||
254 | return QCalendarDateSectionValidator::ThisSection; | - | ||||||||||||
255 | } else if (key == Qt::Key_Down) { | - | ||||||||||||
256 | m_pos = 0; | - | ||||||||||||
257 | --m_month; | - | ||||||||||||
258 | if (m_month < 1) | - | ||||||||||||
259 | m_month = 12; | - | ||||||||||||
260 | return QCalendarDateSectionValidator::ThisSection; | - | ||||||||||||
261 | } else if (key == Qt::Key_Back || key == Qt::Key_Backspace) { | - | ||||||||||||
262 | --m_pos; | - | ||||||||||||
263 | if (m_pos < 0) | - | ||||||||||||
264 | m_pos = 1; | - | ||||||||||||
265 | - | |||||||||||||
266 | if (m_pos == 0) | - | ||||||||||||
267 | m_month = m_oldMonth; | - | ||||||||||||
268 | else | - | ||||||||||||
269 | m_month = m_month / 10; | - | ||||||||||||
270 | //m_month = m_oldMonth / 10 * 10 + m_month / 10; | - | ||||||||||||
271 | - | |||||||||||||
272 | if (m_pos == 0) | - | ||||||||||||
273 | return QCalendarDateSectionValidator::PrevSection; | - | ||||||||||||
274 | return QCalendarDateSectionValidator::ThisSection; | - | ||||||||||||
275 | } | - | ||||||||||||
276 | if (key < Qt::Key_0 || key > Qt::Key_9) | - | ||||||||||||
277 | return QCalendarDateSectionValidator::ThisSection; | - | ||||||||||||
278 | int pressedKey = key - Qt::Key_0; | - | ||||||||||||
279 | if (m_pos == 0) | - | ||||||||||||
280 | m_month = pressedKey; | - | ||||||||||||
281 | else | - | ||||||||||||
282 | m_month = m_month % 10 * 10 + pressedKey; | - | ||||||||||||
283 | if (m_month > 12) | - | ||||||||||||
284 | m_month = 12; | - | ||||||||||||
285 | ++m_pos; | - | ||||||||||||
286 | if (m_pos > 1) { | - | ||||||||||||
287 | m_pos = 0; | - | ||||||||||||
288 | return QCalendarDateSectionValidator::NextSection; | - | ||||||||||||
289 | } | - | ||||||||||||
290 | return QCalendarDateSectionValidator::ThisSection; | - | ||||||||||||
291 | } | - | ||||||||||||
292 | - | |||||||||||||
293 | QDate QCalendarMonthValidator::applyToDate(const QDate &date) const | - | ||||||||||||
294 | { | - | ||||||||||||
295 | int month = m_month; | - | ||||||||||||
296 | if (month < 1) | - | ||||||||||||
297 | month = 1; | - | ||||||||||||
298 | else if (month > 12) | - | ||||||||||||
299 | month = 12; | - | ||||||||||||
300 | QDate newDate(date.year(), m_month, 1); | - | ||||||||||||
301 | int day = date.day(); | - | ||||||||||||
302 | if (day > newDate.daysInMonth()) | - | ||||||||||||
303 | day = newDate.daysInMonth(); | - | ||||||||||||
304 | return QDate(date.year(), month, day); | - | ||||||||||||
305 | } | - | ||||||||||||
306 | - | |||||||||||||
307 | void QCalendarMonthValidator::setDate(const QDate &date) | - | ||||||||||||
308 | { | - | ||||||||||||
309 | m_month = m_oldMonth = date.month(); | - | ||||||||||||
310 | m_pos = 0; | - | ||||||||||||
311 | } | - | ||||||||||||
312 | - | |||||||||||||
313 | QString QCalendarMonthValidator::text() const | - | ||||||||||||
314 | { | - | ||||||||||||
315 | return highlightString(formatNumber(m_month, 2), m_pos); | - | ||||||||||||
316 | } | - | ||||||||||||
317 | - | |||||||||||||
318 | QString QCalendarMonthValidator::text(const QDate &date, int repeat) const | - | ||||||||||||
319 | { | - | ||||||||||||
320 | if (repeat <= 1) { | - | ||||||||||||
321 | return QString::number(date.month()); | - | ||||||||||||
322 | } else if (repeat == 2) { | - | ||||||||||||
323 | return formatNumber(date.month(), 2); | - | ||||||||||||
324 | } else if (repeat == 3) { | - | ||||||||||||
325 | return m_locale.standaloneMonthName(date.month(), QLocale::ShortFormat); | - | ||||||||||||
326 | } else /*if (repeat >= 4)*/ { | - | ||||||||||||
327 | return m_locale.standaloneMonthName(date.month(), QLocale::LongFormat); | - | ||||||||||||
328 | } | - | ||||||||||||
329 | } | - | ||||||||||||
330 | - | |||||||||||||
331 | ////////////////////////////////// | - | ||||||||||||
332 | - | |||||||||||||
333 | class QCalendarYearValidator : public QCalendarDateSectionValidator | - | ||||||||||||
334 | { | - | ||||||||||||
335 | - | |||||||||||||
336 | public: | - | ||||||||||||
337 | QCalendarYearValidator(); | - | ||||||||||||
338 | virtual Section handleKey(int key) Q_DECL_OVERRIDE; | - | ||||||||||||
339 | virtual QDate applyToDate(const QDate &date) const Q_DECL_OVERRIDE; | - | ||||||||||||
340 | virtual void setDate(const QDate &date) Q_DECL_OVERRIDE; | - | ||||||||||||
341 | virtual QString text() const Q_DECL_OVERRIDE; | - | ||||||||||||
342 | virtual QString text(const QDate &date, int repeat) const Q_DECL_OVERRIDE; | - | ||||||||||||
343 | private: | - | ||||||||||||
344 | int pow10(int n); | - | ||||||||||||
345 | int m_pos; | - | ||||||||||||
346 | int m_year; | - | ||||||||||||
347 | int m_oldYear; | - | ||||||||||||
348 | }; | - | ||||||||||||
349 | - | |||||||||||||
350 | QCalendarYearValidator::QCalendarYearValidator() | - | ||||||||||||
351 | : QCalendarDateSectionValidator(), m_pos(0), m_year(2000), m_oldYear(2000) | - | ||||||||||||
352 | { | - | ||||||||||||
353 | } | - | ||||||||||||
354 | - | |||||||||||||
355 | int QCalendarYearValidator::pow10(int n) | - | ||||||||||||
356 | { | - | ||||||||||||
357 | int power = 1; | - | ||||||||||||
358 | for (int i = 0; i < n; i++) | - | ||||||||||||
359 | power *= 10; | - | ||||||||||||
360 | return power; | - | ||||||||||||
361 | } | - | ||||||||||||
362 | - | |||||||||||||
363 | QCalendarDateSectionValidator::Section QCalendarYearValidator::handleKey(int key) | - | ||||||||||||
364 | { | - | ||||||||||||
365 | if (key == Qt::Key_Right || key == Qt::Key_Left) { | - | ||||||||||||
366 | m_pos = 0; | - | ||||||||||||
367 | return QCalendarDateSectionValidator::ThisSection; | - | ||||||||||||
368 | } else if (key == Qt::Key_Up) { | - | ||||||||||||
369 | m_pos = 0; | - | ||||||||||||
370 | ++m_year; | - | ||||||||||||
371 | return QCalendarDateSectionValidator::ThisSection; | - | ||||||||||||
372 | } else if (key == Qt::Key_Down) { | - | ||||||||||||
373 | m_pos = 0; | - | ||||||||||||
374 | --m_year; | - | ||||||||||||
375 | return QCalendarDateSectionValidator::ThisSection; | - | ||||||||||||
376 | } else if (key == Qt::Key_Back || key == Qt::Key_Backspace) { | - | ||||||||||||
377 | --m_pos; | - | ||||||||||||
378 | if (m_pos < 0) | - | ||||||||||||
379 | m_pos = 3; | - | ||||||||||||
380 | - | |||||||||||||
381 | int pow = pow10(m_pos); | - | ||||||||||||
382 | m_year = m_oldYear / pow * pow + m_year % (pow * 10) / 10; | - | ||||||||||||
383 | - | |||||||||||||
384 | if (m_pos == 0) | - | ||||||||||||
385 | return QCalendarDateSectionValidator::PrevSection; | - | ||||||||||||
386 | return QCalendarDateSectionValidator::ThisSection; | - | ||||||||||||
387 | } | - | ||||||||||||
388 | if (key < Qt::Key_0 || key > Qt::Key_9) | - | ||||||||||||
389 | return QCalendarDateSectionValidator::ThisSection; | - | ||||||||||||
390 | int pressedKey = key - Qt::Key_0; | - | ||||||||||||
391 | int pow = pow10(m_pos); | - | ||||||||||||
392 | m_year = m_year / (pow * 10) * (pow * 10) + m_year % pow * 10 + pressedKey; | - | ||||||||||||
393 | ++m_pos; | - | ||||||||||||
394 | if (m_pos > 3) { | - | ||||||||||||
395 | m_pos = 0; | - | ||||||||||||
396 | return QCalendarDateSectionValidator::NextSection; | - | ||||||||||||
397 | } | - | ||||||||||||
398 | return QCalendarDateSectionValidator::ThisSection; | - | ||||||||||||
399 | } | - | ||||||||||||
400 | - | |||||||||||||
401 | QDate QCalendarYearValidator::applyToDate(const QDate &date) const | - | ||||||||||||
402 | { | - | ||||||||||||
403 | int year = m_year; | - | ||||||||||||
404 | if (year < 1) | - | ||||||||||||
405 | year = 1; | - | ||||||||||||
406 | QDate newDate(year, date.month(), 1); | - | ||||||||||||
407 | int day = date.day(); | - | ||||||||||||
408 | if (day > newDate.daysInMonth()) | - | ||||||||||||
409 | day = newDate.daysInMonth(); | - | ||||||||||||
410 | return QDate(year, date.month(), day); | - | ||||||||||||
411 | } | - | ||||||||||||
412 | - | |||||||||||||
413 | void QCalendarYearValidator::setDate(const QDate &date) | - | ||||||||||||
414 | { | - | ||||||||||||
415 | m_year = m_oldYear = date.year(); | - | ||||||||||||
416 | m_pos = 0; | - | ||||||||||||
417 | } | - | ||||||||||||
418 | - | |||||||||||||
419 | QString QCalendarYearValidator::text() const | - | ||||||||||||
420 | { | - | ||||||||||||
421 | return highlightString(formatNumber(m_year, 4), m_pos); | - | ||||||||||||
422 | } | - | ||||||||||||
423 | - | |||||||||||||
424 | QString QCalendarYearValidator::text(const QDate &date, int repeat) const | - | ||||||||||||
425 | { | - | ||||||||||||
426 | if (repeat < 4) | - | ||||||||||||
427 | return formatNumber(date.year() % 100, 2); | - | ||||||||||||
428 | return QString::number(date.year()); | - | ||||||||||||
429 | } | - | ||||||||||||
430 | - | |||||||||||||
431 | /////////////////////////////////// | - | ||||||||||||
432 | - | |||||||||||||
433 | struct SectionToken { | - | ||||||||||||
434 | Q_DECL_CONSTEXPR SectionToken(QCalendarDateSectionValidator *v, int rep) | - | ||||||||||||
435 | : validator(v), repeat(rep) {} | - | ||||||||||||
436 | - | |||||||||||||
437 | QCalendarDateSectionValidator *validator; | - | ||||||||||||
438 | int repeat; | - | ||||||||||||
439 | }; | - | ||||||||||||
440 | } // unnamed namespace | - | ||||||||||||
441 | Q_DECLARE_TYPEINFO(SectionToken, Q_PRIMITIVE_TYPE); | - | ||||||||||||
442 | namespace { | - | ||||||||||||
443 | - | |||||||||||||
444 | class QCalendarDateValidator | - | ||||||||||||
445 | { | - | ||||||||||||
446 | public: | - | ||||||||||||
447 | QCalendarDateValidator(); | - | ||||||||||||
448 | ~QCalendarDateValidator(); | - | ||||||||||||
449 | - | |||||||||||||
450 | void handleKeyEvent(QKeyEvent *keyEvent); | - | ||||||||||||
451 | QString currentText() const; | - | ||||||||||||
452 | QDate currentDate() const { return m_currentDate; } | - | ||||||||||||
453 | void setFormat(const QString &format); | - | ||||||||||||
454 | void setInitialDate(const QDate &date); | - | ||||||||||||
455 | - | |||||||||||||
456 | void setLocale(const QLocale &locale); | - | ||||||||||||
457 | - | |||||||||||||
458 | private: | - | ||||||||||||
struct SectionToken { | ||||||||||||||
SectionToken(QCalendarDateSectionValidator *val, int rep): | ||||||||||||||
459 | validator(val), repeat(rep) {} | - | ||||||||||||
QCalendarDateSectionValidator *validator; | ||||||||||||||
int repeat; | ||||||||||||||
};void toNextToken(); | ||||||||||||||
460 | void toPreviousToken(); | - | ||||||||||||
461 | void applyToDate(); | - | ||||||||||||
462 | - | |||||||||||||
463 | int countRepeat(const QString &str, int index) const; | - | ||||||||||||
464 | void clear(); | - | ||||||||||||
465 | - | |||||||||||||
466 | QStringList m_separators; | - | ||||||||||||
467 | QListstd::vector<SectionToken*>> m_tokens; | - | ||||||||||||
468 | QCalendarYearValidator m_yearValidator; | - | ||||||||||||
469 | QCalendarMonthValidator m_monthValidator; | - | ||||||||||||
470 | QCalendarDayValidator m_dayValidator; | - | ||||||||||||
471 | - | |||||||||||||
472 | SectionToken *int m_currentToken; | - | ||||||||||||
473 | - | |||||||||||||
474 | QDate m_initialDate; | - | ||||||||||||
475 | QDate m_currentDate; | - | ||||||||||||
476 | - | |||||||||||||
477 | QCalendarDateSectionValidator::Section m_lastSectionMove; | - | ||||||||||||
478 | }; | - | ||||||||||||
479 | - | |||||||||||||
480 | QCalendarDateValidator::QCalendarDateValidator() | - | ||||||||||||
481 | : m_currentToken(Q_NULLPTR(-1), | - | ||||||||||||
482 | m_initialDate(QDate::currentDate()), | - | ||||||||||||
483 | m_currentDate(m_initialDate), | - | ||||||||||||
484 | m_lastSectionMove(QCalendarDateSectionValidator::ThisSection) | - | ||||||||||||
485 | { | - | ||||||||||||
486 | } never executed: end of block | 0 | ||||||||||||
487 | - | |||||||||||||
488 | void QCalendarDateValidator::setLocale(const QLocale &locale) | - | ||||||||||||
489 | { | - | ||||||||||||
490 | m_yearValidator.m_locale = locale; | - | ||||||||||||
491 | m_monthValidator.m_locale = locale; | - | ||||||||||||
492 | m_dayValidator.m_locale = locale; | - | ||||||||||||
493 | } | - | ||||||||||||
494 | - | |||||||||||||
495 | QCalendarDateValidator::~QCalendarDateValidator() | - | ||||||||||||
496 | { | - | ||||||||||||
497 | clear(); | - | ||||||||||||
498 | } | - | ||||||||||||
499 | - | |||||||||||||
500 | // from qdatetime.cpp | - | ||||||||||||
501 | int QCalendarDateValidator::countRepeat(const QString &str, int index) const | - | ||||||||||||
502 | { | - | ||||||||||||
503 | Q_ASSERT(index >= 0 && index < str.size()); | - | ||||||||||||
504 | int count = 1; | - | ||||||||||||
505 | const QChar ch = str.at(index); | - | ||||||||||||
506 | while (index + count < str.size() && str.at(index + count) == ch) | - | ||||||||||||
507 | ++count; | - | ||||||||||||
508 | return count; | - | ||||||||||||
509 | } | - | ||||||||||||
510 | - | |||||||||||||
511 | void QCalendarDateValidator::setInitialDate(const QDate &date) | - | ||||||||||||
512 | { | - | ||||||||||||
513 | m_yearValidator.setDate(date); | - | ||||||||||||
514 | m_monthValidator.setDate(date); | - | ||||||||||||
515 | m_dayValidator.setDate(date); | - | ||||||||||||
516 | m_initialDate = date; | - | ||||||||||||
517 | m_currentDate = date; | - | ||||||||||||
518 | m_lastSectionMove = QCalendarDateSectionValidator::ThisSection; | - | ||||||||||||
519 | } | - | ||||||||||||
520 | - | |||||||||||||
521 | QString QCalendarDateValidator::currentText() const | - | ||||||||||||
522 | { | - | ||||||||||||
523 | QString str; | - | ||||||||||||
524 | QStringListIterator itSep(const int numSeps = m_separators); | - | ||||||||||||
QListIterator<SectionToken *> itTok.size(); | ||||||||||||||
525 | const int numTokens = int(m_tokens); | - | ||||||||||||
while (itSep.hasNext())size()); | ||||||||||||||
526 | for (int i = 0; i < numSeps; ++i)
| 0 | ||||||||||||
527 | str += itSepm_separators.next();at(i); | - | ||||||||||||
528 | if (itTok.hasNext())i < numTokens) {
| 0 | ||||||||||||
529 | const SectionToken *token = itTok.next(); | - | ||||||||||||
QCalendarDateSectionValidator *validator&token = token->validator;m_tokens[i]; | ||||||||||||||
530 | if (m_currentTokeni == tokenm_currentToken)
| 0 | ||||||||||||
531 | str += token.validator->text(); never executed: str += token.validator->text(); | 0 | ||||||||||||
532 | else | - | ||||||||||||
533 | str += token.validator->text(m_currentDate, token->.repeat); never executed: str += token.validator->text(m_currentDate, token.repeat); | 0 | ||||||||||||
534 | } | - | ||||||||||||
535 | } never executed: end of block | 0 | ||||||||||||
536 | return str; never executed: return str; | 0 | ||||||||||||
537 | } | - | ||||||||||||
538 | - | |||||||||||||
539 | void QCalendarDateValidator::clear() | - | ||||||||||||
540 | { | - | ||||||||||||
541 | QListIterator<SectionToken *> it(m_tokens); | - | ||||||||||||
while (it.hasNext()) | ||||||||||||||
delete it.next();m_tokens.clear(); | ||||||||||||||
542 | m_separators.clear(); | - | ||||||||||||
543 | - | |||||||||||||
544 | m_currentToken = 0-1; | - | ||||||||||||
545 | } never executed: end of block | 0 | ||||||||||||
546 | - | |||||||||||||
547 | void QCalendarDateValidator::setFormat(const QString &format) | - | ||||||||||||
548 | { | - | ||||||||||||
549 | clear(); | - | ||||||||||||
550 | - | |||||||||||||
551 | int pos = 0; | - | ||||||||||||
552 | const QLatin1Char quote('\''); | - | ||||||||||||
553 | bool quoting = false; | - | ||||||||||||
554 | QString separator; | - | ||||||||||||
555 | while (pos < format.size()) {
| 0 | ||||||||||||
556 | QStringconst QStringRef mid = format.midmidRef(pos); | - | ||||||||||||
557 | int offset = 1; | - | ||||||||||||
558 | - | |||||||||||||
559 | if (mid.startsWith(quote)) {
| 0 | ||||||||||||
560 | quoting = !quoting; | - | ||||||||||||
561 | } else { never executed: end of block | 0 | ||||||||||||
562 | const QChar nextChar = format.at(pos); | - | ||||||||||||
563 | if (quoting) {
| 0 | ||||||||||||
564 | separator += nextChar; | - | ||||||||||||
565 | quoting = false; | - | ||||||||||||
566 | } else { never executed: end of block | 0 | ||||||||||||
567 | SectionTokenQCalendarDateSectionValidator *tokenvalidator = 0; | - | ||||||||||||
568 | if (nextChar == QLatin1Char('d')) {
| 0 | ||||||||||||
569 | offset = qMin(4, countRepeat(format, pos)); | - | ||||||||||||
570 | tokenvalidator = new SectionToken(&m_dayValidator, offset);; | - | ||||||||||||
571 | } else if (nextChar == QLatin1Char('M')) { never executed: end of block
| 0 | ||||||||||||
572 | offset = qMin(4, countRepeat(format, pos)); | - | ||||||||||||
573 | tokenvalidator = new SectionToken(&m_monthValidator, offset);; | - | ||||||||||||
574 | } else if (nextChar == QLatin1Char('y')) { never executed: end of block
| 0 | ||||||||||||
575 | offset = qMin(4, countRepeat(format, pos)); | - | ||||||||||||
576 | tokenvalidator = new SectionToken(&m_yearValidator, offset);; | - | ||||||||||||
577 | } else { never executed: end of block | 0 | ||||||||||||
578 | separator += nextChar; | - | ||||||||||||
579 | } never executed: end of block | 0 | ||||||||||||
580 | if (tokenvalidator) {
| 0 | ||||||||||||
581 | m_tokens.appendpush_back(token);SectionToken(validator, offset)); | - | ||||||||||||
582 | m_separators.append(separator); | - | ||||||||||||
583 | separator = QString(); | - | ||||||||||||
584 | if (!(m_currentToken < 0)
| 0 | ||||||||||||
585 | m_currentToken = tokenint(m_tokens.size()) - 1; never executed: m_currentToken = int(m_tokens.size()) - 1; | 0 | ||||||||||||
586 | - | |||||||||||||
587 | } never executed: end of block | 0 | ||||||||||||
588 | } never executed: end of block | 0 | ||||||||||||
589 | } | - | ||||||||||||
590 | pos += offset; | - | ||||||||||||
591 | } never executed: end of block | 0 | ||||||||||||
592 | m_separators += separator; | - | ||||||||||||
593 | } never executed: end of block | 0 | ||||||||||||
594 | - | |||||||||||||
595 | void QCalendarDateValidator::applyToDate() | - | ||||||||||||
596 | { | - | ||||||||||||
597 | m_currentDate = m_yearValidator.applyToDate(m_currentDate); | - | ||||||||||||
598 | m_currentDate = m_monthValidator.applyToDate(m_currentDate); | - | ||||||||||||
599 | m_currentDate = m_dayValidator.applyToDate(m_currentDate); | - | ||||||||||||
600 | } | - | ||||||||||||
601 | - | |||||||||||||
602 | void QCalendarDateValidator::toNextToken() | - | ||||||||||||
603 | { | - | ||||||||||||
604 | const int idx = m_tokens.indexOf(m_currentToken);if (idx == -1m_currentToken < 0)
| 0 | ||||||||||||
605 | return; never executed: return; | 0 | ||||||||||||
606 | if (idx + 1 >= m_tokens.count())++m_currentToken= m_tokens.first(); | - | ||||||||||||
else; | ||||||||||||||
607 | m_currentToken %= m_tokens.at(idx + 1);size(); | - | ||||||||||||
608 | } never executed: end of block | 0 | ||||||||||||
609 | - | |||||||||||||
610 | void QCalendarDateValidator::toPreviousToken() | - | ||||||||||||
611 | { | - | ||||||||||||
612 | const int idx = m_tokens.indexOf(m_currentToken);
| 0 | ||||||||||||
if (idx == -1)
| ||||||||||||||
return;
| ||||||||||||||
613 | return; never executed: return; | 0 | ||||||||||||
614 | --m_currentToken= m_tokens.last(); | - | ||||||||||||
else; | ||||||||||||||
615 | m_currentToken %= m_tokens.at(idx - 1);size(); | - | ||||||||||||
616 | } never executed: end of block | 0 | ||||||||||||
617 | - | |||||||||||||
618 | void QCalendarDateValidator::handleKeyEvent(QKeyEvent *keyEvent) | - | ||||||||||||
619 | { | - | ||||||||||||
620 | if (!(m_currentToken < 0)
| 0 | ||||||||||||
621 | return; never executed: return; | 0 | ||||||||||||
622 | - | |||||||||||||
623 | int key = keyEvent->key(); | - | ||||||||||||
624 | if (m_lastSectionMove == QCalendarDateSectionValidator::NextSection) {
| 0 | ||||||||||||
625 | if (key == Qt::Key_Back || key == Qt::Key_Backspace)
| 0 | ||||||||||||
626 | toPreviousToken(); never executed: toPreviousToken(); | 0 | ||||||||||||
627 | } never executed: end of block | 0 | ||||||||||||
628 | if (key == Qt::Key_Right)
| 0 | ||||||||||||
629 | toNextToken(); never executed: toNextToken(); | 0 | ||||||||||||
630 | else if (key == Qt::Key_Left)
| 0 | ||||||||||||
631 | toPreviousToken(); never executed: toPreviousToken(); | 0 | ||||||||||||
632 | - | |||||||||||||
633 | m_lastSectionMove = m_tokens[m_currentToken->].validator->handleKey(key); | - | ||||||||||||
634 | - | |||||||||||||
635 | applyToDate(); | - | ||||||||||||
636 | if (m_lastSectionMove == QCalendarDateSectionValidator::NextSection)
| 0 | ||||||||||||
637 | toNextToken(); never executed: toNextToken(); | 0 | ||||||||||||
638 | else if (m_lastSectionMove == QCalendarDateSectionValidator::PrevSection)
| 0 | ||||||||||||
639 | toPreviousToken(); never executed: toPreviousToken(); | 0 | ||||||||||||
640 | } never executed: end of block | 0 | ||||||||||||
641 | - | |||||||||||||
642 | ////////////////////////////////// | - | ||||||||||||
643 | - | |||||||||||||
644 | class QCalendarTextNavigator: public QObject | - | ||||||||||||
645 | { | - | ||||||||||||
646 | Q_OBJECT | - | ||||||||||||
647 | public: | - | ||||||||||||
648 | QCalendarTextNavigator(QObject *parent = 0) | - | ||||||||||||
649 | : QObject(parent), m_dateText(0), m_dateFrame(0), m_dateValidator(0), m_widget(0), m_editDelay(1500), m_date(QDate::currentDate()) { } | - | ||||||||||||
650 | - | |||||||||||||
651 | QWidget *widget() const; | - | ||||||||||||
652 | void setWidget(QWidget *widget); | - | ||||||||||||
653 | - | |||||||||||||
654 | int dateEditAcceptDelay() const; | - | ||||||||||||
655 | void setDateEditAcceptDelay(int delay); | - | ||||||||||||
656 | - | |||||||||||||
657 | void setDate(const QDate &date); | - | ||||||||||||
658 | - | |||||||||||||
659 | bool eventFilter(QObject *o, QEvent *e) Q_DECL_OVERRIDE; | - | ||||||||||||
660 | void timerEvent(QTimerEvent *e) Q_DECL_OVERRIDE; | - | ||||||||||||
661 | - | |||||||||||||
662 | signals: | - | ||||||||||||
663 | void dateChanged(const QDate &date); | - | ||||||||||||
664 | void editingFinished(); | - | ||||||||||||
665 | - | |||||||||||||
666 | private: | - | ||||||||||||
667 | void applyDate(); | - | ||||||||||||
668 | void updateDateLabel(); | - | ||||||||||||
669 | void createDateLabel(); | - | ||||||||||||
670 | void removeDateLabel(); | - | ||||||||||||
671 | - | |||||||||||||
672 | QLabel *m_dateText; | - | ||||||||||||
673 | QFrame *m_dateFrame; | - | ||||||||||||
674 | QBasicTimer m_acceptTimer; | - | ||||||||||||
675 | QCalendarDateValidator *m_dateValidator; | - | ||||||||||||
676 | QWidget *m_widget; | - | ||||||||||||
677 | int m_editDelay; | - | ||||||||||||
678 | - | |||||||||||||
679 | QDate m_date; | - | ||||||||||||
680 | }; | - | ||||||||||||
681 | - | |||||||||||||
682 | QWidget *QCalendarTextNavigator::widget() const | - | ||||||||||||
683 | { | - | ||||||||||||
684 | return m_widget; | - | ||||||||||||
685 | } | - | ||||||||||||
686 | - | |||||||||||||
687 | void QCalendarTextNavigator::setWidget(QWidget *widget) | - | ||||||||||||
688 | { | - | ||||||||||||
689 | m_widget = widget; | - | ||||||||||||
690 | } | - | ||||||||||||
691 | - | |||||||||||||
692 | void QCalendarTextNavigator::setDate(const QDate &date) | - | ||||||||||||
693 | { | - | ||||||||||||
694 | m_date = date; | - | ||||||||||||
695 | } | - | ||||||||||||
696 | - | |||||||||||||
697 | void QCalendarTextNavigator::updateDateLabel() | - | ||||||||||||
698 | { | - | ||||||||||||
699 | if (!m_widget) | - | ||||||||||||
700 | return; | - | ||||||||||||
701 | - | |||||||||||||
702 | m_acceptTimer.start(m_editDelay, this); | - | ||||||||||||
703 | - | |||||||||||||
704 | m_dateText->setText(m_dateValidator->currentText()); | - | ||||||||||||
705 | - | |||||||||||||
706 | QSize s = m_dateFrame->sizeHint(); | - | ||||||||||||
707 | QRect r = m_widget->geometry(); // later, just the table section | - | ||||||||||||
708 | QRect newRect((r.width() - s.width()) / 2, (r.height() - s.height()) / 2, s.width(), s.height()); | - | ||||||||||||
709 | m_dateFrame->setGeometry(newRect); | - | ||||||||||||
710 | // need to set palette after geometry update as phonestyle sets transparency | - | ||||||||||||
711 | // effect in move event. | - | ||||||||||||
712 | QPalette p = m_dateFrame->palette(); | - | ||||||||||||
713 | p.setBrush(QPalette::Window, m_dateFrame->window()->palette().brush(QPalette::Window)); | - | ||||||||||||
714 | m_dateFrame->setPalette(p); | - | ||||||||||||
715 | - | |||||||||||||
716 | m_dateFrame->raise(); | - | ||||||||||||
717 | m_dateFrame->show(); | - | ||||||||||||
718 | } | - | ||||||||||||
719 | - | |||||||||||||
720 | void QCalendarTextNavigator::applyDate() | - | ||||||||||||
721 | { | - | ||||||||||||
722 | QDate date = m_dateValidator->currentDate(); | - | ||||||||||||
723 | if (m_date == date) | - | ||||||||||||
724 | return; | - | ||||||||||||
725 | - | |||||||||||||
726 | m_date = date; | - | ||||||||||||
727 | emit dateChanged(date); | - | ||||||||||||
728 | } | - | ||||||||||||
729 | - | |||||||||||||
730 | void QCalendarTextNavigator::createDateLabel() | - | ||||||||||||
731 | { | - | ||||||||||||
732 | if (m_dateFrame) | - | ||||||||||||
733 | return; | - | ||||||||||||
734 | m_dateFrame = new QFrame(m_widget); | - | ||||||||||||
735 | QVBoxLayout *vl = new QVBoxLayout; | - | ||||||||||||
736 | m_dateText = new QLabel; | - | ||||||||||||
737 | vl->addWidget(m_dateText); | - | ||||||||||||
738 | m_dateFrame->setLayout(vl); | - | ||||||||||||
739 | m_dateFrame->setFrameShadow(QFrame::Plain); | - | ||||||||||||
740 | m_dateFrame->setFrameShape(QFrame::Box); | - | ||||||||||||
741 | m_dateValidator = new QCalendarDateValidator(); | - | ||||||||||||
742 | m_dateValidator->setLocale(m_widget->locale()); | - | ||||||||||||
743 | m_dateValidator->setFormat(m_widget->locale().dateFormat(QLocale::ShortFormat)); | - | ||||||||||||
744 | m_dateValidator->setInitialDate(m_date); | - | ||||||||||||
745 | - | |||||||||||||
746 | m_dateFrame->setAutoFillBackground(true); | - | ||||||||||||
747 | m_dateFrame->setBackgroundRole(QPalette::Window); | - | ||||||||||||
748 | } | - | ||||||||||||
749 | - | |||||||||||||
750 | void QCalendarTextNavigator::removeDateLabel() | - | ||||||||||||
751 | { | - | ||||||||||||
752 | if (!m_dateFrame) | - | ||||||||||||
753 | return; | - | ||||||||||||
754 | m_acceptTimer.stop(); | - | ||||||||||||
755 | m_dateFrame->hide(); | - | ||||||||||||
756 | m_dateFrame->deleteLater(); | - | ||||||||||||
757 | delete m_dateValidator; | - | ||||||||||||
758 | m_dateFrame = 0; | - | ||||||||||||
759 | m_dateText = 0; | - | ||||||||||||
760 | m_dateValidator = 0; | - | ||||||||||||
761 | } | - | ||||||||||||
762 | - | |||||||||||||
763 | bool QCalendarTextNavigator::eventFilter(QObject *o, QEvent *e) | - | ||||||||||||
764 | { | - | ||||||||||||
765 | if (m_widget) { | - | ||||||||||||
766 | if (e->type() == QEvent::KeyPress || e->type() == QEvent::KeyRelease) { | - | ||||||||||||
767 | QKeyEvent* ke = (QKeyEvent*)e; | - | ||||||||||||
768 | if ((ke->text().length() > 0 && ke->text()[0].isPrint()) || m_dateFrame) { | - | ||||||||||||
769 | if (ke->key() == Qt::Key_Return || ke->key() == Qt::Key_Enter || ke->key() == Qt::Key_Select) { | - | ||||||||||||
770 | applyDate(); | - | ||||||||||||
771 | emit editingFinished(); | - | ||||||||||||
772 | removeDateLabel(); | - | ||||||||||||
773 | } else if (ke->matches(QKeySequence::Cancel)) { | - | ||||||||||||
774 | removeDateLabel(); | - | ||||||||||||
775 | } else if (e->type() == QEvent::KeyPress) { | - | ||||||||||||
776 | createDateLabel(); | - | ||||||||||||
777 | m_dateValidator->handleKeyEvent(ke); | - | ||||||||||||
778 | updateDateLabel(); | - | ||||||||||||
779 | } | - | ||||||||||||
780 | ke->accept(); | - | ||||||||||||
781 | return true; | - | ||||||||||||
782 | } | - | ||||||||||||
783 | // If we are navigating let the user finish his date in old locate. | - | ||||||||||||
784 | // If we change our mind and want it to update immediately simply uncomment below | - | ||||||||||||
785 | /* | - | ||||||||||||
786 | } else if (e->type() == QEvent::LocaleChange) { | - | ||||||||||||
787 | if (m_dateValidator) { | - | ||||||||||||
788 | m_dateValidator->setLocale(m_widget->locale()); | - | ||||||||||||
789 | m_dateValidator->setFormat(m_widget->locale().dateFormat(QLocale::ShortFormat)); | - | ||||||||||||
790 | updateDateLabel(); | - | ||||||||||||
791 | } | - | ||||||||||||
792 | */ | - | ||||||||||||
793 | } | - | ||||||||||||
794 | } | - | ||||||||||||
795 | return QObject::eventFilter(o,e); | - | ||||||||||||
796 | } | - | ||||||||||||
797 | - | |||||||||||||
798 | void QCalendarTextNavigator::timerEvent(QTimerEvent *e) | - | ||||||||||||
799 | { | - | ||||||||||||
800 | if (e->timerId() == m_acceptTimer.timerId()) { | - | ||||||||||||
801 | applyDate(); | - | ||||||||||||
802 | removeDateLabel(); | - | ||||||||||||
803 | } | - | ||||||||||||
804 | } | - | ||||||||||||
805 | - | |||||||||||||
806 | int QCalendarTextNavigator::dateEditAcceptDelay() const | - | ||||||||||||
807 | { | - | ||||||||||||
808 | return m_editDelay; | - | ||||||||||||
809 | } | - | ||||||||||||
810 | - | |||||||||||||
811 | void QCalendarTextNavigator::setDateEditAcceptDelay(int delay) | - | ||||||||||||
812 | { | - | ||||||||||||
813 | m_editDelay = delay; | - | ||||||||||||
814 | } | - | ||||||||||||
815 | - | |||||||||||||
816 | class QCalendarView; | - | ||||||||||||
817 | - | |||||||||||||
818 | // a small helper class that replaces a QMap<Qt::DayOfWeek, T>, | - | ||||||||||||
819 | // but requires T to have a member-swap and a default constructor | - | ||||||||||||
820 | // which should be cheap (no memory allocations) | - | ||||||||||||
821 | - | |||||||||||||
822 | QT_WARNING_PUSH | - | ||||||||||||
823 | QT_WARNING_DISABLE_MSVC(4351) // "new behavior: elements of array ... will be default initialized" | - | ||||||||||||
824 | - | |||||||||||||
825 | template <typename T> | - | ||||||||||||
826 | class StaticDayOfWeekAssociativeArray { | - | ||||||||||||
827 | bool contained[7]; | - | ||||||||||||
828 | T data[7]; | - | ||||||||||||
829 | - | |||||||||||||
830 | static Q_DECL_CONSTEXPR int day2idx(Qt::DayOfWeek day) Q_DECL_NOTHROW { return int(day) - 1; } // alt: day % 7 | - | ||||||||||||
831 | public: | - | ||||||||||||
832 | Q_DECL_CONSTEXPR StaticDayOfWeekAssociativeArray() Q_DECL_NOEXCEPT_EXPR(noexcept(T())) | - | ||||||||||||
833 | : contained(), data() {} | - | ||||||||||||
834 | - | |||||||||||||
835 | Q_DECL_CONSTEXPR bool contains(Qt::DayOfWeek day) const Q_DECL_NOTHROW { return contained[day2idx(day)]; } | - | ||||||||||||
836 | Q_DECL_CONSTEXPR const T &value(Qt::DayOfWeek day) const Q_DECL_NOTHROW { return data[day2idx(day)]; } | - | ||||||||||||
837 | - | |||||||||||||
838 | Q_DECL_RELAXED_CONSTEXPR T &operator[](Qt::DayOfWeek day) Q_DECL_NOTHROW | - | ||||||||||||
839 | { | - | ||||||||||||
840 | const int idx = day2idx(day); | - | ||||||||||||
841 | contained[idx] = true; | - | ||||||||||||
842 | return data[idx]; never executed: return data[idx]; | 0 | ||||||||||||
843 | } | - | ||||||||||||
844 | - | |||||||||||||
845 | Q_DECL_RELAXED_CONSTEXPR void insert(Qt::DayOfWeek day, T v) Q_DECL_NOTHROW | - | ||||||||||||
846 | { | - | ||||||||||||
847 | operator[](day).swap(v); | - | ||||||||||||
848 | } never executed: end of block | 0 | ||||||||||||
849 | }; | - | ||||||||||||
850 | - | |||||||||||||
851 | QT_WARNING_POP | - | ||||||||||||
852 | - | |||||||||||||
853 | class QCalendarModel : public QAbstractTableModel | - | ||||||||||||
854 | { | - | ||||||||||||
855 | Q_OBJECT | - | ||||||||||||
856 | public: | - | ||||||||||||
857 | QCalendarModel(QObject *parent = 0); | - | ||||||||||||
858 | - | |||||||||||||
859 | int rowCount(const QModelIndex &) const Q_DECL_OVERRIDE | - | ||||||||||||
860 | { return RowCount + m_firstRow; } | - | ||||||||||||
861 | int columnCount(const QModelIndex &) const Q_DECL_OVERRIDE | - | ||||||||||||
862 | { return ColumnCount + m_firstColumn; } | - | ||||||||||||
863 | QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE; | - | ||||||||||||
864 | Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE; | - | ||||||||||||
865 | - | |||||||||||||
866 | bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) Q_DECL_OVERRIDE | - | ||||||||||||
867 | { | - | ||||||||||||
868 | beginInsertRows(parent, row, row + count - 1); | - | ||||||||||||
869 | endInsertRows(); | - | ||||||||||||
870 | return true; | - | ||||||||||||
871 | } | - | ||||||||||||
872 | bool insertColumns(int column, int count, const QModelIndex &parent = QModelIndex()) Q_DECL_OVERRIDE | - | ||||||||||||
873 | { | - | ||||||||||||
874 | beginInsertColumns(parent, column, column + count - 1); | - | ||||||||||||
875 | endInsertColumns(); | - | ||||||||||||
876 | return true; | - | ||||||||||||
877 | } | - | ||||||||||||
878 | bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) Q_DECL_OVERRIDE | - | ||||||||||||
879 | { | - | ||||||||||||
880 | beginRemoveRows(parent, row, row + count - 1); | - | ||||||||||||
881 | endRemoveRows(); | - | ||||||||||||
882 | return true; | - | ||||||||||||
883 | } | - | ||||||||||||
884 | bool removeColumns(int column, int count, const QModelIndex &parent = QModelIndex()) Q_DECL_OVERRIDE | - | ||||||||||||
885 | { | - | ||||||||||||
886 | beginRemoveColumns(parent, column, column + count - 1); | - | ||||||||||||
887 | endRemoveColumns(); | - | ||||||||||||
888 | return true; | - | ||||||||||||
889 | } | - | ||||||||||||
890 | - | |||||||||||||
891 | void showMonth(int year, int month); | - | ||||||||||||
892 | void setDate(const QDate &d); | - | ||||||||||||
893 | - | |||||||||||||
894 | void setMinimumDate(const QDate &date); | - | ||||||||||||
895 | void setMaximumDate(const QDate &date); | - | ||||||||||||
896 | - | |||||||||||||
897 | void setRange(const QDate &min, const QDate &max); | - | ||||||||||||
898 | - | |||||||||||||
899 | void setHorizontalHeaderFormat(QCalendarWidget::HorizontalHeaderFormat format); | - | ||||||||||||
900 | - | |||||||||||||
901 | void setFirstColumnDay(Qt::DayOfWeek dayOfWeek); | - | ||||||||||||
902 | Qt::DayOfWeek firstColumnDay() const; | - | ||||||||||||
903 | - | |||||||||||||
904 | bool weekNumbersShown() const; | - | ||||||||||||
905 | void setWeekNumbersShown(bool show); | - | ||||||||||||
906 | - | |||||||||||||
907 | QTextCharFormat formatForCell(int row, int col) const; | - | ||||||||||||
908 | Qt::DayOfWeek dayOfWeekForColumn(int section) const; | - | ||||||||||||
909 | int columnForDayOfWeek(Qt::DayOfWeek day) const; | - | ||||||||||||
910 | QDate dateForCell(int row, int column) const; | - | ||||||||||||
911 | void cellForDate(const QDate &date, int *row, int *column) const; | - | ||||||||||||
912 | QString dayName(Qt::DayOfWeek day) const; | - | ||||||||||||
913 | - | |||||||||||||
914 | void setView(QCalendarView *view) | - | ||||||||||||
915 | { m_view = view; } | - | ||||||||||||
916 | - | |||||||||||||
917 | void internalUpdate(); | - | ||||||||||||
918 | QDate referenceDate() const; | - | ||||||||||||
919 | int columnForFirstOfMonth(const QDate &date) const; | - | ||||||||||||
920 | - | |||||||||||||
921 | int m_firstColumn; | - | ||||||||||||
922 | int m_firstRow; | - | ||||||||||||
923 | QDate m_date; | - | ||||||||||||
924 | QDate m_minimumDate; | - | ||||||||||||
925 | QDate m_maximumDate; | - | ||||||||||||
926 | int m_shownYear; | - | ||||||||||||
927 | int m_shownMonth; | - | ||||||||||||
928 | Qt::DayOfWeek m_firstDay; | - | ||||||||||||
929 | QCalendarWidget::HorizontalHeaderFormat m_horizontalHeaderFormat; | - | ||||||||||||
930 | bool m_weekNumbersShown; | - | ||||||||||||
931 | QMapStaticDayOfWeekAssociativeArray<Qt::DayOfWeek,QTextCharFormat> m_dayFormats; | - | ||||||||||||
932 | QMap<QDate, QTextCharFormat> m_dateFormats; | - | ||||||||||||
933 | QTextCharFormat m_headerFormat; | - | ||||||||||||
934 | QCalendarView *m_view; | - | ||||||||||||
935 | }; | - | ||||||||||||
936 | - | |||||||||||||
937 | class QCalendarView : public QTableView | - | ||||||||||||
938 | { | - | ||||||||||||
939 | Q_OBJECT | - | ||||||||||||
940 | public: | - | ||||||||||||
941 | QCalendarView(QWidget *parent = 0); | - | ||||||||||||
942 | - | |||||||||||||
943 | void internalUpdate() { updateGeometries(); } | - | ||||||||||||
944 | void setReadOnly(bool enable); | - | ||||||||||||
945 | virtual void keyboardSearch(const QString & search) Q_DECL_OVERRIDE { Q_UNUSED(search) } | - | ||||||||||||
946 | - | |||||||||||||
947 | signals: | - | ||||||||||||
948 | void showDate(const QDate &date); | - | ||||||||||||
949 | void changeDate(const QDate &date, bool changeMonth); | - | ||||||||||||
950 | void clicked(const QDate &date); | - | ||||||||||||
951 | void editingFinished(); | - | ||||||||||||
952 | protected: | - | ||||||||||||
953 | QModelIndex moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers) Q_DECL_OVERRIDE; | - | ||||||||||||
954 | void mouseDoubleClickEvent(QMouseEvent *event) Q_DECL_OVERRIDE; | - | ||||||||||||
955 | void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE; | - | ||||||||||||
956 | void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE; | - | ||||||||||||
957 | void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE; | - | ||||||||||||
958 | #ifndef QT_NO_WHEELEVENT | - | ||||||||||||
959 | void wheelEvent(QWheelEvent *event) Q_DECL_OVERRIDE; | - | ||||||||||||
960 | #endif | - | ||||||||||||
961 | void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE; | - | ||||||||||||
962 | bool event(QEvent *event) Q_DECL_OVERRIDE; | - | ||||||||||||
963 | - | |||||||||||||
964 | QDate handleMouseEvent(QMouseEvent *event); | - | ||||||||||||
965 | public: | - | ||||||||||||
966 | bool readOnly; | - | ||||||||||||
967 | private: | - | ||||||||||||
968 | bool validDateClicked; | - | ||||||||||||
969 | #ifdef QT_KEYPAD_NAVIGATION | - | ||||||||||||
970 | QDate origDate; | - | ||||||||||||
971 | #endif | - | ||||||||||||
972 | }; | - | ||||||||||||
973 | - | |||||||||||||
974 | QCalendarModel::QCalendarModel(QObject *parent) | - | ||||||||||||
975 | : QAbstractTableModel(parent), | - | ||||||||||||
976 | m_firstColumn(1), | - | ||||||||||||
977 | m_firstRow(1), | - | ||||||||||||
978 | m_date(QDate::currentDate()), | - | ||||||||||||
979 | m_minimumDate(QDate::fromJulianDay(1)), | - | ||||||||||||
980 | m_maximumDate(7999, 12, 31), | - | ||||||||||||
981 | m_shownYear(m_date.year()), | - | ||||||||||||
982 | m_shownMonth(m_date.month()), | - | ||||||||||||
983 | m_firstDay(QLocale().firstDayOfWeek()), | - | ||||||||||||
984 | m_horizontalHeaderFormat(QCalendarWidget::ShortDayNames), | - | ||||||||||||
985 | m_weekNumbersShown(true), | - | ||||||||||||
986 | m_view(Q_NULLPTR) | - | ||||||||||||
987 | { | - | ||||||||||||
988 | } | - | ||||||||||||
989 | - | |||||||||||||
990 | Qt::DayOfWeek QCalendarModel::dayOfWeekForColumn(int column) const | - | ||||||||||||
991 | { | - | ||||||||||||
992 | int col = column - m_firstColumn; | - | ||||||||||||
993 | if (col < 0 || col > 6) | - | ||||||||||||
994 | return Qt::Sunday; | - | ||||||||||||
995 | int day = m_firstDay + col; | - | ||||||||||||
996 | if (day > 7) | - | ||||||||||||
997 | day -= 7; | - | ||||||||||||
998 | return Qt::DayOfWeek(day); | - | ||||||||||||
999 | } | - | ||||||||||||
1000 | - | |||||||||||||
1001 | int QCalendarModel::columnForDayOfWeek(Qt::DayOfWeek day) const | - | ||||||||||||
1002 | { | - | ||||||||||||
1003 | if (day < 1 || day > 7) | - | ||||||||||||
1004 | return -1; | - | ||||||||||||
1005 | int column = (int)day - (int)m_firstDay; | - | ||||||||||||
1006 | if (column < 0) | - | ||||||||||||
1007 | column += 7; | - | ||||||||||||
1008 | return column + m_firstColumn; | - | ||||||||||||
1009 | } | - | ||||||||||||
1010 | - | |||||||||||||
1011 | /* | - | ||||||||||||
1012 | This simple algorithm tries to generate a valid date from the month shown. | - | ||||||||||||
1013 | Some months don't contain a first day (e.g. Jan of -4713 year, | - | ||||||||||||
1014 | so QDate (-4713, 1, 1) would be invalid). In that case we try to generate | - | ||||||||||||
1015 | another valid date for that month. Later, returned date's day is the number of cells | - | ||||||||||||
1016 | calendar widget will reserve for days before referenceDate. (E.g. if returned date's | - | ||||||||||||
1017 | day is 16, that day will be placed in 3rd or 4th row, not in the 1st or 2nd row). | - | ||||||||||||
1018 | Depending on referenceData we can change behaviour of Oct 1582. If referenceDate is 1st | - | ||||||||||||
1019 | of Oct we render 1 Oct in 1st or 2nd row. If referenceDate is 17 of Oct we show always 16 | - | ||||||||||||
1020 | dates before 17 of Oct, and since this month contains the hole 5-14 Oct, the first of Oct | - | ||||||||||||
1021 | will be rendered in 2nd or 3rd row, showing more dates from previous month. | - | ||||||||||||
1022 | */ | - | ||||||||||||
1023 | QDate QCalendarModel::referenceDate() const | - | ||||||||||||
1024 | { | - | ||||||||||||
1025 | int refDay = 1; | - | ||||||||||||
1026 | while (refDay <= 31) { | - | ||||||||||||
1027 | QDate refDate(m_shownYear, m_shownMonth, refDay); | - | ||||||||||||
1028 | if (refDate.isValid()) | - | ||||||||||||
1029 | return refDate; | - | ||||||||||||
1030 | refDay += 1; | - | ||||||||||||
1031 | } | - | ||||||||||||
1032 | return QDate(); | - | ||||||||||||
1033 | } | - | ||||||||||||
1034 | - | |||||||||||||
1035 | int QCalendarModel::columnForFirstOfMonth(const QDate &date) const | - | ||||||||||||
1036 | { | - | ||||||||||||
1037 | return (columnForDayOfWeek(static_cast<Qt::DayOfWeek>(date.dayOfWeek())) - (date.day() % 7) + 8) % 7; | - | ||||||||||||
1038 | } | - | ||||||||||||
1039 | - | |||||||||||||
1040 | QDate QCalendarModel::dateForCell(int row, int column) const | - | ||||||||||||
1041 | { | - | ||||||||||||
1042 | if (row < m_firstRow || row > m_firstRow + RowCount - 1 || | - | ||||||||||||
1043 | column < m_firstColumn || column > m_firstColumn + ColumnCount - 1) | - | ||||||||||||
1044 | return QDate(); | - | ||||||||||||
1045 | const QDate refDate = referenceDate(); | - | ||||||||||||
1046 | if (!refDate.isValid()) | - | ||||||||||||
1047 | return QDate(); | - | ||||||||||||
1048 | - | |||||||||||||
1049 | const int columnForFirstOfShownMonth = columnForFirstOfMonth(refDate); | - | ||||||||||||
1050 | if (columnForFirstOfShownMonth - m_firstColumn < MinimumDayOffset) | - | ||||||||||||
1051 | row -= 1; | - | ||||||||||||
1052 | - | |||||||||||||
1053 | const int requestedDay = 7 * (row - m_firstRow) + column - columnForFirstOfShownMonth - refDate.day() + 1; | - | ||||||||||||
1054 | return refDate.addDays(requestedDay); | - | ||||||||||||
1055 | } | - | ||||||||||||
1056 | - | |||||||||||||
1057 | void QCalendarModel::cellForDate(const QDate &date, int *row, int *column) const | - | ||||||||||||
1058 | { | - | ||||||||||||
1059 | if (!row && !column) | - | ||||||||||||
1060 | return; | - | ||||||||||||
1061 | - | |||||||||||||
1062 | if (row) | - | ||||||||||||
1063 | *row = -1; | - | ||||||||||||
1064 | if (column) | - | ||||||||||||
1065 | *column = -1; | - | ||||||||||||
1066 | - | |||||||||||||
1067 | const QDate refDate = referenceDate(); | - | ||||||||||||
1068 | if (!refDate.isValid()) | - | ||||||||||||
1069 | return; | - | ||||||||||||
1070 | - | |||||||||||||
1071 | const int columnForFirstOfShownMonth = columnForFirstOfMonth(refDate); | - | ||||||||||||
1072 | const int requestedPosition = refDate.daysTo(date) - m_firstColumn + columnForFirstOfShownMonth + refDate.day() - 1; | - | ||||||||||||
1073 | - | |||||||||||||
1074 | int c = requestedPosition % 7; | - | ||||||||||||
1075 | int r = requestedPosition / 7; | - | ||||||||||||
1076 | if (c < 0) { | - | ||||||||||||
1077 | c += 7; | - | ||||||||||||
1078 | r -= 1; | - | ||||||||||||
1079 | } | - | ||||||||||||
1080 | - | |||||||||||||
1081 | if (columnForFirstOfShownMonth - m_firstColumn < MinimumDayOffset) | - | ||||||||||||
1082 | r += 1; | - | ||||||||||||
1083 | - | |||||||||||||
1084 | if (r < 0 || r > RowCount - 1 || c < 0 || c > ColumnCount - 1) | - | ||||||||||||
1085 | return; | - | ||||||||||||
1086 | - | |||||||||||||
1087 | if (row) | - | ||||||||||||
1088 | *row = r + m_firstRow; | - | ||||||||||||
1089 | if (column) | - | ||||||||||||
1090 | *column = c + m_firstColumn; | - | ||||||||||||
1091 | } | - | ||||||||||||
1092 | - | |||||||||||||
1093 | QString QCalendarModel::dayName(Qt::DayOfWeek day) const | - | ||||||||||||
1094 | { | - | ||||||||||||
1095 | switch (m_horizontalHeaderFormat) { | - | ||||||||||||
1096 | case QCalendarWidget::SingleLetterDayNames: { | - | ||||||||||||
1097 | QString standaloneDayName = m_view->locale().standaloneDayName(day, QLocale::NarrowFormat); | - | ||||||||||||
1098 | if (standaloneDayName == m_view->locale().dayName(day, QLocale::NarrowFormat)) | - | ||||||||||||
1099 | return standaloneDayName.left(1); | - | ||||||||||||
1100 | return standaloneDayName; | - | ||||||||||||
1101 | } | - | ||||||||||||
1102 | case QCalendarWidget::ShortDayNames: | - | ||||||||||||
1103 | return m_view->locale().dayName(day, QLocale::ShortFormat); | - | ||||||||||||
1104 | case QCalendarWidget::LongDayNames: | - | ||||||||||||
1105 | return m_view->locale().dayName(day, QLocale::LongFormat); | - | ||||||||||||
1106 | default: | - | ||||||||||||
1107 | break; | - | ||||||||||||
1108 | } | - | ||||||||||||
1109 | return QString(); | - | ||||||||||||
1110 | } | - | ||||||||||||
1111 | - | |||||||||||||
1112 | QTextCharFormat QCalendarModel::formatForCell(int row, int col) const | - | ||||||||||||
1113 | { | - | ||||||||||||
1114 | QPalette pal; | - | ||||||||||||
1115 | QPalette::ColorGroup cg = QPalette::Active; | - | ||||||||||||
1116 | if (m_view) { | - | ||||||||||||
1117 | pal = m_view->palette(); | - | ||||||||||||
1118 | if (!m_view->isEnabled()) | - | ||||||||||||
1119 | cg = QPalette::Disabled; | - | ||||||||||||
1120 | else if (!m_view->isActiveWindow()) | - | ||||||||||||
1121 | cg = QPalette::Inactive; | - | ||||||||||||
1122 | } | - | ||||||||||||
1123 | - | |||||||||||||
1124 | QTextCharFormat format; | - | ||||||||||||
1125 | format.setFont(m_view->font()); | - | ||||||||||||
1126 | bool header = (m_weekNumbersShown && col == HeaderColumn) | - | ||||||||||||
1127 | || (m_horizontalHeaderFormat != QCalendarWidget::NoHorizontalHeader && row == HeaderRow); | - | ||||||||||||
1128 | format.setBackground(pal.brush(cg, header ? QPalette::AlternateBase : QPalette::Base)); | - | ||||||||||||
1129 | format.setForeground(pal.brush(cg, QPalette::Text)); | - | ||||||||||||
1130 | if (header) { | - | ||||||||||||
1131 | format.merge(m_headerFormat); | - | ||||||||||||
1132 | } | - | ||||||||||||
1133 | - | |||||||||||||
1134 | if (col >= m_firstColumn && col < m_firstColumn + ColumnCount) { | - | ||||||||||||
1135 | Qt::DayOfWeek dayOfWeek = dayOfWeekForColumn(col); | - | ||||||||||||
1136 | if (m_dayFormats.contains(dayOfWeek)) | - | ||||||||||||
1137 | format.merge(m_dayFormats.value(dayOfWeek)); | - | ||||||||||||
1138 | } | - | ||||||||||||
1139 | - | |||||||||||||
1140 | if (!header) { | - | ||||||||||||
1141 | QDate date = dateForCell(row, col); | - | ||||||||||||
1142 | format.merge(m_dateFormats.value(date)); | - | ||||||||||||
1143 | if(date < m_minimumDate || date > m_maximumDate) | - | ||||||||||||
1144 | format.setBackground(pal.brush(cg, QPalette::Window)); | - | ||||||||||||
1145 | if (m_shownMonth != date.month()) | - | ||||||||||||
1146 | format.setForeground(pal.brush(QPalette::Disabled, QPalette::Text)); | - | ||||||||||||
1147 | } | - | ||||||||||||
1148 | return format; | - | ||||||||||||
1149 | } | - | ||||||||||||
1150 | - | |||||||||||||
1151 | QVariant QCalendarModel::data(const QModelIndex &index, int role) const | - | ||||||||||||
1152 | { | - | ||||||||||||
1153 | if (role == Qt::TextAlignmentRole) | - | ||||||||||||
1154 | return (int) Qt::AlignCenter; | - | ||||||||||||
1155 | - | |||||||||||||
1156 | int row = index.row(); | - | ||||||||||||
1157 | int column = index.column(); | - | ||||||||||||
1158 | - | |||||||||||||
1159 | if(role == Qt::DisplayRole) { | - | ||||||||||||
1160 | if (m_weekNumbersShown && column == HeaderColumn | - | ||||||||||||
1161 | && row >= m_firstRow && row < m_firstRow + RowCount) { | - | ||||||||||||
1162 | QDate date = dateForCell(row, columnForDayOfWeek(Qt::Monday)); | - | ||||||||||||
1163 | if (date.isValid()) | - | ||||||||||||
1164 | return date.weekNumber(); | - | ||||||||||||
1165 | } | - | ||||||||||||
1166 | if (m_horizontalHeaderFormat != QCalendarWidget::NoHorizontalHeader && row == HeaderRow | - | ||||||||||||
1167 | && column >= m_firstColumn && column < m_firstColumn + ColumnCount) | - | ||||||||||||
1168 | return dayName(dayOfWeekForColumn(column)); | - | ||||||||||||
1169 | QDate date = dateForCell(row, column); | - | ||||||||||||
1170 | if (date.isValid()) | - | ||||||||||||
1171 | return date.day(); | - | ||||||||||||
1172 | return QString(); | - | ||||||||||||
1173 | } | - | ||||||||||||
1174 | - | |||||||||||||
1175 | QTextCharFormat fmt = formatForCell(row, column); | - | ||||||||||||
1176 | if (role == Qt::BackgroundColorRole) | - | ||||||||||||
1177 | return fmt.background().color(); | - | ||||||||||||
1178 | if (role == Qt::TextColorRole) | - | ||||||||||||
1179 | return fmt.foreground().color(); | - | ||||||||||||
1180 | if (role == Qt::FontRole) | - | ||||||||||||
1181 | return fmt.font(); | - | ||||||||||||
1182 | if (role == Qt::ToolTipRole) | - | ||||||||||||
1183 | return fmt.toolTip(); | - | ||||||||||||
1184 | return QVariant(); | - | ||||||||||||
1185 | } | - | ||||||||||||
1186 | - | |||||||||||||
1187 | Qt::ItemFlags QCalendarModel::flags(const QModelIndex &index) const | - | ||||||||||||
1188 | { | - | ||||||||||||
1189 | QDate date = dateForCell(index.row(), index.column()); | - | ||||||||||||
1190 | if (!date.isValid()) | - | ||||||||||||
1191 | return QAbstractTableModel::flags(index); | - | ||||||||||||
1192 | if (date < m_minimumDate) | - | ||||||||||||
1193 | return 0; | - | ||||||||||||
1194 | if (date > m_maximumDate) | - | ||||||||||||
1195 | return 0; | - | ||||||||||||
1196 | return QAbstractTableModel::flags(index); | - | ||||||||||||
1197 | } | - | ||||||||||||
1198 | - | |||||||||||||
1199 | void QCalendarModel::setDate(const QDate &d) | - | ||||||||||||
1200 | { | - | ||||||||||||
1201 | m_date = d; | - | ||||||||||||
1202 | if (m_date < m_minimumDate) | - | ||||||||||||
1203 | m_date = m_minimumDate; | - | ||||||||||||
1204 | else if (m_date > m_maximumDate) | - | ||||||||||||
1205 | m_date = m_maximumDate; | - | ||||||||||||
1206 | } | - | ||||||||||||
1207 | - | |||||||||||||
1208 | void QCalendarModel::showMonth(int year, int month) | - | ||||||||||||
1209 | { | - | ||||||||||||
1210 | if (m_shownYear == year && m_shownMonth == month) | - | ||||||||||||
1211 | return; | - | ||||||||||||
1212 | - | |||||||||||||
1213 | m_shownYear = year; | - | ||||||||||||
1214 | m_shownMonth = month; | - | ||||||||||||
1215 | - | |||||||||||||
1216 | internalUpdate(); | - | ||||||||||||
1217 | } | - | ||||||||||||
1218 | - | |||||||||||||
1219 | void QCalendarModel::setMinimumDate(const QDate &d) | - | ||||||||||||
1220 | { | - | ||||||||||||
1221 | if (!d.isValid() || d == m_minimumDate) | - | ||||||||||||
1222 | return; | - | ||||||||||||
1223 | - | |||||||||||||
1224 | m_minimumDate = d; | - | ||||||||||||
1225 | if (m_maximumDate < m_minimumDate) | - | ||||||||||||
1226 | m_maximumDate = m_minimumDate; | - | ||||||||||||
1227 | if (m_date < m_minimumDate) | - | ||||||||||||
1228 | m_date = m_minimumDate; | - | ||||||||||||
1229 | internalUpdate(); | - | ||||||||||||
1230 | } | - | ||||||||||||
1231 | - | |||||||||||||
1232 | void QCalendarModel::setMaximumDate(const QDate &d) | - | ||||||||||||
1233 | { | - | ||||||||||||
1234 | if (!d.isValid() || d == m_maximumDate) | - | ||||||||||||
1235 | return; | - | ||||||||||||
1236 | - | |||||||||||||
1237 | m_maximumDate = d; | - | ||||||||||||
1238 | if (m_minimumDate > m_maximumDate) | - | ||||||||||||
1239 | m_minimumDate = m_maximumDate; | - | ||||||||||||
1240 | if (m_date > m_maximumDate) | - | ||||||||||||
1241 | m_date = m_maximumDate; | - | ||||||||||||
1242 | internalUpdate(); | - | ||||||||||||
1243 | } | - | ||||||||||||
1244 | - | |||||||||||||
1245 | void QCalendarModel::setRange(const QDate &min, const QDate &max) | - | ||||||||||||
1246 | { | - | ||||||||||||
1247 | m_minimumDate = min; | - | ||||||||||||
1248 | m_maximumDate = max; | - | ||||||||||||
1249 | if (m_minimumDate > m_maximumDate) | - | ||||||||||||
1250 | qSwap(m_minimumDate, m_maximumDate); | - | ||||||||||||
1251 | if (m_date < m_minimumDate) | - | ||||||||||||
1252 | m_date = m_minimumDate; | - | ||||||||||||
1253 | if (m_date > m_maximumDate) | - | ||||||||||||
1254 | m_date = m_maximumDate; | - | ||||||||||||
1255 | internalUpdate(); | - | ||||||||||||
1256 | } | - | ||||||||||||
1257 | - | |||||||||||||
1258 | void QCalendarModel::internalUpdate() | - | ||||||||||||
1259 | { | - | ||||||||||||
1260 | QModelIndex begin = index(0, 0); | - | ||||||||||||
1261 | QModelIndex end = index(m_firstRow + RowCount - 1, m_firstColumn + ColumnCount - 1); | - | ||||||||||||
1262 | emit dataChanged(begin, end); | - | ||||||||||||
1263 | emit headerDataChanged(Qt::Vertical, 0, m_firstRow + RowCount - 1); | - | ||||||||||||
1264 | emit headerDataChanged(Qt::Horizontal, 0, m_firstColumn + ColumnCount - 1); | - | ||||||||||||
1265 | } | - | ||||||||||||
1266 | - | |||||||||||||
1267 | void QCalendarModel::setHorizontalHeaderFormat(QCalendarWidget::HorizontalHeaderFormat format) | - | ||||||||||||
1268 | { | - | ||||||||||||
1269 | if (m_horizontalHeaderFormat == format) | - | ||||||||||||
1270 | return; | - | ||||||||||||
1271 | - | |||||||||||||
1272 | int oldFormat = m_horizontalHeaderFormat; | - | ||||||||||||
1273 | m_horizontalHeaderFormat = format; | - | ||||||||||||
1274 | if (oldFormat == QCalendarWidget::NoHorizontalHeader) { | - | ||||||||||||
1275 | m_firstRow = 1; | - | ||||||||||||
1276 | insertRow(0); | - | ||||||||||||
1277 | } else if (m_horizontalHeaderFormat == QCalendarWidget::NoHorizontalHeader) { | - | ||||||||||||
1278 | m_firstRow = 0; | - | ||||||||||||
1279 | removeRow(0); | - | ||||||||||||
1280 | } | - | ||||||||||||
1281 | internalUpdate(); | - | ||||||||||||
1282 | } | - | ||||||||||||
1283 | - | |||||||||||||
1284 | void QCalendarModel::setFirstColumnDay(Qt::DayOfWeek dayOfWeek) | - | ||||||||||||
1285 | { | - | ||||||||||||
1286 | if (m_firstDay == dayOfWeek) | - | ||||||||||||
1287 | return; | - | ||||||||||||
1288 | - | |||||||||||||
1289 | m_firstDay = dayOfWeek; | - | ||||||||||||
1290 | internalUpdate(); | - | ||||||||||||
1291 | } | - | ||||||||||||
1292 | - | |||||||||||||
1293 | Qt::DayOfWeek QCalendarModel::firstColumnDay() const | - | ||||||||||||
1294 | { | - | ||||||||||||
1295 | return m_firstDay; | - | ||||||||||||
1296 | } | - | ||||||||||||
1297 | - | |||||||||||||
1298 | bool QCalendarModel::weekNumbersShown() const | - | ||||||||||||
1299 | { | - | ||||||||||||
1300 | return m_weekNumbersShown; | - | ||||||||||||
1301 | } | - | ||||||||||||
1302 | - | |||||||||||||
1303 | void QCalendarModel::setWeekNumbersShown(bool show) | - | ||||||||||||
1304 | { | - | ||||||||||||
1305 | if (m_weekNumbersShown == show) | - | ||||||||||||
1306 | return; | - | ||||||||||||
1307 | - | |||||||||||||
1308 | m_weekNumbersShown = show; | - | ||||||||||||
1309 | if (show) { | - | ||||||||||||
1310 | m_firstColumn = 1; | - | ||||||||||||
1311 | insertColumn(0); | - | ||||||||||||
1312 | } else { | - | ||||||||||||
1313 | m_firstColumn = 0; | - | ||||||||||||
1314 | removeColumn(0); | - | ||||||||||||
1315 | } | - | ||||||||||||
1316 | internalUpdate(); | - | ||||||||||||
1317 | } | - | ||||||||||||
1318 | - | |||||||||||||
1319 | QCalendarView::QCalendarView(QWidget *parent) | - | ||||||||||||
1320 | : QTableView(parent), | - | ||||||||||||
1321 | readOnly(false), | - | ||||||||||||
1322 | validDateClicked(false) | - | ||||||||||||
1323 | { | - | ||||||||||||
1324 | setTabKeyNavigation(false); | - | ||||||||||||
1325 | setShowGrid(false); | - | ||||||||||||
1326 | verticalHeader()->setVisible(false); | - | ||||||||||||
1327 | horizontalHeader()->setVisible(false); | - | ||||||||||||
1328 | setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | - | ||||||||||||
1329 | setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); | - | ||||||||||||
1330 | } | - | ||||||||||||
1331 | - | |||||||||||||
1332 | QModelIndex QCalendarView::moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers) | - | ||||||||||||
1333 | { | - | ||||||||||||
1334 | QCalendarModel *calendarModel = qobject_cast<QCalendarModel *>(model()); | - | ||||||||||||
1335 | if (!calendarModel) | - | ||||||||||||
1336 | return QTableView::moveCursor(cursorAction, modifiers); | - | ||||||||||||
1337 | - | |||||||||||||
1338 | if (readOnly) | - | ||||||||||||
1339 | return currentIndex(); | - | ||||||||||||
1340 | - | |||||||||||||
1341 | QModelIndex index = currentIndex(); | - | ||||||||||||
1342 | QDate currentDate = static_cast<QCalendarModel*>(model())->dateForCell(index.row(), index.column()); | - | ||||||||||||
1343 | switch (cursorAction) { | - | ||||||||||||
1344 | case QAbstractItemView::MoveUp: | - | ||||||||||||
1345 | currentDate = currentDate.addDays(-7); | - | ||||||||||||
1346 | break; | - | ||||||||||||
1347 | case QAbstractItemView::MoveDown: | - | ||||||||||||
1348 | currentDate = currentDate.addDays(7); | - | ||||||||||||
1349 | break; | - | ||||||||||||
1350 | case QAbstractItemView::MoveLeft: | - | ||||||||||||
1351 | currentDate = currentDate.addDays(isRightToLeft() ? 1 : -1); | - | ||||||||||||
1352 | break; | - | ||||||||||||
1353 | case QAbstractItemView::MoveRight: | - | ||||||||||||
1354 | currentDate = currentDate.addDays(isRightToLeft() ? -1 : 1); | - | ||||||||||||
1355 | break; | - | ||||||||||||
1356 | case QAbstractItemView::MoveHome: | - | ||||||||||||
1357 | currentDate = QDate(currentDate.year(), currentDate.month(), 1); | - | ||||||||||||
1358 | break; | - | ||||||||||||
1359 | case QAbstractItemView::MoveEnd: | - | ||||||||||||
1360 | currentDate = QDate(currentDate.year(), currentDate.month(), currentDate.daysInMonth()); | - | ||||||||||||
1361 | break; | - | ||||||||||||
1362 | case QAbstractItemView::MovePageUp: | - | ||||||||||||
1363 | currentDate = currentDate.addMonths(-1); | - | ||||||||||||
1364 | break; | - | ||||||||||||
1365 | case QAbstractItemView::MovePageDown: | - | ||||||||||||
1366 | currentDate = currentDate.addMonths(1); | - | ||||||||||||
1367 | break; | - | ||||||||||||
1368 | case QAbstractItemView::MoveNext: | - | ||||||||||||
1369 | case QAbstractItemView::MovePrevious: | - | ||||||||||||
1370 | return currentIndex(); | - | ||||||||||||
1371 | default: | - | ||||||||||||
1372 | break; | - | ||||||||||||
1373 | } | - | ||||||||||||
1374 | emit changeDate(currentDate, true); | - | ||||||||||||
1375 | return currentIndex(); | - | ||||||||||||
1376 | } | - | ||||||||||||
1377 | - | |||||||||||||
1378 | void QCalendarView::keyPressEvent(QKeyEvent *event) | - | ||||||||||||
1379 | { | - | ||||||||||||
1380 | #ifdef QT_KEYPAD_NAVIGATION | - | ||||||||||||
1381 | if (event->key() == Qt::Key_Select) { | - | ||||||||||||
1382 | if (QApplication::keypadNavigationEnabled()) { | - | ||||||||||||
1383 | if (!hasEditFocus()) { | - | ||||||||||||
1384 | setEditFocus(true); | - | ||||||||||||
1385 | return; | - | ||||||||||||
1386 | } | - | ||||||||||||
1387 | } | - | ||||||||||||
1388 | } else if (event->key() == Qt::Key_Back) { | - | ||||||||||||
1389 | if (QApplication::keypadNavigationEnabled() && hasEditFocus()) { | - | ||||||||||||
1390 | if (qobject_cast<QCalendarModel *>(model())) { | - | ||||||||||||
1391 | emit changeDate(origDate, true); //changes selection back to origDate, but doesn't activate | - | ||||||||||||
1392 | setEditFocus(false); | - | ||||||||||||
1393 | return; | - | ||||||||||||
1394 | } | - | ||||||||||||
1395 | } | - | ||||||||||||
1396 | } | - | ||||||||||||
1397 | #endif | - | ||||||||||||
1398 | - | |||||||||||||
1399 | if (!readOnly) { | - | ||||||||||||
1400 | switch (event->key()) { | - | ||||||||||||
1401 | case Qt::Key_Return: | - | ||||||||||||
1402 | case Qt::Key_Enter: | - | ||||||||||||
1403 | case Qt::Key_Select: | - | ||||||||||||
1404 | emit editingFinished(); | - | ||||||||||||
1405 | return; | - | ||||||||||||
1406 | default: | - | ||||||||||||
1407 | break; | - | ||||||||||||
1408 | } | - | ||||||||||||
1409 | } | - | ||||||||||||
1410 | QTableView::keyPressEvent(event); | - | ||||||||||||
1411 | } | - | ||||||||||||
1412 | - | |||||||||||||
1413 | #ifndef QT_NO_WHEELEVENT | - | ||||||||||||
1414 | void QCalendarView::wheelEvent(QWheelEvent *event) | - | ||||||||||||
1415 | { | - | ||||||||||||
1416 | const int numDegrees = event->delta() / 8; | - | ||||||||||||
1417 | const int numSteps = numDegrees / 15; | - | ||||||||||||
1418 | const QModelIndex index = currentIndex(); | - | ||||||||||||
1419 | QDate currentDate = static_cast<QCalendarModel*>(model())->dateForCell(index.row(), index.column()); | - | ||||||||||||
1420 | currentDate = currentDate.addMonths(-numSteps); | - | ||||||||||||
1421 | emit showDate(currentDate); | - | ||||||||||||
1422 | } | - | ||||||||||||
1423 | #endif | - | ||||||||||||
1424 | - | |||||||||||||
1425 | bool QCalendarView::event(QEvent *event) | - | ||||||||||||
1426 | { | - | ||||||||||||
1427 | #ifdef QT_KEYPAD_NAVIGATION | - | ||||||||||||
1428 | if (event->type() == QEvent::FocusIn) { | - | ||||||||||||
1429 | if (QCalendarModel *calendarModel = qobject_cast<QCalendarModel *>(model())) { | - | ||||||||||||
1430 | origDate = calendarModel->m_date; | - | ||||||||||||
1431 | } | - | ||||||||||||
1432 | } | - | ||||||||||||
1433 | #endif | - | ||||||||||||
1434 | - | |||||||||||||
1435 | return QTableView::event(event); | - | ||||||||||||
1436 | } | - | ||||||||||||
1437 | - | |||||||||||||
1438 | QDate QCalendarView::handleMouseEvent(QMouseEvent *event) | - | ||||||||||||
1439 | { | - | ||||||||||||
1440 | QCalendarModel *calendarModel = qobject_cast<QCalendarModel *>(model()); | - | ||||||||||||
1441 | if (!calendarModel) | - | ||||||||||||
1442 | return QDate(); | - | ||||||||||||
1443 | - | |||||||||||||
1444 | QPoint pos = event->pos(); | - | ||||||||||||
1445 | QModelIndex index = indexAt(pos); | - | ||||||||||||
1446 | QDate date = calendarModel->dateForCell(index.row(), index.column()); | - | ||||||||||||
1447 | if (date.isValid() && date >= calendarModel->m_minimumDate | - | ||||||||||||
1448 | && date <= calendarModel->m_maximumDate) { | - | ||||||||||||
1449 | return date; | - | ||||||||||||
1450 | } | - | ||||||||||||
1451 | return QDate(); | - | ||||||||||||
1452 | } | - | ||||||||||||
1453 | - | |||||||||||||
1454 | void QCalendarView::mouseDoubleClickEvent(QMouseEvent *event) | - | ||||||||||||
1455 | { | - | ||||||||||||
1456 | QCalendarModel *calendarModel = qobject_cast<QCalendarModel *>(model()); | - | ||||||||||||
1457 | if (!calendarModel) { | - | ||||||||||||
1458 | QTableView::mouseDoubleClickEvent(event); | - | ||||||||||||
1459 | return; | - | ||||||||||||
1460 | } | - | ||||||||||||
1461 | - | |||||||||||||
1462 | if (readOnly) | - | ||||||||||||
1463 | return; | - | ||||||||||||
1464 | - | |||||||||||||
1465 | QDate date = handleMouseEvent(event); | - | ||||||||||||
1466 | validDateClicked = false; | - | ||||||||||||
1467 | if (date == calendarModel->m_date && !style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick)) { | - | ||||||||||||
1468 | emit editingFinished(); | - | ||||||||||||
1469 | } | - | ||||||||||||
1470 | } | - | ||||||||||||
1471 | - | |||||||||||||
1472 | void QCalendarView::mousePressEvent(QMouseEvent *event) | - | ||||||||||||
1473 | { | - | ||||||||||||
1474 | QCalendarModel *calendarModel = qobject_cast<QCalendarModel *>(model()); | - | ||||||||||||
1475 | if (!calendarModel) { | - | ||||||||||||
1476 | QTableView::mousePressEvent(event); | - | ||||||||||||
1477 | return; | - | ||||||||||||
1478 | } | - | ||||||||||||
1479 | - | |||||||||||||
1480 | if (readOnly) | - | ||||||||||||
1481 | return; | - | ||||||||||||
1482 | - | |||||||||||||
1483 | if (event->button() != Qt::LeftButton) | - | ||||||||||||
1484 | return; | - | ||||||||||||
1485 | - | |||||||||||||
1486 | QDate date = handleMouseEvent(event); | - | ||||||||||||
1487 | if (date.isValid()) { | - | ||||||||||||
1488 | validDateClicked = true; | - | ||||||||||||
1489 | int row = -1, col = -1; | - | ||||||||||||
1490 | static_cast<QCalendarModel *>(model())->cellForDate(date, &row, &col); | - | ||||||||||||
1491 | if (row != -1 && col != -1) { | - | ||||||||||||
1492 | selectionModel()->setCurrentIndex(model()->index(row, col), QItemSelectionModel::NoUpdate); | - | ||||||||||||
1493 | } | - | ||||||||||||
1494 | } else { | - | ||||||||||||
1495 | validDateClicked = false; | - | ||||||||||||
1496 | event->ignore(); | - | ||||||||||||
1497 | } | - | ||||||||||||
1498 | } | - | ||||||||||||
1499 | - | |||||||||||||
1500 | void QCalendarView::mouseMoveEvent(QMouseEvent *event) | - | ||||||||||||
1501 | { | - | ||||||||||||
1502 | QCalendarModel *calendarModel = qobject_cast<QCalendarModel *>(model()); | - | ||||||||||||
1503 | if (!calendarModel) { | - | ||||||||||||
1504 | QTableView::mouseMoveEvent(event); | - | ||||||||||||
1505 | return; | - | ||||||||||||
1506 | } | - | ||||||||||||
1507 | - | |||||||||||||
1508 | if (readOnly) | - | ||||||||||||
1509 | return; | - | ||||||||||||
1510 | - | |||||||||||||
1511 | if (validDateClicked) { | - | ||||||||||||
1512 | QDate date = handleMouseEvent(event); | - | ||||||||||||
1513 | if (date.isValid()) { | - | ||||||||||||
1514 | int row = -1, col = -1; | - | ||||||||||||
1515 | static_cast<QCalendarModel *>(model())->cellForDate(date, &row, &col); | - | ||||||||||||
1516 | if (row != -1 && col != -1) { | - | ||||||||||||
1517 | selectionModel()->setCurrentIndex(model()->index(row, col), QItemSelectionModel::NoUpdate); | - | ||||||||||||
1518 | } | - | ||||||||||||
1519 | } | - | ||||||||||||
1520 | } else { | - | ||||||||||||
1521 | event->ignore(); | - | ||||||||||||
1522 | } | - | ||||||||||||
1523 | } | - | ||||||||||||
1524 | - | |||||||||||||
1525 | void QCalendarView::mouseReleaseEvent(QMouseEvent *event) | - | ||||||||||||
1526 | { | - | ||||||||||||
1527 | QCalendarModel *calendarModel = qobject_cast<QCalendarModel *>(model()); | - | ||||||||||||
1528 | if (!calendarModel) { | - | ||||||||||||
1529 | QTableView::mouseReleaseEvent(event); | - | ||||||||||||
1530 | return; | - | ||||||||||||
1531 | } | - | ||||||||||||
1532 | - | |||||||||||||
1533 | if (event->button() != Qt::LeftButton) | - | ||||||||||||
1534 | return; | - | ||||||||||||
1535 | - | |||||||||||||
1536 | if (readOnly) | - | ||||||||||||
1537 | return; | - | ||||||||||||
1538 | - | |||||||||||||
1539 | if (validDateClicked) { | - | ||||||||||||
1540 | QDate date = handleMouseEvent(event); | - | ||||||||||||
1541 | if (date.isValid()) { | - | ||||||||||||
1542 | emit changeDate(date, true); | - | ||||||||||||
1543 | emit clicked(date); | - | ||||||||||||
1544 | if (style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick)) | - | ||||||||||||
1545 | emit editingFinished(); | - | ||||||||||||
1546 | } | - | ||||||||||||
1547 | validDateClicked = false; | - | ||||||||||||
1548 | } else { | - | ||||||||||||
1549 | event->ignore(); | - | ||||||||||||
1550 | } | - | ||||||||||||
1551 | } | - | ||||||||||||
1552 | - | |||||||||||||
1553 | class QCalendarDelegate : public QItemDelegate | - | ||||||||||||
1554 | { | - | ||||||||||||
1555 | Q_OBJECT | - | ||||||||||||
1556 | public: | - | ||||||||||||
1557 | QCalendarDelegate(QCalendarWidgetPrivate *w, QObject *parent = 0) | - | ||||||||||||
1558 | : QItemDelegate(parent), calendarWidgetPrivate(w) | - | ||||||||||||
1559 | { } | - | ||||||||||||
1560 | virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, | - | ||||||||||||
1561 | const QModelIndex &index) const Q_DECL_OVERRIDE; | - | ||||||||||||
1562 | void paintCell(QPainter *painter, const QRect &rect, const QDate &date) const; | - | ||||||||||||
1563 | - | |||||||||||||
1564 | private: | - | ||||||||||||
1565 | QCalendarWidgetPrivate *calendarWidgetPrivate; | - | ||||||||||||
1566 | mutable QStyleOptionViewItem storedOption; | - | ||||||||||||
1567 | }; | - | ||||||||||||
1568 | - | |||||||||||||
1569 | //Private tool button class | - | ||||||||||||
1570 | class QCalToolButton: public QToolButton | - | ||||||||||||
1571 | { | - | ||||||||||||
1572 | public: | - | ||||||||||||
1573 | QCalToolButton(QWidget * parent) | - | ||||||||||||
1574 | : QToolButton(parent) | - | ||||||||||||
1575 | { } | - | ||||||||||||
1576 | protected: | - | ||||||||||||
1577 | void paintEvent(QPaintEvent *e) Q_DECL_OVERRIDE | - | ||||||||||||
1578 | { | - | ||||||||||||
1579 | Q_UNUSED(e) | - | ||||||||||||
1580 | - | |||||||||||||
1581 | #ifndef Q_DEAD_CODE_FROM_QT4_MAC | - | ||||||||||||
1582 | QStyleOptionToolButton opt; | - | ||||||||||||
1583 | initStyleOption(&opt); | - | ||||||||||||
1584 | - | |||||||||||||
1585 | if (opt.state & QStyle::State_MouseOver || isDown()) { | - | ||||||||||||
1586 | //act as normal button | - | ||||||||||||
1587 | setPalette(QPalette()); | - | ||||||||||||
1588 | } else { | - | ||||||||||||
1589 | //set the highlight color for button text | - | ||||||||||||
1590 | QPalette toolPalette = palette(); | - | ||||||||||||
1591 | toolPalette.setColor(QPalette::ButtonText, toolPalette.color(QPalette::HighlightedText)); | - | ||||||||||||
1592 | setPalette(toolPalette); | - | ||||||||||||
1593 | } | - | ||||||||||||
1594 | #endif | - | ||||||||||||
1595 | QToolButton::paintEvent(e); | - | ||||||||||||
1596 | } | - | ||||||||||||
1597 | }; | - | ||||||||||||
1598 | - | |||||||||||||
1599 | class QPrevNextCalButton : public QToolButton | - | ||||||||||||
1600 | { | - | ||||||||||||
1601 | Q_OBJECT | - | ||||||||||||
1602 | public: | - | ||||||||||||
1603 | QPrevNextCalButton(QWidget *parent) : QToolButton(parent) {} | - | ||||||||||||
1604 | protected: | - | ||||||||||||
1605 | void paintEvent(QPaintEvent *) Q_DECL_OVERRIDE { | - | ||||||||||||
1606 | QStylePainter painter(this); | - | ||||||||||||
1607 | QStyleOptionToolButton opt; | - | ||||||||||||
1608 | initStyleOption(&opt); | - | ||||||||||||
1609 | opt.state &= ~QStyle::State_HasFocus; | - | ||||||||||||
1610 | painter.drawComplexControl(QStyle::CC_ToolButton, opt); | - | ||||||||||||
1611 | } | - | ||||||||||||
1612 | }; | - | ||||||||||||
1613 | - | |||||||||||||
1614 | } // unnamed namespace | - | ||||||||||||
1615 | - | |||||||||||||
1616 | class QCalendarWidgetPrivate : public QWidgetPrivate | - | ||||||||||||
1617 | { | - | ||||||||||||
1618 | Q_DECLARE_PUBLIC(QCalendarWidget) | - | ||||||||||||
1619 | public: | - | ||||||||||||
1620 | QCalendarWidgetPrivate(); | - | ||||||||||||
1621 | - | |||||||||||||
1622 | void showMonth(int year, int month); | - | ||||||||||||
1623 | void update(); | - | ||||||||||||
1624 | void paintCell(QPainter *painter, const QRect &rect, const QDate &date) const; | - | ||||||||||||
1625 | - | |||||||||||||
1626 | void _q_slotShowDate(const QDate &date); | - | ||||||||||||
1627 | void _q_slotChangeDate(const QDate &date); | - | ||||||||||||
1628 | void _q_slotChangeDate(const QDate &date, bool changeMonth); | - | ||||||||||||
1629 | void _q_editingFinished(); | - | ||||||||||||
1630 | void _q_monthChanged(QAction*); | - | ||||||||||||
1631 | void _q_prevMonthClicked(); | - | ||||||||||||
1632 | void _q_nextMonthClicked(); | - | ||||||||||||
1633 | void _q_yearEditingFinished(); | - | ||||||||||||
1634 | void _q_yearClicked(); | - | ||||||||||||
1635 | - | |||||||||||||
1636 | void createNavigationBar(QWidget *widget); | - | ||||||||||||
1637 | void updateButtonIcons(); | - | ||||||||||||
1638 | void updateMonthMenu(); | - | ||||||||||||
1639 | void updateMonthMenuNames(); | - | ||||||||||||
1640 | void updateNavigationBar(); | - | ||||||||||||
1641 | void updateCurrentPage(const QDate &newDate); | - | ||||||||||||
1642 | inline QDate getCurrentDate(); | - | ||||||||||||
1643 | void setNavigatorEnabled(bool enable); | - | ||||||||||||
1644 | - | |||||||||||||
1645 | QCalendarModel *m_model; | - | ||||||||||||
1646 | QCalendarView *m_view; | - | ||||||||||||
1647 | QCalendarDelegate *m_delegate; | - | ||||||||||||
1648 | QItemSelectionModel *m_selection; | - | ||||||||||||
1649 | QCalendarTextNavigator *m_navigator; | - | ||||||||||||
1650 | bool m_dateEditEnabled; | - | ||||||||||||
1651 | - | |||||||||||||
1652 | QToolButton *nextMonth; | - | ||||||||||||
1653 | QToolButton *prevMonth; | - | ||||||||||||
1654 | QCalToolButton *monthButton; | - | ||||||||||||
1655 | QMenu *monthMenu; | - | ||||||||||||
1656 | QMap<int, QAction *> monthToAction; | - | ||||||||||||
1657 | QCalToolButton *yearButton; | - | ||||||||||||
1658 | QSpinBox *yearEdit; | - | ||||||||||||
1659 | QWidget *navBarBackground; | - | ||||||||||||
1660 | QSpacerItem *spaceHolder; | - | ||||||||||||
1661 | - | |||||||||||||
1662 | bool navBarVisible; | - | ||||||||||||
1663 | mutable QSize cachedSizeHint; | - | ||||||||||||
1664 | Qt::FocusPolicy oldFocusPolicy; | - | ||||||||||||
1665 | }; | - | ||||||||||||
1666 | - | |||||||||||||
1667 | void QCalendarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, | - | ||||||||||||
1668 | const QModelIndex &index) const | - | ||||||||||||
1669 | { | - | ||||||||||||
1670 | QDate date = calendarWidgetPrivate->m_model->dateForCell(index.row(), index.column()); | - | ||||||||||||
1671 | if (date.isValid()) { | - | ||||||||||||
1672 | storedOption = option; | - | ||||||||||||
1673 | QRect rect = option.rect; | - | ||||||||||||
1674 | calendarWidgetPrivate->paintCell(painter, rect, date); | - | ||||||||||||
1675 | } else { | - | ||||||||||||
1676 | QItemDelegate::paint(painter, option, index); | - | ||||||||||||
1677 | } | - | ||||||||||||
1678 | } | - | ||||||||||||
1679 | - | |||||||||||||
1680 | void QCalendarDelegate::paintCell(QPainter *painter, const QRect &rect, const QDate &date) const | - | ||||||||||||
1681 | { | - | ||||||||||||
1682 | storedOption.rect = rect; | - | ||||||||||||
1683 | int row = -1; | - | ||||||||||||
1684 | int col = -1; | - | ||||||||||||
1685 | calendarWidgetPrivate->m_model->cellForDate(date, &row, &col); | - | ||||||||||||
1686 | QModelIndex idx = calendarWidgetPrivate->m_model->index(row, col); | - | ||||||||||||
1687 | QItemDelegate::paint(painter, storedOption, idx); | - | ||||||||||||
1688 | } | - | ||||||||||||
1689 | - | |||||||||||||
1690 | QCalendarWidgetPrivate::QCalendarWidgetPrivate() | - | ||||||||||||
1691 | : QWidgetPrivate() | - | ||||||||||||
1692 | { | - | ||||||||||||
1693 | m_model = 0; | - | ||||||||||||
1694 | m_view = 0; | - | ||||||||||||
1695 | m_delegate = 0; | - | ||||||||||||
1696 | m_selection = 0; | - | ||||||||||||
1697 | m_navigator = 0; | - | ||||||||||||
1698 | m_dateEditEnabled = false; | - | ||||||||||||
1699 | navBarVisible = true; | - | ||||||||||||
1700 | oldFocusPolicy = Qt::StrongFocus; | - | ||||||||||||
1701 | } | - | ||||||||||||
1702 | - | |||||||||||||
1703 | void QCalendarWidgetPrivate::setNavigatorEnabled(bool enable) | - | ||||||||||||
1704 | { | - | ||||||||||||
1705 | Q_Q(QCalendarWidget); | - | ||||||||||||
1706 | - | |||||||||||||
1707 | bool navigatorEnabled = (m_navigator->widget() != 0); | - | ||||||||||||
1708 | if (enable == navigatorEnabled) | - | ||||||||||||
1709 | return; | - | ||||||||||||
1710 | - | |||||||||||||
1711 | if (enable) { | - | ||||||||||||
1712 | m_navigator->setWidget(q); | - | ||||||||||||
1713 | q->connect(m_navigator, SIGNAL(dateChanged(QDate)), | - | ||||||||||||
1714 | q, SLOT(_q_slotChangeDate(QDate))); | - | ||||||||||||
1715 | q->connect(m_navigator, SIGNAL(editingFinished()), | - | ||||||||||||
1716 | q, SLOT(_q_editingFinished())); | - | ||||||||||||
1717 | m_view->installEventFilter(m_navigator); | - | ||||||||||||
1718 | } else { | - | ||||||||||||
1719 | m_navigator->setWidget(0); | - | ||||||||||||
1720 | q->disconnect(m_navigator, SIGNAL(dateChanged(QDate)), | - | ||||||||||||
1721 | q, SLOT(_q_slotChangeDate(QDate))); | - | ||||||||||||
1722 | q->disconnect(m_navigator, SIGNAL(editingFinished()), | - | ||||||||||||
1723 | q, SLOT(_q_editingFinished())); | - | ||||||||||||
1724 | m_view->removeEventFilter(m_navigator); | - | ||||||||||||
1725 | } | - | ||||||||||||
1726 | } | - | ||||||||||||
1727 | - | |||||||||||||
1728 | void QCalendarWidgetPrivate::createNavigationBar(QWidget *widget) | - | ||||||||||||
1729 | { | - | ||||||||||||
1730 | Q_Q(QCalendarWidget); | - | ||||||||||||
1731 | navBarBackground = new QWidget(widget); | - | ||||||||||||
1732 | navBarBackground->setObjectName(QLatin1String("qt_calendar_navigationbar")); | - | ||||||||||||
1733 | navBarBackground->setAutoFillBackground(true); | - | ||||||||||||
1734 | navBarBackground->setBackgroundRole(QPalette::Highlight); | - | ||||||||||||
1735 | - | |||||||||||||
1736 | prevMonth = new QPrevNextCalButton(navBarBackground); | - | ||||||||||||
1737 | nextMonth = new QPrevNextCalButton(navBarBackground); | - | ||||||||||||
1738 | prevMonth->setAutoRaise(true); | - | ||||||||||||
1739 | nextMonth->setAutoRaise(true); | - | ||||||||||||
1740 | prevMonth->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum); | - | ||||||||||||
1741 | nextMonth->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum); | - | ||||||||||||
1742 | nextMonth->setAutoRaise(true); | - | ||||||||||||
1743 | updateButtonIcons(); | - | ||||||||||||
1744 | prevMonth->setAutoRepeat(true); | - | ||||||||||||
1745 | nextMonth->setAutoRepeat(true); | - | ||||||||||||
1746 | - | |||||||||||||
1747 | monthButton = new QCalToolButton(navBarBackground); | - | ||||||||||||
1748 | monthButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum); | - | ||||||||||||
1749 | monthButton->setAutoRaise(true); | - | ||||||||||||
1750 | monthButton->setPopupMode(QToolButton::InstantPopup); | - | ||||||||||||
1751 | monthMenu = new QMenu(monthButton); | - | ||||||||||||
1752 | for (int i = 1; i <= 12; i++) { | - | ||||||||||||
1753 | QString monthName(q->locale().standaloneMonthName(i, QLocale::LongFormat)); | - | ||||||||||||
1754 | QAction *act = monthMenu->addAction(monthName); | - | ||||||||||||
1755 | act->setData(i); | - | ||||||||||||
1756 | monthToAction[i] = act; | - | ||||||||||||
1757 | } | - | ||||||||||||
1758 | monthButton->setMenu(monthMenu); | - | ||||||||||||
1759 | yearButton = new QCalToolButton(navBarBackground); | - | ||||||||||||
1760 | yearButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum); | - | ||||||||||||
1761 | yearButton->setAutoRaise(true); | - | ||||||||||||
1762 | yearEdit = new QSpinBox(navBarBackground); | - | ||||||||||||
1763 | - | |||||||||||||
1764 | QFont font = q->font(); | - | ||||||||||||
1765 | font.setBold(true); | - | ||||||||||||
1766 | monthButton->setFont(font); | - | ||||||||||||
1767 | yearButton->setFont(font); | - | ||||||||||||
1768 | yearEdit->setFrame(false); | - | ||||||||||||
1769 | yearEdit->setMinimum(m_model->m_minimumDate.year()); | - | ||||||||||||
1770 | yearEdit->setMaximum(m_model->m_maximumDate.year()); | - | ||||||||||||
1771 | yearEdit->hide(); | - | ||||||||||||
1772 | spaceHolder = new QSpacerItem(0,0); | - | ||||||||||||
1773 | - | |||||||||||||
1774 | QHBoxLayout *headerLayout = new QHBoxLayout; | - | ||||||||||||
1775 | headerLayout->setMargin(0); | - | ||||||||||||
1776 | headerLayout->setSpacing(0); | - | ||||||||||||
1777 | headerLayout->addWidget(prevMonth); | - | ||||||||||||
1778 | headerLayout->insertStretch(headerLayout->count()); | - | ||||||||||||
1779 | headerLayout->addWidget(monthButton); | - | ||||||||||||
1780 | headerLayout->addItem(spaceHolder); | - | ||||||||||||
1781 | headerLayout->addWidget(yearButton); | - | ||||||||||||
1782 | headerLayout->insertStretch(headerLayout->count()); | - | ||||||||||||
1783 | headerLayout->addWidget(nextMonth); | - | ||||||||||||
1784 | navBarBackground->setLayout(headerLayout); | - | ||||||||||||
1785 | - | |||||||||||||
1786 | yearEdit->setFocusPolicy(Qt::StrongFocus); | - | ||||||||||||
1787 | prevMonth->setFocusPolicy(Qt::NoFocus); | - | ||||||||||||
1788 | nextMonth->setFocusPolicy(Qt::NoFocus); | - | ||||||||||||
1789 | yearButton->setFocusPolicy(Qt::NoFocus); | - | ||||||||||||
1790 | monthButton->setFocusPolicy(Qt::NoFocus); | - | ||||||||||||
1791 | - | |||||||||||||
1792 | //set names for the header controls. | - | ||||||||||||
1793 | prevMonth->setObjectName(QLatin1String("qt_calendar_prevmonth")); | - | ||||||||||||
1794 | nextMonth->setObjectName(QLatin1String("qt_calendar_nextmonth")); | - | ||||||||||||
1795 | monthButton->setObjectName(QLatin1String("qt_calendar_monthbutton")); | - | ||||||||||||
1796 | yearButton->setObjectName(QLatin1String("qt_calendar_yearbutton")); | - | ||||||||||||
1797 | yearEdit->setObjectName(QLatin1String("qt_calendar_yearedit")); | - | ||||||||||||
1798 | - | |||||||||||||
1799 | updateMonthMenu(); | - | ||||||||||||
1800 | showMonth(m_model->m_date.year(), m_model->m_date.month()); | - | ||||||||||||
1801 | } | - | ||||||||||||
1802 | - | |||||||||||||
1803 | void QCalendarWidgetPrivate::updateButtonIcons() | - | ||||||||||||
1804 | { | - | ||||||||||||
1805 | Q_Q(QCalendarWidget); | - | ||||||||||||
1806 | prevMonth->setIcon(q->style()->standardIcon(q->isRightToLeft() ? QStyle::SP_ArrowRight : QStyle::SP_ArrowLeft, 0, q)); | - | ||||||||||||
1807 | nextMonth->setIcon(q->style()->standardIcon(q->isRightToLeft() ? QStyle::SP_ArrowLeft : QStyle::SP_ArrowRight, 0, q)); | - | ||||||||||||
1808 | } | - | ||||||||||||
1809 | - | |||||||||||||
1810 | void QCalendarWidgetPrivate::updateMonthMenu() | - | ||||||||||||
1811 | { | - | ||||||||||||
1812 | int beg = 1, end = 12; | - | ||||||||||||
1813 | bool prevEnabled = true; | - | ||||||||||||
1814 | bool nextEnabled = true; | - | ||||||||||||
1815 | if (m_model->m_shownYear == m_model->m_minimumDate.year()) { | - | ||||||||||||
1816 | beg = m_model->m_minimumDate.month(); | - | ||||||||||||
1817 | if (m_model->m_shownMonth == m_model->m_minimumDate.month()) | - | ||||||||||||
1818 | prevEnabled = false; | - | ||||||||||||
1819 | } | - | ||||||||||||
1820 | if (m_model->m_shownYear == m_model->m_maximumDate.year()) { | - | ||||||||||||
1821 | end = m_model->m_maximumDate.month(); | - | ||||||||||||
1822 | if (m_model->m_shownMonth == m_model->m_maximumDate.month()) | - | ||||||||||||
1823 | nextEnabled = false; | - | ||||||||||||
1824 | } | - | ||||||||||||
1825 | prevMonth->setEnabled(prevEnabled); | - | ||||||||||||
1826 | nextMonth->setEnabled(nextEnabled); | - | ||||||||||||
1827 | for (int i = 1; i <= 12; i++) { | - | ||||||||||||
1828 | bool monthEnabled = true; | - | ||||||||||||
1829 | if (i < beg || i > end) | - | ||||||||||||
1830 | monthEnabled = false; | - | ||||||||||||
1831 | monthToAction[i]->setEnabled(monthEnabled); | - | ||||||||||||
1832 | } | - | ||||||||||||
1833 | } | - | ||||||||||||
1834 | - | |||||||||||||
1835 | void QCalendarWidgetPrivate::updateMonthMenuNames() | - | ||||||||||||
1836 | { | - | ||||||||||||
1837 | Q_Q(QCalendarWidget); | - | ||||||||||||
1838 | - | |||||||||||||
1839 | for (int i = 1; i <= 12; i++) { | - | ||||||||||||
1840 | QString monthName(q->locale().standaloneMonthName(i, QLocale::LongFormat)); | - | ||||||||||||
1841 | monthToAction[i]->setText(monthName); | - | ||||||||||||
1842 | } | - | ||||||||||||
1843 | } | - | ||||||||||||
1844 | - | |||||||||||||
1845 | void QCalendarWidgetPrivate::updateCurrentPage(const QDate &date) | - | ||||||||||||
1846 | { | - | ||||||||||||
1847 | Q_Q(QCalendarWidget); | - | ||||||||||||
1848 | - | |||||||||||||
1849 | QDate newDate = date; | - | ||||||||||||
1850 | QDate minDate = q->minimumDate(); | - | ||||||||||||
1851 | QDate maxDate = q->maximumDate(); | - | ||||||||||||
1852 | if (minDate.isValid()&& minDate.daysTo(newDate) < 0) | - | ||||||||||||
1853 | newDate = minDate; | - | ||||||||||||
1854 | if (maxDate.isValid()&& maxDate.daysTo(newDate) > 0) | - | ||||||||||||
1855 | newDate = maxDate; | - | ||||||||||||
1856 | showMonth(newDate.year(), newDate.month()); | - | ||||||||||||
1857 | int row = -1, col = -1; | - | ||||||||||||
1858 | m_model->cellForDate(newDate, &row, &col); | - | ||||||||||||
1859 | if (row != -1 && col != -1) | - | ||||||||||||
1860 | { | - | ||||||||||||
1861 | m_view->selectionModel()->setCurrentIndex(m_model->index(row, col), | - | ||||||||||||
1862 | QItemSelectionModel::NoUpdate); | - | ||||||||||||
1863 | } | - | ||||||||||||
1864 | } | - | ||||||||||||
1865 | - | |||||||||||||
1866 | void QCalendarWidgetPrivate::_q_monthChanged(QAction *act) | - | ||||||||||||
1867 | { | - | ||||||||||||
1868 | monthButton->setText(act->text()); | - | ||||||||||||
1869 | QDate currentDate = getCurrentDate(); | - | ||||||||||||
1870 | QDate newDate = currentDate.addMonths(act->data().toInt()-currentDate.month()); | - | ||||||||||||
1871 | updateCurrentPage(newDate); | - | ||||||||||||
1872 | } | - | ||||||||||||
1873 | - | |||||||||||||
1874 | QDate QCalendarWidgetPrivate::getCurrentDate() | - | ||||||||||||
1875 | { | - | ||||||||||||
1876 | QModelIndex index = m_view->currentIndex(); | - | ||||||||||||
1877 | return m_model->dateForCell(index.row(), index.column()); | - | ||||||||||||
1878 | } | - | ||||||||||||
1879 | - | |||||||||||||
1880 | void QCalendarWidgetPrivate::_q_prevMonthClicked() | - | ||||||||||||
1881 | { | - | ||||||||||||
1882 | QDate currentDate = getCurrentDate().addMonths(-1); | - | ||||||||||||
1883 | updateCurrentPage(currentDate); | - | ||||||||||||
1884 | } | - | ||||||||||||
1885 | - | |||||||||||||
1886 | void QCalendarWidgetPrivate::_q_nextMonthClicked() | - | ||||||||||||
1887 | { | - | ||||||||||||
1888 | QDate currentDate = getCurrentDate().addMonths(1); | - | ||||||||||||
1889 | updateCurrentPage(currentDate); | - | ||||||||||||
1890 | } | - | ||||||||||||
1891 | - | |||||||||||||
1892 | void QCalendarWidgetPrivate::_q_yearEditingFinished() | - | ||||||||||||
1893 | { | - | ||||||||||||
1894 | Q_Q(QCalendarWidget); | - | ||||||||||||
1895 | yearButton->setText(yearEdit->text()); | - | ||||||||||||
1896 | yearEdit->hide(); | - | ||||||||||||
1897 | q->setFocusPolicy(oldFocusPolicy); | - | ||||||||||||
1898 | qApp->removeEventFilter(q); | - | ||||||||||||
1899 | spaceHolder->changeSize(0, 0); | - | ||||||||||||
1900 | yearButton->show(); | - | ||||||||||||
1901 | QDate currentDate = getCurrentDate(); | - | ||||||||||||
1902 | currentDate = currentDate.addYears(yearEdit->text().toInt() - currentDate.year()); | - | ||||||||||||
1903 | updateCurrentPage(currentDate); | - | ||||||||||||
1904 | } | - | ||||||||||||
1905 | - | |||||||||||||
1906 | void QCalendarWidgetPrivate::_q_yearClicked() | - | ||||||||||||
1907 | { | - | ||||||||||||
1908 | Q_Q(QCalendarWidget); | - | ||||||||||||
1909 | //show the spinbox on top of the button | - | ||||||||||||
1910 | yearEdit->setGeometry(yearButton->x(), yearButton->y(), | - | ||||||||||||
1911 | yearEdit->sizeHint().width(), yearButton->height()); | - | ||||||||||||
1912 | spaceHolder->changeSize(yearButton->width(), 0); | - | ||||||||||||
1913 | yearButton->hide(); | - | ||||||||||||
1914 | oldFocusPolicy = q->focusPolicy(); | - | ||||||||||||
1915 | q->setFocusPolicy(Qt::NoFocus); | - | ||||||||||||
1916 | yearEdit->show(); | - | ||||||||||||
1917 | qApp->installEventFilter(q); | - | ||||||||||||
1918 | yearEdit->raise(); | - | ||||||||||||
1919 | yearEdit->selectAll(); | - | ||||||||||||
1920 | yearEdit->setFocus(Qt::MouseFocusReason); | - | ||||||||||||
1921 | } | - | ||||||||||||
1922 | - | |||||||||||||
1923 | void QCalendarWidgetPrivate::showMonth(int year, int month) | - | ||||||||||||
1924 | { | - | ||||||||||||
1925 | if (m_model->m_shownYear == year && m_model->m_shownMonth == month) | - | ||||||||||||
1926 | return; | - | ||||||||||||
1927 | Q_Q(QCalendarWidget); | - | ||||||||||||
1928 | m_model->showMonth(year, month); | - | ||||||||||||
1929 | updateNavigationBar(); | - | ||||||||||||
1930 | emit q->currentPageChanged(year, month); | - | ||||||||||||
1931 | m_view->internalUpdate(); | - | ||||||||||||
1932 | cachedSizeHint = QSize(); | - | ||||||||||||
1933 | update(); | - | ||||||||||||
1934 | updateMonthMenu(); | - | ||||||||||||
1935 | } | - | ||||||||||||
1936 | - | |||||||||||||
1937 | void QCalendarWidgetPrivate::updateNavigationBar() | - | ||||||||||||
1938 | { | - | ||||||||||||
1939 | Q_Q(QCalendarWidget); | - | ||||||||||||
1940 | - | |||||||||||||
1941 | QString monthName = q->locale().standaloneMonthName(m_model->m_shownMonth, QLocale::LongFormat); | - | ||||||||||||
1942 | - | |||||||||||||
1943 | monthButton->setText(monthName); | - | ||||||||||||
1944 | yearButton->setText(QString::number(m_model->m_shownYear)); | - | ||||||||||||
1945 | yearEdit->setValue(m_model->m_shownYear); | - | ||||||||||||
1946 | } | - | ||||||||||||
1947 | - | |||||||||||||
1948 | void QCalendarWidgetPrivate::update() | - | ||||||||||||
1949 | { | - | ||||||||||||
1950 | QDate currentDate = m_model->m_date; | - | ||||||||||||
1951 | int row, column; | - | ||||||||||||
1952 | m_model->cellForDate(currentDate, &row, &column); | - | ||||||||||||
1953 | QModelIndex idx; | - | ||||||||||||
1954 | m_selection->clear(); | - | ||||||||||||
1955 | if (row != -1 && column != -1) { | - | ||||||||||||
1956 | idx = m_model->index(row, column); | - | ||||||||||||
1957 | m_selection->setCurrentIndex(idx, QItemSelectionModel::SelectCurrent); | - | ||||||||||||
1958 | } | - | ||||||||||||
1959 | } | - | ||||||||||||
1960 | - | |||||||||||||
1961 | void QCalendarWidgetPrivate::paintCell(QPainter *painter, const QRect &rect, const QDate &date) const | - | ||||||||||||
1962 | { | - | ||||||||||||
1963 | Q_Q(const QCalendarWidget); | - | ||||||||||||
1964 | q->paintCell(painter, rect, date); | - | ||||||||||||
1965 | } | - | ||||||||||||
1966 | - | |||||||||||||
1967 | void QCalendarWidgetPrivate::_q_slotShowDate(const QDate &date) | - | ||||||||||||
1968 | { | - | ||||||||||||
1969 | updateCurrentPage(date); | - | ||||||||||||
1970 | } | - | ||||||||||||
1971 | - | |||||||||||||
1972 | void QCalendarWidgetPrivate::_q_slotChangeDate(const QDate &date) | - | ||||||||||||
1973 | { | - | ||||||||||||
1974 | _q_slotChangeDate(date, true); | - | ||||||||||||
1975 | } | - | ||||||||||||
1976 | - | |||||||||||||
1977 | void QCalendarWidgetPrivate::_q_slotChangeDate(const QDate &date, bool changeMonth) | - | ||||||||||||
1978 | { | - | ||||||||||||
1979 | QDate oldDate = m_model->m_date; | - | ||||||||||||
1980 | m_model->setDate(date); | - | ||||||||||||
1981 | QDate newDate = m_model->m_date; | - | ||||||||||||
1982 | if (changeMonth) | - | ||||||||||||
1983 | showMonth(newDate.year(), newDate.month()); | - | ||||||||||||
1984 | if (oldDate != newDate) { | - | ||||||||||||
1985 | update(); | - | ||||||||||||
1986 | Q_Q(QCalendarWidget); | - | ||||||||||||
1987 | m_navigator->setDate(newDate); | - | ||||||||||||
1988 | emit q->selectionChanged(); | - | ||||||||||||
1989 | } | - | ||||||||||||
1990 | } | - | ||||||||||||
1991 | - | |||||||||||||
1992 | void QCalendarWidgetPrivate::_q_editingFinished() | - | ||||||||||||
1993 | { | - | ||||||||||||
1994 | Q_Q(QCalendarWidget); | - | ||||||||||||
1995 | emit q->activated(m_model->m_date); | - | ||||||||||||
1996 | } | - | ||||||||||||
1997 | - | |||||||||||||
1998 | /*! | - | ||||||||||||
1999 | \class QCalendarWidget | - | ||||||||||||
2000 | \brief The QCalendarWidget class provides a monthly based | - | ||||||||||||
2001 | calendar widget allowing the user to select a date. | - | ||||||||||||
2002 | \since 4.2 | - | ||||||||||||
2003 | - | |||||||||||||
2004 | \ingroup advanced | - | ||||||||||||
2005 | \inmodule QtWidgets | - | ||||||||||||
2006 | - | |||||||||||||
2007 | \image fusion-calendarwidget.png | - | ||||||||||||
2008 | - | |||||||||||||
2009 | The widget is initialized with the current month and year, but | - | ||||||||||||
2010 | QCalendarWidget provides several public slots to change the year | - | ||||||||||||
2011 | and month that is shown. | - | ||||||||||||
2012 | - | |||||||||||||
2013 | By default, today's date is selected, and the user can select a | - | ||||||||||||
2014 | date using both mouse and keyboard. The currently selected date | - | ||||||||||||
2015 | can be retrieved using the selectedDate() function. It is | - | ||||||||||||
2016 | possible to constrain the user selection to a given date range by | - | ||||||||||||
2017 | setting the minimumDate and maximumDate properties. | - | ||||||||||||
2018 | Alternatively, both properties can be set in one go using the | - | ||||||||||||
2019 | setDateRange() convenience slot. Set the \l selectionMode | - | ||||||||||||
2020 | property to NoSelection to prohibit the user from selecting at | - | ||||||||||||
2021 | all. Note that a date also can be selected programmatically using | - | ||||||||||||
2022 | the setSelectedDate() slot. | - | ||||||||||||
2023 | - | |||||||||||||
2024 | The currently displayed month and year can be retrieved using the | - | ||||||||||||
2025 | monthShown() and yearShown() functions, respectively. | - | ||||||||||||
2026 | - | |||||||||||||
2027 | A newly created calendar widget uses abbreviated day names, and | - | ||||||||||||
2028 | both Saturdays and Sundays are marked in red. The calendar grid is | - | ||||||||||||
2029 | not visible. The week numbers are displayed, and the first column | - | ||||||||||||
2030 | day is the first day of the week for the calendar's locale. | - | ||||||||||||
2031 | - | |||||||||||||
2032 | The notation of the days can be altered to a single letter | - | ||||||||||||
2033 | abbreviations ("M" for "Monday") by setting the | - | ||||||||||||
2034 | horizontalHeaderFormat property to | - | ||||||||||||
2035 | QCalendarWidget::SingleLetterDayNames. Setting the same property | - | ||||||||||||
2036 | to QCalendarWidget::LongDayNames makes the header display the | - | ||||||||||||
2037 | complete day names. The week numbers can be removed by setting | - | ||||||||||||
2038 | the verticalHeaderFormat property to | - | ||||||||||||
2039 | QCalendarWidget::NoVerticalHeader. The calendar grid can be | - | ||||||||||||
2040 | turned on by setting the gridVisible property to true using the | - | ||||||||||||
2041 | setGridVisible() function: | - | ||||||||||||
2042 | - | |||||||||||||
2043 | \table | - | ||||||||||||
2044 | \row \li | - | ||||||||||||
2045 | \image qcalendarwidget-grid.png | - | ||||||||||||
2046 | \row \li | - | ||||||||||||
2047 | \snippet code/src_gui_widgets_qcalendarwidget.cpp 0 | - | ||||||||||||
2048 | \endtable | - | ||||||||||||
2049 | - | |||||||||||||
2050 | Finally, the day in the first column can be altered using the | - | ||||||||||||
2051 | setFirstDayOfWeek() function. | - | ||||||||||||
2052 | - | |||||||||||||
2053 | The QCalendarWidget class also provides three signals, | - | ||||||||||||
2054 | selectionChanged(), activated() and currentPageChanged() making it | - | ||||||||||||
2055 | possible to respond to user interaction. | - | ||||||||||||
2056 | - | |||||||||||||
2057 | The rendering of the headers, weekdays or single days can be | - | ||||||||||||
2058 | largely customized by setting QTextCharFormat's for some special | - | ||||||||||||
2059 | weekday, a special date or for the rendering of the headers. | - | ||||||||||||
2060 | - | |||||||||||||
2061 | Only a subset of the properties in QTextCharFormat are used by the | - | ||||||||||||
2062 | calendar widget. Currently, the foreground, background and font | - | ||||||||||||
2063 | properties are used to determine the rendering of individual cells | - | ||||||||||||
2064 | in the widget. | - | ||||||||||||
2065 | - | |||||||||||||
2066 | \sa QDate, QDateEdit, QTextCharFormat | - | ||||||||||||
2067 | */ | - | ||||||||||||
2068 | - | |||||||||||||
2069 | /*! | - | ||||||||||||
2070 | \enum QCalendarWidget::SelectionMode | - | ||||||||||||
2071 | - | |||||||||||||
2072 | This enum describes the types of selection offered to the user for | - | ||||||||||||
2073 | selecting dates in the calendar. | - | ||||||||||||
2074 | - | |||||||||||||
2075 | \value NoSelection Dates cannot be selected. | - | ||||||||||||
2076 | \value SingleSelection Single dates can be selected. | - | ||||||||||||
2077 | - | |||||||||||||
2078 | \sa selectionMode | - | ||||||||||||
2079 | */ | - | ||||||||||||
2080 | - | |||||||||||||
2081 | /*! | - | ||||||||||||
2082 | Constructs a calendar widget with the given \a parent. | - | ||||||||||||
2083 | - | |||||||||||||
2084 | The widget is initialized with the current month and year, and the | - | ||||||||||||
2085 | currently selected date is today. | - | ||||||||||||
2086 | - | |||||||||||||
2087 | \sa setCurrentPage() | - | ||||||||||||
2088 | */ | - | ||||||||||||
2089 | QCalendarWidget::QCalendarWidget(QWidget *parent) | - | ||||||||||||
2090 | : QWidget(*new QCalendarWidgetPrivate, parent, 0) | - | ||||||||||||
2091 | { | - | ||||||||||||
2092 | Q_D(QCalendarWidget); | - | ||||||||||||
2093 | - | |||||||||||||
2094 | setAutoFillBackground(true); | - | ||||||||||||
2095 | setBackgroundRole(QPalette::Window); | - | ||||||||||||
2096 | - | |||||||||||||
2097 | QVBoxLayout *layoutV = new QVBoxLayout(this); | - | ||||||||||||
2098 | layoutV->setMargin(0); | - | ||||||||||||
2099 | d->m_model = new QCalendarModel(this); | - | ||||||||||||
2100 | QTextCharFormat fmt; | - | ||||||||||||
2101 | fmt.setForeground(QBrush(Qt::red)); | - | ||||||||||||
2102 | d->m_model->m_dayFormats.insert(Qt::Saturday, fmt); | - | ||||||||||||
2103 | d->m_model->m_dayFormats.insert(Qt::Sunday, fmt); | - | ||||||||||||
2104 | d->m_view = new QCalendarView(this); | - | ||||||||||||
2105 | d->m_view->setObjectName(QLatin1String("qt_calendar_calendarview")); | - | ||||||||||||
2106 | d->m_view->setModel(d->m_model); | - | ||||||||||||
2107 | d->m_model->setView(d->m_view); | - | ||||||||||||
2108 | d->m_view->setSelectionBehavior(QAbstractItemView::SelectItems); | - | ||||||||||||
2109 | d->m_view->setSelectionMode(QAbstractItemView::SingleSelection); | - | ||||||||||||
2110 | d->m_view->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); | - | ||||||||||||
2111 | d->m_view->horizontalHeader()->setSectionsClickable(false); | - | ||||||||||||
2112 | d->m_view->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch); | - | ||||||||||||
2113 | d->m_view->verticalHeader()->setSectionsClickable(false); | - | ||||||||||||
2114 | d->m_selection = d->m_view->selectionModel(); | - | ||||||||||||
2115 | d->createNavigationBar(this); | - | ||||||||||||
2116 | d->m_view->setFrameStyle(QFrame::NoFrame); | - | ||||||||||||
2117 | d->m_delegate = new QCalendarDelegate(d, this); | - | ||||||||||||
2118 | d->m_view->setItemDelegate(d->m_delegate); | - | ||||||||||||
2119 | d->update(); | - | ||||||||||||
2120 | d->updateNavigationBar(); | - | ||||||||||||
2121 | setFocusPolicy(Qt::StrongFocus); | - | ||||||||||||
2122 | setFocusProxy(d->m_view); | - | ||||||||||||
2123 | setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); | - | ||||||||||||
2124 | - | |||||||||||||
2125 | connect(d->m_view, SIGNAL(showDate(QDate)), | - | ||||||||||||
2126 | this, SLOT(_q_slotShowDate(QDate))); | - | ||||||||||||
2127 | connect(d->m_view, SIGNAL(changeDate(QDate,bool)), | - | ||||||||||||
2128 | this, SLOT(_q_slotChangeDate(QDate,bool))); | - | ||||||||||||
2129 | connect(d->m_view, SIGNAL(clicked(QDate)), | - | ||||||||||||
2130 | this, SIGNAL(clicked(QDate))); | - | ||||||||||||
2131 | connect(d->m_view, SIGNAL(editingFinished()), | - | ||||||||||||
2132 | this, SLOT(_q_editingFinished())); | - | ||||||||||||
2133 | - | |||||||||||||
2134 | connect(d->prevMonth, SIGNAL(clicked(bool)), | - | ||||||||||||
2135 | this, SLOT(_q_prevMonthClicked())); | - | ||||||||||||
2136 | connect(d->nextMonth, SIGNAL(clicked(bool)), | - | ||||||||||||
2137 | this, SLOT(_q_nextMonthClicked())); | - | ||||||||||||
2138 | connect(d->yearButton, SIGNAL(clicked(bool)), | - | ||||||||||||
2139 | this, SLOT(_q_yearClicked())); | - | ||||||||||||
2140 | connect(d->monthMenu, SIGNAL(triggered(QAction*)), | - | ||||||||||||
2141 | this, SLOT(_q_monthChanged(QAction*))); | - | ||||||||||||
2142 | connect(d->yearEdit, SIGNAL(editingFinished()), | - | ||||||||||||
2143 | this, SLOT(_q_yearEditingFinished())); | - | ||||||||||||
2144 | - | |||||||||||||
2145 | layoutV->setMargin(0); | - | ||||||||||||
2146 | layoutV->setSpacing(0); | - | ||||||||||||
2147 | layoutV->addWidget(d->navBarBackground); | - | ||||||||||||
2148 | layoutV->addWidget(d->m_view); | - | ||||||||||||
2149 | - | |||||||||||||
2150 | d->m_navigator = new QCalendarTextNavigator(this); | - | ||||||||||||
2151 | setDateEditEnabled(true); | - | ||||||||||||
2152 | } | - | ||||||||||||
2153 | - | |||||||||||||
2154 | /*! | - | ||||||||||||
2155 | Destroys the calendar widget. | - | ||||||||||||
2156 | */ | - | ||||||||||||
2157 | QCalendarWidget::~QCalendarWidget() | - | ||||||||||||
2158 | { | - | ||||||||||||
2159 | } | - | ||||||||||||
2160 | - | |||||||||||||
2161 | /*! | - | ||||||||||||
2162 | \reimp | - | ||||||||||||
2163 | */ | - | ||||||||||||
2164 | QSize QCalendarWidget::sizeHint() const | - | ||||||||||||
2165 | { | - | ||||||||||||
2166 | return minimumSizeHint(); | - | ||||||||||||
2167 | } | - | ||||||||||||
2168 | - | |||||||||||||
2169 | /*! | - | ||||||||||||
2170 | \reimp | - | ||||||||||||
2171 | */ | - | ||||||||||||
2172 | QSize QCalendarWidget::minimumSizeHint() const | - | ||||||||||||
2173 | { | - | ||||||||||||
2174 | Q_D(const QCalendarWidget); | - | ||||||||||||
2175 | if (d->cachedSizeHint.isValid()) | - | ||||||||||||
2176 | return d->cachedSizeHint; | - | ||||||||||||
2177 | - | |||||||||||||
2178 | ensurePolished(); | - | ||||||||||||
2179 | - | |||||||||||||
2180 | int w = 0; | - | ||||||||||||
2181 | int h = 0; | - | ||||||||||||
2182 | - | |||||||||||||
2183 | int end = 53; | - | ||||||||||||
2184 | int rows = 7; | - | ||||||||||||
2185 | int cols = 8; | - | ||||||||||||
2186 | - | |||||||||||||
2187 | const int marginH = (style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1) * 2; | - | ||||||||||||
2188 | - | |||||||||||||
2189 | if (horizontalHeaderFormat() == QCalendarWidget::NoHorizontalHeader) { | - | ||||||||||||
2190 | rows = 6; | - | ||||||||||||
2191 | } else { | - | ||||||||||||
2192 | for (int i = 1; i <= 7; i++) { | - | ||||||||||||
2193 | QFontMetrics fm(d->m_model->formatForCell(0, i).font()); | - | ||||||||||||
2194 | w = qMax(w, fm.width(d->m_model->dayName(d->m_model->dayOfWeekForColumn(i))) + marginH); | - | ||||||||||||
2195 | h = qMax(h, fm.height()); | - | ||||||||||||
2196 | } | - | ||||||||||||
2197 | } | - | ||||||||||||
2198 | - | |||||||||||||
2199 | if (verticalHeaderFormat() == QCalendarWidget::NoVerticalHeader) { | - | ||||||||||||
2200 | cols = 7; | - | ||||||||||||
2201 | } else { | - | ||||||||||||
2202 | for (int i = 1; i <= 6; i++) { | - | ||||||||||||
2203 | QFontMetrics fm(d->m_model->formatForCell(i, 0).font()); | - | ||||||||||||
2204 | for (int j = 1; j < end; j++) | - | ||||||||||||
2205 | w = qMax(w, fm.width(QString::number(j)) + marginH); | - | ||||||||||||
2206 | h = qMax(h, fm.height()); | - | ||||||||||||
2207 | } | - | ||||||||||||
2208 | } | - | ||||||||||||
2209 | - | |||||||||||||
2210 | QFontMetrics fm(d->m_model->formatForCell(1, 1).font()); | - | ||||||||||||
2211 | for (int i = 1; i <= end; i++) { | - | ||||||||||||
2212 | w = qMax(w, fm.width(QString::number(i)) + marginH); | - | ||||||||||||
2213 | h = qMax(h, fm.height()); | - | ||||||||||||
2214 | } | - | ||||||||||||
2215 | - | |||||||||||||
2216 | if (d->m_view->showGrid()) { | - | ||||||||||||
2217 | // hardcoded in tableview | - | ||||||||||||
2218 | w += 1; | - | ||||||||||||
2219 | h += 1; | - | ||||||||||||
2220 | } | - | ||||||||||||
2221 | - | |||||||||||||
2222 | w += 1; // default column span | - | ||||||||||||
2223 | - | |||||||||||||
2224 | h = qMax(h, d->m_view->verticalHeader()->minimumSectionSize()); | - | ||||||||||||
2225 | w = qMax(w, d->m_view->horizontalHeader()->minimumSectionSize()); | - | ||||||||||||
2226 | - | |||||||||||||
2227 | //add the size of the header. | - | ||||||||||||
2228 | QSize headerSize(0, 0); | - | ||||||||||||
2229 | if (d->navBarVisible) { | - | ||||||||||||
2230 | int headerH = d->navBarBackground->sizeHint().height(); | - | ||||||||||||
2231 | int headerW = 0; | - | ||||||||||||
2232 | - | |||||||||||||
2233 | headerW += d->prevMonth->sizeHint().width(); | - | ||||||||||||
2234 | headerW += d->nextMonth->sizeHint().width(); | - | ||||||||||||
2235 | - | |||||||||||||
2236 | QFontMetrics fm = d->monthButton->fontMetrics(); | - | ||||||||||||
2237 | int monthW = 0; | - | ||||||||||||
2238 | for (int i = 1; i < 12; i++) { | - | ||||||||||||
2239 | QString monthName = locale().standaloneMonthName(i, QLocale::LongFormat); | - | ||||||||||||
2240 | monthW = qMax(monthW, fm.boundingRect(monthName).width()); | - | ||||||||||||
2241 | } | - | ||||||||||||
2242 | const int buttonDecoMargin = d->monthButton->sizeHint().width() - fm.boundingRect(d->monthButton->text()).width(); | - | ||||||||||||
2243 | headerW += monthW + buttonDecoMargin; | - | ||||||||||||
2244 | - | |||||||||||||
2245 | fm = d->yearButton->fontMetrics(); | - | ||||||||||||
2246 | headerW += fm.boundingRect(QLatin1String("5555")).width() + buttonDecoMargin; | - | ||||||||||||
2247 | - | |||||||||||||
2248 | headerSize = QSize(headerW, headerH); | - | ||||||||||||
2249 | } | - | ||||||||||||
2250 | w *= cols; | - | ||||||||||||
2251 | w = qMax(headerSize.width(), w); | - | ||||||||||||
2252 | h = (h * rows) + headerSize.height(); | - | ||||||||||||
2253 | QMargins cm = contentsMargins(); | - | ||||||||||||
2254 | w += cm.left() + cm.right(); | - | ||||||||||||
2255 | h += cm.top() + cm.bottom(); | - | ||||||||||||
2256 | d->cachedSizeHint = QSize(w, h); | - | ||||||||||||
2257 | return d->cachedSizeHint; | - | ||||||||||||
2258 | } | - | ||||||||||||
2259 | - | |||||||||||||
2260 | /*! | - | ||||||||||||
2261 | Paints the cell specified by the given \a date, using the given \a painter and \a rect. | - | ||||||||||||
2262 | */ | - | ||||||||||||
2263 | - | |||||||||||||
2264 | void QCalendarWidget::paintCell(QPainter *painter, const QRect &rect, const QDate &date) const | - | ||||||||||||
2265 | { | - | ||||||||||||
2266 | Q_D(const QCalendarWidget); | - | ||||||||||||
2267 | d->m_delegate->paintCell(painter, rect, date); | - | ||||||||||||
2268 | } | - | ||||||||||||
2269 | - | |||||||||||||
2270 | /*! | - | ||||||||||||
2271 | \property QCalendarWidget::selectedDate | - | ||||||||||||
2272 | \brief the currently selected date. | - | ||||||||||||
2273 | - | |||||||||||||
2274 | The selected date must be within the date range specified by the | - | ||||||||||||
2275 | minimumDate and maximumDate properties. By default, the selected | - | ||||||||||||
2276 | date is the current date. | - | ||||||||||||
2277 | - | |||||||||||||
2278 | \sa setDateRange() | - | ||||||||||||
2279 | */ | - | ||||||||||||
2280 | - | |||||||||||||
2281 | QDate QCalendarWidget::selectedDate() const | - | ||||||||||||
2282 | { | - | ||||||||||||
2283 | Q_D(const QCalendarWidget); | - | ||||||||||||
2284 | return d->m_model->m_date; | - | ||||||||||||
2285 | } | - | ||||||||||||
2286 | - | |||||||||||||
2287 | void QCalendarWidget::setSelectedDate(const QDate &date) | - | ||||||||||||
2288 | { | - | ||||||||||||
2289 | Q_D(QCalendarWidget); | - | ||||||||||||
2290 | if (d->m_model->m_date == date && date == d->getCurrentDate()) | - | ||||||||||||
2291 | return; | - | ||||||||||||
2292 | - | |||||||||||||
2293 | if (!date.isValid()) | - | ||||||||||||
2294 | return; | - | ||||||||||||
2295 | - | |||||||||||||
2296 | d->m_model->setDate(date); | - | ||||||||||||
2297 | d->update(); | - | ||||||||||||
2298 | QDate newDate = d->m_model->m_date; | - | ||||||||||||
2299 | d->showMonth(newDate.year(), newDate.month()); | - | ||||||||||||
2300 | emit selectionChanged(); | - | ||||||||||||
2301 | } | - | ||||||||||||
2302 | - | |||||||||||||
2303 | /*! | - | ||||||||||||
2304 | Returns the year of the currently displayed month. Months are | - | ||||||||||||
2305 | numbered from 1 to 12. | - | ||||||||||||
2306 | - | |||||||||||||
2307 | \sa monthShown(), setCurrentPage() | - | ||||||||||||
2308 | */ | - | ||||||||||||
2309 | - | |||||||||||||
2310 | int QCalendarWidget::yearShown() const | - | ||||||||||||
2311 | { | - | ||||||||||||
2312 | Q_D(const QCalendarWidget); | - | ||||||||||||
2313 | return d->m_model->m_shownYear; | - | ||||||||||||
2314 | } | - | ||||||||||||
2315 | - | |||||||||||||
2316 | /*! | - | ||||||||||||
2317 | Returns the currently displayed month. Months are numbered from 1 to | - | ||||||||||||
2318 | 12. | - | ||||||||||||
2319 | - | |||||||||||||
2320 | \sa yearShown(), setCurrentPage() | - | ||||||||||||
2321 | */ | - | ||||||||||||
2322 | - | |||||||||||||
2323 | int QCalendarWidget::monthShown() const | - | ||||||||||||
2324 | { | - | ||||||||||||
2325 | Q_D(const QCalendarWidget); | - | ||||||||||||
2326 | return d->m_model->m_shownMonth; | - | ||||||||||||
2327 | } | - | ||||||||||||
2328 | - | |||||||||||||
2329 | /*! | - | ||||||||||||
2330 | Displays the given \a month of the given \a year without changing | - | ||||||||||||
2331 | the selected date. Use the setSelectedDate() function to alter the | - | ||||||||||||
2332 | selected date. | - | ||||||||||||
2333 | - | |||||||||||||
2334 | The currently displayed month and year can be retrieved using the | - | ||||||||||||
2335 | monthShown() and yearShown() functions respectively. | - | ||||||||||||
2336 | - | |||||||||||||
2337 | \sa yearShown(), monthShown(), showPreviousMonth(), showNextMonth(), | - | ||||||||||||
2338 | showPreviousYear(), showNextYear() | - | ||||||||||||
2339 | */ | - | ||||||||||||
2340 | - | |||||||||||||
2341 | void QCalendarWidget::setCurrentPage(int year, int month) | - | ||||||||||||
2342 | { | - | ||||||||||||
2343 | Q_D(QCalendarWidget); | - | ||||||||||||
2344 | QDate currentDate = d->getCurrentDate(); | - | ||||||||||||
2345 | int day = currentDate.day(); | - | ||||||||||||
2346 | int daysInMonths = QDate(year, month, 1).daysInMonth(); | - | ||||||||||||
2347 | if (day > daysInMonths) | - | ||||||||||||
2348 | day = daysInMonths; | - | ||||||||||||
2349 | - | |||||||||||||
2350 | d->showMonth(year, month); | - | ||||||||||||
2351 | - | |||||||||||||
2352 | QDate newDate(year, month, day); | - | ||||||||||||
2353 | int row = -1, col = -1; | - | ||||||||||||
2354 | d->m_model->cellForDate(newDate, &row, &col); | - | ||||||||||||
2355 | if (row != -1 && col != -1) { | - | ||||||||||||
2356 | d->m_view->selectionModel()->setCurrentIndex(d->m_model->index(row, col), | - | ||||||||||||
2357 | QItemSelectionModel::NoUpdate); | - | ||||||||||||
2358 | } | - | ||||||||||||
2359 | } | - | ||||||||||||
2360 | - | |||||||||||||
2361 | /*! | - | ||||||||||||
2362 | Shows the next month relative to the currently displayed | - | ||||||||||||
2363 | month. Note that the selected date is not changed. | - | ||||||||||||
2364 | - | |||||||||||||
2365 | \sa showPreviousMonth(), setCurrentPage(), setSelectedDate() | - | ||||||||||||
2366 | */ | - | ||||||||||||
2367 | - | |||||||||||||
2368 | void QCalendarWidget::showNextMonth() | - | ||||||||||||
2369 | { | - | ||||||||||||
2370 | int year = yearShown(); | - | ||||||||||||
2371 | int month = monthShown(); | - | ||||||||||||
2372 | if (month == 12) { | - | ||||||||||||
2373 | ++year; | - | ||||||||||||
2374 | month = 1; | - | ||||||||||||
2375 | } else { | - | ||||||||||||
2376 | ++month; | - | ||||||||||||
2377 | } | - | ||||||||||||
2378 | setCurrentPage(year, month); | - | ||||||||||||
2379 | } | - | ||||||||||||
2380 | - | |||||||||||||
2381 | /*! | - | ||||||||||||
2382 | Shows the previous month relative to the currently displayed | - | ||||||||||||
2383 | month. Note that the selected date is not changed. | - | ||||||||||||
2384 | - | |||||||||||||
2385 | \sa showNextMonth(), setCurrentPage(), setSelectedDate() | - | ||||||||||||
2386 | */ | - | ||||||||||||
2387 | - | |||||||||||||
2388 | void QCalendarWidget::showPreviousMonth() | - | ||||||||||||
2389 | { | - | ||||||||||||
2390 | int year = yearShown(); | - | ||||||||||||
2391 | int month = monthShown(); | - | ||||||||||||
2392 | if (month == 1) { | - | ||||||||||||
2393 | --year; | - | ||||||||||||
2394 | month = 12; | - | ||||||||||||
2395 | } else { | - | ||||||||||||
2396 | --month; | - | ||||||||||||
2397 | } | - | ||||||||||||
2398 | setCurrentPage(year, month); | - | ||||||||||||
2399 | } | - | ||||||||||||
2400 | - | |||||||||||||
2401 | /*! | - | ||||||||||||
2402 | Shows the currently displayed month in the \e next year relative | - | ||||||||||||
2403 | to the currently displayed year. Note that the selected date is | - | ||||||||||||
2404 | not changed. | - | ||||||||||||
2405 | - | |||||||||||||
2406 | \sa showPreviousYear(), setCurrentPage(), setSelectedDate() | - | ||||||||||||
2407 | */ | - | ||||||||||||
2408 | - | |||||||||||||
2409 | void QCalendarWidget::showNextYear() | - | ||||||||||||
2410 | { | - | ||||||||||||
2411 | int year = yearShown(); | - | ||||||||||||
2412 | int month = monthShown(); | - | ||||||||||||
2413 | ++year; | - | ||||||||||||
2414 | setCurrentPage(year, month); | - | ||||||||||||
2415 | } | - | ||||||||||||
2416 | - | |||||||||||||
2417 | /*! | - | ||||||||||||
2418 | Shows the currently displayed month in the \e previous year | - | ||||||||||||
2419 | relative to the currently displayed year. Note that the selected | - | ||||||||||||
2420 | date is not changed. | - | ||||||||||||
2421 | - | |||||||||||||
2422 | \sa showNextYear(), setCurrentPage(), setSelectedDate() | - | ||||||||||||
2423 | */ | - | ||||||||||||
2424 | - | |||||||||||||
2425 | void QCalendarWidget::showPreviousYear() | - | ||||||||||||
2426 | { | - | ||||||||||||
2427 | int year = yearShown(); | - | ||||||||||||
2428 | int month = monthShown(); | - | ||||||||||||
2429 | --year; | - | ||||||||||||
2430 | setCurrentPage(year, month); | - | ||||||||||||
2431 | } | - | ||||||||||||
2432 | - | |||||||||||||
2433 | /*! | - | ||||||||||||
2434 | Shows the month of the selected date. | - | ||||||||||||
2435 | - | |||||||||||||
2436 | \sa selectedDate(), setCurrentPage() | - | ||||||||||||
2437 | */ | - | ||||||||||||
2438 | void QCalendarWidget::showSelectedDate() | - | ||||||||||||
2439 | { | - | ||||||||||||
2440 | QDate currentDate = selectedDate(); | - | ||||||||||||
2441 | setCurrentPage(currentDate.year(), currentDate.month()); | - | ||||||||||||
2442 | } | - | ||||||||||||
2443 | - | |||||||||||||
2444 | /*! | - | ||||||||||||
2445 | Shows the month of the today's date. | - | ||||||||||||
2446 | - | |||||||||||||
2447 | \sa selectedDate(), setCurrentPage() | - | ||||||||||||
2448 | */ | - | ||||||||||||
2449 | void QCalendarWidget::showToday() | - | ||||||||||||
2450 | { | - | ||||||||||||
2451 | QDate currentDate = QDate::currentDate(); | - | ||||||||||||
2452 | setCurrentPage(currentDate.year(), currentDate.month()); | - | ||||||||||||
2453 | } | - | ||||||||||||
2454 | - | |||||||||||||
2455 | /*! | - | ||||||||||||
2456 | \property QCalendarWidget::minimumDate | - | ||||||||||||
2457 | \brief the minimum date of the currently specified date range. | - | ||||||||||||
2458 | - | |||||||||||||
2459 | The user will not be able to select a date that is before the | - | ||||||||||||
2460 | currently set minimum date. | - | ||||||||||||
2461 | - | |||||||||||||
2462 | \table | - | ||||||||||||
2463 | \row | - | ||||||||||||
2464 | \li \image qcalendarwidget-minimum.png | - | ||||||||||||
2465 | \row | - | ||||||||||||
2466 | \li | - | ||||||||||||
2467 | \snippet code/src_gui_widgets_qcalendarwidget.cpp 1 | - | ||||||||||||
2468 | \endtable | - | ||||||||||||
2469 | - | |||||||||||||
2470 | By default, the minimum date is the earliest date that the QDate | - | ||||||||||||
2471 | class can handle. | - | ||||||||||||
2472 | - | |||||||||||||
2473 | When setting a minimum date, the maximumDate and selectedDate | - | ||||||||||||
2474 | properties are adjusted if the selection range becomes invalid. If | - | ||||||||||||
2475 | the provided date is not a valid QDate object, the | - | ||||||||||||
2476 | setMinimumDate() function does nothing. | - | ||||||||||||
2477 | - | |||||||||||||
2478 | \sa setDateRange() | - | ||||||||||||
2479 | */ | - | ||||||||||||
2480 | - | |||||||||||||
2481 | QDate QCalendarWidget::minimumDate() const | - | ||||||||||||
2482 | { | - | ||||||||||||
2483 | Q_D(const QCalendarWidget); | - | ||||||||||||
2484 | return d->m_model->m_minimumDate; | - | ||||||||||||
2485 | } | - | ||||||||||||
2486 | - | |||||||||||||
2487 | void QCalendarWidget::setMinimumDate(const QDate &date) | - | ||||||||||||
2488 | { | - | ||||||||||||
2489 | Q_D(QCalendarWidget); | - | ||||||||||||
2490 | if (!date.isValid() || d->m_model->m_minimumDate == date) | - | ||||||||||||
2491 | return; | - | ||||||||||||
2492 | - | |||||||||||||
2493 | QDate oldDate = d->m_model->m_date; | - | ||||||||||||
2494 | d->m_model->setMinimumDate(date); | - | ||||||||||||
2495 | d->yearEdit->setMinimum(d->m_model->m_minimumDate.year()); | - | ||||||||||||
2496 | d->updateMonthMenu(); | - | ||||||||||||
2497 | QDate newDate = d->m_model->m_date; | - | ||||||||||||
2498 | if (oldDate != newDate) { | - | ||||||||||||
2499 | d->update(); | - | ||||||||||||
2500 | d->showMonth(newDate.year(), newDate.month()); | - | ||||||||||||
2501 | d->m_navigator->setDate(newDate); | - | ||||||||||||
2502 | emit selectionChanged(); | - | ||||||||||||
2503 | } | - | ||||||||||||
2504 | } | - | ||||||||||||
2505 | - | |||||||||||||
2506 | /*! | - | ||||||||||||
2507 | \property QCalendarWidget::maximumDate | - | ||||||||||||
2508 | \brief the maximum date of the currently specified date range. | - | ||||||||||||
2509 | - | |||||||||||||
2510 | The user will not be able to select a date which is after the | - | ||||||||||||
2511 | currently set maximum date. | - | ||||||||||||
2512 | - | |||||||||||||
2513 | \table | - | ||||||||||||
2514 | \row | - | ||||||||||||
2515 | \li \image qcalendarwidget-maximum.png | - | ||||||||||||
2516 | \row | - | ||||||||||||
2517 | \li | - | ||||||||||||
2518 | \snippet code/src_gui_widgets_qcalendarwidget.cpp 2 | - | ||||||||||||
2519 | \endtable | - | ||||||||||||
2520 | - | |||||||||||||
2521 | By default, the maximum date is the last day the QDate class can | - | ||||||||||||
2522 | handle. | - | ||||||||||||
2523 | - | |||||||||||||
2524 | When setting a maximum date, the minimumDate and selectedDate | - | ||||||||||||
2525 | properties are adjusted if the selection range becomes invalid. If | - | ||||||||||||
2526 | the provided date is not a valid QDate object, the | - | ||||||||||||
2527 | setMaximumDate() function does nothing. | - | ||||||||||||
2528 | - | |||||||||||||
2529 | \sa setDateRange() | - | ||||||||||||
2530 | */ | - | ||||||||||||
2531 | - | |||||||||||||
2532 | QDate QCalendarWidget::maximumDate() const | - | ||||||||||||
2533 | { | - | ||||||||||||
2534 | Q_D(const QCalendarWidget); | - | ||||||||||||
2535 | return d->m_model->m_maximumDate; | - | ||||||||||||
2536 | } | - | ||||||||||||
2537 | - | |||||||||||||
2538 | void QCalendarWidget::setMaximumDate(const QDate &date) | - | ||||||||||||
2539 | { | - | ||||||||||||
2540 | Q_D(QCalendarWidget); | - | ||||||||||||
2541 | if (!date.isValid() || d->m_model->m_maximumDate == date) | - | ||||||||||||
2542 | return; | - | ||||||||||||
2543 | - | |||||||||||||
2544 | QDate oldDate = d->m_model->m_date; | - | ||||||||||||
2545 | d->m_model->setMaximumDate(date); | - | ||||||||||||
2546 | d->yearEdit->setMaximum(d->m_model->m_maximumDate.year()); | - | ||||||||||||
2547 | d->updateMonthMenu(); | - | ||||||||||||
2548 | QDate newDate = d->m_model->m_date; | - | ||||||||||||
2549 | if (oldDate != newDate) { | - | ||||||||||||
2550 | d->update(); | - | ||||||||||||
2551 | d->showMonth(newDate.year(), newDate.month()); | - | ||||||||||||
2552 | d->m_navigator->setDate(newDate); | - | ||||||||||||
2553 | emit selectionChanged(); | - | ||||||||||||
2554 | } | - | ||||||||||||
2555 | } | - | ||||||||||||
2556 | - | |||||||||||||
2557 | /*! | - | ||||||||||||
2558 | Defines a date range by setting the minimumDate and maximumDate | - | ||||||||||||
2559 | properties. | - | ||||||||||||
2560 | - | |||||||||||||
2561 | The date range restricts the user selection, i.e. the user can | - | ||||||||||||
2562 | only select dates within the specified date range. Note that | - | ||||||||||||
2563 | - | |||||||||||||
2564 | \snippet code/src_gui_widgets_qcalendarwidget.cpp 3 | - | ||||||||||||
2565 | - | |||||||||||||
2566 | is analogous to | - | ||||||||||||
2567 | - | |||||||||||||
2568 | \snippet code/src_gui_widgets_qcalendarwidget.cpp 4 | - | ||||||||||||
2569 | - | |||||||||||||
2570 | If either the \a min or \a max parameters are not valid QDate | - | ||||||||||||
2571 | objects, this function does nothing. | - | ||||||||||||
2572 | - | |||||||||||||
2573 | \sa setMinimumDate(), setMaximumDate() | - | ||||||||||||
2574 | */ | - | ||||||||||||
2575 | - | |||||||||||||
2576 | void QCalendarWidget::setDateRange(const QDate &min, const QDate &max) | - | ||||||||||||
2577 | { | - | ||||||||||||
2578 | Q_D(QCalendarWidget); | - | ||||||||||||
2579 | if (d->m_model->m_minimumDate == min && d->m_model->m_maximumDate == max) | - | ||||||||||||
2580 | return; | - | ||||||||||||
2581 | if (!min.isValid() || !max.isValid()) | - | ||||||||||||
2582 | return; | - | ||||||||||||
2583 | - | |||||||||||||
2584 | QDate oldDate = d->m_model->m_date; | - | ||||||||||||
2585 | d->m_model->setRange(min, max); | - | ||||||||||||
2586 | d->yearEdit->setMinimum(d->m_model->m_minimumDate.year()); | - | ||||||||||||
2587 | d->yearEdit->setMaximum(d->m_model->m_maximumDate.year()); | - | ||||||||||||
2588 | d->updateMonthMenu(); | - | ||||||||||||
2589 | QDate newDate = d->m_model->m_date; | - | ||||||||||||
2590 | if (oldDate != newDate) { | - | ||||||||||||
2591 | d->update(); | - | ||||||||||||
2592 | d->showMonth(newDate.year(), newDate.month()); | - | ||||||||||||
2593 | d->m_navigator->setDate(newDate); | - | ||||||||||||
2594 | emit selectionChanged(); | - | ||||||||||||
2595 | } | - | ||||||||||||
2596 | } | - | ||||||||||||
2597 | - | |||||||||||||
2598 | - | |||||||||||||
2599 | /*! \enum QCalendarWidget::HorizontalHeaderFormat | - | ||||||||||||
2600 | - | |||||||||||||
2601 | This enum type defines the various formats the horizontal header can display. | - | ||||||||||||
2602 | - | |||||||||||||
2603 | \value SingleLetterDayNames The header displays a single letter abbreviation for day names (e.g. M for Monday). | - | ||||||||||||
2604 | \value ShortDayNames The header displays a short abbreviation for day names (e.g. Mon for Monday). | - | ||||||||||||
2605 | \value LongDayNames The header displays complete day names (e.g. Monday). | - | ||||||||||||
2606 | \value NoHorizontalHeader The header is hidden. | - | ||||||||||||
2607 | - | |||||||||||||
2608 | \sa horizontalHeaderFormat(), VerticalHeaderFormat | - | ||||||||||||
2609 | */ | - | ||||||||||||
2610 | - | |||||||||||||
2611 | /*! | - | ||||||||||||
2612 | \property QCalendarWidget::horizontalHeaderFormat | - | ||||||||||||
2613 | \brief the format of the horizontal header. | - | ||||||||||||
2614 | - | |||||||||||||
2615 | The default value is QCalendarWidget::ShortDayNames. | - | ||||||||||||
2616 | */ | - | ||||||||||||
2617 | - | |||||||||||||
2618 | void QCalendarWidget::setHorizontalHeaderFormat(QCalendarWidget::HorizontalHeaderFormat format) | - | ||||||||||||
2619 | { | - | ||||||||||||
2620 | Q_D(QCalendarWidget); | - | ||||||||||||
2621 | if (d->m_model->m_horizontalHeaderFormat == format) | - | ||||||||||||
2622 | return; | - | ||||||||||||
2623 | - | |||||||||||||
2624 | d->m_model->setHorizontalHeaderFormat(format); | - | ||||||||||||
2625 | d->cachedSizeHint = QSize(); | - | ||||||||||||
2626 | d->m_view->viewport()->update(); | - | ||||||||||||
2627 | d->m_view->updateGeometry(); | - | ||||||||||||
2628 | } | - | ||||||||||||
2629 | - | |||||||||||||
2630 | QCalendarWidget::HorizontalHeaderFormat QCalendarWidget::horizontalHeaderFormat() const | - | ||||||||||||
2631 | { | - | ||||||||||||
2632 | Q_D(const QCalendarWidget); | - | ||||||||||||
2633 | return d->m_model->m_horizontalHeaderFormat; | - | ||||||||||||
2634 | } | - | ||||||||||||
2635 | - | |||||||||||||
2636 | - | |||||||||||||
2637 | /*! | - | ||||||||||||
2638 | \enum QCalendarWidget::VerticalHeaderFormat | - | ||||||||||||
2639 | - | |||||||||||||
2640 | This enum type defines the various formats the vertical header can display. | - | ||||||||||||
2641 | - | |||||||||||||
2642 | \value ISOWeekNumbers The header displays ISO week numbers as described by \l QDate::weekNumber(). | - | ||||||||||||
2643 | \value NoVerticalHeader The header is hidden. | - | ||||||||||||
2644 | - | |||||||||||||
2645 | \sa verticalHeaderFormat(), HorizontalHeaderFormat | - | ||||||||||||
2646 | */ | - | ||||||||||||
2647 | - | |||||||||||||
2648 | /*! | - | ||||||||||||
2649 | \property QCalendarWidget::verticalHeaderFormat | - | ||||||||||||
2650 | \brief the format of the vertical header. | - | ||||||||||||
2651 | - | |||||||||||||
2652 | The default value is QCalendarWidget::ISOWeekNumber. | - | ||||||||||||
2653 | */ | - | ||||||||||||
2654 | - | |||||||||||||
2655 | QCalendarWidget::VerticalHeaderFormat QCalendarWidget::verticalHeaderFormat() const | - | ||||||||||||
2656 | { | - | ||||||||||||
2657 | Q_D(const QCalendarWidget); | - | ||||||||||||
2658 | bool shown = d->m_model->weekNumbersShown(); | - | ||||||||||||
2659 | if (shown) | - | ||||||||||||
2660 | return QCalendarWidget::ISOWeekNumbers; | - | ||||||||||||
2661 | return QCalendarWidget::NoVerticalHeader; | - | ||||||||||||
2662 | } | - | ||||||||||||
2663 | - | |||||||||||||
2664 | void QCalendarWidget::setVerticalHeaderFormat(QCalendarWidget::VerticalHeaderFormat format) | - | ||||||||||||
2665 | { | - | ||||||||||||
2666 | Q_D(QCalendarWidget); | - | ||||||||||||
2667 | bool show = false; | - | ||||||||||||
2668 | if (format == QCalendarWidget::ISOWeekNumbers) | - | ||||||||||||
2669 | show = true; | - | ||||||||||||
2670 | if (d->m_model->weekNumbersShown() == show) | - | ||||||||||||
2671 | return; | - | ||||||||||||
2672 | d->m_model->setWeekNumbersShown(show); | - | ||||||||||||
2673 | d->cachedSizeHint = QSize(); | - | ||||||||||||
2674 | d->m_view->viewport()->update(); | - | ||||||||||||
2675 | d->m_view->updateGeometry(); | - | ||||||||||||
2676 | } | - | ||||||||||||
2677 | - | |||||||||||||
2678 | /*! | - | ||||||||||||
2679 | \property QCalendarWidget::gridVisible | - | ||||||||||||
2680 | \brief whether the table grid is displayed. | - | ||||||||||||
2681 | - | |||||||||||||
2682 | \table | - | ||||||||||||
2683 | \row | - | ||||||||||||
2684 | \li \inlineimage qcalendarwidget-grid.png | - | ||||||||||||
2685 | \row | - | ||||||||||||
2686 | \li | - | ||||||||||||
2687 | \snippet code/src_gui_widgets_qcalendarwidget.cpp 5 | - | ||||||||||||
2688 | \endtable | - | ||||||||||||
2689 | - | |||||||||||||
2690 | The default value is false. | - | ||||||||||||
2691 | */ | - | ||||||||||||
2692 | - | |||||||||||||
2693 | bool QCalendarWidget::isGridVisible() const | - | ||||||||||||
2694 | { | - | ||||||||||||
2695 | Q_D(const QCalendarWidget); | - | ||||||||||||
2696 | return d->m_view->showGrid(); | - | ||||||||||||
2697 | } | - | ||||||||||||
2698 | - | |||||||||||||
2699 | void QCalendarWidget::setGridVisible(bool show) | - | ||||||||||||
2700 | { | - | ||||||||||||
2701 | Q_D(QCalendarWidget); | - | ||||||||||||
2702 | d->m_view->setShowGrid(show); | - | ||||||||||||
2703 | d->cachedSizeHint = QSize(); | - | ||||||||||||
2704 | d->m_view->viewport()->update(); | - | ||||||||||||
2705 | d->m_view->updateGeometry(); | - | ||||||||||||
2706 | } | - | ||||||||||||
2707 | - | |||||||||||||
2708 | /*! | - | ||||||||||||
2709 | \property QCalendarWidget::selectionMode | - | ||||||||||||
2710 | \brief the type of selection the user can make in the calendar | - | ||||||||||||
2711 | - | |||||||||||||
2712 | When this property is set to SingleSelection, the user can select a date | - | ||||||||||||
2713 | within the minimum and maximum allowed dates, using either the mouse or | - | ||||||||||||
2714 | the keyboard. | - | ||||||||||||
2715 | - | |||||||||||||
2716 | When the property is set to NoSelection, the user will be unable to select | - | ||||||||||||
2717 | dates, but they can still be selected programmatically. Note that the date | - | ||||||||||||
2718 | that is selected when the property is set to NoSelection will still be | - | ||||||||||||
2719 | the selected date of the calendar. | - | ||||||||||||
2720 | - | |||||||||||||
2721 | The default value is SingleSelection. | - | ||||||||||||
2722 | */ | - | ||||||||||||
2723 | - | |||||||||||||
2724 | QCalendarWidget::SelectionMode QCalendarWidget::selectionMode() const | - | ||||||||||||
2725 | { | - | ||||||||||||
2726 | Q_D(const QCalendarWidget); | - | ||||||||||||
2727 | return d->m_view->readOnly ? QCalendarWidget::NoSelection : QCalendarWidget::SingleSelection; | - | ||||||||||||
2728 | } | - | ||||||||||||
2729 | - | |||||||||||||
2730 | void QCalendarWidget::setSelectionMode(SelectionMode mode) | - | ||||||||||||
2731 | { | - | ||||||||||||
2732 | Q_D(QCalendarWidget); | - | ||||||||||||
2733 | d->m_view->readOnly = (mode == QCalendarWidget::NoSelection); | - | ||||||||||||
2734 | d->setNavigatorEnabled(isDateEditEnabled() && (selectionMode() != QCalendarWidget::NoSelection)); | - | ||||||||||||
2735 | d->update(); | - | ||||||||||||
2736 | } | - | ||||||||||||
2737 | - | |||||||||||||
2738 | /*! | - | ||||||||||||
2739 | \property QCalendarWidget::firstDayOfWeek | - | ||||||||||||
2740 | \brief a value identifying the day displayed in the first column. | - | ||||||||||||
2741 | - | |||||||||||||
2742 | By default, the day displayed in the first column | - | ||||||||||||
2743 | is the first day of the week for the calendar's locale. | - | ||||||||||||
2744 | */ | - | ||||||||||||
2745 | - | |||||||||||||
2746 | void QCalendarWidget::setFirstDayOfWeek(Qt::DayOfWeek dayOfWeek) | - | ||||||||||||
2747 | { | - | ||||||||||||
2748 | Q_D(QCalendarWidget); | - | ||||||||||||
2749 | if ((Qt::DayOfWeek)d->m_model->firstColumnDay() == dayOfWeek) | - | ||||||||||||
2750 | return; | - | ||||||||||||
2751 | - | |||||||||||||
2752 | d->m_model->setFirstColumnDay(dayOfWeek); | - | ||||||||||||
2753 | d->update(); | - | ||||||||||||
2754 | } | - | ||||||||||||
2755 | - | |||||||||||||
2756 | Qt::DayOfWeek QCalendarWidget::firstDayOfWeek() const | - | ||||||||||||
2757 | { | - | ||||||||||||
2758 | Q_D(const QCalendarWidget); | - | ||||||||||||
2759 | return (Qt::DayOfWeek)d->m_model->firstColumnDay(); | - | ||||||||||||
2760 | } | - | ||||||||||||
2761 | - | |||||||||||||
2762 | /*! | - | ||||||||||||
2763 | Returns the text char format for rendering the header. | - | ||||||||||||
2764 | */ | - | ||||||||||||
2765 | QTextCharFormat QCalendarWidget::headerTextFormat() const | - | ||||||||||||
2766 | { | - | ||||||||||||
2767 | Q_D(const QCalendarWidget); | - | ||||||||||||
2768 | return d->m_model->m_headerFormat; | - | ||||||||||||
2769 | } | - | ||||||||||||
2770 | - | |||||||||||||
2771 | /*! | - | ||||||||||||
2772 | Sets the text char format for rendering the header to \a format. | - | ||||||||||||
2773 | If you also set a weekday text format, this format's foreground and | - | ||||||||||||
2774 | background color will take precedence over the header's format. | - | ||||||||||||
2775 | The other formatting information will still be decided by | - | ||||||||||||
2776 | the header's format. | - | ||||||||||||
2777 | */ | - | ||||||||||||
2778 | void QCalendarWidget::setHeaderTextFormat(const QTextCharFormat &format) | - | ||||||||||||
2779 | { | - | ||||||||||||
2780 | Q_D(QCalendarWidget); | - | ||||||||||||
2781 | d->m_model->m_headerFormat = format; | - | ||||||||||||
2782 | d->cachedSizeHint = QSize(); | - | ||||||||||||
2783 | d->m_view->viewport()->update(); | - | ||||||||||||
2784 | d->m_view->updateGeometry(); | - | ||||||||||||
2785 | } | - | ||||||||||||
2786 | - | |||||||||||||
2787 | /*! | - | ||||||||||||
2788 | Returns the text char format for rendering of day in the week \a dayOfWeek. | - | ||||||||||||
2789 | - | |||||||||||||
2790 | \sa headerTextFormat() | - | ||||||||||||
2791 | */ | - | ||||||||||||
2792 | QTextCharFormat QCalendarWidget::weekdayTextFormat(Qt::DayOfWeek dayOfWeek) const | - | ||||||||||||
2793 | { | - | ||||||||||||
2794 | Q_D(const QCalendarWidget); | - | ||||||||||||
2795 | return d->m_model->m_dayFormats.value(dayOfWeek); | - | ||||||||||||
2796 | } | - | ||||||||||||
2797 | - | |||||||||||||
2798 | /*! | - | ||||||||||||
2799 | Sets the text char format for rendering of day in the week \a dayOfWeek to \a format. | - | ||||||||||||
2800 | The format will take precedence over the header format in case of foreground | - | ||||||||||||
2801 | and background color. Other text formatting information is taken from the headers format. | - | ||||||||||||
2802 | - | |||||||||||||
2803 | \sa setHeaderTextFormat() | - | ||||||||||||
2804 | */ | - | ||||||||||||
2805 | void QCalendarWidget::setWeekdayTextFormat(Qt::DayOfWeek dayOfWeek, const QTextCharFormat &format) | - | ||||||||||||
2806 | { | - | ||||||||||||
2807 | Q_D(QCalendarWidget); | - | ||||||||||||
2808 | d->m_model->m_dayFormats[dayOfWeek] = format; | - | ||||||||||||
2809 | d->cachedSizeHint = QSize(); | - | ||||||||||||
2810 | d->m_view->viewport()->update(); | - | ||||||||||||
2811 | d->m_view->updateGeometry(); | - | ||||||||||||
2812 | } | - | ||||||||||||
2813 | - | |||||||||||||
2814 | /*! | - | ||||||||||||
2815 | Returns a QMap from QDate to QTextCharFormat showing all dates | - | ||||||||||||
2816 | that use a special format that alters their rendering. | - | ||||||||||||
2817 | */ | - | ||||||||||||
2818 | QMap<QDate, QTextCharFormat> QCalendarWidget::dateTextFormat() const | - | ||||||||||||
2819 | { | - | ||||||||||||
2820 | Q_D(const QCalendarWidget); | - | ||||||||||||
2821 | return d->m_model->m_dateFormats; | - | ||||||||||||
2822 | } | - | ||||||||||||
2823 | - | |||||||||||||
2824 | /*! | - | ||||||||||||
2825 | Returns a QTextCharFormat for \a date. The char format can be be | - | ||||||||||||
2826 | empty if the date is not renderd specially. | - | ||||||||||||
2827 | */ | - | ||||||||||||
2828 | QTextCharFormat QCalendarWidget::dateTextFormat(const QDate &date) const | - | ||||||||||||
2829 | { | - | ||||||||||||
2830 | Q_D(const QCalendarWidget); | - | ||||||||||||
2831 | return d->m_model->m_dateFormats.value(date); | - | ||||||||||||
2832 | } | - | ||||||||||||
2833 | - | |||||||||||||
2834 | /*! | - | ||||||||||||
2835 | Sets the format used to render the given \a date to that specified by \a format. | - | ||||||||||||
2836 | - | |||||||||||||
2837 | If \a date is null, all date formats are cleared. | - | ||||||||||||
2838 | */ | - | ||||||||||||
2839 | void QCalendarWidget::setDateTextFormat(const QDate &date, const QTextCharFormat &format) | - | ||||||||||||
2840 | { | - | ||||||||||||
2841 | Q_D(QCalendarWidget); | - | ||||||||||||
2842 | if (date.isNull()) | - | ||||||||||||
2843 | d->m_model->m_dateFormats.clear(); | - | ||||||||||||
2844 | else | - | ||||||||||||
2845 | d->m_model->m_dateFormats[date] = format; | - | ||||||||||||
2846 | d->m_view->viewport()->update(); | - | ||||||||||||
2847 | d->m_view->updateGeometry(); | - | ||||||||||||
2848 | } | - | ||||||||||||
2849 | - | |||||||||||||
2850 | /*! | - | ||||||||||||
2851 | \property QCalendarWidget::dateEditEnabled | - | ||||||||||||
2852 | \brief whether the date edit popup is enabled | - | ||||||||||||
2853 | \since 4.3 | - | ||||||||||||
2854 | - | |||||||||||||
2855 | If this property is enabled, pressing a non-modifier key will cause a | - | ||||||||||||
2856 | date edit to popup if the calendar widget has focus, allowing the user | - | ||||||||||||
2857 | to specify a date in the form specified by the current locale. | - | ||||||||||||
2858 | - | |||||||||||||
2859 | By default, this property is enabled. | - | ||||||||||||
2860 | - | |||||||||||||
2861 | The date edit is simpler in appearance than QDateEdit, but allows the | - | ||||||||||||
2862 | user to navigate between fields using the left and right cursor keys, | - | ||||||||||||
2863 | increment and decrement individual fields using the up and down cursor | - | ||||||||||||
2864 | keys, and enter values directly using the number keys. | - | ||||||||||||
2865 | - | |||||||||||||
2866 | \sa QCalendarWidget::dateEditAcceptDelay | - | ||||||||||||
2867 | */ | - | ||||||||||||
2868 | bool QCalendarWidget::isDateEditEnabled() const | - | ||||||||||||
2869 | { | - | ||||||||||||
2870 | Q_D(const QCalendarWidget); | - | ||||||||||||
2871 | return d->m_dateEditEnabled; | - | ||||||||||||
2872 | } | - | ||||||||||||
2873 | - | |||||||||||||
2874 | void QCalendarWidget::setDateEditEnabled(bool enable) | - | ||||||||||||
2875 | { | - | ||||||||||||
2876 | Q_D(QCalendarWidget); | - | ||||||||||||
2877 | if (isDateEditEnabled() == enable) | - | ||||||||||||
2878 | return; | - | ||||||||||||
2879 | - | |||||||||||||
2880 | d->m_dateEditEnabled = enable; | - | ||||||||||||
2881 | - | |||||||||||||
2882 | d->setNavigatorEnabled(enable && (selectionMode() != QCalendarWidget::NoSelection)); | - | ||||||||||||
2883 | } | - | ||||||||||||
2884 | - | |||||||||||||
2885 | /*! | - | ||||||||||||
2886 | \property QCalendarWidget::dateEditAcceptDelay | - | ||||||||||||
2887 | \brief the time an inactive date edit is shown before its contents are accepted | - | ||||||||||||
2888 | \since 4.3 | - | ||||||||||||
2889 | - | |||||||||||||
2890 | If the calendar widget's \l{dateEditEnabled}{date edit is enabled}, this | - | ||||||||||||
2891 | property specifies the amount of time (in millseconds) that the date edit | - | ||||||||||||
2892 | remains open after the most recent user input. Once this time has elapsed, | - | ||||||||||||
2893 | the date specified in the date edit is accepted and the popup is closed. | - | ||||||||||||
2894 | - | |||||||||||||
2895 | By default, the delay is defined to be 1500 milliseconds (1.5 seconds). | - | ||||||||||||
2896 | */ | - | ||||||||||||
2897 | int QCalendarWidget::dateEditAcceptDelay() const | - | ||||||||||||
2898 | { | - | ||||||||||||
2899 | Q_D(const QCalendarWidget); | - | ||||||||||||
2900 | return d->m_navigator->dateEditAcceptDelay(); | - | ||||||||||||
2901 | } | - | ||||||||||||
2902 | - | |||||||||||||
2903 | void QCalendarWidget::setDateEditAcceptDelay(int delay) | - | ||||||||||||
2904 | { | - | ||||||||||||
2905 | Q_D(QCalendarWidget); | - | ||||||||||||
2906 | d->m_navigator->setDateEditAcceptDelay(delay); | - | ||||||||||||
2907 | } | - | ||||||||||||
2908 | - | |||||||||||||
2909 | /*! | - | ||||||||||||
2910 | \since 4.4 | - | ||||||||||||
2911 | - | |||||||||||||
2912 | Updates the cell specified by the given \a date unless updates | - | ||||||||||||
2913 | are disabled or the cell is hidden. | - | ||||||||||||
2914 | - | |||||||||||||
2915 | \sa updateCells(), yearShown(), monthShown() | - | ||||||||||||
2916 | */ | - | ||||||||||||
2917 | void QCalendarWidget::updateCell(const QDate &date) | - | ||||||||||||
2918 | { | - | ||||||||||||
2919 | if (Q_UNLIKELY(!date.isValid())())) {
| 0 | ||||||||||||
2920 | qWarning("QCalendarWidget::updateCell: Invalid date"); | - | ||||||||||||
2921 | return; never executed: return; | 0 | ||||||||||||
2922 | } | - | ||||||||||||
2923 | - | |||||||||||||
2924 | if (!isVisible())
| 0 | ||||||||||||
2925 | return; never executed: return; | 0 | ||||||||||||
2926 | - | |||||||||||||
2927 | Q_D(QCalendarWidget); | - | ||||||||||||
2928 | int row, column; | - | ||||||||||||
2929 | d->m_model->cellForDate(date, &row, &column); | - | ||||||||||||
2930 | if (row == -1 || column == -1)
| 0 | ||||||||||||
2931 | return; never executed: return; | 0 | ||||||||||||
2932 | - | |||||||||||||
2933 | QModelIndex modelIndex = d->m_model->index(row, column); | - | ||||||||||||
2934 | if (!modelIndex.isValid())
| 0 | ||||||||||||
2935 | return; never executed: return; | 0 | ||||||||||||
2936 | - | |||||||||||||
2937 | d->m_view->viewport()->update(d->m_view->visualRect(modelIndex)); | - | ||||||||||||
2938 | } never executed: end of block | 0 | ||||||||||||
2939 | - | |||||||||||||
2940 | /*! | - | ||||||||||||
2941 | \since 4.4 | - | ||||||||||||
2942 | - | |||||||||||||
2943 | Updates all visible cells unless updates are disabled. | - | ||||||||||||
2944 | - | |||||||||||||
2945 | \sa updateCell() | - | ||||||||||||
2946 | */ | - | ||||||||||||
2947 | void QCalendarWidget::updateCells() | - | ||||||||||||
2948 | { | - | ||||||||||||
2949 | Q_D(QCalendarWidget); | - | ||||||||||||
2950 | if (isVisible()) | - | ||||||||||||
2951 | d->m_view->viewport()->update(); | - | ||||||||||||
2952 | } | - | ||||||||||||
2953 | - | |||||||||||||
2954 | /*! | - | ||||||||||||
2955 | \fn void QCalendarWidget::selectionChanged() | - | ||||||||||||
2956 | - | |||||||||||||
2957 | This signal is emitted when the currently selected date is | - | ||||||||||||
2958 | changed. | - | ||||||||||||
2959 | - | |||||||||||||
2960 | The currently selected date can be changed by the user using the | - | ||||||||||||
2961 | mouse or keyboard, or by the programmer using setSelectedDate(). | - | ||||||||||||
2962 | - | |||||||||||||
2963 | \sa selectedDate() | - | ||||||||||||
2964 | */ | - | ||||||||||||
2965 | - | |||||||||||||
2966 | /*! | - | ||||||||||||
2967 | \fn void QCalendarWidget::currentPageChanged(int year, int month) | - | ||||||||||||
2968 | - | |||||||||||||
2969 | This signal is emitted when the currently shown month is changed. | - | ||||||||||||
2970 | The new \a year and \a month are passed as parameters. | - | ||||||||||||
2971 | - | |||||||||||||
2972 | \sa setCurrentPage() | - | ||||||||||||
2973 | */ | - | ||||||||||||
2974 | - | |||||||||||||
2975 | /*! | - | ||||||||||||
2976 | \fn void QCalendarWidget::activated(const QDate &date) | - | ||||||||||||
2977 | - | |||||||||||||
2978 | This signal is emitted whenever the user presses the Return or | - | ||||||||||||
2979 | Enter key or double-clicks a \a date in the calendar | - | ||||||||||||
2980 | widget. | - | ||||||||||||
2981 | */ | - | ||||||||||||
2982 | - | |||||||||||||
2983 | /*! | - | ||||||||||||
2984 | \fn void QCalendarWidget::clicked(const QDate &date) | - | ||||||||||||
2985 | - | |||||||||||||
2986 | This signal is emitted when a mouse button is clicked. The date | - | ||||||||||||
2987 | the mouse was clicked on is specified by \a date. The signal is | - | ||||||||||||
2988 | only emitted when clicked on a valid date, e.g., dates are not | - | ||||||||||||
2989 | outside the minimumDate() and maximumDate(). If the selection mode | - | ||||||||||||
2990 | is NoSelection, this signal will not be emitted. | - | ||||||||||||
2991 | - | |||||||||||||
2992 | */ | - | ||||||||||||
2993 | - | |||||||||||||
2994 | /*! | - | ||||||||||||
2995 | \property QCalendarWidget::navigationBarVisible | - | ||||||||||||
2996 | \brief whether the navigation bar is shown or not | - | ||||||||||||
2997 | - | |||||||||||||
2998 | \since 4.3 | - | ||||||||||||
2999 | - | |||||||||||||
3000 | When this property is \c true (the default), the next month, | - | ||||||||||||
3001 | previous month, month selection, year selection controls are | - | ||||||||||||
3002 | shown on top. | - | ||||||||||||
3003 | - | |||||||||||||
3004 | When the property is set to false, these controls are hidden. | - | ||||||||||||
3005 | */ | - | ||||||||||||
3006 | - | |||||||||||||
3007 | bool QCalendarWidget::isNavigationBarVisible() const | - | ||||||||||||
3008 | { | - | ||||||||||||
3009 | Q_D(const QCalendarWidget); | - | ||||||||||||
3010 | return d->navBarVisible; | - | ||||||||||||
3011 | } | - | ||||||||||||
3012 | - | |||||||||||||
3013 | - | |||||||||||||
3014 | void QCalendarWidget::setNavigationBarVisible(bool visible) | - | ||||||||||||
3015 | { | - | ||||||||||||
3016 | Q_D(QCalendarWidget); | - | ||||||||||||
3017 | d->navBarVisible = visible; | - | ||||||||||||
3018 | d->cachedSizeHint = QSize(); | - | ||||||||||||
3019 | d->navBarBackground->setVisible(visible); | - | ||||||||||||
3020 | updateGeometry(); | - | ||||||||||||
3021 | } | - | ||||||||||||
3022 | - | |||||||||||||
3023 | /*! | - | ||||||||||||
3024 | \reimp | - | ||||||||||||
3025 | */ | - | ||||||||||||
3026 | bool QCalendarWidget::event(QEvent *event) | - | ||||||||||||
3027 | { | - | ||||||||||||
3028 | Q_D(QCalendarWidget); | - | ||||||||||||
3029 | switch (event->type()) { | - | ||||||||||||
3030 | case QEvent::LayoutDirectionChange: | - | ||||||||||||
3031 | d->updateButtonIcons(); | - | ||||||||||||
3032 | break; | - | ||||||||||||
3033 | case QEvent::LocaleChange: | - | ||||||||||||
3034 | d->m_model->setFirstColumnDay(locale().firstDayOfWeek()); | - | ||||||||||||
3035 | d->cachedSizeHint = QSize(); | - | ||||||||||||
3036 | d->updateMonthMenuNames(); | - | ||||||||||||
3037 | d->updateNavigationBar(); | - | ||||||||||||
3038 | d->m_view->updateGeometry(); | - | ||||||||||||
3039 | break; | - | ||||||||||||
3040 | case QEvent::FontChange: | - | ||||||||||||
3041 | case QEvent::ApplicationFontChange: | - | ||||||||||||
3042 | d->cachedSizeHint = QSize(); | - | ||||||||||||
3043 | d->m_view->updateGeometry(); | - | ||||||||||||
3044 | break; | - | ||||||||||||
3045 | case QEvent::StyleChange: | - | ||||||||||||
3046 | d->cachedSizeHint = QSize(); | - | ||||||||||||
3047 | d->m_view->updateGeometry(); | - | ||||||||||||
3048 | default: | - | ||||||||||||
3049 | break; | - | ||||||||||||
3050 | } | - | ||||||||||||
3051 | return QWidget::event(event); | - | ||||||||||||
3052 | } | - | ||||||||||||
3053 | - | |||||||||||||
3054 | /*! | - | ||||||||||||
3055 | \reimp | - | ||||||||||||
3056 | */ | - | ||||||||||||
3057 | bool QCalendarWidget::eventFilter(QObject *watched, QEvent *event) | - | ||||||||||||
3058 | { | - | ||||||||||||
3059 | Q_D(QCalendarWidget); | - | ||||||||||||
3060 | if (event->type() == QEvent::MouseButtonPress && d->yearEdit->hasFocus()) { | - | ||||||||||||
3061 | QWidget *tlw = window(); | - | ||||||||||||
3062 | QWidget *widget = static_cast<QWidget*>(watched); | - | ||||||||||||
3063 | //as we have a event filter on the whole application we first make sure that the top level widget | - | ||||||||||||
3064 | //of both this and the watched widget are the same to decide if we should finish the year edition. | - | ||||||||||||
3065 | if (widget->window() == tlw) { | - | ||||||||||||
3066 | QPoint mousePos = widget->mapTo(tlw, static_cast<QMouseEvent *>(event)->pos()); | - | ||||||||||||
3067 | QRect geom = QRect(d->yearEdit->mapTo(tlw, QPoint(0, 0)), d->yearEdit->size()); | - | ||||||||||||
3068 | if (!geom.contains(mousePos)) { | - | ||||||||||||
3069 | event->accept(); | - | ||||||||||||
3070 | d->_q_yearEditingFinished(); | - | ||||||||||||
3071 | setFocus(); | - | ||||||||||||
3072 | return true; | - | ||||||||||||
3073 | } | - | ||||||||||||
3074 | } | - | ||||||||||||
3075 | } | - | ||||||||||||
3076 | return QWidget::eventFilter(watched, event); | - | ||||||||||||
3077 | } | - | ||||||||||||
3078 | - | |||||||||||||
3079 | /*! | - | ||||||||||||
3080 | \reimp | - | ||||||||||||
3081 | */ | - | ||||||||||||
3082 | void QCalendarWidget::mousePressEvent(QMouseEvent *event) | - | ||||||||||||
3083 | { | - | ||||||||||||
3084 | setAttribute(Qt::WA_NoMouseReplay); | - | ||||||||||||
3085 | QWidget::mousePressEvent(event); | - | ||||||||||||
3086 | setFocus(); | - | ||||||||||||
3087 | } | - | ||||||||||||
3088 | - | |||||||||||||
3089 | /*! | - | ||||||||||||
3090 | \reimp | - | ||||||||||||
3091 | */ | - | ||||||||||||
3092 | void QCalendarWidget::resizeEvent(QResizeEvent * event) | - | ||||||||||||
3093 | { | - | ||||||||||||
3094 | Q_D(QCalendarWidget); | - | ||||||||||||
3095 | - | |||||||||||||
3096 | // XXX Should really use a QWidgetStack for yearEdit and yearButton, | - | ||||||||||||
3097 | // XXX here we hide the year edit when the layout is likely to break | - | ||||||||||||
3098 | // XXX the manual positioning of the yearEdit over the yearButton. | - | ||||||||||||
3099 | if(d->yearEdit->isVisible() && event->size().width() != event->oldSize().width()) | - | ||||||||||||
3100 | d->_q_yearEditingFinished(); | - | ||||||||||||
3101 | - | |||||||||||||
3102 | QWidget::resizeEvent(event); | - | ||||||||||||
3103 | } | - | ||||||||||||
3104 | - | |||||||||||||
3105 | /*! | - | ||||||||||||
3106 | \reimp | - | ||||||||||||
3107 | */ | - | ||||||||||||
3108 | void QCalendarWidget::keyPressEvent(QKeyEvent * event) | - | ||||||||||||
3109 | { | - | ||||||||||||
3110 | Q_D(QCalendarWidget); | - | ||||||||||||
3111 | if (d->yearEdit->isVisible()&& event->matches(QKeySequence::Cancel)) { | - | ||||||||||||
3112 | d->yearEdit->setValue(yearShown()); | - | ||||||||||||
3113 | d->_q_yearEditingFinished(); | - | ||||||||||||
3114 | return; | - | ||||||||||||
3115 | } | - | ||||||||||||
3116 | QWidget::keyPressEvent(event); | - | ||||||||||||
3117 | } | - | ||||||||||||
3118 | - | |||||||||||||
3119 | QT_END_NAMESPACE | - | ||||||||||||
3120 | - | |||||||||||||
3121 | #include "qcalendarwidget.moc" | - | ||||||||||||
3122 | #include "moc_qcalendarwidget.cpp" | - | ||||||||||||
3123 | - | |||||||||||||
3124 | #endif //QT_NO_CALENDARWIDGET | - | ||||||||||||
Source code | Switch to Preprocessed file |