diff options
author | Mitch Curtis <mitch.curtis@digia.com> | 2014-01-29 11:19:05 +0100 |
---|---|---|
committer | Mitch Curtis <mitch.curtis@digia.com> | 2014-01-30 15:47:23 +0100 |
commit | 7c44e88269847b8cda306ee30ca953c9f206dde0 (patch) | |
tree | 0c13d67235c17159dd4d34f798e1a7ea0fc8bd31 | |
parent | e97d9c8d3dda6c3b906b45d1cb7e190932496c21 (diff) | |
download | qtquickcontrols-7c44e88269847b8cda306ee30ca953c9f206dde0.tar.gz |
Add home, end, page up, page down key navigation.
Change-Id: I3c22288381eb56dfae2a074be56f0d42df78825c
Reviewed-by: Jens Bache-Wiig <jens.bache-wiig@digia.com>
-rw-r--r-- | src/controls/Calendar.qml | 42 | ||||
-rw-r--r-- | tests/auto/controls/data/tst_calendar.qml | 52 |
2 files changed, 89 insertions, 5 deletions
diff --git a/src/controls/Calendar.qml b/src/controls/Calendar.qml index e78734af..7a65abe5 100644 --- a/src/controls/Calendar.qml +++ b/src/controls/Calendar.qml @@ -50,12 +50,13 @@ import QtQuick.Controls.Private 1.0 \ingroup controls \brief Provides a way to select dates from a calendar - Calendar allows selection of dates from a grid of days, similar to a typical - calendar. The selected date can be set through \l selectedDate, or with the - mouse and directional arrow keys. The current month displayed can be changed - by clicking the previous and next month buttons, or by navigating with the - directional keys. + Calendar allows selection of dates from a grid of days, similar to + QCalendarWidget. + The dates on the calendar can be selected with the mouse, or navigated + with the keyboard. + + The selected date can be set through \l selectedDate. A minimum and maximum date can be set through \l minimumDate and \l maximumDate. The earliest minimum date that can be set is 1 January, 1 AD. The latest maximum date that can be set is 25 October, 275759 AD. @@ -341,6 +342,37 @@ Control { calendar.escapePressed(); } + Keys.onPressed: { + if (event.key === Qt.Key_Home) { + var newDate = new Date(calendar.selectedDate); + newDate.setDate(1); + calendar.selectedDate = newDate; + event.accepted = true; + } else if (event.key === Qt.Key_End) { + newDate = new Date(calendar.selectedDate); + newDate.setDate(DateUtils.daysInMonth(newDate)); + calendar.selectedDate = newDate; + event.accepted = true; + } else if (event.key === Qt.Key_PageUp) { + newDate = new Date(calendar.selectedDate); + var oldDay = newDate.getDate(); + // Set the date to the first so we know we can change months without issues. + newDate.setDate(1); + newDate.setMonth(newDate.getMonth() - 1); + newDate.setDate(Math.min(oldDay, DateUtils.daysInMonth(newDate))); + calendar.selectedDate = newDate; + event.accepted = true; + } else if (event.key === Qt.Key_PageDown) { + newDate = new Date(calendar.selectedDate); + oldDay = newDate.getDate(); + newDate.setDate(1); + newDate.setMonth(newDate.getMonth() + 1); + newDate.setDate(Math.min(oldDay, DateUtils.daysInMonth(newDate))); + calendar.selectedDate = newDate; + event.accepted = true; + } + } + Component.onCompleted: { dateChanged(); diff --git a/tests/auto/controls/data/tst_calendar.qml b/tests/auto/controls/data/tst_calendar.qml index 7cb01073..8203345d 100644 --- a/tests/auto/controls/data/tst_calendar.qml +++ b/tests/auto/controls/data/tst_calendar.qml @@ -239,6 +239,58 @@ Item { compare(calendar.selectedDate, new Date(2013, 2, 31), "Pressing the left key on the left edge of the first row should " + "select the last date of the previous month."); + + /* January 2014 January 2014 + M T W T F S S M T W T F S S + 30 31 [1] 2 3 4 5 30 31 1 2 3 4 5 + 6 7 8 9 10 11 12 6 7 8 9 10 11 12 + 13 14 15 16 17 18 19 ==> 13 14 15 16 17 18 19 + 20 21 22 23 24 25 26 20 21 22 23 24 25 26 + 27 28 29 30 31 1 2 27 28 29 30 [31] 1 2 + 3 4 5 6 7 8 9 3 4 5 6 7 8 9 */ + calendar.selectedDate = new Date(2014, 0, 1); + keyPress(Qt.Key_End); + compare(calendar.selectedDate, new Date(2014, 0, 31), + "Pressing the end key should select the last date in the same month."); + + /* January 2014 January 2014 + M T W T F S S M T W T F S S + 30 31 1 2 3 4 5 30 31 [1] 2 3 4 5 + 6 7 8 9 10 11 12 6 7 8 9 10 11 12 + 13 14 15 16 17 18 19 ==> 13 14 15 16 17 18 19 + 20 21 22 23 24 25 26 20 21 22 23 24 25 26 + 27 28 29 30 [31] 1 2 27 28 29 30 31 1 2 + 3 4 5 6 7 8 9 3 4 5 6 7 8 9 */ + calendar.selectedDate = new Date(2014, 0, 31); + keyPress(Qt.Key_Home); + compare(calendar.selectedDate, new Date(2014, 0, 1), + "Pressing the home key should select the first date in the same month."); + + /* December 2013 November 2013 + M T W T F S S M T W T F S S + 25 26 27 28 29 30 1 28 29 30 31 [1] 2 3 + 2 3 4 5 6 7 8 4 5 6 7 8 9 10 + 9 10 11 12 13 14 15 ==> 11 12 13 14 15 16 17 + 16 17 18 19 20 21 22 18 19 20 21 22 23 24 + 23 24 25 26 27 28 29 25 26 27 28 29 [30] 1 + 30 [31] 1 2 3 4 5 2 3 4 5 6 7 8 */ + calendar.selectedDate = new Date(2013, 11, 31); + keyPress(Qt.Key_PageUp); + compare(calendar.selectedDate, new Date(2013, 10, 30), + "Pressing the page up key should select the equivalent date in the previous month."); + + /* November 2013 December 2013 + M T W T F S S M T W T F S S + 28 29 30 31 [1] 2 3 25 26 27 28 29 30 1 + 4 5 6 7 8 9 10 2 3 4 5 6 7 8 + 11 12 13 14 15 16 17 ==> 9 10 11 12 13 14 15 + 18 19 20 21 22 23 24 16 17 18 19 20 21 22 + 25 26 27 28 29 [30] 1 23 24 25 26 27 28 29 + 2 3 4 5 6 7 8 [30] 31 1 2 3 4 5 */ + calendar.selectedDate = new Date(2013, 10, 30); + keyPress(Qt.Key_PageDown); + compare(calendar.selectedDate, new Date(2013, 11, 30), + "Pressing the page down key should select the equivalent date in the next month."); } function test_previousMonth() { |