summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMitch Curtis <mitch.curtis@digia.com>2014-01-29 17:52:28 +0100
committerJens Bache-Wiig <jens.bache-wiig@digia.com>2014-01-31 10:02:32 +0100
commit2a6aff7c8ab37fb71ae4e9547de899fd339d6831 (patch)
treece131f43a283cdeb5d645cc0abf85062b0bdf272
parent7c44e88269847b8cda306ee30ca953c9f206dde0 (diff)
downloadqtquickcontrols-2a6aff7c8ab37fb71ae4e9547de899fd339d6831.tar.gz
Allow keyboard navigation keys to be customized.
This change adds select* functions. These functions encapsulate the common methods of interacting with the calendar via the keyboard, so that it is possible to specify custom navigation keys: Calendar { id: calendar Keys.onLeftPressed: {} Keys.onRightPressed: {} Keys.onUpPressed: {} Keys.onDownPressed: {} Keys.onPressed: { if (event.key === Qt.Key_W) { calendar.selectPreviousWeek(); event.accepted = true; } else if (...) { ... } ... } } Change-Id: Ic82660f657eaadacd9a170baf07efbabf3eda332 Reviewed-by: Liang Qi <liang.qi@digia.com> Reviewed-by: Jens Bache-Wiig <jens.bache-wiig@digia.com>
-rw-r--r--src/controls/Calendar.qml115
-rw-r--r--src/controls/Styles/Base/CalendarStyle.qml4
-rw-r--r--src/controls/Styles/Desktop/CalendarStyle.qml4
-rw-r--r--tests/auto/controls/data/tst_calendar.qml10
4 files changed, 79 insertions, 54 deletions
diff --git a/src/controls/Calendar.qml b/src/controls/Calendar.qml
index 7a65abe5..c87fbd0a 100644
--- a/src/controls/Calendar.qml
+++ b/src/controls/Calendar.qml
@@ -193,8 +193,6 @@ Control {
style: Qt.createComponent(Settings.style + "/CalendarStyle.qml", calendar)
- Keys.forwardTo: [view]
-
/*!
Returns true if \a date is not \c undefined and not less than
\l minimumDate nor greater than \l maximumDate.
@@ -210,17 +208,75 @@ Control {
/*!
Selects the month before the current month in \l selectedDate.
*/
- function previousMonth() {
+ function selectPreviousMonth() {
calendar.selectedDate = DateUtils.setMonth(calendar.selectedDate, calendar.selectedDate.getMonth() - 1);
}
/*!
Selects the month after the current month in \l selectedDate.
*/
- function nextMonth() {
+ function selectNextMonth() {
calendar.selectedDate = DateUtils.setMonth(calendar.selectedDate, calendar.selectedDate.getMonth() + 1);
}
+ /*!
+ Selects the week before the current week in \l selectedDate.
+ */
+ function selectPreviousWeek() {
+ view.moveCurrentIndexUp();
+ calendar.selectedDate = __model.dateAt(view.currentIndex);
+ }
+
+ /*!
+ Selects the week after the current week in \l selectedDate.
+ */
+ function selectNextWeek() {
+ view.moveCurrentIndexDown();
+ calendar.selectedDate = __model.dateAt(view.currentIndex);
+ }
+
+ /*!
+ Selects the first day of the current month in \l selectedDate.
+ */
+ function selectFirstDayOfMonth() {
+ var newDate = new Date(calendar.selectedDate);
+ newDate.setDate(1);
+ calendar.selectedDate = newDate;
+ }
+
+ /*!
+ Selects the last day of the current month in \l selectedDate.
+ */
+ function selectLastDayOfMonth() {
+ var newDate = new Date(calendar.selectedDate);
+ newDate.setDate(DateUtils.daysInMonth(newDate));
+ calendar.selectedDate = newDate;
+ }
+
+ function selectPreviousDay() {
+ if (view.currentIndex != 0) {
+ // Be lazy and let the view determine which index we're moving
+ // to, then we can calculate the date from that.
+ view.moveCurrentIndexLeft();
+ // This will cause the index to be set again (to the same value).
+ calendar.selectedDate = __model.dateAt(view.currentIndex);
+ } else {
+ // We're at the left edge of the calendar on the first row;
+ // this day is the first of the week and the month, so
+ // moving left should go to the last day of the previous month,
+ // rather than do nothing (which is what GridView does when
+ // keyNavigationWraps is false).
+ var newDate = new Date(calendar.selectedDate);
+ newDate.setDate(newDate.getDate() - 1);
+ calendar.selectedDate = newDate;
+ }
+ }
+
+ function selectNextDay() {
+ view.moveCurrentIndexRight();
+ calendar.selectedDate = __model.dateAt(view.currentIndex);
+ }
+
Item {
/*
This is here instead of CalendarStyle, because the interactive
@@ -304,38 +360,22 @@ Control {
readonly property int weeksToShow: 6
+ Keys.forwardTo: [calendar]
+
Keys.onLeftPressed: {
- if (currentIndex != 0) {
- // Be lazy and let the view determine which index we're moving
- // to, then we can calculate the date from that.
- moveCurrentIndexLeft();
- // This will cause the index to be set again (to the same value).
- calendar.selectedDate = model.dateAt(currentIndex);
- } else {
- // We're at the left edge of the calendar on the first row;
- // this day is the first of the week and the month, so
- // moving left should go to the last day of the previous month,
- // rather than do nothing (which is what GridView does when
- // keyNavigationWraps is false).
- var newDate = new Date(calendar.selectedDate);
- newDate.setDate(newDate.getDate() - 1);
- calendar.selectedDate = newDate;
- }
+ calendar.selectPreviousDay();
}
Keys.onUpPressed: {
- moveCurrentIndexUp();
- calendar.selectedDate = model.dateAt(currentIndex);
+ calendar.selectPreviousWeek();
}
Keys.onDownPressed: {
- moveCurrentIndexDown();
- calendar.selectedDate = model.dateAt(currentIndex);
+ calendar.selectNextWeek();
}
Keys.onRightPressed: {
- moveCurrentIndexRight();
- calendar.selectedDate = model.dateAt(currentIndex);
+ calendar.selectNextDay();
}
Keys.onEscapePressed: {
@@ -344,31 +384,16 @@ Control {
Keys.onPressed: {
if (event.key === Qt.Key_Home) {
- var newDate = new Date(calendar.selectedDate);
- newDate.setDate(1);
- calendar.selectedDate = newDate;
+ calendar.selectFirstDayOfMonth();
event.accepted = true;
} else if (event.key === Qt.Key_End) {
- newDate = new Date(calendar.selectedDate);
- newDate.setDate(DateUtils.daysInMonth(newDate));
- calendar.selectedDate = newDate;
+ calendar.selectLastDayOfMonth();
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;
+ calendar.selectPreviousMonth();
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;
+ calendar.selectNextMonth();
event.accepted = true;
}
}
diff --git a/src/controls/Styles/Base/CalendarStyle.qml b/src/controls/Styles/Base/CalendarStyle.qml
index 95bf48dc..51f50080 100644
--- a/src/controls/Styles/Base/CalendarStyle.qml
+++ b/src/controls/Styles/Base/CalendarStyle.qml
@@ -150,7 +150,7 @@ Style {
anchors.leftMargin: (parent.height - height) / 2
iconSource: "images/arrow-left.png"
- onClicked: control.previousMonth()
+ onClicked: control.selectPreviousMonth()
}
Text {
id: dateText
@@ -174,7 +174,7 @@ Style {
anchors.rightMargin: (parent.height - height) / 2
iconSource: "images/arrow-right.png"
- onClicked: control.nextMonth()
+ onClicked: control.selectNextMonth()
}
}
diff --git a/src/controls/Styles/Desktop/CalendarStyle.qml b/src/controls/Styles/Desktop/CalendarStyle.qml
index b6fc68c5..1d600eab 100644
--- a/src/controls/Styles/Desktop/CalendarStyle.qml
+++ b/src/controls/Styles/Desktop/CalendarStyle.qml
@@ -77,7 +77,7 @@ Style {
anchors.leftMargin: (parent.height - height) / 2
iconName: "go-previous-view"
- onClicked: control.previousMonth()
+ onClicked: control.selectPreviousMonth()
}
Text {
id: dateText
@@ -93,7 +93,7 @@ Style {
anchors.rightMargin: (parent.height - height) / 2
iconName: "go-next-view"
- onClicked: control.nextMonth()
+ onClicked: control.selectNextMonth()
}
}
diff --git a/tests/auto/controls/data/tst_calendar.qml b/tests/auto/controls/data/tst_calendar.qml
index 8203345d..bcfaa34d 100644
--- a/tests/auto/controls/data/tst_calendar.qml
+++ b/tests/auto/controls/data/tst_calendar.qml
@@ -293,11 +293,11 @@ Item {
"Pressing the page down key should select the equivalent date in the next month.");
}
- function test_previousMonth() {
+ function test_selectPreviousMonth() {
calendar.selectedDate = new Date(2013, 0, 1);
compare(calendar.selectedDate, new Date(2013, 0, 1));
- calendar.previousMonth();
+ calendar.selectPreviousMonth();
compare(calendar.selectedDate, new Date(2012, 11, 1));
}
@@ -312,14 +312,14 @@ Item {
compare(calendar.selectedDate, new Date(2012, 11, 28));
}
- function test_nextMonth() {
+ function test_selectNextMonth() {
calendar.selectedDate = new Date(2013, 0, 31);
compare(calendar.selectedDate, new Date(2013, 0, 31));
- calendar.nextMonth();
+ calendar.selectNextMonth();
compare(calendar.selectedDate, new Date(2013, 1, 28));
- calendar.nextMonth();
+ calendar.selectNextMonth();
compare(calendar.selectedDate, new Date(2013, 2, 28));
}