summaryrefslogtreecommitdiffstats
path: root/plugin
diff options
context:
space:
mode:
authorAntti Hölttä <AHoelttae@luxoft.com>2018-12-05 15:41:52 +0100
committerAntti Hölttä <AHoelttae@luxoft.com>2019-03-18 16:34:23 +0100
commit726f0bca0d50a86ff30a367cbdd2f31190fcd30c (patch)
treefd04bf5d92f2f25d317a12af5f29a47d986f6062 /plugin
parentff7c7152388889da7d603b207e575e366873186d (diff)
Add callbacks for magnitude, moves and actions
When cursor is moved, the current item is informed of the actions via callbacks. Eg. when cursor is moved up, the item that had the cursor will have its movedUp signal fired. It is now also possible to inform navigable items over incomplete movement, or the magnitude of the movement. This may be used eg. to make buttons or elements to flip/rotate to indicate that the cursor is about to be moved.
Diffstat (limited to 'plugin')
-rw-r--r--plugin/cursornavigation.cpp113
-rw-r--r--plugin/cursornavigation.h12
-rw-r--r--plugin/cursornavigationattached.cpp102
-rw-r--r--plugin/cursornavigationattached.h21
-rw-r--r--plugin/inputadapter.cpp36
5 files changed, 153 insertions, 131 deletions
diff --git a/plugin/cursornavigation.cpp b/plugin/cursornavigation.cpp
index 155a158..0a09ade 100644
--- a/plugin/cursornavigation.cpp
+++ b/plugin/cursornavigation.cpp
@@ -3,7 +3,6 @@
#include "spatialnavigation4dir.h"
#include <QQuickWindow>
#include <QQuickItem>
-#include <QtMath>
const char CursorNavigation::windowPropertyName[] = "cursor_navigation";
@@ -15,44 +14,60 @@ CursorNavigation::CursorNavigation(QQuickWindow *parent)
,m_rootItem(new CursorNavigationAttached(nullptr))
{
m_rootItem->setParent(m_window->contentItem());
-
- //m_algorithms.push_back(new SpatialNavigation4Dir());
+ m_rootItem->m_cursorNavigation = this;
connect(m_window, &QQuickWindow::activeFocusItemChanged, this, &CursorNavigation::onActiveFocusItemChanged);
onActiveFocusItemChanged();
}
-void CursorNavigation::move(qreal angle, qreal tolerance, bool discrete)
+bool CursorNavigation::move(qreal angle, qreal tolerance, bool discrete)
{
- qreal a = qDegreesToRadians(angle);
- qreal t = qDegreesToRadians(qFabs(std::fmod(tolerance, 180)));
- _move(a, t, discrete);
-}
+ CursorNavigationAttached *nextItem = find(angle, tolerance, discrete);
-void CursorNavigation::move(const QVector2D& vector, qreal tolerance, bool discrete)
-{
- qreal a = qAtan2(vector.y(), vector.x());
- qreal t = qDegreesToRadians(qFabs(std::fmod(tolerance, 180)));
- _move(a, t, discrete);
+ if (nextItem) {
+ setCursorOnItem(nextItem);
+ return true;
+ }
+ return false;
}
CursorNavigationAttached *CursorNavigation::find(qreal angle, qreal tolerance, bool discrete)
{
- qreal a = qDegreesToRadians(angle);
- qreal t = qDegreesToRadians(qFabs(std::fmod(tolerance, 180)));
+ CursorNavigationAttached *nextItem = nullptr;
+ CursorNavigationAttached *parent = m_currentItem ?
+ m_currentItem->m_parentNavigable :
+ m_rootItem;
- return _find(a,t,discrete);
-}
+ if (!m_currentItem)
+ return defaultItem();
-CursorNavigationAttached *CursorNavigation::find(const QVector2D& vector, qreal tolerance, bool discrete)
-{
- qreal a = qAtan2(vector.y(), vector.x());
- qreal t = qDegreesToRadians(qFabs(std::fmod(tolerance, 180)));
+ qWarning() << "find next item, angle = " << angle << " tolerance = " << tolerance << " discrete = " << discrete;
+
+ QList<CursorNavigationAttached*> candidates;
+
+ do {
+ candidates.append(parent->m_children);
- return _find(a,t,discrete);
+ if (parent->trapsCursor())
+ break;
+ parent = parent->m_parentNavigable;
+ } while (parent);
+
+ if (candidates.isEmpty())
+ return nullptr;
+
+ CursorNavigationCommand cmd(angle, tolerance);
+
+ if (discrete) {
+ nextItem = m_navigation4Dir.getNextCandidate(candidates, m_currentItem, cmd);
+ } else {
+ nextItem = m_navigation360.getNextCandidate(candidates, m_currentItem, cmd);
+ }
+
+ return nextItem;
}
-void CursorNavigation::action(Action action)
+bool CursorNavigation::action(Action action)
{
qWarning() << "handleActionCommand, action= " << action;
switch (action) {
@@ -70,12 +85,12 @@ void CursorNavigation::action(Action action)
* if we are already at the root item's children, nothing happens
*/
if (!m_currentItem)
- break;
+ return false;
QQuickItem *escapeTarget = m_currentItem->m_parentNavigable->escapeTarget();
if (!escapeTarget) {
if (m_currentItem->m_parentNavigable == m_rootItem) {
- break;
+ return false;
}
escapeTarget = m_currentItem->m_parentNavigable->m_parentNavigable->item();
}
@@ -84,12 +99,13 @@ void CursorNavigation::action(Action action)
escapeTarget->forceActiveFocus();
onActiveFocusItemChanged();
//escapeTarget->setFocus(true);
- break;
+ return true;
}
default:
break;
}
+ return false;
}
CursorNavigationAttached *CursorNavigation::qmlAttachedProperties(QObject *object)
@@ -202,51 +218,6 @@ void CursorNavigation::unregisterItem(CursorNavigationAttached* item)
//TODO if the item that is being unregistered has children, they should be reassigned to the item's parent
}
-void CursorNavigation::_move(qreal angle, qreal tolerance, bool discrete)
-{
- CursorNavigationAttached *nextItem = _find(angle, tolerance, discrete);
-
- if (nextItem) {
- setCursorOnItem(nextItem);
- }
-}
-
-CursorNavigationAttached *CursorNavigation::_find(qreal angle, qreal tolerance, bool discrete)
-{
- CursorNavigationAttached *nextItem = nullptr;
- CursorNavigationAttached *parent = m_currentItem ?
- m_currentItem->m_parentNavigable :
- m_rootItem;
-
- if (!m_currentItem)
- return defaultItem();
-
- qWarning() << "find next item, angle = " << angle << " tolerance = " << tolerance << " discrete = " << discrete;
-
- QList<CursorNavigationAttached*> candidates;
-
- do {
- candidates.append(parent->m_children);
-
- if (parent->trapsCursor())
- break;
- parent = parent->m_parentNavigable;
- } while (parent);
-
- if (candidates.isEmpty())
- return nullptr;
-
- CursorNavigationCommand cmd(angle, tolerance);
-
- if (discrete) {
- nextItem = m_navigation4Dir.getNextCandidate(candidates, m_currentItem, cmd);
- } else {
- nextItem = m_navigation360.getNextCandidate(candidates, m_currentItem, cmd);
- }
-
- return nextItem;
-}
-
CursorNavigationAttached *CursorNavigation::defaultItem()
{
if (m_rootItem->m_children.size())
diff --git a/plugin/cursornavigation.h b/plugin/cursornavigation.h
index f3a6f91..a32a357 100644
--- a/plugin/cursornavigation.h
+++ b/plugin/cursornavigation.h
@@ -20,13 +20,16 @@ class CursorNavigation : public QObject
public:
CursorNavigation(QQuickWindow *parent);
+ //pass input events forward to the current item
+ //void setMagnitude(qreal angle, qreal magnitude);
+ //void setMagnitude(const QVector2D& vector);
//move the cursor
- void move(qreal angle, qreal tolerance, bool discrete);
- void move(const QVector2D& vector, qreal tolerance, bool discrete);
+ bool move(qreal angle, qreal tolerance, bool discrete);
+ bool move(const QVector2D& vector, qreal tolerance, bool discrete);
//find the next item without moving the cursor
CursorNavigationAttached *find(qreal angle, qreal tolerance, bool discrete);
CursorNavigationAttached *find(const QVector2D& vector, qreal tolerance, bool discrete);
- void action(Action action);
+ bool action(Action action);
static CursorNavigationAttached *qmlAttachedProperties(QObject *object);
@@ -40,8 +43,6 @@ private:
void registerItem(CursorNavigationAttached* item);
void unregisterItem(CursorNavigationAttached* item);
- void _move(qreal angle, qreal tolerance, bool discrete);
- CursorNavigationAttached *_find(qreal angle, qreal tolerance, bool discrete);
CursorNavigationAttached *defaultItem();
private:
@@ -55,6 +56,7 @@ private:
CursorNavigationAttached *m_rootItem;
friend class CursorNavigationAttached;
+ friend class InputAdapter;
};
QML_DECLARE_TYPEINFO(CursorNavigation, QML_HAS_ATTACHED_PROPERTIES)
diff --git a/plugin/cursornavigationattached.cpp b/plugin/cursornavigationattached.cpp
index 2581920..ffa1b93 100644
--- a/plugin/cursornavigationattached.cpp
+++ b/plugin/cursornavigationattached.cpp
@@ -2,6 +2,7 @@
#include "cursornavigation.h"
#include <QQuickItem>
#include <QQuickWindow>
+#include <QtMath>
CursorNavigationAttached::CursorNavigationAttached(QQuickItem *parent)
:QObject(parent),
@@ -76,24 +77,49 @@ void CursorNavigationAttached::setEscapeTarget(QQuickItem *escapeTarget)
emit escapeTargetChanged(m_escapeTarget);
}
+void CursorNavigationAttached::setMagnitude(qreal angle, qreal magnitude)
+{
+ if (m_cursorNavigation && m_cursorNavigation->m_currentItem)
+ m_cursorNavigation->m_currentItem->magnitudeChanged(angle, magnitude);
+}
+
+void CursorNavigationAttached::setMagnitude(QVector2D vector)
+{
+ if (m_cursorNavigation && m_cursorNavigation->m_currentItem)
+ m_cursorNavigation->m_currentItem->magnitudeChanged(
+ qRadiansToDegrees(qAtan2(vector.y(), vector.x())), vector.length());
+}
+
void CursorNavigationAttached::move(qreal angle, qreal tolerance)
{
qWarning() << "move";
- if (m_cursorNavigation)
- m_cursorNavigation->move(angle, tolerance, false);
+ qreal a = qDegreesToRadians(angle);
+ qreal t = qDegreesToRadians(qFabs(std::fmod(tolerance, 180)));
+ if (m_cursorNavigation) {
+ CursorNavigationAttached *item = m_cursorNavigation->m_currentItem;
+ if (m_cursorNavigation->move(a, t, false) && item)
+ item->moved(a,t);
+ }
}
void CursorNavigationAttached::move(QVector2D vector, qreal tolerance)
{
- qWarning() << "move";
- if (m_cursorNavigation)
- m_cursorNavigation->move(vector, tolerance, false);
+ qWarning() << "move (vector)";
+ qreal a = qAtan2(vector.y(), vector.x());
+ qreal t = qDegreesToRadians(qFabs(std::fmod(tolerance, 180)));
+ if (m_cursorNavigation) {
+ CursorNavigationAttached *item = m_cursorNavigation->m_currentItem;
+ if (m_cursorNavigation->move(a, t, false) && item)
+ item->moved(a,t);
+ }
}
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(angle, tolerance, false);
+ CursorNavigationAttached *item = m_cursorNavigation->find(a, t, false);
if (item)
return item->item();
}
@@ -102,8 +128,10 @@ QQuickItem *CursorNavigationAttached::find(qreal angle, qreal tolerance)
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(vector, tolerance, false);
+ CursorNavigationAttached *item = m_cursorNavigation->find(a, t, false);
if (item)
return item->item();
}
@@ -112,50 +140,74 @@ QQuickItem *CursorNavigationAttached::find(QVector2D vector, qreal tolerance)
void CursorNavigationAttached::moveUp()
{
- if (m_cursorNavigation)
- m_cursorNavigation->move(-90, 0, true);
+ if (m_cursorNavigation) {
+ CursorNavigationAttached *item = m_cursorNavigation->m_currentItem;
+ if (m_cursorNavigation->move(qDegreesToRadians(-90.0f), 0, true) && item)
+ item->movedUp();
+ }
}
void CursorNavigationAttached::moveDown()
{
- if (m_cursorNavigation)
- m_cursorNavigation->move(90, 0, true);
+ if (m_cursorNavigation) {
+ CursorNavigationAttached *item = m_cursorNavigation->m_currentItem;
+ if (m_cursorNavigation->move(qDegreesToRadians(90.0f), 0, true) && item)
+ item->movedDown();
+ }
}
void CursorNavigationAttached::moveRight()
{
- if (m_cursorNavigation)
- m_cursorNavigation->move(0, 0, true);
+ if (m_cursorNavigation) {
+ CursorNavigationAttached *item = m_cursorNavigation->m_currentItem;
+ if (m_cursorNavigation->move(qDegreesToRadians(0.0f), 0, true) && item)
+ item->movedRight();
+ }
}
void CursorNavigationAttached::moveLeft()
{
- if (m_cursorNavigation)
- m_cursorNavigation->move(180, 0, true);
+ if (m_cursorNavigation) {
+ CursorNavigationAttached *item = m_cursorNavigation->m_currentItem;
+ if (m_cursorNavigation->move(qDegreesToRadians(180.0f), 0, true) && item)
+ item->movedLeft();
+ }
}
void CursorNavigationAttached::activate()
{
- if (m_cursorNavigation)
- m_cursorNavigation->action(Activate);
+ if (m_cursorNavigation) {
+ CursorNavigationAttached *item = m_cursorNavigation->m_currentItem;
+ if (m_cursorNavigation->action(Activate) && item)
+ item->activated();
+ }
}
-void CursorNavigationAttached::forward()
+void CursorNavigationAttached::moveForward()
{
- if (m_cursorNavigation)
- m_cursorNavigation->action(Forward);
+ if (m_cursorNavigation) {
+ CursorNavigationAttached *item = m_cursorNavigation->m_currentItem;
+ if (m_cursorNavigation->action(Forward) && item)
+ item->movedForward();
+ }
}
-void CursorNavigationAttached::back()
+void CursorNavigationAttached::moveBack()
{
- if (m_cursorNavigation)
- m_cursorNavigation->action(Back);
+ if (m_cursorNavigation) {
+ CursorNavigationAttached *item = m_cursorNavigation->m_currentItem;
+ if (m_cursorNavigation->action(Back) && item)
+ item->movedBack();
+ }
}
void CursorNavigationAttached::escape()
{
- if (m_cursorNavigation)
- m_cursorNavigation->action(Escape);
+ if (m_cursorNavigation) {
+ CursorNavigationAttached *item = m_cursorNavigation->m_currentItem;
+ if (m_cursorNavigation->action(Escape) && item)
+ item->escaped();
+ }
}
void CursorNavigationAttached::onWindowChanged(QQuickWindow *window)
diff --git a/plugin/cursornavigationattached.h b/plugin/cursornavigationattached.h
index 1409a93..f3c729d 100644
--- a/plugin/cursornavigationattached.h
+++ b/plugin/cursornavigationattached.h
@@ -45,6 +45,12 @@ public slots:
void setTrapsCursor(bool trapsCursor);
void setEscapeTarget(QQuickItem * escapeTarget);
+ /* just for passing movement changes forward. does not move the cursor.
+ * events are received by the item that currently has the cursor
+ */
+ void setMagnitude(qreal angle, qreal magnitude);
+ void setMagnitude(QVector2D vector);
+
void move(qreal angle, qreal tolerance = 0);
void move(QVector2D vector, qreal tolerance = 0);
@@ -57,8 +63,8 @@ public slots:
void moveRight();
void moveLeft();
void activate();
- void forward();
- void back();
+ void moveForward();
+ void moveBack();
void escape();
@@ -69,6 +75,17 @@ signals:
void escapeTargetChanged(QQuickItem * escapeTarget);
+ void magnitudeChanged(qreal angle, qreal magnitude);
+ void moved(qreal angle, qreal tolerance);
+ void movedUp();
+ void movedDown();
+ void movedRight();
+ void movedLeft();
+ void activated();
+ void movedForward();
+ void movedBack();
+ void escaped();
+
private slots:
void onWindowChanged(QQuickWindow *window);
diff --git a/plugin/inputadapter.cpp b/plugin/inputadapter.cpp
index feb1373..52eba34 100644
--- a/plugin/inputadapter.cpp
+++ b/plugin/inputadapter.cpp
@@ -21,47 +21,39 @@ bool InputAdapter::eventFilter(QObject *object, QEvent *event)
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
return handleKeyEvent(keyEvent);
- } else if (event->type() == QEvent::Wheel) {
- QWheelEvent *wheelEvent = static_cast<QWheelEvent *>(event);
- return handleWheelEvent(wheelEvent);
- } else if (event->type() == QEvent::MouseMove) {
- QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
- return handleMouseEvent(mouseEvent);
}
return false;
}
bool InputAdapter::handleKeyEvent(QKeyEvent *event)
{
- CursorNavigationCommand cmd;
- //detect arrow keys, tabs, enter and esc
switch (event->key())
{
case Qt::Key_Left:
- m_cursorNavigation->move(180, 0, true);
+ m_cursorNavigation->m_rootItem->moveLeft();
break;
case Qt::Key_Right:
- m_cursorNavigation->move(0, 0, true);
+ m_cursorNavigation->m_rootItem->moveRight();
break;
case Qt::Key_Up:
- m_cursorNavigation->move(-90, 0, true);
+ m_cursorNavigation->m_rootItem->moveUp();
break;
case Qt::Key_Down:
- m_cursorNavigation->move(90, 0, true);
+ m_cursorNavigation->m_rootItem->moveDown();
break;
case Qt::Key_Return:
case Qt::Key_Enter:
- m_cursorNavigation->action(Activate);
+ m_cursorNavigation->m_rootItem->activate();
break;
case Qt::BackButton:
case Qt::Key_Escape:
- m_cursorNavigation->action(Escape);
+ m_cursorNavigation->m_rootItem->escape();
break;
case Qt::Key_Tab:
- m_cursorNavigation->action(Forward);
+ m_cursorNavigation->m_rootItem->moveForward();
break;
case Qt::Key_Backtab:
- m_cursorNavigation->action(Back);
+ m_cursorNavigation->m_rootItem->moveBack();
break;
default:
return false;
@@ -69,15 +61,3 @@ bool InputAdapter::handleKeyEvent(QKeyEvent *event)
return true;
}
-
-bool InputAdapter::handleMouseEvent(QMouseEvent *event)
-{
- //interpret mouse movement as omnnidirectional joystick movements for testing purposes
- return false;
-}
-
-bool InputAdapter::handleWheelEvent(QWheelEvent *event)
-{
- //turn wheel events into tabs
- return false;
-}