diff options
author | Mitch Curtis <mitch.curtis@digia.com> | 2014-02-13 18:07:30 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2014-02-15 11:28:46 +0100 |
commit | e70ba908b753ea9dcef7c5931382a39b9e8b17d9 (patch) | |
tree | 0cabf2d425699d2527924428c5daed52521f0b89 | |
parent | fa001b41c67784c97110452c5800bef960825264 (diff) | |
download | qtquickcontrols-e70ba908b753ea9dcef7c5931382a39b9e8b17d9.tar.gz |
Use childAt() instead of cellIndexAt().
Change-Id: I74ee716620d31363bccaade89d51aa23d3b39830
Reviewed-by: Gabriel de Dietrich <gabriel.dedietrich@digia.com>
-rw-r--r-- | src/controls/Private/CalendarUtils.js | 20 | ||||
-rw-r--r-- | src/controls/Styles/Base/CalendarStyle.qml | 137 | ||||
-rw-r--r-- | tests/auto/controls/data/tst_calendar.qml | 16 |
3 files changed, 72 insertions, 101 deletions
diff --git a/src/controls/Private/CalendarUtils.js b/src/controls/Private/CalendarUtils.js index c055118e..75f3b6c1 100644 --- a/src/controls/Private/CalendarUtils.js +++ b/src/controls/Private/CalendarUtils.js @@ -110,23 +110,3 @@ function cellRectAt(index, columns, rows, availableWidth, availableHeight) { return rect; } - -function cellIndexAt(x, y, columns, rows, availableWidth, availableHeight) { - var remainingHorizontalSpace = Math.floor(availableWidth % columns); - var remainingVerticalSpace = Math.floor(availableHeight % rows); - var baseCellWidth = Math.floor(availableWidth / columns); - var baseCellHeight = Math.floor(availableHeight / rows); - - // TODO: improve this. - for (var row = 0; row < rows; ++row) { - for (var col = 0; col < columns; ++col) { - var index = row * columns + col; - var rect = cellRectAt(index, columns, rows, availableWidth, availableHeight); - if (x >= rect.x && x < rect.x + rect.width && y >= rect.y && y < rect.y + rect.height) { - return index; - } - } - } - - return -1; -} diff --git a/src/controls/Styles/Base/CalendarStyle.qml b/src/controls/Styles/Base/CalendarStyle.qml index 7cfb43c1..40d03636 100644 --- a/src/controls/Styles/Base/CalendarStyle.qml +++ b/src/controls/Styles/Base/CalendarStyle.qml @@ -146,11 +146,6 @@ Style { control.__panel.availableWidth, control.__panel.availableHeight); } - function __cellIndexAt(mouseX, mouseY) { - return CalendarUtils.cellIndexAt(mouseX, mouseY, control.__panel.columns, control.__panel.rows, - control.__panel.availableWidth, control.__panel.availableHeight); - } - function __isValidDate(date) { return date !== undefined && date.getTime() >= control.minimumDate.getTime() @@ -437,67 +432,27 @@ Style { } } - Connections { - target: control - onSelectedDateChanged: view.selectedDateChanged() - } - - Repeater { - id: view - - property int currentIndex: -1 - - model: control.__model - - Component.onCompleted: selectedDateChanged() - - function selectedDateChanged() { - if (model !== undefined && model.locale !== undefined) { - currentIndex = model.indexAt(control.selectedDate); - } - } - - delegate: Loader { - id: delegateLoader - - x: __cellRectAt(index).x + (control.gridVisible ? __gridLineWidth : 0) - y: __cellRectAt(index).y + (control.gridVisible ? __gridLineWidth : 0) - width: __cellRectAt(index).width - (control.gridVisible ? __gridLineWidth : 0) - height: __cellRectAt(index).height - (control.gridVisible ? __gridLineWidth : 0) - - sourceComponent: dayDelegate - - readonly property int __index: index - readonly property date __date: date - // We rely on the fact that an invalid QDate will be converted to a Date - // whose year is -4713, which is always an invalid date since our - // earliest minimum date is the year 1. - readonly property bool valid: __isValidDate(date) - - property QtObject styleData: QtObject { - readonly property alias index: delegateLoader.__index - readonly property bool selected: control.selectedDate.getTime() === date.getTime() - readonly property alias date: delegateLoader.__date - readonly property bool valid: delegateLoader.valid - // TODO: this will not be correct if the app is running when a new day begins. - readonly property bool today: date.getTime() === new Date().setHours(0, 0, 0, 0) - readonly property bool visibleMonth: date.getMonth() === control.visibleMonth - readonly property bool hovered: panelItem.hoveredCellIndex == index - readonly property bool pressed: panelItem.pressedCellIndex == index - // todo: pressed property here, clicked and doubleClicked in the control itself - } - } - } - MouseArea { + id: mouseArea anchors.fill: parent hoverEnabled: true + function cellIndexAt(mouseX, mouseY) { + var viewContainerPos = viewContainer.mapFromItem(mouseArea, mouseX, mouseY); + var child = viewContainer.childAt(viewContainerPos.x, viewContainerPos.y); + // In the tests, the mouseArea sometimes gets picked instead of the cells, + // probably because stuff is still loading. To be safe, we check for that here. + return child && child !== mouseArea ? child.__index : -1; + } + onEntered: { - var indexOfCell = __cellIndexAt(mouseX, mouseY); - hoveredCellIndex = indexOfCell; - var date = view.model.dateAt(indexOfCell); + hoveredCellIndex = cellIndexAt(mouseX, mouseY); + if (hoveredCellIndex === undefined) { + hoveredCellIndex = cellIndexAt(mouseX, mouseY); + } + + var date = view.model.dateAt(hoveredCellIndex); if (__isValidDate(date)) { control.hovered(date); } @@ -508,7 +463,7 @@ Style { } onPositionChanged: { - var indexOfCell = __cellIndexAt(mouse.x, mouse.y); + var indexOfCell = cellIndexAt(mouse.x, mouse.y); var previousHoveredCellIndex = hoveredCellIndex; hoveredCellIndex = indexOfCell; if (indexOfCell !== -1) { @@ -527,7 +482,7 @@ Style { } onPressed: { - var indexOfCell = __cellIndexAt(mouse.x, mouse.y); + var indexOfCell = cellIndexAt(mouse.x, mouse.y); if (indexOfCell !== -1) { var date = view.model.dateAt(indexOfCell); pressedCellIndex = indexOfCell; @@ -539,7 +494,7 @@ Style { } onReleased: { - var indexOfCell = __cellIndexAt(mouse.x, mouse.y); + var indexOfCell = cellIndexAt(mouse.x, mouse.y); if (indexOfCell !== -1) { // The cell index might be valid, but the date has to be too. We could let the // selected date validation take care of this, but then the selected date would @@ -553,7 +508,7 @@ Style { } onClicked: { - var indexOfCell = __cellIndexAt(mouse.x, mouse.y); + var indexOfCell = cellIndexAt(mouse.x, mouse.y); if (indexOfCell !== -1) { var date = view.model.dateAt(indexOfCell); if (__isValidDate(date)) @@ -562,7 +517,7 @@ Style { } onDoubleClicked: { - var indexOfCell = __cellIndexAt(mouse.x, mouse.y); + var indexOfCell = cellIndexAt(mouse.x, mouse.y); if (indexOfCell !== -1) { var date = view.model.dateAt(indexOfCell); if (__isValidDate(date)) @@ -570,6 +525,58 @@ Style { } } } + + Connections { + target: control + onSelectedDateChanged: view.selectedDateChanged() + } + + Repeater { + id: view + + property int currentIndex: -1 + + model: control.__model + + Component.onCompleted: selectedDateChanged() + + function selectedDateChanged() { + if (model !== undefined && model.locale !== undefined) { + currentIndex = model.indexAt(control.selectedDate); + } + } + + delegate: Loader { + id: delegateLoader + + x: __cellRectAt(index).x + (control.gridVisible ? __gridLineWidth : 0) + y: __cellRectAt(index).y + (control.gridVisible ? __gridLineWidth : 0) + width: __cellRectAt(index).width - (control.gridVisible ? __gridLineWidth : 0) + height: __cellRectAt(index).height - (control.gridVisible ? __gridLineWidth : 0) + + sourceComponent: dayDelegate + + readonly property int __index: index + readonly property date __date: date + // We rely on the fact that an invalid QDate will be converted to a Date + // whose year is -4713, which is always an invalid date since our + // earliest minimum date is the year 1. + readonly property bool valid: __isValidDate(date) + + property QtObject styleData: QtObject { + readonly property alias index: delegateLoader.__index + readonly property bool selected: control.selectedDate.getTime() === date.getTime() + readonly property alias date: delegateLoader.__date + readonly property bool valid: delegateLoader.valid + // TODO: this will not be correct if the app is running when a new day begins. + readonly property bool today: date.getTime() === new Date().setHours(0, 0, 0, 0) + readonly property bool visibleMonth: date.getMonth() === control.visibleMonth + readonly property bool hovered: panelItem.hoveredCellIndex == index + readonly property bool pressed: panelItem.pressedCellIndex == index + // todo: pressed property here, clicked and doubleClicked in the control itself + } + } + } } } } diff --git a/tests/auto/controls/data/tst_calendar.qml b/tests/auto/controls/data/tst_calendar.qml index 2032029f..5533b909 100644 --- a/tests/auto/controls/data/tst_calendar.qml +++ b/tests/auto/controls/data/tst_calendar.qml @@ -778,21 +778,5 @@ Item { ensureNoGapsBetweenCells(columns, rows, availableWidth, availableHeight); } } - - function test_cellIndexCalculation() { - var columns = CalendarUtils.daysInAWeek; - var rows = CalendarUtils.weeksOnACalendarMonth; - - var availableWidth = 10 * columns; - var availableHeight = 10 * rows; - var rect = CalendarUtils.cellRectAt(0, columns, rows, availableWidth, availableHeight); - - for (var row = 0; row < rows; ++row) { - for (var col = 0; col < columns; ++col) { - // Test against the center of each cell. - compare(CalendarUtils.cellIndexAt(col * 10 + 5, row * 10 + 5, columns, rows, availableWidth, availableHeight), row * columns + col); - } - } - } } } |