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 /src | |
| parent | fa001b41c67784c97110452c5800bef960825264 (diff) | |
Use childAt() instead of cellIndexAt().
Change-Id: I74ee716620d31363bccaade89d51aa23d3b39830
Reviewed-by: Gabriel de Dietrich <gabriel.dedietrich@digia.com>
Diffstat (limited to 'src')
| -rw-r--r-- | src/controls/Private/CalendarUtils.js | 20 | ||||
| -rw-r--r-- | src/controls/Styles/Base/CalendarStyle.qml | 137 |
2 files changed, 72 insertions, 85 deletions
diff --git a/src/controls/Private/CalendarUtils.js b/src/controls/Private/CalendarUtils.js index c055118e5..75f3b6c17 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 7cfb43c1b..40d03636a 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 + } + } + } } } } |
