From d04c7121acf66af75f557147ce8d055c62262608 Mon Sep 17 00:00:00 2001 From: Mitch Curtis Date: Thu, 7 Nov 2019 12:15:54 +0100 Subject: SplitView: refactor hover handling code - Don't unset the hovered flag only to potentially set it again. - Generally simplify the code. - Move it into a new updateHoveredHandle() function so that follow up patches can call it from other places. - Add more logging to debug hover issues. Change-Id: Iaf06cfe1f556a3f30bd0e883ef504b3df2dbc8e2 Reviewed-by: Richard Moe Gustavsen --- src/quicktemplates2/qquicksplitview.cpp | 77 ++++++++++++++----------------- src/quicktemplates2/qquicksplitview_p_p.h | 1 + 2 files changed, 36 insertions(+), 42 deletions(-) diff --git a/src/quicktemplates2/qquicksplitview.cpp b/src/quicktemplates2/qquicksplitview.cpp index 56392e9a..ba7644a1 100644 --- a/src/quicktemplates2/qquicksplitview.cpp +++ b/src/quicktemplates2/qquicksplitview.cpp @@ -909,6 +909,40 @@ void QQuickSplitViewPrivate::updateHandleVisibilities() } } +void QQuickSplitViewPrivate::updateHoveredHandle(QQuickItem *hoveredItem) +{ + Q_Q(QQuickSplitView); + const int oldHoveredHandleIndex = m_hoveredHandleIndex; + m_hoveredHandleIndex = m_handleItems.indexOf(hoveredItem); + if (m_hoveredHandleIndex == oldHoveredHandleIndex) + return; + + // First, clear the hovered flag of any previously-hovered handle. + if (oldHoveredHandleIndex != -1) { + QQuickItem *oldHoveredHandle = m_handleItems.at(oldHoveredHandleIndex); + QQuickSplitHandleAttached *oldHoveredHandleAttached = qobject_cast( + qmlAttachedPropertiesObject(oldHoveredHandle, true)); + QQuickSplitHandleAttachedPrivate::get(oldHoveredHandleAttached)->setHovered(false); + qCDebug(qlcQQuickSplitViewMouse) << "handle item at index" << oldHoveredHandleIndex << "is no longer hovered"; + } + + if (m_hoveredHandleIndex != -1) { + QQuickSplitHandleAttached *handleAttached = qobject_cast( + qmlAttachedPropertiesObject(hoveredItem, true)); + QQuickSplitHandleAttachedPrivate::get(handleAttached)->setHovered(true); + qCDebug(qlcQQuickSplitViewMouse) << "handle item at index" << m_hoveredHandleIndex << "is now hovered"; + } else { + qCDebug(qlcQQuickSplitViewMouse) << "either there is no hovered item or" << hoveredItem << "is not a handle"; + } + +#if QT_CONFIG(cursor) + if (m_hoveredHandleIndex != -1) + q->setCursor(m_orientation == Qt::Horizontal ? Qt::SplitHCursor : Qt::SplitVCursor); + else + q->setCursor(Qt::ArrowCursor); +#endif +} + void QQuickSplitViewPrivate::setResizing(bool resizing) { Q_Q(QQuickSplitView); @@ -1327,48 +1361,7 @@ void QQuickSplitView::hoverMoveEvent(QHoverEvent *event) QQuickContainer::hoverMoveEvent(event); QQuickItem *hoveredItem = childAt(event->pos().x(), event->pos().y()); - if (!hoveredItem) { - // No handle is hovered. - if (d->m_hoveredHandleIndex != -1) { - // The previously-hovered handle is no longer hovered. - QQuickItem *oldHoveredHandle = d->m_handleItems.at(d->m_hoveredHandleIndex); - QQuickSplitHandleAttached *handleAttached = qobject_cast( - qmlAttachedPropertiesObject(oldHoveredHandle, true)); - QQuickSplitHandleAttachedPrivate::get(handleAttached)->setHovered(false); - } - - qCDebug(qlcQQuickSplitViewMouse) << "handle item at index" << d->m_hoveredHandleIndex << "is no longer hovered"; - - d->m_hoveredHandleIndex = -1; - } else { - // A child item of ours is hovered. - - // First, clear the hovered flag of any previously-hovered handle. - if (d->m_hoveredHandleIndex != -1) { - QQuickItem *oldHoveredHandle = d->m_handleItems.at(d->m_hoveredHandleIndex); - QQuickSplitHandleAttached *handleAttached = qobject_cast( - qmlAttachedPropertiesObject(oldHoveredHandle, true)); - QQuickSplitHandleAttachedPrivate::get(handleAttached)->setHovered(false); - } - - // Now check if the newly hovered item is actually a handle. - d->m_hoveredHandleIndex = d->m_handleItems.indexOf(hoveredItem); - - if (d->m_hoveredHandleIndex != -1) { - QQuickSplitHandleAttached *handleAttached = qobject_cast( - qmlAttachedPropertiesObject(hoveredItem, true)); - QQuickSplitHandleAttachedPrivate::get(handleAttached)->setHovered(true); - - qCDebug(qlcQQuickSplitViewMouse) << "handle item at index" << d->m_hoveredHandleIndex << "is now hovered"; - } - } - -#if QT_CONFIG(cursor) - if (d->m_hoveredHandleIndex != -1) - setCursor(d->m_orientation == Qt::Horizontal ? Qt::SplitHCursor : Qt::SplitVCursor); - else - setCursor(Qt::ArrowCursor); -#endif + d->updateHoveredHandle(hoveredItem); } void QQuickSplitView::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) diff --git a/src/quicktemplates2/qquicksplitview_p_p.h b/src/quicktemplates2/qquicksplitview_p_p.h index 5d71d461..ccefe5ec 100644 --- a/src/quicktemplates2/qquicksplitview_p_p.h +++ b/src/quicktemplates2/qquicksplitview_p_p.h @@ -74,6 +74,7 @@ public: void resizeHandle(QQuickItem *handleItem); void resizeHandles(); void updateHandleVisibilities(); + void updateHoveredHandle(QQuickItem *hoveredItem); void setResizing(bool resizing); bool isHorizontal() const; -- cgit v1.2.3