aboutsummaryrefslogtreecommitdiffstats
path: root/plugin/cursornavigation.cpp
blob: 71ea2d852c84fd4de8d02d5f53de3d140cadd2dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/****************************************************************************
**
** Copyright (C) 2018, 2019 Luxoft Sweden AB. All rights reserved.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the cursor management module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "cursornavigation.h"
#include "cursornavigationalgorithm.h"
#include "spatialnavigation4dir.h"
#include <QQuickWindow>
#include <QQuickItem>
#include <QtMath>

const char CursorNavigation::windowPropertyName[] = "cursor_navigation";

CursorNavigation::CursorNavigation(QQuickWindow *parent)
:QObject(parent)
,m_window(parent)
,m_inputAdapter(m_window->contentItem(), this)
,m_currentItem(nullptr)
,m_rootItem(new CursorNavigationAttached(nullptr))
{
    m_rootItem->setParent(m_window->contentItem());
    m_rootItem->m_cursorNavigation = this;

    connect(m_window, &QQuickWindow::activeFocusItemChanged, this, &CursorNavigation::onActiveFocusItemChanged);
    onActiveFocusItemChanged();
}

bool CursorNavigation::move(qreal angle, qreal tolerance, bool discrete)
{
    QQuickItem *foundItem = find(angle, tolerance, discrete);
    CursorNavigationAttached *nextItem = cursorNavigationAttachment(foundItem);

    if (nextItem) {
        setCursorOnItem(nextItem);
        return true;
    } else if (foundItem) {
        foundItem->forceActiveFocus();
        return true;
    }
    return false;
}

QQuickItem *CursorNavigation::find(qreal angle, qreal tolerance, bool discrete)
{
    if (!m_currentItem)
        return defaultItem()->item();

    if (m_currentItem->m_redirects.size()) {
        for (auto redirect : m_currentItem->m_redirects) {
            if (redirect->angleIsIncluded(angle)) {
                if (!redirect->target())
                    qWarning() << "Redirect target is null";
                return redirect->target();
            }
        }
    }

    CursorNavigationAttached *nextItem = nullptr;
    CursorNavigationAttached *parent = m_currentItem ?
                m_currentItem->m_parentNavigable :
                m_rootItem;
    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 ? nextItem->item() : nullptr;
}

bool CursorNavigation::action(Action action)
{
    qWarning() << "handleActionCommand, action= " << action;
    switch (action) {
        case Forward:
        break;
        case Back:
        break;
        case Activate:
        break;
        case Escape: {
            /* if item has escapeTarget defined, set focus to that. otherwise leave
             * scope, ie. go back to parent's parent in the hierarchy and set focus
             * (back) to it (setting the focus further to one of its children
             * depends on the focus mechanism).
             * if we are already at the root item's children, nothing happens
             */
            if (!m_currentItem)
                return false;

            QQuickItem *escapeTarget = m_currentItem->m_parentNavigable->escapeTarget();
            if (!escapeTarget) {
                if (m_currentItem->m_parentNavigable == m_rootItem) {
                    return false;
                }
                escapeTarget = m_currentItem->m_parentNavigable->m_parentNavigable->item();
            }
            qWarning() << "escaping, target = " << escapeTarget;
            setCursorOnItem(nullptr);
            escapeTarget->forceActiveFocus();
            onActiveFocusItemChanged();
            //escapeTarget->setFocus(true);
            return true;
        }

        default:
        break;
    }
    return false;
}

CursorNavigationAttached *CursorNavigation::qmlAttachedProperties(QObject *object)
{
    // if the object is a window, use its contentItem instead
    if (auto win = qobject_cast<QQuickWindow *>(object)) {
        object = win->contentItem();
    }

    if (!qobject_cast<QQuickItem *>(object)) {
        qWarning("Cannot manage cursor for a non-Item!");
        return nullptr;
    }

    QQuickItem *item = static_cast<QQuickItem *>(object);

    qWarning() << "Created a new CN attachment";
    return new CursorNavigationAttached(item);
}

