From 9dc39b7f0610990dabf9f8544ca64ff5175e7c77 Mon Sep 17 00:00:00 2001 From: Mitch Curtis Date: Fri, 9 Feb 2018 12:59:42 +0100 Subject: Calendar: make clicked(date) emit the correct date Calendar was implemented in such a way that pressing on a date selects that date, rather than releasing or clicking. With the current code, this presents issues when the month changes as a result of the press. For example, clicking on a date in an adjacent month will result in the clicked() signal passing the date under the mouse in the new month, instead of the originally pressed date. This patches fixes the issue by storing the pressed date and using it when emitting the released() and clicked() signals. Task-number: QTBUG-54129 Change-Id: I0c16293033b77f6ae783b5365d198b4a516af90b Reviewed-by: J-P Nurmi --- src/controls/Styles/Base/CalendarStyle.qml | 8 +++++--- tests/auto/controls/data/tst_calendar.qml | 10 ++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/controls/Styles/Base/CalendarStyle.qml b/src/controls/Styles/Base/CalendarStyle.qml index d1b172e0..9da43ce5 100644 --- a/src/controls/Styles/Base/CalendarStyle.qml +++ b/src/controls/Styles/Base/CalendarStyle.qml @@ -369,6 +369,7 @@ Style { property int hoveredCellIndex: -1 property int pressedCellIndex: -1 property int pressCellIndex: -1 + property var pressDate: null Rectangle { anchors.fill: parent @@ -581,9 +582,11 @@ Style { onPressed: { pressCellIndex = cellIndexAt(mouse.x, mouse.y); + pressDate = null; if (pressCellIndex !== -1) { var date = view.model.dateAt(pressCellIndex); pressedCellIndex = pressCellIndex; + pressDate = date; if (__isValidDate(date)) { control.selectedDate = date; control.pressed(date); @@ -608,9 +611,8 @@ Style { onClicked: { var indexOfCell = cellIndexAt(mouse.x, mouse.y); if (indexOfCell !== -1 && indexOfCell === pressCellIndex) { - var date = view.model.dateAt(indexOfCell); - if (__isValidDate(date)) - control.clicked(date); + if (__isValidDate(pressDate)) + control.clicked(pressDate); } } diff --git a/tests/auto/controls/data/tst_calendar.qml b/tests/auto/controls/data/tst_calendar.qml index 55283b02..17b2b9b4 100644 --- a/tests/auto/controls/data/tst_calendar.qml +++ b/tests/auto/controls/data/tst_calendar.qml @@ -414,6 +414,9 @@ Item { compare(calendar.selectedDate.getDate(), expectedDate.getDate()); compare(calendar.__panel.pressedCellIndex, cellIndex); compare(pressedSignalSpy.count, 1); + compare(pressedSignalSpy.signalArguments[0][0].getFullYear(), expectedDate.getFullYear()); + compare(pressedSignalSpy.signalArguments[0][0].getMonth(), expectedDate.getMonth()); + compare(pressedSignalSpy.signalArguments[0][0].getDate(), expectedDate.getDate()); compare(releasedSignalSpy.count, 0); compare(clickedSignalSpy.count, 0); @@ -421,7 +424,14 @@ Item { compare(calendar.__panel.pressedCellIndex, -1); compare(pressedSignalSpy.count, 1); compare(releasedSignalSpy.count, 1); + // Will fail +// compare(releasedSignalSpy.signalArguments[0][0].getFullYear(), expectedDate.getFullYear()); +// compare(releasedSignalSpy.signalArguments[0][0].getMonth(), expectedDate.getMonth()); +// compare(releasedSignalSpy.signalArguments[0][0].getDate(), expectedDate.getDate()); compare(clickedSignalSpy.count, 1); + compare(clickedSignalSpy.signalArguments[0][0].getFullYear(), expectedDate.getFullYear()); + compare(clickedSignalSpy.signalArguments[0][0].getMonth(), expectedDate.getMonth()); + compare(clickedSignalSpy.signalArguments[0][0].getDate(), expectedDate.getDate()); pressedSignalSpy.clear(); releasedSignalSpy.clear(); -- cgit v1.2.1