From 8489f36322c585ec78199e6eb183000c74afae19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antti=20H=C3=B6ltt=C3=A4?= Date: Tue, 19 Feb 2019 15:36:39 +0100 Subject: Add redirect feature for manually fine tuning the cursor's movement Cursornavigation now has a property redirects, that allows defining exceptions to the navigation behaviour. A redirect allows defining a starting and an ending angle and a target object. If the move command's direction falls between the limits, the algorithm is bypassed and cursor is moved to the target object. --- plugin/cursornavigationattached.cpp | 61 +++++++++++++++++++++++++++---------- 1 file changed, 45 insertions(+), 16 deletions(-) (limited to 'plugin/cursornavigationattached.cpp') diff --git a/plugin/cursornavigationattached.cpp b/plugin/cursornavigationattached.cpp index a897327..133890e 100644 --- a/plugin/cursornavigationattached.cpp +++ b/plugin/cursornavigationattached.cpp @@ -92,10 +92,10 @@ void CursorNavigationAttached::setMagnitude(QVector2D vector) void CursorNavigationAttached::move(qreal angle, qreal tolerance) { - qWarning() << "move"; - qreal a = qDegreesToRadians(angle); - qreal t = qDegreesToRadians(qFabs(std::fmod(tolerance, 180))); if (m_cursorNavigation) { + qWarning() << "move"; + qreal a = qDegreesToRadians(angle); + qreal t = qDegreesToRadians(qFabs(std::fmod(tolerance, 180))); CursorNavigationAttached *item = m_cursorNavigation->m_currentItem; if (m_cursorNavigation->move(a, t, false) && item) item->moved(a,t); @@ -104,10 +104,10 @@ void CursorNavigationAttached::move(qreal angle, qreal tolerance) void CursorNavigationAttached::move(QVector2D vector, qreal tolerance) { - qWarning() << "move (vector)"; - qreal a = qAtan2(vector.y(), vector.x()); - qreal t = qDegreesToRadians(qFabs(std::fmod(tolerance, 180))); if (m_cursorNavigation) { + qWarning() << "move (vector)"; + qreal a = qAtan2(vector.y(), vector.x()); + qreal t = qDegreesToRadians(qFabs(std::fmod(tolerance, 180))); CursorNavigationAttached *item = m_cursorNavigation->m_currentItem; if (m_cursorNavigation->move(a, t, false) && item) item->moved(a,t); @@ -116,24 +116,20 @@ void CursorNavigationAttached::move(QVector2D vector, qreal tolerance) QQuickItem *CursorNavigationAttached::find(qreal angle, qreal tolerance) { - qreal a = qDegreesToRadians(angle); - qreal t = qDegreesToRadians(qFabs(std::fmod(tolerance, 180))); if (m_cursorNavigation) { - CursorNavigationAttached *item = m_cursorNavigation->find(a, t, false); - if (item) - return item->item(); + qreal a = qDegreesToRadians(angle); + qreal t = qDegreesToRadians(qFabs(std::fmod(tolerance, 180))); + return m_cursorNavigation->find(a, t, false); } return nullptr; } QQuickItem *CursorNavigationAttached::find(QVector2D vector, qreal tolerance) { - qreal a = qAtan2(vector.y(), vector.x()); - qreal t = qDegreesToRadians(qFabs(std::fmod(tolerance, 180))); if (m_cursorNavigation) { - CursorNavigationAttached *item = m_cursorNavigation->find(a, t, false); - if (item) - return item->item(); + qreal a = qAtan2(vector.y(), vector.x()); + qreal t = qDegreesToRadians(qFabs(std::fmod(tolerance, 180))); + return m_cursorNavigation->find(a, t, false); } return nullptr; } @@ -234,6 +230,15 @@ QQuickItem *CursorNavigationAttached::escapeTarget() const return m_escapeTarget; } +QQmlListProperty CursorNavigationAttached::redirects() +{ + return QQmlListProperty(this, this, + &CursorNavigationAttached::appendRedirect, + &CursorNavigationAttached::redirectCount, + &CursorNavigationAttached::redirect, + &CursorNavigationAttached::clearRedirects); +} + bool CursorNavigationAttached::available() const { if (m_acceptsCursor && item()->isVisible() && item()->isEnabled()) { @@ -252,3 +257,27 @@ void CursorNavigationAttached::setHasCursor(bool hasCursor) } } +void CursorNavigationAttached::appendRedirect(QQmlListProperty *property, Redirect *redirect) +{ + CursorNavigationAttached *cna = static_cast(property->object); + cna->m_redirects.append(redirect); +} + +int CursorNavigationAttached::redirectCount(QQmlListProperty *property) +{ + CursorNavigationAttached *cna = static_cast(property->object); + return cna->m_redirects.count(); +} + +Redirect *CursorNavigationAttached::redirect(QQmlListProperty *property, int index) +{ + CursorNavigationAttached *cna = static_cast(property->object); + return cna->m_redirects.at(index); +} + +void CursorNavigationAttached::clearRedirects(QQmlListProperty *property) +{ + CursorNavigationAttached *cna = static_cast(property->object); + cna->m_redirects.clear(); +} + -- cgit v1.2.3