CursorNavigation *CursorNavigation::cursorNavigationForWindow(QQuickWindow *window)
{
    if (!window)
        return nullptr;

    const QVariant &oldCursorNavigation = window->property(windowPropertyName);
    if (!oldCursorNavigation.isNull())
        return oldCursorNavigation.value<CursorNavigation *>();

    qWarning() << "Created a new CN engine";
    CursorNavigation *cursorNavigation = new CursorNavigation(window);
    window->setProperty(windowPropertyName, QVariant::fromValue(cursorNavigation));

    //why would the context property be needed?
    /*if (QQmlEngine *engine = cn->qmlEngine(window)) {
        engine->rootContext()->setContextProperty("_cursorNavigation", cn);
    } else {
        qDebug() << "Couldn't find QQmlEngine";
    }*/

    return cursorNavigation;
}

CursorNavigationAttached *CursorNavigation::cursorNavigationAttachment(QQuickItem *item)
{
    return dynamic_cast<CursorNavigationAttached *>(qmlAttachedPropertiesObject<CursorNavigation>(item, false));
}

void CursorNavigation::setCursorOnItem(CursorNavigationAttached *item)
{
    qWarning() << "set cursor on item " << item << " , currentItem " << m_currentItem;
    if (item != m_currentItem) {
        if (m_currentItem) {
            m_currentItem->setHasCursor(false);
            //m_currentItem->item()->setFocus(false);
        }
        if (item && item->acceptsCursor()) {
            item->setHasCursor(true);
            m_currentItem = item;
            qWarning() << "Set cursor to " << item->item();
            m_currentItem->item()->forceActiveFocus();
        } else {
            qWarning() << "Set cursor to NULL";
            m_currentItem = nullptr;
        }
    }
}

void CursorNavigation::onActiveFocusItemChanged()
{
    qWarning() << "onActiveFocusItemChanged, item:" << m_window->activeFocusItem();

    QQuickItem *item = m_window->activeFocusItem();
    while (item) {
        CursorNavigationAttached *cursorNavigable = cursorNavigationAttachment(item);
        if (cursorNavigable && cursorNavigable->available()) {
            setCursorOnItem(cursorNavigable);
            return;
        }
        item = item->parentItem();
    }
    setCursorOnItem(nullptr);
}

void CursorNavigation::registerItem(CursorNavigationAttached* item)
{
    //qWarning() << "register item " << item;
    if (!item)
        return;

    //find first cursor navigable parent
    QQuickItem *parentItem = item->item()->parentItem();
    CursorNavigationAttached *parentCNA=nullptr;
    while (parentItem) {
        if ((parentCNA=CursorNavigation::cursorNavigationAttachment(parentItem)) && parentCNA->acceptsCursor())
            break;
        parentItem = parentItem->parentItem();
    }

    if (parentCNA) {
        item->m_parentNavigable=parentCNA;
        parentCNA->m_children.append(item);
    } else {
        m_rootItem->m_children.append(item);
        item->m_parentNavigable=m_rootItem;
    }

    /* TODO fix unregistering and reregistering navigable items with navigable children
     *
     * currently, for est results, items should define if they accpet cursor already
     * at the construction time. this will ensure that the tree is properly constructed
     * currently, unregisterin a container, is going to unregister all of its
     * navigable children too. reregisterin gthe container will reregister the children
     * regitering a container after its navigable children have been registered,
     * will not list the children under the container but as its siblings
     */

    /* TODO reparenting from window to another kind of works, but reparenting
    * within a window not so well. problems arise when reaprenting items
    * contained in an item that is also navigable. for this to work, we would
    * need to detect all parent changes, including items non navigable parents
    * and grandparents, and reorganize the internal tree when needed.
    * For now, consider reparenting as not recommended
    */
}

void CursorNavigation::unregisterItem(CursorNavigationAttached* item)
{
    //qWarning() << "unregister item " << item;
    if (item == m_currentItem)
        setCursorOnItem(nullptr);

    if (item->m_parentNavigable)
        item->m_parentNavigable->m_children.removeOne(item);

}

CursorNavigationAttached *CursorNavigation::defaultItem()
{
    if (m_rootItem->m_children.size())
        return m_rootItem->m_children.first();
    return nullptr;
}